1 19 20 package org.netbeans.api.nodes2looks; 21 22 import java.lang.ref.WeakReference ; 23 import org.openide.util.Lookup; 24 import org.openide.util.io.NbMarshalledObject; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.List ; 28 import org.openide.nodes.Node; 29 import org.openide.nodes.Node.Handle; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.spi.looks.Look; 32 import org.netbeans.spi.looks.Looks; 33 import org.netbeans.spi.looks.Selectors; 34 35 public class HierarchyTest extends NbTestCase { 36 37 private Node node; 38 39 public HierarchyTest(String testName) { 40 super(testName); 41 } 42 43 protected void setUp() throws Exception { 44 45 ArrayList l1 = new ArrayList (); 46 47 ArrayList l2 = new ArrayList (); 48 l2.add ("1"); 49 l2.add ("2"); 50 51 l1.add (l2); 52 53 l2 = new ArrayList (); 54 l2.add ("A"); 55 l2.add ("B"); 56 57 l1.add (l2); 58 node = createLookNode (l1, null); 59 60 } 61 62 private static Node createLookNode(final Object obj, Handle handle) { 63 if (handle == null) { 64 handle = new TestHandle (obj); 65 } 66 67 return Nodes.node (obj, 68 Looks.childrenSelectorProvider( 69 "Root_JLD", 70 CL.FIRST_CL, 71 Selectors.array( new Look[] { CL.FIRST_CL, CL.SECOND_CL, CL.FIRST, CL.SECOND } ) ), 72 73 Selectors.array( new Look[] { CL.FIRST_CL } ), 74 handle 75 ); 76 77 } 78 79 protected void tearDown() throws Exception { 80 node = null; 81 } 82 83 85 public void testHierarchy () { 86 checkLook (node.getChildren().getNodes(), CL.FIRST_CL); 87 } 88 89 91 public void testClonedHierarchy () { 92 checkLook (node.cloneNode().getChildren().getNodes(), CL.FIRST_CL); 93 } 94 95 97 public void testChangedLookOnHierarchy () { 98 Node n = node.cloneNode (); 99 100 TestUtil.setLook (n, CL.SECOND_CL); 101 102 assertEquals ("Look must change ", CL.SECOND_CL, TestUtil.getLook( n )); 103 checkLook (n.getChildren ().getNodes (), CL.FIRST_CL); 105 106 TestUtil.setLook ( n, CL.SECOND ); 107 assertEquals ("LookSelector must change ", CL.SECOND, TestUtil.getLook( n )); 108 checkLook (n, CL.SECOND_CL); 109 110 } 111 112 115 public void testLookSelector () throws Exception { 116 117 Node n = node.cloneNode (); 118 Node[] arr = n.getChildren ().getNodes (); 119 120 Node ln = arr[0]; 121 122 TestUtil.setLook( ln, CL.SECOND ); 123 assertEquals( "Look should change. ", CL.SECOND, TestUtil.getLook( ln ) ); 124 checkLook ( ln.getChildren().getNodes(), CL.SECOND_CL); 125 126 WeakReference ref = new WeakReference (ln); 127 128 ln = null; 129 arr = null; 130 131 assertGC ("The node must disapear", ref); 132 133 arr = n.getChildren ().getNodes (); 134 135 ln = arr[0]; 136 137 assertEquals ("Look should remain changed", CL.SECOND, TestUtil.getLook( ln ) ); 138 checkLook (ln.getChildren().getNodes(), CL.SECOND_CL); 139 140 } 141 142 143 145 public void testLookSelectorSerialization () throws Exception { 146 147 Node n = node.cloneNode (); 148 Node[] arr = n.getChildren ().getNodes (); 149 150 151 Node ln = arr[0]; 152 153 TestUtil.setLook( ln, CL.SECOND ); 154 assertEquals ("Look should change", CL.SECOND, TestUtil.getLook( ln ) ); 155 checkLook (ln.getChildren().getNodes(), CL.SECOND_CL); 156 157 Object oldRO = TestUtil.getRepresentedObject( n ); 158 159 Handle h = n.getHandle (); 160 assertNotNull ("Every node has a handle", h); 161 h = (Handle)new NbMarshalledObject (h).get (); 162 n = h.getNode (); 163 164 assertEquals ("Represented object remains equal", oldRO, TestUtil.getRepresentedObject ( n )); 165 assertTrue ("but not the same (deserialized)", oldRO != TestUtil.getRepresentedObject ( n )); 166 167 arr = n.getChildren ().getNodes (); 168 169 170 ln = arr[0]; 171 172 assertEquals ("Look should remain changed", CL.SECOND, TestUtil.getLook( ln ) ); 173 checkLook (ln.getChildren().getNodes(), CL.SECOND_CL); 174 175 } 176 177 178 179 180 private static void checkLook (Node from, Look look) { 181 checkLook (from.getChildren ().getNodes (), look); 182 } 183 184 private static void checkLook (Node[] arr, Look look) { 185 for (int i = 0; i < arr.length; i++) { 186 Node l = arr[i]; 187 188 assertEquals ("Inherits the look", TestUtil.getLook( l ), look); 189 190 checkLook (l, look); 191 } 192 } 193 194 195 197 198 private static final class CL extends Look { 199 200 204 private static final CL FIRST_CL = new CL ("First_LOOK"); 205 private static final CL SECOND_CL = new CL ("Second_LOOK"); 206 207 public static final Look FIRST = Looks.childrenSelectorProvider( 208 "First_JLD", 209 FIRST_CL, 210 Selectors.array( new CL[] { FIRST_CL, SECOND_CL } ) ); 211 212 public static final Look SECOND = Looks.childrenSelectorProvider( 213 "Second_JLD", 214 SECOND_CL, 215 Selectors.array( new CL[] { SECOND_CL, FIRST_CL } ) ); 216 217 218 private CL (String name) { 219 super (name); 220 } 221 222 public boolean isLeaf ( Object representedObject, Lookup env ) { 223 return ! (representedObject instanceof List ); 224 } 225 226 public java.util.List getChildObjects( Object representedObject, Lookup env ) { 227 if ("Empty".equals (getName ())) return null; 228 229 if (representedObject instanceof Collection ) { 230 Collection c = (Collection )representedObject; 231 return new ArrayList ( c ); 232 } else { 233 return null; 234 } 235 } 236 237 public String getName( Object representedObject, Lookup env ) { 238 return representedObject.toString(); 239 } 240 241 public String toString () { 242 return "CL[" + getName () + "]"; 243 } 244 } 245 246 static class TestHandle implements Handle { 247 static final long serialVersionUID = 4503853940L; 248 249 private Object x; 250 251 public TestHandle (Object x) { 252 this.x = x; 253 } 254 255 public Node getNode () { 256 return createLookNode (x, this); 257 } 258 } 259 260 261 } 262 | Popular Tags |