home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / SampleTree / src / SampleTreeCellRenderer.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  5.4 KB  |  158 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)SampleTreeCellRenderer.java    1.15 02/06/13
  38.  */
  39.  
  40. import javax.swing.Icon;
  41. import javax.swing.ImageIcon;
  42. import javax.swing.JLabel;
  43. import javax.swing.JTree;
  44. import javax.swing.tree.TreeCellRenderer;
  45. import javax.swing.tree.DefaultMutableTreeNode;
  46. import java.awt.Component;
  47. import java.awt.Color;
  48. import java.awt.Font;
  49. import java.awt.Graphics;
  50.  
  51. public class SampleTreeCellRenderer extends JLabel implements TreeCellRenderer
  52. {
  53.     /** Font used if the string to be displayed isn't a font. */
  54.     static protected Font             defaultFont;
  55.     /** Icon to use when the item is collapsed. */
  56.     static protected ImageIcon        collapsedIcon;
  57.     /** Icon to use when the item is expanded. */
  58.     static protected ImageIcon        expandedIcon;
  59.  
  60.     /** Color to use for the background when selected. */
  61.     static protected final Color SelectedBackgroundColor = Color.yellow;//new Color(0, 0, 128);
  62.  
  63.     static
  64.     {
  65.     try {
  66.         defaultFont = new Font("SansSerif", 0, 12);
  67.     } catch (Exception e) {}
  68.     try {
  69.         collapsedIcon = new ImageIcon("images/collapsed.gif");
  70.         expandedIcon = new ImageIcon("images/expanded.gif");
  71.     } catch (Exception e) {
  72.         System.out.println("Couldn't load images: " + e);
  73.     }
  74.     }
  75.  
  76.     /** Whether or not the item that was last configured is selected. */
  77.     protected boolean            selected;
  78.  
  79.     /**
  80.       * This is messaged from JTree whenever it needs to get the size
  81.       * of the component or it wants to draw it.
  82.       * This attempts to set the font based on value, which will be
  83.       * a TreeNode.
  84.       */
  85.     public Component getTreeCellRendererComponent(JTree tree, Object value,
  86.                       boolean selected, boolean expanded,
  87.                       boolean leaf, int row,
  88.                           boolean hasFocus) {
  89.     Font            font;
  90.     String          stringValue = tree.convertValueToText(value, selected,
  91.                        expanded, leaf, row, hasFocus);
  92.  
  93.     /* Set the text. */
  94.     setText(stringValue);
  95.     /* Tooltips used by the tree. */
  96.     setToolTipText(stringValue);
  97.  
  98.     /* Set the image. */
  99.     if(expanded)
  100.         setIcon(expandedIcon);
  101.     else if(!leaf)
  102.         setIcon(collapsedIcon);
  103.     else
  104.         setIcon(null);
  105.  
  106.     /* Set the color and the font based on the SampleData userObject. */
  107.     SampleData         userObject = (SampleData)((DefaultMutableTreeNode)value)
  108.                                     .getUserObject();
  109.     if(hasFocus)
  110.         setForeground(Color.cyan);
  111.     else
  112.         setForeground(userObject.getColor());
  113.     if(userObject.getFont() == null)
  114.         setFont(defaultFont);
  115.     else
  116.         setFont(userObject.getFont());
  117.  
  118.     /* Update the selected flag for the next paint. */
  119.     this.selected = selected;
  120.  
  121.     return this;
  122.     }
  123.  
  124.     /**
  125.       * paint is subclassed to draw the background correctly.  JLabel
  126.       * currently does not allow backgrounds other than white, and it
  127.       * will also fill behind the icon.  Something that isn't desirable.
  128.       */
  129.     public void paint(Graphics g) {
  130.     Color            bColor;
  131.     Icon             currentI = getIcon();
  132.  
  133.     if(selected)
  134.         bColor = SelectedBackgroundColor;
  135.     else if(getParent() != null)
  136.         /* Pick background color up from parent (which will come from
  137.            the JTree we're contained in). */
  138.         bColor = getParent().getBackground();
  139.     else
  140.         bColor = getBackground();
  141.     g.setColor(bColor);
  142.     if(currentI != null && getText() != null) {
  143.         int          offset = (currentI.getIconWidth() + getIconTextGap());
  144.  
  145.             if (getComponentOrientation().isLeftToRight()) {
  146.                 g.fillRect(offset, 0, getWidth() - 1 - offset,
  147.                            getHeight() - 1);
  148.             }
  149.             else {
  150.                 g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
  151.             }
  152.     }
  153.     else
  154.         g.fillRect(0, 0, getWidth()-1, getHeight()-1);
  155.     super.paint(g);
  156.     }
  157. }
  158.