KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > table > DefaultTableCellRenderer


1 /*
2  * @(#)DefaultTableCellRenderer.java 1.42 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.table;
9
10 import javax.swing.*;
11 import javax.swing.table.TableCellRenderer JavaDoc;
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  * The standard class for rendering (displaying) individual cells
23  * in a <code>JTable</code>.
24  * <p>
25  *
26  * <strong><a name="override">Implementation Note:</a></strong>
27  * This class inherits from <code>JLabel</code>, a standard component class.
28  * However <code>JTable</code> employs a unique mechanism for rendering
29  * its cells and therefore requires some slightly modified behavior
30  * from its cell renderer.
31  * The table class defines a single cell renderer and uses it as a
32  * as a rubber-stamp for rendering all cells in the table;
33  * it renders the first cell,
34  * changes the contents of that cell renderer,
35  * shifts the origin to the new location, re-draws it, and so on.
36  * The standard <code>JLabel</code> component was not
37  * designed to be used this way and we want to avoid
38  * triggering a <code>revalidate</code> each time the
39  * cell is drawn. This would greatly decrease performance because the
40  * <code>revalidate</code> message would be
41  * passed up the hierarchy of the container to determine whether any other
42  * components would be affected.
43  * As the renderer is only parented for the lifetime of a painting operation
44  * we similarly want to avoid the overhead associated with walking the
45  * hierarchy for painting operations.
46  * So this class
47  * overrides the <code>validate</code>, <code>invalidate</code>,
48  * <code>revalidate</code>, <code>repaint</code>, and
49  * <code>firePropertyChange</code> methods to be
50  * no-ops and override the <code>isOpaque</code> method solely to improve
51  * performance. If you write your own renderer,
52  * please keep this performance consideration in mind.
53  * <p>
54  *
55  * <strong>Warning:</strong>
56  * Serialized objects of this class will not be compatible with
57  * future Swing releases. The current serialization support is
58  * appropriate for short term storage or RMI between applications running
59  * the same version of Swing. As of 1.4, support for long term storage
60  * of all JavaBeans<sup><font size="-2">TM</font></sup>
61  * has been added to the <code>java.beans</code> package.
62  * Please see {@link java.beans.XMLEncoder}.
63  *
64  * @version 1.42 10/31/05
65  * @author Philip Milne
66  * @see JTable
67  */

68 public class DefaultTableCellRenderer extends JLabel
69     implements TableCellRenderer JavaDoc, Serializable JavaDoc
70 {
71
72     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
73     private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
74
75     // We need a place to store the color the JLabel should be returned
76
// to after its foreground and background colors have been set
77
// to the selection background color.
78
// These ivars will be made protected when their names are finalized.
79
private Color JavaDoc unselectedForeground;
80     private Color JavaDoc unselectedBackground;
81
82     /**
83      * Creates a default table cell renderer.
84      */

85     public DefaultTableCellRenderer() {
86     super();
87     setOpaque(true);
88         setBorder(getNoFocusBorder());
89     }
90
91     private static Border getNoFocusBorder() {
92         if (System.getSecurityManager() != null) {
93             return SAFE_NO_FOCUS_BORDER;
94         } else {
95             return noFocusBorder;
96         }
97     }
98
99     /**
100      * Overrides <code>JComponent.setForeground</code> to assign
101      * the unselected-foreground color to the specified color.
102      *
103      * @param c set the foreground color to this value
104      */

105     public void setForeground(Color JavaDoc c) {
106         super.setForeground(c);
107         unselectedForeground = c;
108     }
109     
110     /**
111      * Overrides <code>JComponent.setBackground</code> to assign
112      * the unselected-background color to the specified color.
113      *
114      * @param c set the background color to this value
115      */

116     public void setBackground(Color JavaDoc c) {
117         super.setBackground(c);
118         unselectedBackground = c;
119     }
120
121     /**
122      * Notification from the <code>UIManager</code> that the look and feel
123      * [L&F] has changed.
124      * Replaces the current UI object with the latest version from the
125      * <code>UIManager</code>.
126      *
127      * @see JComponent#updateUI
128      */

129     public void updateUI() {
130         super.updateUI();
131     setForeground(null);
132     setBackground(null);
133     }
134     
135     // implements javax.swing.table.TableCellRenderer
136
/**
137      *
138      * Returns the default table cell renderer.
139      *
140      * @param table the <code>JTable</code>
141      * @param value the value to assign to the cell at
142      * <code>[row, column]</code>
143      * @param isSelected true if cell is selected
144      * @param hasFocus true if cell has focus
145      * @param row the row of the cell to render
146      * @param column the column of the cell to render
147      * @return the default table cell renderer
148      */

149     public Component JavaDoc getTableCellRendererComponent(JTable table, Object JavaDoc value,
150                           boolean isSelected, boolean hasFocus, int row, int column) {
151
152     if (isSelected) {
153        super.setForeground(table.getSelectionForeground());
154        super.setBackground(table.getSelectionBackground());
155     }
156     else {
157         super.setForeground((unselectedForeground != null) ? unselectedForeground
158                                                            : table.getForeground());
159         super.setBackground((unselectedBackground != null) ? unselectedBackground
160                                                            : table.getBackground());
161     }
162     
163     setFont(table.getFont());
164
165     if (hasFocus) {
166             Border border = null;
167             if (isSelected) {
168                 border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
169             }
170             if (border == null) {
171                 border = UIManager.getBorder("Table.focusCellHighlightBorder");
172             }
173             setBorder(border);
174
175         if (!isSelected && table.isCellEditable(row, column)) {
176                 Color JavaDoc col;
177                 col = UIManager.getColor("Table.focusCellForeground");
178                 if (col != null) {
179                     super.setForeground(col);
180                 }
181                 col = UIManager.getColor("Table.focusCellBackground");
182                 if (col != null) {
183                     super.setBackground(col);
184                 }
185         }
186     } else {
187             setBorder(getNoFocusBorder());
188     }
189
190         setValue(value);
191
192     return this;
193     }
194     
195     /*
196      * The following methods are overridden as a performance measure to
197      * to prune code-paths are often called in the case of renders
198      * but which we know are unnecessary. Great care should be taken
199      * when writing your own renderer to weigh the benefits and
200      * drawbacks of overriding methods like these.
201      */

202
203     /**
204      * Overridden for performance reasons.
205      * See the <a HREF="#override">Implementation Note</a>
206      * for more information.
207      */

208     public boolean isOpaque() {
209     Color JavaDoc back = getBackground();
210     Component JavaDoc p = getParent();
211     if (p != null) {
212         p = p.getParent();
213     }
214     // p should now be the JTable.
215
boolean colorMatch = (back != null) && (p != null) &&
216         back.equals(p.getBackground()) &&
217             p.isOpaque();
218     return !colorMatch && super.isOpaque();
219     }
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     public void invalidate() {}
229
230     /**
231      * Overridden for performance reasons.
232      * See the <a HREF="#override">Implementation Note</a>
233      * for more information.
234      */

235     public void validate() {}
236
237     /**
238      * Overridden for performance reasons.
239      * See the <a HREF="#override">Implementation Note</a>
240      * for more information.
241      */

242     public void revalidate() {}
243
244     /**
245      * Overridden for performance reasons.
246      * See the <a HREF="#override">Implementation Note</a>
247      * for more information.
248      */

249     public void repaint(long tm, int x, int y, int width, int height) {}
250
251     /**
252      * Overridden for performance reasons.
253      * See the <a HREF="#override">Implementation Note</a>
254      * for more information.
255      */

256     public void repaint(Rectangle JavaDoc r) { }
257
258     /**
259      * Overridden for performance reasons.
260      * See the <a HREF="#override">Implementation Note</a>
261      * for more information.
262      *
263      * @since 1.5
264      */

265     public void repaint() {
266     }
267
268     /**
269      * Overridden for performance reasons.
270      * See the <a HREF="#override">Implementation Note</a>
271      * for more information.
272      */

273     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
274     // Strings get interned...
275
if (propertyName=="text") {
276         super.firePropertyChange(propertyName, oldValue, newValue);
277     }
278     }
279
280     /**
281      * Overridden for performance reasons.
282      * See the <a HREF="#override">Implementation Note</a>
283      * for more information.
284      */

285     public void firePropertyChange(String JavaDoc propertyName, boolean oldValue, boolean newValue) { }
286
287
288     /**
289      * Sets the <code>String</code> object for the cell being rendered to
290      * <code>value</code>.
291      *
292      * @param value the string value for this cell; if value is
293      * <code>null</code> it sets the text value to an empty string
294      * @see JLabel#setText
295      *
296      */

297     protected void setValue(Object JavaDoc value) {
298     setText((value == null) ? "" : value.toString());
299     }
300     
301
302     /**
303      * A subclass of <code>DefaultTableCellRenderer</code> that
304      * implements <code>UIResource</code>.
305      * <code>DefaultTableCellRenderer</code> doesn't implement
306      * <code>UIResource</code>
307      * directly so that applications can safely override the
308      * <code>cellRenderer</code> property with
309      * <code>DefaultTableCellRenderer</code> subclasses.
310      * <p>
311      * <strong>Warning:</strong>
312      * Serialized objects of this class will not be compatible with
313      * future Swing releases. The current serialization support is
314      * appropriate for short term storage or RMI between applications running
315      * the same version of Swing. As of 1.4, support for long term storage
316      * of all JavaBeans<sup><font size="-2">TM</font></sup>
317      * has been added to the <code>java.beans</code> package.
318      * Please see {@link java.beans.XMLEncoder}.
319      */

320     public static class UIResource extends DefaultTableCellRenderer JavaDoc
321         implements javax.swing.plaf.UIResource JavaDoc
322     {
323     }
324
325 }
326
327
328
Popular Tags