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

Last change on this file since 271 was 271, checked in by patdui, 14 years ago
File size: 7.5 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                                createAndAddValueScheme(element.valueSchemeSimple, element.valueSchemePattern, element.valueSchemeEnumeration);
117                                handleCMDAttributeList(element.attributeList);
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                                        createAndAddValueScheme(attribute.valueSchemeSimple, attribute.valueSchemePattern, attribute.valueSchemeEnumeration);
128                                }
129                        }
130                }
131
132                protected function createAndAddValueScheme(value:String = null, valuePattern:String = null, valueList:XMLListCollection = null):void {
133                        var formItem:FormItem;
134                        if (valueList) {
135                                formItem = createFormItem("Type", null);
136                                var enumeration:DisplayObject = createEnumeration(valueList);
137                                formItem.addChild(enumeration);
138                        } else if (valuePattern) {
139                                formItem = createFormItem("Type", valuePattern);
140                        } else {
141                                formItem = createFormItem("Type", value);
142                        }
143                        addFormChild(formItem);
144                }
145
146                private function createEnumeration(enumeration:XMLListCollection):DisplayObject {
147                        var result:ComboBox = createValueSchemeComboBox();
148                        result.dataProvider = enumeration;
149                        return result;
150                }
151
152                public static function createValueSchemeComboBox():ComboBox {
153                        var result:ComboBox = new ComboBox();
154                        result.itemRenderer=new ClassFactory(Label);                   
155                        result.labelFunction = function(item:Object):String {
156                                var xmlItem:XML = item as XML;
157                                if (item.hasOwnProperty("@" + ComponentMD.APP_INFO) && xmlItem.attribute(ComponentMD.APP_INFO) != "") { //TODO PD what about conceptlinks? also not shown in Browser.
158                                        return xmlItem.attribute(ComponentMD.APP_INFO) + " - " + xmlItem.text();
159                                } else {
160                                        return xmlItem.text();
161                                }
162                        };
163                        return result;
164                }
165
166                /**
167                 * Responsible for creating a form item, override in subclasses to create different items if needed.
168                 * value is optional and added as a Text field if not null by default.
169                 * xmlElement is optional and ignored by default.
170                 */
171                protected function createFormItem(name:String, value:String = null):FormItem {
172                        var field:FormItem = new FormItem();
173                        field.label = name;
174                        field.styleName = StyleConstants.XMLBROWSER_FIELD;
175                        if (value != null && value != "") {
176                                var fieldValue:DisplayObject = createFormItemFieldValue(name, value);
177                                field.addChild(fieldValue);
178                        }
179                        return field;
180                }
181
182                protected function createFormItemFieldValue(name:String, value:String):DisplayObject {
183                        var fieldValue:Text = new Text();
184                        fieldValue.text = value;
185                        fieldValue.styleName = StyleConstants.XMLBROWSER_FIELD_VALUE;
186                        fieldValue.width = 500;
187                        return fieldValue;
188                }
189
190                protected function addFormHeading(name:String):void {
191                        addFormChild(createFormHeading(name));
192                }
193
194                protected function createFormHeading(name:String):UIComponent {
195                        var heading:FormItem = new FormItem();
196                        heading.label = name;
197                        if (name == ComponentMD.ATTRIBUTE_LIST) {
198                                heading.styleName = StyleConstants.XMLBROWSER_HEADER_SMALL;
199                        } else {
200                                heading.styleName = StyleConstants.XMLBROWSER_HEADER;
201                        }
202                        return heading;
203                }
204
205                private function removeAddedChildren():void {
206                        for each (var child:DisplayObject in addedChildren) {
207                                removeChild(child);
208                        }
209                        addedChildren = new ArrayCollection();
210                }
211
212        }
213
214
215}
Note: See TracBrowser for help on using the repository browser.