Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.openide.explorer.view; |
15 |
|
16 |
import java.awt.datatransfer.DataFlavor; |
17 |
import java.awt.datatransfer.StringSelection; |
18 |
import junit.framework.*; |
19 |
|
20 |
import java.awt.*; |
21 |
import java.awt.datatransfer.Clipboard; |
22 |
import java.awt.datatransfer.Transferable; |
23 |
import java.awt.datatransfer.UnsupportedFlavorException; |
24 |
import java.awt.dnd.DnDConstants; |
25 |
import java.awt.dnd.DragSource; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.awt.image.BufferedImage; |
29 |
import java.awt.image.ColorModel; |
30 |
import java.io.IOException; |
31 |
import java.util.Arrays; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
import java.util.ArrayList; |
35 |
import java.util.TreeSet; |
36 |
import javax.swing.JMenuItem; |
37 |
import javax.swing.JPopupMenu; |
38 |
import javax.swing.SwingUtilities; |
39 |
|
40 |
import org.openide.DialogDisplayer; |
41 |
import org.openide.ErrorManager; |
42 |
import org.openide.NotifyDescriptor; |
43 |
import org.openide.NotifyDescriptor.Message; |
44 |
import org.openide.awt.JPopupMenuPlus; |
45 |
import org.openide.nodes.Node; |
46 |
import org.openide.util.Lookup; |
47 |
import org.openide.util.NbBundle; |
48 |
import org.openide.util.UserCancelException; |
49 |
import org.openide.util.Utilities; |
50 |
import org.openide.util.datatransfer.ExClipboard; |
51 |
import org.openide.util.datatransfer.ExTransferable; |
52 |
import org.openide.util.datatransfer.MultiTransferObject; |
53 |
import org.openide.util.datatransfer.PasteType; |
54 |
import org.openide.util.datatransfer.ExTransferable.Multi; |
55 |
|
56 |
/** |
57 |
* |
58 |
* @author Jaroslav Tulach |
59 |
*/ |
60 |
public class DragDropUtilitiesTest extends org.netbeans.junit.NbTestCase { |
61 |
static { |
62 |
System.setProperty ("org.openide.util.Lookup", "org.openide.explorer.view.DragDropUtilitiesTest$Lkp"); |
63 |
} |
64 |
|
65 |
|
66 |
private Lkp lookup; |
67 |
|
68 |
public DragDropUtilitiesTest (String testName) { |
69 |
super (testName); |
70 |
} |
71 |
|
72 |
protected void setUp () throws Exception { |
73 |
Lookup l = Lookup.getDefault (); |
74 |
assertEquals (Lkp.class, l.getClass ()); |
75 |
lookup = (Lkp)l; |
76 |
lookup.last = null; |
77 |
} |
78 |
|
79 |
protected void tearDown () throws Exception { |
80 |
} |
81 |
|
82 |
public void testGetNodeTransferableForSingleNodeCopy () throws Exception { |
83 |
N node = new N (); |
84 |
|
85 |
Transferable t = DragDropUtilities.getNodeTransferable (node, org.openide.nodes.NodeTransfer.DND_COPY); |
86 |
|
87 |
assertEquals ("One call to copy", 1, node.copy); |
88 |
assertEquals ("Also one call to drag which delegates to copy", 1, node.drag); |
89 |
assertEquals ("No call to cut", 0, node.cut); |
90 |
assertNotNull ("Call to convertor", lookup.last); |
91 |
assertTrue ("StringSelection got to ExClipboard convertor", lookup.last instanceof StringSelection); |
92 |
} |
93 |
|
94 |
public void testGetNodeTransferableForSingleNodeCut () throws Exception { |
95 |
N node = new N (); |
96 |
|
97 |
Transferable t = DragDropUtilities.getNodeTransferable (node, org.openide.nodes.NodeTransfer.DND_MOVE); |
98 |
|
99 |
assertEquals ("One call to cut", 1, node.cut); |
100 |
assertEquals ("No call to drag", 0, node.drag); |
101 |
assertEquals ("No call to copy", 0, node.copy); |
102 |
assertNotNull ("Call to convertor", lookup.last); |
103 |
assertTrue ("StringSelection got to ExClipboard convertor", lookup.last instanceof StringSelection); |
104 |
} |
105 |
|
106 |
public void testMultiTransferableForCopy () throws Exception { |
107 |
N node = new N (); |
108 |
N n2 = new N (); |
109 |
N[] arr = { node, n2 }; |
110 |
|
111 |
Transferable t = DragDropUtilities.getNodeTransferable (arr, org.openide.nodes.NodeTransfer.DND_COPY); |
112 |
|
113 |
assertEquals ("One call to copy", 1, node.copy); |
114 |
assertEquals ("One call to copy on n2", 1, n2.copy); |
115 |
assertEquals ("Also one call to drag which delegates to copy", 1, node.drag); |
116 |
assertEquals ("Also one call to drag which delegates to copy on n2", 1, n2.drag); |
117 |
assertEquals ("No call to cut", 0, node.cut); |
118 |
assertEquals ("No call to cut", 0, n2.cut); |
119 |
|
120 |
assertNotNull ("Call to convertor", lookup.last); |
121 |
assertTrue ("multi flavor supported", lookup.last.isDataFlavorSupported (ExTransferable.multiFlavor)); |
122 |
Object obj = lookup.last.getTransferData (ExTransferable.multiFlavor); |
123 |
if (!( obj instanceof MultiTransferObject)) { |
124 |
fail ("It should be MultiTransferObject: " + obj); |
125 |
} |
126 |
MultiTransferObject m = (MultiTransferObject)obj; |
127 |
|
128 |
assertEquals ("Two in multi", 2, m.getCount ()); |
129 |
assertTrue ("Is string", m.getTransferData (0, DataFlavor.stringFlavor) instanceof String); |
130 |
assertTrue ("Is string2", m.getTransferData (1, DataFlavor.stringFlavor) instanceof String); |
131 |
} |
132 |
|
133 |
public void testMultiTransferableForCut () throws Exception { |
134 |
N node = new N (); |
135 |
N n2 = new N (); |
136 |
N[] arr = { node, n2 }; |
137 |
|
138 |
Transferable t = DragDropUtilities.getNodeTransferable (arr, org.openide.nodes.NodeTransfer.DND_MOVE); |
139 |
|
140 |
assertEquals ("One call to cut ", 1, node.cut); |
141 |
assertEquals ("One call to cut on n2", 1, n2.cut); |
142 |
assertEquals ("No to drag", 0, node.drag); |
143 |
assertEquals ("No to drag on n2", 0, n2.drag); |
144 |
assertEquals ("No call to copy", 0, node.copy); |
145 |
assertEquals ("No call to copy on n2", 0, n2.copy); |
146 |
|
147 |
assertNotNull ("Call to convertor", lookup.last); |
148 |
assertTrue ("multi flavor supported", lookup.last.isDataFlavorSupported (ExTransferable.multiFlavor)); |
149 |
Object obj = lookup.last.getTransferData (ExTransferable.multiFlavor); |
150 |
if (!( obj instanceof MultiTransferObject)) { |
151 |
fail ("It should be MultiTransferObject: " + obj); |
152 |
} |
153 |
MultiTransferObject m = (MultiTransferObject)obj; |
154 |
|
155 |
assertEquals ("Two in multi", 2, m.getCount ()); |
156 |
assertTrue ("Is string", m.getTransferData (0, DataFlavor.stringFlavor) instanceof String); |
157 |
assertTrue ("Is string2", m.getTransferData (1, DataFlavor.stringFlavor) instanceof String); |
158 |
} |
159 |
|
160 |
|
161 |
|
162 |
private static class N extends org.openide.nodes.AbstractNode { |
163 |
public int copy; |
164 |
public int cut; |
165 |
public int drag; |
166 |
public Transferable ret = new StringSelection ("A text"); |
167 |
|
168 |
public N () { |
169 |
super (org.openide.nodes.Children.LEAF); |
170 |
} |
171 |
|
172 |
public Transferable clipboardCut () throws IOException { |
173 |
cut++; |
174 |
return ret; |
175 |
} |
176 |
|
177 |
public Transferable clipboardCopy () throws IOException { |
178 |
copy++; |
179 |
return ret; |
180 |
} |
181 |
|
182 |
public Transferable drag () throws IOException { |
183 |
drag++; |
184 |
return super.drag (); |
185 |
} |
186 |
} |
187 |
|
188 |
public static final class Lkp extends org.openide.util.lookup.AbstractLookup |
189 |
implements ExClipboard.Convertor { |
190 |
public Transferable last; |
191 |
|
192 |
public Lkp () { |
193 |
this (new org.openide.util.lookup.InstanceContent ()); |
194 |
} |
195 |
|
196 |
protected Lkp (org.openide.util.lookup.InstanceContent ic) { |
197 |
super (ic); |
198 |
ic.add (new MyClipboard (this)); |
199 |
} |
200 |
|
201 |
public Transferable convert (Transferable t) { |
202 |
last = t; |
203 |
return t; |
204 |
} |
205 |
} |
206 |
|
207 |
private static final class MyClipboard extends ExClipboard { |
208 |
private Lkp lookup; |
209 |
|
210 |
public MyClipboard (Lkp lookup) { |
211 |
super ("Empty"); |
212 |
this.lookup = lookup; |
213 |
} |
214 |
|
215 |
public Convertor[] getConvertors () { |
216 |
return new Convertor[] { lookup }; |
217 |
} |
218 |
} |
219 |
} |