Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DefaultListCellRenderer.java
38829 views
1
/*
2
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.swing;
27
28
import javax.swing.*;
29
import javax.swing.event.*;
30
import javax.swing.border.*;
31
32
import java.awt.Component;
33
import java.awt.Color;
34
import java.awt.Rectangle;
35
36
import java.io.Serializable;
37
import sun.swing.DefaultLookup;
38
39
40
/**
41
* Renders an item in a list.
42
* <p>
43
* <strong><a name="override">Implementation Note:</a></strong>
44
* This class overrides
45
* <code>invalidate</code>,
46
* <code>validate</code>,
47
* <code>revalidate</code>,
48
* <code>repaint</code>,
49
* <code>isOpaque</code>,
50
* and
51
* <code>firePropertyChange</code>
52
* solely to improve performance.
53
* If not overridden, these frequently called methods would execute code paths
54
* that are unnecessary for the default list cell renderer.
55
* If you write your own renderer,
56
* take care to weigh the benefits and
57
* drawbacks of overriding these methods.
58
*
59
* <p>
60
*
61
* <strong>Warning:</strong>
62
* Serialized objects of this class will not be compatible with
63
* future Swing releases. The current serialization support is
64
* appropriate for short term storage or RMI between applications running
65
* the same version of Swing. As of 1.4, support for long term storage
66
* of all JavaBeans&trade;
67
* has been added to the <code>java.beans</code> package.
68
* Please see {@link java.beans.XMLEncoder}.
69
*
70
* @author Philip Milne
71
* @author Hans Muller
72
*/
73
public class DefaultListCellRenderer extends JLabel
74
implements ListCellRenderer<Object>, Serializable
75
{
76
77
/**
78
* An empty <code>Border</code>. This field might not be used. To change the
79
* <code>Border</code> used by this renderer override the
80
* <code>getListCellRendererComponent</code> method and set the border
81
* of the returned component directly.
82
*/
83
private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
84
private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
85
protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;
86
87
/**
88
* Constructs a default renderer object for an item
89
* in a list.
90
*/
91
public DefaultListCellRenderer() {
92
super();
93
setOpaque(true);
94
setBorder(getNoFocusBorder());
95
setName("List.cellRenderer");
96
}
97
98
private Border getNoFocusBorder() {
99
Border border = DefaultLookup.getBorder(this, ui, "List.cellNoFocusBorder");
100
if (System.getSecurityManager() != null) {
101
if (border != null) return border;
102
return SAFE_NO_FOCUS_BORDER;
103
} else {
104
if (border != null &&
105
(noFocusBorder == null ||
106
noFocusBorder == DEFAULT_NO_FOCUS_BORDER)) {
107
return border;
108
}
109
return noFocusBorder;
110
}
111
}
112
113
public Component getListCellRendererComponent(
114
JList<?> list,
115
Object value,
116
int index,
117
boolean isSelected,
118
boolean cellHasFocus)
119
{
120
setComponentOrientation(list.getComponentOrientation());
121
122
Color bg = null;
123
Color fg = null;
124
125
JList.DropLocation dropLocation = list.getDropLocation();
126
if (dropLocation != null
127
&& !dropLocation.isInsert()
128
&& dropLocation.getIndex() == index) {
129
130
bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground");
131
fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground");
132
133
isSelected = true;
134
}
135
136
if (isSelected) {
137
setBackground(bg == null ? list.getSelectionBackground() : bg);
138
setForeground(fg == null ? list.getSelectionForeground() : fg);
139
}
140
else {
141
setBackground(list.getBackground());
142
setForeground(list.getForeground());
143
}
144
145
if (value instanceof Icon) {
146
setIcon((Icon)value);
147
setText("");
148
}
149
else {
150
setIcon(null);
151
setText((value == null) ? "" : value.toString());
152
}
153
154
setEnabled(list.isEnabled());
155
setFont(list.getFont());
156
157
Border border = null;
158
if (cellHasFocus) {
159
if (isSelected) {
160
border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");
161
}
162
if (border == null) {
163
border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");
164
}
165
} else {
166
border = getNoFocusBorder();
167
}
168
setBorder(border);
169
170
return this;
171
}
172
173
/**
174
* Overridden for performance reasons.
175
* See the <a href="#override">Implementation Note</a>
176
* for more information.
177
*
178
* @since 1.5
179
* @return <code>true</code> if the background is completely opaque
180
* and differs from the JList's background;
181
* <code>false</code> otherwise
182
*/
183
@Override
184
public boolean isOpaque() {
185
Color back = getBackground();
186
Component p = getParent();
187
if (p != null) {
188
p = p.getParent();
189
}
190
// p should now be the JList.
191
boolean colorMatch = (back != null) && (p != null) &&
192
back.equals(p.getBackground()) &&
193
p.isOpaque();
194
return !colorMatch && super.isOpaque();
195
}
196
197
/**
198
* Overridden for performance reasons.
199
* See the <a href="#override">Implementation Note</a>
200
* for more information.
201
*/
202
@Override
203
public void validate() {}
204
205
/**
206
* Overridden for performance reasons.
207
* See the <a href="#override">Implementation Note</a>
208
* for more information.
209
*
210
* @since 1.5
211
*/
212
@Override
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
* @since 1.5
221
*/
222
@Override
223
public void repaint() {}
224
225
/**
226
* Overridden for performance reasons.
227
* See the <a href="#override">Implementation Note</a>
228
* for more information.
229
*/
230
@Override
231
public void revalidate() {}
232
/**
233
* Overridden for performance reasons.
234
* See the <a href="#override">Implementation Note</a>
235
* for more information.
236
*/
237
@Override
238
public void repaint(long tm, int x, int y, int width, int height) {}
239
240
/**
241
* Overridden for performance reasons.
242
* See the <a href="#override">Implementation Note</a>
243
* for more information.
244
*/
245
@Override
246
public void repaint(Rectangle r) {}
247
248
/**
249
* Overridden for performance reasons.
250
* See the <a href="#override">Implementation Note</a>
251
* for more information.
252
*/
253
@Override
254
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
255
// Strings get interned...
256
if (propertyName == "text"
257
|| ((propertyName == "font" || propertyName == "foreground")
258
&& oldValue != newValue
259
&& getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {
260
261
super.firePropertyChange(propertyName, oldValue, newValue);
262
}
263
}
264
265
/**
266
* Overridden for performance reasons.
267
* See the <a href="#override">Implementation Note</a>
268
* for more information.
269
*/
270
@Override
271
public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {}
272
273
/**
274
* Overridden for performance reasons.
275
* See the <a href="#override">Implementation Note</a>
276
* for more information.
277
*/
278
@Override
279
public void firePropertyChange(String propertyName, char oldValue, char newValue) {}
280
281
/**
282
* Overridden for performance reasons.
283
* See the <a href="#override">Implementation Note</a>
284
* for more information.
285
*/
286
@Override
287
public void firePropertyChange(String propertyName, short oldValue, short newValue) {}
288
289
/**
290
* Overridden for performance reasons.
291
* See the <a href="#override">Implementation Note</a>
292
* for more information.
293
*/
294
@Override
295
public void firePropertyChange(String propertyName, int oldValue, int newValue) {}
296
297
/**
298
* Overridden for performance reasons.
299
* See the <a href="#override">Implementation Note</a>
300
* for more information.
301
*/
302
@Override
303
public void firePropertyChange(String propertyName, long oldValue, long newValue) {}
304
305
/**
306
* Overridden for performance reasons.
307
* See the <a href="#override">Implementation Note</a>
308
* for more information.
309
*/
310
@Override
311
public void firePropertyChange(String propertyName, float oldValue, float newValue) {}
312
313
/**
314
* Overridden for performance reasons.
315
* See the <a href="#override">Implementation Note</a>
316
* for more information.
317
*/
318
@Override
319
public void firePropertyChange(String propertyName, double oldValue, double newValue) {}
320
321
/**
322
* Overridden for performance reasons.
323
* See the <a href="#override">Implementation Note</a>
324
* for more information.
325
*/
326
@Override
327
public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
328
329
/**
330
* A subclass of DefaultListCellRenderer that implements UIResource.
331
* DefaultListCellRenderer doesn't implement UIResource
332
* directly so that applications can safely override the
333
* cellRenderer property with DefaultListCellRenderer subclasses.
334
* <p>
335
* <strong>Warning:</strong>
336
* Serialized objects of this class will not be compatible with
337
* future Swing releases. The current serialization support is
338
* appropriate for short term storage or RMI between applications running
339
* the same version of Swing. As of 1.4, support for long term storage
340
* of all JavaBeans&trade;
341
* has been added to the <code>java.beans</code> package.
342
* Please see {@link java.beans.XMLEncoder}.
343
*/
344
public static class UIResource extends DefaultListCellRenderer
345
implements javax.swing.plaf.UIResource
346
{
347
}
348
}
349
350