jdk/src/share/classes/javax/swing/CellRendererPane.java
author rriggs
Tue, 03 Jun 2014 11:18:03 -0400
changeset 25122 1ecc464c69d2
parent 22574 7f8ce0c8c20a
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8044460: Cleanup new Boolean and single character strings Reviewed-by: pchelko, serb, rriggs Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class is inserted in between cell renderers and the components that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * use them.  It just exists to thwart the repaint() and invalidate() methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * which would otherwise propagate up the tree when the renderer was configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * It's used by the implementations of JTable, JTree, and JList.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * here's how CellRendererPane is used in the code the paints each row
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * in a JList:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   cellRendererPane = new CellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   Component rendererComponent = renderer.getListCellRendererComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   renderer.configureListCellRenderer(dataModel.getElementAt(row), row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * A renderer component must override isShowing() and unconditionally return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * true to work correctly because the Swing paint does nothing for components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * with isShowing false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    60
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class CellRendererPane extends Container implements Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Construct a CellRendererPane object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public CellRendererPane() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Overridden to avoid propagating a invalidate up the tree when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * cell renderer child is configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void invalidate() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Shouldn't be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void paint(Graphics g) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Shouldn't be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void update(Graphics g) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * If the specified component is already a child of this then we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * bother doing anything - stacking order doesn't matter for cell
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 20458
diff changeset
   100
     * renderer components (CellRendererPane doesn't paint anyway).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected void addImpl(Component x, Object constraints, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (x.getParent() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            super.addImpl(x, constraints, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Paint a cell renderer component c on graphics object g.  Before the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * is drawn it's reparented to this (if that's necessary), it's bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * are set to w,h and the graphics object is (effectively) translated to x,y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * If it's a JComponent, double buffering is temporarily turned off. After
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * it's the last renderer component painted, it will not start consuming input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The Container p is the component we're actually drawing on, typically it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * equal to this.getParent(). If shouldValidate is true the component c will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * validated before painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                Color oldColor = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                g.setColor(p.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                g.fillRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (c.getParent() != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            this.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        c.setBounds(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if(shouldValidate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            c.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        boolean wasDoubleBuffered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            wasDoubleBuffered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            ((JComponent)c).setDoubleBuffered(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Graphics cg = g.create(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            c.paint(cg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            cg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (wasDoubleBuffered && (c instanceof JComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            ((JComponent)c).setDoubleBuffered(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        c.setBounds(-w, -h, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Calls this.paintComponent(g, c, p, x, y, w, h, false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        paintComponent(g, c, p, x, y, w, h, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Calls this.paintComponent() with the rectangles x,y,width,height fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        paintComponent(g, c, p, r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    protected AccessibleContext accessibleContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Gets the AccessibleContext associated with this CellRendererPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * For CellRendererPanes, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * AccessibleCellRendererPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * A new AccessibleCellRendererPane instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return an AccessibleCellRendererPane that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *         AccessibleContext of this CellRendererPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            accessibleContext = new AccessibleCellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <code>CellRendererPane</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    protected class AccessibleCellRendererPane extends AccessibleAWTContainer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // AccessibleContext methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return AccessibleRole.PANEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    } // inner class AccessibleCellRendererPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
}