home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SampleTree / SampleTreeCellRenderer.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  3.9 KB  |  128 lines

  1. /*
  2.  * @(#)SampleTreeCellRenderer.java    1.10 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.Icon;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JLabel;
  18. import javax.swing.JTree;
  19. import javax.swing.tree.TreeCellRenderer;
  20. import javax.swing.tree.DefaultMutableTreeNode;
  21. import java.awt.Component;
  22. import java.awt.Color;
  23. import java.awt.Font;
  24. import java.awt.Graphics;
  25.  
  26. public class SampleTreeCellRenderer extends JLabel implements TreeCellRenderer
  27. {
  28.     /** Font used if the string to be displayed isn't a font. */
  29.     static protected Font             defaultFont;
  30.     /** Icon to use when the item is collapsed. */
  31.     static protected ImageIcon        collapsedIcon;
  32.     /** Icon to use when the item is expanded. */
  33.     static protected ImageIcon        expandedIcon;
  34.  
  35.     /** Color to use for the background when selected. */
  36.     static protected final Color SelectedBackgroundColor = Color.yellow;//new Color(0, 0, 128);
  37.  
  38.     static
  39.     {
  40.     try {
  41.         defaultFont = new Font("SansSerif", 0, 12);
  42.     } catch (Exception e) {}
  43.     try {
  44.         collapsedIcon = new ImageIcon("images/collapsed.gif");
  45.         expandedIcon = new ImageIcon("images/expanded.gif");
  46.     } catch (Exception e) {
  47.         System.out.println("Couldn't load images: " + e);
  48.     }
  49.     }
  50.  
  51.     /** Whether or not the item that was last configured is selected. */
  52.     protected boolean            selected;
  53.  
  54.     /**
  55.       * This is messaged from JTree whenever it needs to get the size
  56.       * of the component or it wants to draw it.
  57.       * This attempts to set the font based on value, which will be
  58.       * a TreeNode.
  59.       */
  60.     public Component getTreeCellRendererComponent(JTree tree, Object value,
  61.                       boolean selected, boolean expanded,
  62.                       boolean leaf, int row,
  63.                           boolean hasFocus) {
  64.     Font            font;
  65.     String          stringValue = tree.convertValueToText(value, selected,
  66.                        expanded, leaf, row, hasFocus);
  67.  
  68.     /* Set the text. */
  69.     setText(stringValue);
  70.     /* Tooltips used by the tree. */
  71.     setToolTipText(stringValue);
  72.  
  73.     /* Set the image. */
  74.     if(expanded)
  75.         setIcon(expandedIcon);
  76.     else if(!leaf)
  77.         setIcon(collapsedIcon);
  78.     else
  79.         setIcon(null);
  80.  
  81.     /* Set the color and the font based on the SampleData userObject. */
  82.     SampleData         userObject = (SampleData)((DefaultMutableTreeNode)value)
  83.                                     .getUserObject();
  84.     if(hasFocus)
  85.         setForeground(Color.cyan);
  86.     else
  87.         setForeground(userObject.getColor());
  88.     if(userObject.getFont() == null)
  89.         setFont(defaultFont);
  90.     else
  91.         setFont(userObject.getFont());
  92.  
  93.     /* Update the selected flag for the next paint. */
  94.     this.selected = selected;
  95.  
  96.     return this;
  97.     }
  98.  
  99.     /**
  100.       * paint is subclassed to draw the background correctly.  JLabel
  101.       * currently does not allow backgrounds other than white, and it
  102.       * will also fill behind the icon.  Something that isn't desirable.
  103.       */
  104.     public void paint(Graphics g) {
  105.     Color            bColor;
  106.     Icon             currentI = getIcon();
  107.  
  108.     if(selected)
  109.         bColor = SelectedBackgroundColor;
  110.     else if(getParent() != null)
  111.         /* Pick background color up from parent (which will come from
  112.            the JTree we're contained in). */
  113.         bColor = getParent().getBackground();
  114.     else
  115.         bColor = getBackground();
  116.     g.setColor(bColor);
  117.     if(currentI != null && getText() != null) {
  118.         int          offset = (currentI.getIconWidth() + getIconTextGap());
  119.  
  120.         g.fillRect(offset, 0, getWidth() - 1 - offset,
  121.                getHeight() - 1);
  122.     }
  123.     else
  124.         g.fillRect(0, 0, getWidth()-1, getHeight()-1);
  125.     super.paint(g);
  126.     }
  127. }
  128.