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