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