1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model.attributes;
19
20 import java.beans.PropertyChangeEvent;
21 import java.beans.PropertyChangeListener;
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.SortedMap;
28 import java.util.TreeMap;
29
30 import org.eclipse.osgi.util.NLS;
31
32 import pl.edu.agh.cast.data.model.property.IPropertyChangeProvider;
33 import pl.edu.agh.cast.data.model.property.PropertyChangeProviderHelper;
34 import pl.edu.agh.cast.util.Messages;
35
36 import com.thoughtworks.xstream.annotations.XStreamOmitField;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public abstract class AttributeManager implements Serializable, IPropertyChangeProvider, PropertyChangeListener {
51
52 private static final long serialVersionUID = 8685580835572389886L;
53
54
55
56
57 public static final String ATTRIBUTES_PROPERTY_CHANGE = "AttributeManager.AttributesProperty";
58
59
60
61
62 public static final String ATTRIBUTE_REGISTRATION_STATUS = "AttributeManager.RegistrationStatus";
63
64 private SortedMap<String, Attribute> registeredAttributes;
65
66
67
68
69 public AttributeManager() {
70 registeredAttributes = new TreeMap<String, Attribute>();
71 pcpHelper = new PropertyChangeProviderHelper(this);
72 }
73
74
75
76
77
78
79
80
81 public boolean isRegisteredId(String id) {
82 return registeredAttributes.containsKey(id);
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 private void registerAttribute(String id, boolean nameIsLocalizable, ValueType type, Object defaultValue,
113 boolean permanent, boolean editable, boolean showAsLabel, String ownerTypeName, String modelExtensionId) {
114 if (isRegisteredId(id)) {
115 throw new IllegalArgumentException(NLS.bind("Attribute '{0}' already registered", id));
116 } else {
117 Attribute attribute = new Attribute(id, nameIsLocalizable, type, defaultValue, permanent, editable,
118 showAsLabel, ownerTypeName, modelExtensionId);
119 registeredAttributes.put(id, attribute);
120 attribute.addPropertyChangeListener(this);
121 firePropertyChange(ATTRIBUTE_REGISTRATION_STATUS, null, attribute);
122 }
123 }
124
125
126
127
128
129
130 public List<Attribute> getPermanentAttributes() {
131 List<Attribute> permanentAttributes = new ArrayList<Attribute>();
132 for (Attribute attribute : registeredAttributes.values()) {
133 if (attribute.isPermanent()) {
134 permanentAttributes.add(attribute);
135 }
136 }
137 return permanentAttributes;
138 }
139
140
141
142
143
144
145
146
147
148 public void registerAttribute(String id, ValueType valueType) {
149 registerAttribute(id, false, valueType, valueType.getDefaultValue(), false, true, false, null, null);
150 }
151
152
153
154
155
156
157
158
159
160
161
162 public void setAttributeIsLabel(String id, boolean showAsLabel) {
163 getAttribute(id).setShowAsLabel(showAsLabel);
164 }
165
166
167
168
169 protected void registerPermanentAttribute(String id, ValueType type, boolean editable, boolean showAsLabel) {
170 registerAttribute(id, true, type, type.getDefaultValue(), true, editable, showAsLabel, null, null);
171 }
172
173
174
175
176 protected void registerPermanentAttribute(String id, ValueType type, boolean editable, boolean showAsLabel,
177 String ownerTypeId, String modelExtensionId) {
178 registerAttribute(id, true, type, type.getDefaultValue(), true, editable, showAsLabel, ownerTypeId,
179 modelExtensionId);
180 }
181
182
183
184
185
186
187 public Collection<Attribute> getAttributes() {
188 return Collections.unmodifiableCollection(registeredAttributes.values());
189 }
190
191
192
193
194
195
196
197
198
199
200 public void unregisterId(String id) {
201 if (!registeredAttributes.containsKey(id)) {
202 throw new IllegalArgumentException(NLS.bind(Messages.ModelElementPropertyManager_0, id));
203 }
204
205 if (registeredAttributes.get(id).isPermanent()) {
206 throw new IllegalArgumentException(NLS.bind("Permanent attribute '{0}' cannot be unregistered", id));
207 }
208 Attribute removed = registeredAttributes.remove(id);
209 removed.removePropertyChangeListener(this);
210 firePropertyChange(ATTRIBUTE_REGISTRATION_STATUS, removed, null);
211 }
212
213
214
215
216
217
218
219
220
221
222 public Attribute getAttribute(String id) {
223 if (!registeredAttributes.containsKey(id)) {
224 throw new IllegalArgumentException(NLS.bind(Messages.ModelElementPropertyManager_0, id));
225 }
226 return registeredAttributes.get(id);
227 }
228
229
230
231
232
233
234
235 public void init(List<Attribute> attributes) {
236 for (Attribute a : attributes) {
237 registeredAttributes.put(a.getName(), a);
238 a.addPropertyChangeListener(this);
239 }
240 }
241
242
243
244
245
246
247
248
249 public void propertyChange(PropertyChangeEvent evt) {
250 if (Attribute.PROPERTY_SHOW_AS_LABEL.equals(evt.getPropertyName())) {
251 firePropertyChange(AttributeManager.ATTRIBUTES_PROPERTY_CHANGE, evt, null);
252 }
253 }
254
255
256
257
258 @XStreamOmitField
259 private transient PropertyChangeProviderHelper pcpHelper;
260
261 protected Object readResolve() {
262 pcpHelper = new PropertyChangeProviderHelper(this);
263 for (Attribute a : registeredAttributes.values()) {
264 a.addPropertyChangeListener(this);
265 }
266 return this;
267 }
268
269
270
271
272
273
274 public void addPropertyChangeListener(PropertyChangeListener l) {
275 pcpHelper.addPropertyChangeListener(l);
276 }
277
278
279
280
281
282
283
284 public void removePropertyChangeListener(PropertyChangeListener l) {
285 pcpHelper.removePropertyChangeListener(l);
286 }
287
288 protected void firePropertyChange(String property, Object oldValue, Object newValue) {
289 pcpHelper.firePropertyChange(property, oldValue, newValue);
290 }
291
292 }