GNU Classpath (0.96.1) | |
Frames | No Frames |
1: /* DefaultTreeCellRenderer.java 2: Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.swing.tree; 40: 41: import java.awt.Color; 42: import java.awt.Component; 43: import java.awt.Dimension; 44: import java.awt.Font; 45: import java.awt.Graphics; 46: import java.awt.Rectangle; 47: 48: import javax.swing.Icon; 49: import javax.swing.JLabel; 50: import javax.swing.JTree; 51: import javax.swing.LookAndFeel; 52: import javax.swing.UIManager; 53: import javax.swing.plaf.UIResource; 54: 55: /** 56: * A default implementation of the {@link TreeCellRenderer} interface. 57: * 58: * @author Andrew Selkirk 59: */ 60: public class DefaultTreeCellRenderer 61: extends JLabel 62: implements TreeCellRenderer 63: { 64: 65: /** 66: * A flag indicating the current selection status. 67: */ 68: protected boolean selected; 69: 70: /** 71: * A flag indicating the current focus status. 72: */ 73: protected boolean hasFocus; 74: 75: /** 76: * Indicates if the focus border is also drawn around the icon. 77: */ 78: private boolean drawsFocusBorderAroundIcon; 79: 80: /** 81: * The icon used to represent non-leaf nodes that are closed. 82: * 83: * @see #setClosedIcon(Icon) 84: */ 85: protected transient Icon closedIcon; 86: 87: /** 88: * The icon used to represent leaf nodes. 89: * 90: * @see #setLeafIcon(Icon) 91: */ 92: protected transient Icon leafIcon; 93: 94: /** 95: * The icon used to represent non-leaf nodes that are open. 96: * 97: * @see #setOpenIcon(Icon) 98: */ 99: protected transient Icon openIcon; 100: 101: /** 102: * The color used for text in selected cells. 103: * 104: * @see #setTextSelectionColor(Color) 105: */ 106: protected Color textSelectionColor; 107: 108: /** 109: * The color used for text in non-selected cells. 110: * 111: * @see #setTextNonSelectionColor(Color) 112: */ 113: protected Color textNonSelectionColor; 114: 115: /** 116: * The background color for selected cells. 117: * 118: * @see #setBackgroundSelectionColor(Color) 119: */ 120: protected Color backgroundSelectionColor; 121: 122: /** 123: * The background color for non-selected cells. 124: * 125: * @see #setBackgroundNonSelectionColor(Color) 126: */ 127: protected Color backgroundNonSelectionColor; 128: 129: /** 130: * The border color for selected tree cells. 131: * 132: * @see #setBorderSelectionColor(Color) 133: */ 134: protected Color borderSelectionColor; 135: 136: /** 137: * Creates a new tree cell renderer with defaults appropriate for the 138: * current {@link LookAndFeel}. 139: */ 140: public DefaultTreeCellRenderer() 141: { 142: setLeafIcon(getDefaultLeafIcon()); 143: setOpenIcon(getDefaultOpenIcon()); 144: setClosedIcon(getDefaultClosedIcon()); 145: 146: setTextNonSelectionColor(UIManager.getColor("Tree.textForeground")); 147: setTextSelectionColor(UIManager.getColor("Tree.selectionForeground")); 148: setBackgroundNonSelectionColor(UIManager.getColor("Tree.textBackground")); 149: setBackgroundSelectionColor(UIManager.getColor("Tree.selectionBackground")); 150: setBorderSelectionColor(UIManager.getColor("Tree.selectionBorderColor")); 151: Object val = UIManager.get("Tree.drawsFocusBorderAroundIcon"); 152: drawsFocusBorderAroundIcon = val != null && ((Boolean) val).booleanValue(); 153: } 154: 155: /** 156: * Returns the default icon for non-leaf tree cells that are open (expanded). 157: * The icon is fetched from the defaults table for the current 158: * {@link LookAndFeel} using the key <code>Tree.openIcon</code>. 159: * 160: * @return The default icon. 161: */ 162: public Icon getDefaultOpenIcon() 163: { 164: return UIManager.getIcon("Tree.openIcon"); 165: } 166: 167: /** 168: * Returns the default icon for non-leaf tree cells that are closed (not 169: * expanded). The icon is fetched from the defaults table for the current 170: * {@link LookAndFeel} using the key <code>Tree.closedIcon</code>. 171: * 172: * @return The default icon. 173: */ 174: public Icon getDefaultClosedIcon() 175: { 176: return UIManager.getIcon("Tree.closedIcon"); 177: } 178: 179: /** 180: * Returns the default icon for leaf tree cells. The icon is fetched from 181: * the defaults table for the current {@link LookAndFeel} using the key 182: * <code>Tree.leafIcon</code>. 183: * 184: * @return The default icon. 185: */ 186: public Icon getDefaultLeafIcon() 187: { 188: return UIManager.getIcon("Tree.leafIcon"); 189: } 190: 191: /** 192: * Sets the icon to be displayed for non-leaf nodes that are open (expanded). 193: * Set this to <code>null</code> if no icon is required. 194: * 195: * @param icon the icon (<code>null</code> permitted). 196: * 197: * @see #getOpenIcon() 198: */ 199: public void setOpenIcon(Icon icon) 200: { 201: openIcon = icon; 202: } 203: 204: /** 205: * Returns the icon displayed for non-leaf nodes that are open (expanded). 206: * The default value is initialised from the {@link LookAndFeel}. 207: * 208: * @return The open icon (possibly <code>null</code>). 209: * 210: * @see #setOpenIcon(Icon) 211: */ 212: public Icon getOpenIcon() 213: { 214: return openIcon; 215: } 216: 217: /** 218: * Sets the icon to be displayed for non-leaf nodes that are closed. Set 219: * this to <code>null</code> if no icon is required. 220: * 221: * @param icon the icon (<code>null</code> permitted). 222: * 223: * @see #getClosedIcon() 224: */ 225: public void setClosedIcon(Icon icon) 226: { 227: closedIcon = icon; 228: } 229: 230: /** 231: * Returns the icon displayed for non-leaf nodes that are closed. The 232: * default value is initialised from the {@link LookAndFeel}. 233: * 234: * @return The closed icon (possibly <code>null</code>). 235: * 236: * @see #setClosedIcon(Icon) 237: */ 238: public Icon getClosedIcon() 239: { 240: return closedIcon; 241: } 242: 243: /** 244: * Sets the icon to be displayed for leaf nodes. Set this to 245: * <code>null</code> if no icon is required. 246: * 247: * @param icon the icon (<code>null</code> permitted). 248: * 249: * @see #getLeafIcon() 250: */ 251: public void setLeafIcon(Icon icon) 252: { 253: leafIcon = icon; 254: } 255: 256: /** 257: * Returns the icon displayed for leaf nodes. The default value is 258: * initialised from the {@link LookAndFeel}. 259: * 260: * @return The leaf icon (possibly <code>null</code>). 261: * 262: * @see #setLeafIcon(Icon) 263: */ 264: public Icon getLeafIcon() 265: { 266: return leafIcon; 267: } 268: 269: /** 270: * Sets the text color for tree cells that are selected. 271: * 272: * @param c the color (<code>null</code> permitted). 273: * 274: * @see #getTextSelectionColor() 275: */ 276: public void setTextSelectionColor(Color c) 277: { 278: textSelectionColor = c; 279: } 280: 281: /** 282: * Returns the text color for tree cells that are selected. 283: * The default value is obtained from the {@link LookAndFeel} defaults 284: * table using the key <code>Tree.selectionForeground</code>. 285: * 286: * @return The text color for tree cells that are selected. 287: * 288: * @see #setTextSelectionColor(Color) 289: */ 290: public Color getTextSelectionColor() 291: { 292: return textSelectionColor; 293: } 294: 295: /** 296: * Sets the text color for tree cells that are not selected. 297: * 298: * @param c the color (<code>null</code> permitted). 299: * 300: * @see #getTextNonSelectionColor() 301: */ 302: public void setTextNonSelectionColor(Color c) 303: { 304: textNonSelectionColor = c; 305: } 306: 307: /** 308: * Returns the text color for tree cells that are not selected. 309: * The default value is obtained from the {@link LookAndFeel} defaults 310: * table using the key <code>Tree.selectionForeground</code>. 311: * 312: * @return The background color for tree cells that are not selected. 313: * 314: * @see #setTextgroundNonSelectionColor(Color) 315: */ 316: public Color getTextNonSelectionColor() 317: { 318: return textNonSelectionColor; 319: } 320: 321: /** 322: * Sets the background color for tree cells that are selected. 323: * 324: * @param c the color (<code>null</code> permitted). 325: * 326: * @see #getBackgroundSelectionColor() 327: */ 328: public void setBackgroundSelectionColor(Color c) 329: { 330: backgroundSelectionColor = c; 331: } 332: 333: /** 334: * Returns the background color for tree cells that are selected. 335: * The default value is obtained from the {@link LookAndFeel} defaults 336: * table using the key <code>Tree.selectionBackground</code>. 337: * 338: * @return The background color for tree cells that are selected. 339: * 340: * @see #setBackgroundSelectionColor(Color) 341: */ 342: public Color getBackgroundSelectionColor() 343: { 344: return backgroundSelectionColor; 345: } 346: 347: /** 348: * Sets the background color for tree cells that are not selected. 349: * 350: * @param c the color (<code>null</code> permitted). 351: * 352: * @see #getBackgroundNonSelectionColor() 353: */ 354: public void setBackgroundNonSelectionColor(Color c) 355: { 356: backgroundNonSelectionColor = c; 357: } 358: 359: /** 360: * Returns the background color for tree cells that are not selected. 361: * The default value is obtained from the {@link LookAndFeel} defaults 362: * table using the key <code>Tree.textBackground</code>. 363: * 364: * @return The background color for tree cells that are not selected. 365: * 366: * @see #setBackgroundNonSelectionColor(Color) 367: */ 368: public Color getBackgroundNonSelectionColor() 369: { 370: return backgroundNonSelectionColor; 371: } 372: 373: /** 374: * Sets the border color for tree cells that are selected. 375: * 376: * @param c the color (<code>null</code> permitted). 377: * 378: * @see #getBorderSelectionColor() 379: */ 380: public void setBorderSelectionColor(Color c) 381: { 382: borderSelectionColor = c; 383: } 384: 385: /** 386: * Returns the border color for tree cells that are selected. 387: * The default value is obtained from the {@link LookAndFeel} defaults 388: * table using the key <code>Tree.selectionBorderColor</code>. 389: * 390: * @return The border color for tree cells that are selected. 391: * 392: * @see #setBorderSelectionColor(Color) 393: */ 394: public Color getBorderSelectionColor() 395: { 396: return borderSelectionColor; 397: } 398: 399: /** 400: * Sets the font. 401: * 402: * @param f the font. 403: * 404: * @see #getFont() 405: */ 406: public void setFont(Font f) 407: { 408: if (f != null && f instanceof UIResource) 409: f = null; 410: super.setFont(f); 411: } 412: 413: /** 414: * Sets the background color. 415: * 416: * @param c the color. 417: */ 418: public void setBackground(Color c) 419: { 420: if (c != null && c instanceof UIResource) 421: c = null; 422: super.setBackground(c); 423: } 424: 425: /** 426: * Returns a component (in fact <code>this</code>) that can be used to 427: * render a tree cell with the specified state. 428: * 429: * @param tree the tree that the cell belongs to. 430: * @param val the cell value. 431: * @param selected indicates whether or not the cell is selected. 432: * @param expanded indicates whether or not the cell is expanded. 433: * @param leaf indicates whether or not the cell is a leaf in the tree. 434: * @param row the row index. 435: * @param hasFocus indicates whether or not the cell has the focus. 436: * 437: * @return <code>this</code>. 438: */ 439: public Component getTreeCellRendererComponent(JTree tree, Object val, 440: boolean selected, 441: boolean expanded, boolean leaf, 442: int row, boolean hasFocus) 443: { 444: if (leaf) 445: setIcon(getLeafIcon()); 446: else if (expanded) 447: setIcon(getOpenIcon()); 448: else 449: setIcon(getClosedIcon()); 450: 451: setText(val.toString()); 452: this.selected = selected; 453: this.hasFocus = hasFocus; 454: setHorizontalAlignment(LEFT); 455: setOpaque(false); 456: setVerticalAlignment(CENTER); 457: setEnabled(true); 458: super.setFont(UIManager.getFont("Tree.font")); 459: 460: if (selected) 461: { 462: super.setBackground(getBackgroundSelectionColor()); 463: setForeground(getTextSelectionColor()); 464: 465: if (hasFocus) 466: setBorderSelectionColor(UIManager.getLookAndFeelDefaults(). 467: getColor("Tree.selectionBorderColor")); 468: else 469: setBorderSelectionColor(null); 470: } 471: else 472: { 473: super.setBackground(getBackgroundNonSelectionColor()); 474: setForeground(getTextNonSelectionColor()); 475: setBorderSelectionColor(null); 476: } 477: 478: return this; 479: } 480: 481: /** 482: * Returns the current font. 483: * 484: * @return The current font. 485: * 486: * @see #setFont(Font) 487: */ 488: public Font getFont() 489: { 490: return super.getFont(); 491: } 492: 493: /** 494: * Paints the value. The background is filled based on selected. 495: * 496: * @param g the graphics device. 497: */ 498: public void paint(Graphics g) 499: { 500: // Determine background color. 501: Color bgColor; 502: if (selected) 503: bgColor = getBackgroundSelectionColor(); 504: else 505: { 506: bgColor = getBackgroundNonSelectionColor(); 507: if (bgColor == null) 508: bgColor = getBackground(); 509: } 510: // Paint background. 511: int xOffset = -1; 512: if (bgColor != null) 513: { 514: Icon i = getIcon(); 515: xOffset = getXOffset(); 516: g.setColor(bgColor); 517: g.fillRect(xOffset, 0, getWidth() - xOffset, getHeight()); 518: } 519: 520: if (hasFocus) 521: { 522: if (drawsFocusBorderAroundIcon) 523: xOffset = 0; 524: else if (xOffset == -1) 525: xOffset = getXOffset(); 526: paintFocus(g, xOffset, 0, getWidth() - xOffset, getHeight()); 527: } 528: super.paint(g); 529: } 530: 531: /** 532: * Paints the focus indicator. 533: */ 534: private void paintFocus(Graphics g, int x, int y, int w, int h) 535: { 536: Color col = getBorderSelectionColor(); 537: if (col != null) 538: { 539: g.setColor(col); 540: g.drawRect(x, y, w - 1, h - 1); 541: } 542: } 543: 544: /** 545: * Determines the X offset of the label that is caused by 546: * the icon. 547: * 548: * @return the X offset of the label 549: */ 550: private int getXOffset() 551: { 552: Icon i = getIcon(); 553: int offs = 0; 554: if (i != null && getText() != null) 555: offs = i.getIconWidth() + Math.max(0, getIconTextGap() - 1); 556: return offs; 557: } 558: 559: /** 560: * Returns the preferred size of the cell. 561: * 562: * @return The preferred size of the cell. 563: */ 564: public Dimension getPreferredSize() 565: { 566: Dimension size = super.getPreferredSize(); 567: size.width += 3; 568: return size; 569: } 570: 571: /** 572: * For performance reasons, this method is overridden to do nothing. 573: */ 574: public void validate() 575: { 576: // Overridden for performance reasons. 577: } 578: 579: /** 580: * For performance reasons, this method is overridden to do nothing. 581: */ 582: public void revalidate() 583: { 584: // Overridden for performance reasons. 585: } 586: 587: /** 588: * For performance reasons, this method is overridden to do nothing. 589: * 590: * @param tm ignored 591: * @param x coordinate of the region to mark as dirty 592: * @param y coordinate of the region to mark as dirty 593: * @param width dimension of the region to mark as dirty 594: * @param height dimension of the region to mark as dirty 595: */ 596: public void repaint(long tm, int x, int y, int width, int height) 597: { 598: // Overridden for performance reasons. 599: } 600: 601: /** 602: * For performance reasons, this method is overridden to do nothing. 603: * 604: * @param area the area to repaint. 605: */ 606: public void repaint(Rectangle area) 607: { 608: // Overridden for performance reasons. 609: } 610: 611: /** 612: * For performance reasons, this method is overridden to do nothing. 613: * 614: * @param name the property name. 615: * @param oldValue the old value. 616: * @param newValue the new value. 617: */ 618: protected void firePropertyChange(String name, Object oldValue, 619: Object newValue) 620: { 621: // Overridden for performance reasons. 622: } 623: 624: /** 625: * For performance reasons, this method is overridden to do nothing. 626: * 627: * @param name the property name. 628: * @param oldValue the old value. 629: * @param newValue the new value. 630: */ 631: public void firePropertyChange(String name, byte oldValue, byte newValue) 632: { 633: // Overridden for performance reasons. 634: } 635: 636: /** 637: * For performance reasons, this method is overridden to do nothing. 638: * 639: * @param name the property name. 640: * @param oldValue the old value. 641: * @param newValue the new value. 642: */ 643: public void firePropertyChange(String name, char oldValue, char newValue) 644: { 645: // Overridden for performance reasons. 646: } 647: 648: /** 649: * For performance reasons, this method is overridden to do nothing. 650: * 651: * @param name the property name. 652: * @param oldValue the old value. 653: * @param newValue the new value. 654: */ 655: public void firePropertyChange(String name, short oldValue, short newValue) 656: { 657: // Overridden for performance reasons. 658: } 659: 660: /** 661: * For performance reasons, this method is overridden to do nothing. 662: * 663: * @param name the property name. 664: * @param oldValue the old value. 665: * @param newValue the new value. 666: */ 667: public void firePropertyChange(String name, int oldValue, int newValue) 668: { 669: // Overridden for performance reasons. 670: } 671: 672: /** 673: * For performance reasons, this method is overridden to do nothing. 674: * 675: * @param name the property name. 676: * @param oldValue the old value. 677: * @param newValue the new value. 678: */ 679: public void firePropertyChange(String name, long oldValue, long newValue) 680: { 681: // Overridden for performance reasons. 682: } 683: 684: /** 685: * For performance reasons, this method is overridden to do nothing. 686: * 687: * @param name the property name. 688: * @param oldValue the old value. 689: * @param newValue the new value. 690: */ 691: public void firePropertyChange(String name, float oldValue, float newValue) 692: { 693: // Overridden for performance reasons. 694: } 695: 696: /** 697: * For performance reasons, this method is overridden to do nothing. 698: * 699: * @param name the property name. 700: * @param oldValue the old value. 701: * @param newValue the new value. 702: */ 703: public void firePropertyChange(String name, double oldValue, double newValue) 704: { 705: // Overridden for performance reasons. 706: } 707: 708: /** 709: * For performance reasons, this method is overridden to do nothing. 710: * 711: * @param name the property name. 712: * @param oldValue the old value. 713: * @param newValue the new value. 714: */ 715: public void firePropertyChange(String name, boolean oldValue, 716: boolean newValue) 717: { 718: // Overridden for performance reasons. 719: } 720: 721: }
GNU Classpath (0.96.1) |