ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Create Skinnable Application Using Flex + AIR
http://www.actionscript.org/resources/articles/834/1/Create-Skinnable-Application-Using-Flex--AIR/Page1.html
Chintan Buch
here I m – Chintan Buch from India. I m working as Interactive Media Developer (its a bit longer, plz bear with it) in AOL. probably few people want to know @ my AS exp, so I've to tell u that this is not what I was earlier! started my career with Network Admin with the passion of Digital Art & Animation. I remember it was Flash4 when I was playing with it.. was doing animation stuffs during my college time (yes I was getting paid). after couple of yrs (precisely 3), some1 decided to gamble on me & from that day, I was officially a part of AS community. most of my work is in de area of intranet/remoting, unbelievable huh! oh yes I remember, I did couple of projects with different social networking sites as well ;-)

these days I m playing with Flex & AIR, & you know what!? I m pretty much in good shape (although I make silly mistakes most of the time, but m newbie... ;-).

yes I’ve my site globulous.net, but don’t expect anything there ;-) only my R&D projects & few personal files will be there (don’t try to search for those links, you won’t find it) hopefully I’ll design my site someday & add all favorite things there... but till that time u can reach me through email if u need any help from me : chintanbuch <at> gmail (dot) com. btw don’t try with exact text, change <at> with @ & (dot) with “.” else it’ll bounce back to you ;-)

 
By Chintan Buch
Published on November 16, 2008
 

This article will cover some cool features of the AIR (Adobe Integrated Run-time) like custom chrome & file system access. It requires some basic knowledge of event handling & XML parsing.

Article gives you a fair idea on creating a custom shape window application (any odd shape window), change the look & feel of the application in run-time & saving configuration data in a file (here we are using XML format).


Ok so we start with the “main-app.xml” file. This is the AIR specific configuration file, where we can change the Application specific parameters like:

  1. Application Name
  2. Version
  3. Description etc...

But we’ll directly move to the following settings which allow us to change the windows related settings. When dealing with custom chrome window, following settings need to keep in mind.

  1. SystemChrome
    • For creating skinnable application, this parameter should be “none”
  2. Transparent
    • This parameter should be “true”

Initially in the configuration file, both the properties (1 & 2) may be commented, so you need to remove the comment for these parameters & then set their values.

 

There may be a possibility that our Application doesn’t require resizing of the window (because of the odd shape). So we can remove the resizing option by changing the “resizable” parameter value to “false”.

 

Ok that’s all we need to change in the configuration file.

 

Now we can move to “main.mxml” file. This is our main UI where we want to set the Application skin. Here we need to set the following properties.

 

  1. borderStyle = “none”
    • By setting this value, window won’t show the border around it.
  2. backgroundAlpha = “0”
    • This will make windows background transparent. This’ll be helpful when you place an transparent image (PNG file with alpha transparency) on the main Application interface.
  3. showTitleBar = “false”
  4. showStatusBar = “false”
    • By default Title Window & Status Bar display is true (Even if the background is transparent, because default value is true). We need to set these values to false.
  5. horizontalScrollPolicy = “off”
  6. vertical ScrollPolicy = “off”

<mx:WindowedApplication

      xmlns:mx="http://www.adobe.com/2006/mxml"

      width="350" height="350" layout="absolute"

      borderStyle="none" backgroundAlpha="0"

      showTitleBar="false" showStatusBar="false"

      horizontalScrollPolicy="off" verticalScrollPolicy="off"

      initialize="{evntAppInit()}"

      applicationComplete="{evntAppComplete()}"

> 

 


Next step is to add an Image component from the component library & set its ID to “appSkinImg” from the Flex Properties / Source View, whichever you are comfortable with.

This is the main control which serves as a skin for our application.

 

Now add 3 Button components. We are using these buttons to change our application skin in runtime & store it in the config file. This is just to provide a high level idea; real time scenario may be differing for different Applications.

 


 

Now let’s get into the Script part of this Application.

 

I’ve split this part into three different categories:

·         Application Event(s)

·         Component Event(s)

·         Methods to Load / Update Skin Data

 

Application Event(s):

-          Every time when Application starts, we need to load the skin specific data from the configuration file (Not the AIR configuration file, which we discussed above) to change the look & feel on the start-up of our Application. Same file will be used to store the skin details, when user changes the skin.  In “initialize” event we are calling the “evntAppInit()” method which loads the data. We can directly call the loadConfig() method, but in case if we want to do some additional processes on initialize event, it is better to go with this approach.

 

-          We are using Window event, named “applicationComplete” to get the reference of our main Application Window. You can find more info on this event in Flex help.

 

      private function evntAppInit():void

      {

            loadConfig();

      }


private function evntAppComplete():void

      {

            // reference for the main window

            thisWin = this.stage.nativeWindow;

      }

 

We are using “thisWin” variable to reference the main Application window to perform different operations. For this application scope we’ll use it for moving the Application when user start dragging by mouse clicking on the main interface of the Application & close the Application when user double click on the main interface.

Component Event(s):

-          There are three events, from which two events for Image component & one event is for Buttons (for changing the Application skin & store it in the configuration file).

 

      private function evntMoveMe():void

      {

            thisWin.startMove();

      }

     

      private function evntCloseMe():void

      {

            thisWin.close();

      }

     

      private function evntChangeSkin(event:MouseEvent):void

      {

            var tmpObj:Button = event.currentTarget as Button;

            // changing the skin value

            configData.skin = tmpObj.label;

            // change the application look with the new skin

            changeSkin(tmpObj.label);

            // update config file

            updateConfig(tmpObj.label);

      }

 

-          “startMove()” method is used to move the main Application window. We are calling this method on the “mouseDown” event of the image component (evntMoveMe()). Moving process of the window will be automatically ended on “mouseUp” event, so we don’t need to declare that event.

 

-          “close()” method is used to close the window. This method unloads the contents of the window; basically it’ll close the Application. (You may need to read more in AIR documentation, in-case your Application is dealing with multiple windows).

o   In this Application, we are using this method on the “doubleClick” event of the image component (evntCloseMe()), so when user double clicks on the Application interface (internally its image which is dispatching this event) we are closing the Application.

 

-          evntChangeSkin()” method is used to change the skin. It is attached with click event of all buttons. Method will change the skin as well as store it in the configuration file.

o   Method first changes the value in XML data object.

o   Then it’ll change the source of the Image component, from the resource folder, using “changeSkin()” method.

o   Finally it’ll update the file with this updated skin data.


Methods to Load / Update Skin Data:

-          There are two methods “loadConfig()” & “updateConfig()”, both methods require File operation.  These methods are using “File” & “FileStream” objects.

o   “File” object creates a pointer to the configuration file & “FileStream” object is used to open configuration file for reading or writing the data.

 

      // load configuration data from XML file

private function loadConfig():void

{

      // resolve the file path of config file

      configFile = File.applicationStorageDirectory.resolvePath("config.xml");

      // incase file doesn't exist copy to storage directory

      if(!configFile.exists)

      {

            configFile = File.applicationDirectory.resolvePath("config.xml");

            configFile.copyTo(File.applicationStorageDirectory.resolvePath("config.xml"));

      }

      // create a file stream object to read config file

      fStrm = new FileStream();

      // read config

      fStrm.open(configFile, FileMode.READ);

      // getting XML content

      configData = XML(fStrm.readUTFBytes(fStrm.bytesAvailable));

      // change skin

      changeSkin(configData.skin);

      // close file

      fStrm.close();

}

           

// save changes back to XML

private function updateConfig(ParamValue:String):void

{

      // load current file to update the value

      var configOld:File = File.applicationStorageDirectory.resolvePath("config.xml");

      fStrm = new FileStream();

      // open it in write mode

      fStrm.open(configOld, FileMode.WRITE);

      // form new data for config file

      var strNewData:String = '<?xml version="1.0" encoding="UTF-8"?>';

      strNewData += configData;

      // wirte new data

      fStrm.writeUTFBytes(strNewData);

      fStrm.close();

}

 

 

-           loadConfig()” will do read operation. First it’ll try to read the configuration file from the Application storage directory. If it doesn’t found the file then it’ll copy the existing file to that directory & then load the data.

o   Using “applicationDirectory” I was getting security error and after some googling I found a LINK for this solution HERE or you can go directly to the solution URL HERE.

 

-          updateConfig()” will update the config file with new data (skin detail).