01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.Toolkit;
22: import java.awt.event.ActionEvent;
23: import java.util.HashSet;
24: import java.util.Set;
25:
26: import org.apache.jmeter.exceptions.IllegalUserActionException;
27: import org.apache.jmeter.gui.GuiPackage;
28: import org.apache.jmeter.gui.tree.JMeterTreeListener;
29: import org.apache.jmeter.gui.tree.JMeterTreeNode;
30: import org.apache.jmeter.gui.util.MenuFactory;
31: import org.apache.jmeter.util.JMeterUtils;
32: import org.apache.jorphan.logging.LoggingManager;
33: import org.apache.log.Logger;
34:
35: /**
36: * Places a copied JMeterTreeNode under the selected node.
37: *
38: */
39: public class Paste extends AbstractAction {
40:
41: private static final Logger log = LoggingManager
42: .getLoggerForClass();
43:
44: private static Set commands = new HashSet();
45: static {
46: commands.add(ActionNames.PASTE);
47: }
48:
49: /**
50: * @see Command#getActionNames()
51: */
52: public Set getActionNames() {
53: return commands;
54: }
55:
56: /**
57: * @see Command#doAction(ActionEvent)
58: */
59: public void doAction(ActionEvent e) {
60: JMeterTreeNode draggedNodes[] = Copy.getCopiedNodes();
61: JMeterTreeListener treeListener = GuiPackage.getInstance()
62: .getTreeListener();
63: JMeterTreeNode currentNode = treeListener.getCurrentNode();
64: if (MenuFactory.canAddTo(currentNode, draggedNodes)) {
65: for (int i = 0; i < draggedNodes.length; i++) {
66: if (draggedNodes[i] != null) {
67: addNode(currentNode, draggedNodes[i]);
68: }
69: }
70: } else {
71: Toolkit.getDefaultToolkit().beep();
72: }
73: GuiPackage.getInstance().getMainFrame().repaint();
74: }
75:
76: private void addNode(JMeterTreeNode parent, JMeterTreeNode node) {
77: try {
78: // Add this node
79: JMeterTreeNode newNode = GuiPackage.getInstance()
80: .getTreeModel().addComponent(node.getTestElement(),
81: parent);
82: // Add all the child nodes of the node we are adding
83: for (int i = 0; i < node.getChildCount(); i++) {
84: addNode(newNode, (JMeterTreeNode) node.getChildAt(i));
85: }
86: } catch (IllegalUserActionException iuae) {
87: log.error("", iuae); // $NON-NLS-1$
88: JMeterUtils.reportErrorToUser(iuae.getMessage());
89: }
90: }
91: }
|