Archive for the ‘Flex’ Category

Using an XML Configuration File for Flex

Okay, if you are a new Flex developer in training and are going to use a configuration file for your appliation, you might be tempted as I was to do it all in Actionscript code using the URLLoader class. While you can do this, it takes a lot of elbow grease to get the timing just right.

My best experience was using the <mx:HTTPService> tag to load the configuration file and process it into the e4x parsing model as shown:

Let's say you have the following configuration file:

XML:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <botconfig>
  3.     <botname>Andy Richards</botname>
  4.     <picture>andyRichards.gif</picture>
  5.     <backgroundStyle>insideRoomTablesBed</backgroundStyle>
  6. </botconfig>

Now you want to read that in and assign the contents of each node to a configuration settings object:

Actionscript:
  1. /*
  2. ConfigSettings.as
  3. ConfigSettings class defines a custom object for
  4. parsing a configuration xml file and distributing the
  5. nodes to fields of the object so they are easily accessible to
  6. the main Flex Application.
  7. */
  8. package edu.asu.simlandia {
  9.     public class ConfigSettings {
  10.        
  11.         private var _botName:String;
  12.         private var _botImage:String;
  13.         private var _backgroundStyle:String;
  14.        
  15.         private var xmlDoc:XML;
  16.         public function get botName():String {
  17.             return this._botName;
  18.         }
  19.         public function get botImage():String {
  20.             return this._botImage;
  21.         }
  22.         public function get backgroundStyle():String {
  23.             return this._backgroundStyle;
  24.         }
  25.         public function ConfigSettings():void {
  26.             this._interactions = new Array();
  27.         }
  28.         public function set botName(value:String):void {
  29.             this._botName = value;
  30.         }
  31.         public function set botImage(value:String):void {
  32.             this._botImage = value;
  33.         }
  34.         public function set backgroundStyle(value:String):void {
  35.             this._backgroundStyle = value;
  36.         }
  37.         /*
  38.             Function responsible for loading the configuration file,
  39.             parsing it, and assigning the text properties to the
  40.             configuration object
  41.         */
  42.         public function loadData(xmlData:XML):void {
  43.        
  44.             this.xmlDoc = xmlData;
  45.             this.botImage = this.xmlDoc.picture;
  46.             this.botName = this.xmlDoc.botname;
  47.             this.backgroundStyle = this.xmlDoc.backgroundStyle;
  48.            
  49.            
  50.         }
  51.        
  52.     }
  53. }

Use the following MXML code to do it:

XML:
  1. <mx:Application
  2.     xmlns:mx="http://www.adobe.com/2006/mxml"
  3.     layout="absolute" backgroundColor="#CCCC99"
  4.     creationComplete="configService.send()">
  5. <mx:Script>
  6.       <![CDATA[
  7.              import edu.asu.simlandia.*;
  8.          import mx.rpc.events.ResultEvent;
  9.              
  10.              private var settings:ConfigSettings;
  11.              private var configXML:XML;
  12.  
  13.              private function configure(event:ResultEvent):void {
  14.                      settings = new ConfigSettings();
  15.              settings.loadData(this.configXML);
  16.              this.botName = this.settings.botName;
  17.              this.botImage = "images/" + this.settings.botImage;
  18.              this.styleName = this.settings.backgroundStyle;
  19.              }
  20.              
  21.       ]]>
  22. </mx:Script>
  23.  
  24. <mx:HTTPService url="configFile.xml" id="configService" resultFormat="e4x" result="configure(event)"/>
  25. </mx:Application>

Posted by Jeffrey on December 6th, 2007 No Comments

Rapid Prototyping for E-Learning

In the presentation I gave for my Current Ed Tech Trends course topic earlier this year, I had mentioned the fact that the development of the Graphical User Interface made the transition from entering commands via a keyboard from a cognitive process (knowing and typing) to a more visual process (clicking and dragging). Therefore, metaphors had to be developed that made sense to end users. Along with a trash bin (for deleting files), came the standard widgets we see today. (Text boxes, buttons, radio buttons, check boxes, etc.) Integrated development environments (IDE's) offered a method to create an interface using the exact same tools and widgets as the eventual produced final product.
(more...)

Posted by Jeffrey on November 18th, 2007 No Comments