KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > DragDropUtilitiesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.explorer.view;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.StringSelection JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import org.netbeans.junit.MockServices;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.nodes.AbstractNode;
29 import org.openide.nodes.Children;
30 import org.openide.nodes.NodeTransfer;
31 import org.openide.util.datatransfer.ExClipboard;
32 import org.openide.util.datatransfer.ExTransferable;
33 import org.openide.util.datatransfer.MultiTransferObject;
34
35 /**
36  *
37  * @author Jaroslav Tulach
38  */

39 public class DragDropUtilitiesTest extends NbTestCase {
40     
41     public DragDropUtilitiesTest(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46         MockServices.setServices(new Class JavaDoc[] {MyClipboard.class});
47         last = null;
48     }
49     
50     public void testGetNodeTransferableForSingleNodeCopy() throws Exception JavaDoc {
51         N node = new N();
52         
53         Transferable JavaDoc t = DragDropUtilities.getNodeTransferable(node, NodeTransfer.DND_COPY);
54         
55         assertEquals("One call to copy", 1, node.copy);
56         assertEquals("Also one call to drag which delegates to copy", 1, node.drag);
57         assertEquals("No call to cut", 0, node.cut);
58         assertNotNull("Call to convertor", last);
59         assertTrue("StringSelection got to ExClipboard convertor", last instanceof StringSelection JavaDoc);
60     }
61     
62     public void testGetNodeTransferableForSingleNodeCut() throws Exception JavaDoc {
63         N node = new N();
64         
65         Transferable JavaDoc t = DragDropUtilities.getNodeTransferable(node, NodeTransfer.DND_MOVE);
66         
67         assertEquals("One call to cut", 1, node.cut);
68         assertEquals("No call to drag", 0, node.drag);
69         assertEquals("No call to copy", 0, node.copy);
70         assertNotNull("Call to convertor", last);
71         assertTrue("StringSelection got to ExClipboard convertor", last instanceof StringSelection JavaDoc);
72     }
73     
74     public void testMultiTransferableForCopy() throws Exception JavaDoc {
75         N node = new N();
76         N n2 = new N();
77         N[] arr = { node, n2 };
78         
79         Transferable JavaDoc t = DragDropUtilities.getNodeTransferable(arr, NodeTransfer.DND_COPY);
80         
81         assertEquals("One call to copy", 1, node.copy);
82         assertEquals("One call to copy on n2", 1, n2.copy);
83         assertEquals("Also one call to drag which delegates to copy", 1, node.drag);
84         assertEquals("Also one call to drag which delegates to copy on n2", 1, n2.drag);
85         assertEquals("No call to cut", 0, node.cut);
86         assertEquals("No call to cut", 0, n2.cut);
87         
88         assertNotNull("Call to convertor", last);
89         assertTrue("multi flavor supported", last.isDataFlavorSupported(ExTransferable.multiFlavor));
90         Object JavaDoc obj = last.getTransferData(ExTransferable.multiFlavor);
91         if (!( obj instanceof MultiTransferObject)) {
92             fail("It should be MultiTransferObject: " + obj);
93         }
94         MultiTransferObject m = (MultiTransferObject)obj;
95         
96         assertEquals("Two in multi", 2, m.getCount());
97         assertTrue("Is string", m.getTransferData(0, DataFlavor.stringFlavor) instanceof String JavaDoc);
98         assertTrue("Is string2", m.getTransferData(1, DataFlavor.stringFlavor) instanceof String JavaDoc);
99     }
100     
101     public void testMultiTransferableForCut() throws Exception JavaDoc {
102         N node = new N();
103         N n2 = new N();
104         N[] arr = { node, n2 };
105         
106         Transferable JavaDoc t = DragDropUtilities.getNodeTransferable(arr, NodeTransfer.DND_MOVE);
107         
108         assertEquals("One call to cut ", 1, node.cut);
109         assertEquals("One call to cut on n2", 1, n2.cut);
110         assertEquals("No to drag", 0, node.drag);
111         assertEquals("No to drag on n2", 0, n2.drag);
112         assertEquals("No call to copy", 0, node.copy);
113         assertEquals("No call to copy on n2", 0, n2.copy);
114         
115         assertNotNull("Call to convertor", last);
116         assertTrue("multi flavor supported", last.isDataFlavorSupported(ExTransferable.multiFlavor));
117         Object JavaDoc obj = last.getTransferData(ExTransferable.multiFlavor);
118         if (!( obj instanceof MultiTransferObject)) {
119             fail("It should be MultiTransferObject: " + obj);
120         }
121         MultiTransferObject m = (MultiTransferObject)obj;
122         
123         assertEquals("Two in multi", 2, m.getCount());
124         assertTrue("Is string", m.getTransferData(0, DataFlavor.stringFlavor) instanceof String JavaDoc);
125         assertTrue("Is string2", m.getTransferData(1, DataFlavor.stringFlavor) instanceof String JavaDoc);
126     }
127     
128     
129     
130     private static class N extends AbstractNode {
131         public int copy;
132         public int cut;
133         public int drag;
134         public Transferable JavaDoc ret = new StringSelection JavaDoc("A text");
135         
136         public N() {
137             super(Children.LEAF);
138         }
139         
140         public Transferable JavaDoc clipboardCut() throws IOException JavaDoc {
141             cut++;
142             return ret;
143         }
144         
145         public Transferable JavaDoc clipboardCopy() throws IOException JavaDoc {
146             copy++;
147             return ret;
148         }
149         
150         public Transferable JavaDoc drag() throws IOException JavaDoc {
151             drag++;
152             return super.drag();
153         }
154     }
155     
156     public static Transferable JavaDoc last;
157     
158     public static final class MyClipboard extends ExClipboard {
159         
160         public MyClipboard() {
161             super("Empty");
162         }
163         
164         public ExClipboard.Convertor[] getConvertors() {
165             return new ExClipboard.Convertor[] {new ExClipboard.Convertor() {
166                 public Transferable JavaDoc convert(Transferable JavaDoc t) {
167                     last = t;
168                     return t;
169                 }
170             }};
171         }
172     }
173 }
174
Popular Tags