source: ComponentRegistry/trunk/ComponentBrowserGui/src/main/flex/clarin/cmdi/componentregistry/browser/XMLBrowser.as @ 269

Last change on this file since 269 was 269, checked in by patdui, 14 years ago
  • moved interface to common package
File size: 7.6 KB
Line 
1package clarin.cmdi.componentregistry.browser {
2        import clarin.cmdi.componentregistry.common.ComponentMD;
3        import clarin.cmdi.componentregistry.common.StyleConstants;
4        import clarin.cmdi.componentregistry.common.CMDSpecRenderer;
5        import clarin.cmdi.componentregistry.editor.model.CMDAttribute;
6        import clarin.cmdi.componentregistry.editor.model.CMDComponent;
7        import clarin.cmdi.componentregistry.editor.model.CMDComponentElement;
8        import clarin.cmdi.componentregistry.editor.model.CMDSpec;
9       
10        import flash.display.DisplayObject;
11        import flash.utils.getTimer;
12       
13        import mx.collections.ArrayCollection;
14        import mx.collections.XMLListCollection;
15        import mx.containers.Form;
16        import mx.containers.FormItem;
17        import mx.controls.ComboBox;
18        import mx.controls.HRule;
19        import mx.controls.Label;
20        import mx.controls.Text;
21        import mx.core.ClassFactory;
22        import mx.core.UIComponent;
23        import mx.managers.IFocusManagerComponent;
24
25        /**
26         * Generic XMLBrowser converts an xml file into a form. Use subclasses to override default methods and add custom behaviour.
27         */
28        public class XMLBrowser extends Form implements IFocusManagerComponent, CMDSpecRenderer {
29
30                //Label names
31                public static const CONCEPTLINK:String = "ConceptLink";
32                public static const COMPONENT:String = "Component";
33                public static const COMPONENT_ID:String = "ComponentId";
34                public static const DESCRIPTION:String = "Description";
35
36                private var _spec:CMDSpec;
37                private var addedChildren:ArrayCollection = new ArrayCollection();
38                protected var indent:Boolean = false;
39
40                public function XMLBrowser() {
41                        super();
42                        focusEnabled = true;
43                        tabEnabled = true;
44                }
45
46
47                public function set cmdSpec(cmdSpec:CMDSpec):void {
48                        _spec = cmdSpec;
49                        createNewBrowser();
50                }
51
52                [Bindable]
53                public function get cmdSpec():CMDSpec {
54                        return _spec;
55                }
56
57                private function createNewBrowser():void {
58                        var start:int = getTimer();
59                        removeAddedChildren();
60                        handleHeader(_spec);
61                        handleComponents(_spec.cmdComponents);
62                        trace("Created browser2 view in " + (getTimer() - start) + " ms.");
63                }
64
65                protected function addFormChild(child:UIComponent):void {
66                        if (indent) {
67                                child.setStyle("paddingLeft", "50");
68                        }
69                        addChildAt(child, addedChildren.length); //Add children before already added children (e.g. from mxml), they are at the bottom then. Can make this optional if needs be.
70                        addedChildren.addItem(child); //Only remove children we added so other GUI element with be retained.
71                }
72
73                protected function handleHeader(spec:CMDSpec):void {
74                        addFormHeading("Header");
75                        createAndAddFormChild("Name", spec.headerName);
76                        createAndAddFormChild(DESCRIPTION, spec.headerDescription);
77                        createAndAddFormChild("Id", spec.headerId);
78                }
79
80                protected function createAndAddFormChild(name:String, value:String):void {
81                        if (value) { //only add if we have somekind of value
82                                addFormChild(createFormItem(name, value));
83                        }
84                }
85
86                protected function handleComponents(components:ArrayCollection):void {
87                        for each (var component:CMDComponent in components) {
88                                addComponent(component);
89                        }
90                }
91
92                public function addComponent(component:CMDComponent):void {
93                        var ruler:HRule = new HRule();
94                        ruler.percentWidth = 80;
95                        addFormChild(ruler);
96                        addFormHeading(COMPONENT);
97                        createAndAddFormChild("Name", component.name);
98                        createAndAddFormChild(CONCEPTLINK, component.conceptLink);
99                        createAndAddFormChild("FileName", component.filename);
100                        if (component.cardinalityMin != "" || component.cardinalityMax != "")
101                                createAndAddFormChild("Cardinality", component.cardinalityMin + " - " + component.cardinalityMax);
102                        createAndAddFormChild(COMPONENT_ID, component.componentId);
103                        handleCMDAttributeList(component.attributeList);
104                        handleCMDElements(component.cmdElements);
105                        handleComponents(component.cmdComponents); //recursion
106                }
107
108                protected function handleCMDElements(elements:ArrayCollection):void {
109                        for each (var element:CMDComponentElement in elements) {
110                                indent = true;
111                                addFormHeading("Element");
112                                createAndAddFormChild("Name", element.name);
113                                createAndAddFormChild(CONCEPTLINK, element.conceptLink);
114                                if (element.cardinalityMin != "" || element.cardinalityMax != "")
115                                        createAndAddFormChild("Cardinality", element.cardinalityMin + " - " + element.cardinalityMax);
116                                handleCMDAttributeList(element.attributeList);
117                                createAndAddValueScheme(element.valueSchemeSimple, element.valueSchemePattern, element.valueSchemeEnumeration);
118                                indent = false;
119                        }
120                }
121
122                protected function handleCMDAttributeList(attributes:ArrayCollection):void {
123                        if (attributes.length > 0) {
124                                addFormHeading("AttributeList");
125                                for each (var attribute:CMDAttribute in attributes) {
126                                        createAndAddFormChild("Name", attribute.name);
127                                        if (!attribute.valueSchemeSimple) {
128                                                createAndAddFormChild("Type", attribute.valueSchemeSimple);
129                                        } else {
130                                                createAndAddValueScheme(null, attribute.valueSchemePattern, attribute.valueSchemeEnumeration);
131                                        }
132                                }
133                        }
134                }
135
136                protected function createAndAddValueScheme(value:String = null, valuePattern:String = null, valueList:XMLListCollection = null):void {
137                        var formItem:FormItem;
138                        if (valueList) {
139                                formItem = createFormItem("ValueScheme", null);
140                                var enumeration:DisplayObject = createEnumeration(valueList);
141                                formItem.addChild(enumeration);
142                        } else if (valuePattern) {
143                                formItem = createFormItem("ValueScheme", valuePattern);
144                        } else {
145                                formItem = createFormItem("ValueScheme", value);
146                        }
147                        addFormChild(formItem);
148                }
149
150                private function createEnumeration(enumeration:XMLListCollection):DisplayObject {
151                        var result:ComboBox = createValueSchemeComboBox();
152                        result.dataProvider = enumeration;
153                        return result;
154                }
155
156                public static function createValueSchemeComboBox():ComboBox {
157                        var result:ComboBox = new ComboBox();
158                        result.itemRenderer=new ClassFactory(Label);                   
159                        result.labelFunction = function(item:Object):String {
160                                var xmlItem:XML = item as XML;
161                                if (item.hasOwnProperty("@" + ComponentMD.APP_INFO) && xmlItem.attribute(ComponentMD.APP_INFO) != "") { //TODO PD what about conceptlinks? also not shown in Browser.
162                                        return xmlItem.attribute(ComponentMD.APP_INFO) + " - " + xmlItem.text();
163                                } else {
164                                        return xmlItem.text();
165                                }
166                        };
167                        return result;
168                }
169
170                /**
171                 * Responsible for creating a form item, override in subclasses to create different items if needed.
172                 * value is optional and added as a Text field if not null by default.
173                 * xmlElement is optional and ignored by default.
174                 */
175                protected function createFormItem(name:String, value:String = null):FormItem {
176                        var field:FormItem = new FormItem();
177                        field.label = name;
178                        field.styleName = StyleConstants.XMLBROWSER_FIELD;
179                        if (value != null && value != "") {
180                                var fieldValue:DisplayObject = createFormItemFieldValue(name, value);
181                                field.addChild(fieldValue);
182                        }
183                        return field;
184                }
185
186                protected function createFormItemFieldValue(name:String, value:String):DisplayObject {
187                        var fieldValue:Text = new Text();
188                        fieldValue.text = value;
189                        fieldValue.styleName = StyleConstants.XMLBROWSER_FIELD_VALUE;
190                        fieldValue.width = 500;
191                        return fieldValue;
192                }
193
194                protected function addFormHeading(name:String):void {
195                        addFormChild(createFormHeading(name));
196                }
197
198                protected function createFormHeading(name:String):UIComponent {
199                        var heading:FormItem = new FormItem();
200                        heading.label = name;
201                        if (name == ComponentMD.ATTRIBUTE_LIST) {
202                                heading.styleName = StyleConstants.XMLBROWSER_HEADER_SMALL;
203                        } else {
204                                heading.styleName = StyleConstants.XMLBROWSER_HEADER;
205                        }
206                        return heading;
207                }
208
209                private function removeAddedChildren():void {
210                        for each (var child:DisplayObject in addedChildren) {
211                                removeChild(child);
212                        }
213                        addedChildren = new ArrayCollection();
214                }
215
216        }
217
218
219}
Note: See TracBrowser for help on using the repository browser.