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