2 Replies Latest reply on Apr 19, 2010 11:53 PM by langzifan

    New way to do Dynamic Icons in Flex

    norrisoft Level 1

      Hi All,

       

      I figured out another way to make dynamic icons in Flex:

       

      Flex Code:

       

      In the container mxml:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"
      paddingLeft="2" paddingRight="2" horizontalGap="2">
      <mx:Script>
          <![CDATA[
         import mx.core.Application;
        [Bindable]
        [Embed(source="common/swf/dynamicIcon.swf") ]
        public var isDynamicIcon:Class;
              ]]>
      </mx:Script>
      <mx:Image id="ico_mag_sm" source="{isDynamicIcon}" />
      <mx:Spacer width="1"/>
      <mx:Text id="_name" text="{data.ApplicationName}"
      paddingLeft="5" selectable="false"/>       
      </mx:HBox>

       

      In the Applicaton mxml:

       

      <mx:Script>
          <![CDATA[
          i// ActionScript file
              import inc.ApplicationProperties;
              //NOTE: do NOT type the commonPath variable. Typing it to String breaks the code.
               public var commonPath = ApplicationProperties.COMMON_PATH;//this is the url to the server where the icons are located.

       

              ]]>
          </mx:Script>

       

      dynamicIcon.swf AS 3.0 code:

       

      stop();
      var iconFolder:String = "inc/icons/";
      var  parentArray:Array = new Array();

      //find the highest display container
      function getIconName(myObject){
            while (myObject.parent){
               myObject = myObject.parent;
                var topParent = myObject;
               var objectName:String =  myObject.name;
               parentArray.push(objectName);
           }

          // for some reason I can't just set the iconName equal to the myObject.name. The only way I could get it to work is to push

         //it onto an array first and then access it in the array.
            for(var i:Number=0;i<parentArray.length; i++){
                if(parentArray[i].indexOf("ico_")==0){
                   var  iconName:String = parentArray[i];
                   break;
               }
            }
           var myURL:String =  topParent.getChildAt(0).application["commonPath"];
           var  loadPath:String = myURL+iconFolder+iconName+".png";
           var loadit =  new Loader();
           addChild(loadit);
           loadit.load(new  URLRequest(loadPath));
      }
      getIconName(this.parent);

       

      BUT, I am new to Flex and AS 3.0 and I know this code is uglier than it needs to be and have the following two issues.

       

      1) In the AS 3.0 code, to determine the parent application, I should be able to just reference the parent application directly rather than have to iterate through .parent.  Since I am using the Image tag and not using a loader, the only way I could figure out how is to iterate thought .parent. Is there a more direct way to get the parent or more specifically, the COMMON_PATH attribute of the ApplicationProperties class?

       

      2) In the AS 3.0 code, for some reason, I could not just stick the retrieved myObject.name in into the iconName variable and add it to the URL string. Just out of desperation, I pushed it onto an array first and then accessed it and it worked.

       

      Anyone got any ideas on either or both item 1 or 2 above?

       

      TIA,

       

      Steve