View | Details | Raw Unified | Return to bug 52176
Collapse All | Expand All

(-)src/core/org/apache/jmeter/gui/action/Copy.java (-12 / +21 lines)
Lines 23-46 Link Here
23
 */
23
 */
24
package org.apache.jmeter.gui.action;
24
package org.apache.jmeter.gui.action;
25
25
26
import java.awt.Toolkit;
27
import java.awt.datatransfer.Clipboard;
26
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionEvent;
27
import java.util.Enumeration;
29
import java.util.Enumeration;
28
import java.util.HashSet;
30
import java.util.HashSet;
29
import java.util.Set;
31
import java.util.Set;
30
32
33
import javax.swing.JOptionPane;
34
31
import org.apache.jmeter.gui.GuiPackage;
35
import org.apache.jmeter.gui.GuiPackage;
32
import org.apache.jmeter.gui.tree.JMeterTreeListener;
36
import org.apache.jmeter.gui.tree.JMeterTreeListener;
33
import org.apache.jmeter.gui.tree.JMeterTreeNode;
37
import org.apache.jmeter.gui.tree.JMeterTreeNode;
34
import org.apache.jmeter.testelement.TestElement;
38
import org.apache.jmeter.testelement.TestElement;
39
import org.apache.jmeter.util.JMeterTreeNodeTransferable;
40
import org.apache.jmeter.util.JMeterUtils;
35
41
36
/**
42
/**
37
 * Implements the Copy menu command
43
 * Implements the Copy menu command
38
 */
44
 */
39
public class Copy extends AbstractAction {
45
public class Copy extends AbstractAction {
46
    
40
    private static JMeterTreeNode copiedNode = null;
47
    private static JMeterTreeNode copiedNode = null;
41
48
42
    private static JMeterTreeNode copiedNodes[] = null;
43
44
    private static final HashSet<String> commands = new HashSet<String>();
49
    private static final HashSet<String> commands = new HashSet<String>();
45
50
46
    static {
51
    static {
Lines 63-77 Link Here
63
    }
68
    }
64
69
65
    public static JMeterTreeNode[] getCopiedNodes() {
70
    public static JMeterTreeNode[] getCopiedNodes() {
66
        if (copiedNodes == null) { // can be null if Copy has yet to be used
71
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
67
            return null;
72
        if(clipboard.isDataFlavorAvailable(JMeterTreeNodeTransferable.JMETER_TREE_NODE_ARRAY_DATA_FLAVOR)) {
68
        }
73
            try {
69
        for (int i = 0; i < copiedNodes.length; i++) {
74
                return (JMeterTreeNode[]) clipboard.getData(JMeterTreeNodeTransferable.JMETER_TREE_NODE_ARRAY_DATA_FLAVOR);
70
            if (copiedNodes[i] == null) {
75
            } catch (Exception ex) {
71
                return null;
76
                JOptionPane.showMessageDialog(GuiPackage.getInstance().getMainFrame(), JMeterUtils.getResString("clipboard_node_read_error")+":\n" + ex.getLocalizedMessage(), JMeterUtils.getResString("error_title"), JOptionPane.ERROR_MESSAGE);
72
            }
77
            }
73
        }
78
        }
74
        return cloneTreeNodes(copiedNodes);
79
        return null;
75
    }
80
    }
76
81
77
    public static JMeterTreeNode getCopiedNode() {
82
    public static JMeterTreeNode getCopiedNode() {
Lines 93-101 Link Here
93
    }
98
    }
94
99
95
    public static void setCopiedNodes(JMeterTreeNode nodes[]) {
100
    public static void setCopiedNodes(JMeterTreeNode nodes[]) {
96
        copiedNodes = new JMeterTreeNode[nodes.length];
101
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
97
        for (int i = 0; i < nodes.length; i++) {
102
        try {
98
            copiedNodes[i] = cloneTreeNode(nodes[i]);
103
            JMeterTreeNodeTransferable transferable = new JMeterTreeNodeTransferable();
104
            transferable.setTransferData(nodes);
105
            clipboard.setContents(transferable, null);
106
        } catch (Exception ex) {
107
            JOptionPane.showMessageDialog(GuiPackage.getInstance().getMainFrame(), JMeterUtils.getResString("clipboard_node_read_error")+":\n" + ex.getLocalizedMessage(), JMeterUtils.getResString("error_title"), JOptionPane.ERROR_MESSAGE);
99
        }
108
        }
100
    }
109
    }
101
110
(-)src/core/org/apache/jmeter/resources/messages.properties (+2 lines)
Lines 125-130 Link Here
125
clear_all=Clear All
125
clear_all=Clear All
126
clear_cache_per_iter=Clear cache each iteration?
126
clear_cache_per_iter=Clear cache each iteration?
127
clear_cookies_per_iter=Clear cookies each iteration?
127
clear_cookies_per_iter=Clear cookies each iteration?
128
clipboard_node_read_error=Error retrieving nodes from clipboard
129
clipboard_node_write_error=Error coping nodes to clipboard
128
column_delete_disallowed=Deleting this column is not permitted
130
column_delete_disallowed=Deleting this column is not permitted
129
column_number=Column number of CSV file | next | *alias
131
column_number=Column number of CSV file | next | *alias
130
compare=Compare
132
compare=Compare
(-)src/core/org/apache/jmeter/util/JMeterTreeNodeTransferable.java (+68 lines)
Line 0 Link Here
1
package org.apache.jmeter.util;
2
3
import java.awt.datatransfer.DataFlavor;
4
import java.awt.datatransfer.Transferable;
5
import java.awt.datatransfer.UnsupportedFlavorException;
6
import java.io.ByteArrayInputStream;
7
import java.io.ByteArrayOutputStream;
8
import java.io.IOException;
9
import java.io.ObjectInput;
10
import java.io.ObjectInputStream;
11
import java.io.ObjectOutputStream;
12
13
import org.apache.jmeter.gui.tree.JMeterTreeNode;
14
15
/**
16
 * Simple implementation of a transferable for {@link JMeterTreeNode} arrays based on serialization.
17
 */
18
public class JMeterTreeNodeTransferable implements Transferable {
19
    
20
    private static final long serialVersionUID = 192L;
21
    
22
    public final static DataFlavor JMETER_TREE_NODE_ARRAY_DATA_FLAVOR = new DataFlavor(JMeterTreeNode[].class, JMeterTreeNode[].class.getName());
23
    
24
    private final static DataFlavor[] DATA_FLAVORS = new DataFlavor[]{JMETER_TREE_NODE_ARRAY_DATA_FLAVOR};
25
    
26
    private byte[] data = null;
27
28
    @Override
29
    public DataFlavor[] getTransferDataFlavors() {
30
        return DATA_FLAVORS;
31
    }
32
33
    @Override
34
    public boolean isDataFlavorSupported(DataFlavor flavor) {
35
        return flavor.match(JMETER_TREE_NODE_ARRAY_DATA_FLAVOR);
36
    }
37
38
    @Override
39
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
40
        if(!isDataFlavorSupported(flavor)) {
41
            throw new UnsupportedFlavorException(flavor);
42
        }
43
        if(data != null) {
44
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
45
            ObjectInput ois = new ObjectInputStream(bis);
46
            try {
47
                JMeterTreeNode[] nodes = (JMeterTreeNode[]) ois.readObject();
48
                return nodes;
49
            } catch (ClassNotFoundException cnfe) {
50
                throw new IOException("Failed to read object stream.", cnfe);
51
            } finally {
52
                ois.close();
53
                bis.close();
54
            }
55
        }
56
        return null;
57
    }
58
    
59
    public void setTransferData(JMeterTreeNode[] nodes) throws IOException {
60
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
61
        ObjectOutputStream oos = new ObjectOutputStream(bos);
62
        oos.writeObject(nodes);
63
        oos.close();
64
        bos.close();
65
        data = bos.toByteArray();
66
    }
67
68
}

Return to bug 52176