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

Last change on this file since 238 was 238, checked in by patdui, 14 years ago
File size: 4.9 KB
Line 
1package clarin.cmdi.componentregistry.editor {
2        import clarin.cmdi.componentregistry.common.StyleConstants;
3        import clarin.cmdi.componentregistry.editor.model.CMDAttribute;
4
5        import flash.events.Event;
6        import flash.events.MouseEvent;
7
8        import mx.binding.utils.BindingUtils;
9        import mx.collections.ArrayCollection;
10        import mx.collections.XMLListCollection;
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                        attributeBox.addChild(createAndAddValueScheme(attribute));
52                        return attributeBox;
53                }
54
55                private function createAndAddValueScheme(attribute:CMDAttribute):UIComponent {
56                        var valueSchemeInput:ValueSchemeInput = new ValueSchemeInput("Type");
57                        BindingUtils.bindSetter(function(val:String):void {
58                                        attribute.valueSchemePattern = val;
59                                        attribute.valueSchemeEnumeration = null;
60                                        attribute.type = "";
61                                }, valueSchemeInput, "valueSchemePattern");
62                        BindingUtils.bindSetter(function(val:String):void {
63                                        attribute.type = val;
64                                        attribute.valueSchemePattern ="";
65                                        attribute.valueSchemeEnumeration = null;
66                                }, valueSchemeInput, "valueSchemeSimple");
67                        BindingUtils.bindSetter(function(val:XMLListCollection):void {
68                                        attribute.valueSchemeEnumeration = val;
69                                        attribute.type = "";
70                                        attribute.valueSchemePattern = "";
71                                }, valueSchemeInput, "valueSchemeEnumeration");
72                        if (attribute.valueSchemeEnumeration == null) {
73                                if (attribute.valueSchemePattern != null) {
74                                        valueSchemeInput.valueSchemePattern = attribute.valueSchemePattern;
75                                } else {
76                                        valueSchemeInput.valueSchemeSimple = attribute.type;
77                                }
78                        } else {
79                                valueSchemeInput.valueSchemeEnumeration = attribute.valueSchemeEnumeration;
80                        }
81                        return valueSchemeInput;
82                }
83
84                private function addEditBar(attribute:CMDAttribute, attributeBox:Form):void {
85                        var name:FormItemInputLine = new FormItemInputLine("Name", attribute.name, function(val:String):void {
86                                        attribute.name = val;
87                                });
88                        name.direction = FormItemDirection.HORIZONTAL
89                        var removeButton:Button = new Button();
90                        removeButton.height = 20;
91                        removeButton.label = "X";
92                        removeButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
93                                        attributeBox.drawFocus(false);
94                                        removeAttribute(attribute);
95                                        removeChild(attributeBox);
96                                });
97                        removeButton.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void {
98                                        attributeBox.drawFocus(true);
99                                });
100                        removeButton.addEventListener(MouseEvent.MOUSE_OUT, function(event:MouseEvent):void {
101                                        attributeBox.drawFocus(false);
102                                });
103                        attributeBox.addChild(name);
104                        name.addChild(removeButton);
105                }
106
107                private function removeAttribute(attribute:CMDAttribute):void {
108                        var index:int = _attributes.getItemIndex(attribute);
109                        if (index != -1) {
110                                _attributes.removeItemAt(index);
111                        }
112                }
113
114                private function createHeading():FormItem {
115                        var heading:FormItem = new FormItem();
116                        heading.label = "AttributeList";
117                        heading.styleName = StyleConstants.XMLBROWSER_HEADER_SMALL;
118                        return heading;
119                }
120
121                private function dragEnterHandler(event:DragEvent):void {
122                        DragManager.acceptDragDrop(event.currentTarget as UIComponent);
123                        UIComponent(event.currentTarget).drawFocus(true);
124                }
125
126                private function dragOverHandler(event:DragEvent):void {
127                        if (event.dragSource.hasFormat(CMDComponentXMLEditor.DRAG_NEW_ATTRIBUTE)) {
128                                DragManager.showFeedback(DragManager.COPY);
129                        } else {
130                                DragManager.showFeedback(DragManager.NONE);
131                        }
132                }
133
134                private function dragDropHandler(event:DragEvent):void {
135                        if (event.dragSource.hasFormat(CMDComponentXMLEditor.DRAG_NEW_ATTRIBUTE)) {
136                                var attr:CMDAttribute = event.dragSource.dataForFormat(CMDComponentXMLEditor.DRAG_NEW_ATTRIBUTE) as CMDAttribute;
137                                _attributes.addItem(attr);
138                                addChild(createAttributeBox(attr));
139                        }
140                }
141
142        }
143}
Note: See TracBrowser for help on using the repository browser.