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