KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dimension JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.JFrame JavaDoc;
29 import javax.swing.JScrollBar JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31 import org.netbeans.junit.NbTestCase;
32 import org.openide.explorer.ExplorerManager;
33 import org.openide.nodes.AbstractNode;
34 import org.openide.nodes.Children;
35 import org.openide.nodes.FilterNode;
36 import org.openide.nodes.Node;
37 import org.openide.util.HelpCtx;
38 import org.openide.util.Lookup;
39 import org.openide.util.RequestProcessor;
40 import org.openide.util.actions.NodeAction;
41 import org.openide.util.actions.SystemAction;
42
43 /**
44  *
45  * @author Marian Petras, Andrei Badea
46  */

47 public final class TreeViewTest extends NbTestCase {
48     
49     private TestTreeView treeView;
50     private JFrame JavaDoc testWindow;
51     private volatile boolean isScrolledDown;
52     private final Object JavaDoc semaphore = new Object JavaDoc();
53     
54     public TreeViewTest(String JavaDoc testName) {
55         super(testName);
56     }
57     
58     /**
59      * Tests whether <code>JTree</code>'s property <code>scrollsOnExpand</code>
60      * is taken into account in
61      * <code>TreeView.TreePropertyListener.treeExpanded(...)</code>.
62      */

63     public void testAutoscrollOnOff() throws InterruptedException JavaDoc {
64         assert !EventQueue.isDispatchThread();
65         
66         testWindow = new ExplorerWindow();
67         testWindow.getContentPane().add(treeView = new TestTreeView());
68         
69         class WindowDisplayer implements Runnable JavaDoc {
70             public void run() {
71                 testWindow.pack();
72                 testWindow.setVisible(true);
73             }
74         }
75         
76         class Detector implements Runnable JavaDoc {
77             public void run() {
78                 if (!EventQueue.isDispatchThread()) {
79                     EventQueue.invokeLater(this);
80                     return;
81                 }
82                 
83                 isScrolledDown = !treeView.isUp();
84                 
85                 synchronized (semaphore) {
86                     semaphore.notify();
87                 }
88             }
89         }
90
91         class Tester implements Runnable JavaDoc {
92             private final boolean autoscroll;
93             private final int part;
94             Tester(boolean autoscroll, int part) {
95                 this.autoscroll = autoscroll;
96                 this.part = part;
97             }
98             public void run() {
99                 assert (part == 1) || (part == 2);
100                 if (part == 1) {
101                     treeView.collapse();
102                     treeView.scrollUp();
103                     assert treeView.isUp();
104                 } else {
105                     treeView.setAutoscroll(autoscroll);
106                     treeView.expand(); //<-- posts a request to the RequestProcessor
107
RequestProcessor.getDefault().post(new Detector(), 1000 /*ms*/);
108                 }
109             }
110         }
111
112         try {
113             EventQueue.invokeAndWait(new WindowDisplayer());
114         } catch (InvocationTargetException JavaDoc ex) {
115             ex.printStackTrace();
116         }
117         
118         //Wait for the AWT thread to actually display the dialog:
119
Thread.currentThread().sleep(5000);
120         
121         EventQueue.invokeLater(new Tester(true, 1));
122         Thread.currentThread().sleep(2000); //wait for update of the screen
123
EventQueue.invokeLater(new Tester(true, 2));
124         synchronized (semaphore) {
125             semaphore.wait();
126         }
127         assertTrue("Check the view has scrolled", isScrolledDown);
128
129         EventQueue.invokeLater(new Tester(false, 1));
130         Thread.currentThread().sleep(2000); //wait for update of the screen
131
EventQueue.invokeLater(new Tester(false, 2));
132         synchronized (semaphore) {
133             semaphore.wait();
134         }
135         assertTrue("Check the view has not scrolled", !isScrolledDown);
136
137         EventQueue.invokeLater(new Tester(true, 1)); //just collapse the tree
138
Thread.currentThread().sleep(2000);
139     }
140     
141     
142     private static final class TestTreeView extends BeanTreeView {
143         
144         private final Node rootNode;
145         final JScrollBar JavaDoc verticalScrollBar;
146         private transient ExplorerManager explManager;
147         
148         TestTreeView() {
149             super();
150             tree.setAutoscrolls(true);
151
152             setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
153             verticalScrollBar = getVerticalScrollBar();
154             
155             rootNode = new AbstractNode(new TreeChildren());
156             rootNode.setDisplayName("Root node");
157             
158             tree.setRowHeight(20);
159             
160             Dimension JavaDoc prefSize = new Dimension JavaDoc(200, 6 * tree.getRowHeight() + 8);
161             prefSize.width = (int) (prefSize.width * 1.25f)
162                              + verticalScrollBar.getWidth();
163             setPreferredSize(prefSize);
164         }
165         
166         public void addNotify() {
167             super.addNotify();
168             explManager = ExplorerManager.find(this);
169             explManager.setRootContext(rootNode);
170             collapse();
171         }
172         
173         void setAutoscroll(boolean autoscroll) {
174             tree.setScrollsOnExpand(autoscroll);
175         }
176         
177         void scrollUp() {
178             verticalScrollBar.setValue(verticalScrollBar.getMinimum());
179         }
180         
181         boolean isUp() {
182             return verticalScrollBar.getValue()
183                    == verticalScrollBar.getMinimum();
184         }
185         
186         void expand() {
187             tree.expandRow(4);
188         }
189         
190         void collapse() {
191             tree.collapseRow(4);
192         }
193         
194         final static class TreeChildren extends Children.Array {
195             
196             private static final char[] letters
197                     = new char[] {'A', 'B', 'C', 'D', 'E'};
198             
199             TreeChildren() {
200                 this(-1);
201             }
202             
203             TreeChildren(final int first) {
204                 super();
205                 
206                 Node[] childNodes = new Node[5];
207                 int i;
208                 if (first == -1) {
209                     for (i = 0; i < childNodes.length; i++) {
210                         AbstractNode childNode = new AbstractNode(new TreeChildren(i));
211                         childNode.setDisplayName("Child node " + i);
212                         childNodes[i] = childNode;
213                     }
214                 } else {
215                     for (i = 0; i < childNodes.length; i++) {
216                         AbstractNode childNode = new AbstractNode(Children.LEAF);
217                         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(3);
218                         childNode.setDisplayName(buf.append(first)
219                                                     .append('.')
220                                                     .append(letters[i])
221                                                     .toString());
222                         childNodes[i] = childNode;
223                     }
224                 }
225                 add(childNodes);
226             }
227             
228         }
229         
230         
231     }
232     
233     
234     private static final class ExplorerWindow extends JFrame JavaDoc
235                                implements ExplorerManager.Provider {
236         
237         private final ExplorerManager explManager = new ExplorerManager();
238         
239         ExplorerWindow() {
240             super("TreeView test"); //NOI18N
241
}
242         
243         public ExplorerManager getExplorerManager() {
244             return explManager;
245         }
246         
247     }
248     
249     /**
250      * Used as the preferred actions by the nodes below
251      */

252     private static class MyAction extends NodeAction {
253
254         public boolean enable(Node[] nodes) {
255             return true;
256         }
257
258         public void performAction(Node[] nodes) {
259         }
260
261         public HelpCtx getHelpCtx() {
262             return HelpCtx.DEFAULT_HELP;
263         }
264
265         public String JavaDoc getName() {
266             return "My Action";
267         }
268
269         public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
270             return new MyDelegateAction(actionContext);
271         }
272     }
273     
274     /**
275      * Returned by MyAction.createContextAwareInstance().
276      */

277     private static class MyDelegateAction extends AbstractAction JavaDoc {
278         Lookup contextLookup;
279         
280         public MyDelegateAction(Lookup contextLookup) {
281             this.contextLookup = contextLookup;
282         }
283         
284         public void actionPerformed(ActionEvent JavaDoc e) {
285         }
286     }
287
288     private static class NodeWhichHasItselfInLookup extends AbstractNode {
289         public NodeWhichHasItselfInLookup() {
290             super(Children.LEAF);
291         }
292
293         public Action JavaDoc getPreferredAction() {
294             return SystemAction.get(MyAction.class);
295         }
296     }
297
298     private static class NodeWhichDoesntHaveItselfInLookup extends AbstractNode {
299         public NodeWhichDoesntHaveItselfInLookup() {
300             super(Children.LEAF, Lookup.EMPTY);
301         }
302
303         public Action JavaDoc getPreferredAction() {
304             return SystemAction.get(MyAction.class);
305         }
306     }
307     
308     /**
309      * Tests that the context lookup created by TreeView.takeAction() only contains
310      * the node once when the node contains itself in its lookup.
311      */

312     public void testTakeActionNodeInLookup() {
313         doTestTakeAction(new NodeWhichHasItselfInLookup());
314     }
315
316     /**
317      * Tests that the context lookup created by TreeView.takeAction() only contains
318      * the node once when the node doesn't contain itself in its lookup.
319      */

320     public void testTakeActionNodeNotInLookup() {
321         doTestTakeAction(new NodeWhichDoesntHaveItselfInLookup());
322     }
323     
324     /**
325      * Tests that the context lookup created by TreeView.takeAction() only contains
326      * the node once when the node contains itself in its lookup and is filtered by a FilterNode.
327      */

328     public void testTakeActionNodeInLookupAndFiltered() {
329         doTestTakeAction(new FilterNode(new NodeWhichHasItselfInLookup()));
330     }
331
332     /**
333      * Tests that the context lookup created by TreeView.takeAction() only contains
334      * the node once when the node doesn't contain itself in its lookup
335      * and is filtered by a FilterNode.
336      */

337     public void testTakeActionNodeNotInLookupAndFiltered() {
338         doTestTakeAction(new FilterNode(new NodeWhichDoesntHaveItselfInLookup()));
339     }
340     
341     private void doTestTakeAction(Node node) {
342         // if the preferred action instanceof ContextAwareAction
343
// calls its createContextAwareInstance() method
344
Action JavaDoc a = TreeView.takeAction(node.getPreferredAction(), node);
345         int count = ((MyDelegateAction)a).contextLookup.lookup(new Lookup.Template(Node.class)).allInstances().size();
346         assertEquals("The context lookup created by TreeView.takeAction() should contain the node only once.", 1, count);
347     }
348     
349 }
350
Popular Tags