author | darcy |
Fri, 24 Jan 2014 07:16:53 -0800 | |
changeset 22574 | 7f8ce0c8c20a |
parent 21592 | da6abe91602a |
child 25201 | 4adc75e0c4e5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
21592
diff
changeset
|
2 |
* Copyright (c) 1997, 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 |
package javax.swing; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.event.*; |
|
29 |
import java.io.*; |
|
30 |
import java.beans.PropertyChangeListener; |
|
31 |
import java.util.Locale; |
|
32 |
import java.util.Vector; |
|
33 |
||
34 |
import javax.accessibility.*; |
|
35 |
||
36 |
/** |
|
37 |
* This class is inserted in between cell renderers and the components that |
|
38 |
* use them. It just exists to thwart the repaint() and invalidate() methods |
|
39 |
* which would otherwise propagate up the tree when the renderer was configured. |
|
40 |
* It's used by the implementations of JTable, JTree, and JList. For example, |
|
41 |
* here's how CellRendererPane is used in the code the paints each row |
|
42 |
* in a JList: |
|
43 |
* <pre> |
|
44 |
* cellRendererPane = new CellRendererPane(); |
|
45 |
* ... |
|
46 |
* Component rendererComponent = renderer.getListCellRendererComponent(); |
|
47 |
* renderer.configureListCellRenderer(dataModel.getElementAt(row), row); |
|
48 |
* cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h); |
|
49 |
* </pre> |
|
50 |
* <p> |
|
51 |
* A renderer component must override isShowing() and unconditionally return |
|
52 |
* true to work correctly because the Swing paint does nothing for components |
|
53 |
* with isShowing false. |
|
54 |
* <p> |
|
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 |
|
20458 | 60 |
* of all JavaBeans™ |
2 | 61 |
* has been added to the <code>java.beans</code> package. |
62 |
* Please see {@link java.beans.XMLEncoder}. |
|
63 |
* |
|
64 |
* @author Hans Muller |
|
65 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
21592
diff
changeset
|
66 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 67 |
public class CellRendererPane extends Container implements Accessible |
68 |
{ |
|
69 |
/** |
|
70 |
* Construct a CellRendererPane object. |
|
71 |
*/ |
|
72 |
public CellRendererPane() { |
|
73 |
super(); |
|
74 |
setLayout(null); |
|
75 |
setVisible(false); |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Overridden to avoid propagating a invalidate up the tree when the |
|
80 |
* cell renderer child is configured. |
|
81 |
*/ |
|
82 |
public void invalidate() { } |
|
83 |
||
84 |
||
85 |
/** |
|
86 |
* Shouldn't be called. |
|
87 |
*/ |
|
88 |
public void paint(Graphics g) { } |
|
89 |
||
90 |
||
91 |
/** |
|
92 |
* Shouldn't be called. |
|
93 |
*/ |
|
94 |
public void update(Graphics g) { } |
|
95 |
||
96 |
||
97 |
/** |
|
98 |
* If the specified component is already a child of this then we don't |
|
99 |
* bother doing anything - stacking order doesn't matter for cell |
|
21592 | 100 |
* renderer components (CellRendererPane doesn't paint anyway). |
2 | 101 |
*/ |
102 |
protected void addImpl(Component x, Object constraints, int index) { |
|
103 |
if (x.getParent() == this) { |
|
104 |
return; |
|
105 |
} |
|
106 |
else { |
|
107 |
super.addImpl(x, constraints, index); |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
||
112 |
/** |
|
113 |
* Paint a cell renderer component c on graphics object g. Before the component |
|
114 |
* is drawn it's reparented to this (if that's necessary), it's bounds |
|
115 |
* are set to w,h and the graphics object is (effectively) translated to x,y. |
|
116 |
* If it's a JComponent, double buffering is temporarily turned off. After |
|
117 |
* the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if |
|
118 |
* it's the last renderer component painted, it will not start consuming input. |
|
119 |
* The Container p is the component we're actually drawing on, typically it's |
|
120 |
* equal to this.getParent(). If shouldValidate is true the component c will be |
|
121 |
* validated before painted. |
|
122 |
*/ |
|
123 |
public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) { |
|
124 |
if (c == null) { |
|
125 |
if (p != null) { |
|
126 |
Color oldColor = g.getColor(); |
|
127 |
g.setColor(p.getBackground()); |
|
128 |
g.fillRect(x, y, w, h); |
|
129 |
g.setColor(oldColor); |
|
130 |
} |
|
131 |
return; |
|
132 |
} |
|
133 |
||
134 |
if (c.getParent() != this) { |
|
135 |
this.add(c); |
|
136 |
} |
|
137 |
||
138 |
c.setBounds(x, y, w, h); |
|
139 |
||
140 |
if(shouldValidate) { |
|
141 |
c.validate(); |
|
142 |
} |
|
143 |
||
144 |
boolean wasDoubleBuffered = false; |
|
145 |
if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) { |
|
146 |
wasDoubleBuffered = true; |
|
147 |
((JComponent)c).setDoubleBuffered(false); |
|
148 |
} |
|
149 |
||
150 |
Graphics cg = g.create(x, y, w, h); |
|
151 |
try { |
|
152 |
c.paint(cg); |
|
153 |
} |
|
154 |
finally { |
|
155 |
cg.dispose(); |
|
156 |
} |
|
157 |
||
158 |
if (wasDoubleBuffered && (c instanceof JComponent)) { |
|
159 |
((JComponent)c).setDoubleBuffered(true); |
|
160 |
} |
|
161 |
||
162 |
c.setBounds(-w, -h, 0, 0); |
|
163 |
} |
|
164 |
||
165 |
||
166 |
/** |
|
167 |
* Calls this.paintComponent(g, c, p, x, y, w, h, false). |
|
168 |
*/ |
|
169 |
public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) { |
|
170 |
paintComponent(g, c, p, x, y, w, h, false); |
|
171 |
} |
|
172 |
||
173 |
||
174 |
/** |
|
175 |
* Calls this.paintComponent() with the rectangles x,y,width,height fields. |
|
176 |
*/ |
|
177 |
public void paintComponent(Graphics g, Component c, Container p, Rectangle r) { |
|
178 |
paintComponent(g, c, p, r.x, r.y, r.width, r.height); |
|
179 |
} |
|
180 |
||
181 |
||
182 |
private void writeObject(ObjectOutputStream s) throws IOException { |
|
183 |
removeAll(); |
|
184 |
s.defaultWriteObject(); |
|
185 |
} |
|
186 |
||
187 |
||
188 |
///////////////// |
|
189 |
// Accessibility support |
|
190 |
//////////////// |
|
191 |
||
192 |
protected AccessibleContext accessibleContext = null; |
|
193 |
||
194 |
/** |
|
195 |
* Gets the AccessibleContext associated with this CellRendererPane. |
|
196 |
* For CellRendererPanes, the AccessibleContext takes the form of an |
|
197 |
* AccessibleCellRendererPane. |
|
198 |
* A new AccessibleCellRendererPane instance is created if necessary. |
|
199 |
* |
|
200 |
* @return an AccessibleCellRendererPane that serves as the |
|
201 |
* AccessibleContext of this CellRendererPane |
|
202 |
*/ |
|
203 |
public AccessibleContext getAccessibleContext() { |
|
204 |
if (accessibleContext == null) { |
|
205 |
accessibleContext = new AccessibleCellRendererPane(); |
|
206 |
} |
|
207 |
return accessibleContext; |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* This class implements accessibility support for the |
|
212 |
* <code>CellRendererPane</code> class. |
|
213 |
*/ |
|
214 |
protected class AccessibleCellRendererPane extends AccessibleAWTContainer { |
|
215 |
// AccessibleContext methods |
|
216 |
// |
|
217 |
/** |
|
218 |
* Get the role of this object. |
|
219 |
* |
|
220 |
* @return an instance of AccessibleRole describing the role of the |
|
221 |
* object |
|
222 |
* @see AccessibleRole |
|
223 |
*/ |
|
224 |
public AccessibleRole getAccessibleRole() { |
|
225 |
return AccessibleRole.PANEL; |
|
226 |
} |
|
227 |
} // inner class AccessibleCellRendererPane |
|
228 |
} |