Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DnDJGraph |
|
| 2.6363636363636362;2.636 |
1 | /* $Id: DnDJGraph.java 18729 2010-09-10 16:10:34Z bobtarling $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009-2010 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 | * Michiel van der Wulp | |
11 | * Bob Tarling | |
12 | ***************************************************************************** | |
13 | * | |
14 | * Some portions of this file was previously release using the BSD License: | |
15 | */ | |
16 | ||
17 | // Copyright (c) 2005-2009 The Regents of the University of California. All | |
18 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
19 | // software and its documentation without fee, and without a written | |
20 | // agreement is hereby granted, provided that the above copyright notice | |
21 | // and this paragraph appear in all copies. This software program and | |
22 | // documentation are copyrighted by The Regents of the University of | |
23 | // California. The software program and documentation are supplied "AS | |
24 | // IS", without any accompanying services from The Regents. The Regents | |
25 | // does not warrant that the operation of the program will be | |
26 | // uninterrupted or error-free. The end-user understands that the program | |
27 | // was developed for research purposes and is advised not to rely | |
28 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
29 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
30 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
31 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
32 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
33 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
34 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
35 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
36 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
37 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
38 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
39 | ||
40 | package org.argouml.uml.diagram.ui; | |
41 | ||
42 | import java.awt.datatransfer.Transferable; | |
43 | import java.awt.datatransfer.UnsupportedFlavorException; | |
44 | import java.awt.dnd.DnDConstants; | |
45 | import java.awt.dnd.DropTarget; | |
46 | import java.awt.dnd.DropTargetDragEvent; | |
47 | import java.awt.dnd.DropTargetDropEvent; | |
48 | import java.awt.dnd.DropTargetEvent; | |
49 | import java.awt.dnd.DropTargetListener; | |
50 | import java.io.IOException; | |
51 | import java.util.Collection; | |
52 | import java.util.Iterator; | |
53 | ||
54 | import org.apache.log4j.Logger; | |
55 | import org.argouml.kernel.Owned; | |
56 | import org.argouml.ui.TransferableModelElements; | |
57 | import org.argouml.uml.diagram.ArgoDiagram; | |
58 | import org.argouml.uml.diagram.DiagramElement; | |
59 | import org.argouml.uml.diagram.DiagramUtils; | |
60 | import org.tigris.gef.base.Diagram; | |
61 | import org.tigris.gef.base.Editor; | |
62 | import org.tigris.gef.base.Globals; | |
63 | import org.tigris.gef.graph.ConnectionConstrainer; | |
64 | import org.tigris.gef.graph.GraphModel; | |
65 | import org.tigris.gef.graph.MutableGraphModel; | |
66 | import org.tigris.gef.graph.presentation.JGraph; | |
67 | import org.tigris.gef.presentation.Fig; | |
68 | import org.tigris.gef.presentation.FigNode; | |
69 | ||
70 | /** | |
71 | * This is a JGraph with Drag and Drop capabilities. | |
72 | * | |
73 | * @author mvw | |
74 | */ | |
75 | class DnDJGraph | |
76 | extends JGraph | |
77 | implements DropTargetListener { | |
78 | ||
79 | 900 | private static final Logger LOG = Logger.getLogger(DnDJGraph.class); |
80 | ||
81 | /** | |
82 | * The constructor. | |
83 | * | |
84 | */ | |
85 | public DnDJGraph() { | |
86 | 900 | super(); |
87 | 900 | makeDropTarget(); |
88 | 900 | } |
89 | ||
90 | /** | |
91 | * The constructor. | |
92 | * | |
93 | * @param cc The ConnectionConstrainer. | |
94 | */ | |
95 | public DnDJGraph(ConnectionConstrainer cc) { | |
96 | 0 | super(cc); |
97 | 0 | makeDropTarget(); |
98 | 0 | } |
99 | ||
100 | /** | |
101 | * The constructor. | |
102 | * | |
103 | * @param d The Diagram. | |
104 | */ | |
105 | public DnDJGraph(Diagram d) { | |
106 | 0 | super(d); |
107 | 0 | makeDropTarget(); |
108 | 0 | } |
109 | ||
110 | /** | |
111 | * The constructor. | |
112 | * | |
113 | * @param gm The GraphModel. | |
114 | */ | |
115 | public DnDJGraph(GraphModel gm) { | |
116 | 0 | super(gm); |
117 | 0 | makeDropTarget(); |
118 | 0 | } |
119 | ||
120 | /** | |
121 | * The constructor. | |
122 | * | |
123 | * @param ed The Editor. | |
124 | */ | |
125 | public DnDJGraph(Editor ed) { | |
126 | 0 | super(ed); |
127 | 0 | makeDropTarget(); |
128 | 0 | } |
129 | ||
130 | private void makeDropTarget() { | |
131 | 900 | new DropTarget(this, |
132 | DnDConstants.ACTION_COPY_OR_MOVE, | |
133 | this); | |
134 | 900 | } |
135 | ||
136 | /* | |
137 | * @see java.awt.dnd.DropTargetListener#dragEnter( | |
138 | * java.awt.dnd.DropTargetDragEvent) | |
139 | */ | |
140 | public void dragEnter(DropTargetDragEvent dtde) { | |
141 | try { | |
142 | 0 | if (dtde.isDataFlavorSupported( |
143 | TransferableModelElements.UML_COLLECTION_FLAVOR)) { | |
144 | 0 | dtde.acceptDrag(dtde.getDropAction()); |
145 | 0 | return; |
146 | } | |
147 | 0 | } catch (NullPointerException e) { |
148 | // System.err.println("NullPointerException ignored."); | |
149 | 0 | } |
150 | 0 | dtde.rejectDrag(); |
151 | 0 | } |
152 | ||
153 | /* | |
154 | * @see java.awt.dnd.DropTargetListener#dragOver( | |
155 | * java.awt.dnd.DropTargetDragEvent) | |
156 | */ | |
157 | public void dragOver(DropTargetDragEvent dtde) { | |
158 | try { | |
159 | 0 | ArgoDiagram dia = DiagramUtils.getActiveDiagram(); |
160 | 0 | if (dia instanceof UMLDiagram |
161 | /*&& ((UMLDiagram) dia).doesAccept(dtde.getSource())*/) { | |
162 | 0 | dtde.acceptDrag(dtde.getDropAction()); |
163 | 0 | return; |
164 | } | |
165 | 0 | if (dtde.isDataFlavorSupported( |
166 | TransferableModelElements.UML_COLLECTION_FLAVOR)) { | |
167 | 0 | dtde.acceptDrag(dtde.getDropAction()); |
168 | 0 | return; |
169 | } | |
170 | 0 | } catch (NullPointerException e) { |
171 | // System.err.println("NullPointerException ignored."); | |
172 | 0 | } |
173 | 0 | dtde.rejectDrag(); |
174 | 0 | } |
175 | ||
176 | /* | |
177 | * @see java.awt.dnd.DropTargetListener#dropActionChanged( | |
178 | * java.awt.dnd.DropTargetDragEvent) | |
179 | */ | |
180 | public void dropActionChanged(DropTargetDragEvent dtde) { | |
181 | // ignored | |
182 | 0 | } |
183 | ||
184 | /* | |
185 | * @see java.awt.dnd.DropTargetListener#dragExit( | |
186 | * java.awt.dnd.DropTargetEvent) | |
187 | */ | |
188 | public void dragExit(DropTargetEvent dte) { | |
189 | // ignored | |
190 | 0 | } |
191 | ||
192 | /* | |
193 | * @see java.awt.dnd.DropTargetListener#drop( | |
194 | * java.awt.dnd.DropTargetDropEvent) | |
195 | */ | |
196 | public void drop(DropTargetDropEvent dropTargetDropEvent) { | |
197 | 0 | Transferable tr = dropTargetDropEvent.getTransferable(); |
198 | //if the flavor is not supported, then reject the drop: | |
199 | 0 | if (!tr.isDataFlavorSupported( |
200 | TransferableModelElements.UML_COLLECTION_FLAVOR)) { | |
201 | 0 | dropTargetDropEvent.rejectDrop(); |
202 | 0 | return; |
203 | } | |
204 | ||
205 | 0 | dropTargetDropEvent.acceptDrop(dropTargetDropEvent.getDropAction()); |
206 | //get the model elements that are being transfered. | |
207 | Collection modelElements; | |
208 | try { | |
209 | 0 | ArgoDiagram diagram = DiagramUtils.getActiveDiagram(); |
210 | 0 | modelElements = |
211 | (Collection) tr.getTransferData( | |
212 | TransferableModelElements.UML_COLLECTION_FLAVOR); | |
213 | ||
214 | 0 | Iterator i = modelElements.iterator(); |
215 | 0 | while (i.hasNext()) { |
216 | /* TODO: Why not call UMLDiagram.doesAccept() first, | |
217 | * like in ClassDiagramRenderer? */ | |
218 | 0 | DiagramElement figNode = ((UMLDiagram ) diagram).drop(i.next(), |
219 | dropTargetDropEvent.getLocation()); | |
220 | ||
221 | 0 | if (figNode != null && figNode instanceof Owned) { |
222 | 0 | MutableGraphModel gm = |
223 | (MutableGraphModel) diagram.getGraphModel(); | |
224 | 0 | Object owner = ((Owned) figNode).getOwner(); |
225 | 0 | if (!gm.getNodes().contains(owner)) { |
226 | 0 | gm.getNodes().add(owner); |
227 | } | |
228 | ||
229 | 0 | Globals.curEditor().getLayerManager().getActiveLayer() |
230 | .add((Fig) figNode); | |
231 | 0 | if (figNode instanceof FigNode && figNode instanceof Owned) { |
232 | 0 | gm.addNodeRelatedEdges(((Owned) figNode).getOwner()); |
233 | } | |
234 | } | |
235 | 0 | } |
236 | ||
237 | 0 | dropTargetDropEvent.getDropTargetContext().dropComplete(true); |
238 | 0 | } catch (UnsupportedFlavorException e) { |
239 | 0 | LOG.debug("Exception caught", e); |
240 | 0 | } catch (IOException e) { |
241 | 0 | LOG.debug("Exception caught", e); |
242 | 0 | } |
243 | 0 | } |
244 | ||
245 | ||
246 | /** | |
247 | * The UID. | |
248 | */ | |
249 | private static final long serialVersionUID = -5753683239435014182L; | |
250 | } |