Inspecting the properties in a class using the describeType() method and E4X/XML

The following example shows how you can use the describeType() method to inspect a class’s accessor methods.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/19/inspecting-the-properties-in-a-class-using-the-describetype-method-and-e4xxml/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import flash.utils.getDefinitionByName;

            import mx.collections.Sort;
            import mx.collections.SortField;
            import mx.collections.XMLListCollection;
            import mx.controls.*;
            import mx.utils.StringUtil;
            import mx.utils.ObjectUtil;

            [Embed("bullet_red.png")]
            private const WRITE_ONLY_ICON:Class;

            [Embed("bullet_yellow.png")]
            private const READ_ONLY_ICON:Class;

            [Embed("bullet_green.png")]
            private const READ_WRITE_ICON:Class;

            private var theXML:XML;
            private var theXMLList:XMLList;
            private var theXMLListColl:XMLListCollection;

            private function init():void {
                var theSortField:SortField = new SortField("@name", true);
                var theSort:Sort = new Sort();
                theSort.fields = [theSortField];

                // var theXML:XML = describeType(getDefinitionByName("mx.controls.Alert"));
                theXML = describeType(Label);
                theXMLList = theXML.factory.accessor.(@declaredBy == theXML.@name);
                theXMLListColl = new XMLListCollection(theXMLList);
                theXMLListColl.sort = theSort;
                theXMLListColl.refresh();
                list.dataProvider = theXMLListColl;
                panel.title = "Accessor methods for the " + theXML.@name + " class:"
            }

            private function list_labelFunc(item:XML):String {
                var itemName:String = item.@name;
                var itemType:String = item.@type.split("::").pop();
                return StringUtil.substitute("{0} : {1}",
                            itemName,
                            itemType);
            }

            private function list_iconFunc(item:XML):Class {
                var access:String = item.@access;
                switch (access) {
                    case "readwrite":
                        return READ_WRITE_ICON;
                        break;
                    case "readonly":
                        return READ_ONLY_ICON;
                        break;
                    case "writeonly":
                        return WRITE_ONLY_ICON;
                        break;
                    default:
                        break;
                }
                return null;
            }
        ]]>
    </mx:Script>

    <mx:Panel id="panel" width="350">
        <mx:List id="list"
                labelFunction="list_labelFunc"
                iconFunction="list_iconFunc"
                verticalScrollPolicy="on"
                width="100%"
                itemClick="Alert.show(list.selectedItem.toXMLString());" />

        <mx:ControlBar>
            <mx:Button label="Read/Write" icon="{READ_WRITE_ICON}" skin="{null}" />
            <mx:Button label="Read Only" icon="{READ_ONLY_ICON}" skin="{null}" />
            <mx:Button label="Write Only" icon="{WRITE_ONLY_ICON}" skin="{null}" />
        </mx:ControlBar>
    </mx:Panel>

</mx:Application>

2 thoughts on “Inspecting the properties in a class using the describeType() method and E4X/XML

  1. Hi Peter

    I need to dynamically add columns to a datagrid. I’m populating the datagrid from an HttpService, which calls 1 of 2 XML files. The files have 2 different sets of columns, so I either need to add/remove columns dynamically, or define all columns and toggle visibility accordingly. I’m having trouble with both approaches. What would be the best way to approach this?

    Thank in advance.

  2. Adam,

    I’d probably see if you can dynamically set the DataGrid instance’s columns property after setting the DataGrid control’s data provider.

    Peter

Comments are closed.