jdk/src/share/classes/javax/swing/table/DefaultTableCellRenderer.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7011 f290783add29
child 20428 929cd48fca8a
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7011
diff changeset
     2
 * Copyright (c) 1998, 2010, 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: 4283
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: 4283
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: 4283
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4283
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4283
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.Serializable;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    36
import sun.swing.DefaultLookup;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The standard class for rendering (displaying) individual cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * in a <code>JTable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <strong><a name="override">Implementation Note:</a></strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class inherits from <code>JLabel</code>, a standard component class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * However <code>JTable</code> employs a unique mechanism for rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * its cells and therefore requires some slightly modified behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * from its cell renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The table class defines a single cell renderer and uses it as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * as a rubber-stamp for rendering all cells in the table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * it renders the first cell,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * changes the contents of that cell renderer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * shifts the origin to the new location, re-draws it, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The standard <code>JLabel</code> component was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * designed to be used this way and we want to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * triggering a <code>revalidate</code> each time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * cell is drawn. This would greatly decrease performance because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>revalidate</code> message would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * passed up the hierarchy of the container to determine whether any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * components would be affected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * As the renderer is only parented for the lifetime of a painting operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * we similarly want to avoid the overhead associated with walking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * hierarchy for painting operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * So this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * overrides the <code>validate</code>, <code>invalidate</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>revalidate</code>, <code>repaint</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <code>firePropertyChange</code> methods to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * no-ops and override the <code>isOpaque</code> method solely to improve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * performance.  If you write your own renderer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * please keep this performance consideration in mind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see JTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public class DefaultTableCellRenderer extends JLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    implements TableCellRenderer, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    * An empty <code>Border</code>. This field might not be used. To change the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    * <code>Border</code> used by this renderer override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    * <code>getTableCellRendererComponent</code> method and set the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    * of the returned component directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    96
    private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    97
    protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // We need a place to store the color the JLabel should be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // to after its foreground and background colors have been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // to the selection background color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // These ivars will be made protected when their names are finalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private Color unselectedForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private Color unselectedBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Creates a default table cell renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public DefaultTableCellRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        setBorder(getNoFocusBorder());
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   113
        setName("Table.cellRenderer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   116
    private Border getNoFocusBorder() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   117
        Border border = DefaultLookup.getBorder(this, ui, "Table.cellNoFocusBorder");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (System.getSecurityManager() != null) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   119
            if (border != null) return border;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return SAFE_NO_FOCUS_BORDER;
3096
9ed0268ea411 6737700: api/javax_swing/table/DefaultTableCellRenderer/index.html#getset:DefaultTableCellRenderer
malenkov
parents: 2658
diff changeset
   121
        } else if (border != null) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   122
            if (noFocusBorder == null || noFocusBorder == DEFAULT_NO_FOCUS_BORDER) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   123
                return border;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   124
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
3096
9ed0268ea411 6737700: api/javax_swing/table/DefaultTableCellRenderer/index.html#getset:DefaultTableCellRenderer
malenkov
parents: 2658
diff changeset
   126
        return noFocusBorder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Overrides <code>JComponent.setForeground</code> to assign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * the unselected-foreground color to the specified color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param c set the foreground color to this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void setForeground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        super.setForeground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        unselectedForeground = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Overrides <code>JComponent.setBackground</code> to assign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * the unselected-background color to the specified color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param c set the background color to this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void setBackground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        super.setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        unselectedBackground = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Notification from the <code>UIManager</code> that the look and feel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * [L&F] has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Replaces the current UI object with the latest version from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <code>UIManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        super.updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        setForeground(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        setBackground(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // implements javax.swing.table.TableCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns the default table cell renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * During a printing operation, this method will be called with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>isSelected</code> and <code>hasFocus</code> values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <code>false</code> to prevent selection and focus from appearing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * in the printed output. To do other customization based on whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * or not the table is being printed, check the return value from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * {@link javax.swing.JComponent#isPaintingForPrint()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param table  the <code>JTable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param value  the value to assign to the cell at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *                  <code>[row, column]</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param isSelected true if cell is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param hasFocus true if cell has focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param row  the row of the cell to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param column the column of the cell to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @return the default table cell renderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see javax.swing.JComponent#isPaintingForPrint()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public Component getTableCellRendererComponent(JTable table, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                          boolean isSelected, boolean hasFocus, int row, int column) {
7011
f290783add29 6735286: javax.swing.DefaultTableCellRender.getTableCellRendererComponent() doesn't allow passing null Tables
rupashka
parents: 5506
diff changeset
   189
        if (table == null) {
f290783add29 6735286: javax.swing.DefaultTableCellRender.getTableCellRendererComponent() doesn't allow passing null Tables
rupashka
parents: 5506
diff changeset
   190
            return this;
f290783add29 6735286: javax.swing.DefaultTableCellRender.getTableCellRendererComponent() doesn't allow passing null Tables
rupashka
parents: 5506
diff changeset
   191
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        Color fg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Color bg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        JTable.DropLocation dropLocation = table.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (dropLocation != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                && !dropLocation.isInsertRow()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                && !dropLocation.isInsertColumn()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                && dropLocation.getRow() == row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                && dropLocation.getColumn() == column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   203
            fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   204
            bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            isSelected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (isSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            super.setForeground(fg == null ? table.getSelectionForeground()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                           : fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            super.setBackground(bg == null ? table.getSelectionBackground()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                           : bg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   215
            Color background = unselectedBackground != null
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   216
                                    ? unselectedBackground
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   217
                                    : table.getBackground();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   218
            if (background == null || background instanceof javax.swing.plaf.UIResource) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   219
                Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
4283
06d5378aaff3 6882917: Nimbus and DefaultTableCellRenderer: must start with normal background
peterz
parents: 3096
diff changeset
   220
                if (alternateColor != null && row % 2 != 0) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   221
                    background = alternateColor;
4283
06d5378aaff3 6882917: Nimbus and DefaultTableCellRenderer: must start with normal background
peterz
parents: 3096
diff changeset
   222
                }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   223
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            super.setForeground(unselectedForeground != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                    ? unselectedForeground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                    : table.getForeground());
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   227
            super.setBackground(background);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        setFont(table.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (hasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            Border border = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (isSelected) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   235
                border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (border == null) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   238
                border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            setBorder(border);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (!isSelected && table.isCellEditable(row, column)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                Color col;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   244
                col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (col != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    super.setForeground(col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   248
                col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (col != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    super.setBackground(col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            setBorder(getNoFocusBorder());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * The following methods are overridden as a performance measure to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * to prune code-paths are often called in the case of renders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * but which we know are unnecessary.  Great care should be taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * when writing your own renderer to weigh the benefits and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * drawbacks of overriding methods like these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public boolean isOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        Color back = getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        Component p = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            p = p.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   281
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // p should now be the JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        boolean colorMatch = (back != null) && (p != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            back.equals(p.getBackground()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        p.isOpaque();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return !colorMatch && super.isOpaque();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public void invalidate() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public void validate() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public void revalidate() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void repaint(long tm, int x, int y, int width, int height) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public void repaint(Rectangle r) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void repaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // Strings get interned...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (propertyName=="text"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                || propertyName == "labelFor"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                || propertyName == "displayedMnemonic"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                || ((propertyName == "font" || propertyName == "foreground")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    && oldValue != newValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            super.firePropertyChange(propertyName, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Overridden for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * See the <a href="#override">Implementation Note</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Sets the <code>String</code> object for the cell being rendered to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param value  the string value for this cell; if value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *          <code>null</code> it sets the text value to an empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @see JLabel#setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    protected void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        setText((value == null) ? "" : value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * A subclass of <code>DefaultTableCellRenderer</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * implements <code>UIResource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <code>DefaultTableCellRenderer</code> doesn't implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <code>UIResource</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * directly so that applications can safely override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <code>cellRenderer</code> property with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <code>DefaultTableCellRenderer</code> subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public static class UIResource extends DefaultTableCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        implements javax.swing.plaf.UIResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
}