KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
23 import java.beans.PropertyVetoException JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import javax.swing.JFrame JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JTree JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.tree.TreePath JavaDoc;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.explorer.ExplorerManager;
32 import org.openide.nodes.AbstractNode;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.Node;
35
36 /**
37  * Tests for class BeanTreeViewTest
38  */

39 public class BeanTreeViewTest extends NbTestCase {
40     
41     private static final int NO_OF_NODES = 3;
42     
43     
44     public BeanTreeViewTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     public void testFirstChildRemovalCausesSelectionOfSibling() throws Throwable JavaDoc {
49         doChildRemovalTest("foo");
50     }
51     public void testSecondChildRemovalCausesSelectionOfSibling() throws Throwable JavaDoc {
52         doChildRemovalTest("bar");
53     }
54     public void testThirdChildRemovalCausesSelectionOfSibling() throws Throwable JavaDoc {
55         doChildRemovalTest("bla");
56     }
57     
58     private void doChildRemovalTest(final String JavaDoc name) throws Throwable JavaDoc {
59         final AbstractNode root = new AbstractNode(new Children.Array());
60         root.setName("test root");
61         
62         final Node[] children = {
63             createLeaf("foo"),
64             createLeaf("bar"),
65             createLeaf("bla"),
66         };
67         
68         root.getChildren().add(children);
69         
70         final Panel JavaDoc p = new Panel JavaDoc();
71         p.getExplorerManager().setRootContext(root);
72         
73         final BeanTreeView btv = new BeanTreeView();
74         p.add(BorderLayout.CENTER, btv);
75         
76         JFrame JavaDoc f = new JFrame JavaDoc();
77         f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
78         f.getContentPane().add(BorderLayout.CENTER, p);
79         f.setVisible(true);
80         
81         final JTree JavaDoc tree = btv.tree;
82         
83         class AWTTst implements Runnable JavaDoc {
84             public void run() {
85                 Node operateOn;
86                 
87                 for (int i = 0; ; i++) {
88                     if (name.equals(children[i].getName())) {
89                         // this should select a sibling of the removed node
90
operateOn = children[i];
91                         break;
92                     }
93                 }
94                 
95                 try {
96                     p.getExplorerManager().setSelectedNodes(new Node[] { operateOn });
97                 } catch (PropertyVetoException JavaDoc e) {
98                     fail("Unexpected PropertyVetoException from ExplorerManager.setSelectedNodes()");
99                 }
100                 
101                 TreePath JavaDoc[] paths = tree.getSelectionPaths();
102                 assertNotNull("Before removal: one node should be selected, but there are none.", paths);
103                 assertEquals("Before removal: one node should be selected, but there are none.", 1, paths.length);
104                 assertEquals("Before removal: one node should be selected, but there are none.", operateOn, Visualizer.findNode(paths[0].getLastPathComponent()));
105                 
106                 // this should select a sibling of the removed node
107
root.getChildren().remove(new Node[] { operateOn });
108                 
109                 assertNotNull("After removal: one node should be selected, but there are none.", tree.getSelectionPath());
110             }
111         }
112         AWTTst awt = new AWTTst();
113         try {
114             SwingUtilities.invokeAndWait(awt);
115         } catch (InvocationTargetException JavaDoc ex) {
116             throw ex.getTargetException();
117         }
118     }
119     
120     private static Node createLeaf(String JavaDoc name) {
121         AbstractNode n = new AbstractNode(Children.LEAF);
122         n.setName(name);
123         return n;
124     }
125     
126     private static class Panel extends JPanel JavaDoc
127             implements ExplorerManager.Provider {
128         private ExplorerManager em = new ExplorerManager();
129         
130         public ExplorerManager getExplorerManager() {
131             return em;
132         }
133     }
134 }
135
Popular Tags