KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
21  *
22  */

23
24 package org.openide.explorer.view;
25
26 import java.lang.ref.*;
27 import java.awt.event.*;
28 import java.awt.*;
29 import java.beans.PropertyVetoException JavaDoc;
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.*;
33 import javax.swing.JList JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import javax.swing.tree.TreeSelectionModel JavaDoc;
36
37 import org.openide.*;
38 import org.openide.explorer.*;
39 import org.openide.nodes.*;
40 import org.openide.util.*;
41 import junit.framework.*;
42 import junit.textui.TestRunner;
43 import org.netbeans.junit.*;
44
45 /**
46  * Tests for control selection mode on TreeView (test on BeanTreeView).
47  * Note: here are used TreeView's method which hasn't been introduced yet,
48  * will be introduced when the enh 11928 will be implemented.
49  * Run test with attached patch in issue 11928.
50  *
51  * @author Jiri Rechtacek
52  * @see "#11928"
53  */

54 public class SelectionModeTest extends NbTestCase {
55     
56     ExplorerManager mgr;
57     TreeView tree;
58     Node[] singleSelection, contiguousSelection, discontiguousSelection;
59     
60     public SelectionModeTest(String JavaDoc name) {
61         super(name);
62     }
63    
64     public static void main (String JavaDoc args[]) {
65         TestRunner.run (new NbTestSuite (SelectionModeTest.class));
66         System.exit(0);
67     }
68
69
70     protected boolean runInEQ() {
71         return true;
72     }
73     
74     /** Create tree and a selection of nodes for test.
75      */

76     protected void setUp () {
77         // disable any lookup, to isolate the test from other registered
78
// subsystems like core/windows
79
System.setProperty ("org.openide.util.Lookup", "-");
80         
81         // create tree:
82
// root +--- parent_one +--- one1
83
// |--- one2
84
// |--- parent_two +--- two1
85
// |--- leaf
86

87         final Children parents = new Children.Array ();
88         Node root = new AbstractNode (parents);
89         root.setName ("root");
90         
91         tree = new BeanTreeView ();
92         //tree = new ContextTreeView ();
93

94         final ExplorerPanel p = new ExplorerPanel ();
95         p.setName ("SelectionModeTest");
96         
97         p.add (tree, BorderLayout.CENTER);
98         p.getExplorerManager ().setRootContext (root);
99         p.open ();
100
101         final Children ones = new Children.Array ();
102         Node parent_one = new AbstractNode (ones);
103         parent_one.setName ("parent_one");
104         final Children twos = new Children.Array ();
105         Node parent_two = new AbstractNode (twos);
106         parent_two.setName ("parent_two");
107
108         final Node one1 = new AbstractNode (Children.LEAF);
109         one1.setName("one1");
110
111         final Node one2 = new AbstractNode (Children.LEAF);
112         one2.setName("one2");
113
114         ones.add(new Node[] { one1, one2 });
115         
116         final Node two1 = new AbstractNode (Children.LEAF);
117         two1.setName("two1");
118
119         twos.add (new Node[] { two1 });
120         
121         parents.add (new Node[] { parent_one, parent_two });
122         
123         
124         // the test selections
125
singleSelection = new Node[] {parent_two};
126         contiguousSelection = new Node[] {one1, one2};
127         discontiguousSelection = new Node[] {one2, two1};
128         
129         mgr = p.getExplorerManager();
130     }
131
132     /** Test set all nodes selections if the mode SINGLE_TREE_SELECTION is set.
133      * @throws Exception */

134     public void testSingleSelectionMode () throws Exception JavaDoc {
135         // try setSelectionMode; if not present then fail
136
setSelectionMode (tree, TreeSelectionModel.SINGLE_TREE_SELECTION);
137         PropertyVetoException JavaDoc exp = null;
138         
139         // single
140
try {
141             // have to be equal
142
assertTrue ("[MODE: SINGLE_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
143
trySelection (mgr, singleSelection));
144         } catch (PropertyVetoException JavaDoc e) {
145             // no exp should be thrown
146
fail ("[MODE: SINGLE_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); // NO18N
147
}
148         
149         // contiguous
150
try {
151             exp = null;
152             // cont' be equal
153
assertTrue ("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes contiguous] Can't be getSelectedNodes equal setSelectedNodes.", // NO18N
154
!trySelection (mgr, contiguousSelection));
155         } catch (PropertyVetoException JavaDoc e) {
156             // exp should be thrown
157
exp = e;
158         } finally {
159             if (exp==null)
160                 fail ("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException was NOT thrown."); // NO18N
161
}
162         
163         // discontiguous
164
try {
165             exp = null;
166             assertTrue ("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", // NO18N
167
trySelection (mgr, discontiguousSelection));
168         } catch (PropertyVetoException JavaDoc e) {
169             // exp should be thrown
170
exp = e;
171         } finally {
172             if (exp==null)
173                 fail ("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); // NO18N
174
}
175         
176     }
177
178     /** Test set all nodes selections if the mode CONTIGUOUS_TREE_SELECTION is set.
179      * @throws Exception */

180     public void testContigousSelection () throws Exception JavaDoc {
181         // try setSelectionMode; if not present then fail
182
setSelectionMode (tree, TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
183         PropertyVetoException JavaDoc exp = null;
184         
185         // single
186
try {
187             // have to be equal
188
assertTrue ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
189
trySelection (mgr, singleSelection));
190         } catch (PropertyVetoException JavaDoc e) {
191             // no exp should be thrown
192
fail ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); // NO18N
193
}
194         
195         // contiguous
196
try {
197             exp = null;
198             // have to be equal
199
assertTrue ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
200
trySelection (mgr, contiguousSelection));
201         } catch (PropertyVetoException JavaDoc e) {
202             // no exp should be thrown
203
fail ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException can't be thrown."); // NO18N
204
}
205         
206         // discontiguous
207
try {
208             // cont' be equal
209
exp = null;
210             assertTrue ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", // NO18N
211
trySelection (mgr, discontiguousSelection));
212         } catch (PropertyVetoException JavaDoc e) {
213             // exp should be thrown
214
exp = e;
215         } finally {
216             if (exp==null)
217                 fail ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); // NO18N
218
}
219         
220     }
221
222     /** Test set all nodes selections if the mode DISCONTIGUOUS_TREE_SELECTION is set.
223      * @throws Exception */

224     public void testDiscontigousSelection () throws Exception JavaDoc {
225         // try setSelectionMode; if not present then fail
226
setSelectionMode (tree, TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
227         PropertyVetoException JavaDoc exp = null;
228         
229         // single
230
try {
231             // have to be equal
232
assertTrue ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
233
trySelection (mgr, singleSelection));
234         } catch (PropertyVetoException JavaDoc e) {
235             // no exp should be thrown
236
fail ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); // NO18N
237
}
238         
239         // contiguous
240
try {
241             // have to be equal
242
assertTrue ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
243
trySelection (mgr, contiguousSelection));
244         } catch (PropertyVetoException JavaDoc e) {
245             // no exp should be thrown
246
fail ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException can't be thrown."); // NO18N
247
}
248         
249         // discontiguous
250
try {
251             // have to be equal
252
assertTrue ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", // NO18N
253
trySelection (mgr, discontiguousSelection));
254         } catch (PropertyVetoException JavaDoc e) {
255             // no exp should be thrown
256
fail ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); // NO18N
257
}
258         
259     }
260     
261     
262     /** Try set array of nodes and check a array which is get back.
263      * @param mgr Explorer manager
264      * @param selected arrar of nodes which will be set
265      * @throws PropertyVetoException may be thrown from setSelecedNodes
266      * @return true if Explorer Manager returned same array as was set.*/

267     private boolean trySelection (ExplorerManager mgr, Node[] selected) throws PropertyVetoException JavaDoc {
268         mgr.setSelectedNodes (selected);
269         if (selected!=null) {
270             return Arrays.equals (selected, mgr.getSelectedNodes ());
271         }
272         return true;
273     }
274     
275     /** Set selection on TreeView if the method is present. If not then the test failed.
276      * @param TreeView tree instance TreeView
277      * @param int mode selection mode */

278     private void setSelectionMode (TreeView tree, int mode) {
279         try {
280             Class JavaDoc c = tree.getClass ();
281             Method JavaDoc m = c.getMethod ("setSelectionMode", new Class JavaDoc[] {Integer.TYPE});
282             m.invoke (tree, new Object JavaDoc[] {new Integer JavaDoc (mode)});
283         } catch (NoSuchMethodException JavaDoc nsme) {
284             fail ("The method setSelectionMode can't be called on this object. See enh #11928.");
285         } catch (IllegalAccessException JavaDoc iae) {
286             fail ("IllegalAccessException thrown from setSelectionMode.");
287         } catch (InvocationTargetException JavaDoc ite) {
288             fail ("InvocationTargetException thrown from setSelectionMode.");
289         }
290     }
291
292 }
293
Popular Tags