001 /*
002 * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing;
027
028 import javax.swing.*;
029 import javax.swing.event.*;
030 import javax.swing.border.*;
031
032 import java.awt.Component;
033 import java.awt.Color;
034 import java.awt.Rectangle;
035
036 import java.io.Serializable;
037
038 /**
039 * Renders an item in a list.
040 * <p>
041 * <strong><a name="override">Implementation Note:</a></strong>
042 * This class overrides
043 * <code>invalidate</code>,
044 * <code>validate</code>,
045 * <code>revalidate</code>,
046 * <code>repaint</code>,
047 * <code>isOpaque</code>,
048 * and
049 * <code>firePropertyChange</code>
050 * solely to improve performance.
051 * If not overridden, these frequently called methods would execute code paths
052 * that are unnecessary for the default list cell renderer.
053 * If you write your own renderer,
054 * take care to weigh the benefits and
055 * drawbacks of overriding these methods.
056 *
057 * <p>
058 *
059 * <strong>Warning:</strong>
060 * Serialized objects of this class will not be compatible with
061 * future Swing releases. The current serialization support is
062 * appropriate for short term storage or RMI between applications running
063 * the same version of Swing. As of 1.4, support for long term storage
064 * of all JavaBeans<sup><font size="-2">TM</font></sup>
065 * has been added to the <code>java.beans</code> package.
066 * Please see {@link java.beans.XMLEncoder}.
067 *
068 * @version 1.37 05/05/07
069 * @author Philip Milne
070 * @author Hans Muller
071 */
072 public class DefaultListCellRenderer extends JLabel implements
073 ListCellRenderer, Serializable {
074
075 /**
076 * An empty <code>Border</code>. This field might not be used. To change the
077 * <code>Border</code> used by this renderer override the
078 * <code>getListCellRendererComponent</code> method and set the border
079 * of the returned component directly.
080 */
081 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
082 private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(
083 1, 1, 1, 1);
084
085 /**
086 * Constructs a default renderer object for an item
087 * in a list.
088 */
089 public DefaultListCellRenderer() {
090 super ();
091 setOpaque(true);
092 setBorder(getNoFocusBorder());
093 }
094
095 private static Border getNoFocusBorder() {
096 if (System.getSecurityManager() != null) {
097 return SAFE_NO_FOCUS_BORDER;
098 } else {
099 return UIManager.getBorder("List.noFocusBorder");
100 }
101 }
102
103 public Component getListCellRendererComponent(JList list,
104 Object value, int index, boolean isSelected,
105 boolean cellHasFocus) {
106 setComponentOrientation(list.getComponentOrientation());
107
108 Color bg = null;
109 Color fg = null;
110
111 JList.DropLocation dropLocation = list.getDropLocation();
112 if (dropLocation != null && !dropLocation.isInsert()
113 && dropLocation.getIndex() == index) {
114
115 bg = UIManager.getColor("List.dropCellBackground");
116 fg = UIManager.getColor("List.dropCellForeground");
117
118 isSelected = true;
119 }
120
121 if (isSelected) {
122 setBackground(bg == null ? list.getSelectionBackground()
123 : bg);
124 setForeground(fg == null ? list.getSelectionForeground()
125 : fg);
126 } else {
127 setBackground(list.getBackground());
128 setForeground(list.getForeground());
129 }
130
131 if (value instanceof Icon) {
132 setIcon((Icon) value);
133 setText("");
134 } else {
135 setIcon(null);
136 setText((value == null) ? "" : value.toString());
137 }
138
139 setEnabled(list.isEnabled());
140 setFont(list.getFont());
141
142 Border border = null;
143 if (cellHasFocus) {
144 if (isSelected) {
145 border = UIManager
146 .getBorder("List.focusSelectedCellHighlightBorder");
147 }
148 if (border == null) {
149 border = UIManager
150 .getBorder("List.focusCellHighlightBorder");
151 }
152 } else {
153 border = getNoFocusBorder();
154 }
155 setBorder(border);
156
157 return this ;
158 }
159
160 /**
161 * Overridden for performance reasons.
162 * See the <a href="#override">Implementation Note</a>
163 * for more information.
164 *
165 * @since 1.5
166 * @return <code>true</code> if the background is completely opaque
167 * and differs from the JList's background;
168 * <code>false</code> otherwise
169 */
170 public boolean isOpaque() {
171 Color back = getBackground();
172 Component p = getParent();
173 if (p != null) {
174 p = p.getParent();
175 }
176 // p should now be the JList.
177 boolean colorMatch = (back != null) && (p != null)
178 && back.equals(p.getBackground()) && p.isOpaque();
179 return !colorMatch && super .isOpaque();
180 }
181
182 /**
183 * Overridden for performance reasons.
184 * See the <a href="#override">Implementation Note</a>
185 * for more information.
186 */
187 public void validate() {
188 }
189
190 /**
191 * Overridden for performance reasons.
192 * See the <a href="#override">Implementation Note</a>
193 * for more information.
194 *
195 * @since 1.5
196 */
197 public void invalidate() {
198 }
199
200 /**
201 * Overridden for performance reasons.
202 * See the <a href="#override">Implementation Note</a>
203 * for more information.
204 *
205 * @since 1.5
206 */
207 public void repaint() {
208 }
209
210 /**
211 * Overridden for performance reasons.
212 * See the <a href="#override">Implementation Note</a>
213 * for more information.
214 */
215 public void revalidate() {
216 }
217
218 /**
219 * Overridden for performance reasons.
220 * See the <a href="#override">Implementation Note</a>
221 * for more information.
222 */
223 public void repaint(long tm, int x, int y, int width, int height) {
224 }
225
226 /**
227 * Overridden for performance reasons.
228 * See the <a href="#override">Implementation Note</a>
229 * for more information.
230 */
231 public void repaint(Rectangle r) {
232 }
233
234 /**
235 * Overridden for performance reasons.
236 * See the <a href="#override">Implementation Note</a>
237 * for more information.
238 */
239 protected void firePropertyChange(String propertyName,
240 Object oldValue, Object newValue) {
241 // Strings get interned...
242 if (propertyName == "text"
243 || ((propertyName == "font" || propertyName == "foreground")
244 && oldValue != newValue && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {
245
246 super .firePropertyChange(propertyName, oldValue, newValue);
247 }
248 }
249
250 /**
251 * Overridden for performance reasons.
252 * See the <a href="#override">Implementation Note</a>
253 * for more information.
254 */
255 public void firePropertyChange(String propertyName, byte oldValue,
256 byte newValue) {
257 }
258
259 /**
260 * Overridden for performance reasons.
261 * See the <a href="#override">Implementation Note</a>
262 * for more information.
263 */
264 public void firePropertyChange(String propertyName, char oldValue,
265 char newValue) {
266 }
267
268 /**
269 * Overridden for performance reasons.
270 * See the <a href="#override">Implementation Note</a>
271 * for more information.
272 */
273 public void firePropertyChange(String propertyName, short oldValue,
274 short newValue) {
275 }
276
277 /**
278 * Overridden for performance reasons.
279 * See the <a href="#override">Implementation Note</a>
280 * for more information.
281 */
282 public void firePropertyChange(String propertyName, int oldValue,
283 int newValue) {
284 }
285
286 /**
287 * Overridden for performance reasons.
288 * See the <a href="#override">Implementation Note</a>
289 * for more information.
290 */
291 public void firePropertyChange(String propertyName, long oldValue,
292 long newValue) {
293 }
294
295 /**
296 * Overridden for performance reasons.
297 * See the <a href="#override">Implementation Note</a>
298 * for more information.
299 */
300 public void firePropertyChange(String propertyName, float oldValue,
301 float newValue) {
302 }
303
304 /**
305 * Overridden for performance reasons.
306 * See the <a href="#override">Implementation Note</a>
307 * for more information.
308 */
309 public void firePropertyChange(String propertyName,
310 double oldValue, double newValue) {
311 }
312
313 /**
314 * Overridden for performance reasons.
315 * See the <a href="#override">Implementation Note</a>
316 * for more information.
317 */
318 public void firePropertyChange(String propertyName,
319 boolean oldValue, boolean newValue) {
320 }
321
322 /**
323 * A subclass of DefaultListCellRenderer that implements UIResource.
324 * DefaultListCellRenderer doesn't implement UIResource
325 * directly so that applications can safely override the
326 * cellRenderer property with DefaultListCellRenderer subclasses.
327 * <p>
328 * <strong>Warning:</strong>
329 * Serialized objects of this class will not be compatible with
330 * future Swing releases. The current serialization support is
331 * appropriate for short term storage or RMI between applications running
332 * the same version of Swing. As of 1.4, support for long term storage
333 * of all JavaBeans<sup><font size="-2">TM</font></sup>
334 * has been added to the <code>java.beans</code> package.
335 * Please see {@link java.beans.XMLEncoder}.
336 */
337 public static class UIResource extends DefaultListCellRenderer
338 implements javax.swing.plaf.UIResource {
339 }
340
341 }
|