01: package org.gui4j.core.swing;
02:
03: import java.awt.Color;
04: import java.awt.Component;
05:
06: import javax.swing.JCheckBox;
07: import javax.swing.JTable;
08: import javax.swing.SwingConstants;
09: import javax.swing.UIManager;
10: import javax.swing.border.Border;
11: import javax.swing.table.TableCellRenderer;
12:
13: public class BooleanTableCellRenderer extends JCheckBox implements
14: TableCellRenderer {
15:
16: private final Border noFocusBorder;
17:
18: // background color used when the cell is not selected
19: private Color unselectedBackground;
20:
21: public BooleanTableCellRenderer(Border noFocusBorder) {
22: super ();
23: setHorizontalAlignment(SwingConstants.CENTER);
24: this .noFocusBorder = noFocusBorder;
25:
26: setBorder(noFocusBorder);
27: setBorderPainted(true);
28: setOpaque(true);
29: }
30:
31: public Component getTableCellRendererComponent(JTable table,
32: Object value, boolean isSelected, boolean hasFocus,
33: int row, int column) {
34:
35: // is the table cell selected?
36: if (isSelected) {
37: setForeground(table.getSelectionForeground());
38: setBackground(table.getSelectionBackground());
39: } else {
40: setForeground(table.getForeground());
41: setBackground(table.getBackground());
42: }
43:
44: // should the checkmark be set?
45: setSelected((value != null && ((Boolean) value).booleanValue()));
46:
47: // should we paint a focus border?
48: if (hasFocus) {
49: setBorder(UIManager
50: .getBorder("Table.focusCellHighlightBorder"));
51: if (table.isCellEditable(row, column)) {
52: super .setForeground(UIManager
53: .getColor("Table.focusCellForeground"));
54: super .setBackground(UIManager
55: .getColor("Table.focusCellBackground"));
56: }
57: } else {
58: setBorder(noFocusBorder);
59: }
60:
61: return this ;
62: }
63:
64: public void setUnselectedBackground(Color unselectedBackground) {
65: this .unselectedBackground = unselectedBackground;
66: }
67:
68: /**
69: * This method returns the background color to use for an unselected table cell.
70: * This method is provided for custom swing UI delegates that need to know the
71: * original background color, e.g. for rendering the checkbox icon independently
72: * from the cell being selected or not.
73: * @return The currently active background color for non-selected table cells.
74: */
75: public Color getUnselectedBackground() {
76: return unselectedBackground;
77: }
78:
79: }
|