- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathCellRendererPane.java
273 lines (239 loc) · 9.49 KB
/
CellRendererPane.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing;
importjava.awt.Color;
importjava.awt.Component;
importjava.awt.Container;
importjava.awt.Graphics;
importjava.awt.Rectangle;
importjava.io.IOException;
importjava.io.ObjectOutputStream;
importjava.io.Serial;
importjavax.accessibility.Accessible;
importjavax.accessibility.AccessibleContext;
importjavax.accessibility.AccessibleRole;
/**
* This class is inserted in between cell renderers and the components that
* use them. It just exists to thwart the repaint() and invalidate() methods
* which would otherwise propagate up the tree when the renderer was configured.
* It's used by the implementations of JTable, JTree, and JList. For example,
* here's how CellRendererPane is used in the code the paints each row
* in a JList:
* <pre>
* cellRendererPane = new CellRendererPane();
* ...
* Component rendererComponent = renderer.getListCellRendererComponent();
* renderer.configureListCellRenderer(dataModel.getElementAt(row), row);
* cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h);
* </pre>
* <p>
* A renderer component must override isShowing() and unconditionally return
* true to work correctly because the Swing paint does nothing for components
* with isShowing false.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @author Hans Muller
* @since 1.2
*/
@SuppressWarnings("serial") // Same-version serialization only
publicclassCellRendererPaneextendsContainerimplementsAccessible
{
/**
* Construct a CellRendererPane object.
*/
publicCellRendererPane() {
super();
setLayout(null);
setVisible(false);
}
/**
* Overridden to avoid propagating a invalidate up the tree when the
* cell renderer child is configured.
*/
publicvoidinvalidate() { }
/**
* Shouldn't be called.
*/
publicvoidpaint(Graphicsg) { }
/**
* Shouldn't be called.
*/
publicvoidupdate(Graphicsg) { }
/**
* If the specified component is already a child of this then we don't
* bother doing anything - stacking order doesn't matter for cell
* renderer components (CellRendererPane doesn't paint anyway).
*/
protectedvoidaddImpl(Componentx, Objectconstraints, intindex) {
if (x.getParent() == this) {
return;
}
else {
super.addImpl(x, constraints, index);
}
}
/**
* Paint a cell renderer component c on graphics object g. Before the component
* is drawn it's reparented to this (if that's necessary), it's bounds
* are set to w,h and the graphics object is (effectively) translated to x,y.
* If it's a JComponent, double buffering is temporarily turned off. After
* the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if
* it's the last renderer component painted, it will not start consuming input.
* The Container p is the component we're actually drawing on, typically it's
* equal to this.getParent(). If shouldValidate is true the component c will be
* validated before painted.
*
* @param g the {@code Graphics} object to draw on
* @param c the {@code Component} to draw
* @param p the {@code Container} component actually drawn on
* @param x an int specifying the left side of the area draw in, in pixels,
* measured from the left edge of the graphics context
* @param y an int specifying the top of the area to draw in, in pixels
* measured down from the top edge of the graphics context
* @param w an int specifying the width of the area draw in, in pixels
* @param h an int specifying the height of the area draw in, in pixels
* @param shouldValidate if true, component {@code c} will be validated
* before being painted
*/
publicvoidpaintComponent(Graphicsg, Componentc, Containerp, intx, inty, intw, inth, booleanshouldValidate) {
if (c == null) {
if (p != null) {
ColoroldColor = g.getColor();
g.setColor(p.getBackground());
g.fillRect(x, y, w, h);
g.setColor(oldColor);
}
return;
}
if (c.getParent() != this) {
this.add(c);
}
c.setBounds(x, y, w, h);
if(shouldValidate) {
c.validate();
}
booleanwasDoubleBuffered = false;
if ((cinstanceofJComponent) && ((JComponent)c).isDoubleBuffered()) {
wasDoubleBuffered = true;
((JComponent)c).setDoubleBuffered(false);
}
Graphicscg = g.create(x, y, w, h);
try {
c.paint(cg);
}
finally {
cg.dispose();
}
if (wasDoubleBuffered && (cinstanceofJComponent)) {
((JComponent)c).setDoubleBuffered(true);
}
c.setBounds(-w, -h, 0, 0);
}
/**
* Calls this.paintComponent(g, c, p, x, y, w, h, false).
*
* @param g the {@code Graphics} object to draw on
* @param c the {@code Component} to draw
* @param p the {@code Container} component actually drawn on
* @param x an int specifying the left side of the area draw in, in pixels,
* measured from the left edge of the graphics context
* @param y an int specifying the top of the area to draw in, in pixels
* measured down from the top edge of the graphics context
* @param w an int specifying the width of the area draw in, in pixels
* @param h an int specifying the height of the area draw in, in pixels
*/
publicvoidpaintComponent(Graphicsg, Componentc, Containerp, intx, inty, intw, inth) {
paintComponent(g, c, p, x, y, w, h, false);
}
/**
* Calls this.paintComponent(g, c, p, r.x, r.y, r.width, r.height) where
* {@code r} is the input {@code Rectangle} parameter.
*
* @param g the {@code Graphics} object to draw on
* @param c the {@code Component} to draw
* @param p the {@code Container} component actually drawn on
* @param r the {@code Rectangle} to draw in
*/
publicvoidpaintComponent(Graphicsg, Componentc, Containerp, Rectangler) {
paintComponent(g, c, p, r.x, r.y, r.width, r.height);
}
@Serial
privatevoidwriteObject(ObjectOutputStreams) throwsIOException {
removeAll();
s.defaultWriteObject();
}
/////////////////
// Accessibility support
////////////////
/**
* {@code AccessibleContext} associated with this {@code CellRendererPan}
*/
protectedAccessibleContextaccessibleContext = null;
/**
* Gets the AccessibleContext associated with this CellRendererPane.
* For CellRendererPanes, the AccessibleContext takes the form of an
* AccessibleCellRendererPane.
* A new AccessibleCellRendererPane instance is created if necessary.
*
* @return an AccessibleCellRendererPane that serves as the
* AccessibleContext of this CellRendererPane
*/
publicAccessibleContextgetAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = newAccessibleCellRendererPane();
}
returnaccessibleContext;
}
/**
* This class implements accessibility support for the
* <code>CellRendererPane</code> class.
*/
protectedclassAccessibleCellRendererPaneextendsAccessibleAWTContainer {
/**
* Constructs an {@code AccessibleCellRendererPane}.
*/
protectedAccessibleCellRendererPane() {}
// AccessibleContext methods
//
/**
* Get the role of this object.
*
* @return an instance of AccessibleRole describing the role of the
* object
* @see AccessibleRole
*/
publicAccessibleRolegetAccessibleRole() {
returnAccessibleRole.PANEL;
}
} // inner class AccessibleCellRendererPane
}