1 | package clarin.cmdi.componentregistry.common.components { |
---|
2 | import clarin.cmdi.componentregistry.editor.CMDSpecRenderer; |
---|
3 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
4 | import clarin.cmdi.componentregistry.common.StyleConstants; |
---|
5 | import clarin.cmdi.componentregistry.editor.ComponentEdit; |
---|
6 | import clarin.cmdi.componentregistry.editor.FormItemInputLine; |
---|
7 | import clarin.cmdi.componentregistry.editor.FormItemInputText; |
---|
8 | import clarin.cmdi.componentregistry.editor.model.CMDComponent; |
---|
9 | import clarin.cmdi.componentregistry.editor.model.CMDSpec; |
---|
10 | |
---|
11 | import flash.display.DisplayObject; |
---|
12 | import flash.events.Event; |
---|
13 | import flash.events.MouseEvent; |
---|
14 | import flash.utils.getTimer; |
---|
15 | |
---|
16 | import mx.collections.ArrayCollection; |
---|
17 | import mx.containers.Form; |
---|
18 | import mx.containers.FormItem; |
---|
19 | import mx.controls.Button; |
---|
20 | import mx.core.Container; |
---|
21 | import mx.core.UIComponent; |
---|
22 | import mx.events.ChildExistenceChangedEvent; |
---|
23 | import mx.events.DragEvent; |
---|
24 | import mx.managers.DragManager; |
---|
25 | import mx.managers.IFocusManagerComponent; |
---|
26 | |
---|
27 | [Event(name="editorChange", type="flash.events.Event")] |
---|
28 | public class CMDComponentXMLEditor extends Form implements IFocusManagerComponent, CMDSpecRenderer { |
---|
29 | |
---|
30 | public static const EDITOR_CHANGE_EVENT:String = "editorChange"; |
---|
31 | private var _spec:CMDSpec; |
---|
32 | |
---|
33 | public function CMDComponentXMLEditor() { |
---|
34 | super(); |
---|
35 | focusEnabled = true; |
---|
36 | tabEnabled = true; |
---|
37 | styleName = StyleConstants.XMLBROWSER; |
---|
38 | addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler); |
---|
39 | addEventListener(DragEvent.DRAG_OVER, dragOverHandler); |
---|
40 | addEventListener(DragEvent.DRAG_DROP, dragDropHandler); |
---|
41 | addEventListener(ChildExistenceChangedEvent.CHILD_ADD, dispatchEditorChangeEvent); |
---|
42 | addEventListener(ChildExistenceChangedEvent.CHILD_REMOVE, dispatchEditorChangeEvent); |
---|
43 | } |
---|
44 | |
---|
45 | private function dragEnterHandler(event:DragEvent):void { |
---|
46 | DragManager.acceptDragDrop(event.currentTarget as UIComponent); |
---|
47 | UIComponent(event.currentTarget).drawFocus(true); |
---|
48 | } |
---|
49 | |
---|
50 | private function dragOverHandler(event:DragEvent):void { |
---|
51 | if (event.dragSource.hasFormat("items")) { |
---|
52 | DragManager.showFeedback(DragManager.COPY); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | private function dragDropHandler(event:DragEvent):void { |
---|
57 | var items:Array = event.dragSource.dataForFormat("items") as Array; |
---|
58 | for each (var item:ItemDescription in items) { |
---|
59 | var comp:CMDComponent = new CMDComponent(); |
---|
60 | comp.componentId = item.id; |
---|
61 | _spec.cmdComponents.addItem(comp); |
---|
62 | addComponent(comp); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | public function set cmdSpec(cmdSpec:CMDSpec):void { |
---|
67 | _spec = cmdSpec; |
---|
68 | createNewBrowser(); |
---|
69 | dispatchEditorChangeEvent(); |
---|
70 | } |
---|
71 | |
---|
72 | private function dispatchEditorChangeEvent(event:Event=null):void { |
---|
73 | dispatchEvent(new Event(EDITOR_CHANGE_EVENT)); |
---|
74 | } |
---|
75 | |
---|
76 | [Bindable] |
---|
77 | public function get cmdSpec():CMDSpec { |
---|
78 | return _spec; |
---|
79 | } |
---|
80 | |
---|
81 | private function createNewBrowser():void { |
---|
82 | var start:int = getTimer(); |
---|
83 | removeAllChildren() |
---|
84 | handleHeader(_spec); |
---|
85 | handleComponents(_spec.cmdComponents); |
---|
86 | trace("Created browser2 view in " + (getTimer() - start) + " ms."); |
---|
87 | } |
---|
88 | |
---|
89 | private function handleHeader(spec:CMDSpec):void { |
---|
90 | addChild(createHeading()); |
---|
91 | addChild(new FormItemInputLine("Name", spec.headerName, function(val:String):void { |
---|
92 | spec.headerName = val; |
---|
93 | })); |
---|
94 | addChild(new FormItemInputLine("Id", spec.headerId, function(val:String):void { |
---|
95 | spec.headerId = val; |
---|
96 | })); |
---|
97 | addChild(new FormItemInputText(XMLBrowser.DESCRIPTION, spec.headerDescription, function(val:String):void { |
---|
98 | spec.headerDescription = val; |
---|
99 | })); |
---|
100 | } |
---|
101 | |
---|
102 | private function createHeading():FormItem { |
---|
103 | var heading:FormItem = new FormItem(); |
---|
104 | heading.label = "Header"; |
---|
105 | heading.styleName = StyleConstants.XMLBROWSER_HEADER; |
---|
106 | return heading; |
---|
107 | } |
---|
108 | |
---|
109 | private function handleComponents(components:ArrayCollection):void { |
---|
110 | for each (var component:CMDComponent in components) { |
---|
111 | addComponent(component); |
---|
112 | } |
---|
113 | var btn:Button = new Button(); |
---|
114 | btn.label = "add Component"; |
---|
115 | btn.addEventListener(MouseEvent.CLICK, handleAddComponentClick); |
---|
116 | btn.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void {drawFocus(true);}); |
---|
117 | btn.addEventListener(MouseEvent.MOUSE_OUT, function(event:MouseEvent):void {drawFocus(false);}); |
---|
118 | addChild(btn); |
---|
119 | } |
---|
120 | |
---|
121 | private function handleAddComponentClick(event:MouseEvent):void { |
---|
122 | var comp:CMDComponent = new CMDComponent(); |
---|
123 | _spec.cmdComponents.addItem(comp); |
---|
124 | var index:int = getChildIndex(event.currentTarget as DisplayObject); |
---|
125 | addComponent(comp, index); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | public function addComponent(component:CMDComponent, index:int=-1):void { |
---|
131 | var comp:Container = new ComponentEdit(component, this); |
---|
132 | comp.addEventListener(ComponentEdit.REMOVE_COMPONENT_EVENT, removeComponent); |
---|
133 | if (index == -1) { |
---|
134 | addChild(comp); |
---|
135 | } else { |
---|
136 | addChildAt(comp, index); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | private function removeComponent(event:Event):void { |
---|
141 | var comp:CMDComponent = ComponentEdit(event.currentTarget).component; |
---|
142 | _spec.removeComponent(comp); |
---|
143 | removeChild(event.currentTarget as DisplayObject); |
---|
144 | } |
---|
145 | |
---|
146 | // protected override function createFormHeading(name:String):UIComponent { |
---|
147 | // var heading:UIComponent = super.createFormHeading(name); |
---|
148 | // if (name == COMPONENT) { |
---|
149 | // var editBar:HBox = new HBox(); |
---|
150 | // editBar.addChild(heading); |
---|
151 | // editBar.percentWidth = 100; |
---|
152 | // var spacer:Spacer = new Spacer(); |
---|
153 | // spacer.percentWidth = 90; |
---|
154 | // editBar.addChild(spacer); |
---|
155 | // var removeButton:Button = new Button(); |
---|
156 | // removeButton.height = 20; |
---|
157 | // removeButton.label = "X"; |
---|
158 | // removeButton.addEventListener(MouseEvent.CLICK, removeComponent); |
---|
159 | // editBar.addChild(removeButton); |
---|
160 | // return editBar; |
---|
161 | // } else { |
---|
162 | // return heading; |
---|
163 | // } |
---|
164 | // } |
---|
165 | // |
---|
166 | // protected override function createAndAddFormChild(name:String, value:String, bindingFunction:Function = null):void { |
---|
167 | // addFormChild(createFormItem(name, value, bindingFunction)); //Always add all children so they can be edited |
---|
168 | // } |
---|
169 | // |
---|
170 | // private function removeComponent(event:MouseEvent):void { |
---|
171 | // Alert.show("Fire remove!"); |
---|
172 | // } |
---|
173 | // |
---|
174 | // protected override function createFormItemFieldValue(name:String, value:String, bindingFunction:Function = null):DisplayObject { |
---|
175 | // if (name == COMPONENT_ID) { |
---|
176 | // return createComponentIdLabel(value); |
---|
177 | // } else { |
---|
178 | // return createDefaultEditField(name, value, bindingFunction); |
---|
179 | // } |
---|
180 | // } |
---|
181 | // |
---|
182 | // private function createDefaultEditField(name:String, value:String, bindingFunction:Function = null):UIComponent { |
---|
183 | // var result:UIComponent; |
---|
184 | // if (name == DESCRIPTION) { |
---|
185 | // var editArea:TextArea = new TextArea(); |
---|
186 | // editArea.text = value; |
---|
187 | // editArea.styleName = StyleConstants.XMLEDITOR_EDIT_FIELD; |
---|
188 | // editArea.width = 400; |
---|
189 | // result = editArea; |
---|
190 | // } else { |
---|
191 | // var editField:TextInput = new TextInput(); |
---|
192 | // editField.text = value; |
---|
193 | // editField.styleName = StyleConstants.XMLEDITOR_EDIT_FIELD; |
---|
194 | // editField.width = 400; |
---|
195 | // result = editField; |
---|
196 | // BindingUtils.bindSetter(bindingFunction, editField, "text"); |
---|
197 | // } |
---|
198 | // return result; |
---|
199 | // } |
---|
200 | // |
---|
201 | // private function createComponentIdLabel(value:String):ExpandingComponentLabel { |
---|
202 | // var componentLabel:ExpandingComponentLabel = new ExpandingComponentLabel(value, true); |
---|
203 | // return componentLabel; |
---|
204 | // } |
---|
205 | // |
---|
206 | } |
---|
207 | |
---|
208 | } |
---|