1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
package org.argouml.kernel; |
40 | |
|
41 | |
import java.awt.Image; |
42 | |
import java.beans.PropertyChangeEvent; |
43 | |
import java.util.ArrayList; |
44 | |
import java.util.Collection; |
45 | |
import java.util.HashSet; |
46 | |
import java.util.Iterator; |
47 | |
import java.util.List; |
48 | |
import java.util.Set; |
49 | |
|
50 | |
import org.apache.log4j.Logger; |
51 | |
import org.argouml.application.events.ArgoEventPump; |
52 | |
import org.argouml.application.events.ArgoEventTypes; |
53 | |
import org.argouml.application.events.ArgoProfileEvent; |
54 | |
import org.argouml.configuration.Configuration; |
55 | |
import org.argouml.configuration.ConfigurationKey; |
56 | |
import org.argouml.model.Model; |
57 | |
import org.argouml.profile.DefaultTypeStrategy; |
58 | |
import org.argouml.profile.FigNodeStrategy; |
59 | |
import org.argouml.profile.FormatingStrategy; |
60 | |
import org.argouml.profile.Profile; |
61 | |
import org.argouml.profile.ProfileException; |
62 | |
import org.argouml.profile.ProfileFacade; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | 0 | public class ProfileConfiguration extends AbstractProjectMember { |
71 | |
|
72 | |
|
73 | |
|
74 | 900 | private static final Logger LOG = Logger |
75 | |
.getLogger(ProfileConfiguration.class); |
76 | |
|
77 | |
private FormatingStrategy formatingStrategy; |
78 | |
|
79 | |
private DefaultTypeStrategy defaultTypeStrategy; |
80 | |
|
81 | 968 | private List figNodeStrategies = new ArrayList(); |
82 | |
|
83 | 968 | private List<Profile> profiles = new ArrayList<Profile>(); |
84 | |
|
85 | 968 | private List<Object> profileModels = new ArrayList<Object>(); |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public static final String EXTENSION = "profile"; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | 900 | public static final ConfigurationKey KEY_DEFAULT_STEREOTYPE_VIEW = |
97 | |
Configuration.makeKey("profiles", "stereotypeView"); |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public ProfileConfiguration(Project project) { |
107 | 968 | super(EXTENSION, project); |
108 | 968 | List c = project.getUserDefinedModelList(); |
109 | 968 | Object m = c.isEmpty() ? null : c.get(0); |
110 | 968 | if (project.getProjectType() != Project.PROFILE_PROJECT) { |
111 | |
|
112 | 954 | for (Profile p : ProfileFacade.getManager().getDefaultProfiles()) { |
113 | 2862 | addProfile(p, m); |
114 | |
} |
115 | |
} |
116 | |
|
117 | 968 | updateStrategies(); |
118 | 968 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public ProfileConfiguration(Project project, |
128 | |
Collection<Profile> configuredProfiles) { |
129 | 0 | super(EXTENSION, project); |
130 | 0 | List c = project.getUserDefinedModelList(); |
131 | 0 | Object m = c.isEmpty() ? null : c.get(0); |
132 | 0 | for (Profile profile : configuredProfiles) { |
133 | 0 | addProfile(profile, m); |
134 | |
} |
135 | 0 | updateStrategies(); |
136 | 0 | } |
137 | |
|
138 | |
private void updateStrategies() { |
139 | 3830 | for (Profile profile : profiles) { |
140 | 8586 | activateFormatingStrategy(profile); |
141 | 8586 | activateDefaultTypeStrategy(profile); |
142 | |
} |
143 | 3830 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public FormatingStrategy getFormatingStrategy() { |
149 | 0 | return formatingStrategy; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public DefaultTypeStrategy getDefaultTypeStrategy() { |
156 | 0 | return defaultTypeStrategy; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public void activateDefaultTypeStrategy(Profile profile) { |
166 | 8586 | if (profile != null && profile.getDefaultTypeStrategy() != null |
167 | |
&& getProfiles().contains(profile)) { |
168 | 3816 | this.defaultTypeStrategy = profile.getDefaultTypeStrategy(); |
169 | |
} |
170 | 8586 | } |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public void activateFormatingStrategy(Profile profile) { |
179 | 8586 | if (profile != null && profile.getFormatingStrategy() != null |
180 | |
&& getProfiles().contains(profile)) { |
181 | 3816 | this.formatingStrategy = profile.getFormatingStrategy(); |
182 | |
} |
183 | 8586 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public List<Profile> getProfiles() { |
189 | 13752 | return profiles; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
@Deprecated |
200 | |
public void addProfile(Profile p) { |
201 | 0 | addProfile(p, null); |
202 | 0 | } |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
@SuppressWarnings("unchecked") |
212 | |
public void addProfile(Profile p, Object m) { |
213 | 3816 | if (!profiles.contains(p)) { |
214 | 2862 | profiles.add(p); |
215 | |
try { |
216 | 2862 | for (Object profile : p.getProfilePackages()) { |
217 | 954 | Model.getExtensionMechanismsHelper().applyProfile(m, profile); |
218 | |
} |
219 | 2862 | profileModels.addAll(p.getProfilePackages()); |
220 | 0 | } catch (ProfileException e) { |
221 | 0 | LOG.warn("Error retrieving profile's " + p + " packages.", e); |
222 | 2862 | } |
223 | |
|
224 | 2862 | FigNodeStrategy fns = p.getFigureStrategy(); |
225 | 2862 | if (fns != null) { |
226 | 0 | figNodeStrategies.add(fns); |
227 | |
} |
228 | |
|
229 | 2862 | for (Profile dependency : p.getDependencies()) { |
230 | 954 | addProfile(dependency, m); |
231 | |
} |
232 | |
|
233 | 2862 | updateStrategies(); |
234 | 2862 | ArgoEventPump.fireEvent(new ArgoProfileEvent( |
235 | |
ArgoEventTypes.PROFILE_ADDED, new PropertyChangeEvent(this, |
236 | |
"profile", null, p))); |
237 | |
} |
238 | 3816 | } |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
private List getProfileModels() { |
244 | 56 | return profileModels; |
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
@Deprecated |
255 | |
public void removeProfile(Profile p) { |
256 | 0 | removeProfile(p, null); |
257 | 0 | } |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public void removeProfile(Profile p, Object m) { |
267 | 0 | profiles.remove(p); |
268 | |
try { |
269 | 0 | for (Object profile : p.getProfilePackages()) { |
270 | 0 | Model.getExtensionMechanismsHelper().unapplyProfile(m, profile); |
271 | |
} |
272 | 0 | profileModels.removeAll(p.getProfilePackages()); |
273 | 0 | } catch (ProfileException e) { |
274 | 0 | LOG.error("Exception", e); |
275 | 0 | } |
276 | |
|
277 | 0 | FigNodeStrategy fns = p.getFigureStrategy(); |
278 | 0 | if (fns != null) { |
279 | 0 | figNodeStrategies.remove(fns); |
280 | |
} |
281 | |
|
282 | 0 | if (formatingStrategy == p.getFormatingStrategy()) { |
283 | 0 | formatingStrategy = null; |
284 | |
} |
285 | |
|
286 | 0 | List<Profile> markForRemoval = new ArrayList<Profile>(); |
287 | 0 | for (Profile profile : profiles) { |
288 | 0 | if (profile.getDependencies().contains(p)) { |
289 | 0 | markForRemoval.add(profile); |
290 | |
} |
291 | |
} |
292 | |
|
293 | 0 | for (Profile profile : markForRemoval) { |
294 | 0 | removeProfile(profile, m); |
295 | |
} |
296 | |
|
297 | 0 | updateStrategies(); |
298 | 0 | ArgoEventPump.fireEvent(new ArgoProfileEvent( |
299 | |
ArgoEventTypes.PROFILE_REMOVED, new PropertyChangeEvent(this, |
300 | |
"profile", p, null))); |
301 | 0 | } |
302 | |
|
303 | 968 | private FigNodeStrategy compositeFigNodeStrategy = new FigNodeStrategy() { |
304 | |
|
305 | |
public Image getIconForStereotype(Object element) { |
306 | 0 | Iterator it = figNodeStrategies.iterator(); |
307 | |
|
308 | 0 | while (it.hasNext()) { |
309 | 0 | FigNodeStrategy strat = (FigNodeStrategy) it.next(); |
310 | 0 | Image extra = strat.getIconForStereotype(element); |
311 | |
|
312 | 0 | if (extra != null) { |
313 | 0 | return extra; |
314 | |
} |
315 | 0 | } |
316 | 0 | return null; |
317 | |
} |
318 | |
|
319 | |
}; |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
public FigNodeStrategy getFigNodeStrategy() { |
325 | 0 | return compositeFigNodeStrategy; |
326 | |
} |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
public String getType() { |
333 | 968 | return EXTENSION; |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
public String repair() { |
344 | 0 | return ""; |
345 | |
} |
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
@Override |
352 | |
public String toString() { |
353 | 8922 | return "Profile Configuration"; |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
public Object findStereotypeForObject(String name, Object element) { |
366 | 0 | Iterator iter = null; |
367 | |
|
368 | 0 | for (Object model : profileModels) { |
369 | 0 | iter = Model.getFacade().getOwnedElements(model).iterator(); |
370 | |
|
371 | 0 | while (iter.hasNext()) { |
372 | 0 | Object stereo = iter.next(); |
373 | 0 | if (!Model.getFacade().isAStereotype(stereo) |
374 | |
|| !name.equals(Model.getFacade().getName(stereo))) { |
375 | 0 | continue; |
376 | |
} |
377 | |
|
378 | 0 | if (Model.getExtensionMechanismsHelper().isValidStereotype( |
379 | |
element, stereo)) { |
380 | 0 | return stereo; |
381 | |
} |
382 | 0 | } |
383 | |
} |
384 | |
|
385 | 0 | return null; |
386 | |
} |
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
public Object findType(String name) { |
395 | 56 | for (Object model : getProfileModels()) { |
396 | 56 | Object result = findTypeInModel(name, model); |
397 | 56 | if (result != null) { |
398 | 0 | return result; |
399 | |
} |
400 | 56 | } |
401 | 56 | return null; |
402 | |
} |
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
public static Object findTypeInModel(String s, Object model) { |
415 | |
|
416 | 56 | if (!Model.getFacade().isANamespace(model)) { |
417 | 0 | throw new IllegalArgumentException( |
418 | |
"Looking for the classifier " + s |
419 | |
+ " in a non-namespace object of " + model |
420 | |
+ ". A namespace was expected."); |
421 | |
} |
422 | |
|
423 | 56 | Collection allClassifiers = |
424 | |
Model.getModelManagementHelper() |
425 | |
.getAllModelElementsOfKind(model, |
426 | |
Model.getMetaTypes().getClassifier()); |
427 | |
|
428 | 56 | Object[] classifiers = allClassifiers.toArray(); |
429 | 56 | Object classifier = null; |
430 | |
|
431 | 280 | for (int i = 0; i < classifiers.length; i++) { |
432 | |
|
433 | 224 | classifier = classifiers[i]; |
434 | 224 | if (Model.getFacade().getName(classifier) != null |
435 | |
&& Model.getFacade().getName(classifier).equals(s)) { |
436 | 0 | return classifier; |
437 | |
} |
438 | |
} |
439 | |
|
440 | 56 | return null; |
441 | |
} |
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
@SuppressWarnings("unchecked") |
452 | |
public Collection findByMetaType(Object metaType) { |
453 | 0 | Set elements = new HashSet(); |
454 | |
|
455 | 0 | Iterator it = getProfileModels().iterator(); |
456 | 0 | while (it.hasNext()) { |
457 | 0 | Object model = it.next(); |
458 | 0 | elements.addAll(Model.getModelManagementHelper() |
459 | |
.getAllModelElementsOfKind(model, metaType)); |
460 | 0 | } |
461 | 0 | return elements; |
462 | |
} |
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
public Collection findAllStereotypesForModelElement(Object modelElement) { |
471 | 0 | return Model.getExtensionMechanismsHelper().getAllPossibleStereotypes( |
472 | |
getProfileModels(), modelElement); |
473 | |
} |
474 | |
} |