- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathDefaultTableCellRenderer.java
405 lines (365 loc) · 14.3 KB
/
DefaultTableCellRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing.table;
importjavax.swing.*;
importjavax.swing.border.*;
importjava.awt.Component;
importjava.awt.Color;
importjava.awt.Rectangle;
importjava.io.Serializable;
importsun.swing.DefaultLookup;
importsun.swing.SwingUtilities2;
/**
* The standard class for rendering (displaying) individual cells
* in a <code>JTable</code>.
* <p>
*
* <strong><a id="override">Implementation Note:</a></strong>
* This class inherits from <code>JLabel</code>, a standard component class.
* However <code>JTable</code> employs a unique mechanism for rendering
* its cells and therefore requires some slightly modified behavior
* from its cell renderer.
* The table class defines a single cell renderer and uses it as a
* as a rubber-stamp for rendering all cells in the table;
* it renders the first cell,
* changes the contents of that cell renderer,
* shifts the origin to the new location, re-draws it, and so on.
* The standard <code>JLabel</code> component was not
* designed to be used this way and we want to avoid
* triggering a <code>revalidate</code> each time the
* cell is drawn. This would greatly decrease performance because the
* <code>revalidate</code> message would be
* passed up the hierarchy of the container to determine whether any other
* components would be affected.
* As the renderer is only parented for the lifetime of a painting operation
* we similarly want to avoid the overhead associated with walking the
* hierarchy for painting operations.
* So this class
* overrides the <code>validate</code>, <code>invalidate</code>,
* <code>revalidate</code>, <code>repaint</code>, and
* <code>firePropertyChange</code> methods to be
* no-ops and override the <code>isOpaque</code> method solely to improve
* performance. If you write your own renderer,
* please keep this performance consideration in mind.
* <p>
*
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @author Philip Milne
* @see JTable
*/
@SuppressWarnings("serial") // Same-version serialization only
publicclassDefaultTableCellRendererextendsJLabel
implementsTableCellRenderer, Serializable
{
/**
* An empty <code>Border</code>. This field might not be used. To change the
* <code>Border</code> used by this renderer override the
* <code>getTableCellRendererComponent</code> method and set the border
* of the returned component directly.
*/
privatestaticfinalBorderDEFAULT_NO_FOCUS_BORDER = newEmptyBorder(1, 1, 1, 1);
/**
* A border without focus.
*/
protectedstaticBordernoFocusBorder = DEFAULT_NO_FOCUS_BORDER;
// We need a place to store the color the JLabel should be returned
// to after its foreground and background colors have been set
// to the selection background color.
// These ivars will be made protected when their names are finalized.
privateColorunselectedForeground;
privateColorunselectedBackground;
/**
* Creates a default table cell renderer.
*/
publicDefaultTableCellRenderer() {
super();
setOpaque(true);
setBorder(getNoFocusBorder());
setName("Table.cellRenderer");
}
privateBordergetNoFocusBorder() {
Borderborder = DefaultLookup.getBorder(this, ui, "Table.cellNoFocusBorder");
if (border != null) {
if (noFocusBorder == null || noFocusBorder == DEFAULT_NO_FOCUS_BORDER) {
returnborder;
}
}
returnnoFocusBorder;
}
/**
* Overrides <code>JComponent.setForeground</code> to assign
* the unselected-foreground color to the specified color.
*
* @param c set the foreground color to this value
*/
publicvoidsetForeground(Colorc) {
super.setForeground(c);
unselectedForeground = c;
}
/**
* Overrides <code>JComponent.setBackground</code> to assign
* the unselected-background color to the specified color.
*
* @param c set the background color to this value
*/
publicvoidsetBackground(Colorc) {
super.setBackground(c);
unselectedBackground = c;
}
/**
* Notification from the <code>UIManager</code> that the look and feel
* [L&F] has changed.
* Replaces the current UI object with the latest version from the
* <code>UIManager</code>.
*
* @see JComponent#updateUI
*/
publicvoidupdateUI() {
super.updateUI();
setForeground(null);
setBackground(null);
}
// implements javax.swing.table.TableCellRenderer
/**
*
* Returns the default table cell renderer.
* <p>
* During a printing operation, this method will be called with
* <code>isSelected</code> and <code>hasFocus</code> values of
* <code>false</code> to prevent selection and focus from appearing
* in the printed output. To do other customization based on whether
* or not the table is being printed, check the return value from
* {@link javax.swing.JComponent#isPaintingForPrint()}.
*
* @param table the <code>JTable</code>
* @param value the value to assign to the cell at
* <code>[row, column]</code>
* @param isSelected true if cell is selected
* @param hasFocus true if cell has focus
* @param row the row of the cell to render
* @param column the column of the cell to render
* @return the default table cell renderer
* @see javax.swing.JComponent#isPaintingForPrint()
*/
publicComponentgetTableCellRendererComponent(JTabletable, Objectvalue,
booleanisSelected, booleanhasFocus, introw, intcolumn) {
if (table == null) {
returnthis;
}
Colorfg = null;
Colorbg = null;
JTable.DropLocationdropLocation = table.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsertRow()
&& !dropLocation.isInsertColumn()
&& dropLocation.getRow() == row
&& dropLocation.getColumn() == column) {
fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");
isSelected = true;
}
if (isSelected) {
super.setForeground(fg == null ? table.getSelectionForeground()
: fg);
super.setBackground(bg == null ? table.getSelectionBackground()
: bg);
} else {
Colorbackground = unselectedBackground != null
? unselectedBackground
: table.getBackground();
if (background == null || backgroundinstanceofjavax.swing.plaf.UIResource) {
ColoralternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
if (alternateColor != null && row % 2 != 0) {
background = alternateColor;
}
}
super.setForeground(unselectedForeground != null
? unselectedForeground
: table.getForeground());
super.setBackground(background);
}
setFont(table.getFont());
if (hasFocus) {
Borderborder = null;
if (isSelected) {
border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
}
if (border == null) {
border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
}
setBorder(border);
if (!isSelected && table.isCellEditable(row, column)) {
Colorcol;
col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
if (col != null) {
super.setForeground(col);
}
col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
if (col != null) {
super.setBackground(col);
}
}
} else {
setBorder(getNoFocusBorder());
}
setValue(value);
returnthis;
}
/*
* The following methods are overridden as a performance measure to
* to prune code-paths are often called in the case of renders
* but which we know are unnecessary. Great care should be taken
* when writing your own renderer to weigh the benefits and
* drawbacks of overriding methods like these.
*/
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*/
publicbooleanisOpaque() {
Colorback = getBackground();
Componentp = getParent();
if (p != null) {
p = p.getParent();
}
// p should now be the JTable.
booleancolorMatch = (back != null) && (p != null) &&
back.equals(p.getBackground()) &&
p.isOpaque();
return !colorMatch && super.isOpaque();
}
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*
* @since 1.5
*/
publicvoidinvalidate() {}
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*/
publicvoidvalidate() {}
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*/
publicvoidrevalidate() {}
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*/
publicvoidrepaint(longtm, intx, inty, intwidth, intheight) {}
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*/
publicvoidrepaint(Rectangler) { }
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*
* @since 1.5
*/
publicvoidrepaint() {
}
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*/
protectedvoidfirePropertyChange(StringpropertyName, ObjectoldValue, ObjectnewValue) {
// Strings get interned...
if (propertyName=="text"
|| propertyName == "labelFor"
|| propertyName == "displayedMnemonic"
|| ((SwingUtilities2.isScaleChanged(propertyName, oldValue, newValue)
|| propertyName == "font" || propertyName == "foreground")
&& oldValue != newValue
&& getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*/
publicvoidfirePropertyChange(StringpropertyName, booleanoldValue, booleannewValue) { }
/**
* Sets the <code>String</code> object for the cell being rendered to
* <code>value</code>.
*
* @param value the string value for this cell; if value is
* <code>null</code> it sets the text value to an empty string
* @see JLabel#setText
*
*/
protectedvoidsetValue(Objectvalue) {
setText((value == null) ? "" : value.toString());
}
/**
* A subclass of <code>DefaultTableCellRenderer</code> that
* implements <code>UIResource</code>.
* <code>DefaultTableCellRenderer</code> doesn't implement
* <code>UIResource</code>
* directly so that applications can safely override the
* <code>cellRenderer</code> property with
* <code>DefaultTableCellRenderer</code> subclasses.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
@SuppressWarnings("serial") // Same-version serialization only
publicstaticclassUIResourceextendsDefaultTableCellRenderer
implementsjavax.swing.plaf.UIResource
{
/**
* Constructs a {@code UIResource}.
*/
publicUIResource() {}
}
}