Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AddExistingNodeCommand |
|
| 1.8571428571428572;1.857 |
1 | /* $Id: AddExistingNodeCommand.java 17865 2010-01-12 20:45:26Z 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-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 | ||
40 | package org.argouml.uml.diagram.ui; | |
41 | ||
42 | import java.awt.Point; | |
43 | import java.awt.Rectangle; | |
44 | import java.awt.dnd.DropTargetDropEvent; | |
45 | import java.awt.event.MouseEvent; | |
46 | ||
47 | import org.argouml.i18n.Translator; | |
48 | import org.argouml.model.Model; | |
49 | import org.argouml.uml.diagram.ArgoDiagram; | |
50 | import org.argouml.uml.diagram.DiagramUtils; | |
51 | import org.tigris.gef.base.Command; | |
52 | import org.tigris.gef.base.Editor; | |
53 | import org.tigris.gef.base.Globals; | |
54 | import org.tigris.gef.base.ModePlace; | |
55 | import org.tigris.gef.graph.GraphFactory; | |
56 | import org.tigris.gef.graph.GraphModel; | |
57 | import org.tigris.gef.graph.MutableGraphModel; | |
58 | import org.tigris.gef.presentation.Fig; | |
59 | ||
60 | /** | |
61 | * ActionAddExistingNode enables pasting of an existing node into a Diagram. | |
62 | */ | |
63 | public class AddExistingNodeCommand implements Command, GraphFactory { | |
64 | ||
65 | /** | |
66 | * The UML object to be added to the diagram. | |
67 | */ | |
68 | private Object object; | |
69 | ||
70 | /** | |
71 | * the location to drop the node. | |
72 | */ | |
73 | private Point location; | |
74 | ||
75 | /** | |
76 | * 0 if this is the 1st element dropped here, | |
77 | * n if this is the (n+1)-th element dropped here. | |
78 | */ | |
79 | private int count; | |
80 | ||
81 | /** | |
82 | * The constructor. | |
83 | * | |
84 | * @param o the UML modelelement to be added | |
85 | */ | |
86 | 0 | public AddExistingNodeCommand(Object o) { |
87 | 0 | object = o; |
88 | 0 | } |
89 | ||
90 | /** | |
91 | * The constructor. | |
92 | * | |
93 | * @param o the UML modelelement to be added | |
94 | * @param event the DropTargetDropEvent that caused this. | |
95 | * Also <code>null</code> is acceptable | |
96 | * @param cnt 0 if this is the 1st element dropped here, | |
97 | * n if this is the (n+1)-th element dropped here. | |
98 | */ | |
99 | public AddExistingNodeCommand(Object o, DropTargetDropEvent event, | |
100 | 0 | int cnt) { |
101 | 0 | object = o; |
102 | 0 | location = event.getLocation(); |
103 | 0 | count = cnt; |
104 | 0 | } |
105 | ||
106 | /** | |
107 | * @param o the UML modelelement to be added | |
108 | * @param dropLocation the point where to drop the node. | |
109 | * Also <code>null</code> is acceptable. | |
110 | * @param cnt 0 if this is the 1st element dropped here, | |
111 | * n if this is the (n+1)-th element dropped here. | |
112 | */ | |
113 | public AddExistingNodeCommand(Object o, Point dropLocation, | |
114 | 0 | int cnt) { |
115 | 0 | object = o; |
116 | 0 | location = dropLocation; |
117 | 0 | count = cnt; |
118 | 0 | } |
119 | ||
120 | /* | |
121 | * @see org.tigris.gef.base.Command#execute() | |
122 | */ | |
123 | public void execute() { | |
124 | 0 | Editor ce = Globals.curEditor(); |
125 | 0 | GraphModel gm = ce.getGraphModel(); |
126 | 0 | if (!(gm instanceof MutableGraphModel)) { |
127 | 0 | return; |
128 | } | |
129 | ||
130 | 0 | String instructions = null; |
131 | 0 | ModePlace placeMode = null; |
132 | 0 | if (object != null) { |
133 | 0 | ArgoDiagram activeDiagram = DiagramUtils.getActiveDiagram(); |
134 | ||
135 | 0 | if (activeDiagram instanceof UMLDiagram |
136 | && ((UMLDiagram) activeDiagram).doesAccept(object)) { | |
137 | 0 | instructions = ((UMLDiagram) activeDiagram). |
138 | getInstructions(object); | |
139 | 0 | placeMode = ((UMLDiagram) activeDiagram). |
140 | getModePlace(this, instructions); | |
141 | 0 | placeMode.setAddRelatedEdges(true); |
142 | } else { | |
143 | // TODO: work here ! | |
144 | 0 | instructions = |
145 | Translator.localize( | |
146 | "misc.message.click-on-diagram-to-add", | |
147 | new Object[] {Model.getFacade().toString(object), }); | |
148 | 0 | placeMode = new ModePlace(this, instructions); |
149 | 0 | placeMode.setAddRelatedEdges(true); |
150 | } | |
151 | 0 | Globals.showStatus(instructions); |
152 | } | |
153 | ||
154 | 0 | if (location == null) { |
155 | 0 | Globals.mode(placeMode, false); |
156 | } else { | |
157 | /* Calculate the drop location, and place every n-th element | |
158 | * at an offset proportional to n. | |
159 | */ | |
160 | 0 | Point p = |
161 | new Point( | |
162 | location.x + (count * 100), | |
163 | location.y); | |
164 | /* Take canvas scrolling into account. | |
165 | * The implementation below does place the element correctly | |
166 | * when the canvas has been scrolled. | |
167 | */ | |
168 | 0 | Rectangle r = ce.getJComponent().getVisibleRect(); |
169 | 0 | p.translate(r.x, r.y); |
170 | /* Simulate a press of the mouse above the calculated point: */ | |
171 | 0 | MouseEvent me = |
172 | new MouseEvent( | |
173 | ce.getJComponent(), | |
174 | 0, | |
175 | 0, | |
176 | 0, | |
177 | p.x, | |
178 | p.y, | |
179 | 0, | |
180 | false); | |
181 | 0 | placeMode.mousePressed(me); |
182 | /* Simulate a release of the mouse: */ | |
183 | 0 | me = |
184 | new MouseEvent( | |
185 | ce.getJComponent(), | |
186 | 0, | |
187 | 0, | |
188 | 0, | |
189 | p.x, | |
190 | p.y, | |
191 | 0, | |
192 | false); | |
193 | 0 | placeMode.mouseReleased(me); |
194 | ||
195 | /* Set the size of the object's fig to minimum. | |
196 | * See issue 3410. | |
197 | * This binds the use of this Command to the | |
198 | * current diagram of the current project! | |
199 | */ | |
200 | 0 | ArgoDiagram diagram = DiagramUtils.getActiveDiagram(); |
201 | 0 | Fig aFig = diagram.presentationFor(object); |
202 | 0 | aFig.setSize(aFig.getPreferredSize()); |
203 | } | |
204 | 0 | } |
205 | ||
206 | //////////////////////////////////////////////////////////////// | |
207 | // GraphFactory implementation | |
208 | ||
209 | /* | |
210 | * @see org.tigris.gef.graph.GraphFactory#makeGraphModel() | |
211 | */ | |
212 | 0 | public GraphModel makeGraphModel() { return null; } |
213 | ||
214 | /* | |
215 | * @see org.tigris.gef.graph.GraphFactory#makeEdge() | |
216 | */ | |
217 | 0 | public Object makeEdge() { return null; } |
218 | ||
219 | /* | |
220 | * @see org.tigris.gef.graph.GraphFactory#makeNode() | |
221 | */ | |
222 | public Object makeNode() { | |
223 | 0 | return object; |
224 | } | |
225 | ||
226 | } |