Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DeploymentDiagramGraphModel |
|
| 12.0;12 |
1 | /* $Id: DeploymentDiagramGraphModel.java 17856 2010-01-12 19:57:04Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 2003-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.diagram.deployment; | |
40 | ||
41 | import java.beans.PropertyChangeEvent; | |
42 | import java.beans.VetoableChangeListener; | |
43 | import java.util.ArrayList; | |
44 | import java.util.Collection; | |
45 | import java.util.Collections; | |
46 | import java.util.List; | |
47 | ||
48 | import org.apache.log4j.Logger; | |
49 | import org.argouml.model.Model; | |
50 | import org.argouml.uml.CommentEdge; | |
51 | import org.argouml.uml.diagram.UMLMutableGraphSupport; | |
52 | ||
53 | /** | |
54 | * This class defines a bridge between the UML meta-model | |
55 | * representation of the design and the GraphModel interface used by | |
56 | * GEF.<p> | |
57 | * | |
58 | * This class handles only UML Deployment Diagrams.<p> | |
59 | * | |
60 | */ | |
61 | 19 | public class DeploymentDiagramGraphModel |
62 | extends UMLMutableGraphSupport | |
63 | implements VetoableChangeListener { | |
64 | /** | |
65 | * Logger. | |
66 | */ | |
67 | 19 | private static final Logger LOG = |
68 | Logger.getLogger(DeploymentDiagramGraphModel.class); | |
69 | ||
70 | //////////////////////////////////////////////////////////////// | |
71 | // GraphModel implementation | |
72 | ||
73 | ||
74 | /* | |
75 | * @see org.tigris.gef.graph.GraphModel#getPorts(java.lang.Object) | |
76 | */ | |
77 | public List getPorts(Object nodeOrEdge) { | |
78 | 0 | List res = new ArrayList(); |
79 | 0 | if (Model.getFacade().isANode(nodeOrEdge)) { |
80 | 0 | res.add(nodeOrEdge); |
81 | } | |
82 | 0 | if (Model.getFacade().isANodeInstance(nodeOrEdge)) { |
83 | 0 | res.add(nodeOrEdge); |
84 | } | |
85 | ||
86 | 0 | if (Model.getFacade().isAComponent(nodeOrEdge)) { |
87 | 0 | res.add(nodeOrEdge); |
88 | } | |
89 | 0 | if (Model.getFacade().isAComponentInstance(nodeOrEdge)) { |
90 | 0 | res.add(nodeOrEdge); |
91 | } | |
92 | 0 | if (Model.getFacade().isAClass(nodeOrEdge)) { |
93 | 0 | res.add(nodeOrEdge); |
94 | } | |
95 | 0 | if (Model.getFacade().isAInterface(nodeOrEdge)) { |
96 | 0 | res.add(nodeOrEdge); |
97 | } | |
98 | 0 | if (Model.getFacade().isAObject(nodeOrEdge)) { |
99 | 0 | res.add(nodeOrEdge); |
100 | } | |
101 | 0 | return res; |
102 | } | |
103 | ||
104 | /* | |
105 | * @see org.tigris.gef.graph.BaseGraphModel#getOwner(java.lang.Object) | |
106 | */ | |
107 | public Object getOwner(Object port) { | |
108 | 0 | return port; |
109 | } | |
110 | ||
111 | ||
112 | /* | |
113 | * @see org.tigris.gef.graph.GraphModel#getInEdges(java.lang.Object) | |
114 | */ | |
115 | public List getInEdges(Object port) { | |
116 | 0 | List res = new ArrayList(); |
117 | 0 | if (Model.getFacade().isANode(port)) { |
118 | 0 | Collection ends = Model.getFacade().getAssociationEnds(port); |
119 | 0 | if (ends == null) { |
120 | 0 | return Collections.EMPTY_LIST; |
121 | } | |
122 | 0 | for (Object end : ends) { |
123 | 0 | res.add(Model.getFacade().getAssociation(end)); |
124 | } | |
125 | } | |
126 | 0 | if (Model.getFacade().isANodeInstance(port)) { |
127 | 0 | Object noi = port; |
128 | 0 | Collection ends = Model.getFacade().getLinkEnds(noi); |
129 | 0 | res.addAll(ends); |
130 | } | |
131 | 0 | if (Model.getFacade().isAComponent(port)) { |
132 | 0 | Collection ends = Model.getFacade().getAssociationEnds(port); |
133 | 0 | if (ends == null) { |
134 | 0 | return Collections.EMPTY_LIST; |
135 | } | |
136 | 0 | for (Object end : ends) { |
137 | 0 | res.add(Model.getFacade().getAssociation(end)); |
138 | } | |
139 | } | |
140 | 0 | if (Model.getFacade().isAComponentInstance(port)) { |
141 | 0 | Object coi = port; |
142 | 0 | Collection ends = Model.getFacade().getLinkEnds(coi); |
143 | 0 | res.addAll(ends); |
144 | } | |
145 | 0 | if (Model.getFacade().isAClass(port)) { |
146 | 0 | Collection ends = Model.getFacade().getAssociationEnds(port); |
147 | 0 | if (ends == null) { |
148 | 0 | return Collections.EMPTY_LIST; |
149 | } | |
150 | 0 | for (Object end : ends) { |
151 | 0 | res.add(Model.getFacade().getAssociation(end)); |
152 | } | |
153 | } | |
154 | 0 | if (Model.getFacade().isAInterface(port)) { |
155 | 0 | Collection ends = Model.getFacade().getAssociationEnds(port); |
156 | 0 | if (ends == null) { |
157 | 0 | return Collections.EMPTY_LIST; |
158 | } | |
159 | 0 | for (Object end : ends) { |
160 | 0 | res.add(Model.getFacade().getAssociation(end)); |
161 | } | |
162 | } | |
163 | 0 | if (Model.getFacade().isAObject(port)) { |
164 | 0 | Object clo = port; |
165 | 0 | Collection ends = Model.getFacade().getLinkEnds(clo); |
166 | 0 | res.addAll(ends); |
167 | } | |
168 | ||
169 | ||
170 | 0 | return res; |
171 | } | |
172 | ||
173 | /* | |
174 | * @see org.tigris.gef.graph.GraphModel#getOutEdges(java.lang.Object) | |
175 | */ | |
176 | public List getOutEdges(Object port) { | |
177 | 0 | return Collections.EMPTY_LIST; // TODO:? |
178 | } | |
179 | ||
180 | //////////////////////////////////////////////////////////////// | |
181 | // MutableGraphModel implementation | |
182 | ||
183 | /* | |
184 | * @see org.tigris.gef.graph.MutableGraphModel#canAddNode(java.lang.Object) | |
185 | */ | |
186 | @Override | |
187 | public boolean canAddNode(Object node) { | |
188 | 0 | if (node == null) { |
189 | 0 | return false; |
190 | } | |
191 | 0 | if (Model.getFacade().isAAssociation(node) |
192 | && !Model.getFacade().isANaryAssociation(node)) { | |
193 | // A binary association is not a node so reject. | |
194 | 0 | return false; |
195 | } | |
196 | 0 | if (containsNode(node)) { |
197 | 0 | return false; |
198 | } | |
199 | 0 | if (Model.getFacade().isAAssociation(node)) { |
200 | 0 | Collection ends = Model.getFacade().getConnections(node); |
201 | 0 | boolean canAdd = true; |
202 | 0 | for (Object end : ends) { |
203 | 0 | Object classifier = |
204 | Model.getFacade().getClassifier(end); | |
205 | 0 | if (!containsNode(classifier)) { |
206 | 0 | canAdd = false; |
207 | 0 | break; |
208 | } | |
209 | 0 | } |
210 | 0 | return canAdd; |
211 | } | |
212 | 0 | return (Model.getFacade().isANode(node)) |
213 | || (Model.getFacade().isAComponent(node)) | |
214 | || (Model.getFacade().isAClass(node)) | |
215 | || (Model.getFacade().isAInterface(node)) | |
216 | || (Model.getFacade().isAObject(node)) | |
217 | || (Model.getFacade().isANodeInstance(node)) | |
218 | || (Model.getFacade().isAComponentInstance(node) | |
219 | || (Model.getFacade().isAComment(node))); | |
220 | } | |
221 | ||
222 | /* | |
223 | * @see org.tigris.gef.graph.MutableGraphModel#canAddEdge(java.lang.Object) | |
224 | */ | |
225 | @Override | |
226 | public boolean canAddEdge(Object edge) { | |
227 | 0 | if (edge == null) { |
228 | 0 | return false; |
229 | } | |
230 | 0 | if (containsEdge(edge)) { |
231 | 0 | return false; |
232 | } | |
233 | 0 | Object end0 = null, end1 = null; |
234 | 0 | if (edge instanceof CommentEdge) { |
235 | 0 | end0 = ((CommentEdge) edge).getSource(); |
236 | 0 | end1 = ((CommentEdge) edge).getDestination(); |
237 | 0 | } else if (Model.getFacade().isAAssociationEnd(edge)) { |
238 | 0 | end0 = Model.getFacade().getAssociation(edge); |
239 | 0 | end1 = Model.getFacade().getType(edge); |
240 | ||
241 | 0 | return (end0 != null |
242 | && end1 != null | |
243 | && (containsEdge(end0) || containsNode(end0)) | |
244 | && containsNode(end1)); | |
245 | 0 | } else if (Model.getFacade().isARelationship(edge)) { |
246 | 0 | end0 = Model.getCoreHelper().getSource(edge); |
247 | 0 | end1 = Model.getCoreHelper().getDestination(edge); |
248 | 0 | } else if (Model.getFacade().isALink(edge)) { |
249 | 0 | end0 = Model.getCommonBehaviorHelper().getSource(edge); |
250 | 0 | end1 = |
251 | Model.getCommonBehaviorHelper().getDestination(edge); | |
252 | 0 | } else if (edge instanceof CommentEdge) { |
253 | 0 | end0 = ((CommentEdge) edge).getSource(); |
254 | 0 | end1 = ((CommentEdge) edge).getDestination(); |
255 | } else { | |
256 | 0 | return false; |
257 | } | |
258 | ||
259 | // Both ends must be defined and nodes that are on the graph already. | |
260 | 0 | if (end0 == null || end1 == null) { |
261 | 0 | LOG.error("Edge rejected. Its ends are not attached to anything"); |
262 | 0 | return false; |
263 | } | |
264 | ||
265 | 0 | if (!containsNode(end0) |
266 | && !containsEdge(end0)) { | |
267 | 0 | LOG.error("Edge rejected. Its source end is attached to " |
268 | + end0 | |
269 | + " but this is not in the graph model"); | |
270 | 0 | return false; |
271 | } | |
272 | 0 | if (!containsNode(end1) |
273 | && !containsEdge(end1)) { | |
274 | 0 | LOG.error("Edge rejected. Its destination end is attached to " |
275 | + end1 | |
276 | + " but this is not in the graph model"); | |
277 | 0 | return false; |
278 | } | |
279 | ||
280 | 0 | return true; |
281 | } | |
282 | ||
283 | ||
284 | /* | |
285 | * @see org.tigris.gef.graph.MutableGraphModel#addNode(java.lang.Object) | |
286 | */ | |
287 | @Override | |
288 | public void addNode(Object node) { | |
289 | 0 | LOG.debug("adding class node!!"); |
290 | 0 | if (!canAddNode(node)) { |
291 | 0 | return; |
292 | } | |
293 | 0 | getNodes().add(node); |
294 | // TODO: assumes public, user pref for default visibility? | |
295 | //do I have to check the namespace here? (Toby) | |
296 | 0 | if (Model.getFacade().isAModelElement(node) |
297 | && (Model.getFacade().getNamespace(node) == null)) { | |
298 | 0 | Model.getCoreHelper().addOwnedElement(getHomeModel(), node); |
299 | } | |
300 | 0 | fireNodeAdded(node); |
301 | 0 | } |
302 | ||
303 | /* | |
304 | * @see org.tigris.gef.graph.MutableGraphModel#addEdge(java.lang.Object) | |
305 | */ | |
306 | @Override | |
307 | public void addEdge(Object edge) { | |
308 | 0 | LOG.debug("adding class edge!!!!!!"); |
309 | 0 | if (!canAddEdge(edge)) { |
310 | 0 | return; |
311 | } | |
312 | 0 | getEdges().add(edge); |
313 | // TODO: assumes public | |
314 | 0 | if (Model.getFacade().isAModelElement(edge) |
315 | && !Model.getFacade().isAAssociationEnd(edge)) { | |
316 | 0 | Model.getCoreHelper().addOwnedElement(getHomeModel(), edge); |
317 | } | |
318 | 0 | fireEdgeAdded(edge); |
319 | 0 | } |
320 | ||
321 | /* | |
322 | * @see org.tigris.gef.graph.MutableGraphModel#addNodeRelatedEdges(java.lang.Object) | |
323 | */ | |
324 | @Override | |
325 | public void addNodeRelatedEdges(Object node) { | |
326 | 0 | super.addNodeRelatedEdges(node); |
327 | ||
328 | 0 | if (Model.getFacade().isAClassifier(node)) { |
329 | 0 | Collection ends = Model.getFacade().getAssociationEnds(node); |
330 | 0 | for (Object ae : ends) { |
331 | 0 | if (!Model.getFacade().isANaryAssociation( |
332 | Model.getFacade().getAssociation(ae)) | |
333 | && canAddEdge(Model.getFacade().getAssociation(ae))) { | |
334 | 0 | addEdge(Model.getFacade().getAssociation(ae)); |
335 | } | |
336 | 0 | return; |
337 | } | |
338 | } | |
339 | 0 | if (Model.getFacade().isAAssociation(node)) { |
340 | 0 | Collection ends = Model.getFacade().getConnections(node); |
341 | 0 | for (Object associationEnd : ends) { |
342 | 0 | if (canAddEdge(associationEnd)) { |
343 | 0 | addEdge(associationEnd); |
344 | } | |
345 | } | |
346 | } | |
347 | 0 | if (Model.getFacade().isAInstance(node)) { |
348 | 0 | Collection ends = Model.getFacade().getLinkEnds(node); |
349 | 0 | for (Object end : ends) { |
350 | 0 | Object link = Model.getFacade().getLink(end); |
351 | 0 | if (canAddEdge(link)) { |
352 | 0 | addEdge(link); |
353 | } | |
354 | 0 | return; |
355 | } | |
356 | } | |
357 | 0 | if (Model.getFacade().isAGeneralizableElement(node)) { |
358 | 0 | Collection generalizations = |
359 | Model.getFacade().getGeneralizations(node); | |
360 | ||
361 | 0 | for (Object generalization : generalizations) { |
362 | 0 | if (canAddEdge(generalization)) { |
363 | 0 | addEdge(generalization); |
364 | } | |
365 | 0 | return; |
366 | } | |
367 | 0 | Collection specializations = |
368 | Model.getFacade().getSpecializations(node); | |
369 | 0 | for (Object specialization : specializations) { |
370 | 0 | if (canAddEdge(specialization)) { |
371 | 0 | addEdge(specialization); |
372 | } | |
373 | 0 | return; |
374 | } | |
375 | } | |
376 | 0 | if (Model.getFacade().isAModelElement(node)) { |
377 | 0 | List dependencies = |
378 | new ArrayList(Model.getFacade().getClientDependencies(node)); | |
379 | 0 | dependencies.addAll(Model.getFacade().getSupplierDependencies(node)); |
380 | 0 | for (Object dependency : dependencies) { |
381 | 0 | if (canAddEdge(dependency)) { |
382 | 0 | addEdge(dependency); |
383 | } | |
384 | 0 | return; |
385 | } | |
386 | } | |
387 | 0 | } |
388 | ||
389 | ||
390 | /* | |
391 | * @see java.beans.VetoableChangeListener#vetoableChange(java.beans.PropertyChangeEvent) | |
392 | */ | |
393 | public void vetoableChange(PropertyChangeEvent pce) { | |
394 | 0 | if ("ownedElement".equals(pce.getPropertyName())) { |
395 | 0 | List oldOwned = (List) pce.getOldValue(); |
396 | 0 | Object eo = pce.getNewValue(); |
397 | 0 | Object me = Model.getFacade().getModelElement(eo); |
398 | 0 | if (oldOwned.contains(eo)) { |
399 | 0 | LOG.debug("model removed " + me); |
400 | 0 | if (Model.getFacade().isANode(me)) { |
401 | 0 | removeNode(me); |
402 | } | |
403 | 0 | if (Model.getFacade().isANodeInstance(me)) { |
404 | 0 | removeNode(me); |
405 | } | |
406 | 0 | if (Model.getFacade().isAComponent(me)) { |
407 | 0 | removeNode(me); |
408 | } | |
409 | 0 | if (Model.getFacade().isAComponentInstance(me)) { |
410 | 0 | removeNode(me); |
411 | } | |
412 | 0 | if (Model.getFacade().isAClass(me)) { |
413 | 0 | removeNode(me); |
414 | } | |
415 | 0 | if (Model.getFacade().isAInterface(me)) { |
416 | 0 | removeNode(me); |
417 | } | |
418 | 0 | if (Model.getFacade().isAObject(me)) { |
419 | 0 | removeNode(me); |
420 | } | |
421 | 0 | if (Model.getFacade().isAAssociation(me)) { |
422 | 0 | removeEdge(me); |
423 | } | |
424 | 0 | if (Model.getFacade().isADependency(me)) { |
425 | 0 | removeEdge(me); |
426 | } | |
427 | 0 | if (Model.getFacade().isALink(me)) { |
428 | 0 | removeEdge(me); |
429 | } | |
430 | } else { | |
431 | 0 | LOG.debug("model added " + me); |
432 | } | |
433 | } | |
434 | 0 | } |
435 | ||
436 | /** | |
437 | * The UID. | |
438 | */ | |
439 | static final long serialVersionUID = 1003748292917485298L; | |
440 | ||
441 | } |