1 18 19 package org.apache.jmeter.gui.action; 20 21 import java.awt.event.ActionEvent ; 22 import java.util.HashSet ; 23 import java.util.Set ; 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 38 public class DragNDrop extends AbstractAction 39 { 40 public final static String ADD = "drag_n_drop.add"; public final static String INSERT_BEFORE = "drag_n_drop.insert_before"; public final static String INSERT_AFTER = "drag_n_drop.insert_after"; 44 private static Set commands = new HashSet (); 45 static { 46 commands.add(ADD); 47 commands.add(INSERT_BEFORE); 48 commands.add(INSERT_AFTER); 49 } 50 53 public void doAction(ActionEvent e) 54 { 55 String 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; } 67 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 115 static boolean canAddTo(JMeterTreeNode parentNode) { 116 if (null==parentNode) return false; 117 TestElement te = parentNode.getTestElement(); 118 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 141 public Set getActionNames() 142 { 143 return commands; 144 } 145 } 146 | Popular Tags |