Skip to main content
summaryrefslogtreecommitdiffstats
blob: 51ce1a83e91501cc1af91b7ea4552022e9340f0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*******************************************************************************
 * Copyright (c) 2007, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import org.eclipse.jpt.utility.internal.swing.TableCellEditorAdapter.ImmediateEditListener;

/**
 * Make the cell look like a check box.
 */
public class CheckBoxTableCellRenderer implements TableCellEditorAdapter.Renderer {

	/** the component used to paint the cell */
	private final JCheckBox checkBox;
	
	/** the listener to be notified on an immediate edit */
	protected TableCellEditorAdapter.ImmediateEditListener immediateEditListener;

	/** "normal" border - assume the default table "focus" border is 1 pixel thick */
	private static final Border NO_FOCUS_BORDER = BorderFactory.createEmptyBorder(1, 1, 1, 1);


	// ********** constructors/initialization **********

	/**
	 * Construct a cell renderer with no label or icon.
	 */
	public CheckBoxTableCellRenderer() {
		super();
		this.checkBox = this.buildCheckBox();
		// by default, check boxes do not paint their borders
		this.checkBox.setBorderPainted(true);
		// this setting is recommended for check boxes inside of trees and tables
		this.checkBox.setBorderPaintedFlat(true);
	}

	/**
	 * Construct a cell renderer with the specified text and icon,
	 * either of which may be null.
	 */
	public CheckBoxTableCellRenderer(String text, Icon icon) {
		this();
		this.setText(text);
		this.setIcon(icon);
	}

	/**
	 * Construct a cell renderer with the specified text.
	 */
	public CheckBoxTableCellRenderer(String text) {
		this(text, null);
	}

	/**
	 * Construct a cell renderer with the specified icon.
	 */
	public CheckBoxTableCellRenderer(Icon icon) {
		this(null, icon);
	}

	protected JCheckBox buildCheckBox() {
		JCheckBox cb = new JCheckBox();
		cb.addActionListener(this.buildActionListener());
		return cb;
	}
	
	private ActionListener buildActionListener() {
		return new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (CheckBoxTableCellRenderer.this.immediateEditListener != null) {
					CheckBoxTableCellRenderer.this.immediateEditListener.immediateEdit();
				}
			}
		};
	}


	// ********** TableCellRenderer implementation **********

	public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {
	    this.checkBox.setHorizontalAlignment(SwingConstants.CENTER);
		this.checkBox.setComponentOrientation(table.getComponentOrientation());
		this.checkBox.setFont(table.getFont());
		this.checkBox.setEnabled(table.isEnabled());

		this.checkBox.setForeground(this.foregroundColor(table, value, selected, hasFocus, row, column));
		this.checkBox.setBackground(this.backgroundColor(table, value, selected, hasFocus, row, column));
		// once the colors are set, calculate opaque setting
		this.checkBox.setOpaque(this.cellIsOpaqueIn(table, value, selected, hasFocus, row, column));
		this.checkBox.setBorder(this.border(table, value, selected, hasFocus, row, column));

		this.setValue(value);
		return this.checkBox;
	}

	/**
	 * Return the cell's foreground color.
	 */
	protected Color foregroundColor(JTable table, @SuppressWarnings("unused") Object value, boolean selected, boolean hasFocus, int row, int column) {
		if (selected) {
			if (hasFocus && table.isCellEditable(row, column)) {
				return UIManager.getColor("Table.focusCellForeground"); //$NON-NLS-1$
			}
			return table.getSelectionForeground();
		}
		return table.getForeground();
	}

	/**
	 * Return the cell's background color.
	 */
	protected Color backgroundColor(JTable table, @SuppressWarnings("unused") Object value, boolean selected, boolean hasFocus, int row, int column) {
		if (selected) {
			if (hasFocus && table.isCellEditable(row, column)) {
				return UIManager.getColor("Table.focusCellBackground"); //$NON-NLS-1$
			}
			return table.getSelectionBackground();
		}
		return table.getBackground();
	}

	/**
	 * Return the cell's border.
	 */
	protected Border border(@SuppressWarnings("unused") JTable table, @SuppressWarnings("unused") Object value, @SuppressWarnings("unused") boolean selected, boolean hasFocus, @SuppressWarnings("unused") int row, @SuppressWarnings("unused") int column) {
		return hasFocus ?  UIManager.getBorder("Table.focusCellHighlightBorder") : NO_FOCUS_BORDER; //$NON-NLS-1$
	}

	/**
	 * Return whether the cell should be opaque in the table.
	 * If the cell's background is the same as the table's background
	 * and table is opaque, we don't need to paint the background -
	 * the table will do it.
	 */
	protected boolean cellIsOpaqueIn(JTable table, @SuppressWarnings("unused") Object value, @SuppressWarnings("unused") boolean selected, @SuppressWarnings("unused") boolean hasFocus, @SuppressWarnings("unused") int row, @SuppressWarnings("unused") int column) {
		Color cellBackground = this.checkBox.getBackground();
		Color tableBackground = table.getBackground();
		return ! (table.isOpaque() && cellBackground.equals(tableBackground));
	}

	/**
	 * Set the check box's value.
	 */
	protected void setValue(Object value) {
		// CR#3999318 - This null check needs to be removed once JDK bug is fixed
		if (value == null) {
			value = Boolean.FALSE;
		}
		this.checkBox.setSelected(((Boolean) value).booleanValue());
	}


	// ********** TableCellEditorAdapter.Renderer implementation **********

	public Object getValue() {
		return Boolean.valueOf(this.checkBox.isSelected());
	}
	
	public void setImmediateEditListener(ImmediateEditListener listener) {
		this.immediateEditListener = listener;
	}

	// ********** public API **********

	/**
	 * Set the check box's text; which by default is blank.
	 */
	public void setText(String text) {
		this.checkBox.setText(text);
	}

	/**
	 * Set the check box's icon; which by default is not present.
	 */
	public void setIcon(Icon icon) {
		this.checkBox.setIcon(icon);
	}

	/**
	 * Return the renderer's preferred height. This allows you
	 * to set the table's row height to something the check box
	 * will look good in....
	 */
	public int preferredHeight() {
		// add in space for the border top and bottom
		return (int) this.checkBox.getPreferredSize().getHeight() + 2;
	}

}

Back to the top