1 package org.simantics.spreadsheet.ui;
4 import java.awt.Component;
5 import java.awt.Rectangle;
6 import java.io.Serializable;
8 import javax.swing.JComponent;
9 import javax.swing.JLabel;
10 import javax.swing.JTable;
11 import javax.swing.SwingConstants;
12 import javax.swing.border.Border;
13 import javax.swing.table.TableCellRenderer;
15 public class ColumnHeaderRenderer extends JLabel implements TableCellRenderer, Serializable {
17 private static final long serialVersionUID = 1L;
21 * An empty <code>Border</code>. This field might not be used. To change the
22 * <code>Border</code> used by this renderer override the
23 * <code>getTableCellRendererComponent</code> method and set the border
24 * of the returned component directly.
27 // We need a place to store the color the JLabel should be returned
28 // to after its foreground and background colors have been set
29 // to the selection background color.
30 // These ivars will be made protected when their names are finalized.
31 private Color unselectedForeground;
32 private Color unselectedBackground;
35 * Creates a default table cell renderer.
37 public ColumnHeaderRenderer() {
40 setHorizontalAlignment(SwingConstants.CENTER);
41 setBackground(DefaultLookup.GRAY);
42 setBorder(getNoFocusBorder());
43 setName("Table.cellRenderer");
46 private Border getNoFocusBorder() {
47 return TableBorder.BORDER;
51 * Overrides <code>JComponent.setForeground</code> to assign
52 * the unselected-foreground color to the specified color.
54 * @param c set the foreground color to this value
56 public void setForeground(Color c) {
57 super.setForeground(c);
58 unselectedForeground = c;
62 * Overrides <code>JComponent.setBackground</code> to assign
63 * the unselected-background color to the specified color.
65 * @param c set the background color to this value
67 public void setBackground(Color c) {
68 super.setBackground(c);
69 unselectedBackground = c;
73 * Notification from the <code>UIManager</code> that the look and feel
75 * Replaces the current UI object with the latest version from the
76 * <code>UIManager</code>.
78 * @see JComponent#updateUI
80 public void updateUI() {
86 // implements javax.swing.table.TableCellRenderer
89 * Returns the default table cell renderer.
91 * During a printing operation, this method will be called with
92 * <code>isSelected</code> and <code>hasFocus</code> values of
93 * <code>false</code> to prevent selection and focus from appearing
94 * in the printed output. To do other customization based on whether
95 * or not the table is being printed, check the return value from
96 * {@link javax.swing.JComponent#isPaintingForPrint()}.
98 * @param table the <code>JTable</code>
99 * @param value the value to assign to the cell at
100 * <code>[row, column]</code>
101 * @param isSelected true if cell is selected
102 * @param hasFocus true if cell has focus
103 * @param row the row of the cell to render
104 * @param column the column of the cell to render
105 * @return the default table cell renderer
106 * @see javax.swing.JComponent#isPaintingForPrint()
108 public Component getTableCellRendererComponent(JTable table, Object value,
109 boolean isSelected, boolean hasFocus, int row, int column) {
114 JTable.DropLocation dropLocation = table.getDropLocation();
115 if (dropLocation != null
116 && !dropLocation.isInsertRow()
117 && !dropLocation.isInsertColumn()
118 && dropLocation.getRow() == row
119 && dropLocation.getColumn() == column) {
121 fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
122 bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");
128 super.setForeground(fg == null ? table.getSelectionForeground()
130 super.setBackground(bg == null ? table.getSelectionBackground()
133 Color background = unselectedBackground != null
134 ? unselectedBackground
135 : table.getBackground();
136 if (background == null || background instanceof javax.swing.plaf.UIResource) {
137 Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
138 if (alternateColor != null && row % 2 == 0)
139 background = alternateColor;
141 super.setForeground(unselectedForeground != null
142 ? unselectedForeground
143 : table.getForeground());
144 super.setBackground(background);
147 setFont(table.getFont());
150 Border border = null;
152 border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
154 if (border == null) {
155 border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
159 if (!isSelected && table.isCellEditable(row, column)) {
161 col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
163 super.setForeground(col);
165 col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
167 super.setBackground(col);
171 setBorder(getNoFocusBorder());
180 * The following methods are overridden as a performance measure to
181 * to prune code-paths are often called in the case of renders
182 * but which we know are unnecessary. Great care should be taken
183 * when writing your own renderer to weigh the benefits and
184 * drawbacks of overriding methods like these.
188 * Overridden for performance reasons.
189 * See the <a href="#override">Implementation Note</a>
190 * for more information.
192 public boolean isOpaque() {
193 Color back = getBackground();
194 Component p = getParent();
199 // p should now be the JTable.
200 boolean colorMatch = (back != null) && (p != null) &&
201 back.equals(p.getBackground()) &&
203 return !colorMatch && super.isOpaque();
207 * Overridden for performance reasons.
208 * See the <a href="#override">Implementation Note</a>
209 * for more information.
213 public void invalidate() {}
216 * Overridden for performance reasons.
217 * See the <a href="#override">Implementation Note</a>
218 * for more information.
220 public void validate() {}
223 * Overridden for performance reasons.
224 * See the <a href="#override">Implementation Note</a>
225 * for more information.
227 public void revalidate() {}
230 * Overridden for performance reasons.
231 * See the <a href="#override">Implementation Note</a>
232 * for more information.
234 public void repaint(long tm, int x, int y, int width, int height) {}
237 * Overridden for performance reasons.
238 * See the <a href="#override">Implementation Note</a>
239 * for more information.
241 public void repaint(Rectangle r) { }
244 * Overridden for performance reasons.
245 * See the <a href="#override">Implementation Note</a>
246 * for more information.
250 public void repaint() {
254 * Overridden for performance reasons.
255 * See the <a href="#override">Implementation Note</a>
256 * for more information.
258 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
259 // Strings get interned...
260 if (propertyName=="text"
261 || propertyName == "labelFor"
262 || propertyName == "displayedMnemonic"
263 || ((propertyName == "font" || propertyName == "foreground")
264 && oldValue != newValue
265 && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {
267 super.firePropertyChange(propertyName, oldValue, newValue);
272 * Overridden for performance reasons.
273 * See the <a href="#override">Implementation Note</a>
274 * for more information.
276 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
280 * Sets the <code>String</code> object for the cell being rendered to
281 * <code>value</code>.
283 * @param value the string value for this cell; if value is
284 * <code>null</code> it sets the text value to an empty string
285 * @see JLabel#setText
288 protected void setValue(Object value) {
289 setText((value == null) ? "" : value.toString());