Source for javax.swing.tree.DefaultTreeCellRenderer

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