1 19 20 package org.netbeans.spi.looks; 21 22 import java.awt.Component ; 23 import java.awt.datatransfer.Transferable ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 import java.util.IdentityHashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import javax.swing.Action ; 34 import org.netbeans.modules.looks.LookListener; 35 import org.netbeans.modules.looks.LookEvent; 36 import org.openide.nodes.Node; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.Lookup; 39 import org.openide.util.datatransfer.NewType; 40 import org.openide.util.datatransfer.PasteType; 41 42 61 public abstract class Look<T> extends Object { 62 63 64 private static final long PROPERTY_CHANGE = -1; 65 66 67 public static final long ALL_METHODS = Long.MAX_VALUE; 68 69 public static final long NO_METHODS = 0; 70 71 72 73 public static final long DESTROY = 1; 74 75 public static final long RENAME = DESTROY << 1; 76 77 public static final long GET_LOOKUP_ITEMS = RENAME << 1; 78 79 public static final long GET_DISPLAY_NAME = GET_LOOKUP_ITEMS << 1; 80 81 public static final long GET_NAME = GET_DISPLAY_NAME << 1; 82 83 public static final long GET_SHORT_DESCRIPTION = GET_NAME << 1; 84 85 public static final long GET_ICON = GET_SHORT_DESCRIPTION << 1; 86 87 public static final long GET_OPENED_ICON = GET_ICON << 1; 88 89 public static final long GET_HELP_CTX = GET_OPENED_ICON << 1; 90 91 public static final long GET_CHILD_OBJECTS = GET_HELP_CTX << 1; 92 93 public static final long GET_NEW_TYPES = GET_CHILD_OBJECTS << 1; 94 95 public static final long GET_ACTIONS = GET_NEW_TYPES << 1; 96 97 public static final long GET_CONTEXT_ACTIONS = GET_ACTIONS << 1; 98 99 public static final long GET_DEFAULT_ACTION = GET_CONTEXT_ACTIONS << 1; 100 101 public static final long GET_PROPERTY_SETS = GET_DEFAULT_ACTION << 1; 102 103 public static final long GET_CUSTOMIZER = GET_PROPERTY_SETS << 1; 104 105 public static final long CAN_RENAME = GET_CUSTOMIZER << 1; 106 107 public static final long CAN_DESTROY = CAN_RENAME << 1; 108 109 public static final long CAN_COPY = CAN_DESTROY << 1; 110 111 public static final long CAN_CUT = CAN_COPY << 1; 112 113 public static final long GET_PASTE_TYPES = CAN_CUT << 1; 114 115 public static final long GET_DROP_TYPE = GET_PASTE_TYPES << 1; 116 117 public static final long CLIPBOARD_COPY = GET_DROP_TYPE << 1; 118 119 public static final long CLIPBOARD_CUT = CLIPBOARD_COPY << 1; 120 121 public static final long DRAG = CLIPBOARD_CUT << 1; 122 123 public static final long HAS_CUSTOMIZER = DRAG << 1; 124 125 126 private ListenerCache<T> listenerCache; 127 128 130 private String name; 131 132 static { 133 org.netbeans.modules.looks.Accessor.DEFAULT = new AccessorImpl (); 135 } 136 137 139 142 protected Look( String name ) { 143 this.name = name; 144 } 145 146 148 151 public final String getName() { 152 return name; 153 } 154 155 159 public String getDisplayName() { 160 return getName(); 161 } 162 163 public String toString() { 164 return getName(); 165 } 166 167 169 187 protected void attachTo( T representedObject ) { 188 } 189 190 191 204 protected void detachFrom( T representedObject ) { 205 } 206 207 209 216 public java.util.Collection getLookupItems( T representedObject, Lookup oldEnv ) { 217 return null; 218 } 219 220 222 230 public String getName( T representedObject, Lookup env ) { 231 return null; 232 } 233 234 239 public String getDisplayName( T representedObject, Lookup env ) { 240 return null; 241 } 242 243 248 public void rename( T representedObject, String newName, Lookup env ) throws IOException { 249 } 250 251 257 public String getShortDescription( T representedObject, Lookup env ) { 258 return null; 259 } 260 261 267 public java.awt.Image getIcon( T representedObject, int type, Lookup env ) { 268 return null; 269 } 270 271 272 273 280 public java.awt.Image getOpenedIcon( T representedObject, int type, Lookup env ) { 281 return null; 282 } 283 284 290 public HelpCtx getHelpCtx( T representedObject, Lookup env ) { 291 return null; 292 } 293 294 296 303 public List <?> getChildObjects( T representedObject, Lookup env ) { 304 return null; 305 } 306 307 324 public boolean isLeaf( T representedObject, Lookup env ) { 325 return false; 326 } 327 328 329 331 338 public NewType[] getNewTypes( T representedObject, Lookup env ) { 339 return null; 340 } 341 342 350 public Action [] getActions( T representedObject, Lookup env ) { 351 return null; 352 } 353 354 355 361 public Action [] getContextActions( T representedObject, Lookup env ) { 362 return null; 363 } 364 365 372 public Action getDefaultAction( T representedObject, Lookup env ) { 373 return null; 374 } 375 376 378 386 public Node.PropertySet[] getPropertySets( T representedObject, Lookup env ) { 387 return null; 388 } 389 390 398 public boolean hasCustomizer( T representedObject, Lookup env ) { 399 return false; 400 } 401 402 407 public Component getCustomizer( T representedObject, Lookup env ) { 408 return null; 409 } 410 411 412 414 421 public boolean canRename( T representedObject, Lookup env ) { 422 return false; 423 } 424 425 430 public boolean canDestroy( T representedObject, Lookup env ) { 431 return false; 432 } 433 434 439 public boolean canCopy( T representedObject, Lookup env ) { 440 return false; 441 } 442 443 448 public boolean canCut( T representedObject, Lookup env ) { 449 return false; 450 } 451 452 461 public PasteType[] getPasteTypes( T representedObject, Transferable t, Lookup env ) { 462 return null; 463 } 464 465 477 public PasteType getDropType( T representedObject, Transferable t, int action, int index, Lookup env ) { 478 return null; 479 } 480 481 487 public Transferable clipboardCopy( T representedObject, Lookup env ) throws IOException { 488 return null; 489 } 490 491 497 public Transferable clipboardCut( T representedObject, Lookup env ) throws IOException { 498 return null; 499 } 500 501 509 public Transferable drag( T representedObject, Lookup env ) throws IOException { 510 return null; 511 } 512 513 519 public void destroy( T representedObject, Lookup env ) throws IOException { 520 } 521 522 524 533 protected final void fireChange( T representedObject, long mask ) { 534 fireUniversal( mask, representedObject, null ); 535 } 536 537 543 protected final void firePropertyChange( T representedObject, String propertyName ) { 544 fireUniversal( PROPERTY_CHANGE, representedObject, propertyName ); 545 } 546 547 549 synchronized void addLookListener( T representedObject, LookListener listener ) { 550 551 if ( representedObject != null && 552 ( listenerCache == null || 553 listenerCache.getListenersCount( representedObject ) == 0 ) ) { 554 attachTo( representedObject ); 555 } 556 557 if ( listenerCache == null ) { 558 listenerCache = new ListenerCache<T>(); 559 } 560 561 listenerCache.addListener( representedObject, listener ); 562 563 } 564 565 synchronized void changeLookListener( T representedObject, LookListener oldListener, LookListener newListener ) { 566 if ( listenerCache != null ) { 567 listenerCache.changeListener( representedObject, oldListener, newListener ); 568 } 569 } 570 571 synchronized void removeLookListener( T representedObject, LookListener listener ) { 572 573 if ( representedObject != null && listenerCache != null && 574 listenerCache.getListenersCount( representedObject ) == 1 ) { 575 detachFrom( representedObject ); 576 } 577 578 if ( listenerCache != null ) { 579 listenerCache.removeListener( representedObject, listener ); 580 } 581 } 582 583 585 589 synchronized T[] getAllObjects() { 590 Collection <T> allObjects = listenerCache.getAllObjects(); 591 @SuppressWarnings ("unchecked") 592 T[] objects = (T[]) allObjects.toArray(); 593 return objects; 594 } 595 596 598 private void fireUniversal( long mask, T representedObject, String propertyName ) { 599 if ( listenerCache == null ) { 600 return; 601 } 602 603 if ( representedObject == null ) { T[] objects = getAllObjects(); 605 if ( objects == null ) { 606 return; 607 } 608 for( int i = 0; i < objects.length; i++ ) { 609 fireUniversal( mask, objects[i], name ); 610 } 611 } 612 else { LookEvent evt = mask == PROPERTY_CHANGE ? 614 new LookEvent( representedObject, propertyName ) : 615 new LookEvent( representedObject, mask ); 616 617 List listeners; 618 synchronized( this ) { 619 listeners = listenerCache.getListeners( representedObject ); 620 } 621 for( int i = 0; i < listeners.size(); i++ ) { 622 if ( mask == PROPERTY_CHANGE ) { 623 ((LookListener)listeners.get( i )).propertyChange( evt ); 624 } 625 else { 626 ((LookListener)listeners.get( i )).change( evt ); 627 } 628 } 629 } 630 631 } 632 633 634 636 private static final Object PLACE_HOLDER = new Object (); 637 638 private class ListenerCache<T> { 640 641 private Set <LookListener> allObjectListeners; private Map <T,Object > obj2l; 643 644 void addListener( T object, LookListener listener ) { 645 646 if ( object == null ) { 648 if ( allObjectListeners == null ) { 649 allObjectListeners = new HashSet <LookListener>(); 650 } 651 if ( listener != null ) { 652 allObjectListeners.add( listener ); 653 } 654 } 655 else { 657 if ( obj2l == null ) { 658 obj2l = new IdentityHashMap <T,Object >(); 659 } 660 661 Object l = obj2l.get( object ); 662 663 if ( l == null ) { obj2l.put( object, listener == null ? PLACE_HOLDER : listener ); } 666 else { if ( l instanceof LookListener || l == PLACE_HOLDER ) { if (l == listener) { 669 return; 670 } 671 List ll = new ArrayList ( 2 ); ll.add( l ); 673 ll.add( listener == null ? PLACE_HOLDER : listener ); 674 obj2l.put( object, ll ); 675 } 676 else { List list = (List )l; 678 if (list.contains(listener)) { 679 return; 680 } 681 list.add( listener == null ? PLACE_HOLDER : listener ); 682 } 683 } 684 } 685 } 686 687 void removeListener( T object, LookListener listener ) { 688 689 if ( object == null ) { 690 if ( allObjectListeners != null ) { 691 allObjectListeners.remove( listener ); 692 } 693 return; 694 } 695 696 if ( obj2l == null ) { 697 return; 698 } 699 700 Object l = obj2l.get( object ); 701 702 if ( l == PLACE_HOLDER || l instanceof LookListener ) { obj2l.remove( object ); 704 } 705 else if ( l != null ) { List ll = (List )l; 707 ll.remove( listener == null ? PLACE_HOLDER : listener ); 708 if ( ll.size() == 1 ) { 709 obj2l.put( object, ll.get( 0 ) ); } 711 } 712 713 } 714 715 void changeListener( T object, LookListener oldListener, LookListener newListener ) { 716 717 if ( object == null ) { 718 if ( allObjectListeners != null ) { 719 allObjectListeners.remove( oldListener ); 720 allObjectListeners.add( newListener ); 721 } 722 return; 723 } 724 725 if ( obj2l == null ) { 726 return; 727 } 728 729 Object l = obj2l.get( object ); 730 731 if ( l == oldListener ) { 732 obj2l.put( object, newListener == null ? PLACE_HOLDER : newListener ); } 734 else { List ll = (List )l; 736 if ( ll.remove( oldListener ) ) { 737 ll.add( newListener == null ? PLACE_HOLDER : newListener ); 738 } 739 } 740 741 } 742 743 List getListeners( Object object ) { 744 745 if ( object == null ) { 746 throw new IllegalStateException ( "Reperesented object is null" ); 747 } 748 749 List <LookListener> result = new ArrayList <LookListener>( 4 ); 750 751 if ( allObjectListeners != null ) { 752 result.addAll( allObjectListeners ); 753 } 754 755 if ( obj2l != null ) { 756 Object l = obj2l.get( object ); 757 758 if ( l == null ) { 759 } 762 else if ( l instanceof LookListener ) { 763 result.add((LookListener) l); 764 } 765 else if ( l == PLACE_HOLDER ) { 766 } 768 else { 769 for( Iterator it = ((List )l).iterator(); it.hasNext(); ) { 771 Object listener = it.next(); 772 if ( listener != PLACE_HOLDER ) { 773 result.add((LookListener) listener); 774 } 775 } 776 } 777 } 778 779 return result; 780 } 781 782 783 int getListenersCount( Object object ) { 784 785 if ( object == null ) { 786 throw new IllegalStateException ( "Reperesented object is null" ); 787 } 788 789 if ( obj2l != null ) { 790 Object l = obj2l.get( object ); 791 792 if ( l == null ) { 793 return 0; 794 } 795 else if ( l == PLACE_HOLDER || l instanceof LookListener ) { 796 return 1; 797 } 798 else { 799 return ((List )l).size(); 800 } 801 } 802 803 return 0; 804 805 } 806 807 Collection <T> getAllObjects() { 808 if ( obj2l == null ) { 809 return null; 810 } 811 else { 812 return obj2l.keySet(); 813 } 814 } 815 816 } 817 818 819 820 } 821 | Popular Tags |