Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionAddDiagram |
|
| 4.166666666666667;4.167 |
1 | /* $Id: ActionAddDiagram.java 17881 2010-01-12 21:09:28Z 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 | * bobtarling | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2008 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.ui; | |
40 | ||
41 | import java.awt.event.ActionEvent; | |
42 | import java.util.Collection; | |
43 | import java.util.Iterator; | |
44 | ||
45 | import javax.swing.Action; | |
46 | ||
47 | import org.apache.log4j.Logger; | |
48 | import org.argouml.application.helpers.ResourceLoaderWrapper; | |
49 | import org.argouml.i18n.Translator; | |
50 | import org.argouml.kernel.Project; | |
51 | import org.argouml.kernel.ProjectManager; | |
52 | import org.argouml.model.Model; | |
53 | import org.argouml.ui.explorer.ExplorerEventAdaptor; | |
54 | import org.argouml.ui.targetmanager.TargetManager; | |
55 | import org.argouml.uml.diagram.ArgoDiagram; | |
56 | import org.argouml.uml.diagram.DiagramSettings; | |
57 | import org.argouml.ui.UndoableAction; | |
58 | ||
59 | /** | |
60 | * Abstract class that is the parent of all actions adding diagrams to ArgoUML. | |
61 | * The children of this class should implement createDiagram to do any specific | |
62 | * actions for creating a diagram and isValidNamespace that checks if some | |
63 | * namespace is valid to add the diagram to. <p> | |
64 | * | |
65 | * ArgoUML shall never create a diagram for a read-only modelelement.<p> | |
66 | * | |
67 | * TODO: This class should be merged with ActionNewDiagram. | |
68 | * | |
69 | * @author jaap.branderhorst@xs4all.nl | |
70 | */ | |
71 | public abstract class ActionAddDiagram extends UndoableAction { | |
72 | /** | |
73 | * Logger. | |
74 | */ | |
75 | 900 | private static final Logger LOG = |
76 | Logger.getLogger(ActionAddDiagram.class); | |
77 | ||
78 | /** | |
79 | * Constructor for ActionAddDiagram. | |
80 | * | |
81 | * @param s the name for this action | |
82 | */ | |
83 | public ActionAddDiagram(String s) { | |
84 | 13500 | super(Translator.localize(s), |
85 | ResourceLoaderWrapper.lookupIcon(s)); | |
86 | // Set the tooltip string: | |
87 | 13500 | putValue(Action.SHORT_DESCRIPTION, |
88 | Translator.localize(s)); | |
89 | 13500 | } |
90 | ||
91 | /* | |
92 | * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) | |
93 | */ | |
94 | @Override | |
95 | public void actionPerformed(ActionEvent e) { | |
96 | // TODO: The project should be bound to the action when it is created? | |
97 | 97 | Project p = ProjectManager.getManager().getCurrentProject(); |
98 | 97 | Object ns = findNamespace(); |
99 | ||
100 | 97 | if (ns != null && isValidNamespace(ns)) { |
101 | 97 | super.actionPerformed(e); |
102 | 97 | DiagramSettings settings = |
103 | p.getProjectSettings().getDefaultDiagramSettings(); | |
104 | // TODO: We should really be passing the default settings to | |
105 | // the diagram factory so they get set at creation time | |
106 | 97 | ArgoDiagram diagram = createDiagram(ns, settings); |
107 | ||
108 | 97 | p.addMember(diagram); |
109 | //TODO: make the explorer listen to project member property | |
110 | //changes... to eliminate coupling on gui. | |
111 | 97 | ExplorerEventAdaptor.getInstance().modelElementAdded(ns); |
112 | 97 | TargetManager.getInstance().setTarget(diagram); |
113 | 97 | } else { |
114 | 0 | LOG.error("No valid namespace found"); |
115 | 0 | throw new IllegalStateException("No valid namespace found"); |
116 | } | |
117 | 97 | } |
118 | ||
119 | /** | |
120 | * Find the right namespace for the diagram. | |
121 | * | |
122 | * @return the namespace or null | |
123 | */ | |
124 | protected Object findNamespace() { | |
125 | 97 | Project p = ProjectManager.getManager().getCurrentProject(); |
126 | 97 | Object target = TargetManager.getInstance().getModelTarget(); |
127 | 97 | Object ns = null; |
128 | 97 | if (target == null || !Model.getFacade().isAModelElement(target) |
129 | || Model.getModelManagementHelper().isReadOnly(target)) { | |
130 | // get the first editable extent (which is OK unless there is more | |
131 | // than one editable extent) | |
132 | 97 | target = null; |
133 | 97 | Iterator iter = p.getRoots().iterator(); |
134 | 97 | while (iter.hasNext()) { |
135 | 97 | Object o = iter.next(); |
136 | 97 | if (!Model.getModelManagementHelper().isReadOnly(o)) { |
137 | 97 | target = o; |
138 | 97 | break; |
139 | } | |
140 | 0 | } |
141 | 97 | if (target == null) { |
142 | // no way, we have to give up | |
143 | 0 | return null; |
144 | } | |
145 | } | |
146 | 97 | if (Model.getFacade().isANamespace(target)) { |
147 | 97 | ns = target; |
148 | } else { | |
149 | 0 | Object owner = null; |
150 | 0 | if (Model.getFacade().isAOperation(target)) { |
151 | 0 | owner = Model.getFacade().getOwner(target); |
152 | 0 | if (owner != null && Model.getFacade().isANamespace(owner)) { |
153 | 0 | ns = owner; |
154 | } | |
155 | } | |
156 | 0 | if (ns == null && Model.getFacade().isAModelElement(target)) { |
157 | 0 | owner = Model.getFacade().getNamespace(target); |
158 | 0 | if (owner != null && Model.getFacade().isANamespace(owner)) { |
159 | 0 | ns = owner; |
160 | } | |
161 | } | |
162 | } | |
163 | 97 | if (ns == null) { |
164 | 0 | ns = p.getRoot(); |
165 | } | |
166 | 97 | return ns; |
167 | } | |
168 | ||
169 | /** | |
170 | * Test if the given namespace is a valid namespace to add the diagram to. | |
171 | * | |
172 | * @param ns the namespace to check | |
173 | * @return Returns <code>true</code> if valid. | |
174 | */ | |
175 | public abstract boolean isValidNamespace(Object ns); | |
176 | ||
177 | /** | |
178 | * Creates the diagram. Classes derived from this class should implement any | |
179 | * specific behaviour to create the diagram. | |
180 | * | |
181 | * @param ns The namespace the UMLDiagram should get. | |
182 | * @return UMLDiagram | |
183 | * @deprecated for 0.27.3 by tfmorris. Subclasses should override | |
184 | * {@link #createDiagram(Object, DiagramSettings)}. This method | |
185 | * is no longer abstract, so implementing classes may remove it. | |
186 | */ | |
187 | @Deprecated | |
188 | public ArgoDiagram createDiagram(@SuppressWarnings("unused") Object ns) { | |
189 | // Do nothing during the deprecation period, then it can be removed. | |
190 | 0 | return null; |
191 | } | |
192 | ||
193 | /** | |
194 | * Create a new diagram. To be implemented by subclasses. It will become | |
195 | * abstract after 0.28 to enforce this requirement. | |
196 | * | |
197 | * @param owner owner of the diagram. May be a namespace, statemachine, or | |
198 | * collaboration depending on the type of diagram. | |
199 | * @param settings default rendering settings for all figs in the new | |
200 | * diagram | |
201 | * @return newly created diagram | |
202 | */ | |
203 | public ArgoDiagram createDiagram(Object owner, DiagramSettings settings) { | |
204 | 19 | ArgoDiagram d = createDiagram(owner); |
205 | 19 | d.setDiagramSettings(settings); |
206 | 19 | return d; |
207 | } | |
208 | ||
209 | } |