Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/swing/DefaultListCellRenderer.java
41153 views
1
/*
2
* Copyright (c) 1998, 2021, 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
import sun.swing.SwingUtilities2;
39
40
/**
41
* Renders an item in a list.
42
* <p>
43
* <strong><a id="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
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
* @since 1.2
73
*/
74
@SuppressWarnings("serial") // Same-version serialization only
75
public class DefaultListCellRenderer extends JLabel
76
implements ListCellRenderer<Object>, Serializable
77
{
78
79
/**
80
* An empty <code>Border</code>. This field might not be used. To change the
81
* <code>Border</code> used by this renderer override the
82
* <code>getListCellRendererComponent</code> method and set the border
83
* of the returned component directly.
84
*/
85
private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
86
private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
87
/**
88
* No focus border
89
*/
90
protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;
91
92
/**
93
* Constructs a default renderer object for an item
94
* in a list.
95
*/
96
public DefaultListCellRenderer() {
97
super();
98
setOpaque(true);
99
setBorder(getNoFocusBorder());
100
setName("List.cellRenderer");
101
}
102
103
@SuppressWarnings("removal")
104
private Border getNoFocusBorder() {
105
Border border = DefaultLookup.getBorder(this, ui, "List.cellNoFocusBorder");
106
if (System.getSecurityManager() != null) {
107
if (border != null) return border;
108
return SAFE_NO_FOCUS_BORDER;
109
} else {
110
if (border != null &&
111
(noFocusBorder == null ||
112
noFocusBorder == DEFAULT_NO_FOCUS_BORDER)) {
113
return border;
114
}
115
return noFocusBorder;
116
}
117
}
118
119
public Component getListCellRendererComponent(
120
JList<?> list,
121
Object value,
122
int index,
123
boolean isSelected,
124
boolean cellHasFocus)
125
{
126
setComponentOrientation(list.getComponentOrientation());
127
128
Color bg = null;
129
Color fg = null;
130
131
JList.DropLocation dropLocation = list.getDropLocation();
132
if (dropLocation != null
133
&& !dropLocation.isInsert()
134
&& dropLocation.getIndex() == index) {
135
136
bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground");
137
fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground");
138
139
isSelected = true;
140
}
141
142
if (isSelected) {
143
setBackground(bg == null ? list.getSelectionBackground() : bg);
144
setForeground(fg == null ? list.getSelectionForeground() : fg);
145
}
146
else {
147
setBackground(list.getBackground());
148
setForeground(list.getForeground());
149
}
150
151
if (value instanceof Icon) {
152
setIcon((Icon)value);
153
setText("");
154
}
155
else {
156
setIcon(null);
157
setText((value == null) ? "" : value.toString());
158
}
159
160
setEnabled(list.isEnabled());
161
setFont(list.getFont());
162
163
Border border = null;
164
if (cellHasFocus) {
165
if (isSelected) {
166
border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");
167
}
168
if (border == null) {
169
border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");
170
}
171
} else {
172
border = getNoFocusBorder();
173
}
174
setBorder(border);
175
176
return this;
177
}
178
179
/**
180
* Overridden for performance reasons.
181
* See the <a href="#override">Implementation Note</a>
182
* for more information.
183
*
184
* @since 1.5
185
* @return <code>true</code> if the background is completely opaque
186
* and differs from the JList's background;
187
* <code>false</code> otherwise
188
*/
189
@Override
190
public boolean isOpaque() {
191
Color back = getBackground();
192
Component p = getParent();
193
if (p != null) {
194
p = p.getParent();
195
}
196
// p should now be the JList.
197
boolean colorMatch = (back != null) && (p != null) &&
198
back.equals(p.getBackground()) &&
199
p.isOpaque();
200
return !colorMatch && super.isOpaque();
201
}
202
203
/**
204
* Overridden for performance reasons.
205
* See the <a href="#override">Implementation Note</a>
206
* for more information.
207
*/
208
@Override
209
public void validate() {}
210
211
/**
212
* Overridden for performance reasons.
213
* See the <a href="#override">Implementation Note</a>
214
* for more information.
215
*
216
* @since 1.5
217
*/
218
@Override
219
public void invalidate() {}
220
221
/**
222
* Overridden for performance reasons.
223
* See the <a href="#override">Implementation Note</a>
224
* for more information.
225
*
226
* @since 1.5
227
*/
228
@Override
229
public void repaint() {}
230
231
/**
232
* Overridden for performance reasons.
233
* See the <a href="#override">Implementation Note</a>
234
* for more information.
235
*/
236
@Override
237
public void revalidate() {}
238
/**
239
* Overridden for performance reasons.
240
* See the <a href="#override">Implementation Note</a>
241
* for more information.
242
*/
243
@Override
244
public void repaint(long tm, int x, int y, int width, int height) {}
245
246
/**
247
* Overridden for performance reasons.
248
* See the <a href="#override">Implementation Note</a>
249
* for more information.
250
*/
251
@Override
252
public void repaint(Rectangle r) {}
253
254
/**
255
* Overridden for performance reasons.
256
* See the <a href="#override">Implementation Note</a>
257
* for more information.
258
*/
259
@Override
260
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
261
// Strings get interned...
262
if (propertyName == "text"
263
|| ((SwingUtilities2.isScaleChanged(propertyName, oldValue, newValue)
264
|| propertyName == "font" || propertyName == "foreground")
265
&& oldValue != newValue
266
&& getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {
267
268
super.firePropertyChange(propertyName, oldValue, newValue);
269
}
270
}
271
272
/**
273
* Overridden for performance reasons.
274
* See the <a href="#override">Implementation Note</a>
275
* for more information.
276
*/
277
@Override
278
public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {}
279
280
/**
281
* Overridden for performance reasons.
282
* See the <a href="#override">Implementation Note</a>
283
* for more information.
284
*/
285
@Override
286
public void firePropertyChange(String propertyName, char oldValue, char newValue) {}
287
288
/**
289
* Overridden for performance reasons.
290
* See the <a href="#override">Implementation Note</a>
291
* for more information.
292
*/
293
@Override
294
public void firePropertyChange(String propertyName, short oldValue, short newValue) {}
295
296
/**
297
* Overridden for performance reasons.
298
* See the <a href="#override">Implementation Note</a>
299
* for more information.
300
*/
301
@Override
302
public void firePropertyChange(String propertyName, int oldValue, int newValue) {}
303
304
/**
305
* Overridden for performance reasons.
306
* See the <a href="#override">Implementation Note</a>
307
* for more information.
308
*/
309
@Override
310
public void firePropertyChange(String propertyName, long oldValue, long newValue) {}
311
312
/**
313
* Overridden for performance reasons.
314
* See the <a href="#override">Implementation Note</a>
315
* for more information.
316
*/
317
@Override
318
public void firePropertyChange(String propertyName, float oldValue, float newValue) {}
319
320
/**
321
* Overridden for performance reasons.
322
* See the <a href="#override">Implementation Note</a>
323
* for more information.
324
*/
325
@Override
326
public void firePropertyChange(String propertyName, double oldValue, double newValue) {}
327
328
/**
329
* Overridden for performance reasons.
330
* See the <a href="#override">Implementation Note</a>
331
* for more information.
332
*/
333
@Override
334
public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
335
336
/**
337
* A subclass of DefaultListCellRenderer that implements UIResource.
338
* DefaultListCellRenderer doesn't implement UIResource
339
* directly so that applications can safely override the
340
* cellRenderer property with DefaultListCellRenderer subclasses.
341
* <p>
342
* <strong>Warning:</strong>
343
* Serialized objects of this class will not be compatible with
344
* future Swing releases. The current serialization support is
345
* appropriate for short term storage or RMI between applications running
346
* the same version of Swing. As of 1.4, support for long term storage
347
* of all JavaBeans
348
* has been added to the <code>java.beans</code> package.
349
* Please see {@link java.beans.XMLEncoder}.
350
*/
351
@SuppressWarnings("serial") // Same-version serialization only
352
public static class UIResource extends DefaultListCellRenderer
353
implements javax.swing.plaf.UIResource
354
{
355
/**
356
* Constructs a {@code UIResource}.
357
*/
358
public UIResource() {}
359
}
360
}
361
362