1 | package clarin.cmdi.componentregistry.common.components { |
---|
2 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
3 | import clarin.cmdi.componentregistry.common.StyleConstants; |
---|
4 | import clarin.cmdi.componentregistry.editor.CMDSpecRenderer; |
---|
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.utils.getTimer; |
---|
14 | |
---|
15 | import mx.collections.ArrayCollection; |
---|
16 | import mx.containers.Form; |
---|
17 | import mx.containers.FormItem; |
---|
18 | import mx.core.Container; |
---|
19 | import mx.core.UIComponent; |
---|
20 | import mx.events.ChildExistenceChangedEvent; |
---|
21 | import mx.events.DragEvent; |
---|
22 | import mx.managers.DragManager; |
---|
23 | import mx.managers.IFocusManagerComponent; |
---|
24 | |
---|
25 | [Event(name="editorChange", type="flash.events.Event")] |
---|
26 | public class CMDComponentXMLEditor extends Form implements IFocusManagerComponent, CMDSpecRenderer { |
---|
27 | |
---|
28 | public static const DRAG_NEW_COMPONENT:String = "newComponent"; |
---|
29 | public static const DRAG_NEW_ELEMENT:String = "newElement"; |
---|
30 | public static const DRAG_NEW_ATTRIBUTE:String = "newAttribute"; |
---|
31 | |
---|
32 | public static const EDITOR_CHANGE_EVENT:String = "editorChange"; |
---|
33 | private var _spec:CMDSpec; |
---|
34 | |
---|
35 | public function CMDComponentXMLEditor() { |
---|
36 | super(); |
---|
37 | focusEnabled = true; |
---|
38 | tabEnabled = true; |
---|
39 | styleName = StyleConstants.XMLBROWSER; |
---|
40 | addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler); |
---|
41 | addEventListener(DragEvent.DRAG_OVER, dragOverHandler); |
---|
42 | addEventListener(DragEvent.DRAG_DROP, dragDropHandler); |
---|
43 | addEventListener(ChildExistenceChangedEvent.CHILD_ADD, dispatchEditorChangeEvent); |
---|
44 | addEventListener(ChildExistenceChangedEvent.CHILD_REMOVE, dispatchEditorChangeEvent); |
---|
45 | } |
---|
46 | |
---|
47 | private function dragEnterHandler(event:DragEvent):void { |
---|
48 | DragManager.acceptDragDrop(event.currentTarget as UIComponent); |
---|
49 | UIComponent(event.currentTarget).drawFocus(true); |
---|
50 | } |
---|
51 | |
---|
52 | private function dragOverHandler(event:DragEvent):void { |
---|
53 | if (event.dragSource.hasFormat("items")) { |
---|
54 | DragManager.showFeedback(DragManager.COPY); |
---|
55 | } else if (event.dragSource.hasFormat(DRAG_NEW_COMPONENT)) { |
---|
56 | DragManager.showFeedback(DragManager.COPY); |
---|
57 | } else { |
---|
58 | DragManager.showFeedback(DragManager.NONE); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | private function dragDropHandler(event:DragEvent):void { |
---|
63 | if (event.dragSource.hasFormat("items")) { |
---|
64 | var items:Array = event.dragSource.dataForFormat("items") as Array; |
---|
65 | for each (var item:ItemDescription in items) { |
---|
66 | var comp:CMDComponent = new CMDComponent(); |
---|
67 | comp.componentId = item.id; |
---|
68 | _spec.cmdComponents.addItem(comp); |
---|
69 | addComponent(comp); |
---|
70 | } |
---|
71 | } else if (event.dragSource.hasFormat(DRAG_NEW_COMPONENT)) { |
---|
72 | var emptyComp:CMDComponent = event.dragSource.dataForFormat(DRAG_NEW_COMPONENT) as CMDComponent; |
---|
73 | _spec.cmdComponents.addItem(emptyComp); |
---|
74 | addComponent(emptyComp); |
---|
75 | } |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | public function set cmdSpec(cmdSpec:CMDSpec):void { |
---|
80 | _spec = cmdSpec; |
---|
81 | createNewBrowser(); |
---|
82 | dispatchEditorChangeEvent(); |
---|
83 | } |
---|
84 | |
---|
85 | private function dispatchEditorChangeEvent(event:Event = null):void { |
---|
86 | dispatchEvent(new Event(EDITOR_CHANGE_EVENT)); |
---|
87 | } |
---|
88 | |
---|
89 | [Bindable] |
---|
90 | public function get cmdSpec():CMDSpec { |
---|
91 | return _spec; |
---|
92 | } |
---|
93 | |
---|
94 | private function createNewBrowser():void { |
---|
95 | var start:int = getTimer(); |
---|
96 | removeAllChildren() |
---|
97 | handleHeader(_spec); |
---|
98 | handleComponents(_spec.cmdComponents); |
---|
99 | trace("Created editor view in " + (getTimer() - start) + " ms."); |
---|
100 | } |
---|
101 | |
---|
102 | private function handleHeader(spec:CMDSpec):void { |
---|
103 | addChild(createHeading()); |
---|
104 | addChild(new FormItemInputLine("Name", spec.headerName, function(val:String):void { |
---|
105 | spec.headerName = val; |
---|
106 | })); |
---|
107 | addChild(new FormItemInputLine("Id", spec.headerId, function(val:String):void { |
---|
108 | spec.headerId = val; |
---|
109 | })); |
---|
110 | addChild(new FormItemInputText(XMLBrowser.DESCRIPTION, spec.headerDescription, function(val:String):void { |
---|
111 | spec.headerDescription = val; |
---|
112 | })); |
---|
113 | } |
---|
114 | |
---|
115 | private function createHeading():FormItem { |
---|
116 | var heading:FormItem = new FormItem(); |
---|
117 | heading.label = "Header"; |
---|
118 | heading.styleName = StyleConstants.XMLBROWSER_HEADER; |
---|
119 | return heading; |
---|
120 | } |
---|
121 | |
---|
122 | private function handleComponents(components:ArrayCollection):void { |
---|
123 | for each (var component:CMDComponent in components) { |
---|
124 | addComponent(component); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | public function addComponent(component:CMDComponent, index:int = -1):void { |
---|
129 | var comp:Container = new ComponentEdit(component, this); |
---|
130 | comp.addEventListener(ComponentEdit.REMOVE_COMPONENT_EVENT, removeComponent); |
---|
131 | if (index == -1) { |
---|
132 | addChild(comp); |
---|
133 | } else { |
---|
134 | addChildAt(comp, index); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | private function removeComponent(event:Event):void { |
---|
139 | var comp:CMDComponent = ComponentEdit(event.currentTarget).component; |
---|
140 | _spec.removeComponent(comp); |
---|
141 | removeChild(event.currentTarget as DisplayObject); |
---|
142 | } |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | } |
---|