001: /* swing1.1 */
002:
003: package jp.gr.java_conf.tame.swing.tree;
004:
005: import java.io.*;
006: import java.awt.*;
007: import java.awt.event.*;
008: import javax.swing.*;
009: import javax.swing.tree.*;
010: import javax.swing.plaf.ColorUIResource;
011:
012: /**
013: * @version 1.0 01/11/99
014: */
015: public class CheckRenderer extends JPanel implements TreeCellRenderer {
016: protected JCheckBox check;
017: protected TreeLabel label;
018:
019: public CheckRenderer() {
020: setLayout(null);
021: add(check = new JCheckBox());
022: add(label = new TreeLabel());
023: check.setBackground(UIManager.getColor("Tree.textBackground"));
024: }
025:
026: public Component getTreeCellRendererComponent(JTree tree,
027: Object value, boolean isSelected, boolean expanded,
028: boolean leaf, int row, boolean hasFocus) {
029: String stringValue = tree.convertValueToText(value, isSelected,
030: expanded, leaf, row, hasFocus);
031: setEnabled(tree.isEnabled());
032: check.setSelected(((CheckNode) value).isSelected());
033: label.setFont(tree.getFont());
034: label.setText(stringValue);
035: label.setSelected(isSelected);
036: label.setFocus(hasFocus);
037: if (leaf) {
038: label.setIcon(UIManager.getIcon("Tree.leafIcon"));
039: } else if (expanded) {
040: label.setIcon(UIManager.getIcon("Tree.openIcon"));
041: } else {
042: label.setIcon(UIManager.getIcon("Tree.closedIcon"));
043: }
044: return this ;
045: }
046:
047: public Dimension getPreferredSize() {
048: Dimension d_check = check.getPreferredSize();
049: Dimension d_label = label.getPreferredSize();
050: return new Dimension(d_check.width + d_label.width,
051: (d_check.height < d_label.height ? d_label.height
052: : d_check.height));
053: }
054:
055: public void doLayout() {
056: Dimension d_check = check.getPreferredSize();
057: Dimension d_label = label.getPreferredSize();
058: int y_check = 0;
059: int y_label = 0;
060: if (d_check.height < d_label.height) {
061: y_check = (d_label.height - d_check.height) / 2;
062: } else {
063: y_label = (d_check.height - d_label.height) / 2;
064: }
065: check.setLocation(0, y_check);
066: check.setBounds(0, y_check, d_check.width, d_check.height);
067: label.setLocation(d_check.width, y_label);
068: label.setBounds(d_check.width, y_label, d_label.width,
069: d_label.height);
070: }
071:
072: public void setBackground(Color color) {
073: if (color instanceof ColorUIResource)
074: color = null;
075: super .setBackground(color);
076: }
077:
078: class TreeLabel extends JLabel {
079: boolean isSelected;
080: boolean hasFocus;
081:
082: TreeLabel() {
083: }
084:
085: public void setBackground(Color color) {
086: if (color instanceof ColorUIResource)
087: color = null;
088: super .setBackground(color);
089: }
090:
091: public void paint(Graphics g) {
092: String str;
093: if ((str = getText()) != null) {
094: if (0 < str.length()) {
095: if (isSelected) {
096: g.setColor(UIManager
097: .getColor("Tree.selectionBackground"));
098: } else {
099: g.setColor(UIManager
100: .getColor("Tree.textBackground"));
101: }
102: Dimension d = getPreferredSize();
103: int imageOffset = 0;
104: Icon currentI = getIcon();
105: if (currentI != null) {
106: imageOffset = currentI.getIconWidth()
107: + Math.max(0, getIconTextGap() - 1);
108: }
109: g.fillRect(imageOffset, 0, d.width - 1
110: - imageOffset, d.height);
111: if (hasFocus) {
112: g.setColor(UIManager
113: .getColor("Tree.selectionBorderColor"));
114: g.drawRect(imageOffset, 0, d.width - 1
115: - imageOffset, d.height - 1);
116: }
117: }
118: }
119: super .paint(g);
120: }
121:
122: public Dimension getPreferredSize() {
123: Dimension retDimension = super .getPreferredSize();
124: if (retDimension != null) {
125: retDimension = new Dimension(retDimension.width + 3,
126: retDimension.height);
127: }
128: return retDimension;
129: }
130:
131: void setSelected(boolean isSelected) {
132: this .isSelected = isSelected;
133: }
134:
135: void setFocus(boolean hasFocus) {
136: this.hasFocus = hasFocus;
137: }
138: }
139: }
|