1 | package clarin.cmdi.componentregistry.editor.model { |
---|
2 | import clarin.cmdi.componentregistry.common.ChangeTrackingCMDElement; |
---|
3 | import clarin.cmdi.componentregistry.common.XmlAble; |
---|
4 | |
---|
5 | import mx.collections.ArrayCollection; |
---|
6 | import mx.events.CollectionEvent; |
---|
7 | import mx.events.CollectionEventKind; |
---|
8 | |
---|
9 | [Bindable] |
---|
10 | public class CMDComponent implements XmlAble, AttributeContainer, ChangeTrackingCMDElement { |
---|
11 | |
---|
12 | //Attributes |
---|
13 | public var name:String; |
---|
14 | public var componentId:String; |
---|
15 | public var conceptLink:String; |
---|
16 | public var filename:String; |
---|
17 | public var cardinalityMin:String = "1"; |
---|
18 | public var cardinalityMax:String = "1"; |
---|
19 | |
---|
20 | //elements |
---|
21 | public var attributeList:ArrayCollection = new ArrayCollection(); |
---|
22 | public var cmdElements:ArrayCollection = new ArrayCollection(); |
---|
23 | public var cmdComponents:ArrayCollection = new ArrayCollection(); |
---|
24 | |
---|
25 | private var changed:Boolean = false; |
---|
26 | private var _changeTracking:Boolean = false; |
---|
27 | |
---|
28 | public function CMDComponent() { |
---|
29 | attributeList.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangedHandler); |
---|
30 | cmdElements.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangedHandler); |
---|
31 | cmdComponents.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangedHandler); |
---|
32 | } |
---|
33 | |
---|
34 | public static function createEmptyComponent():CMDComponent { |
---|
35 | var result:CMDComponent = new CMDComponent(); |
---|
36 | return result; |
---|
37 | } |
---|
38 | |
---|
39 | private function collectionChangedHandler(event:CollectionEvent):void { |
---|
40 | if(event.kind == CollectionEventKind.ADD ||event.kind == CollectionEventKind.MOVE || event.kind == CollectionEventKind.REMOVE || event.kind == CollectionEventKind.REPLACE){ |
---|
41 | setChanged(true); |
---|
42 | } |
---|
43 | if(event.kind == CollectionEventKind.ADD){ |
---|
44 | for each(var item:ChangeTrackingCMDElement in event.items){ |
---|
45 | item.changeTracking = _changeTracking; |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public function set changeTracking(value:Boolean):void { |
---|
51 | _changeTracking = value; |
---|
52 | for each (var component:CMDComponent in cmdComponents){ |
---|
53 | component.changeTracking = value; |
---|
54 | } |
---|
55 | for each (var element:CMDComponentElement in cmdElements){ |
---|
56 | element.changeTracking = value; |
---|
57 | } |
---|
58 | for each (var attribute:CMDAttribute in attributeList){ |
---|
59 | attribute.changeTracking = value; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | public function setChanged(value:Boolean):void { |
---|
64 | if(_changeTracking) { |
---|
65 | this.changed = value; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | public function get hasChanged():Boolean{ |
---|
70 | if(changed){ |
---|
71 | return changed; |
---|
72 | } else { |
---|
73 | for each (var attriubte:CMDAttribute in attributeList){ |
---|
74 | if(attriubte.hasChanged){ |
---|
75 | return true; |
---|
76 | } |
---|
77 | } |
---|
78 | for each (var element:CMDComponentElement in cmdElements){ |
---|
79 | if(element.hasChanged){ |
---|
80 | return true; |
---|
81 | } |
---|
82 | } |
---|
83 | for each (var component:CMDComponent in cmdComponents){ |
---|
84 | if(component.hasChanged){ |
---|
85 | return true; |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | return false; |
---|
90 | } |
---|
91 | |
---|
92 | public function removeComponent(component:CMDComponent):void { |
---|
93 | var index:int = cmdComponents.getItemIndex(component); |
---|
94 | if (index != -1) { |
---|
95 | cmdComponents.removeItemAt(index); |
---|
96 | setChanged(true); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | public function removeElement(element:CMDComponentElement):void { |
---|
101 | var index:int = cmdElements.getItemIndex(element); |
---|
102 | if (index != -1) { |
---|
103 | cmdElements.removeItemAt(index); |
---|
104 | setChanged(true); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | public function moveDownElement(element:CMDComponentElement):Boolean { |
---|
109 | var index:int = cmdElements.getItemIndex(element); |
---|
110 | if (index < cmdElements.length - 1) { |
---|
111 | cmdElements.removeItemAt(index); |
---|
112 | cmdElements.addItemAt(element, index + 1); |
---|
113 | setChanged(true); |
---|
114 | return true; |
---|
115 | } |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | public function moveUpElement(element:CMDComponentElement):Boolean { |
---|
120 | var index:int = cmdElements.getItemIndex(element); |
---|
121 | if (index > 0) { |
---|
122 | cmdElements.removeItemAt(index); |
---|
123 | cmdElements.addItemAt(element, index - 1); |
---|
124 | setChanged(true); |
---|
125 | return true; |
---|
126 | } |
---|
127 | return false; |
---|
128 | } |
---|
129 | |
---|
130 | public function moveDownComponent(comp:CMDComponent):Boolean { |
---|
131 | var index:int = cmdComponents.getItemIndex(comp); |
---|
132 | if (index < cmdComponents.length - 1) { |
---|
133 | cmdComponents.removeItemAt(index); |
---|
134 | cmdComponents.addItemAt(comp, index + 1); |
---|
135 | setChanged(true); |
---|
136 | return true; |
---|
137 | } |
---|
138 | return false; |
---|
139 | } |
---|
140 | |
---|
141 | public function moveUpComponent(comp:CMDComponent):Boolean { |
---|
142 | var index:int = cmdComponents.getItemIndex(comp); |
---|
143 | if (index > 0) { |
---|
144 | cmdComponents.removeItemAt(index); |
---|
145 | cmdComponents.addItemAt(comp, index - 1); |
---|
146 | setChanged(true); |
---|
147 | return true; |
---|
148 | } |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | public function toXml():XML { |
---|
153 | var result:XML = <CMD_Component></CMD_Component>; |
---|
154 | if (name) |
---|
155 | result.@name = name; |
---|
156 | if (componentId) |
---|
157 | result.@ComponentId = componentId; |
---|
158 | if (conceptLink) |
---|
159 | result.@ConceptLink = conceptLink; |
---|
160 | if (filename) |
---|
161 | result.@filename = filename; |
---|
162 | if (cardinalityMin) |
---|
163 | result.@CardinalityMin = cardinalityMin; |
---|
164 | if (cardinalityMax) |
---|
165 | result.@CardinalityMax = cardinalityMax; |
---|
166 | if (attributeList.length > 0) { |
---|
167 | var attributeListTag:XML = <AttributeList></AttributeList>; |
---|
168 | for each (var attribute:CMDAttribute in attributeList) { |
---|
169 | attributeListTag.appendChild(attribute.toXml()); |
---|
170 | } |
---|
171 | result.appendChild(attributeListTag); |
---|
172 | } |
---|
173 | for each (var element:CMDComponentElement in cmdElements) { |
---|
174 | result.appendChild(element.toXml()); |
---|
175 | } |
---|
176 | for each (var component:CMDComponent in cmdComponents) { |
---|
177 | result.appendChild(component.toXml()); |
---|
178 | } |
---|
179 | return result; |
---|
180 | } |
---|
181 | |
---|
182 | public function getAttributeList():ArrayCollection { |
---|
183 | return attributeList; |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|