1 19 package org.openide.nodes; 20 21 import org.openide.util.HelpCtx; 22 import org.openide.util.Lookup; 23 import org.openide.util.Utilities; 24 import org.openide.util.actions.SystemAction; 25 import org.openide.util.datatransfer.*; 26 27 import java.awt.Image ; 28 import java.awt.datatransfer.Transferable ; 29 30 import java.beans.PropertyChangeEvent ; 31 import java.beans.PropertyChangeListener ; 32 33 import java.io.IOException ; 34 35 import java.text.MessageFormat ; 36 37 import java.util.*; 38 39 import javax.swing.Action ; 40 import javax.swing.event.ChangeEvent ; 41 import javax.swing.event.ChangeListener ; 42 import org.openide.util.Exceptions; 43 44 45 53 public class AbstractNode extends Node { 54 57 private static final String [] icons = { 58 59 "", 62 "32", 65 "", 68 "32", 71 "Open", 74 "Open32", 77 "Open", 80 "Open32" }; 83 84 87 private static final int ICON_BASE = -1; 88 89 90 private static final int OPENED_ICON_BASE = 3; 91 92 93 private static final PasteType[] NO_PASTE_TYPES = { }; 94 95 96 private static final NewType[] NO_NEW_TYPES = { }; 97 98 99 private static final String DEFAULT_ICON_BASE = "org/openide/nodes/defaultNode"; private static final String DEFAULT_ICON_EXTENSION = ".gif"; private static final String DEFAULT_ICON = DEFAULT_ICON_BASE + DEFAULT_ICON_EXTENSION; 103 private static final WeakHashMap<Class , Object > overridesGetDefaultAction = new WeakHashMap<Class , Object >(32); 105 106 116 protected MessageFormat displayFormat; 117 118 119 private Action preferredAction; 120 121 122 private String iconBase = DEFAULT_ICON_BASE; 123 124 125 private String iconExtension = DEFAULT_ICON_EXTENSION; 126 127 128 private Object lookup; 129 130 131 private Sheet sheet; 132 133 138 @Deprecated protected SystemAction[] systemActions; 139 private SheetAndCookieListener sheetCookieL = null; 140 141 144 public AbstractNode(Children children) { 145 this(children, null); 146 } 147 148 162 public AbstractNode(Children children, Lookup lookup) { 163 super(children, lookup); 164 165 super.setName(""); } 172 173 175 AbstractNode(CookieSet set) { 176 super(Children.LEAF); 177 lookup = set; 178 } 179 180 186 public Node cloneNode() { 187 try { 188 if (this instanceof Cloneable ) { 189 return (Node) clone(); 190 } 191 } catch (CloneNotSupportedException ex) { 192 } 193 194 return new FilterNode(this); 195 } 196 197 202 public void setName(String s) { 203 super.setName(s); 204 205 MessageFormat mf = displayFormat; 206 207 if (mf != null) { 208 setDisplayName(mf.format(new Object [] { s })); 209 } else { 210 fireDisplayNameChange(null, null); 215 } 216 } 217 218 230 @Deprecated 231 public void setIconBase(String base) { 232 setIconBaseWithExtension(base, DEFAULT_ICON_EXTENSION); 233 } 234 235 258 public final void setIconBaseWithExtension(String baseExt) { 259 int lastDot = baseExt.lastIndexOf('.'); 260 int lastSlash = baseExt.lastIndexOf('/'); 261 262 if ((lastSlash > lastDot) || (lastDot == -1)) { setIconBaseWithExtension(baseExt, ""); 264 } else { 265 String base = baseExt.substring(0, lastDot); 266 String ext = baseExt.substring(lastDot); 267 setIconBaseWithExtension(base, ext); 268 } 269 270 } 271 272 273 private final void setIconBaseWithExtension(String base, String extension) { 274 if (base.equals(iconBase) && extension.equals(iconExtension)) { 275 return; 276 } 277 278 this.iconBase = base; 279 this.iconExtension = extension; 280 fireIconChange(); 281 fireOpenedIconChange(); 282 } 283 284 285 286 292 public Image getIcon(int type) { 293 return findIcon(type, ICON_BASE); 294 } 295 296 302 public Image getOpenedIcon(int type) { 303 return findIcon(type, OPENED_ICON_BASE); 304 } 305 306 public HelpCtx getHelpCtx() { 307 return HelpCtx.DEFAULT_HELP; 308 } 309 310 314 private Image findIcon(int type, int ib) { 315 String res = iconBase + icons[type + ib] + iconExtension; 316 Image im = Utilities.loadImage(res, true); 317 318 if (im != null) { 319 return im; 320 } 321 322 res = iconBase + icons[java.beans.BeanInfo.ICON_COLOR_16x16 + ib] + iconExtension; 324 325 im = Utilities.loadImage(res, true); 326 327 if (im != null) { 328 return im; 329 } 330 331 if (ib == OPENED_ICON_BASE) { 332 return findIcon(type, ICON_BASE); 334 } 335 336 return getDefaultIcon(); 338 } 339 340 Image getDefaultIcon() { 341 Image i = Utilities.loadImage(DEFAULT_ICON, true); 342 343 if (i == null) { 344 throw new MissingResourceException("No default icon", "", DEFAULT_ICON); } 346 347 return i; 348 } 349 350 353 public boolean canRename() { 354 return false; 355 } 356 357 360 public boolean canDestroy() { 361 return false; 362 } 363 364 371 protected final synchronized void setSheet(Sheet s) { 372 setSheetImpl(s); 373 firePropertySetsChange(null, null); 374 } 375 376 private synchronized void setSheetImpl(Sheet s) { 377 if (sheetCookieL == null) { 378 sheetCookieL = new SheetAndCookieListener(); 379 } 380 381 if (sheet != null) { 382 sheet.removePropertyChangeListener(sheetCookieL); 383 } 384 385 s.addPropertyChangeListener(sheetCookieL); 386 sheet = s; 387 } 388 389 401 protected Sheet createSheet() { 402 return new Sheet(); 403 } 404 405 411 protected final synchronized Sheet getSheet() { 412 if (sheet != null) { 413 return sheet; 414 } 415 416 setSheetImpl(createSheet()); 417 418 return sheet; 419 } 420 421 426 public PropertySet[] getPropertySets() { 427 Sheet s = getSheet(); 428 429 return s.toArray(); 430 } 431 432 boolean propertySetsAreKnown() { 433 return (sheet != null); 434 } 435 436 442 public Transferable clipboardCopy() throws IOException { 443 return NodeTransfer.transferable(this, NodeTransfer.CLIPBOARD_COPY); 444 } 445 446 452 public Transferable clipboardCut() throws IOException { 453 return NodeTransfer.transferable(this, NodeTransfer.CLIPBOARD_CUT); 454 } 455 456 464 public Transferable drag() throws IOException { 465 return clipboardCopy(); 466 } 467 468 471 public boolean canCopy() { 472 return true; 473 } 474 475 478 public boolean canCut() { 479 return false; 480 } 481 482 496 protected void createPasteTypes(Transferable t, List<PasteType> s) { 497 NodeTransfer.Paste p = NodeTransfer.findPaste(t); 498 499 if (p != null) { 500 s.addAll(Arrays.asList(p.types(this))); 502 } 503 } 504 505 511 public final PasteType[] getPasteTypes(Transferable t) { 512 List<PasteType> s = new LinkedList<PasteType>(); 513 createPasteTypes(t, s); 514 515 return s.toArray(NO_PASTE_TYPES); 516 } 517 518 529 public PasteType getDropType(Transferable t, int action, int index) { 530 java.util.List <PasteType> s = new LinkedList<PasteType>(); 531 createPasteTypes(t, s); 532 533 return s.isEmpty() ? null : s.get(0); 534 } 535 536 539 public NewType[] getNewTypes() { 540 return NO_NEW_TYPES; 541 } 542 543 545 private boolean overridesAMethod(String name, Class [] arguments) { 546 try { 548 java.lang.reflect.Method m = getClass().getMethod(name, arguments); 549 550 if (m.getDeclaringClass() != AbstractNode.class) { 551 return true; 553 } 554 } catch (NoSuchMethodException ex) { 555 Exceptions.printStackTrace(ex); 556 } 557 558 return false; 559 } 560 561 567 public Action getPreferredAction() { 568 boolean delegate = false; 569 570 Class c = getClass(); 571 572 if (c != AbstractNode.class) { 573 synchronized (overridesGetDefaultAction) { 574 Object in = overridesGetDefaultAction.get(c); 575 576 if (in == this) { 577 overridesGetDefaultAction.put(c, Boolean.FALSE); 581 582 return preferredAction; 583 } 584 585 Boolean b; 586 587 if (in == null) { 588 b = overridesAMethod("getDefaultAction", new Class [0]) ? Boolean.TRUE : Boolean.FALSE; 590 if (b.booleanValue()) { 591 overridesGetDefaultAction.put(c, this); 593 getDefaultAction(); 594 595 if (overridesGetDefaultAction.get(c) == this) { 596 overridesGetDefaultAction.put(c, b); 598 } 599 } else { 600 overridesGetDefaultAction.put(c, b); 601 } 602 } else { 603 b = (Boolean ) in; 604 } 605 606 delegate = b.booleanValue(); 607 } 608 } 609 610 return delegate ? getDefaultAction() : preferredAction; 611 } 612 613 617 @Deprecated 618 public SystemAction getDefaultAction() { 619 Action a = getPreferredAction(); 620 621 if (a instanceof SystemAction) { 622 return (SystemAction) a; 623 } 624 625 return null; 626 } 627 628 632 @Deprecated 633 public void setDefaultAction(SystemAction action) { 634 preferredAction = action; 635 } 636 637 643 @Deprecated 644 public SystemAction[] getActions() { 645 if (systemActions == null) { 646 systemActions = createActions(); 647 648 if (systemActions == null) { 649 systemActions = super.getActions(); 650 } 651 } 652 653 return systemActions; 654 } 655 656 662 @Deprecated 663 protected SystemAction[] createActions() { 664 return null; 665 } 666 667 670 public boolean hasCustomizer() { 671 return false; 672 } 673 674 677 public java.awt.Component getCustomizer() { 678 return null; 679 } 680 681 691 @Deprecated 692 protected final synchronized void setCookieSet(CookieSet s) { 693 if (internalLookup(false) != null) { 694 throw new IllegalStateException ("CookieSet cannot be used when lookup is associated with the node"); } 696 697 if (sheetCookieL == null) { 698 sheetCookieL = new SheetAndCookieListener(); 699 } 700 701 CookieSet cookieSet = (CookieSet) lookup; 702 703 if (cookieSet != null) { 704 cookieSet.removeChangeListener(sheetCookieL); 705 } 706 707 s.addChangeListener(sheetCookieL); 708 lookup = s; 709 710 fireCookieChange(); 711 } 712 713 719 protected final CookieSet getCookieSet() { 720 if (internalLookup(false) != null) { 721 throw new IllegalStateException ("CookieSet cannot be used when lookup is associated with the node"); } 723 724 CookieSet s = (CookieSet) lookup; 725 726 if (s != null) { 727 return s; 728 } 729 730 synchronized (this) { 731 if (lookup != null) { 732 return (CookieSet) lookup; 733 } 734 735 setCookieSet(new CookieSet()); 737 738 return (CookieSet) lookup; 739 } 740 } 741 742 748 @Override 749 public <T extends Node.Cookie> T getCookie(Class <T> type) { 750 if (lookup instanceof CookieSet) { 751 CookieSet c = (CookieSet) lookup; 752 753 return c.getCookie(type); 754 } else { 755 return super.getCookie(type); 756 } 757 } 758 759 762 public Handle getHandle() { 763 return DefaultHandle.createHandle(this); 764 } 765 766 767 private final class SheetAndCookieListener implements PropertyChangeListener , ChangeListener { 768 SheetAndCookieListener() { 769 } 770 771 public void propertyChange(PropertyChangeEvent ev) { 772 AbstractNode.this.firePropertySetsChange(null, null); 773 } 774 775 public void stateChanged(ChangeEvent ev) { 776 AbstractNode.this.fireCookieChange(); 777 } 778 } 779 } 780 | Popular Tags |