1 | package clarin.cmdi.componentregistry.editor { |
---|
2 | import clarin.cmdi.componentregistry.common.StyleConstants; |
---|
3 | import clarin.cmdi.componentregistry.editor.model.CMDAttribute; |
---|
4 | import clarin.cmdi.componentregistry.editor.model.ValueSchemeInterface; |
---|
5 | |
---|
6 | import flash.events.Event; |
---|
7 | import flash.events.MouseEvent; |
---|
8 | |
---|
9 | import mx.binding.utils.ChangeWatcher; |
---|
10 | import mx.collections.ArrayCollection; |
---|
11 | import mx.collections.XMLListCollection; |
---|
12 | import mx.containers.Form; |
---|
13 | import mx.containers.FormItem; |
---|
14 | import mx.containers.FormItemDirection; |
---|
15 | import mx.controls.Button; |
---|
16 | import mx.core.UIComponent; |
---|
17 | import mx.events.DragEvent; |
---|
18 | import mx.events.PropertyChangeEvent; |
---|
19 | import mx.managers.DragManager; |
---|
20 | |
---|
21 | [Event(name="removeAttribute", type="flash.events.Event")] |
---|
22 | public class AttributeListEdit extends Form { |
---|
23 | public static const REMOVE_ATTRIBUTE_EVENT:String = "removeAttribute"; |
---|
24 | |
---|
25 | private var _attributes:ArrayCollection; |
---|
26 | private var _parent:UIComponent; |
---|
27 | |
---|
28 | public function AttributeListEdit(attributes:ArrayCollection, parent:UIComponent) { |
---|
29 | super(); |
---|
30 | _attributes = attributes; |
---|
31 | _parent = parent; |
---|
32 | styleName = StyleConstants.XMLBROWSER; |
---|
33 | addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler); |
---|
34 | addEventListener(DragEvent.DRAG_OVER, dragOverHandler); |
---|
35 | addEventListener(DragEvent.DRAG_DROP, dragDropHandler); |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | protected override function createChildren():void { |
---|
40 | super.createChildren(); |
---|
41 | addChild(createHeading()); |
---|
42 | if (_attributes.length > 0) { |
---|
43 | for each (var attribute:CMDAttribute in _attributes) { |
---|
44 | addChild(createAttributeBox(attribute)); |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | private function createAttributeBox(attribute:CMDAttribute):Form { |
---|
50 | var attributeBox:Form = new Form(); |
---|
51 | attributeBox.styleName = StyleConstants.XMLBROWSER; |
---|
52 | addEditBar(attribute, attributeBox); |
---|
53 | attributeBox.addChild(createAndAddValueScheme(attribute)); |
---|
54 | return attributeBox; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Public utility method to create ValueScheme Component. By lack of better placed put in this class |
---|
59 | **/ |
---|
60 | public static function createAndAddValueScheme(valueScheme:ValueSchemeInterface):UIComponent { |
---|
61 | var valueSchemeInput:ValueSchemeInput = new ValueSchemeInput("Type"); |
---|
62 | if (valueScheme.valueSchemeEnumeration == null) { |
---|
63 | if (valueScheme.valueSchemePattern) { |
---|
64 | valueSchemeInput.valueSchemePattern = valueScheme.valueSchemePattern; |
---|
65 | } else { |
---|
66 | valueSchemeInput.valueSchemeSimple = valueScheme.valueSchemeSimple; |
---|
67 | } |
---|
68 | } else { |
---|
69 | valueSchemeInput.valueSchemeEnumeration = valueScheme.valueSchemeEnumeration; |
---|
70 | } |
---|
71 | ChangeWatcher.watch(valueSchemeInput, "valueSchemeSimple", function(e:PropertyChangeEvent):void { |
---|
72 | valueScheme.valueSchemeSimple = e.newValue as String; |
---|
73 | valueScheme.valueSchemePattern = ""; |
---|
74 | valueScheme.valueSchemeEnumeration = null; |
---|
75 | }); |
---|
76 | ChangeWatcher.watch(valueSchemeInput, "valueSchemePattern", function(e:PropertyChangeEvent):void { |
---|
77 | valueScheme.valueSchemePattern = e.newValue as String; |
---|
78 | valueScheme.valueSchemeEnumeration = null; |
---|
79 | valueScheme.valueSchemeSimple = ""; |
---|
80 | }); |
---|
81 | ChangeWatcher.watch(valueSchemeInput, "valueSchemeEnumeration", function(e:PropertyChangeEvent):void { |
---|
82 | valueScheme.valueSchemeEnumeration = e.newValue as XMLListCollection; |
---|
83 | valueScheme.valueSchemeSimple = ""; |
---|
84 | valueScheme.valueSchemePattern = ""; |
---|
85 | }); |
---|
86 | return valueSchemeInput; |
---|
87 | } |
---|
88 | |
---|
89 | private function addEditBar(attribute:CMDAttribute, attributeBox:Form):void { |
---|
90 | var name:FormItemInputLine = new FormItemInputLine("Name", attribute.name, function(val:String):void { |
---|
91 | attribute.name = val; |
---|
92 | }); |
---|
93 | name.direction = FormItemDirection.HORIZONTAL |
---|
94 | var removeButton:Button = new Button(); |
---|
95 | removeButton.height = 20; |
---|
96 | removeButton.label = "X"; |
---|
97 | removeButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void { |
---|
98 | attributeBox.drawFocus(false); |
---|
99 | removeAttribute(attribute); |
---|
100 | removeChild(attributeBox); |
---|
101 | }); |
---|
102 | removeButton.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void { |
---|
103 | attributeBox.drawFocus(true); |
---|
104 | }); |
---|
105 | removeButton.addEventListener(MouseEvent.MOUSE_OUT, function(event:MouseEvent):void { |
---|
106 | attributeBox.drawFocus(false); |
---|
107 | }); |
---|
108 | attributeBox.addChild(name); |
---|
109 | name.addChild(removeButton); |
---|
110 | } |
---|
111 | |
---|
112 | private function removeAttribute(attribute:CMDAttribute):void { |
---|
113 | var index:int = _attributes.getItemIndex(attribute); |
---|
114 | if (index != -1) { |
---|
115 | _attributes.removeItemAt(index); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | private function createHeading():FormItem { |
---|
120 | var heading:FormItem = new FormItem(); |
---|
121 | heading.label = "AttributeList"; |
---|
122 | heading.styleName = StyleConstants.XMLBROWSER_HEADER_SMALL; |
---|
123 | return heading; |
---|
124 | } |
---|
125 | |
---|
126 | private function dragEnterHandler(event:DragEvent):void { |
---|
127 | DragManager.acceptDragDrop(event.currentTarget as UIComponent); |
---|
128 | UIComponent(event.currentTarget).drawFocus(true); |
---|
129 | } |
---|
130 | |
---|
131 | private function dragOverHandler(event:DragEvent):void { |
---|
132 | if (event.dragSource.hasFormat(CMDComponentXMLEditor.DRAG_NEW_ATTRIBUTE)) { |
---|
133 | DragManager.showFeedback(DragManager.COPY); |
---|
134 | } else { |
---|
135 | DragManager.showFeedback(DragManager.NONE); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | private function dragDropHandler(event:DragEvent):void { |
---|
140 | if (event.dragSource.hasFormat(CMDComponentXMLEditor.DRAG_NEW_ATTRIBUTE)) { |
---|
141 | var attr:CMDAttribute = event.dragSource.dataForFormat(CMDComponentXMLEditor.DRAG_NEW_ATTRIBUTE) as CMDAttribute; |
---|
142 | _attributes.addItem(attr); |
---|
143 | addChild(createAttributeBox(attr)); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|
148 | } |
---|