001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.gui.action;
020:
021: import java.awt.Toolkit;
022: import java.awt.event.ActionEvent;
023: import java.util.HashSet;
024: import java.util.Set;
025:
026: import org.apache.jmeter.gui.GuiPackage;
027: import org.apache.jmeter.gui.tree.JMeterTreeListener;
028: import org.apache.jmeter.gui.tree.JMeterTreeNode;
029: import org.apache.jmeter.gui.util.MenuFactory;
030: import org.apache.jmeter.testelement.TestElement;
031: import org.apache.jmeter.testelement.TestPlan;
032: import org.apache.jmeter.testelement.WorkBench;
033:
034: public class DragNDrop extends AbstractAction {
035: private static Set commands = new HashSet();
036: static {
037: commands.add(ActionNames.DRAG_ADD);
038: commands.add(ActionNames.INSERT_BEFORE);
039: commands.add(ActionNames.INSERT_AFTER);
040: }
041:
042: /**
043: * @see Command#doAction(ActionEvent)
044: */
045: public void doAction(ActionEvent e) {
046: String action = e.getActionCommand();
047: GuiPackage guiPackage = GuiPackage.getInstance();
048: JMeterTreeNode[] draggedNodes = guiPackage.getTreeListener()
049: .getDraggedNodes();
050: JMeterTreeListener treeListener = guiPackage.getTreeListener();
051: JMeterTreeNode currentNode = treeListener.getCurrentNode();
052: JMeterTreeNode parentNode = (JMeterTreeNode) currentNode
053: .getParent();
054: TestElement te = currentNode.getTestElement();
055: if (te instanceof TestPlan || te instanceof WorkBench) {
056: parentNode = null; // So elements can only be added as children
057: }
058: // System.out.println(action+" "+te.getClass().getName());
059:
060: if (ActionNames.DRAG_ADD.equals(action)
061: && canAddTo(currentNode, draggedNodes)) {
062: removeNodesFromParents(draggedNodes);
063: for (int i = 0; i < draggedNodes.length; i++) {
064: GuiPackage.getInstance().getTreeModel().insertNodeInto(
065: draggedNodes[i], currentNode,
066: currentNode.getChildCount());
067: }
068: } else if (ActionNames.INSERT_BEFORE.equals(action)
069: && canAddTo(parentNode, draggedNodes)) {
070: removeNodesFromParents(draggedNodes);
071: for (int i = 0; i < draggedNodes.length; i++) {
072: int index = parentNode.getIndex(currentNode);
073: GuiPackage.getInstance().getTreeModel().insertNodeInto(
074: draggedNodes[i], parentNode, index);
075: }
076: } else if (ActionNames.INSERT_AFTER.equals(action)
077: && canAddTo(parentNode, draggedNodes)) {
078: removeNodesFromParents(draggedNodes);
079: for (int i = 0; i < draggedNodes.length; i++) {
080: int index = parentNode.getIndex(currentNode) + 1;
081: GuiPackage.getInstance().getTreeModel().insertNodeInto(
082: draggedNodes[i], parentNode, index);
083: }
084: }
085: GuiPackage.getInstance().getMainFrame().repaint();
086: }
087:
088: private static boolean canAddTo(JMeterTreeNode parentNode,
089: JMeterTreeNode[] draggedNodes) {
090: boolean ok = MenuFactory.canAddTo(parentNode, draggedNodes);
091: if (!ok) {
092: Toolkit.getDefaultToolkit().beep();
093: }
094: return ok;
095: }
096:
097: protected void removeNodesFromParents(JMeterTreeNode[] nodes) {
098: for (int i = 0; i < nodes.length; i++) {
099: GuiPackage.getInstance().getTreeModel()
100: .removeNodeFromParent(nodes[i]);
101: }
102: }
103:
104: /**
105: * @see Command#getActionNames()
106: */
107: public Set getActionNames() {
108: return commands;
109: }
110: }
|