home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / plaf / basic / BasicTreeCellRenderer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  4.2 KB  |  173 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.Icon;
  4. import com.sun.java.swing.JComponent;
  5. import com.sun.java.swing.JLabel;
  6. import com.sun.java.swing.JTree;
  7. import com.sun.java.swing.UIManager;
  8. import com.sun.java.swing.tree.TreeCellRenderer;
  9. import java.awt.Color;
  10. import java.awt.Component;
  11. import java.awt.Dimension;
  12. import java.awt.Graphics;
  13.  
  14. public class BasicTreeCellRenderer extends JLabel implements TreeCellRenderer {
  15.    protected boolean selected;
  16.    protected transient Icon closedIcon;
  17.    protected transient Icon leafIcon;
  18.    protected transient Icon openIcon;
  19.    protected Color textSelectionColor;
  20.    protected Color textNonSelectionColor;
  21.    protected Color backgroundSelectionColor;
  22.    protected Color backgroundNonSelectionColor;
  23.    protected Color borderSelectionColor;
  24.  
  25.    public BasicTreeCellRenderer() {
  26.       ((JLabel)this).setHorizontalAlignment(2);
  27.       this.setLeafIcon(UIManager.getIcon("Tree.leafIcon"));
  28.       this.setClosedIcon(UIManager.getIcon("Tree.closedIcon"));
  29.       this.setOpenIcon(UIManager.getIcon("Tree.openIcon"));
  30.       this.setTextSelectionColor(UIManager.getColor("Tree.textSelectionColor"));
  31.       this.setTextNonSelectionColor(UIManager.getColor("Tree.textNonSelectionColor"));
  32.       this.setBackgroundSelectionColor(UIManager.getColor("Tree.backgroundSelectionColor"));
  33.       this.setBackgroundNonSelectionColor(UIManager.getColor("Tree.backgroundNonSelectionColor"));
  34.       this.setBorderSelectionColor(UIManager.getColor("Tree.borderSelectionColor"));
  35.    }
  36.  
  37.    public Icon getDefaultOpenIcon() {
  38.       return this.openIcon;
  39.    }
  40.  
  41.    public Icon getDefaultClosedIcon() {
  42.       return this.closedIcon;
  43.    }
  44.  
  45.    public Icon getDefaultLeafIcon() {
  46.       return this.leafIcon;
  47.    }
  48.  
  49.    public void setOpenIcon(Icon var1) {
  50.       this.openIcon = var1;
  51.    }
  52.  
  53.    public Icon getOpenIcon() {
  54.       return this.openIcon;
  55.    }
  56.  
  57.    public void setClosedIcon(Icon var1) {
  58.       this.closedIcon = var1;
  59.    }
  60.  
  61.    public Icon getClosedIcon() {
  62.       return this.closedIcon;
  63.    }
  64.  
  65.    public void setLeafIcon(Icon var1) {
  66.       this.leafIcon = var1;
  67.    }
  68.  
  69.    public Icon getLeafIcon() {
  70.       return this.leafIcon;
  71.    }
  72.  
  73.    public void setTextSelectionColor(Color var1) {
  74.       this.textSelectionColor = var1;
  75.    }
  76.  
  77.    public Color getTextSelectionColor() {
  78.       return this.textSelectionColor;
  79.    }
  80.  
  81.    public void setTextNonSelectionColor(Color var1) {
  82.       this.textNonSelectionColor = var1;
  83.    }
  84.  
  85.    public Color getTextNonSelectionColor() {
  86.       return this.textNonSelectionColor;
  87.    }
  88.  
  89.    public void setBackgroundSelectionColor(Color var1) {
  90.       this.backgroundSelectionColor = var1;
  91.    }
  92.  
  93.    public Color getBackgroundSelectionColor() {
  94.       return this.backgroundSelectionColor;
  95.    }
  96.  
  97.    public void setBackgroundNonSelectionColor(Color var1) {
  98.       this.backgroundNonSelectionColor = var1;
  99.    }
  100.  
  101.    public Color getBackgroundNonSelectionColor() {
  102.       return this.backgroundNonSelectionColor;
  103.    }
  104.  
  105.    public void setBorderSelectionColor(Color var1) {
  106.       this.borderSelectionColor = var1;
  107.    }
  108.  
  109.    public Color getBorderSelectionColor() {
  110.       return this.borderSelectionColor;
  111.    }
  112.  
  113.    public Component getTreeCellRendererComponent(JTree var1, Object var2, boolean var3, boolean var4, boolean var5, int var6, boolean var7) {
  114.       String var8 = var1.convertValueToText(var2, var3, var4, var5, var6, var7);
  115.       ((JLabel)this).setText(var8);
  116.       if (var3) {
  117.          ((Component)this).setForeground(this.getTextSelectionColor());
  118.       } else {
  119.          ((Component)this).setForeground(this.getTextNonSelectionColor());
  120.       }
  121.  
  122.       if (var5) {
  123.          ((JLabel)this).setIcon(this.getLeafIcon());
  124.       } else if (var4) {
  125.          ((JLabel)this).setIcon(this.getOpenIcon());
  126.       } else {
  127.          ((JLabel)this).setIcon(this.getClosedIcon());
  128.       }
  129.  
  130.       this.selected = var3;
  131.       return this;
  132.    }
  133.  
  134.    public void paint(Graphics var1) {
  135.       Color var2;
  136.       if (this.selected) {
  137.          var2 = this.getBackgroundSelectionColor();
  138.       } else {
  139.          var2 = this.getBackgroundNonSelectionColor();
  140.          if (var2 == null) {
  141.             var2 = ((Component)this).getBackground();
  142.          }
  143.       }
  144.  
  145.       if (var2 != null) {
  146.          Icon var3 = ((JLabel)this).getIcon();
  147.          var1.setColor(var2);
  148.          if (var3 != null && ((JLabel)this).getText() != null) {
  149.             int var4 = var3.getIconWidth() + ((JLabel)this).getIconTextGap();
  150.             var1.fillRect(var4, 0, ((JComponent)this).getWidth() - 1 - var4, ((JComponent)this).getHeight() - 1);
  151.          } else {
  152.             var1.fillRect(0, 0, ((JComponent)this).getWidth() - 1, ((JComponent)this).getHeight() - 1);
  153.          }
  154.       }
  155.  
  156.       if (this.selected) {
  157.          var1.setColor(this.getBorderSelectionColor());
  158.          var1.drawRect(0, 0, ((JComponent)this).getWidth() - 1, ((JComponent)this).getHeight() - 1);
  159.       }
  160.  
  161.       super.paint(var1);
  162.    }
  163.  
  164.    public Dimension getPreferredSize() {
  165.       Dimension var1 = super.getPreferredSize();
  166.       if (var1 != null) {
  167.          var1 = new Dimension(var1.width + 3, var1.height);
  168.       }
  169.  
  170.       return var1;
  171.    }
  172. }
  173.