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

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