source: ComponentRegistry/trunk/ComponentBrowserGui/src/main/flex/clarin/cmdi/componentregistry/editor/CMDComponentXMLEditor.as @ 269

Last change on this file since 269 was 269, checked in by patdui, 14 years ago
  • moved interface to common package
File size: 5.3 KB
Line 
1package clarin.cmdi.componentregistry.editor {
2        import clarin.cmdi.componentregistry.browser.XMLBrowser;
3        import clarin.cmdi.componentregistry.common.ItemDescription;
4        import clarin.cmdi.componentregistry.common.StyleConstants;
5        import clarin.cmdi.componentregistry.common.CMDSpecRenderer;
6        import clarin.cmdi.componentregistry.editor.model.CMDComponent;
7        import clarin.cmdi.componentregistry.editor.model.CMDSpec;
8       
9        import flash.display.DisplayObject;
10        import flash.events.Event;
11        import flash.utils.getTimer;
12       
13        import mx.binding.utils.BindingUtils;
14        import mx.collections.ArrayCollection;
15        import mx.containers.Form;
16        import mx.containers.FormItem;
17        import mx.core.Container;
18        import mx.core.UIComponent;
19        import mx.events.ChildExistenceChangedEvent;
20        import mx.events.DragEvent;
21        import mx.managers.DragManager;
22        import mx.managers.IFocusManagerComponent;
23
24        [Event(name="editorChange", type="flash.events.Event")]
25        public class CMDComponentXMLEditor extends Form implements IFocusManagerComponent, CMDSpecRenderer {
26
27                public static const DRAG_NEW_COMPONENT:String = "newComponent";
28                public static const DRAG_NEW_ELEMENT:String = "newElement";
29                public static const DRAG_NEW_ATTRIBUTE:String = "newAttribute";
30
31                public static const EDITOR_CHANGE_EVENT:String = "editorChange";
32                private var _spec:CMDSpec;
33
34                public function CMDComponentXMLEditor() {
35                        super();
36                        focusEnabled = true;
37                        tabEnabled = true;
38                        styleName = StyleConstants.XMLBROWSER;
39                        addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler);
40                        addEventListener(DragEvent.DRAG_OVER, dragOverHandler);
41                        addEventListener(DragEvent.DRAG_DROP, dragDropHandler);
42                        addEventListener(ChildExistenceChangedEvent.CHILD_ADD, dispatchEditorChangeEvent);
43                        addEventListener(ChildExistenceChangedEvent.CHILD_REMOVE, dispatchEditorChangeEvent);
44                }
45
46                private function dragEnterHandler(event:DragEvent):void {
47                        DragManager.acceptDragDrop(event.currentTarget as UIComponent);
48                        UIComponent(event.currentTarget).drawFocus(true);
49                }
50
51                private function dragOverHandler(event:DragEvent):void {
52                        if (event.dragSource.hasFormat("items")) {
53                                DragManager.showFeedback(DragManager.COPY);
54                        } else if (event.dragSource.hasFormat(DRAG_NEW_COMPONENT)) {
55                                DragManager.showFeedback(DragManager.COPY);
56                        } else {
57                                DragManager.showFeedback(DragManager.NONE);
58                        }
59                }
60
61                private function dragDropHandler(event:DragEvent):void {
62                        if (event.dragSource.hasFormat("items")) {
63                                var items:Array = event.dragSource.dataForFormat("items") as Array;
64                                for each (var item:ItemDescription in items) {
65                                        var comp:CMDComponent = new CMDComponent();
66                                        comp.componentId = item.id;
67                                        _spec.cmdComponents.addItem(comp);
68                                        addComponent(comp);
69                                }
70                        } else if (event.dragSource.hasFormat(DRAG_NEW_COMPONENT)) {
71                                var emptyComp:CMDComponent = event.dragSource.dataForFormat(DRAG_NEW_COMPONENT) as CMDComponent;
72                                _spec.cmdComponents.addItem(emptyComp);
73                                addComponent(emptyComp);
74                        }
75                }
76
77                public function set cmdSpec(cmdSpec:CMDSpec):void {
78                        _spec = cmdSpec;
79                        createNewEditor();
80                        dispatchEditorChangeEvent();
81                }
82
83                private function dispatchEditorChangeEvent(event:Event = null):void {
84                        dispatchEvent(new Event(EDITOR_CHANGE_EVENT));
85                }
86
87                [Bindable]
88                public function get cmdSpec():CMDSpec {
89                        return _spec;
90                }
91
92                private function createNewEditor():void {
93                        var start:int = getTimer();
94                        removeAllChildren()
95                        handleHeader(_spec);
96                        handleComponents(_spec.cmdComponents);
97                        trace("Created editor view in " + (getTimer() - start) + " ms.");
98                }
99
100                private function handleHeader(spec:CMDSpec):void {
101                        addChild(new SelectTypeRadioButtons(spec));
102                        addChild(createOptionalGroupNameInput(spec));
103                        addChild(createHeading());
104                        addChild(new FormItemInputLine("Name", spec.headerName, function(val:String):void {
105                                        spec.headerName = val;
106                                }));
107                        addChild(new FormItemInputText(XMLBrowser.DESCRIPTION, spec.headerDescription, function(val:String):void {
108                                        spec.headerDescription = val;
109                                }));
110                        var idInput:FormItemInputLine = new FormItemInputLine("Id", spec.headerId, function(val:String):void {
111                                        spec.headerId = val;
112                                }, false);
113                        idInput.toolTip = "Id will be generated";
114                        addChild(idInput);
115                }
116
117                private function createOptionalGroupNameInput(spec:CMDSpec):FormItem {
118                        var result:FormItem = new FormItemInputLine("Group Name", spec.groupName, function(val:String):void {
119                                        spec.groupName = val;
120                                })
121                        BindingUtils.bindSetter(function(val:Boolean):void {
122                                        result.visible = !val;
123                                }, spec, "isProfile");
124                        return result;
125                }
126
127                private function createHeading():FormItem {
128                        var heading:FormItem = new FormItem();
129                        heading.label = "Header";
130                        heading.styleName = StyleConstants.XMLBROWSER_HEADER;
131                        return heading;
132                }
133
134                private function handleComponents(components:ArrayCollection):void {
135                        for each (var component:CMDComponent in components) {
136                                addComponent(component);
137                        }
138                }
139
140                public function addComponent(component:CMDComponent, index:int = -1):void {
141                        var comp:Container = new ComponentEdit(component, this);
142                        comp.addEventListener(ComponentEdit.REMOVE_COMPONENT_EVENT, removeComponent);
143                        if (index == -1) {
144                                addChild(comp);
145                        } else {
146                                addChildAt(comp, index);
147                        }
148                }
149
150                private function removeComponent(event:Event):void {
151                        var comp:CMDComponent = ComponentEdit(event.currentTarget).component;
152                        _spec.removeComponent(comp);
153                        removeChild(event.currentTarget as DisplayObject);
154                }
155
156        }
157
158}
Note: See TracBrowser for help on using the repository browser.