1 | package 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.components.ExpandingComponentLabel; |
---|
6 | import clarin.cmdi.componentregistry.editor.model.CMDComponent; |
---|
7 | import clarin.cmdi.componentregistry.editor.model.CMDComponentElement; |
---|
8 | |
---|
9 | import flash.display.DisplayObject; |
---|
10 | import flash.events.Event; |
---|
11 | import flash.events.MouseEvent; |
---|
12 | |
---|
13 | import mx.collections.ArrayCollection; |
---|
14 | import mx.containers.Form; |
---|
15 | import mx.containers.FormItem; |
---|
16 | import mx.containers.HBox; |
---|
17 | import mx.controls.Button; |
---|
18 | import mx.controls.HRule; |
---|
19 | import mx.core.Container; |
---|
20 | import mx.core.UIComponent; |
---|
21 | import mx.events.DragEvent; |
---|
22 | import mx.managers.DragManager; |
---|
23 | |
---|
24 | [Event(name="removeComponent", type="flash.events.Event")] |
---|
25 | public class ComponentEdit extends Form { |
---|
26 | public static const REMOVE_COMPONENT_EVENT:String = "removeComponent"; |
---|
27 | |
---|
28 | private var _parent:UIComponent; |
---|
29 | private var _component:CMDComponent; |
---|
30 | private var currentElementIndex:int = -1; |
---|
31 | |
---|
32 | public function ComponentEdit(component:CMDComponent, parent:UIComponent) { |
---|
33 | super(); |
---|
34 | _component = component; |
---|
35 | _parent = parent; |
---|
36 | styleName = StyleConstants.XMLBROWSER; |
---|
37 | if (!component.componentId) { // new empty component, otherwise it would be an already existed component which cannot be edited. |
---|
38 | addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler); |
---|
39 | addEventListener(DragEvent.DRAG_OVER, dragOverHandler); |
---|
40 | addEventListener(DragEvent.DRAG_DROP, dragDropHandler); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | public function get component():CMDComponent { |
---|
45 | return _component; |
---|
46 | } |
---|
47 | |
---|
48 | private function dragEnterHandler(event:DragEvent):void { |
---|
49 | DragManager.acceptDragDrop(event.currentTarget as UIComponent); |
---|
50 | UIComponent(event.currentTarget).drawFocus(true); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | private function dragOverHandler(event:DragEvent):void { |
---|
55 | if (event.dragSource.hasFormat("items")) { |
---|
56 | DragManager.showFeedback(DragManager.COPY); |
---|
57 | } else if (event.dragSource.hasFormat(CMDComponentXMLEditor.DRAG_NEW_COMPONENT)) { |
---|
58 | DragManager.showFeedback(DragManager.COPY); |
---|
59 | } else if (event.dragSource.hasFormat(CMDComponentXMLEditor.DRAG_NEW_ELEMENT)) { |
---|
60 | DragManager.showFeedback(DragManager.COPY); |
---|
61 | } else { |
---|
62 | DragManager.showFeedback(DragManager.NONE); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | private function dragDropHandler(event:DragEvent):void { |
---|
67 | if (event.dragSource.hasFormat("items")) { |
---|
68 | var items:Array = event.dragSource.dataForFormat("items") as Array; |
---|
69 | for each (var item:ItemDescription in items) { |
---|
70 | var comp:CMDComponent = new CMDComponent(); |
---|
71 | comp.componentId = item.id; |
---|
72 | _component.cmdComponents.addItem(comp); |
---|
73 | addComponent(comp); |
---|
74 | } |
---|
75 | } else if (event.dragSource.hasFormat(CMDComponentXMLEditor.DRAG_NEW_COMPONENT)) { |
---|
76 | var emptyComp:CMDComponent = event.dragSource.dataForFormat(CMDComponentXMLEditor.DRAG_NEW_COMPONENT) as CMDComponent; |
---|
77 | _component.cmdComponents.addItem(emptyComp); |
---|
78 | addComponent(emptyComp); |
---|
79 | } else if (event.dragSource.hasFormat(CMDComponentXMLEditor.DRAG_NEW_ELEMENT)) { |
---|
80 | var element:CMDComponentElement = event.dragSource.dataForFormat(CMDComponentXMLEditor.DRAG_NEW_ELEMENT) as CMDComponentElement; |
---|
81 | _component.cmdElements.addItem(element); |
---|
82 | addElement(element, currentElementIndex); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | private function fireRemoveComponent(mouseEvent:MouseEvent):void { |
---|
87 | drawFocus(false); |
---|
88 | var event:Event = new Event(REMOVE_COMPONENT_EVENT); |
---|
89 | dispatchEvent(event); |
---|
90 | } |
---|
91 | |
---|
92 | protected override function createChildren():void { |
---|
93 | super.createChildren(); |
---|
94 | var ruler:HRule = new HRule(); |
---|
95 | ruler.percentWidth = 80; |
---|
96 | addChild(ruler); |
---|
97 | addChild(createComponentEditBar()); |
---|
98 | |
---|
99 | var componentLink:FormItem = createComponentLink(_component); |
---|
100 | if (componentLink != null) { |
---|
101 | addCardinalityInput(); |
---|
102 | addChild(componentLink); |
---|
103 | } else { |
---|
104 | addChild(new FormItemInputLine("Name", _component.name, function(val:String):void { |
---|
105 | _component.name = val; |
---|
106 | })); |
---|
107 | addChild(new ConceptLinkInput(XMLBrowser.CONCEPTLINK, _component.conceptLink, function(val:String):void { |
---|
108 | _component.conceptLink = val; |
---|
109 | })); |
---|
110 | addCardinalityInput(); |
---|
111 | handleCMDAttributeList(); |
---|
112 | handleElements(_component.cmdElements); |
---|
113 | handleComponents(_component.cmdComponents); //recursion |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | private function addCardinalityInput():void { |
---|
118 | addChild(new FormItemInputLine("CardinalityMin", _component.cardinalityMin, function(val:String):void { |
---|
119 | _component.cardinalityMin = val; |
---|
120 | })); |
---|
121 | addChild(new FormItemInputLine("CardinalityMax", _component.cardinalityMax, function(val:String):void { |
---|
122 | _component.cardinalityMax = val; |
---|
123 | })); |
---|
124 | } |
---|
125 | |
---|
126 | private function createComponentEditBar():HBox { |
---|
127 | var editBar:HBox = new HBox(); |
---|
128 | editBar.addChild(createHeading()); |
---|
129 | var removeButton:Button = new Button(); |
---|
130 | removeButton.height = 20; |
---|
131 | removeButton.label = "X"; |
---|
132 | removeButton.addEventListener(MouseEvent.CLICK, fireRemoveComponent); |
---|
133 | removeButton.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void { |
---|
134 | drawFocus(true); |
---|
135 | }); |
---|
136 | removeButton.addEventListener(MouseEvent.MOUSE_OUT, function(event:MouseEvent):void { |
---|
137 | drawFocus(false); |
---|
138 | }); |
---|
139 | |
---|
140 | editBar.addChild(removeButton); |
---|
141 | return editBar; |
---|
142 | } |
---|
143 | |
---|
144 | private function createComponentLink(component:CMDComponent):FormItem { |
---|
145 | if (component.componentId != "" && component.componentId != null) { |
---|
146 | var result:FormItem = new FormItem(); |
---|
147 | result.styleName = StyleConstants.XMLBROWSER_FIELD; |
---|
148 | result.label = XMLBrowser.COMPONENT_ID; |
---|
149 | result.addChild(new ExpandingComponentLabel(component.componentId, false)); |
---|
150 | return result; |
---|
151 | } |
---|
152 | return null; |
---|
153 | } |
---|
154 | |
---|
155 | private function handleCMDAttributeList():void { |
---|
156 | if (_component.attributeList.length > 0) |
---|
157 | addChild(new AttributeListEdit(_component.attributeList, this)); |
---|
158 | } |
---|
159 | |
---|
160 | private function handleComponents(components:ArrayCollection):void { |
---|
161 | for each (var component:CMDComponent in components) { |
---|
162 | addComponent(component); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | public function addComponent(component:CMDComponent, index:int = -1):void { |
---|
167 | var comp:Container = new ComponentEdit(component, this); |
---|
168 | comp.addEventListener(ComponentEdit.REMOVE_COMPONENT_EVENT, removeComponent); |
---|
169 | comp.setStyle("paddingLeft", "50"); |
---|
170 | if (index == -1) { |
---|
171 | addChild(comp); |
---|
172 | } else { |
---|
173 | addChildAt(comp, index); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | private function removeComponent(event:Event):void { |
---|
178 | var comp:CMDComponent = ComponentEdit(event.currentTarget).component; |
---|
179 | _component.removeComponent(comp); |
---|
180 | removeChild(event.currentTarget as DisplayObject); |
---|
181 | } |
---|
182 | |
---|
183 | private function handleElements(elements:ArrayCollection):void { |
---|
184 | for each (var element:CMDComponentElement in elements) { |
---|
185 | addElement(element); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | public function addElement(element:CMDComponentElement, index:int = -1):void { |
---|
190 | var elem:Container = new ElementEdit(element, this); |
---|
191 | elem.setStyle("paddingLeft", "50"); |
---|
192 | elem.addEventListener(ElementEdit.REMOVE_ELEMENT_EVENT, removeElement); |
---|
193 | if (index == -1) { |
---|
194 | addChild(elem); |
---|
195 | } else { |
---|
196 | addChildAt(elem, index); |
---|
197 | } |
---|
198 | currentElementIndex = getChildIndex(elem) + 1; |
---|
199 | } |
---|
200 | |
---|
201 | private function removeElement(event:Event):void { |
---|
202 | var elem:CMDComponentElement = ElementEdit(event.currentTarget).element; |
---|
203 | _component.removeElement(elem); |
---|
204 | removeChild(event.currentTarget as DisplayObject); |
---|
205 | currentElementIndex--; |
---|
206 | } |
---|
207 | |
---|
208 | private function createHeading():FormItem { |
---|
209 | var heading:FormItem = new FormItem(); |
---|
210 | heading.label = XMLBrowser.COMPONENT; |
---|
211 | heading.styleName = StyleConstants.XMLBROWSER_HEADER; |
---|
212 | return heading; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | } |
---|
217 | } |
---|