1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model.visual.backward;
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.LinkedList;
26 import java.util.List;
27 import java.util.SortedMap;
28 import java.util.TreeMap;
29
30 import pl.edu.agh.cast.backward.resources.xml.AttributeValuesConverter;
31 import pl.edu.agh.cast.data.model.property.IPropertyChangeProvider;
32 import pl.edu.agh.cast.data.model.property.PropertyChangeProviderHelper;
33 import pl.edu.agh.cast.model.attributes.Attribute;
34 import pl.edu.agh.cast.model.attributes.AttributeManager;
35 import pl.edu.agh.cast.model.attributes.AttributeMergePolicy;
36 import pl.edu.agh.cast.model.attributes.AttributeValue;
37 import pl.edu.agh.cast.model.mapper.Mappable;
38 import pl.edu.agh.cast.util.Messages;
39
40 import com.thoughtworks.xstream.annotations.XStreamAlias;
41 import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
42 import com.thoughtworks.xstream.annotations.XStreamConverter;
43 import com.thoughtworks.xstream.annotations.XStreamOmitField;
44
45
46
47
48
49
50 public abstract class ModelElement implements Serializable, IPropertyChangeProvider, AttributeValueContainer,
51 PropertyChangeListener {
52
53 private static final long serialVersionUID = 4933221188249730849L;
54
55
56
57
58
59 public static final String ATTRIBUTE_CHANGE = "ModelElement.attributeValue";
60
61
62
63
64 @XStreamAlias("attributeValues")
65 @XStreamConverter(AttributeValuesConverter.class)
66 protected SortedMap<Attribute, AttributeValue> attributeValues;
67
68 @XStreamAlias("mid")
69 @XStreamAsAttribute
70 private long mid;
71
72 @XStreamAlias("saved")
73 @XStreamAsAttribute
74 private boolean saved;
75
76 @XStreamOmitField
77 private transient PropertyChangeProviderHelper pcpHelper;
78
79
80
81
82 protected ModelElement() {
83 pcpHelper = new PropertyChangeProviderHelper(this);
84 attributeValues = new TreeMap<Attribute, AttributeValue>();
85 }
86
87
88
89
90
91
92 public abstract AttributeManager getAttributeManager();
93
94 public long getMid() {
95 return mid;
96 }
97
98 public void setMid(long mid) {
99 this.mid = mid;
100 }
101
102 public boolean isSaved() {
103 return saved;
104 }
105
106 public void setSaved(boolean saved) {
107 this.saved = saved;
108 }
109
110 protected Object readResolve() {
111 pcpHelper = new PropertyChangeProviderHelper(this);
112 return this;
113 }
114
115
116
117
118
119
120
121
122 public void addPropertyChangeListener(PropertyChangeListener l) {
123 pcpHelper.addPropertyChangeListener(l);
124 }
125
126
127
128
129
130
131
132 public void removePropertyChangeListener(PropertyChangeListener l) {
133 pcpHelper.removePropertyChangeListener(l);
134 }
135
136
137
138
139
140
141
142
143
144
145
146 protected void firePropertyChange(String property, Object oldValue, Object newValue) {
147 pcpHelper.firePropertyChange(property, oldValue, newValue);
148 }
149
150
151
152
153
154
155
156
157
158
159 public AttributeValue getAttributeValue(String name) {
160 Attribute attribute = getAttributeManager().getAttribute(name);
161 if (attributeValues.containsKey(attribute)) {
162 return attributeValues.get(attribute);
163 } else {
164
165 return new AttributeValue(attribute, attribute.getDefaultValue());
166 }
167 }
168
169
170
171
172
173
174
175
176 public boolean isAttributeSettable(String name) {
177 Attribute attribute = getAttributeManager().getAttribute(name);
178 return attribute.isEditable() || getAttributeValue(name).getValue() == null;
179 }
180
181
182
183
184
185
186 public void setAttributeValue(String name, Object newValue) {
187 setAttributeValue(name, newValue, null);
188 }
189
190
191
192
193
194
195
196 public void setAttributeValue(String name, Object newValue, AttributeMergePolicy policy) {
197
198 Attribute attribute = getAttributeManager().getAttribute(name);
199
200 if (!isAttributeSettable(name)) {
201
202
203 throw new UnsupportedOperationException(Messages.ModelElementPropertyHolder_0);
204 } else {
205 Object oldVal = getAttributeValue(name);
206 Object newVal = newValue;
207 if (policy != null) {
208 newVal = policy.mergeValues(attribute, oldVal == null ? null : ((AttributeValue)oldVal)
209 .getValue(), newValue);
210 }
211 attributeValues.put(attribute, new AttributeValue(attribute, newVal));
212
213
214
215
216 firePropertyChange(ATTRIBUTE_CHANGE, oldVal, newVal);
217 }
218 }
219
220
221
222
223
224
225 public boolean isAttributeEditable(String attributeName) {
226 Attribute attribute = getAttributeManager().getAttribute(attributeName);
227 return attribute.isEditable();
228 }
229
230
231
232
233
234
235 public List<AttributeValue> getAllValues() {
236 Collection<Attribute> attributes = getAttributeManager().getAttributes();
237
238
239 for (Attribute attribute : attributes) {
240 if (!attributeValues.containsKey(attribute)) {
241
242 setAttributeValue(attribute.getName(), getAttributeValue(attribute.getName()).getValue());
243 }
244 }
245
246
247 Collection<Attribute> keySet = new LinkedList<Attribute>(attributeValues.keySet());
248 for (Attribute setAttribute : keySet) {
249 if (!attributes.contains(setAttribute)) {
250 removePropertyValue(setAttribute.getName());
251 }
252 }
253
254 return new ArrayList<AttributeValue>(attributeValues.values());
255 }
256
257
258
259
260
261
262 public AttributeValue removePropertyValue(String name) {
263
264
265 for (AttributeValue value : attributeValues.values()) {
266 if (value.getAttribute().getName().equals(name)) {
267 return attributeValues.remove(value.getAttribute());
268 }
269 }
270 return null;
271 }
272
273
274
275
276
277
278
279 protected void copyAttributes(AttributeValueContainer target) {
280 for (AttributeValue value : attributeValues.values()) {
281 if (isAttributeSettable(value.getAttribute().getName())) {
282 target.setAttributeValue(value.getAttribute().getName(), value.getValue());
283 }
284 }
285 }
286
287
288
289
290
291
292 public void propertyChange(PropertyChangeEvent evt) {
293 if (evt.getNewValue() == null && AttributeManager.ATTRIBUTE_REGISTRATION_STATUS.equals(evt.getPropertyName())) {
294
295 removePropertyValue(((Attribute)evt.getOldValue()).getName());
296 }
297 }
298
299
300
301
302 protected final void bindToAttributeManager() {
303 getAttributeManager().addPropertyChangeListener(this);
304 }
305
306
307
308
309
310
311
312
313
314
315
316 public void copyAttributeValues(Mappable mappable) {
317
318 for (String attributeName : mappable.getAttributeNames()) {
319
320
321 if (getAttributeManager().isRegisteredId(attributeName) && isAttributeSettable(attributeName)) {
322
323 AttributeValue attributeValue = getAttributeValue(attributeName);
324 Object newValue = mappable.getAttribute(attributeName);
325
326
327 if (attributeValue != null) {
328 Attribute attribute = attributeValue.getAttribute();
329 if (attribute.getDefaultMergePolicy() != null) {
330 Object oldValue = attributeValue == null ? null : attributeValue.getValue();
331 newValue = attribute.getDefaultMergePolicy().mergeValues(attribute, oldValue, newValue);
332 }
333 }
334
335
336 setAttributeValue(attributeName, newValue);
337 }
338
339 }
340
341 }
342
343
344
345 }