001: //$Header$
002: /*
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */
019:
020: package org.apache.jmeter.report.gui.action;
021:
022: import java.awt.event.ActionEvent;
023: import java.util.HashSet;
024: import java.util.Set;
025:
026: import org.apache.jmeter.control.Controller;
027: import org.apache.jmeter.gui.ReportGuiPackage;
028: import org.apache.jmeter.report.gui.action.AbstractAction;
029: import org.apache.jmeter.report.gui.tree.ReportTreeListener;
030: import org.apache.jmeter.report.gui.tree.ReportTreeNode;
031: import org.apache.jmeter.samplers.Sampler;
032: import org.apache.jmeter.testelement.TestElement;
033: import org.apache.jmeter.testelement.TestPlan;
034: import org.apache.jmeter.testelement.WorkBench;
035:
036: /**
037: * @author Peter Lin
038: * @version $Revision: 571988 $
039: */
040: public class ReportDragNDrop extends AbstractAction {
041: public final static String ADD = "drag_n_drop.add";//$NON-NLS-1$
042:
043: public final static String INSERT_BEFORE = "drag_n_drop.insert_before";//$NON-NLS-1$
044:
045: public final static String INSERT_AFTER = "drag_n_drop.insert_after";//$NON-NLS-1$
046:
047: private static Set commands = new HashSet();
048: static {
049: commands.add(ADD);
050: commands.add(INSERT_BEFORE);
051: commands.add(INSERT_AFTER);
052: }
053:
054: /**
055: * @see org.apache.jmeter.gui.action.Command#doAction(ActionEvent)
056: */
057: public void doAction(ActionEvent e) {
058: String action = e.getActionCommand();
059: ReportGuiPackage guiPackage = ReportGuiPackage.getInstance();
060: ReportTreeNode[] draggedNodes = guiPackage.getTreeListener()
061: .getDraggedNodes();
062: ReportTreeListener treeListener = guiPackage.getTreeListener();
063: ReportTreeNode currentNode = treeListener.getCurrentNode();
064: ReportTreeNode parentNode = (ReportTreeNode) currentNode
065: .getParent();
066: TestElement te = currentNode.getTestElement();
067: if (te instanceof TestPlan || te instanceof WorkBench) {
068: parentNode = null; // So elements can only be added as children
069: }
070: // System.out.println(action+" "+te.getClass().getName());
071:
072: if (ADD.equals(action) && canAddTo(currentNode)) {
073: removeNodesFromParents(draggedNodes);
074: for (int i = 0; i < draggedNodes.length; i++) {
075: ReportGuiPackage.getInstance().getTreeModel()
076: .insertNodeInto(draggedNodes[i], currentNode,
077: currentNode.getChildCount());
078: }
079: } else if (INSERT_BEFORE.equals(action) && canAddTo(parentNode)) {
080: removeNodesFromParents(draggedNodes);
081: for (int i = 0; i < draggedNodes.length; i++) {
082: int index = parentNode.getIndex(currentNode);
083: ReportGuiPackage.getInstance().getTreeModel()
084: .insertNodeInto(draggedNodes[i], parentNode,
085: index);
086: }
087: } else if (INSERT_AFTER.equals(action) && canAddTo(parentNode)) {
088: removeNodesFromParents(draggedNodes);
089: for (int i = 0; i < draggedNodes.length; i++) {
090: int index = parentNode.getIndex(currentNode) + 1;
091: ReportGuiPackage.getInstance().getTreeModel()
092: .insertNodeInto(draggedNodes[i], parentNode,
093: index);
094: }
095: }
096: ReportGuiPackage.getInstance().getMainFrame().repaint();
097: }
098:
099: /**
100: * Determine whether or not dragged nodes can be added to this parent. Also
101: * used by Paste TODO tighten rules TODO move to MenuFactory?
102: *
103: * @param parentNode
104: * @return whether it is OK to add the dragged nodes to this parent
105: */
106: static boolean canAddTo(ReportTreeNode parentNode) {
107: if (null == parentNode)
108: return false;
109: TestElement te = parentNode.getTestElement();
110: // System.out.println("Add to: "+te.getClass().getName());
111: if (te instanceof Controller)
112: return true;
113: if (te instanceof Sampler)
114: return true;
115: if (te instanceof WorkBench)
116: return true;
117: if (te instanceof TestPlan)
118: return true;
119: return false;
120: }
121:
122: protected void removeNodesFromParents(ReportTreeNode[] nodes) {
123: for (int i = 0; i < nodes.length; i++) {
124: ReportGuiPackage.getInstance().getTreeModel()
125: .removeNodeFromParent(nodes[i]);
126: }
127: }
128:
129: /**
130: * @see org.apache.jmeter.gui.action.Command#getActionNames()
131: */
132: public Set getActionNames() {
133: return commands;
134: }
135: }
|