/*
 * Created on Apr 9, 2003
 *
 * Clones a JMeterTreeNode
 */
package org.apache.jmeter.gui.action;

import java.awt.event.ActionEvent;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.tree.JMeterTreeListener;
import org.apache.jmeter.gui.tree.JMeterTreeNode;

/**
 * @author Thad Smith
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class Copy extends AbstractAction {
	
	private static JMeterTreeNode copiedNode = null;
	
	private static String COPY = "Copy";
	private static HashSet commands = new HashSet();
	static {
		commands.add(COPY);
	}
	
	/*
	 * @see org.apache.jmeter.gui.action.Command#getActionNames()
	 */
	public Set getActionNames() {
		return commands;
	}

	public void doAction(ActionEvent e) 
	{
		String action = e.getActionCommand();
		JMeterTreeNode draggedNode = GuiPackage.getInstance().getTreeListener().getDraggedNode();
		JMeterTreeListener treeListener = GuiPackage.getInstance().getTreeListener();
		setCopiedNode( (JMeterTreeNode)treeListener.getCurrentNode() );
	}
	
	public static JMeterTreeNode getCopiedNode() 
	{
		return cloneTreeNode(copiedNode);
	}

	public static void setCopiedNode(JMeterTreeNode node) {
		copiedNode = cloneTreeNode(node);			
	}
	
	public static JMeterTreeNode cloneTreeNode(JMeterTreeNode node)
	{
		JMeterTreeNode treeNode = (JMeterTreeNode)node.clone();
		cloneChildren(treeNode,node);
		return treeNode;
	}

	private static void cloneChildren(JMeterTreeNode to, JMeterTreeNode from) 
	{
		Enumeration enum = from.children();
		while (enum.hasMoreElements()) 
		{
			JMeterTreeNode child = (JMeterTreeNode)enum.nextElement();
			to.add((JMeterTreeNode)child.clone());	
			cloneChildren((JMeterTreeNode)to.getLastChild(),child);
		}
	}

}

@@ -353,16 +353,66 @@
 reeNode;
+%0Aimport org.apache.jmeter.testelement.TestElement;
 %0A%0A/**%0A *
@@ -1552,16 +1552,87 @@
 lone();%0A
+%09%09treeNode.setUserObject(((TestElement)node.getUserObject()).clone());%0A
 %09%09cloneC
@@ -1902,15 +1902,36 @@
 %0A%09%09%09
-to.add(
+JMeterTreeNode childClone = 
 (JMe
@@ -1955,16 +1955,113 @@
 .clone()
+;%0A%09%09%09childClone.setUserObject(((TestElement)child.getUserObject()).clone());%0A%09%09%09to.add(childClone
 );%09%0A%09%09%09c

Fix for Copy PR: Obtained from: Submitted by: Reviewed by: CVS: ---------------------------------------------------------------------- CVS: PR: CVS: If this change addresses a PR in the problem report tracking CVS: database, then enter the PR number(s) here. CVS: Obtained from: CVS: If this change has been taken from another system, such as NCSA, CVS: then name the system in this line, otherwise delete it. CVS: Submitted by: CVS: If this code has been contributed to Apache by someone else; i.e., CVS: they sent us a patch or a new module, then include their name/email CVS: address here. If this is your work then delete this line. CVS: Reviewed by: CVS: If we are doing pre-commit code reviews and someone else has CVS: reviewed your changes, include their name(s) here. CVS: If you have not had it reviewed then delete this line.