3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005-2009 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
23 package ini
.trakem2
.tree
;
25 import ini
.trakem2
.Project
;
26 import ini
.trakem2
.utils
.IJError
;
27 import ini
.trakem2
.utils
.Utils
;
28 import ini
.trakem2
.display
.Display
;
31 import javax
.swing
.tree
.*;
32 import java
.awt
.dnd
.*;
33 import java
.util
.ArrayList
;
35 /** Adapted from freely available code by DeuDeu from http://forum.java.sun.com/thread.jspa?threadID=296255&start=0&tstart=0 */
36 public class DefaultTreeTransferHandler
extends AbstractTreeTransferHandler
{
40 public DefaultTreeTransferHandler(Project project
, DNDTree tree
, int action
) {
41 super(tree
, action
, true);
42 this.project
= project
;
45 public boolean canPerformAction(DNDTree target
, DefaultMutableTreeNode dragged_node
, int action
, Point location
) {
48 Utils.log2(DnDConstants.ACTION_COPY + " " + DnDConstants.ACTION_COPY_OR_MOVE + " " + DnDConstants.ACTION_LINK + " " + DnDConstants.ACTION_MOVE + " " + DnDConstants.ACTION_NONE + " " + DnDConstants.ACTION_REFERENCE);
49 Utils.log2("action: " + action);
54 // prevent drags from non-tree components
55 if (null == dragged_node
) return false;
56 // Can't drop onto a TemplateTree
57 if (target
instanceof TemplateTree
) {
60 // Can't drag attribute nodes!
61 if (dragged_node
.getUserObject() instanceof Attribute
) {
64 // Can't drag a node that contains a Project!
65 if (dragged_node
.getUserObject() instanceof ProjectThing
&& ((ProjectThing
)dragged_node
.getUserObject()).getObject() instanceof Project
) {
68 // Can't drag basic object nodes from a template tree RECONSIDERED, I like it even if it looks inconsistent (but types are types!)
70 if (dragged_node.getUserObject() instanceof TemplateThing && project.isBasicType(((Thing)dragged_node.getUserObject()).getType())) {
75 // else, the target has to be not null
76 TreePath pathTarget
= target
.getPathForLocation(location
.x
, location
.y
);
77 if (pathTarget
== null) {
78 target
.setSelectionPath(null);
83 if (action == DnDConstants.ACTION_COPY) {
84 Utils.log("can drop: Action copy");
85 } else if (action == DnDConstants.ACTION_MOVE) {
86 Utils.log("can drop: Action move");
88 Utils.log("can drop: Unexpected action: " + action);
92 target
.setSelectionPath(pathTarget
);
93 DefaultMutableTreeNode parent_node
= (DefaultMutableTreeNode
)pathTarget
.getLastPathComponent();
94 Object parent_ob
= parent_node
.getUserObject(); // can be a Thing or an Attribute
95 Thing child_thing
= (Thing
)dragged_node
.getUserObject();
97 if (DnDConstants
.ACTION_MOVE
== action
|| DnDConstants
.ACTION_COPY
== action
) {
98 if (parent_ob
instanceof ProjectThing
) {
99 ProjectThing parent_thing
= (ProjectThing
)parent_ob
;
101 // TODO: check if the parent/parent/parent/.../parent of the dragged thing can have such parent as child, and autocreate them (but not for basic things though, or only if there is no confusion possible)
103 // check if it's allowed to give to this parent such a child:
104 if (!parent_thing
.uniquePathExists(child_thing
.getType()) && !parent_thing
.canHaveAsChild(child_thing
)) {
105 //Utils.log("Not possible.");
109 // - any of the leafs in the template, including the root, to the project tree
111 // - the root leaf of the project tree
112 // - the leaf that is going to be dropped into itself or any of its descendants.
113 if (parent_node
== dragged_node
.getParent() || dragged_node
.isNodeDescendant(parent_node
)) {
114 //Utils.log("preventing dragging onto itself or any of the self children.");
119 } else if (parent_ob
instanceof Attribute
) {
120 DefaultMutableTreeNode parent_parent
= (DefaultMutableTreeNode
)parent_node
.getParent();
121 if (null == parent_parent
) return false; // should not happen
122 // the parent of an Attribute node contains ALWAYS a ProjectThing
123 Thing thing
= (Thing
)parent_parent
.getUserObject();
124 if (thing
.canHaveAsAttribute(child_thing
.getType() +"_id")) {
134 public synchronized boolean executeDrop(final DNDTree target
, DefaultMutableTreeNode dragged_node
, DefaultMutableTreeNode new_parent_node
, int action
) {
137 Utils.log2(DnDConstants.ACTION_COPY + " " + DnDConstants.ACTION_COPY_OR_MOVE + " " + DnDConstants.ACTION_LINK + " " + DnDConstants.ACTION_MOVE + " " + DnDConstants.ACTION_NONE + " " + DnDConstants.ACTION_REFERENCE);
138 Utils.log2("action: " + action);
142 // Can't drop onto a TemplateTree
143 if (target
instanceof TemplateTree
) {
146 // Can't drag attribute nodes!
147 if (dragged_node
.getUserObject() instanceof Attribute
) {
151 Thing dragged_thing
= null;
152 Object ob
= dragged_node
.getUserObject();
153 if (null != ob
&& ob
instanceof Thing
) {
154 dragged_thing
= (Thing
)ob
;
156 //Utils.log("DefaultTreeTransferHandler.executeDrop(....): null Thing in the dragged node, or not a Thing instance.");
159 ProjectThing new_parent_thing
= null;
160 Object obp
= new_parent_node
.getUserObject();
161 if (null != obp
&& obp
instanceof ProjectThing
) {
162 new_parent_thing
= (ProjectThing
)obp
;
163 } else if (obp
instanceof Attribute
) {
164 ProjectThing pt
= (ProjectThing
)((DefaultMutableTreeNode
)new_parent_node
.getParent()).getUserObject();
165 if (pt
.canHaveAsAttribute(dragged_thing
.getType() + "_id")) {
166 pt
.setAttribute(dragged_thing
.getType() + "_id", dragged_thing
);
170 // repaint the attribute node
171 target
.updateUILater();
172 //don't change the nodes of the tree
176 // Prevent adding more profiles to a profile_list if it contains at least one already
177 if (new_parent_thing
.getType().equals("profile_list") && null != new_parent_thing
.getChildren() && new_parent_thing
.getChildren().size() > 0) {
178 Utils
.showMessage("Add new profiles by duplicating and linking existing ones.\nAlternatively, start a new profile_list.");
183 if (action == DnDConstants.ACTION_COPY) {
184 Utils.log("exec drop: Action copy");
185 } else if (action == DnDConstants.ACTION_MOVE) {
186 Utils.log("exec drop: Action move");
188 Utils.log("exec drop: Unexpected action: " + action);
192 if (DnDConstants
.ACTION_MOVE
== action
|| action
== DnDConstants
.ACTION_COPY
) {
193 // MOVE is used for both dragging from the template tree to the project tree, and also for dragging within the project tree! Insane!
194 // So, detect if the dragged node is part of the tempalte or part of the project:
195 if (dragged_thing
instanceof TemplateThing
) {
196 // make a copy of the node without its children into the project tree
198 // create a new Thing of the same type of the dragged_node, and add it as child to the parent Thing. That it is of the proper type has been checked in the method above 'canPerformAction()'
199 TemplateThing tt
= (TemplateThing
)dragged_thing
;
200 //ProjectThing new_thing = new ProjectThing(tt, this.project, this.project.makeObject(tt)); // TODO WARNING: I think the this.project will always be the project of the tree in which the node is being dropped, because the DefaultTreeTransferHandler is a listener on the right tree.
203 //Utils.log2("DTTH: isBasicType: " + Project.isBasicType(dragged_thing.getType()));
204 //Utils.log2("DTTH: canHaveAsChild: " + new_parent_thing.canHaveAsChild(dragged_thing));
205 //Utils.log2("DTTH: uniquePathExists: " + new_parent_thing.uniquePathExists(dragged_thing.getType()));
206 // add missing parents if any
207 if (!Project
.isBasicType(dragged_thing
.getType()) && !new_parent_thing
.canHaveAsChild(dragged_thing
) && new_parent_thing
.uniquePathExists(dragged_thing
.getType())) {
208 // a unique path exists to one of its children or children of children, etc.
209 // 1 - get the cascade of parent types
210 ArrayList al
= new_parent_thing
.getTemplatePathTo(dragged_thing
.getType());
211 // discard first (the self) and last (the child to make)
213 al
.remove(al
.size()-1);
214 // 2 - check if any of such parents exists already, and create them if necessary
215 if (0 == al
.size()) return false; // some error ocurred ...
216 ProjectThing a_parent
= new_parent_thing
;
217 DefaultMutableTreeNode a_parent_node
= new_parent_node
;
219 while (i
<al
.size()) {
220 String type
= ((TemplateThing
)al
.get(i
)).getType();
221 ArrayList al_c
= a_parent
.findChildrenOfType(type
);
222 if (0 == al_c
.size()) {
223 // create a parent of the given type and assign it to a_parent
224 ProjectThing a_pt
= a_parent
.createChild(type
);
225 DefaultMutableTreeNode a_node
= ProjectTree
.makeNode(a_pt
);
226 ((DefaultTreeModel
)target
.getModel()).insertNodeInto(a_node
,a_parent_node
,a_parent_node
.getChildCount());
229 a_parent_node
= a_node
;
232 //if (1 != al_c.size()) return false; // more than one, can't decide!
233 // just add it to the first found:
234 a_parent
= (ProjectThing
)al_c
.get(0);
235 a_parent_node
= DNDTree
.findNode(a_parent
, target
);
239 // 3 - add the node, finally:
240 new_parent_node
= a_parent_node
;
241 new_parent_thing
= a_parent
;
242 // The creation is done below! In the above two lines the parent is adjusted, that's all.
246 for (int k = 0; k<al.size(); k++) {
247 Utils.log2("parent: " + al.get(k));
252 if (DnDConstants
.ACTION_COPY
== action
) {
253 if (Project
.isBasicType(tt
.getType()) && null == Display
.getFront()) {
256 // create nodes recursively
257 final ArrayList nc
= new_parent_thing
.createChildren(tt
.getType(), 1, true);
258 target
.addLeafs((ArrayList
<Thing
>)nc
);
263 ProjectThing new_thing
= new_parent_thing
.createChild(tt
.getType());
264 if (null == new_thing
) return false; // for example, if no Display is open for a Profile or Pipe
265 DefaultMutableTreeNode new_node
= ProjectTree
.makeNode(new_thing
);
266 if (null == new_node
) {
267 //Utils.log("DefaultTreeTransferHandler.executeDrop(....): can't add new project thing.");
271 // on success, edit the target tree
272 ((DefaultTreeModel
)target
.getModel()).insertNodeInto(new_node
,new_parent_node
,new_parent_node
.getChildCount());
273 TreePath treePath
= new TreePath(new_node
.getPath());
274 target
.scrollPathToVisible(treePath
);
275 target
.setSelectionPath(treePath
);
276 // and set the new_thing as child!
277 new_parent_thing
.addChild(new_thing
);
279 // open the parent node
280 //DNDTree.expandAllNodes(target, new_parent_node);
281 DNDTree
.expandNode(target
, new_parent_node
);
283 // the dragged node that remains in the template tree is being collapsed for no good reason: expand it
284 //DNDTree.expandAllNodes(project.getTemplateTree(), dragged_node);
285 DNDTree
.expandNode(project
.getTemplateTree(), dragged_node
);
288 } else if (DnDConstants
.ACTION_MOVE
== action
&& dragged_thing
instanceof ProjectThing
) {
289 // change the parent of the dragged thing, within the project tree, only if possible:
290 // check first if possible
291 if (!new_parent_thing
.canHaveAsChild(dragged_thing
)) return false;
292 // remove from previous parent
293 ProjectThing p_dragged_thing
= (ProjectThing
)dragged_thing
;
294 ProjectThing old_parent
= (ProjectThing
)p_dragged_thing
.getParent();
295 if (null != old_parent
) {
296 if (!old_parent
.removeChild(p_dragged_thing
)) {
300 if (!new_parent_thing
.addChild(p_dragged_thing
)) {
301 // on failure, restore
302 old_parent
.addChild(p_dragged_thing
);
305 // on success, edit the tree:
306 dragged_node
.removeFromParent();
307 ((DefaultTreeModel
)target
.getModel()).insertNodeInto(dragged_node
,new_parent_node
,new_parent_node
.getChildCount());
308 TreePath treePath
= new TreePath(dragged_node
.getPath());
309 target
.scrollPathToVisible(treePath
);
310 target
.setSelectionPath(treePath
);
312 // open the parent node
313 //DNDTree.expandAllNodes(target, new_parent_node);
314 DNDTree
.expandNode(target
, new_parent_node
);
320 } catch (Exception e
) {
329 public void destroy() {