搞一下EdgeRendererSelector,layoutSelector

一直搞不懂为什么EdgeRendererSelector 是继承ComboBox的,看一下source code 就知道了

http://code.google.com/p/birdeye/source/browse/trunk/ravis/libRaVis/org/un/cava/birdeye/ravis/components/ui/controls/vgraphControls/EdgeRendererSelector.mxml?r=1807

<?xml version="1.0" encoding="utf-8"?>
<!--
*
* The MIT License
*
* Copyright (c) 2008
* United Nations Office at Geneva
* Center for Advanced Visual Analytics
* http://cava.unog.ch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
-->
<mx:ComboBoxxmlns:mx="http://www.adobe.com/2006/mxml"
    height="20"
    width="100%"
    color="#CCCCCC"
    prompt="Select edge style."
    toolTip="Select the style of line drawn between nodes."
    initialize="initData()"
    change="changeEdgeRenderer()">
   
    <mx:ArrayCollectionid="ervalues">
        <mx:String>Default</mx:String>
        <mx:String>DirectedArrows</mx:String>
        <mx:String>DirectedBalloons</mx:String>
        <mx:String>Orthogonal</mx:String>
        <mx:String>Flow</mx:String>
        <mx:String>Bezier</mx:String>
        <mx:String>Circular</mx:String>
        <mx:String>Hyperbolic</mx:String>
    </mx:ArrayCollection>
   
    <mx:Script>
        <![CDATA[
            import mx.utils.ObjectUtil;
           
            import org.un.cava.birdeye.ravis.graphLayout.visual.IVisualGraph;
            import org.un.cava.birdeye.ravis.graphLayout.visual.VisualGraph;
            import org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers.BaseEdgeRenderer;
            import org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers.CircularEdgeRenderer;
            import org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers.DirectedArrowEdgeRenderer;
            import org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers.DirectedBalloonEdgeRenderer;
            import org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers.FlowCurveEdgeRenderer;
            import org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers.FlowEdgeRenderer;
            import org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers.HyperbolicEdgeRenderer;
            import org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers.OrthogonalEdgeRenderer;
            import org.un.cava.birdeye.ravis.utils.LogUtil;
            import org.un.cava.birdeye.ravis.utils.events.VGraphEvent;
           
            private static const _LOG:String = "components.ui.controls.vgraphControls.EdgeRendererSelector";
            private var _vgraph:VisualGraph;
           
            /**
             * Provides access to the registered vgraph object.
             * */
            public function set vgraph(v:VisualGraph):void {
                _vgraph = v;
                registerListeners();
            }
           
           
            /**
             * @private
             * */
            public function get vgraph():VisualGraph {
                return _vgraph;
            }
           
            /**
             * When enabling or disabling this component, we also
             * perform some specific tasks.
             * Attention do completely disable interaction
             * there is also the mouseEnabled property.
             *
             * @inheritDoc
             * */
            override public function set enabled(e:Boolean):void {
                if(e == true) {
                    this.setStyle("color",0x000000);
                    this.alpha=1;
                } else {
                    this.setStyle("color",0xCCCCCC);
                    this.alpha=0.3;
                }
                /* call superclass (ComboBox) */
                super.enabled = e;
            }
           
            /**
             * Set the selected EdgeLabelRenderer.
             * */
            public function changeEdgeRenderer():void {
               
                var er:String;
                var g:IVisualGraph;
               
                if(_vgraph == null) {
                    LogUtil.warn(_LOG, "Cannot change EdgeRenderer, no valid vgraph!");
                    return;
                }
                g = _vgraph;
               
                /* get the currently selected EdgeRenderer */
                er = this.selectedItem as String;
               
                switch(er) {
                case "Default":
                    _vgraph.edgeRendererFactory = new ClassFactory(BaseEdgeRenderer);
                    break;
                case "DirectedArrows":
                    _vgraph.edgeRendererFactory = new ClassFactory(DirectedArrowEdgeRenderer);
                    break;
                case "DirectedBalloons":
                    _vgraph.edgeRendererFactory = new ClassFactory(DirectedBalloonEdgeRenderer);
                    break;
                case "Orthogonal":
                    _vgraph.edgeRendererFactory = new ClassFactory(OrthogonalEdgeRenderer);
                    break;
                case "Flow":
                    _vgraph.edgeRendererFactory = new ClassFactory(FlowEdgeRenderer);
                    break;
                case "Bezier":
                    _vgraph.edgeRendererFactory = new ClassFactory(FlowCurveEdgeRenderer);
                    break;
                case "Circular":
                    _vgraph.edgeRendererFactory = new ClassFactory(CircularEdgeRenderer);
                    break;
                case "Hyperbolic":
                    _vgraph.edgeRendererFactory = new ClassFactory(HyperbolicEdgeRenderer);
                    break;
                default:
                    LogUtil.warn(_LOG, "Illegal EdgeRenderer selected:"+er);
                    _vgraph.edgeRendererFactory = new ClassFactory(BaseEdgeRenderer);
                    break;
                }
                _vgraph.refresh();
            }
           
            /**
             * Refresh the selector with the current EdgeLabelRenderer.
             * */
            public function refreshSelector(e:VGraphEvent = null):void {
               
                var erClassName:String;
               
                if(_vgraph == null) {
                    LogUtil.warn(_LOG, "Cannot refresh EdgeRenderer, no valid vgraph!");
                    return;
                }
               
                erClassName = ObjectUtil.getClassInfo(_vgraph.edgeRendererFactory.newInstance()).name;
                erClassName = erClassName.replace(/org.un.cava.birdeye.ravis.graphLayout.visual.edgeRenderers::/,"");
               
                switch(erClassName) {
                case "BaseEdgeRenderer":
                    this.selectedIndex = 0;
                    break;
                case "DirectedArrowEdgeRenderer":
                    this.selectedIndex = 1;
                    break;
                case "DirectedBalloonEdgeRenderer":
                    this.selectedIndex = 2;
                    break;
                case "OrthogonalEdgeRenderer":
                    this.selectedIndex = 3;
                    break;
                case "FlowEdgeRenderer":
                    this.selectedIndex = 4;
                    break;
                case "FlowCurveEdgeRenderer":
                    this.selectedIndex = 5;
                    break;
                case "CircularEdgeRenderer":
                    this.selectedIndex = 6;
                    break;
                case "HyperbolicEdgeRenderer":
                    this.selectedIndex = 7;
                    break;
                default:
                    LogUtil.warn(_LOG, "Illegal EdgeRenderer class received:"+erClassName);
                    break;
                }
            }
           
            /**
             * initialise the selection data.
             * It is a bit unclear, why this seems to be necessary.
             * */
            private function initData():void {
                this.dataProvider = ervalues;
            }
           
            /**
             * register event listener
             * */
            private function registerListeners():void {
                _vgraph.addEventListener(VGraphEvent.VGRAPH_CHANGED,refreshSelector);
            }
        ]]>
    </mx:Script>
</mx:ComboBox>
<?xml version="1.0" encoding="utf-8"?>
<!--
*
* The MIT License
*
* Copyright (c) 2008
* United Nations Office at Geneva
* Center for Advanced Visual Analytics
* http://cava.unog.ch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
-->
<mx:ComboBoxxmlns:mx="http://www.adobe.com/2006/mxml"
        height="20"
        width="95%"
        color="#CCCCCC"
        prompt="Select layout algorithm."
        toolTip="Some layouters may not be fully stable."
        initialize="initData()"
        change="changeLayouter()">
       
        <mx:ArrayCollectionid="layouts">
                <mx:String>---Radial---</mx:String>
                <mx:String>ConcentricRadial</mx:String>
                <mx:String>ParentCenteredRadial</mx:String>
                <mx:String>SingleCycleCircle</mx:String>
                <mx:String>Hyperbolic</mx:String>
                <mx:String>---Tree---</mx:String>
                <mx:String>Hierarchical</mx:String>
                <mx:String>---Physics---</mx:String>
                <mx:String>ForceDirected</mx:String>
                <mx:String>ISOM</mx:String>
                <mx:String>---Coordinates---</mx:String>
                <mx:String>DirectPlacement</mx:String>
                <mx:String>---Experimental---</mx:String>
                <mx:String>Phyllotactic</mx:String>
        </mx:ArrayCollection>
 
        <mx:Script>    
                <![CDATA[
                        import mx.utils.ObjectUtil;
                        import org.un.cava.birdeye.ravis.utils.LogUtil;
                        import org.un.cava.birdeye.ravis.utils.events.VGraphEvent;
                        import org.un.cava.birdeye.ravis.graphLayout.visual.VisualGraph;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.ILayoutAlgorithm;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.CircularLayouter;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.ConcentricRadialLayouter;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.DirectPlacementLayouter;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.ForceDirectedLayouter;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.HierarchicalLayouter;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.Hyperbolic2DLayouter;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.ISOMLayouter;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.ParentCenteredRadialLayouter;
                        import org.un.cava.birdeye.ravis.graphLayout.layout.PhylloTreeLayouter;
                       
                  private static const _LOG:String = "components.ui.controls.layouterControls.LayoutSelector";
                        private var _vgraph:VisualGraph;
 
                        /**
                         * Provides access to the registered vgraph object.
                         * */
                        public function set vgraph(v:VisualGraph):void {
                                _vgraph = v;
                                registerListeners();
                        }
 
 
                        /**
                         * @private
                         * */
                        public function get vgraph():VisualGraph {
                                return _vgraph;
                        }
                       
                        /**
                         * When enabling or disabling this component, we also
                         * perform some specific tasks.
                         * Attention do completely disable interaction
                         * there is also the mouseEnabled property.
                         *
                         * @inheritDoc
                         * */
                        override public function set enabled(e:Boolean):void {
                                if(e == true) {
                                        this.setStyle("color",0x000000);
                                        this.alpha=1;
                                } else {
                                        this.setStyle("color",0xCCCCCC);
                                        this.alpha=0.3;
                                }
                                /* call superclass (ComboBox) */
                                super.enabled = e;
                        }
       
                        /**
                         * Set the selected Layouter.
                         * */
                        public function changeLayouter():void {
                                /* check if we have a vgraph at all */
                                if(_vgraph == null) {
                                        LogUtil.warn(_LOG, "Cannot change Layouter without vgraph.");
                                        return;
                                }
                                setLayouter();
                                _vgraph.draw(); // run the layout
                        }
 
                        /**
                         * Set/Activate the layouter set in the selector.
                         * */
                        public function setLayouter():void {
                               
                                var layouter:ILayoutAlgorithm;
                                var layouterName:String = (this.selectedItem as String);
                                                               
                                /* check if we have a vgraph at all */
                                if(_vgraph == null) {
                                        LogUtil.warn(_LOG, "Cannot change Layouter without vgraph.");
                                        return;
                                }
       
                                /* kill off animation in old layouter if present */
                                if(_vgraph.layouter != null) {
                                        _vgraph.layouter.resetAll();
                                        /* remove also existing references thus
                                         * destroying the object (maybe this is not needed?) */
                                        _vgraph.layouter = null;
                                }
       
                                /* now choose the selected layouter */
                                switch(layouterName) {
                                        case "ConcentricRadial":
                                                layouter = new ConcentricRadialLayouter(_vgraph);
                                                break;
                                        case "ParentCenteredRadial":
                                                layouter = new ParentCenteredRadialLayouter(_vgraph);
                                                break;
                                        case "SingleCycleCircle":
                                                layouter = new CircularLayouter(_vgraph);
                                               
                                                /* set the hyperbolic edge renderer type *
                                                _vgraph.edgeRenderer = new CircularEdgeRenderer();
                                                _vgraph.scrollBackgroundInDrag = false;
                                                _vgraph.moveNodeInDrag = false;
                                                absoluteScaling = true;
                                                updateScale();
                                                */
                                                break;
                                        case "Hyperbolic":
                                                layouter = new Hyperbolic2DLayouter(_vgraph);
                                               
                                                /* set some layouter specific defaults:
                                                _vgraph.edgeRenderer = new HyperbolicEdgeRenderer((layouter as Hyperbolic2DLayouter).projector);
                                                _vgraph.scrollBackgroundInDrag = false;
                                                _vgraph.moveNodeInDrag = false;
                                                absoluteScaling = false;
                                                */
                                                break;
                                        case "Hierarchical":
                                                layouter = new HierarchicalLayouter(_vgraph);
                                                break;
                                        case "ForceDirected":
                                                layouter = new ForceDirectedLayouter(_vgraph);
                                                break;
                                        case "ISOM":
                                                layouter = new ISOMLayouter(_vgraph);
                                                break;
                                        case "DirectPlacement":
                                                layouter = new DirectPlacementLayouter(_vgraph);
                                                /* set some layouter specific values, i.e. create a control
                                                 * for these first, also they could be prepopulated from
                                                 * XML data
                                                (layouter as DirectPlacementLayouter).relativeHeight = 400;
                                                (layouter as DirectPlacementLayouter).relativeWidth = 400;
                                                 */
                                                /*
                                                /* set the orthogonal edge renderer type *
                                                _vgraph.edgeRenderer = new OrthogonalEdgeRenderer();
                                                _vgraph.scrollBackgroundInDrag = true;
                                                _vgraph.moveNodeInDrag = true;
                                                absoluteScaling = true;
                                                updateScale();
                                                */
                                                break;
                                        case "Phyllotactic":
                                                layouter = new PhylloTreeLayouter(_vgraph);
                                                break;
                                        default:
                                                LogUtil.warn(_LOG, "Illegal Layouter selected, defaulting to ConcentricRadial"+
                                                        layouterName);
                                                layouter = new ConcentricRadialLayouter(_vgraph);
                                                break;
                                }
                                _vgraph.layouter = layouter;
                        }
 
                        /**
                         * Refresh the selector if an external event changes the layouter.
                         * */
                        public function refreshSelector(e:VGraphEvent = null):void {
                               
                                var layouterClassName:String;
                                var layouterName:String;
                                                               
                                /* check if we have a vgraph/layouter at all */
                                if(_vgraph == null || _vgraph.layouter == null) {
                                        LogUtil.warn(_LOG, "Cannot refresh the LayoutSelector without vgraph or Layouter.");
                                        return;
                                }
       
                                layouterClassName = ObjectUtil.getClassInfo(_vgraph.layouter).name;
                                layouterClassName = layouterClassName.replace(/org.un.cava.birdeye.ravis.graphLayout.layout::/,"");
       
                                /* now choose the selected layouter */
                                switch(layouterClassName) {
                                        case "ConcentricRadialLayouter":
                                                layouterName = "ConcentricRadial";
                                                break;
                                        case "ParentCenteredRadialLayouter":
                                                layouterName = "ParentCenteredRadial";
                                                break;
                                        case "CircularLayouter":
                                                layouterName = "SingleCycleCircle";
                                                break;
                                        case "Hyperbolic2DLayouter":
                                                layouterName = "Hyperbolic";
                                                break;
                                        case "HierarchicalLayouter":
                                                layouterName = "Hierarchical";
                                                break;
                                        case "ForceDirectedLayouter":
                                                layouterName = "ForceDirected";
                                                break;
                                        case "ISOMLayouter":
                                                layouterName = "ISOM";
                                                break;
                                        case "DirectPlacementLayouter":
                                                layouterName = "DirectPlacement";
                                                break;
                                        case "PhylloTreeLayouter":
                                                layouterName = "Phyllotactic";
                                                break;
                                        default:
                                                layouterName = "Unknown";
                                                LogUtil.warn(_LOG, "Unknown Layouter found:"+layouterClassName);
                                                break;
                                }
                                /* make the selection */
                               
                                /* XXX THIS MAY CAUSE A LOOP POSSIBLY */
                                this.selectedItem = layouterName;
                        }
 
                        /**
                         * initialise the selection data.
                         * It is a bit unclear, why this seems to be necessary.
                         * */
                        private function initData():void {
                                this.dataProvider = layouts;
                        }
                       
                        /**
                         * Adds the listeners to update on changes in the VGraph
                         * */
                        private function registerListeners():void {
                                _vgraph.addEventListener(VGraphEvent.LAYOUTER_CHANGED,refreshSelector);
                        }
                ]]>
        </mx:Script>
</mx:ComboBox>

posted on 2013-02-18 15:26  lauraxia  阅读(245)  评论(0编辑  收藏  举报

导航