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: package javax.swing.tree;
  39: 
  40: import java.awt.Color;
  41: import java.awt.Component;
  42: import java.awt.Dimension;
  43: import java.awt.Font;
  44: import java.awt.Graphics;
  45: import java.awt.Rectangle;
  46: 
  47: import javax.swing.Icon;
  48: import javax.swing.JLabel;
  49: import javax.swing.JTree;
  50: import javax.swing.UIDefaults;
  51: import javax.swing.UIManager;
  52: import javax.swing.plaf.UIResource;
  53: 
  54: /**
  55:  * DefaultTreeCellRenderer
  56:  * 
  57:  * @author Andrew Selkirk
  58:  */
  59: public class DefaultTreeCellRenderer
  60:         extends JLabel
  61:         implements TreeCellRenderer
  62: {
  63:     // -------------------------------------------------------------
  64:     // Variables --------------------------------------------------
  65:     // -------------------------------------------------------------
  66: 
  67:     /**
  68:      * selected
  69:      */
  70:     protected boolean selected;
  71: 
  72:     /**
  73:      * hasFocus
  74:      */
  75:     protected boolean hasFocus;
  76: 
  77:     /**
  78:      * drawsFocusBorderAroundIcon
  79:      */
  80:     private boolean drawsFocusBorderAroundIcon;
  81: 
  82:     /**
  83:      * closedIcon
  84:      */
  85:     protected transient Icon closedIcon;
  86: 
  87:     /**
  88:      * leafIcon
  89:      */
  90:     protected transient Icon leafIcon;
  91: 
  92:     /**
  93:      * openIcon
  94:      */
  95:     protected transient Icon openIcon;
  96: 
  97:     /**
  98:      * textSelectionColor
  99:      */
 100:     protected Color textSelectionColor;
 101: 
 102:     /**
 103:      * textNonSelectionColor
 104:      */
 105:     protected Color textNonSelectionColor;
 106: 
 107:     /**
 108:      * backgroundSelectionColor
 109:      */
 110:     protected Color backgroundSelectionColor;
 111: 
 112:     /**
 113:      * backgroundNonSelectionColor
 114:      */
 115:     protected Color backgroundNonSelectionColor;
 116: 
 117:     /**
 118:      * borderSelectionColor
 119:      */
 120:     protected Color borderSelectionColor;
 121:     
 122: 
 123:     // -------------------------------------------------------------
 124:     // Initialization ---------------------------------------------
 125:     // -------------------------------------------------------------
 126: 
 127:     /**
 128:      * Constructor DefaultTreeCellRenderer
 129:      */
 130:     public DefaultTreeCellRenderer()
 131:     {
 132:         UIDefaults defaults = UIManager.getLookAndFeelDefaults();
 133: 
 134:         setLeafIcon(getDefaultLeafIcon());
 135:         setOpenIcon(getDefaultOpenIcon());
 136:         setClosedIcon(getDefaultClosedIcon());
 137: 
 138:         setTextNonSelectionColor(defaults.getColor("Tree.textForeground"));
 139:         setTextSelectionColor(defaults.getColor("Tree.selectionForeground"));
 140:         setBackgroundNonSelectionColor(defaults
 141:                 .getColor("Tree.nonSelectionBackground"));
 142:         setBackgroundSelectionColor(defaults
 143:                 .getColor("Tree.selectionBackground"));
 144:         setBorderSelectionColor(defaults
 145:                 .getColor("Tree.selectionBorderColor"));
 146:     }
 147: 
 148:     // -------------------------------------------------------------
 149:     // Methods ----------------------------------------------------
 150:     // -------------------------------------------------------------
 151: 
 152:     /**
 153:      * getDefaultOpenIcon
 154:      * 
 155:      * @returns Icon
 156:      */
 157:     public Icon getDefaultOpenIcon()
 158:     {
 159:         return UIManager.getLookAndFeelDefaults().getIcon("Tree.openIcon");
 160:     }
 161: 
 162:     /**
 163:      * getDefaultClosedIcon
 164:      * 
 165:      * @returns Icon
 166:      */
 167:     public Icon getDefaultClosedIcon()
 168:     {
 169:         return UIManager.getLookAndFeelDefaults().getIcon("Tree.closedIcon");
 170:     }
 171: 
 172:     /**
 173:      * getDefaultLeafIcon
 174:      * 
 175:      * @returns Icon
 176:      */
 177:     public Icon getDefaultLeafIcon()
 178:     {
 179:         return UIManager.getLookAndFeelDefaults().getIcon("Tree.leafIcon");
 180:     }
 181: 
 182:     /**
 183:      * setOpenIcon
 184:      * 
 185:      * @param value0 TODO
 186:      */
 187:     public void setOpenIcon(Icon i)
 188:     {
 189:         openIcon = i;
 190:     }
 191: 
 192:     /**
 193:      * getOpenIcon
 194:      * 
 195:      * @returns Icon
 196:      */
 197:     public Icon getOpenIcon()
 198:     {
 199:         return openIcon;
 200:     }
 201: 
 202:     /**
 203:      * setClosedIcon
 204:      * 
 205:      * @param value0 TODO
 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 value0 TODO
 226:      */
 227:     public void setLeafIcon(Icon i)
 228:     {
 229:         leafIcon = i;
 230:     }
 231: 
 232:     /**
 233:      * getLeafIcon
 234:      * 
 235:      * @returns Icon
 236:      */
 237:     public Icon getLeafIcon()
 238:     {
 239:         return leafIcon;
 240:     }
 241: 
 242:     /**
 243:      * setTextSelectionColor
 244:      * 
 245:      * @param value0 TODO
 246:      */
 247:     public void setTextSelectionColor(Color c)
 248:     {
 249:         textSelectionColor = c;
 250:     }
 251: 
 252:     /**
 253:      * getTextSelectionColor
 254:      * 
 255:      * @returns Color
 256:      */
 257:     public Color getTextSelectionColor()
 258:     {
 259:         return textSelectionColor;
 260:     }
 261: 
 262:     /**
 263:      * setTextNonSelectionColor
 264:      * 
 265:      * @param value0 TODO
 266:      */
 267:     public void setTextNonSelectionColor(Color c)
 268:     {
 269:         textNonSelectionColor = c;
 270:     }
 271: 
 272:     /**
 273:      * getTextNonSelectionColor
 274:      * 
 275:      * @returns Color
 276:      */
 277:     public Color getTextNonSelectionColor()
 278:     {
 279:         return textNonSelectionColor;
 280:     }
 281: 
 282:     /**
 283:      * setBackgroundSelectionColor
 284:      * 
 285:      * @param value0 TODO
 286:      */
 287:     public void setBackgroundSelectionColor(Color c)
 288:     {
 289:         backgroundSelectionColor = c;
 290:     }
 291: 
 292:     /**
 293:      * getBackgroundSelectionColor
 294:      * 
 295:      * @returns Color
 296:      */
 297:     public Color getBackgroundSelectionColor()
 298:     {
 299:         return backgroundSelectionColor;
 300:     }
 301: 
 302:     /**
 303:      * setBackgroundNonSelectionColor
 304:      * 
 305:      * @param value0 TODO
 306:      */
 307:     public void setBackgroundNonSelectionColor(Color c)
 308:     {
 309:         backgroundNonSelectionColor = c;
 310:     }
 311: 
 312:     /**
 313:      * getBackgroundNonSelectionColor
 314:      * 
 315:      * @returns Color
 316:      */
 317:     public Color getBackgroundNonSelectionColor()
 318:     {
 319:         return backgroundNonSelectionColor;
 320:     }
 321: 
 322:     /**
 323:      * setBorderSelectionColor
 324:      * 
 325:      * @param value0 TODO
 326:      */
 327:     public void setBorderSelectionColor(Color c)
 328:     {
 329:         borderSelectionColor = c;
 330:     }
 331: 
 332:     /**
 333:      * getBorderSelectionColor
 334:      * 
 335:      * @returns Color
 336:      */
 337:     public Color getBorderSelectionColor()
 338:     {
 339:         return borderSelectionColor;
 340:     }
 341: 
 342:     /**
 343:      * setFont
 344:      * 
 345:      * @param value0 TODO
 346:      */
 347:     public void setFont(Font f)
 348:     {
 349:         if (f != null && f instanceof UIResource)
 350:             f = null;
 351:         super.setFont(f);
 352:     }
 353: 
 354:     /**
 355:      * setBackground
 356:      * 
 357:      * @param value0 TODO
 358:      */
 359:     public void setBackground(Color c)
 360:     {
 361:         if (c != null && c instanceof UIResource)
 362:             c = null;
 363:         super.setBackground(c);
 364:     }
 365: 
 366:     /**
 367:      * getTreeCellRendererComponent
 368:      * 
 369:      * @param value0 TODO
 370:      * @param value1 TODO
 371:      * @param value2 TODO
 372:      * @param value3 TODO
 373:      * @param value4 TODO
 374:      * @param value5 TODO
 375:      * @param value6 TODO
 376:      * @returns Component
 377:      */
 378:     public Component getTreeCellRendererComponent(JTree tree, Object val,
 379:             boolean selected, boolean expanded, boolean leaf, int row,
 380:             boolean hasFocus)
 381:     {
 382:         if (val instanceof Icon)
 383:           setIcon((Icon) val);
 384:       else
 385:       {
 386:          setText(val.toString());
 387:          setIcon(null);
 388:          this.selected = selected;
 389:          this.hasFocus = hasFocus;
 390:          setHorizontalAlignment(LEFT);
 391:          setOpaque(true);
 392:          setVerticalAlignment(TOP);
 393:          setEnabled(true);
 394:          super.setFont(UIManager.getLookAndFeelDefaults().getFont("Tree.font"));
 395:       }
 396: 
 397:         if (selected) 
 398:         {
 399:             super.setBackground(getBackgroundSelectionColor());
 400:             setForeground(getTextSelectionColor());
 401:         }
 402:         else
 403:         {
 404:             super.setBackground(getBackgroundNonSelectionColor());
 405:             setForeground(getTextNonSelectionColor());
 406:         }        
 407:         
 408:         return this;
 409:     }
 410:     
 411:     /**
 412:      * getFont
 413:      * 
 414:      * @return the current Font
 415:      */
 416:     public Font getFont()
 417:     {
 418:         return super.getFont();
 419:     }
 420: 
 421:     /**
 422:      * paint
 423:      * 
 424:      * @param value0 TODO
 425:      */
 426:     public void paint(Graphics g)
 427:     {
 428:         super.paint(g);
 429:     }
 430: 
 431:     /**
 432:      * getPreferredSize
 433:      * 
 434:      * @returns Dimension
 435:      */
 436:     public Dimension getPreferredSize()
 437:     {
 438:         return null; // TODO
 439:     } // getPreferredSize()
 440: 
 441:     /**
 442:      * validate
 443:      */
 444:     public void validate()
 445:     {
 446:         // Overridden for performance reasons.
 447:     } // validate()
 448: 
 449:     /**
 450:      * revalidate
 451:      */
 452:     public void revalidate()
 453:     {
 454:         // Overridden for performance reasons.
 455:     } // revalidate()
 456: 
 457:     /**
 458:      * repaint
 459:      * 
 460:      * @param value0 TODO
 461:      * @param value1 TODO
 462:      * @param value2 TODO
 463:      * @param value3 TODO
 464:      * @param value4 TODO
 465:      */
 466:     public void repaint(long value0, int value1, int value2, int value3,
 467:             int value4)
 468:     {
 469:         // Overridden for performance reasons.
 470:     } // repaint()
 471: 
 472:     /**
 473:      * repaint
 474:      * 
 475:      * @param value0 TODO
 476:      */
 477:     public void repaint(Rectangle value0)
 478:     {
 479:         //  Overridden for performance reasons.
 480:     } // repaint()
 481: 
 482:     /**
 483:      * firePropertyChange
 484:      * 
 485:      * @param value0 TODO
 486:      * @param value1 TODO
 487:      * @param value2 TODO
 488:      */
 489:     protected void firePropertyChange(String value0, Object value1,
 490:             Object value2)
 491:     {
 492:         //  Overridden for performance reasons.
 493:     } // firePropertyChange()
 494: 
 495:     /**
 496:      * firePropertyChange
 497:      * 
 498:      * @param value0 TODO
 499:      * @param value1 TODO
 500:      * @param value2 TODO
 501:      */
 502:     public void firePropertyChange(String value0, byte value1, byte value2)
 503:     {
 504:         //  Overridden for performance reasons.
 505:     } // firePropertyChange()
 506: 
 507:     /**
 508:      * firePropertyChange
 509:      * 
 510:      * @param value0 TODO
 511:      * @param value1 TODO
 512:      * @param value2 TODO
 513:      */
 514:     public void firePropertyChange(String value0, char value1, char value2)
 515:     {
 516:         // Overridden for performance reasons.
 517:     } // firePropertyChange()
 518: 
 519:     /**
 520:      * firePropertyChange
 521:      * 
 522:      * @param value0 TODO
 523:      * @param value1 TODO
 524:      * @param value2 TODO
 525:      */
 526:     public void firePropertyChange(String value0, short value1, short value2)
 527:     {
 528:         //  Overridden for performance reasons.
 529:     } // firePropertyChange()
 530: 
 531:     /**
 532:      * firePropertyChange
 533:      * 
 534:      * @param value0 TODO
 535:      * @param value1 TODO
 536:      * @param value2 TODO
 537:      */
 538:     public void firePropertyChange(String value0, int value1, int value2)
 539:     {
 540:         // Overridden for performance reasons.
 541:     } // firePropertyChange()
 542: 
 543:     /**
 544:      * firePropertyChange
 545:      * 
 546:      * @param value0 TODO
 547:      * @param value1 TODO
 548:      * @param value2 TODO
 549:      */
 550:     public void firePropertyChange(String value0, long value1, long value2)
 551:     {
 552:         //  Overridden for performance reasons.
 553:     } // firePropertyChange()
 554: 
 555:     /**
 556:      * firePropertyChange
 557:      * 
 558:      * @param value0 TODO
 559:      * @param value1 TODO
 560:      * @param value2 TODO
 561:      */
 562:     public void firePropertyChange(String value0, float value1, float value2)
 563:     {
 564:         //  Overridden for performance reasons.
 565:     } // firePropertyChange()
 566: 
 567:     /**
 568:      * firePropertyChange
 569:      * 
 570:      * @param value0 TODO
 571:      * @param value1 TODO
 572:      * @param value2 TODO
 573:      */
 574:     public void firePropertyChange(String value0, double value1, double value2)
 575:     {
 576:         //  Overridden for performance reasons.
 577:     } // firePropertyChange()
 578: 
 579:     /**
 580:      * firePropertyChange
 581:      * 
 582:      * @param value0 TODO
 583:      * @param value1 TODO
 584:      * @param value2 TODO
 585:      */
 586:     public void firePropertyChange(String value0, boolean v1, boolean v2)
 587:     {
 588:         //  Overridden for performance reasons.
 589:     } // firePropertyChange()
 590: 
 591: } // DefaultTreeCellRenderer