1 19 20 package org.netbeans.spi.looks; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.List ; 28 import org.netbeans.api.nodes2looks.Nodes; 29 import org.netbeans.api.nodes2looks.TestUtil; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.nodes.Node; 32 import org.openide.util.Enumerations; 33 import org.openide.util.Lookup; 34 35 39 public class SelectorTest extends NbTestCase { 40 41 public SelectorTest(String name) { 42 super(name); 43 } 44 45 48 public void testSimpleSelector() { 49 50 LookSelector rootSelector = Selectors.selector( new RootProvider( null ) ); 51 Node root = Nodes.node ( "ROOT", null, rootSelector); 52 53 Node[] nodes = root.getChildren ().getNodes (); 54 55 assertEquals("Root has two childner.", 2, nodes.length); 57 assertEquals("Root LookNode has RootSelector.", rootSelector, TestUtil.getLookSelector ( root )); 58 Node children[] = root.getChildren().getNodes(); 59 60 assertEquals( "Look of child is a instance of LookForRepObj1", Looks.bean().getClass(), TestUtil.getLook( children[0] ).getClass() ); 62 assertEquals( "Look of child is a instance of LookForRepObj2", Looks.bean().getClass(), TestUtil.getLook ( children[1] ).getClass() ); 63 64 assertTrue ("Selector of child 1 ", rootSelector == TestUtil.getLookSelector ( children[0] ) ); 66 assertTrue ("Selector of child 2 ", rootSelector == TestUtil.getLookSelector( children[1] ) ); 67 } 68 69 71 public void testChangeSimpleSelector() { 72 LookSelector restrictedSelector = Selectors.selector( new RestrictedProvider() ); 73 LookSelector rootSelector = Selectors.selector( new RootProvider ( restrictedSelector ) ); 74 Node root = Nodes.node ( "ROOT", null, rootSelector); 75 76 Node[] nodes = root.getChildren ().getNodes (); 77 78 assertEquals("Root has two childner.", 2, nodes.length); 80 assertEquals("Root LookNode has RootSelector.", rootSelector, TestUtil.getLookSelector ( root )); 81 Node children[] = root.getChildren().getNodes(); 82 83 assertEquals( "Look of child is a instance of LookForRepObj1", LookForRepObj1.class, TestUtil.getLook( children[0] ).getClass() ); 85 assertEquals( "Look of child is a instance of LookForRepObj2", LookForRepObj2.class, TestUtil.getLook ( children[1] ).getClass() ); 86 87 assertTrue ("Selector of child 1 ", restrictedSelector == TestUtil.getLookSelector ( children[0] ) ); 89 assertTrue ("Selector of child 2 ", restrictedSelector == TestUtil.getLookSelector( children[1] ) ); 90 } 91 92 131 132 167 168 254 255 257 public static class RootProvider implements LookProvider { 258 259 private Look stringLook; 260 private Look r1Look; 261 private Look r2Look; 262 263 public RootProvider( LookSelector chidrenSelector ) { 264 if ( chidrenSelector == null ) { 265 stringLook = new RootLook( null ); 266 r1Look = Looks.bean(); 267 r2Look = Looks.bean(); 268 } 269 else { 270 stringLook = new RootLook( chidrenSelector ); 271 r1Look = LookForRepObj1.INSTANCE; 272 r2Look = LookForRepObj2.INSTANCE; 273 } 274 } 275 276 public Enumeration getLooksForObject(Object representedObject) { 277 if (representedObject instanceof RepObj1) { 278 return Enumerations.singleton(r1Look); 279 } 280 else if ( representedObject instanceof RepObj2 ) { 281 return Enumerations.singleton(r2Look); 282 } 283 else if ( representedObject instanceof String ) { 284 return Enumerations.singleton(stringLook); 285 } 286 else { 287 return Enumerations.singleton(Looks.bean()); 288 } 289 } 290 291 } 292 293 public class RestrictedProvider implements LookProvider { 294 295 public RestrictedProvider() { 296 } 297 298 public Enumeration getLooksForObject( Object representedObject ) { 299 if ( representedObject instanceof RepObj1 ) { 300 return Enumerations.singleton(LookForRepObj1.INSTANCE); 301 } 302 else if ( representedObject instanceof RepObj2 ) { 303 return Enumerations.singleton(LookForRepObj2.INSTANCE); 304 } 305 else 306 throw new RuntimeException ("Wrong type of represented object, was "+representedObject); 307 } 308 309 } 310 311 313 333 334 335 337 public class ProviderForRepObj1 implements LookProvider { 338 private String name = "SelectorForRepObj1"; 339 340 public ProviderForRepObj1 () { 341 } 342 343 public Enumeration getLooksForObject(Object representedObject) { 344 if (representedObject instanceof RepObj1) 345 return Collections.enumeration (Arrays.asList (new Object [] { new LookForRepObj1 () })); 346 else 347 throw new RuntimeException ("Wrong type of represented object, was "+representedObject); 348 } 349 350 } 351 352 354 public class ProviderForRepObj2 implements LookProvider { 355 private String name = "SelectorForRepObj2"; 356 357 public ProviderForRepObj2 () { 358 } 359 360 public Enumeration getLooksForObject(Object representedObject) { 361 if (representedObject instanceof RepObj2) 362 return Collections.enumeration (Arrays.asList (new Object [] { new LookForRepObj2 () })); 363 else 364 throw new RuntimeException ("Wrong type of represented object, was "+representedObject); 365 } 366 367 } 368 369 371 private static class RootLook extends Look { 372 373 private LookSelector childrenSelector; 374 375 public RootLook ( LookSelector childrenSelector ) { 376 super( "RootLook" ); 377 this.childrenSelector = childrenSelector; 378 } 379 380 public String getName( Object representedObject, Lookup env ) { 381 return getName(); 382 } 383 384 public boolean isLeaf ( Object representedObject, Lookup env ) { 385 return false; 386 } 387 388 public List getChildObjects( Object representedObject, Lookup env ) { 389 ArrayList objects = new ArrayList (); 390 objects.add (new RepObj1 ("RepObj1")); 391 objects.add (new RepObj2 ("RepObj2")); 392 return objects; 393 } 394 395 public Collection getLookupItems( Object representedObject, Lookup env ) { 396 if ( childrenSelector != null ) { 397 ArrayList items = new ArrayList (); 398 items.add( new GoldenValue.TestLookupItem( childrenSelector ) ); 399 return items; 400 } 401 else { 402 return null; 403 } 404 } 405 406 } 407 408 private static class LookForRepObj1 extends Look { 409 410 public static Look INSTANCE = new LookForRepObj1(); 411 412 public LookForRepObj1 () { 413 super( "LookForRepObj1" ); 414 } 415 416 public String getName( Object representedObject, Lookup env ) { 417 return getName(); 418 } 419 420 public boolean isLeaf( Object representedObject, Lookup env ) { 421 return false; 422 } 423 424 public List getChildObjects( Object representedObject, Lookup env ) { 425 ArrayList objects = new ArrayList (); 426 objects.add (new RepObj1 ("RepObj1")); 427 return objects; 428 } 429 } 430 431 private static class LookForRepObj2 extends Look { 432 433 public static Look INSTANCE = new LookForRepObj2(); 434 435 public LookForRepObj2 () { 436 super( "LookForRepObj2" ); 437 } 438 439 public String getDisplayName() { 440 return getName(); 441 } 442 443 public String getName( Object representedObject, Lookup env ) { 444 return getName(); 445 } 446 447 public boolean isLeaf( Object representedObject, Lookup env ) { 448 return false; 449 } 450 451 public List getChildObjects( Object representedObject, Lookup env ) { 452 ArrayList objects = new ArrayList (); 453 objects.add (new RepObj2 ("RepObj2")); 454 return objects; 455 } 456 } 457 458 private static class DecoratorLook extends Look { 459 460 461 public DecoratorLook () { 462 super( "DecoratorLook" ); 463 } 464 465 466 public String getName( Object representedObject, Lookup env ) { 467 return getName(); 468 } 469 470 public boolean isLeaf ( Object representedObject, Lookup env ) { 471 return true; 472 } 473 } 474 475 477 public static class RepObj1 extends Object { 478 479 private String name; 480 481 public RepObj1 (String name) { 482 this.name = name; 483 } 484 485 public String toString () { 486 return "Represented object "+name; 487 } 488 } 489 490 public static class RepObj2 extends Object { 491 private String name; 492 493 public RepObj2 (String name) { 494 this.name = name; 495 } 496 497 public String toString () { 498 return "Represented object "+name; 499 } 500 } 501 502 503 } 504 | Popular Tags |