jdk/src/share/classes/javax/swing/CellRendererPane.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class CellRendererPane extends Container implements Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Construct a CellRendererPane object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public CellRendererPane() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Overridden to avoid propagating a invalidate up the tree when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * cell renderer child is configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public void invalidate() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Shouldn't be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void paint(Graphics g) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Shouldn't be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void update(Graphics g) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * If the specified component is already a child of this then we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * bother doing anything - stacking order doesn't matter for cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * renderer components (CellRendererPane doesn't paint anyway).<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected void addImpl(Component x, Object constraints, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (x.getParent() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            super.addImpl(x, constraints, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
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
     * Paint a cell renderer component c on graphics object g.  Before the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * is drawn it's reparented to this (if that's necessary), it's bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * are set to w,h and the graphics object is (effectively) translated to x,y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * If it's a JComponent, double buffering is temporarily turned off. After
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * it's the last renderer component painted, it will not start consuming input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The Container p is the component we're actually drawing on, typically it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * equal to this.getParent(). If shouldValidate is true the component c will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * validated before painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    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
   123
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                Color oldColor = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                g.setColor(p.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                g.fillRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (c.getParent() != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            this.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        c.setBounds(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if(shouldValidate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            c.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        boolean wasDoubleBuffered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            wasDoubleBuffered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            ((JComponent)c).setDoubleBuffered(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        Graphics cg = g.create(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            c.paint(cg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            cg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (wasDoubleBuffered && (c instanceof JComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            ((JComponent)c).setDoubleBuffered(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        c.setBounds(-w, -h, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
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
     * Calls this.paintComponent(g, c, p, x, y, w, h, false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        paintComponent(g, c, p, x, y, w, h, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
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
     * Calls this.paintComponent() with the rectangles x,y,width,height fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        paintComponent(g, c, p, r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
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
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected AccessibleContext accessibleContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Gets the AccessibleContext associated with this CellRendererPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * For CellRendererPanes, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * AccessibleCellRendererPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * A new AccessibleCellRendererPane instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return an AccessibleCellRendererPane that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *         AccessibleContext of this CellRendererPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            accessibleContext = new AccessibleCellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <code>CellRendererPane</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    protected class AccessibleCellRendererPane extends AccessibleAWTContainer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // AccessibleContext methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return AccessibleRole.PANEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    } // inner class AccessibleCellRendererPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}