Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DiagramFactory |
|
| 3.2222222222222223;3.222 | ||||
DiagramFactory$DiagramType |
|
| 3.2222222222222223;3.222 |
1 | /* $Id: DiagramFactory.java 17850 2010-01-12 19:53:35Z 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) 1996-2009 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; | |
40 | ||
41 | import java.util.EnumMap; | |
42 | import java.util.Map; | |
43 | ||
44 | import org.argouml.kernel.ProjectManager; | |
45 | import org.argouml.model.ActivityDiagram; | |
46 | import org.argouml.model.ClassDiagram; | |
47 | import org.argouml.model.CollaborationDiagram; | |
48 | import org.argouml.model.DeploymentDiagram; | |
49 | import org.argouml.model.DiDiagram; | |
50 | import org.argouml.model.Model; | |
51 | import org.argouml.model.StateDiagram; | |
52 | import org.argouml.model.UseCaseDiagram; | |
53 | import org.argouml.uml.diagram.activity.ui.UMLActivityDiagram; | |
54 | import org.argouml.uml.diagram.collaboration.ui.UMLCollaborationDiagram; | |
55 | import org.argouml.uml.diagram.deployment.ui.UMLDeploymentDiagram; | |
56 | import org.argouml.uml.diagram.state.ui.UMLStateDiagram; | |
57 | import org.argouml.uml.diagram.static_structure.ui.UMLClassDiagram; | |
58 | import org.argouml.uml.diagram.use_case.ui.UMLUseCaseDiagram; | |
59 | ||
60 | /** | |
61 | * Provide a factory method to create different UML diagrams. | |
62 | * | |
63 | * @author Bob Tarling | |
64 | */ | |
65 | public final class DiagramFactory { | |
66 | ||
67 | /** | |
68 | * Map from our public enum to our internal implementation classes. | |
69 | * This allows use to hide the implementation classes from users of | |
70 | * the factory. | |
71 | * NOTE: This needs to be initialized before the constructor is called | |
72 | * to initialize the singleton. | |
73 | */ | |
74 | 900 | private static Map<DiagramType, Class> diagramClasses = |
75 | new EnumMap<DiagramType, Class>(DiagramType.class); | |
76 | ||
77 | /** | |
78 | * The singleton instance. | |
79 | */ | |
80 | 900 | private static DiagramFactory diagramFactory = new DiagramFactory(); |
81 | ||
82 | /** | |
83 | * Enumeration containing all the different types of UML diagrams. | |
84 | */ | |
85 | 8100 | public enum DiagramType { |
86 | 900 | Class, UseCase, State, Deployment, Collaboration, Activity, Sequence |
87 | } | |
88 | ||
89 | 900 | private Map<DiagramType, DiagramFactoryInterface2> factories = |
90 | new EnumMap<DiagramType, DiagramFactoryInterface2>(DiagramType.class); | |
91 | ||
92 | private DiagramFactory() { | |
93 | 900 | super(); |
94 | // TODO: Use our extension registration mechanism for our internal | |
95 | // classes as well, so everything is treated the same | |
96 | 900 | diagramClasses.put(DiagramType.Class, UMLClassDiagram.class); |
97 | 900 | diagramClasses.put(DiagramType.UseCase, UMLUseCaseDiagram.class); |
98 | 900 | diagramClasses.put(DiagramType.State, UMLStateDiagram.class); |
99 | 900 | diagramClasses.put(DiagramType.Deployment, UMLDeploymentDiagram.class); |
100 | 900 | diagramClasses.put(DiagramType.Collaboration, |
101 | UMLCollaborationDiagram.class); | |
102 | 900 | diagramClasses.put(DiagramType.Activity, UMLActivityDiagram.class); |
103 | 900 | } |
104 | ||
105 | /** | |
106 | * @return the singleton | |
107 | */ | |
108 | public static DiagramFactory getInstance() { | |
109 | 3016 | return diagramFactory; |
110 | } | |
111 | ||
112 | ||
113 | /** | |
114 | * Factory method to create a new default instance of an ArgoDiagram. | |
115 | * @param namespace The namespace that (in)directly | |
116 | * owns the elements on the diagram | |
117 | * @return the newly instantiated class diagram | |
118 | */ | |
119 | public ArgoDiagram createDefaultDiagram(Object namespace) { | |
120 | 1 | return createDiagram(DiagramType.Class, namespace, null); |
121 | } | |
122 | ||
123 | /** | |
124 | * Factory method to create a new instance of an ArgoDiagram. | |
125 | * | |
126 | * @param type The class of rendering diagram to create | |
127 | * @param namespace The namespace that (in)directly | |
128 | * owns the elements on the diagram | |
129 | * @param machine The StateMachine for the diagram | |
130 | * (only: statemachine - activitygraph) | |
131 | * @return the newly instantiated class diagram | |
132 | * @deprecated for 0.27.3 by tfmorris. Use | |
133 | * {@link #create(DiagramType, Object, DiagramSettings)}. The 'owner' | |
134 | * argument should be the 'machine' for a state diagram or activity diagram | |
135 | * (which can figure out the correct namespace from that) and the | |
136 | * 'namespace' for all others. | |
137 | */ | |
138 | @Deprecated | |
139 | public ArgoDiagram createDiagram(final DiagramType type, | |
140 | final Object namespace, final Object machine) { | |
141 | ||
142 | 20 | DiagramSettings settings = ProjectManager.getManager() |
143 | .getCurrentProject().getProjectSettings() | |
144 | .getDefaultDiagramSettings(); | |
145 | ||
146 | 20 | return createInternal(type, namespace, machine, settings); |
147 | } | |
148 | ||
149 | ||
150 | /** | |
151 | * Factory method to create a new instance of an ArgoDiagram. | |
152 | * | |
153 | * @param type The class of rendering diagram to create | |
154 | * @param owner the owning UML element. For most diagrams this is a | |
155 | * namespace, but for the state diagram it is the state machine | |
156 | * and for the activity diagram it is the context. | |
157 | * @param settings default rendering settings for the diagram | |
158 | * @return the newly instantiated class diagram | |
159 | */ | |
160 | public ArgoDiagram create( | |
161 | final DiagramType type, | |
162 | final Object owner, | |
163 | final DiagramSettings settings) { | |
164 | ||
165 | 2096 | return createInternal(type, owner, null, settings); |
166 | } | |
167 | ||
168 | ||
169 | /* | |
170 | * Create a diagram. This 4-arg version is only for internal use. The | |
171 | * 'namespace' argument is deprecated and not used in the new APIs. | |
172 | */ | |
173 | private ArgoDiagram createInternal(final DiagramType type, | |
174 | final Object namespace, final Object machine, | |
175 | DiagramSettings settings) { | |
176 | final ArgoDiagram diagram; | |
177 | ||
178 | 2116 | if (settings == null) { |
179 | 0 | throw new IllegalArgumentException( |
180 | "DiagramSettings may not be null"); | |
181 | } | |
182 | ||
183 | 2116 | Object factory = factories.get(type); |
184 | 2116 | if (factory != null) { |
185 | Object owner; | |
186 | 19 | if (machine != null) { |
187 | 0 | owner = machine; |
188 | } else { | |
189 | 19 | owner = namespace; |
190 | } | |
191 | 19 | if (factory instanceof DiagramFactoryInterface2) { |
192 | 19 | diagram = ((DiagramFactoryInterface2) factory).createDiagram( |
193 | owner, (String) null, settings); | |
194 | 0 | } else if (factory instanceof DiagramFactoryInterface) { |
195 | 0 | diagram = ((DiagramFactoryInterface) factory).createDiagram( |
196 | namespace, machine); | |
197 | 0 | diagram.setDiagramSettings(settings); |
198 | } else { | |
199 | // This shouldn't be possible, but just in case | |
200 | 0 | throw new IllegalStateException( |
201 | "Unknown factory type registered"); | |
202 | } | |
203 | 19 | } else { |
204 | 2097 | if ((type == DiagramType.State || type == DiagramType.Activity) |
205 | && machine == null) { | |
206 | 58 | diagram = createDiagram(diagramClasses.get(type), null, |
207 | namespace); | |
208 | } else { | |
209 | 2039 | diagram = createDiagram(diagramClasses.get(type), namespace, |
210 | machine); | |
211 | } | |
212 | 2097 | diagram.setDiagramSettings(settings); |
213 | } | |
214 | ||
215 | 2116 | return diagram; |
216 | } | |
217 | ||
218 | /** | |
219 | * Factory method to create a new instance of an ArgoDiagram. | |
220 | * | |
221 | * @param type The class of rendering diagram to create | |
222 | * @param namespace The namespace that (in)directly | |
223 | * owns the elements on the diagram | |
224 | * @param machine The StateMachine for the diagram | |
225 | * (only: statemachine - activitygraph) | |
226 | * @return the newly instantiated class diagram | |
227 | * @deprecated for 0.25.4 by tfmorris. Use | |
228 | * {@link #create(DiagramType, Object, DiagramSettings)}. The 'owner' | |
229 | * argument should be the 'machine' for a state diagram or activity diagram | |
230 | * (which can figure out the correct namespace from that) and the | |
231 | * 'namespace' for all others. | |
232 | */ | |
233 | @Deprecated | |
234 | private ArgoDiagram createDiagram(Class type, Object namespace, | |
235 | Object machine) { | |
236 | ||
237 | 2097 | ArgoDiagram diagram = null; |
238 | 2097 | Class diType = null; |
239 | ||
240 | // TODO: Convert all to use standard factory registration | |
241 | 2097 | if (type == UMLClassDiagram.class) { |
242 | 1028 | diagram = new UMLClassDiagram(namespace); |
243 | 1028 | diType = ClassDiagram.class; |
244 | 1069 | } else if (type == UMLUseCaseDiagram.class) { |
245 | 973 | diagram = new UMLUseCaseDiagram(namespace); |
246 | 973 | diType = UseCaseDiagram.class; |
247 | 96 | } else if (type == UMLStateDiagram.class) { |
248 | 39 | diagram = new UMLStateDiagram(namespace, machine); |
249 | 39 | diType = StateDiagram.class; |
250 | 57 | } else if (type == UMLDeploymentDiagram.class) { |
251 | 19 | diagram = new UMLDeploymentDiagram(namespace); |
252 | 19 | diType = DeploymentDiagram.class; |
253 | 38 | } else if (type == UMLCollaborationDiagram.class) { |
254 | 19 | diagram = new UMLCollaborationDiagram(namespace); |
255 | 19 | diType = CollaborationDiagram.class; |
256 | 19 | } else if (type == UMLActivityDiagram.class) { |
257 | 19 | diagram = new UMLActivityDiagram(namespace, machine); |
258 | 19 | diType = ActivityDiagram.class; |
259 | } | |
260 | ||
261 | 2097 | if (diagram == null) { |
262 | 0 | throw new IllegalArgumentException ("Unknown diagram type"); |
263 | } | |
264 | ||
265 | 2097 | if (Model.getDiagramInterchangeModel() != null) { |
266 | // TODO: This is never executed as Ludos DI work was never | |
267 | // finished. | |
268 | 0 | diagram.getGraphModel().addGraphEventListener( |
269 | GraphChangeAdapter.getInstance()); | |
270 | /* | |
271 | * The diagram are always owned by the model | |
272 | * in this first implementation. | |
273 | */ | |
274 | 0 | DiDiagram dd = GraphChangeAdapter.getInstance() |
275 | .createDiagram(diType, namespace); | |
276 | 0 | ((UMLMutableGraphSupport) diagram.getGraphModel()).setDiDiagram(dd); |
277 | } | |
278 | ||
279 | 2097 | return diagram; |
280 | } | |
281 | ||
282 | /** | |
283 | * Factory method to remove a diagram. | |
284 | * | |
285 | * @param diagram the diagram | |
286 | * @return the diagram that was removed | |
287 | */ | |
288 | public ArgoDiagram removeDiagram(ArgoDiagram diagram) { | |
289 | ||
290 | 0 | DiDiagram dd = |
291 | ((UMLMutableGraphSupport) diagram.getGraphModel()).getDiDiagram(); | |
292 | 0 | if (dd != null) { |
293 | 0 | GraphChangeAdapter.getInstance().removeDiagram(dd); |
294 | } | |
295 | 0 | return diagram; |
296 | } | |
297 | ||
298 | ||
299 | /** | |
300 | * Register a specific factory class to create diagram instances for a | |
301 | * specific diagram type | |
302 | * | |
303 | * @param type the diagram type | |
304 | * @param factory the factory instance | |
305 | */ | |
306 | public void registerDiagramFactory( | |
307 | final DiagramType type, | |
308 | final DiagramFactoryInterface2 factory) { | |
309 | // TODO: This uses a "last one wins" algorithm for registration | |
310 | // We should warn if a factory is being overwritten. | |
311 | 900 | factories.put(type, factory); |
312 | 900 | } |
313 | } |