source: ComponentRegistry/trunk/ComponentBrowserGui/src/main/flex/clarin/cmdi/componentregistry/editor/ComponentEdit.as @ 1773

Last change on this file since 1773 was 1773, checked in by twagoo, 12 years ago

Created ChildNameValidator? which checks if a component or element name is unique among siblings. Both ComponentEdit? and ElementEdit? add set it as a validator on the NameInputLine? field, which now takes a validator as constructor parameter.

File size: 9.9 KB
Line 
1package clarin.cmdi.componentregistry.editor {
2        import clarin.cmdi.componentregistry.common.ItemDescription;
3        import clarin.cmdi.componentregistry.common.LabelConstants;
4        import clarin.cmdi.componentregistry.common.StyleConstants;
5        import clarin.cmdi.componentregistry.common.components.AddComponentLabelButton;
6        import clarin.cmdi.componentregistry.common.components.AddElementLabelButton;
7        import clarin.cmdi.componentregistry.common.components.DownIconButton;
8        import clarin.cmdi.componentregistry.common.components.ExpandingComponentLabel;
9        import clarin.cmdi.componentregistry.common.components.RemoveLabelButton;
10        import clarin.cmdi.componentregistry.common.components.UpIconButton;
11        import clarin.cmdi.componentregistry.editor.model.CMDComponent;
12        import clarin.cmdi.componentregistry.editor.model.CMDComponentElement;
13       
14        import flash.display.DisplayObject;
15        import flash.events.Event;
16        import flash.events.MouseEvent;
17       
18        import mx.collections.ArrayCollection;
19        import mx.containers.Form;
20        import mx.containers.FormItem;
21        import mx.containers.HBox;
22        import mx.controls.Button;
23        import mx.controls.HRule;
24        import mx.controls.Label;
25        import mx.core.Container;
26        import mx.core.UIComponent;
27        import mx.events.DragEvent;
28        import mx.managers.DragManager;
29       
30        [Event(name="removeComponent", type="flash.events.Event")]
31        public class ComponentEdit extends Form {
32                public static const REMOVE_COMPONENT_EVENT:String = "removeComponent";
33                private static const DRAG_ITEMS:String = "items";
34               
35                private var _parent:UIComponent;
36                private var _component:CMDComponent;
37                private var _parentComponent:CMDComponent;
38                private var addComponentLabel:Label
39                private var addElementLabel:Label
40               
41               
42                public function ComponentEdit(component:CMDComponent, parent:UIComponent, parentComponent:CMDComponent) {
43                        super();
44                        _component = component;
45                        _parentComponent = parentComponent;
46                        _parent = parent;
47                        styleName = StyleConstants.XMLBROWSER;
48                        if (!component.componentId) { // new empty component, otherwise it would be an already existed component which cannot be edited.
49                                addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler);
50                                addEventListener(DragEvent.DRAG_OVER, dragOverHandler);
51                                addEventListener(DragEvent.DRAG_DROP, dragDropHandler);
52                        }
53                }
54               
55                public function get component():CMDComponent {
56                        return _component;
57                }
58               
59                public function get parentComponent():CMDComponent {
60                        return _parentComponent;
61                }
62               
63                private function dragEnterHandler(event:DragEvent):void {
64                        DragManager.acceptDragDrop(event.currentTarget as UIComponent);
65                        UIComponent(event.currentTarget).drawFocus(true);
66                }
67               
68               
69                private function dragOverHandler(event:DragEvent):void {
70                        if (event.dragSource.hasFormat(DRAG_ITEMS)) {
71                                DragManager.showFeedback(DragManager.COPY);
72                        } else {
73                                DragManager.showFeedback(DragManager.NONE);
74                        }
75                }
76               
77                private function dragDropHandler(event:DragEvent):void {
78                        if (event.dragSource.hasFormat(DRAG_ITEMS)) {
79                                var items:Array = event.dragSource.dataForFormat(DRAG_ITEMS) as Array;
80                                for each (var item:ItemDescription in items) {
81                                        var comp:CMDComponent = new CMDComponent();
82                                        comp.componentId = item.id;
83                                        _component.cmdComponents.addItem(comp);
84                                        addComponent(comp);
85                                }
86                        }
87                }
88               
89                private function fireRemoveComponent(mouseEvent:MouseEvent):void {
90                        drawFocus(false);
91                        var event:Event = new Event(REMOVE_COMPONENT_EVENT);
92                        dispatchEvent(event);
93                }
94               
95                protected override function createChildren():void {
96                        super.createChildren();
97                        addRuler();
98                        createComponentEditBar();
99                       
100                        var componentLink:FormItem = createComponentLink(_component);
101                        if (componentLink != null) {
102                                addCardinalityInput();
103                                addChild(componentLink);
104                        } else {
105                                addNameInput();
106                                addConceptLink();
107                                addCardinalityInput();
108                                handleCMDAttributeList();
109                                handleElements(_component.cmdElements);
110                                addElementAddButton();
111                                handleComponents(_component.cmdComponents); //recursion
112                                addComponentAddButton();
113                        }
114                }
115               
116                private function addComponentAddButton():void {
117                        addComponentLabel = new AddComponentLabelButton();
118                        addComponentLabel.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
119                                var comp:CMDComponent = CMDComponent.createEmptyComponent();
120                                _component.cmdComponents.addItem(comp);
121                                addComponent(comp);
122                        });
123                        addComponentLabel.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void {
124                                drawFocus(true);
125                        });
126                        addComponentLabel.addEventListener(MouseEvent.MOUSE_OUT, function(event:MouseEvent):void {
127                                drawFocus(false);
128                        });
129                        addChild(addComponentLabel);
130                }
131               
132                private function addElementAddButton():void {
133                        addElementLabel = new AddElementLabelButton();
134                        addElementLabel.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
135                                var element:CMDComponentElement = CMDComponentElement.createEmptyElement();
136                                _component.cmdElements.addItem(element);
137                                addElement(element);
138                               
139                        });
140                        addElementLabel.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void {
141                                drawFocus(true);
142                        });
143                        addElementLabel.addEventListener(MouseEvent.MOUSE_OUT, function(event:MouseEvent):void {
144                                drawFocus(false);
145                        });
146                        addChild(addElementLabel);
147                }
148               
149                private function addConceptLink():void {
150                        addChild(new ConceptLinkInput(LabelConstants.CONCEPTLINK, _component.conceptLink, function(val:String):void {
151                                _component.conceptLink = val;
152                        }));
153                }
154               
155                private function addRuler():void {
156                        var ruler:HRule = new HRule();
157                        ruler.percentWidth = 80;
158                        addChild(ruler);
159                }
160               
161                private function addNameInput():void {
162                        var nameInput:FormItemInputLine = new NameInputLine(_component.name, function(val:String):void {
163                                _component.name = val; 
164                        }, new ChildNameValidator(_parentComponent, component));
165                        addChild(nameInput);
166                }
167               
168                private function addCardinalityInput():void {
169                        addChild(new CardinalityInput(LabelConstants.CARDINALITY_MIN, _component.cardinalityMin, function(val:String):void {
170                                _component.cardinalityMin = val;
171                        }));
172                        addChild(new CardinalityInput(LabelConstants.CARDINALITY_MAX, _component.cardinalityMax, function(val:String):void {
173                                _component.cardinalityMax = val;
174                        }));
175                }
176               
177                private function createComponentEditBar():void {
178                        var editBar:HBox = new HBox();
179                        editBar.addChild(createHeading());
180                        var removeButton:Label = new RemoveLabelButton();
181                        addFocusListeners(removeButton).addEventListener(MouseEvent.CLICK, fireRemoveComponent);
182                        editBar.addChild(removeButton);
183                       
184                        var downButton:Button = new DownIconButton();
185                        addFocusListeners(downButton).addEventListener(MouseEvent.CLICK, moveDownComponent);
186                        editBar.addChild(downButton);
187                       
188                        var upButton:Button = new UpIconButton();
189                        addFocusListeners(upButton).addEventListener(MouseEvent.CLICK, moveUpComponent);
190                        editBar.addChild(upButton);
191                        addChild(editBar);
192                }
193               
194                private function moveDownComponent(event:Event):void {
195                        var comp:CMDComponent = component;
196                        if (parentComponent.moveDownComponent(comp)) {
197                                var index:int = _parent.getChildIndex(this);
198                                if (index != _parent.numChildren - 1) {
199                                        _parent.removeChild(this);
200                                        _parent.addChildAt(this, index + 1);
201                                }
202                        }
203                }
204               
205                private function moveUpComponent(event:Event):void {
206                        var comp:CMDComponent = component;
207                        if (parentComponent.moveUpComponent(comp)) {
208                                var index:int = _parent.getChildIndex(this);
209                                if (index != 0) {
210                                        _parent.removeChild(this);
211                                        _parent.addChildAt(this, index - 1);
212                                }
213                        }
214                }
215               
216                private function addFocusListeners(comp:UIComponent):UIComponent {
217                        comp.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void {
218                                drawFocus(true);
219                        });
220                        comp.addEventListener(MouseEvent.MOUSE_OUT, function(event:MouseEvent):void {
221                                drawFocus(false);
222                        });
223                        return comp;
224                }
225               
226                private function createComponentLink(component:CMDComponent):FormItem {
227                        if (component.componentId != "" && component.componentId != null) {
228                                var result:FormItem = new FormItem();
229                                result.styleName = StyleConstants.XMLBROWSER_FIELD;
230                                result.label = LabelConstants.COMPONENT_ID;
231                                result.addChild(new ExpandingComponentLabel(component.componentId, false));
232                                return result;
233                        }
234                        return null;
235                }
236               
237                private function handleCMDAttributeList():void {
238                        addChild(new AttributeListEdit(_component.attributeList, this));
239                }
240               
241                private function handleComponents(components:ArrayCollection):void {
242                        for each (var component:CMDComponent in components) {
243                                addComponent(component);
244                        }
245                }
246               
247                public function addComponent(component:CMDComponent):void {
248                        var comp:Container = new ComponentEdit(component, this, _component);
249                        comp.addEventListener(ComponentEdit.REMOVE_COMPONENT_EVENT, removeComponent);
250                        comp.setStyle("paddingLeft", "50");
251                        if (!addComponentLabel) {
252                                addChild(comp);
253                        } else {
254                                addChildAt(comp, getChildIndex(addComponentLabel));
255                        }
256                }
257               
258                private function removeComponent(event:Event):void {
259                        var comp:CMDComponent = ComponentEdit(event.currentTarget).component;
260                        _component.removeComponent(comp);
261                        removeChild(event.currentTarget as DisplayObject);
262                }
263               
264                private function handleElements(elements:ArrayCollection):void {
265                        for each (var element:CMDComponentElement in elements) {
266                                addElement(element);
267                        }
268                }
269               
270                public function addElement(element:CMDComponentElement):void {
271                        var elem:Container = new ElementEdit(element, this, _component);
272                        elem.setStyle("paddingLeft", "50");
273                        elem.addEventListener(ElementEdit.REMOVE_ELEMENT_EVENT, removeElement);
274                        if (!addElementLabel) {
275                                addChild(elem);
276                        } else {
277                                addChildAt(elem, getChildIndex(addElementLabel));
278                        }
279                }
280               
281                private function removeElement(event:Event):void {
282                        var elem:CMDComponentElement = ElementEdit(event.currentTarget).element;
283                        _component.removeElement(elem);
284                        removeChild(event.currentTarget as DisplayObject);
285                }
286               
287                private function createHeading():FormItem {
288                        var heading:FormItem = new FormItem();
289                        heading.label = LabelConstants.COMPONENT;
290                        heading.styleName = StyleConstants.XMLBROWSER_HEADER;
291                        return heading;
292                }
293        }
294}
Note: See TracBrowser for help on using the repository browser.