KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > action > DragNDrop


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/action/DragNDrop.java,v 1.4.2.2 2004/09/26 15:36:26 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.action;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.apache.jmeter.control.Controller;
26 import org.apache.jmeter.gui.GuiPackage;
27 import org.apache.jmeter.gui.tree.JMeterTreeListener;
28 import org.apache.jmeter.gui.tree.JMeterTreeNode;
29 import org.apache.jmeter.samplers.Sampler;
30 import org.apache.jmeter.testelement.TestElement;
31 import org.apache.jmeter.testelement.TestPlan;
32 import org.apache.jmeter.testelement.WorkBench;
33
34 /**
35  * @author mstover
36  * @version $Revision: 1.4.2.2 $
37  */

38 public class DragNDrop extends AbstractAction
39 {
40     public final static String JavaDoc ADD = "drag_n_drop.add";//$NON-NLS-1$
41
public final static String JavaDoc INSERT_BEFORE = "drag_n_drop.insert_before";//$NON-NLS-1$
42
public final static String JavaDoc INSERT_AFTER = "drag_n_drop.insert_after";//$NON-NLS-1$
43

44     private static Set JavaDoc commands = new HashSet JavaDoc();
45     static {
46         commands.add(ADD);
47         commands.add(INSERT_BEFORE);
48         commands.add(INSERT_AFTER);
49     }
50     /**
51      * @see Command#doAction(ActionEvent)
52      */

53     public void doAction(ActionEvent JavaDoc e)
54     {
55         String JavaDoc action = e.getActionCommand();
56         GuiPackage guiPackage = GuiPackage.getInstance();
57         JMeterTreeNode[] draggedNodes = guiPackage.getTreeListener().getDraggedNodes();
58         JMeterTreeListener treeListener = guiPackage.getTreeListener();
59         JMeterTreeNode currentNode = treeListener.getCurrentNode();
60         JMeterTreeNode parentNode =
61             (JMeterTreeNode) currentNode.getParent();
62         TestElement te = currentNode.getTestElement();
63         if (te instanceof TestPlan || te instanceof WorkBench)
64         {
65             parentNode=null; // So elements can only be added as children
66
}
67         //System.out.println(action+" "+te.getClass().getName());
68

69         if (ADD.equals(action) && canAddTo(currentNode))
70         {
71             removeNodesFromParents(draggedNodes);
72             for (int i = 0; i < draggedNodes.length; i++)
73             {
74                 GuiPackage.getInstance().getTreeModel().insertNodeInto(
75                     draggedNodes[i],
76                     currentNode,
77                     currentNode.getChildCount());
78             }
79         }
80         else if (INSERT_BEFORE.equals(action) && canAddTo(parentNode))
81         {
82             removeNodesFromParents(draggedNodes);
83             for (int i = 0; i < draggedNodes.length; i++)
84             {
85                 int index = parentNode.getIndex(currentNode);
86                 GuiPackage.getInstance().getTreeModel().insertNodeInto(
87                     draggedNodes[i],
88                     parentNode,
89                     index);
90             }
91         }
92         else if (INSERT_AFTER.equals(action) && canAddTo(parentNode))
93         {
94             removeNodesFromParents(draggedNodes);
95             for (int i = 0; i < draggedNodes.length; i++)
96             {
97                 int index = parentNode.getIndex(currentNode) + 1;
98                 GuiPackage.getInstance().getTreeModel().insertNodeInto(
99                     draggedNodes[i],
100                     parentNode,
101                     index);
102             }
103         }
104         GuiPackage.getInstance().getMainFrame().repaint();
105     }
106     
107     /**
108      * Determine whether or not dragged nodes can be added to this parent.
109      * Also used by Paste
110      * TODO tighten rules
111      * TODO move to MenuFactory?
112      * @param parentNode
113      * @return whether it is OK to add the dragged nodes to this parent
114      */

115     static boolean canAddTo(JMeterTreeNode parentNode) {
116         if (null==parentNode) return false;
117         TestElement te = parentNode.getTestElement();
118         //System.out.println("Add to: "+te.getClass().getName());
119
if (te instanceof Controller)
120             return true;
121         if (te instanceof Sampler)
122             return true;
123         if (te instanceof WorkBench)
124             return true;
125         if (te instanceof TestPlan)
126             return true;
127         return false;
128     }
129
130     protected void removeNodesFromParents(JMeterTreeNode[] nodes)
131     {
132         for (int i = 0; i < nodes.length; i++)
133         {
134             GuiPackage.getInstance().getTreeModel().removeNodeFromParent(nodes[i]);
135         }
136     }
137
138     /**
139      * @see Command#getActionNames()
140      */

141     public Set JavaDoc getActionNames()
142     {
143         return commands;
144     }
145 }
146
Popular Tags