KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > DefaultListCellRenderer


1 /*
2  * @(#)DefaultListCellRenderer.java 1.29 05/10/31
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import javax.swing.*;
11 import javax.swing.event.*;
12 import javax.swing.border.*;
13
14 import java.awt.Component JavaDoc;
15 import java.awt.Color JavaDoc;
16 import java.awt.Rectangle JavaDoc;
17
18 import java.io.Serializable JavaDoc;
19
20
21 /**
22  * Renders an item in a list.
23  * <p>
24  * <strong><a name="override">Implementation Note:</a></strong>
25  * This class overrides
26  * <code>invalidate</code>,
27  * <code>validate</code>,
28  * <code>revalidate</code>,
29  * <code>repaint</code>,
30  * <code>isOpaque</code>,
31  * and
32  * <code>firePropertyChange</code>
33  * solely to improve performance.
34  * If not overridden, these frequently called methods would execute code paths
35  * that are unnecessary for the default list cell renderer.
36  * If you write your own renderer,
37  * take care to weigh the benefits and
38  * drawbacks of overriding these methods.
39  *
40  * <p>
41  *
42  * <strong>Warning:</strong>
43  * Serialized objects of this class will not be compatible with
44  * future Swing releases. The current serialization support is
45  * appropriate for short term storage or RMI between applications running
46  * the same version of Swing. As of 1.4, support for long term storage
47  * of all JavaBeans<sup><font size="-2">TM</font></sup>
48  * has been added to the <code>java.beans</code> package.
49  * Please see {@link java.beans.XMLEncoder}.
50  *
51  * @version 1.29 10/31/05
52  * @author Philip Milne
53  * @author Hans Muller
54  */

55 public class DefaultListCellRenderer extends JLabel JavaDoc
56     implements ListCellRenderer JavaDoc, Serializable JavaDoc
57 {
58
59     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
60     private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
61     
62     /**
63      * Constructs a default renderer object for an item
64      * in a list.
65      */

66     public DefaultListCellRenderer() {
67     super();
68     setOpaque(true);
69         setBorder(getNoFocusBorder());
70     }
71
72
73     private static Border getNoFocusBorder() {
74         if (System.getSecurityManager() != null) {
75             return SAFE_NO_FOCUS_BORDER;
76         } else {
77             return noFocusBorder;
78         }
79     }
80
81     public Component JavaDoc getListCellRendererComponent(
82         JList JavaDoc list,
83     Object JavaDoc value,
84         int index,
85         boolean isSelected,
86         boolean cellHasFocus)
87     {
88         setComponentOrientation(list.getComponentOrientation());
89     if (isSelected) {
90         setBackground(list.getSelectionBackground());
91         setForeground(list.getSelectionForeground());
92     }
93     else {
94         setBackground(list.getBackground());
95         setForeground(list.getForeground());
96     }
97
98     if (value instanceof Icon JavaDoc) {
99         setIcon((Icon JavaDoc)value);
100         setText("");
101     }
102     else {
103         setIcon(null);
104         setText((value == null) ? "" : value.toString());
105     }
106
107     setEnabled(list.isEnabled());
108     setFont(list.getFont());
109
110         Border border = null;
111         if (cellHasFocus) {
112             if (isSelected) {
113                 border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
114             }
115             if (border == null) {
116                 border = UIManager.getBorder("List.focusCellHighlightBorder");
117             }
118         } else {
119             border = getNoFocusBorder();
120         }
121     setBorder(border);
122
123     return this;
124     }
125
126
127     /**
128      * Overridden for performance reasons.
129      * See the <a HREF="#override">Implementation Note</a>
130      * for more information.
131      *
132      * @since 1.5
133      * @return <code>true</code> if the background is completely opaque
134      * and differs from the JList's background;
135      * <code>false</code> otherwise
136      */

137     public boolean isOpaque() {
138     Color JavaDoc back = getBackground();
139     Component JavaDoc p = getParent();
140     if (p != null) {
141         p = p.getParent();
142     }
143     // p should now be the JList.
144
boolean colorMatch = (back != null) && (p != null) &&
145         back.equals(p.getBackground()) &&
146             p.isOpaque();
147     return !colorMatch && super.isOpaque();
148     }
149
150    /**
151     * Overridden for performance reasons.
152     * See the <a HREF="#override">Implementation Note</a>
153     * for more information.
154     */

155     public void validate() {}
156
157    /**
158     * Overridden for performance reasons.
159     * See the <a HREF="#override">Implementation Note</a>
160     * for more information.
161     *
162     * @since 1.5
163     */

164     public void invalidate() {}
165
166    /**
167     * Overridden for performance reasons.
168     * See the <a HREF="#override">Implementation Note</a>
169     * for more information.
170     *
171     * @since 1.5
172     */

173     public void repaint() {}
174
175    /**
176     * Overridden for performance reasons.
177     * See the <a HREF="#override">Implementation Note</a>
178     * for more information.
179     */

180     public void revalidate() {}
181    /**
182     * Overridden for performance reasons.
183     * See the <a HREF="#override">Implementation Note</a>
184     * for more information.
185     */

186     public void repaint(long tm, int x, int y, int width, int height) {}
187
188    /**
189     * Overridden for performance reasons.
190     * See the <a HREF="#override">Implementation Note</a>
191     * for more information.
192     */

193     public void repaint(Rectangle JavaDoc r) {}
194
195    /**
196     * Overridden for performance reasons.
197     * See the <a HREF="#override">Implementation Note</a>
198     * for more information.
199     */

200     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
201     // Strings get interned...
202
if (propertyName=="text")
203         super.firePropertyChange(propertyName, oldValue, newValue);
204     }
205
206    /**
207     * Overridden for performance reasons.
208     * See the <a HREF="#override">Implementation Note</a>
209     * for more information.
210     */

211     public void firePropertyChange(String JavaDoc propertyName, byte oldValue, byte newValue) {}
212
213    /**
214     * Overridden for performance reasons.
215     * See the <a HREF="#override">Implementation Note</a>
216     * for more information.
217     */

218     public void firePropertyChange(String JavaDoc propertyName, char oldValue, char newValue) {}
219
220    /**
221     * Overridden for performance reasons.
222     * See the <a HREF="#override">Implementation Note</a>
223     * for more information.
224     */

225     public void firePropertyChange(String JavaDoc propertyName, short oldValue, short newValue) {}
226
227    /**
228     * Overridden for performance reasons.
229     * See the <a HREF="#override">Implementation Note</a>
230     * for more information.
231     */

232     public void firePropertyChange(String JavaDoc propertyName, int oldValue, int newValue) {}
233
234    /**
235     * Overridden for performance reasons.
236     * See the <a HREF="#override">Implementation Note</a>
237     * for more information.
238     */

239     public void firePropertyChange(String JavaDoc propertyName, long oldValue, long newValue) {}
240
241    /**
242     * Overridden for performance reasons.
243     * See the <a HREF="#override">Implementation Note</a>
244     * for more information.
245     */

246     public void firePropertyChange(String JavaDoc propertyName, float oldValue, float newValue) {}
247
248    /**
249     * Overridden for performance reasons.
250     * See the <a HREF="#override">Implementation Note</a>
251     * for more information.
252     */

253     public void firePropertyChange(String JavaDoc propertyName, double oldValue, double newValue) {}
254
255    /**
256     * Overridden for performance reasons.
257     * See the <a HREF="#override">Implementation Note</a>
258     * for more information.
259     */

260     public void firePropertyChange(String JavaDoc propertyName, boolean oldValue, boolean newValue) {}
261
262     /**
263      * A subclass of DefaultListCellRenderer that implements UIResource.
264      * DefaultListCellRenderer doesn't implement UIResource
265      * directly so that applications can safely override the
266      * cellRenderer property with DefaultListCellRenderer subclasses.
267      * <p>
268      * <strong>Warning:</strong>
269      * Serialized objects of this class will not be compatible with
270      * future Swing releases. The current serialization support is
271      * appropriate for short term storage or RMI between applications running
272      * the same version of Swing. As of 1.4, support for long term storage
273      * of all JavaBeans<sup><font size="-2">TM</font></sup>
274      * has been added to the <code>java.beans</code> package.
275      * Please see {@link java.beans.XMLEncoder}.
276      */

277     public static class UIResource extends DefaultListCellRenderer JavaDoc
278         implements javax.swing.plaf.UIResource JavaDoc
279     {
280     }
281
282 }
283
Popular Tags