Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/swing/CellRendererPane.java
41153 views
1
/*
2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package javax.swing;
27
28
import java.awt.Color;
29
import java.awt.Component;
30
import java.awt.Container;
31
import java.awt.Graphics;
32
import java.awt.Rectangle;
33
import java.io.IOException;
34
import java.io.ObjectOutputStream;
35
import java.io.Serial;
36
37
import javax.accessibility.Accessible;
38
import javax.accessibility.AccessibleContext;
39
import javax.accessibility.AccessibleRole;
40
41
/**
42
* This class is inserted in between cell renderers and the components that
43
* use them. It just exists to thwart the repaint() and invalidate() methods
44
* which would otherwise propagate up the tree when the renderer was configured.
45
* It's used by the implementations of JTable, JTree, and JList. For example,
46
* here's how CellRendererPane is used in the code the paints each row
47
* in a JList:
48
* <pre>
49
* cellRendererPane = new CellRendererPane();
50
* ...
51
* Component rendererComponent = renderer.getListCellRendererComponent();
52
* renderer.configureListCellRenderer(dataModel.getElementAt(row), row);
53
* cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h);
54
* </pre>
55
* <p>
56
* A renderer component must override isShowing() and unconditionally return
57
* true to work correctly because the Swing paint does nothing for components
58
* with isShowing false.
59
* <p>
60
* <strong>Warning:</strong>
61
* Serialized objects of this class will not be compatible with
62
* future Swing releases. The current serialization support is
63
* appropriate for short term storage or RMI between applications running
64
* the same version of Swing. As of 1.4, support for long term storage
65
* of all JavaBeans
66
* has been added to the <code>java.beans</code> package.
67
* Please see {@link java.beans.XMLEncoder}.
68
*
69
* @author Hans Muller
70
* @since 1.2
71
*/
72
@SuppressWarnings("serial") // Same-version serialization only
73
public class CellRendererPane extends Container implements Accessible
74
{
75
/**
76
* Construct a CellRendererPane object.
77
*/
78
public CellRendererPane() {
79
super();
80
setLayout(null);
81
setVisible(false);
82
}
83
84
/**
85
* Overridden to avoid propagating a invalidate up the tree when the
86
* cell renderer child is configured.
87
*/
88
public void invalidate() { }
89
90
91
/**
92
* Shouldn't be called.
93
*/
94
public void paint(Graphics g) { }
95
96
97
/**
98
* Shouldn't be called.
99
*/
100
public void update(Graphics g) { }
101
102
103
/**
104
* If the specified component is already a child of this then we don't
105
* bother doing anything - stacking order doesn't matter for cell
106
* renderer components (CellRendererPane doesn't paint anyway).
107
*/
108
protected void addImpl(Component x, Object constraints, int index) {
109
if (x.getParent() == this) {
110
return;
111
}
112
else {
113
super.addImpl(x, constraints, index);
114
}
115
}
116
117
118
/**
119
* Paint a cell renderer component c on graphics object g. Before the component
120
* is drawn it's reparented to this (if that's necessary), it's bounds
121
* are set to w,h and the graphics object is (effectively) translated to x,y.
122
* If it's a JComponent, double buffering is temporarily turned off. After
123
* the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if
124
* it's the last renderer component painted, it will not start consuming input.
125
* The Container p is the component we're actually drawing on, typically it's
126
* equal to this.getParent(). If shouldValidate is true the component c will be
127
* validated before painted.
128
*
129
* @param g the {@code Graphics} object to draw on
130
* @param c the {@code Component} to draw
131
* @param p the {@code Container} component actually drawn on
132
* @param x an int specifying the left side of the area draw in, in pixels,
133
* measured from the left edge of the graphics context
134
* @param y an int specifying the top of the area to draw in, in pixels
135
* measured down from the top edge of the graphics context
136
* @param w an int specifying the width of the area draw in, in pixels
137
* @param h an int specifying the height of the area draw in, in pixels
138
* @param shouldValidate if true, component {@code c} will be validated
139
* before being painted
140
*/
141
public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) {
142
if (c == null) {
143
if (p != null) {
144
Color oldColor = g.getColor();
145
g.setColor(p.getBackground());
146
g.fillRect(x, y, w, h);
147
g.setColor(oldColor);
148
}
149
return;
150
}
151
152
if (c.getParent() != this) {
153
this.add(c);
154
}
155
156
c.setBounds(x, y, w, h);
157
158
if(shouldValidate) {
159
c.validate();
160
}
161
162
boolean wasDoubleBuffered = false;
163
if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) {
164
wasDoubleBuffered = true;
165
((JComponent)c).setDoubleBuffered(false);
166
}
167
168
Graphics cg = g.create(x, y, w, h);
169
try {
170
c.paint(cg);
171
}
172
finally {
173
cg.dispose();
174
}
175
176
if (wasDoubleBuffered && (c instanceof JComponent)) {
177
((JComponent)c).setDoubleBuffered(true);
178
}
179
180
c.setBounds(-w, -h, 0, 0);
181
}
182
183
184
/**
185
* Calls this.paintComponent(g, c, p, x, y, w, h, false).
186
*
187
* @param g the {@code Graphics} object to draw on
188
* @param c the {@code Component} to draw
189
* @param p the {@code Container} component actually drawn on
190
* @param x an int specifying the left side of the area draw in, in pixels,
191
* measured from the left edge of the graphics context
192
* @param y an int specifying the top of the area to draw in, in pixels
193
* measured down from the top edge of the graphics context
194
* @param w an int specifying the width of the area draw in, in pixels
195
* @param h an int specifying the height of the area draw in, in pixels
196
*/
197
public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
198
paintComponent(g, c, p, x, y, w, h, false);
199
}
200
201
202
/**
203
* Calls this.paintComponent(g, c, p, r.x, r.y, r.width, r.height) where
204
* {@code r} is the input {@code Rectangle} parameter.
205
*
206
* @param g the {@code Graphics} object to draw on
207
* @param c the {@code Component} to draw
208
* @param p the {@code Container} component actually drawn on
209
* @param r the {@code Rectangle} to draw in
210
*/
211
public void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
212
paintComponent(g, c, p, r.x, r.y, r.width, r.height);
213
}
214
215
216
@Serial
217
private void writeObject(ObjectOutputStream s) throws IOException {
218
removeAll();
219
s.defaultWriteObject();
220
}
221
222
223
/////////////////
224
// Accessibility support
225
////////////////
226
227
/**
228
* {@code AccessibleContext} associated with this {@code CellRendererPan}
229
*/
230
protected AccessibleContext accessibleContext = null;
231
232
233
/**
234
* Gets the AccessibleContext associated with this CellRendererPane.
235
* For CellRendererPanes, the AccessibleContext takes the form of an
236
* AccessibleCellRendererPane.
237
* A new AccessibleCellRendererPane instance is created if necessary.
238
*
239
* @return an AccessibleCellRendererPane that serves as the
240
* AccessibleContext of this CellRendererPane
241
*/
242
public AccessibleContext getAccessibleContext() {
243
if (accessibleContext == null) {
244
accessibleContext = new AccessibleCellRendererPane();
245
}
246
return accessibleContext;
247
}
248
249
/**
250
* This class implements accessibility support for the
251
* <code>CellRendererPane</code> class.
252
*/
253
protected class AccessibleCellRendererPane extends AccessibleAWTContainer {
254
255
/**
256
* Constructs an {@code AccessibleCellRendererPane}.
257
*/
258
protected AccessibleCellRendererPane() {}
259
260
// AccessibleContext methods
261
//
262
/**
263
* Get the role of this object.
264
*
265
* @return an instance of AccessibleRole describing the role of the
266
* object
267
* @see AccessibleRole
268
*/
269
public AccessibleRole getAccessibleRole() {
270
return AccessibleRole.PANEL;
271
}
272
} // inner class AccessibleCellRendererPane
273
}
274
275