1 /*
2  * @(#)DefaultCellRenderer.java  1.0  April 11, 2004
3  *
4  * Copyright (c) 2004 Werner Randelshofer
5  * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
6  * All rights reserved.
7  *
8  * The copyright of this software is owned by Werner Randelshofer.
9  * You may not use, copy or modify this software, except in
10  * accordance with the license agreement you entered into with
11  * Werner Randelshofer. For details see accompanying license terms.
12  */
13 
14 //package ch.randelshofer.gui;
15 
16 package test;
17 
18 import java.awt.*;
19 import java.io.*;
20 import javax.swing.*;
21 import javax.swing.table.*;
22 import javax.swing.tree.*;
23 import javax.swing.border.*;
24 /**
25  * DefaultCellRenderer.
26  *
27  * @author  Werner Randelshofer
28  * @version 1.0 April 11, 2004 Created.
29  */
30 public class DefaultCellRenderer implements TableCellRenderer, ListCellRenderer {
31     /** The Swing component being rendered. */
32     protected JComponent renderComponent;
33 
34     /**
35      * The delegate class which handles all methods sent from the
36      * <code>CellEditor</code>.
37      */
38     protected RenderDelegate delegate;
39     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
40 
41     /**
42      * Constructs a <code>DefaultCellRenderer</code> that uses a text field.
43      *
44      * @param textField  a <code>JTextField</code> object
45      */
DefaultCellRenderer(final JTextField textField)46     public DefaultCellRenderer(final JTextField textField) {
47         renderComponent = textField;
48         delegate = new RenderDelegate() {
49             public void setValue(Object value) {
50 		textField.setText((value != null) ? value.toString() : "");
51             }
52         };
53     }
54 
55     /**
56      * Constructs a <code>DefaultCellRenderer</code> object that uses a check box.
57      *
58      * @param checkBox  a <code>JCheckBox</code> object
59      */
DefaultCellRenderer(final JCheckBox checkBox)60     public DefaultCellRenderer(final JCheckBox checkBox) {
61         renderComponent = checkBox;
62         delegate = new RenderDelegate() {
63             public void setValue(Object value) {
64             	boolean selected = false;
65 		if (value instanceof Boolean) {
66 		    selected = ((Boolean)value).booleanValue();
67 		}
68 		else if (value instanceof String) {
69 		    selected = value.equals("true");
70 		}
71 		checkBox.setSelected(selected);
72             }
73         };
74     }
75 
76     /**
77      * Constructs a <code>DefaultCellRenderer</code> object that uses a
78      * combo box.
79      *
80      * @param comboBox  a <code>JComboBox</code> object
81      */
DefaultCellRenderer(final JComboBox comboBox)82     public DefaultCellRenderer(final JComboBox comboBox) {
83         renderComponent = comboBox;
84 	comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
85         delegate = new RenderDelegate() {
86 	    public void setValue(Object value) {
87 		comboBox.setSelectedItem(value);
88             }
89         };
90     }
91 
getTableCellRendererComponent(JTable parent, Object value, boolean isSelected, boolean hasFocus, int row, int column)92     public Component getTableCellRendererComponent(JTable parent, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
93         delegate.setValue(value);
94         if (hasFocus) {
95             renderComponent.setBackground(UIManager.getColor("Table.focusCellBackground"));
96             renderComponent.setForeground(UIManager.getColor("Table.focusCellForeground"));
97 	    renderComponent.setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
98         } else if (isSelected) {
99             renderComponent.setBackground(parent.getSelectionBackground());
100             renderComponent.setForeground(parent.getSelectionForeground());
101 	    renderComponent.setBorder(noFocusBorder);
102         } else {
103             renderComponent.setBackground(parent.getBackground());
104             renderComponent.setForeground(parent.getForeground());
105 	    renderComponent.setBorder(noFocusBorder);
106         }
107         renderComponent.setFont(parent.getFont());
108 
109         return renderComponent;
110     }
111 
getListCellRendererComponent(JList parent, Object value, int index, boolean isSelected, boolean hasFocus)112     public Component getListCellRendererComponent(JList parent, Object value, int index, boolean isSelected, boolean hasFocus) {
113         delegate.setValue(value);
114 
115         if (isSelected) {
116             renderComponent.setBackground(parent.getSelectionBackground());
117             renderComponent.setForeground(parent.getSelectionForeground());
118         } else {
119             renderComponent.setBackground(parent.getBackground());
120             renderComponent.setForeground(parent.getSelectionForeground());
121         }
122         renderComponent.setFont(parent.getFont());
123 
124         return renderComponent;
125     }
126 
127 
128     /**
129      * The protected <code>RenderDelegate</code> class.
130      */
131     protected interface RenderDelegate {
132        /**
133         * Sets the value of this cell.
134         * @param value the new value of this cell
135         */
setValue(Object value)136     	public void setValue(Object value);
137     }
138 }
139