DisplayObjectの階層構造をチェック

by fff
♥1 | Line 212 | Modified 2009-05-30 23:42:39 | MIT License
play

ActionScript3 source code

/**
 * Copyright fff ( http://wonderfl.net/user/fff )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/nIorC
 */

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    paddingTop="1"
    layout="vertical" horizontalAlign="right"
    applicationComplete="init()">
    <mx:Script>
        <![CDATA[
            import mx.controls.DateField;
            import mx.events.CloseEvent;
            import mx.containers.TitleWindow;
            import mx.managers.PopUpManager;
            
            private function init():void
            {
                dotWindow.update();
            }
            
            private function clickHandler(event:Event):void
            {
                switch (event.currentTarget)
                {
                    case addButtonBtn:
                    {
                        var b:Button = new Button();
                        b.label = "Button";
                        b.addEventListener(MouseEvent.CLICK, function(e:Event):void
                        {
                            removeChild(b);
                            dotWindow.update();
                        });
                        addChild(b);
                        break;
                    }
                    case addPopupBtn:
                    {
                        var tw:TitleWindow = new TitleWindow();
                        tw.title = "TitleWindow";
                        tw.addChild(new DateField());
                        tw.showCloseButton = true;
                        tw.addEventListener(CloseEvent.CLOSE, function(e:Event):void
                        {
                            PopUpManager.removePopUp(tw);
                            dotWindow.update();
                        });
                        PopUpManager.addPopUp(tw, this);
                        break;
                    }
                }
                dotWindow.update();
            }
            
        ]]>
    </mx:Script>
    <mx:ApplicationControlBar dock="true" horizontalAlign="right">
        <mx:Button id="addButtonBtn" label="Application直下にボタン追加" click="clickHandler(event)"/>
        <mx:Button id="addPopupBtn" label="ポップアップ追加" click="clickHandler(event)"/>
    </mx:ApplicationControlBar>
    
    <DisplayObjectTreeWindow xmlns="*" id="dotWindow" width="100%" height="100%" />
    
    <!-- 以下、コンポーネント定義 -->
    
    <mx:Component className="DisplayObjectTreeWindow">
        <mx:TitleWindow title="DisplayObjectの階層構造をチェック"
            status="DisplayObjectの数: { nodes.length }"
            minWidth="300" minHeight="300">
            <mx:Script>
                <![CDATA[
            import mx.core.UIComponent;
            import flash.utils.getQualifiedClassName;
            import flash.utils.getQualifiedSuperclassName;
            import mx.controls.CheckBox;
            import mx.core.Application;
            import mx.core.IRawChildrenContainer;
            import mx.utils.DisplayUtil;
            
            [Bindable]
            private var dp:XML;
            [Bindable]
            private var nodes:Array;
            [Bindable]
            private var openNodes:Array;
            [Bindable]
            private var includeRawChildren:Boolean = false;
            
            public function update():void
            {
                if (!stage) return;
                
                var newDP:XML = <dp/>;
                var newNodes:Array = [];
                var newOpenNodes:Array = [];
                walkDisplayObjects(stage, newDP, newNodes, newOpenNodes, includeRawChildren);
                dp = newDP;
                nodes = newNodes;
                openNodes = newOpenNodes;
                
                trace(newDP);
            }
            
            /**
             * @see mx.utils.DisplayUtil#walkDisplayObjects()
             */
            private function walkDisplayObjects(displayObject:DisplayObject,
                                              parentNode:XML,
                                              nodes:Array,
                                              openNodes:Array,
                                              includeRawChildren:Boolean,
                                              index:int=-1):void
            {
                var node:XML = <node />;
                if (index != -1)
                    node.@index = index;
                if (displayObject.name)
                    node.@name = displayObject.name;
                if (displayObject is UIComponent)
                    node.@isUIComponent = true;
                node.@className = getQualifiedClassName(displayObject);
                node.@superClassName = getQualifiedSuperclassName(displayObject);
                
                switch(displayObject)
                {
                    case stage:
                    {
                        node.@tag += "[stage]";
                        break;
                    }
                    case root:
                    {
                        node.@tag += "[root]";
                        break;
                    }
                    case Application.application:
                    {
                        node.@tag += "[app]";
                        break;
                    }
                    case this:
                    {
                        node.@tag += "[this]";
                        break;
                    }
                }
                
                parentNode.appendChild(node);
                nodes.push(node);
                
                switch(displayObject)
                {
                    case stage:
                    case root:
                    case Application.application:
                    {
                        openNodes.push(node);
                        break;
                    }
                }
                
                // DisplayObjectTreeWindowの中は走査しない
                if (displayObject == this) return;
        
                if (displayObject is DisplayObjectContainer)
                {
                    var i:int;
                    var n:int;
                    var child:DisplayObject;
                    
                    if (includeRawChildren && displayObject is IRawChildrenContainer)
                    {
                        n = IRawChildrenContainer(displayObject).rawChildren.numChildren;
                        for (i = 0; i < n; i++)
                        {
                            child = IRawChildrenContainer(displayObject).rawChildren.getChildAt(i);
                            walkDisplayObjects(child, node, nodes, openNodes, includeRawChildren, i);
                        }
                    }
                    else
                    {
                        n = DisplayObjectContainer(displayObject).numChildren;
                        for (i = 0; i < n; i++)
                        {
                            child = DisplayObjectContainer(displayObject).getChildAt(i);
                            walkDisplayObjects(child, node, nodes, openNodes, includeRawChildren, i);
                        }
                    }
                }
            }
            private function labelFunc(item:XML):String
            {
                var label:String = "";
                if (item.@index.length())
                    label += item.@index + " ";
                if (item.@tag.length())
                    label += item.@tag + " ";
                if (item.@name.length())
                    label += item.@name + " ";
                return label;
                
            }
                ]]>
            </mx:Script>
            <mx:states>
                <mx:State name="showClassInfo">
                    <mx:AddChild relativeTo="{ tree }" position="after">
                        <mx:Form width="100%" paddingTop="0" paddingBottom="0"
                            doubleClickEnabled="true" doubleClick="currentState=null">
                            <mx:FormItem label="isUIComponent:">
                                <mx:Label text="{ XML(tree.selectedItem).@isUIComponent }" />
                            </mx:FormItem>
                            <mx:FormItem label="className:">
                                <mx:Label text="{ XML(tree.selectedItem).@className }" />
                            </mx:FormItem>
                            <mx:FormItem label="superClassName:">
                                <mx:Label text="{ XML(tree.selectedItem).@superClassName }" />
                            </mx:FormItem>
                        </mx:Form>
                    </mx:AddChild>
                </mx:State>
            </mx:states>
            <mx:Tree id="tree" width="100%" height="100%"
                showRoot="false"
                change="currentState='showClassInfo'"
                dataProvider="{ dp }" openItems="{ openNodes }"
                labelField="@label"
                labelFunction="{ labelFunc }"/>
            <mx:ControlBar>
                <mx:Button label="更新" click="update()" />
                <mx:CheckBox id="includeRawChildrenCheck" label="non-content(rawChildren)も含める"
                    selected="{ includeRawChildren }"
                    change="includeRawChildren=includeRawChildrenCheck.selected;update()" />
            </mx:ControlBar>
        </mx:TitleWindow>
    </mx:Component>
</mx:Application>