In my previous post, “Introducing the StyleManager.selectors property in Flex 3“, we looked at the new StyleManager class’s static selectors property introduced in Flex 3.

This example shows how you can make a simple app which lets you loop over styles currently registered with the StyleManager and display their current style names and values.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/12/building-a-simple-style-browser-in-flex-3/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()">

    <mx:Script>
        <![CDATA[
            import mx.styles.StyleManager;
            import mx.controls.ComboBox;

            private function init():void {
                arr = StyleManager.selectors;
                arr.sort(Array.CASEINSENSITIVE);
            }

            private function list_change(evt:Event):void {
                var styleName:String = String(ComboBox(evt.currentTarget).selectedItem);
                var cssStyle:CSSStyleDeclaration = StyleManager.getStyleDeclaration(styleName);
                var obj:Object = new cssStyle.defaultFactory();
                var styles:Array = [];
                var key:String;

                for (key in obj) {
                    styles.push({key:key, value:obj[key]});
                }

                styles.sortOn("key", Array.CASEINSENSITIVE);
                dataGrid.dataProvider = styles;
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr" />

    <mx:ApplicationControlBar dock="true">
        <mx:Label text="selectors:" />
        <mx:ComboBox id="comboBox"
                prompt="Please select a style..."
                dataProvider="{arr}"
                width="250"
                change="list_change(event)" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            rowHeight="22"
            width="300"
            verticalScrollPolicy="on">
        <mx:columns>
            <mx:DataGridColumn dataField="key"
                    headerText="Style:"
                    itemRenderer="mx.controls.Label" />
            <mx:DataGridColumn dataField="value"
                    headerText="Value:"
                    itemRenderer="mx.controls.Label" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

4 Responses to Building a simple style browser in Flex 3

  1. wespire says:

    Where i can get flex 3? I can’t find it

  2. peterd says:

    wespire,

    You can get the beta of Flex Builder 3 from the Adobe Labs site at http://labs.adobe.com/technologies/flex/flexbuilder3/. Flex Builder 3 includes the Flex 2 and Flex 3 SDKs, but if you want to just download the SDK (or want to update the SDK that shipped with Flex Builder 3), you can grab nightly builds of the Flex 3 SDK from http://labs.adobe.com/technologies/flex/sdk/flex3sdk.html.

    Peter

  3. Arnaud says:

    Hello,

    I got an error on the line
    var obj:Object = new cssStyle.defaultFactory();
    Telling me this is not a valid constructor.
    Would you have any idea of what to do or any workaround ?
    I ‘m working on the last Flex SDK.

    Regards

    Arnaud

  4. Matt says:

    Hi Peter,

    Your Flex Exmples site has provided so many excellent insights and solutions. I just wanted to say “thanks” for sharing your work over the years.

    BTW, your bio cracked me up! :-)

    Regards,

    Matt

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree