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