KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > nodes2looks > IsLeafTest


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.netbeans.api.nodes2looks;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.spi.looks.Look;
26 import org.openide.nodes.AbstractNode;
27 import org.openide.nodes.Children;
28 import org.openide.nodes.Node;
29 import org.openide.nodes.NodeEvent;
30 import org.openide.nodes.NodeListener;
31 import org.openide.nodes.NodeMemberEvent;
32 import org.openide.nodes.NodeReorderEvent;
33 import org.openide.util.Lookup;
34
35 /** Tests whether notification to NodeListener is fired under Mutex.writeAccess
36  *
37  * @author Jiri Rechtacek
38  */

39 public class IsLeafTest extends NbTestCase {
40     
41     public IsLeafTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46         super.setUp();
47         TestUtil.setUpRegistryToDefault();
48     }
49     
50     public void testChangeLeafToNonLeafRepObjects () {
51         
52         Children ch = new Children.Array ();
53         Object JavaDoc repLeaf = new AbstractNode (Children.LEAF);
54         Object JavaDoc repRoot = new AbstractNode (ch);
55         Look look = Nodes.nodeLook();
56         
57         Node lnLeaf = Nodes.node( repLeaf, look );
58         Node lnRoot = Nodes.node( repRoot, look );
59         
60         Node n = (Node)repLeaf;
61         assertTrue ("NODE - Leaf is Leaf.", lnLeaf.isLeaf () );
62         assertTrue ("NODE - Root is not Leaf.", !lnRoot.isLeaf () );
63         
64         assertTrue ("LOOK - Leaf is Leaf.", look.isLeaf ( repLeaf, Lookup.EMPTY ) );
65         assertTrue ("LOOK - Root is not Leaf.", !look.isLeaf ( repRoot, Lookup.EMPTY ) );
66         
67         assertEquals ("Leaf has no children.", 0, look.getChildObjects (repLeaf, Lookup.EMPTY).size());
68         assertEquals ("Root has no children.", 0, look.getChildObjects (repRoot, Lookup.EMPTY).size());
69         
70         ch.add (new Node[] { new AbstractNode (Children.LEAF) } );
71         
72         assertTrue ("NODE - Leaf is Leaf.", lnLeaf.isLeaf () );
73         assertTrue ("NODE - Root is not Leaf.", !lnRoot.isLeaf () );
74         
75         assertTrue ("LOOK - Leaf is Leaf.", look.isLeaf ( repLeaf, Lookup.EMPTY ) );
76         assertTrue ("LOOK - Root is not Leaf.", !look.isLeaf ( repRoot, Lookup.EMPTY ) );
77         
78         assertEquals ("Leaf has no children.", 0, look.getChildObjects (repLeaf, Lookup.EMPTY).size());
79         assertEquals ("Root has children.", 1, look.getChildObjects (repRoot, Lookup.EMPTY).size());
80         
81     }
82
83  
84     public void testChangeLeafToNonLeaf () {
85         
86         
87         javax.swing.JTextField JavaDoc rep = new javax.swing.JTextField JavaDoc( TestIsLeafLook.LEAF );
88         
89         TestIsLeafLook look = new TestIsLeafLook ("LeafTestLook");
90         
91         
92         
93         Node node = Nodes.node (rep, look);
94         
95         Listener l = new Listener ();
96         node.addNodeListener (l);
97         
98         // initially is leaf
99
assertTrue ("Look is leaf.", look.isLeaf ( rep, Lookup.EMPTY ));
100         assertTrue ("LookNode is leaf.", node.isLeaf ());
101         
102         if (look.getChildObjects (rep, Lookup.EMPTY ) != null)
103             assertEquals ("Look has no child objects.", 0, look.getChildObjects ( rep, Lookup.EMPTY ).size ());
104         l.assertEvents ("No property change", 0);
105         
106         // set children
107
rep.setText ( TestIsLeafLook.PARENT );
108         
109         look.refreshChildren( rep );
110         assertTrue ("Look is not leaf.", !look.isLeaf (rep, Lookup.EMPTY ));
111         assertTrue ("LookNode is not leaf.", !node.isLeaf ());
112         
113         if (look.getChildObjects ( rep, Lookup.EMPTY ) != null)
114             assertEquals ("Look has one child object.", 1, look.getChildObjects ( rep, Lookup.EMPTY ).size ());
115         else
116             fail ("Look has one child object.");
117         l.assertEvents ("One property change", 1);
118         
119         // check vasted events
120
look.refreshChildren ( rep );
121         look.refreshChildren ( rep );
122         look.refreshChildren ( rep );
123         look.refreshChildren ( rep );
124         l.assertEvents ("No property change", 0);
125         
126         // no children
127
rep.setText (TestIsLeafLook.LEAF);
128         look.refreshChildren ( rep );
129         assertTrue ("Look is leaf.", look.isLeaf ( rep, Lookup.EMPTY ));
130         assertTrue ("LookNode is leaf.", node.isLeaf ());
131         if (look.getChildObjects ( rep, Lookup.EMPTY ) != null)
132             assertEquals ("Look has no child objects.", 0, look.getChildObjects ( rep, Lookup.EMPTY ).size ());
133         l.assertEvents ("One property change", 1);
134         
135         // check vasted events
136
look.refreshChildren ( rep );
137         look.refreshChildren ( rep );
138         look.refreshChildren ( rep );
139         look.refreshChildren ( rep );
140         l.assertEvents ("No property change", 0);
141         
142         // set children
143
rep.setText (TestIsLeafLook.PARENT);
144         look.refreshChildren ( rep );
145         look.refreshChildren ( rep );
146         assertTrue ("Look is not leaf.", !look.isLeaf ( rep, Lookup.EMPTY ));
147         assertTrue ("LookNode is not leaf.", !node.isLeaf ());
148         if (look.getChildObjects ( rep, Lookup.EMPTY ) != null)
149             assertEquals ("Look has one child object.", 1, look.getChildObjects ( rep, Lookup.EMPTY ).size ());
150         else
151             fail ("Look has one child object.");
152         l.assertEvents ("One property change", 1);
153     
154     }
155     
156     private static class Listener extends Object JavaDoc implements NodeListener {
157         private int leafs = 0;
158         
159         public void assertEvents (String JavaDoc txt, int eventsCount) {
160             if (leafs != -1)
161                 assertEquals (txt + " leafs", eventsCount, leafs);
162             leafs = 0;
163         }
164         
165         public void propertyChange (java.beans.PropertyChangeEvent JavaDoc evt) {
166             if (Node.PROP_LEAF == evt.getPropertyName()) {
167                 leafs++;
168             }
169         }
170         public void childrenAdded(NodeMemberEvent ev) {}
171         public void childrenRemoved(NodeMemberEvent ev) {}
172         public void childrenReordered(NodeReorderEvent ev) {}
173         public void objectDestroyed(NodeEvent ev) {}
174       
175         public void nodeDestroyed(NodeEvent ev) {
176         }
177         
178     } // end of Listener
179

180     // substitute with public fireChildrenChange() method
181

182     
183     private static class TestIsLeafLook extends Look {
184         
185         final static String JavaDoc LEAF = "Leaf";
186         final static String JavaDoc PARENT = "Parent";
187                 
188         TestIsLeafLook( String JavaDoc name ) {
189             super( name );
190         }
191
192         public List JavaDoc getChildObjects( Object JavaDoc ro, Lookup env ) {
193             String JavaDoc s = ((javax.swing.JTextField JavaDoc)ro).getText ();
194             if (LEAF.equals (s)) {
195                 return null;
196             } else {
197                return Arrays.asList (new Object JavaDoc[] {"Child"});
198             }
199         }
200
201         public boolean isLeaf ( Object JavaDoc ro, Lookup env ) {
202             return getChildObjects (ro, env) == null ? true : getChildObjects (ro, env).size () == 0;
203         }
204
205         public void refreshChildren( Object JavaDoc o ) {
206             fireChange( o, Look.GET_CHILD_OBJECTS );
207         }
208     };
209     
210 }
211
Popular Tags