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.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(CENTER);
 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 the graphics device.
 449:    */
 450:   public void paint(Graphics g)
 451:   {
 452:     // paint background
 453:     Rectangle vr = new Rectangle();
 454:     Rectangle ir = new Rectangle();
 455:     Rectangle tr = new Rectangle();
 456: 
 457:     Insets insets = new Insets(0, 0, 0, 0);
 458:     Border border = UIManager.getBorder("Tree.selectionBorder");
 459:     if (border != null)
 460:       insets = border.getBorderInsets(this);
 461: 
 462:     FontMetrics fm = getToolkit().getFontMetrics(getFont());
 463:     SwingUtilities.layoutCompoundLabel(((JLabel) this), fm, getText(),
 464:                                        getIcon(), getVerticalAlignment(),
 465:                                        getHorizontalAlignment(),
 466:                                        getVerticalTextPosition(),
 467:                                        getHorizontalTextPosition(), vr, ir, tr,
 468:                                        getIconTextGap());
 469: 
 470:     // Reusing one rectangle.
 471:     Rectangle bounds = getBounds(ir);
 472:     
 473:     bounds.x = tr.x - insets.left;
 474:     bounds.width = tr.width + insets.left+insets.right;
 475:     
 476:     g.setColor(super.getBackground());
 477:     g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
 478: 
 479:     super.paint(g);
 480:     
 481:     // Paint the border of the focused element only (lead selection)
 482:     if (hasFocus)
 483:       {
 484:         Color b = getBorderSelectionColor();
 485:         if (b != null)
 486:           {
 487:             g.setColor(b);
 488:             g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height - 1);
 489:           }
 490:       }
 491:   }
 492: 
 493:   /**
 494:    * returns the preferred size of the cell.
 495:    * 
 496:    * @returns Dimension
 497:    */
 498:   public Dimension getPreferredSize()
 499:   {
 500:     Rectangle vr = new Rectangle();
 501:     Rectangle ir = new Rectangle();
 502:     Rectangle tr = new Rectangle();
 503: 
 504:     FontMetrics fm = getToolkit().getFontMetrics(getFont());
 505:     SwingUtilities.layoutCompoundLabel(((JLabel) this), fm, getText(),
 506:                                        getIcon(), getVerticalAlignment(),
 507:                                        getHorizontalAlignment(),
 508:                                        getVerticalTextPosition(),
 509:                                        getHorizontalTextPosition(), vr, ir, tr,
 510:                                        getIconTextGap());
 511:     Rectangle cr = ir.union(tr);
 512:     return new Dimension(cr.width, cr.height);
 513:   } // getPreferredSize()
 514: 
 515:   /**
 516:    * validate
 517:    */
 518:   public void validate()
 519:   {
 520:     // Overridden for performance reasons.
 521:   } // validate()
 522: 
 523:   /**
 524:    * revalidate
 525:    */
 526:   public void revalidate()
 527:   {
 528:     // Overridden for performance reasons.
 529:   } // revalidate()
 530: 
 531:   /**
 532:    * repaint
 533:    * 
 534:    * @param value0
 535:    *          TODO
 536:    * @param value1
 537:    *          TODO
 538:    * @param value2
 539:    *          TODO
 540:    * @param value3
 541:    *          TODO
 542:    * @param value4
 543:    *          TODO
 544:    */
 545:   public void repaint(long value0, int value1, int value2, int value3,
 546:                       int value4)
 547:   {
 548:     // Overridden for performance reasons.
 549:   } // repaint()
 550: 
 551:   /**
 552:    * repaint
 553:    * 
 554:    * @param value0
 555:    *          TODO
 556:    */
 557:   public void repaint(Rectangle value0)
 558:   {
 559:     // Overridden for performance reasons.
 560:   } // repaint()
 561: 
 562:   /**
 563:    * firePropertyChange
 564:    * 
 565:    * @param value0
 566:    *          TODO
 567:    * @param value1
 568:    *          TODO
 569:    * @param value2
 570:    *          TODO
 571:    */
 572:   protected void firePropertyChange(String value0, Object value1, Object value2)
 573:   {
 574:     // Overridden for performance reasons.
 575:   } // firePropertyChange()
 576: 
 577:   /**
 578:    * firePropertyChange
 579:    * 
 580:    * @param value0
 581:    *          TODO
 582:    * @param value1
 583:    *          TODO
 584:    * @param value2
 585:    *          TODO
 586:    */
 587:   public void firePropertyChange(String value0, byte value1, byte value2)
 588:   {
 589:     // Overridden for performance reasons.
 590:   } // firePropertyChange()
 591: 
 592:   /**
 593:    * firePropertyChange
 594:    * 
 595:    * @param value0
 596:    *          TODO
 597:    * @param value1
 598:    *          TODO
 599:    * @param value2
 600:    *          TODO
 601:    */
 602:   public void firePropertyChange(String value0, char value1, char value2)
 603:   {
 604:     // Overridden for performance reasons.
 605:   } // firePropertyChange()
 606: 
 607:   /**
 608:    * firePropertyChange
 609:    * 
 610:    * @param value0
 611:    *          TODO
 612:    * @param value1
 613:    *          TODO
 614:    * @param value2
 615:    *          TODO
 616:    */
 617:   public void firePropertyChange(String value0, short value1, short value2)
 618:   {
 619:     // Overridden for performance reasons.
 620:   } // firePropertyChange()
 621: 
 622:   /**
 623:    * firePropertyChange
 624:    * 
 625:    * @param value0
 626:    *          TODO
 627:    * @param value1
 628:    *          TODO
 629:    * @param value2
 630:    *          TODO
 631:    */
 632:   public void firePropertyChange(String value0, int value1, int value2)
 633:   {
 634:     // Overridden for performance reasons.
 635:   } // firePropertyChange()
 636: 
 637:   /**
 638:    * firePropertyChange
 639:    * 
 640:    * @param value0
 641:    *          TODO
 642:    * @param value1
 643:    *          TODO
 644:    * @param value2
 645:    *          TODO
 646:    */
 647:   public void firePropertyChange(String value0, long value1, long value2)
 648:   {
 649:     // Overridden for performance reasons.
 650:   } // firePropertyChange()
 651: 
 652:   /**
 653:    * firePropertyChange
 654:    * 
 655:    * @param value0
 656:    *          TODO
 657:    * @param value1
 658:    *          TODO
 659:    * @param value2
 660:    *          TODO
 661:    */
 662:   public void firePropertyChange(String value0, float value1, float value2)
 663:   {
 664:     // Overridden for performance reasons.
 665:   } // firePropertyChange()
 666: 
 667:   /**
 668:    * firePropertyChange
 669:    * 
 670:    * @param value0 TODO
 671:    * @param value1 TODO
 672:    * @param value2 TODO
 673:    */
 674:   public void firePropertyChange(String value0, double value1, double value2)
 675:   {
 676:     //  Overridden for performance reasons.
 677:   } // firePropertyChange()
 678: 
 679:   /**
 680:    * firePropertyChange
 681:    * 
 682:    * @param name the property name.
 683:    * @param v1 the old value.
 684:    * @param v2 the new value.
 685:    */
 686:   public void firePropertyChange(String name, boolean v1, boolean v2)
 687:   {
 688:     //  Overridden for performance reasons.
 689:   } // firePropertyChange()
 690: 
 691: } // DefaultTreeCellRenderer