GNU Classpath (0.20) | |
Frames | No Frames |
1: /* DefaultTreeCellRenderer.java 2: Copyright (C) 2002, 2004 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.FontMetrics; 46: import java.awt.Graphics; 47: import java.awt.Insets; 48: import java.awt.Rectangle; 49: 50: import javax.swing.border.Border; 51: import javax.swing.Icon; 52: import javax.swing.JLabel; 53: import javax.swing.JTree; 54: import javax.swing.UIManager; 55: import javax.swing.SwingUtilities; 56: import javax.swing.plaf.UIResource; 57: 58: /** 59: * DefaultTreeCellRenderer 60: * 61: * @author Andrew Selkirk 62: */ 63: public class DefaultTreeCellRenderer 64: extends JLabel 65: implements TreeCellRenderer 66: { 67: // ------------------------------------------------------------- 68: // Variables -------------------------------------------------- 69: // ------------------------------------------------------------- 70: 71: /** 72: * selected 73: */ 74: protected boolean selected; 75: 76: /** 77: * hasFocus 78: */ 79: protected boolean hasFocus; 80: 81: /** 82: * drawsFocusBorderAroundIcon 83: */ 84: private boolean drawsFocusBorderAroundIcon; 85: 86: /** 87: * closedIcon 88: */ 89: protected transient Icon closedIcon; 90: 91: /** 92: * leafIcon 93: */ 94: protected transient Icon leafIcon; 95: 96: /** 97: * openIcon 98: */ 99: protected transient Icon openIcon; 100: 101: /** 102: * textSelectionColor 103: */ 104: protected Color textSelectionColor; 105: 106: /** 107: * textNonSelectionColor 108: */ 109: protected Color textNonSelectionColor; 110: 111: /** 112: * backgroundSelectionColor 113: */ 114: protected Color backgroundSelectionColor; 115: 116: /** 117: * backgroundNonSelectionColor 118: */ 119: protected Color backgroundNonSelectionColor; 120: 121: /** 122: * borderSelectionColor 123: */ 124: protected Color borderSelectionColor; 125: 126: // ------------------------------------------------------------- 127: // Initialization --------------------------------------------- 128: // ------------------------------------------------------------- 129: 130: /** 131: * Constructor DefaultTreeCellRenderer 132: */ 133: public DefaultTreeCellRenderer() 134: { 135: setLeafIcon(getDefaultLeafIcon()); 136: setOpenIcon(getDefaultOpenIcon()); 137: setClosedIcon(getDefaultClosedIcon()); 138: 139: setTextNonSelectionColor(UIManager.getColor("Tree.textForeground")); 140: setTextSelectionColor(UIManager.getColor("Tree.selectionForeground")); 141: setBackgroundNonSelectionColor(UIManager.getColor("Tree.nonSelectionBackground")); 142: setBackgroundSelectionColor(UIManager.getColor("Tree.selectionBackground")); 143: setBorderSelectionColor(UIManager.getColor("Tree.selectionBorderColor")); 144: } 145: 146: // ------------------------------------------------------------- 147: // Methods ---------------------------------------------------- 148: // ------------------------------------------------------------- 149: 150: /** 151: * getDefaultOpenIcon 152: * 153: * @returns Icon 154: */ 155: public Icon getDefaultOpenIcon() 156: { 157: return UIManager.getIcon("Tree.openIcon"); 158: } 159: 160: /** 161: * getDefaultClosedIcon 162: * 163: * @returns Icon 164: */ 165: public Icon getDefaultClosedIcon() 166: { 167: return UIManager.getIcon("Tree.closedIcon"); 168: } 169: 170: /** 171: * getDefaultLeafIcon 172: * 173: * @returns Icon 174: */ 175: public Icon getDefaultLeafIcon() 176: { 177: return UIManager.getIcon("Tree.leafIcon"); 178: } 179: 180: /** 181: * setOpenIcon 182: * 183: * @param i 184: * the icon. 185: */ 186: public void setOpenIcon(Icon i) 187: { 188: openIcon = i; 189: } 190: 191: /** 192: * getOpenIcon 193: * 194: * @returns Icon 195: */ 196: public Icon getOpenIcon() 197: { 198: return openIcon; 199: } 200: 201: /** 202: * setClosedIcon 203: * 204: * @param i 205: * the icon. 206: */ 207: public void setClosedIcon(Icon i) 208: { 209: closedIcon = i; 210: } 211: 212: /** 213: * getClosedIcon 214: * 215: * @returns Icon 216: */ 217: public Icon getClosedIcon() 218: { 219: return closedIcon; 220: } 221: 222: /** 223: * setLeafIcon 224: * 225: * @param i 226: * the icon. 227: */ 228: public void setLeafIcon(Icon i) 229: { 230: leafIcon = i; 231: } 232: 233: /** 234: * getLeafIcon 235: * 236: * @returns Icon 237: */ 238: public Icon getLeafIcon() 239: { 240: return leafIcon; 241: } 242: 243: /** 244: * setTextSelectionColor 245: * 246: * @param c 247: * the color. 248: */ 249: public void setTextSelectionColor(Color c) 250: { 251: textSelectionColor = c; 252: } 253: 254: /** 255: * getTextSelectionColor 256: * 257: * @returns Color 258: */ 259: public Color getTextSelectionColor() 260: { 261: return textSelectionColor; 262: } 263: 264: /** 265: * setTextNonSelectionColor 266: * 267: * @param c 268: * the color. 269: */ 270: public void setTextNonSelectionColor(Color c) 271: { 272: textNonSelectionColor = c; 273: } 274: 275: /** 276: * getTextNonSelectionColor 277: * 278: * @returns Color 279: */ 280: public Color getTextNonSelectionColor() 281: { 282: return textNonSelectionColor; 283: } 284: 285: /** 286: * setBackgroundSelectionColor 287: * 288: * @param c 289: * the color. 290: */ 291: public void setBackgroundSelectionColor(Color c) 292: { 293: backgroundSelectionColor = c; 294: } 295: 296: /** 297: * getBackgroundSelectionColor 298: * 299: * @returns Color 300: */ 301: public Color getBackgroundSelectionColor() 302: { 303: return backgroundSelectionColor; 304: } 305: 306: /** 307: * setBackgroundNonSelectionColor 308: * 309: * @param c 310: * the color. 311: */ 312: public void setBackgroundNonSelectionColor(Color c) 313: { 314: backgroundNonSelectionColor = c; 315: } 316: 317: /** 318: * getBackgroundNonSelectionColor 319: * 320: * @returns Color 321: */ 322: public Color getBackgroundNonSelectionColor() 323: { 324: return backgroundNonSelectionColor; 325: } 326: 327: /** 328: * setBorderSelectionColor 329: * 330: * @param c 331: * the color. 332: */ 333: public void setBorderSelectionColor(Color c) 334: { 335: borderSelectionColor = c; 336: } 337: 338: /** 339: * getBorderSelectionColor 340: * 341: * @returns Color 342: */ 343: public Color getBorderSelectionColor() 344: { 345: return borderSelectionColor; 346: } 347: 348: /** 349: * setFont 350: * 351: * @param f 352: * the font. 353: */ 354: public void setFont(Font f) 355: { 356: if (f != null && f instanceof UIResource) 357: f = null; 358: super.setFont(f); 359: } 360: 361: /** 362: * setBackground 363: * 364: * @param c 365: * the color. 366: */ 367: public void setBackground(Color c) 368: { 369: if (c != null && c instanceof UIResource) 370: c = null; 371: super.setBackground(c); 372: } 373: 374: /** 375: * getTreeCellRendererComponent 376: * 377: * @param tree 378: * TODO 379: * @param val 380: * TODO 381: * @param selected 382: * TODO 383: * @param expanded 384: * TODO 385: * @param leaf 386: * TODO 387: * @param row 388: * TODO 389: * @param hasFocus 390: * TODO 391: * @returns Component 392: */ 393: public Component getTreeCellRendererComponent(JTree tree, Object val, 394: boolean selected, 395: boolean expanded, boolean leaf, 396: int row, boolean hasFocus) 397: { 398: if (leaf) 399: setIcon(getLeafIcon()); 400: else if (expanded) 401: setIcon(getOpenIcon()); 402: else 403: setIcon(getClosedIcon()); 404: 405: setText(val.toString()); 406: this.selected = selected; 407: this.hasFocus = hasFocus; 408: setHorizontalAlignment(LEFT); 409: setOpaque(false); 410: setVerticalAlignment(TOP); 411: setEnabled(true); 412: super.setFont(UIManager.getFont("Tree.font")); 413: 414: if (selected) 415: { 416: super.setBackground(getBackgroundSelectionColor()); 417: setForeground(getTextSelectionColor()); 418: 419: if (hasFocus) 420: setBorderSelectionColor(UIManager.getLookAndFeelDefaults(). 421: getColor("Tree.selectionBorderColor")); 422: else 423: setBorderSelectionColor(null); 424: } 425: else 426: { 427: super.setBackground(getBackgroundNonSelectionColor()); 428: setForeground(getTextNonSelectionColor()); 429: setBorderSelectionColor(null); 430: } 431: 432: return this; 433: } 434: 435: /** 436: * getFont 437: * 438: * @return the current Font 439: */ 440: public Font getFont() 441: { 442: return super.getFont(); 443: } 444: 445: /** 446: * Paints the value. The background is filled based on selected. 447: * 448: * @param g 449: * the graphics device. 450: */ 451: public void paint(Graphics g) 452: { 453: // paint background 454: Rectangle vr = new Rectangle(); 455: Rectangle ir = new Rectangle(); 456: Rectangle tr = new Rectangle(); 457: 458: Insets insets = new Insets(0, 0, 0, 0); 459: Border border = UIManager.getBorder("Tree.selectionBorder"); 460: if (border != null) 461: insets = border.getBorderInsets(this); 462: 463: FontMetrics fm = getToolkit().getFontMetrics(getFont()); 464: SwingUtilities.layoutCompoundLabel(((JLabel) this), fm, getText(), 465: getIcon(), getVerticalAlignment(), 466: getHorizontalAlignment(), 467: getVerticalTextPosition(), 468: getHorizontalTextPosition(), vr, ir, tr, 469: getIconTextGap()); 470: 471: g.setColor(super.getBackground()); 472: g.fillRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom); 473: 474: // paint border 475: Color b = getBorderSelectionColor(); 476: if (b != null) 477: { 478: g.setColor(b); 479: g.drawRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom); 480: } 481: super.paint(g); 482: } 483: 484: /** 485: * returns the preferred size of the cell. 486: * 487: * @returns Dimension 488: */ 489: public Dimension getPreferredSize() 490: { 491: Rectangle vr = new Rectangle(); 492: Rectangle ir = new Rectangle(); 493: Rectangle tr = new Rectangle(); 494: 495: FontMetrics fm = getToolkit().getFontMetrics(getFont()); 496: SwingUtilities.layoutCompoundLabel(((JLabel) this), fm, getText(), 497: getIcon(), getVerticalAlignment(), 498: getHorizontalAlignment(), 499: getVerticalTextPosition(), 500: getHorizontalTextPosition(), vr, ir, tr, 501: getIconTextGap()); 502: Rectangle cr = ir.union(tr); 503: return new Dimension(cr.width, cr.height); 504: } // getPreferredSize() 505: 506: /** 507: * validate 508: */ 509: public void validate() 510: { 511: // Overridden for performance reasons. 512: } // validate() 513: 514: /** 515: * revalidate 516: */ 517: public void revalidate() 518: { 519: // Overridden for performance reasons. 520: } // revalidate() 521: 522: /** 523: * repaint 524: * 525: * @param value0 526: * TODO 527: * @param value1 528: * TODO 529: * @param value2 530: * TODO 531: * @param value3 532: * TODO 533: * @param value4 534: * TODO 535: */ 536: public void repaint(long value0, int value1, int value2, int value3, 537: int value4) 538: { 539: // Overridden for performance reasons. 540: } // repaint() 541: 542: /** 543: * repaint 544: * 545: * @param value0 546: * TODO 547: */ 548: public void repaint(Rectangle value0) 549: { 550: // Overridden for performance reasons. 551: } // repaint() 552: 553: /** 554: * firePropertyChange 555: * 556: * @param value0 557: * TODO 558: * @param value1 559: * TODO 560: * @param value2 561: * TODO 562: */ 563: protected void firePropertyChange(String value0, Object value1, Object value2) 564: { 565: // Overridden for performance reasons. 566: } // firePropertyChange() 567: 568: /** 569: * firePropertyChange 570: * 571: * @param value0 572: * TODO 573: * @param value1 574: * TODO 575: * @param value2 576: * TODO 577: */ 578: public void firePropertyChange(String value0, byte value1, byte value2) 579: { 580: // Overridden for performance reasons. 581: } // firePropertyChange() 582: 583: /** 584: * firePropertyChange 585: * 586: * @param value0 587: * TODO 588: * @param value1 589: * TODO 590: * @param value2 591: * TODO 592: */ 593: public void firePropertyChange(String value0, char value1, char value2) 594: { 595: // Overridden for performance reasons. 596: } // firePropertyChange() 597: 598: /** 599: * firePropertyChange 600: * 601: * @param value0 602: * TODO 603: * @param value1 604: * TODO 605: * @param value2 606: * TODO 607: */ 608: public void firePropertyChange(String value0, short value1, short value2) 609: { 610: // Overridden for performance reasons. 611: } // firePropertyChange() 612: 613: /** 614: * firePropertyChange 615: * 616: * @param value0 617: * TODO 618: * @param value1 619: * TODO 620: * @param value2 621: * TODO 622: */ 623: public void firePropertyChange(String value0, int value1, int value2) 624: { 625: // Overridden for performance reasons. 626: } // firePropertyChange() 627: 628: /** 629: * firePropertyChange 630: * 631: * @param value0 632: * TODO 633: * @param value1 634: * TODO 635: * @param value2 636: * TODO 637: */ 638: public void firePropertyChange(String value0, long value1, long value2) 639: { 640: // Overridden for performance reasons. 641: } // firePropertyChange() 642: 643: /** 644: * firePropertyChange 645: * 646: * @param value0 647: * TODO 648: * @param value1 649: * TODO 650: * @param value2 651: * TODO 652: */ 653: public void firePropertyChange(String value0, float value1, float value2) 654: { 655: // Overridden for performance reasons. 656: } // firePropertyChange() 657: 658: /** 659: * firePropertyChange 660: * 661: * @param value0 TODO 662: * @param value1 TODO 663: * @param value2 TODO 664: */ 665: public void firePropertyChange(String value0, double value1, double value2) 666: { 667: // Overridden for performance reasons. 668: } // firePropertyChange() 669: 670: /** 671: * firePropertyChange 672: * 673: * @param name the property name. 674: * @param v1 the old value. 675: * @param v2 the new value. 676: */ 677: public void firePropertyChange(String name, boolean v1, boolean v2) 678: { 679: // Overridden for performance reasons. 680: } // firePropertyChange() 681: 682: } // DefaultTreeCellRenderer
GNU Classpath (0.20) |