1 19 20 package org.netbeans.api.nodes2looks; 21 22 import java.util.Arrays ; 23 import java.util.List ; 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 39 public class IsLeafTest extends NbTestCase { 40 41 public IsLeafTest(String name) { 42 super(name); 43 } 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 TestUtil.setUpRegistryToDefault(); 48 } 49 50 public void testChangeLeafToNonLeafRepObjects () { 51 52 Children ch = new Children.Array (); 53 Object repLeaf = new AbstractNode (Children.LEAF); 54 Object 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 rep = new javax.swing.JTextField ( 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 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 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 look.refreshChildren ( rep ); 121 look.refreshChildren ( rep ); 122 look.refreshChildren ( rep ); 123 look.refreshChildren ( rep ); 124 l.assertEvents ("No property change", 0); 125 126 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 look.refreshChildren ( rep ); 137 look.refreshChildren ( rep ); 138 look.refreshChildren ( rep ); 139 look.refreshChildren ( rep ); 140 l.assertEvents ("No property change", 0); 141 142 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 implements NodeListener { 157 private int leafs = 0; 158 159 public void assertEvents (String 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 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 } 180 182 183 private static class TestIsLeafLook extends Look { 184 185 final static String LEAF = "Leaf"; 186 final static String PARENT = "Parent"; 187 188 TestIsLeafLook( String name ) { 189 super( name ); 190 } 191 192 public List getChildObjects( Object ro, Lookup env ) { 193 String s = ((javax.swing.JTextField )ro).getText (); 194 if (LEAF.equals (s)) { 195 return null; 196 } else { 197 return Arrays.asList (new Object [] {"Child"}); 198 } 199 } 200 201 public boolean isLeaf ( Object ro, Lookup env ) { 202 return getChildObjects (ro, env) == null ? true : getChildObjects (ro, env).size () == 0; 203 } 204 205 public void refreshChildren( Object o ) { 206 fireChange( o, Look.GET_CHILD_OBJECTS ); 207 } 208 }; 209 210 } 211 | Popular Tags |