KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > gui > tree > dnd > ScenarioTreeDropAdapter


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.gui.tree.dnd;
24
25 import java.util.Vector JavaDoc;
26
27 import org.apache.log4j.Category;
28 import org.eclipse.jface.viewers.TreeViewer;
29 import org.eclipse.jface.viewers.ViewerDropAdapter;
30 import org.eclipse.swt.dnd.DND;
31 import org.eclipse.swt.dnd.TransferData;
32 import org.objectweb.clif.scenario.util.isac.gui.tree.ScenarioTreeViewer;
33 import org.objectweb.clif.scenario.util.isac.util.tree.Node;
34 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode;
35 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
36 /**
37  * Supports dropping ScenarioNode into a tree viewer.
38  *
39  * @author JC Meillaud
40  * @author A Peyrard
41  */

42 public class ScenarioTreeDropAdapter extends ViewerDropAdapter {
43     static Category cat = Category.getInstance(ScenarioTreeDropAdapter.class.getName());
44     private TreeManager treeManager;
45
46     public ScenarioTreeDropAdapter(TreeViewer viewer) {
47         super(viewer);
48         cat.debug("-> constructor") ;
49         this.treeManager = TreeManager.getTreeManager(null);
50     }
51
52     /**
53      * Method declared on ViewerDropAdapter
54      */

55     public boolean performDrop(Object JavaDoc data) {
56         cat.debug("-> performDrop") ;
57         ScenarioNode target = (ScenarioNode) getCurrentTarget();
58         if (target == null)
59             target = (ScenarioNode) getViewer().getInput();
60         ScenarioNode toDrop = (ScenarioNode) data;
61         TreeViewer viewer = (TreeViewer) getViewer();
62         // cannot drop a ScenarioNode onto itself or a child
63
if (toDrop.contains(target))
64             return false;
65
66         // Verify that the node to drop can be a child of the target
67
String JavaDoc typeToDrop = treeManager.getNodeType(toDrop);
68         Vector JavaDoc childrenAllowed = treeManager.childrenAllowed((ScenarioNode)target);
69         if (!childrenAllowed.contains(typeToDrop))
70             return false;
71         
72         // copy the node to be drop
73
ScenarioNode copy = treeManager.copyScenarioNode(toDrop) ;
74         // add the node to the target
75
target.addChild(copy) ;
76         
77         ((ScenarioTreeViewer)viewer).expand(copy, true) ;
78         
79         viewer.refresh();
80
81         return true;
82     }
83
84     /**
85      * Method declared on ViewerDropAdapter
86      */

87     public boolean validateDrop(Object JavaDoc target, int op, TransferData type) {
88         cat.debug("-> validateDrop") ;
89         // We can't move an objet on the plugins node
90
// however we don't need to move a session object into it's location
91
if (treeManager.getNodeType((ScenarioNode)target).equals(Node.PLUGINS))
92             if (op == DND.DROP_MOVE)
93                 return false ;
94         if (ScenarioTreeTransfer.getInstance().isSupportedType(type)) {
95            return true ;
96         }
97         return false;
98     }
99
100 }
Popular Tags