Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/CellRendererPane.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package javax.swing;2526import java.awt.*;27import java.awt.event.*;28import java.io.*;29import java.beans.PropertyChangeListener;30import java.util.Locale;31import java.util.Vector;3233import javax.accessibility.*;3435/**36* This class is inserted in between cell renderers and the components that37* use them. It just exists to thwart the repaint() and invalidate() methods38* which would otherwise propagate up the tree when the renderer was configured.39* It's used by the implementations of JTable, JTree, and JList. For example,40* here's how CellRendererPane is used in the code the paints each row41* in a JList:42* <pre>43* cellRendererPane = new CellRendererPane();44* ...45* Component rendererComponent = renderer.getListCellRendererComponent();46* renderer.configureListCellRenderer(dataModel.getElementAt(row), row);47* cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h);48* </pre>49* <p>50* A renderer component must override isShowing() and unconditionally return51* true to work correctly because the Swing paint does nothing for components52* with isShowing false.53* <p>54* <strong>Warning:</strong>55* Serialized objects of this class will not be compatible with56* future Swing releases. The current serialization support is57* appropriate for short term storage or RMI between applications running58* the same version of Swing. As of 1.4, support for long term storage59* of all JavaBeans™60* has been added to the <code>java.beans</code> package.61* Please see {@link java.beans.XMLEncoder}.62*63* @author Hans Muller64*/65public class CellRendererPane extends Container implements Accessible66{67/**68* Construct a CellRendererPane object.69*/70public CellRendererPane() {71super();72setLayout(null);73setVisible(false);74}7576/**77* Overridden to avoid propagating a invalidate up the tree when the78* cell renderer child is configured.79*/80public void invalidate() { }818283/**84* Shouldn't be called.85*/86public void paint(Graphics g) { }878889/**90* Shouldn't be called.91*/92public void update(Graphics g) { }939495/**96* If the specified component is already a child of this then we don't97* bother doing anything - stacking order doesn't matter for cell98* renderer components (CellRendererPane doesn't paint anyway).99*/100protected void addImpl(Component x, Object constraints, int index) {101if (x.getParent() == this) {102return;103}104else {105super.addImpl(x, constraints, index);106}107}108109110/**111* Paint a cell renderer component c on graphics object g. Before the component112* is drawn it's reparented to this (if that's necessary), it's bounds113* are set to w,h and the graphics object is (effectively) translated to x,y.114* If it's a JComponent, double buffering is temporarily turned off. After115* the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if116* it's the last renderer component painted, it will not start consuming input.117* The Container p is the component we're actually drawing on, typically it's118* equal to this.getParent(). If shouldValidate is true the component c will be119* validated before painted.120*/121public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) {122if (c == null) {123if (p != null) {124Color oldColor = g.getColor();125g.setColor(p.getBackground());126g.fillRect(x, y, w, h);127g.setColor(oldColor);128}129return;130}131132if (c.getParent() != this) {133this.add(c);134}135136c.setBounds(x, y, w, h);137138if(shouldValidate) {139c.validate();140}141142boolean wasDoubleBuffered = false;143if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) {144wasDoubleBuffered = true;145((JComponent)c).setDoubleBuffered(false);146}147148Graphics cg = g.create(x, y, w, h);149try {150c.paint(cg);151}152finally {153cg.dispose();154}155156if (wasDoubleBuffered && (c instanceof JComponent)) {157((JComponent)c).setDoubleBuffered(true);158}159160c.setBounds(-w, -h, 0, 0);161}162163164/**165* Calls this.paintComponent(g, c, p, x, y, w, h, false).166*/167public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {168paintComponent(g, c, p, x, y, w, h, false);169}170171172/**173* Calls this.paintComponent() with the rectangles x,y,width,height fields.174*/175public void paintComponent(Graphics g, Component c, Container p, Rectangle r) {176paintComponent(g, c, p, r.x, r.y, r.width, r.height);177}178179180private void writeObject(ObjectOutputStream s) throws IOException {181removeAll();182s.defaultWriteObject();183}184185186/////////////////187// Accessibility support188////////////////189190protected AccessibleContext accessibleContext = null;191192/**193* Gets the AccessibleContext associated with this CellRendererPane.194* For CellRendererPanes, the AccessibleContext takes the form of an195* AccessibleCellRendererPane.196* A new AccessibleCellRendererPane instance is created if necessary.197*198* @return an AccessibleCellRendererPane that serves as the199* AccessibleContext of this CellRendererPane200*/201public AccessibleContext getAccessibleContext() {202if (accessibleContext == null) {203accessibleContext = new AccessibleCellRendererPane();204}205return accessibleContext;206}207208/**209* This class implements accessibility support for the210* <code>CellRendererPane</code> class.211*/212protected class AccessibleCellRendererPane extends AccessibleAWTContainer {213// AccessibleContext methods214//215/**216* Get the role of this object.217*218* @return an instance of AccessibleRole describing the role of the219* object220* @see AccessibleRole221*/222public AccessibleRole getAccessibleRole() {223return AccessibleRole.PANEL;224}225} // inner class AccessibleCellRendererPane226}227228229