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: /*
020: * Created on Apr 9, 2003
021: *
022: * Clones a JMeterTreeNode
023: */
024: package org.apache.jmeter.gui.action;
025:
026: import java.awt.event.ActionEvent;
027: import java.util.Enumeration;
028: import java.util.HashSet;
029: import java.util.Set;
030:
031: import org.apache.jmeter.gui.GuiPackage;
032: import org.apache.jmeter.gui.tree.JMeterTreeListener;
033: import org.apache.jmeter.gui.tree.JMeterTreeNode;
034: import org.apache.jmeter.testelement.TestElement;
035:
036: /**
037: * @author Thad Smith
038: * @version $Revision: 493779 $
039: */
040: public class Copy extends AbstractAction {
041: private static JMeterTreeNode copiedNode = null;
042:
043: private static JMeterTreeNode copiedNodes[] = null;
044:
045: private static HashSet commands = new HashSet();
046:
047: static {
048: commands.add(ActionNames.COPY);
049: }
050:
051: /*
052: * @see org.apache.jmeter.gui.action.Command#getActionNames()
053: */
054: public Set getActionNames() {
055: return commands;
056: }
057:
058: public void doAction(ActionEvent e) {
059: JMeterTreeListener treeListener = GuiPackage.getInstance()
060: .getTreeListener();
061: JMeterTreeNode[] nodes = treeListener.getSelectedNodes();
062: setCopiedNodes(nodes);
063: }
064:
065: public static JMeterTreeNode[] getCopiedNodes() {
066: for (int i = 0; i < copiedNodes.length; i++) {
067: if (copiedNodes[i] == null) {
068: return null;
069: }
070: }
071: return cloneTreeNodes(copiedNodes);
072: }
073:
074: public static JMeterTreeNode getCopiedNode() {
075: if (copiedNode == null) {
076: return null;
077: }
078: return cloneTreeNode(copiedNode);
079: }
080:
081: public static void setCopiedNode(JMeterTreeNode node) {
082: copiedNode = cloneTreeNode(node);
083: }
084:
085: public static JMeterTreeNode cloneTreeNode(JMeterTreeNode node) {
086: JMeterTreeNode treeNode = (JMeterTreeNode) node.clone();
087: treeNode.setUserObject(((TestElement) node.getUserObject())
088: .clone());
089: cloneChildren(treeNode, node);
090: return treeNode;
091: }
092:
093: public static void setCopiedNodes(JMeterTreeNode nodes[]) {
094: copiedNodes = new JMeterTreeNode[nodes.length];
095: for (int i = 0; i < nodes.length; i++) {
096: copiedNodes[i] = cloneTreeNode(nodes[i]);
097: }
098: }
099:
100: public static JMeterTreeNode[] cloneTreeNodes(
101: JMeterTreeNode nodes[]) {
102: JMeterTreeNode treeNodes[] = new JMeterTreeNode[nodes.length];
103: for (int i = 0; i < nodes.length; i++) {
104: treeNodes[i] = cloneTreeNode(nodes[i]);
105: }
106: return treeNodes;
107: }
108:
109: private static void cloneChildren(JMeterTreeNode to,
110: JMeterTreeNode from) {
111: Enumeration enumFrom = from.children();
112: while (enumFrom.hasMoreElements()) {
113: JMeterTreeNode child = (JMeterTreeNode) enumFrom
114: .nextElement();
115: JMeterTreeNode childClone = (JMeterTreeNode) child.clone();
116: childClone.setUserObject(((TestElement) child
117: .getUserObject()).clone());
118: to.add(childClone);
119: cloneChildren((JMeterTreeNode) to.getLastChild(), child);
120: }
121: }
122: }
|