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

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