]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/ColumnHeaderRenderer.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / ColumnHeaderRenderer.java
1 package org.simantics.spreadsheet.ui;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Rectangle;
6 import java.io.Serializable;
7
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;
14
15 public class ColumnHeaderRenderer extends JLabel implements TableCellRenderer, Serializable {
16
17         private static final long serialVersionUID = 1L;
18
19
20         /**
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.
25          */
26
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; 
33
34         /**
35          * Creates a default table cell renderer.
36          */
37         public ColumnHeaderRenderer() {
38                 super();
39                 setOpaque(true);
40                 setHorizontalAlignment(SwingConstants.CENTER);
41                 setBackground(DefaultLookup.GRAY);
42                 setBorder(getNoFocusBorder());
43                 setName("Table.cellRenderer");
44         }
45
46         private Border getNoFocusBorder() {
47                 return TableBorder.BORDER;
48         }
49
50         /**
51          * Overrides <code>JComponent.setForeground</code> to assign
52          * the unselected-foreground color to the specified color.
53          * 
54          * @param c set the foreground color to this value
55          */
56         public void setForeground(Color c) {
57                 super.setForeground(c); 
58                 unselectedForeground = c; 
59         }
60
61         /**
62          * Overrides <code>JComponent.setBackground</code> to assign
63          * the unselected-background color to the specified color.
64          *
65          * @param c set the background color to this value
66          */
67         public void setBackground(Color c) {
68                 super.setBackground(c); 
69                 unselectedBackground = c; 
70         }
71
72         /**
73          * Notification from the <code>UIManager</code> that the look and feel
74          * [L&F] has changed.
75          * Replaces the current UI object with the latest version from the 
76          * <code>UIManager</code>.
77          *
78          * @see JComponent#updateUI
79          */
80         public void updateUI() {
81                 super.updateUI(); 
82                 setForeground(null);
83                 setBackground(null);
84         }
85
86         // implements javax.swing.table.TableCellRenderer
87         /**
88          *
89          * Returns the default table cell renderer.
90          * <p>
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()}.
97          *
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()
107          */
108         public Component getTableCellRendererComponent(JTable table, Object value,
109                         boolean isSelected, boolean hasFocus, int row, int column) {
110
111                 Color fg = null;
112                 Color bg = null;
113
114                 JTable.DropLocation dropLocation = table.getDropLocation();
115                 if (dropLocation != null
116                                 && !dropLocation.isInsertRow()
117                                 && !dropLocation.isInsertColumn()
118                                 && dropLocation.getRow() == row
119                                 && dropLocation.getColumn() == column) {
120
121                         fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
122                         bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");
123
124                         isSelected = true;
125                 }
126
127                 if (isSelected) {
128                         super.setForeground(fg == null ? table.getSelectionForeground()
129                                         : fg);
130                         super.setBackground(bg == null ? table.getSelectionBackground()
131                                         : bg);
132                 } else {
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;
140                         }
141                         super.setForeground(unselectedForeground != null
142                                         ? unselectedForeground
143                                                         : table.getForeground());
144                         super.setBackground(background);
145                 }
146
147                 setFont(table.getFont());
148
149                 if (hasFocus) {
150                         Border border = null;
151                         if (isSelected) {
152                                 border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
153                         }
154                         if (border == null) {
155                                 border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
156                         }
157                         setBorder(border);
158
159                         if (!isSelected && table.isCellEditable(row, column)) {
160                                 Color col;
161                                 col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
162                                 if (col != null) {
163                                         super.setForeground(col);
164                                 }
165                                 col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
166                                 if (col != null) {
167                                         super.setBackground(col);
168                                 }
169                         }
170                 } else {
171                         setBorder(getNoFocusBorder());
172                 }
173
174                 setValue(value); 
175
176                 return this;
177         }
178
179         /*
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.
185          */
186
187         /**
188          * Overridden for performance reasons.
189          * See the <a href="#override">Implementation Note</a> 
190          * for more information.
191          */
192         public boolean isOpaque() { 
193                 Color back = getBackground();
194                 Component p = getParent(); 
195                 if (p != null) { 
196                         p = p.getParent(); 
197                 }
198
199                 // p should now be the JTable. 
200                 boolean colorMatch = (back != null) && (p != null) && 
201                 back.equals(p.getBackground()) && 
202                 p.isOpaque();
203                 return !colorMatch && super.isOpaque(); 
204         }
205
206         /**
207          * Overridden for performance reasons.
208          * See the <a href="#override">Implementation Note</a> 
209          * for more information.
210          *
211          * @since 1.5
212          */
213         public void invalidate() {}
214
215         /**
216          * Overridden for performance reasons.
217          * See the <a href="#override">Implementation Note</a> 
218          * for more information.
219          */
220         public void validate() {}
221
222         /**
223          * Overridden for performance reasons.
224          * See the <a href="#override">Implementation Note</a> 
225          * for more information.
226          */
227         public void revalidate() {}
228
229         /**
230          * Overridden for performance reasons.
231          * See the <a href="#override">Implementation Note</a> 
232          * for more information.
233          */
234         public void repaint(long tm, int x, int y, int width, int height) {}
235
236         /**
237          * Overridden for performance reasons.
238          * See the <a href="#override">Implementation Note</a> 
239          * for more information.
240          */
241         public void repaint(Rectangle r) { }
242
243         /**
244          * Overridden for performance reasons.
245          * See the <a href="#override">Implementation Note</a> 
246          * for more information.
247          *
248          * @since 1.5
249          */
250         public void repaint() {
251         }
252
253         /**
254          * Overridden for performance reasons.
255          * See the <a href="#override">Implementation Note</a> 
256          * for more information.
257          */
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)) {
266
267                         super.firePropertyChange(propertyName, oldValue, newValue);
268                 }
269         }
270
271         /**
272          * Overridden for performance reasons.
273          * See the <a href="#override">Implementation Note</a> 
274          * for more information.
275          */
276         public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
277
278
279         /**
280          * Sets the <code>String</code> object for the cell being rendered to
281          * <code>value</code>.
282          * 
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
286          * 
287          */
288         protected void setValue(Object value) {
289                 setText((value == null) ? "" : value.toString());
290         }
291
292 }