KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > CellRendererPane


1 /*
2  * @(#)CellRendererPane.java 1.39 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.io.*;
12 import java.beans.PropertyChangeListener JavaDoc;
13 import java.util.Locale JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import javax.accessibility.*;
17
18 /**
19  * This class is inserted in between cell renderers and the components that
20  * use them. It just exists to thwart the repaint() and invalidate() methods
21  * which would otherwise propagate up the tree when the renderer was configured.
22  * It's used by the implementations of JTable, JTree, and JList. For example,
23  * here's how CellRendererPane is used in the code the paints each row
24  * in a JList:
25  * <pre>
26  * cellRendererPane = new CellRendererPane();
27  * ...
28  * Component rendererComponent = renderer.getListCellRendererComponent();
29  * renderer.configureListCellRenderer(dataModel.getElementAt(row), row);
30  * cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h);
31  * </pre>
32  * <p>
33  * A renderer component must override isShowing() and unconditionally return
34  * true to work correctly because the Swing paint does nothing for components
35  * with isShowing false.
36  * <p>
37  * <strong>Warning:</strong>
38  * Serialized objects of this class will not be compatible with
39  * future Swing releases. The current serialization support is
40  * appropriate for short term storage or RMI between applications running
41  * the same version of Swing. As of 1.4, support for long term storage
42  * of all JavaBeans<sup><font size="-2">TM</font></sup>
43  * has been added to the <code>java.beans</code> package.
44  * Please see {@link java.beans.XMLEncoder}.
45  *
46  * @version 1.39 12/19/03
47  * @author Hans Muller
48  */

49 public class CellRendererPane extends Container implements Accessible
50 {
51     /**
52      * Construct a CellRendererPane object.
53      */

54     public CellRendererPane() {
55     super();
56     setLayout(null);
57     setVisible(false);
58     }
59
60     /**
61      * Overridden to avoid propagating a invalidate up the tree when the
62      * cell renderer child is configured.
63      */

64     public void invalidate() { }
65
66
67     /**
68      * Shouldn't be called.
69      */

70     public void paint(Graphics g) { }
71
72   
73     /**
74      * Shouldn't be called.
75      */

76     public void update(Graphics g) { }
77
78
79     /**
80      * If the specified component is already a child of this then we don't
81      * bother doing anything - stacking order doesn't matter for cell
82      * renderer components (CellRendererPane doesn't paint anyway).<
83      */

84     protected void addImpl(Component x, Object JavaDoc constraints, int index) {
85     if (x.getParent() == this) {
86         return;
87     }
88     else {
89         super.addImpl(x, constraints, index);
90     }
91     }
92
93
94     /**
95      * Paint a cell renderer component c on graphics object g. Before the component
96      * is drawn it's reparented to this (if that's necessary), it's bounds
97      * are set to w,h and the graphics object is (effectively) translated to x,y.
98      * If it's a JComponent, double buffering is temporarily turned off. After
99      * the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if
100      * it's the last renderer component painted, it will not start consuming input.
101      * The Container p is the component we're actually drawing on, typically it's
102      * equal to this.getParent(). If shouldValidate is true the component c will be
103      * validated before painted.
104      */

105     public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) {
106     if (c == null) {
107         if (p != null) {
108         Color oldColor = g.getColor();
109         g.setColor(p.getBackground());
110         g.fillRect(x, y, w, h);
111         g.setColor(oldColor);
112         }
113         return;
114     }
115
116     if (c.getParent() != this) {
117         this.add(c);
118     }
119
120     c.setBounds(x, y, w, h);
121
122     if(shouldValidate) {
123         c.validate();
124     }
125
126     boolean wasDoubleBuffered = false;
127     if ((c instanceof JComponent JavaDoc) && ((JComponent JavaDoc)c).isDoubleBuffered()) {
128         wasDoubleBuffered = true;
129         ((JComponent JavaDoc)c).setDoubleBuffered(false);
130     }
131
132     Graphics cg = g.create(x, y, w, h);
133     try {
134         c.paint(cg);
135     }
136     finally {
137         cg.dispose();
138     }
139
140     if (wasDoubleBuffered && (c instanceof JComponent JavaDoc)) {
141         ((JComponent JavaDoc)c).setDoubleBuffered(true);
142     }
143
144     c.setBounds(-w, -h, 0, 0);
145     }
146
147
148     /**
149      * Calls this.paintComponent(g, c, p, x, y, w, h, false).
150      */

151     public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
152     paintComponent(g, c, p, x, y, w, h, false);
153     }
154
155
156     /**
157      * Calls this.paintComponent() with the rectangles x,y,width,height fields.
158      */

159     public void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
160     paintComponent(g, c, p, r.x, r.y, r.width, r.height);
161     }
162
163
164     private void writeObject(ObjectOutputStream s) throws IOException {
165     removeAll();
166     s.defaultWriteObject();
167     }
168
169
170 /////////////////
171
// Accessibility support
172
////////////////
173

174     protected AccessibleContext accessibleContext = null;
175
176     /**
177      * Gets the AccessibleContext associated with this CellRendererPane.
178      * For CellRendererPanes, the AccessibleContext takes the form of an
179      * AccessibleCellRendererPane.
180      * A new AccessibleCellRendererPane instance is created if necessary.
181      *
182      * @return an AccessibleCellRendererPane that serves as the
183      * AccessibleContext of this CellRendererPane
184      */

185     public AccessibleContext getAccessibleContext() {
186     if (accessibleContext == null) {
187         accessibleContext = new AccessibleCellRendererPane();
188     }
189     return accessibleContext;
190     }
191
192     /**
193      * This class implements accessibility support for the
194      * <code>CellRendererPane</code> class.
195      */

196     protected class AccessibleCellRendererPane extends AccessibleAWTContainer {
197         // AccessibleContext methods
198
//
199
/**
200          * Get the role of this object.
201          *
202          * @return an instance of AccessibleRole describing the role of the
203      * object
204          * @see AccessibleRole
205          */

206         public AccessibleRole getAccessibleRole() {
207         return AccessibleRole.PANEL;
208         }
209     } // inner class AccessibleCellRendererPane
210
}
211
212
213
Popular Tags