jdk/src/share/classes/javax/swing/plaf/synth/SynthTableUI.java
author peterz
Wed, 18 Nov 2009 17:36:46 +0300
changeset 4283 06d5378aaff3
parent 2658 43e06bc950ec
child 4394 92a8ec883f5d
permissions -rw-r--r--
6882917: Nimbus and DefaultTableCellRenderer: must start with normal background Reviewed-by: rupashka
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 2002-2007 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    30
import java.awt.Dimension;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.text.DateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.text.Format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.text.NumberFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.Icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.ImageIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.JCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.JComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.JLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.JTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.swing.LookAndFeel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.swing.border.Border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.swing.plaf.basic.BasicTableUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.swing.table.DefaultTableCellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.swing.table.JTableHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.swing.table.TableCellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.swing.table.TableColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.swing.table.TableColumnModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.swing.plaf.synth.SynthUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * SynthTableUI implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
class SynthTableUI extends BasicTableUI implements SynthUI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
// Instance Variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private SynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean useTableColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private boolean useUIBorder;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    74
    private Color alternateColor; //the background color to use for cells for alternate cells
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // TableCellRenderer installed on the JTable at the time we're installed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // cached so that we can reinstall them at uninstallUI time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private TableCellRenderer dateRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private TableCellRenderer numberRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private TableCellRenderer doubleRender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private TableCellRenderer floatRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private TableCellRenderer iconRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private TableCellRenderer imageIconRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private TableCellRenderer booleanRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private TableCellRenderer objectRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
//  The installation/uninstall procedures and support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return new SynthTableUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Initialize JTable properties, e.g. font, foreground, and background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The font, foreground, and background properties are only set if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * current value is either null or a UIResource, other properties are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * if the current value is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        dateRenderer = installRendererIfPossible(Date.class, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        numberRenderer = installRendererIfPossible(Number.class, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        doubleRender = installRendererIfPossible(Double.class, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        floatRenderer = installRendererIfPossible(Float.class, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        iconRenderer = installRendererIfPossible(Icon.class, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        imageIconRenderer = installRendererIfPossible(ImageIcon.class, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        booleanRenderer = installRendererIfPossible(Boolean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                 new SynthBooleanTableCellRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        objectRenderer = installRendererIfPossible(Object.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                        new SynthTableCellRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        updateStyle(table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private TableCellRenderer installRendererIfPossible(Class objectClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                     TableCellRenderer renderer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        TableCellRenderer currentRenderer = table.getDefaultRenderer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                 objectClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (currentRenderer instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            table.setDefaultRenderer(objectClass, renderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return currentRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private void updateStyle(JTable c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        SynthContext context = getContext(c, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        SynthStyle oldStyle = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        style = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (style != oldStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            context.setComponentState(ENABLED | SELECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Color sbg = table.getSelectionBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (sbg == null || sbg instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                table.setSelectionBackground(style.getColor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                        context, ColorType.TEXT_BACKGROUND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            Color sfg = table.getSelectionForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (sfg == null || sfg instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                table.setSelectionForeground(style.getColor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                  context, ColorType.TEXT_FOREGROUND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            context.setComponentState(ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Color gridColor = table.getGridColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (gridColor == null || gridColor instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                gridColor = (Color)style.get(context, "Table.gridColor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (gridColor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    gridColor = style.getColor(context, ColorType.FOREGROUND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                table.setGridColor(gridColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            useTableColors = style.getBoolean(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                  "Table.rendererUseTableColors", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            useUIBorder = style.getBoolean(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                  "Table.rendererUseUIBorder", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Object rowHeight = style.get(context, "Table.rowHeight");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (rowHeight != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                LookAndFeel.installProperty(table, "rowHeight", rowHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   166
            boolean showGrid = style.getBoolean(context, "Table.showGrid", true);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   167
            if (!showGrid) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   168
                table.setShowGrid(false);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   169
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   170
            Dimension d = table.getIntercellSpacing();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   171
//            if (d == null || d instanceof UIResource) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   172
            if (d != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   173
                d = (Dimension)style.get(context, "Table.intercellSpacing");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   174
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   175
            alternateColor = (Color)style.get(context, "Table.alternateRowColor");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   176
            if (d != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   177
                table.setIntercellSpacing(d);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   178
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   179
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   180
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Attaches listeners to the JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        table.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        table.setDefaultRenderer(Date.class, dateRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        table.setDefaultRenderer(Number.class, numberRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        table.setDefaultRenderer(Double.class, doubleRender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        table.setDefaultRenderer(Float.class, floatRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        table.setDefaultRenderer(Icon.class, iconRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        table.setDefaultRenderer(ImageIcon.class, imageIconRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        table.setDefaultRenderer(Boolean.class, booleanRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        table.setDefaultRenderer(Object.class, objectRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (table.getTransferHandler() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            table.setTransferHandler(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        SynthContext context = getContext(table, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        style.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        table.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    // SynthUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public SynthContext getContext(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return getContext(c, getComponentState(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private SynthContext getContext(JComponent c, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return SynthContext.getContext(SynthContext.class, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    SynthLookAndFeel.getRegion(c), style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private Region getRegion(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return SynthLookAndFeel.getRegion(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private int getComponentState(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return SynthLookAndFeel.getComponentState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
//  Paint methods and support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        SynthLookAndFeel.update(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        context.getPainter().paintTableBackground(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                          g, 0, 0, c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        context.getPainter().paintTableBorder(context, g, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        Rectangle bounds = table.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // account for the fact that the graphics has already been translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // into the table's bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        bounds.x = bounds.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                // this check prevents us from painting the entire table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // when the clip doesn't intersect our bounds at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                !bounds.intersects(clip)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            paintDropLines(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        boolean ltr = table.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        Point upperLeft = clip.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        Point lowerRight = new Point(clip.x + clip.width - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                     clip.y + clip.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int rMin = table.rowAtPoint(upperLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int rMax = table.rowAtPoint(lowerRight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // This should never happen (as long as our bounds intersect the clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // which is why we bail above if that is the case).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (rMin == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            rMin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // If the table does not have enough rows to fill the view we'll get -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // (We could also get -1 if our bounds don't intersect the clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // which is why we bail above if that is the case).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // Replace this with the index of the last row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (rMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            rMax = table.getRowCount()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // This should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (cMin == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            cMin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // If the table does not have enough columns to fill the view we'll get -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // Replace this with the index of the last column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (cMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            cMax = table.getColumnCount()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // Paint the grid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        paintGrid(context, g, rMin, rMax, cMin, cMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // Paint the cells.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        paintCells(context, g, rMin, rMax, cMin, cMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        paintDropLines(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private void paintDropLines(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        JTable.DropLocation loc = table.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (loc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Color color = (Color)style.get(context, "Table.dropLineColor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        Color shortColor = (Color)style.get(context, "Table.dropLineShortColor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (color == null && shortColor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        Rectangle rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        rect = getHDropLineRect(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            int x = rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            int w = rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                extendRect(rect, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                g.fillRect(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (!loc.isInsertColumn() && shortColor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                g.setColor(shortColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                g.fillRect(x, rect.y, w, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        rect = getVDropLineRect(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            int y = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            int h = rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                extendRect(rect, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                g.fillRect(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (!loc.isInsertRow() && shortColor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                g.setColor(shortColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                g.fillRect(rect.x, y, rect.width, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private Rectangle getHDropLineRect(JTable.DropLocation loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (!loc.isInsertRow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int row = loc.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int col = loc.getColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (col >= table.getColumnCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            col--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        Rectangle rect = table.getCellRect(row, col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (row >= table.getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            row--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            Rectangle prevRect = table.getCellRect(row, col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            rect.y = prevRect.y + prevRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (rect.y == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            rect.y = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            rect.y -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        rect.height = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private Rectangle getVDropLineRect(JTable.DropLocation loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (!loc.isInsertColumn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        boolean ltr = table.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int col = loc.getColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        Rectangle rect = table.getCellRect(loc.getRow(), col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (col >= table.getColumnCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            col--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            rect = table.getCellRect(loc.getRow(), col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                rect.x = rect.x + rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } else if (!ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            rect.x = rect.x + rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (rect.x == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            rect.x = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            rect.x -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        rect.width = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private Rectangle extendRect(Rectangle rect, boolean horizontal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (rect == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (horizontal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            rect.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            rect.width = table.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            rect.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (table.getRowCount() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                Rectangle lastRect = table.getCellRect(table.getRowCount() - 1, 0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                rect.height = lastRect.y + lastRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                rect.height = table.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Paints the grid lines within <I>aRect</I>, using the grid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * color set with <I>setGridColor</I>. Paints vertical lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * if <code>getShowVerticalLines()</code> returns true and paints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * horizontal lines if <code>getShowHorizontalLines()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private void paintGrid(SynthContext context, Graphics g, int rMin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                           int rMax, int cMin, int cMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        g.setColor(table.getGridColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        Rectangle minCell = table.getCellRect(rMin, cMin, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        Rectangle maxCell = table.getCellRect(rMax, cMax, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        Rectangle damagedArea = minCell.union( maxCell );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        SynthGraphicsUtils synthG = context.getStyle().getGraphicsUtils(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                     context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (table.getShowHorizontalLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            int tableWidth = damagedArea.x + damagedArea.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            int y = damagedArea.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            for (int row = rMin; row <= rMax; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                y += table.getRowHeight(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                synthG.drawLine(context, "Table.grid",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                g, damagedArea.x, y - 1, tableWidth - 1,y - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (table.getShowVerticalLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            TableColumnModel cm = table.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            int tableHeight = damagedArea.y + damagedArea.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            int x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (table.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                x = damagedArea.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                for (int column = cMin; column <= cMax; column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    int w = cm.getColumn(column).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    x += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    synthG.drawLine(context, "Table.grid", g, x - 1, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                    x - 1, tableHeight - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                x = damagedArea.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                for (int column = cMax; column >= cMin; column--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    int w = cm.getColumn(column).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    x += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    synthG.drawLine(context, "Table.grid", g, x - 1, 0, x - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                                    tableHeight - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    private int viewIndexForColumn(TableColumn aColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        TableColumnModel cm = table.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        for (int column = 0; column < cm.getColumnCount(); column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            if (cm.getColumn(column) == aColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                return column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    private void paintCells(SynthContext context, Graphics g, int rMin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                            int rMax, int cMin, int cMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        JTableHeader header = table.getTableHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        TableColumnModel cm = table.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int columnMargin = cm.getColumnMargin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        Rectangle cellRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        TableColumn aColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        int columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (table.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            for(int row = rMin; row <= rMax; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                cellRect = table.getCellRect(row, cMin, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                for(int column = cMin; column <= cMax; column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    aColumn = cm.getColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    columnWidth = aColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    cellRect.width = columnWidth - columnMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    if (aColumn != draggedColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        paintCell(context, g, cellRect, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    cellRect.x += columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            for(int row = rMin; row <= rMax; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                cellRect = table.getCellRect(row, cMin, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                aColumn = cm.getColumn(cMin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (aColumn != draggedColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    columnWidth = aColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    cellRect.width = columnWidth - columnMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    paintCell(context, g, cellRect, row, cMin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                for(int column = cMin+1; column <= cMax; column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    aColumn = cm.getColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    columnWidth = aColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    cellRect.width = columnWidth - columnMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    cellRect.x -= columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    if (aColumn != draggedColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        paintCell(context, g, cellRect, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        // Paint the dragged column if we are dragging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (draggedColumn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            paintDraggedArea(context, g, rMin, rMax, draggedColumn, header.getDraggedDistance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // Remove any renderers that may be left in the rendererPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        rendererPane.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    private void paintDraggedArea(SynthContext context, Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int draggedColumnIndex = viewIndexForColumn(draggedColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        Rectangle vacatedColumnRect = minCell.union(maxCell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        // Paint a gray well in place of the moving column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        g.setColor(table.getParent().getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                   vacatedColumnRect.width, vacatedColumnRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // Move to the where the cell has been dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        vacatedColumnRect.x += distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // Fill the background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        g.setColor(context.getStyle().getColor(context, ColorType.BACKGROUND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                   vacatedColumnRect.width, vacatedColumnRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        SynthGraphicsUtils synthG = context.getStyle().getGraphicsUtils(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                                            context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // Paint the vertical grid lines if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (table.getShowVerticalLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            g.setColor(table.getGridColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            int x1 = vacatedColumnRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            int y1 = vacatedColumnRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            int x2 = x1 + vacatedColumnRect.width - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            int y2 = y1 + vacatedColumnRect.height - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            // Left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            synthG.drawLine(context, "Table.grid", g, x1-1, y1, x1-1, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            // Right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            synthG.drawLine(context, "Table.grid", g, x2, y1, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        for(int row = rMin; row <= rMax; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            // Render the cell value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            Rectangle r = table.getCellRect(row, draggedColumnIndex, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            r.x += distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            paintCell(context, g, r, row, draggedColumnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            // Paint the (lower) horizontal grid line if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            if (table.getShowHorizontalLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                g.setColor(table.getGridColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                Rectangle rcr = table.getCellRect(row, draggedColumnIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                rcr.x += distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                int x1 = rcr.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                int y1 = rcr.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                int x2 = x1 + rcr.width - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                int y2 = y1 + rcr.height - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                synthG.drawLine(context, "Table.grid", g, x1, y2, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    private void paintCell(SynthContext context, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            Rectangle cellRect, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (table.isEditing() && table.getEditingRow()==row &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                 table.getEditingColumn()==column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            Component component = table.getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            component.setBounds(cellRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            component.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            TableCellRenderer renderer = table.getCellRenderer(row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            Component component = table.prepareRenderer(renderer, row, column);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   637
            Color b = component.getBackground();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   638
            if ((b == null || b instanceof UIResource
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   639
                    || component instanceof SynthBooleanTableCellRenderer)
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   640
                    && !table.isCellSelected(row, column)) {
4283
06d5378aaff3 6882917: Nimbus and DefaultTableCellRenderer: must start with normal background
peterz
parents: 2658
diff changeset
   641
                if (alternateColor != null && row % 2 != 0) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   642
                    component.setBackground(alternateColor);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   643
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   644
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            rendererPane.paintComponent(g, component, table, cellRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    cellRect.y, cellRect.width, cellRect.height, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (SynthLookAndFeel.shouldUpdateStyle(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            updateStyle((JTable)event.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    private class SynthBooleanTableCellRenderer extends JCheckBox implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                      TableCellRenderer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        private boolean isRowSelected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        public SynthBooleanTableCellRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            setHorizontalAlignment(JLabel.CENTER);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   663
            setName("Table.cellRenderer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        public Component getTableCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                            JTable table, Object value, boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                            boolean hasFocus, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            isRowSelected = isSelected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (isSelected) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   672
                setForeground(unwrap(table.getSelectionForeground()));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   673
                setBackground(unwrap(table.getSelectionBackground()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            } else {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   675
                setForeground(unwrap(table.getForeground()));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   676
                setBackground(unwrap(table.getBackground()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            setSelected((value != null && ((Boolean)value).booleanValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   683
        private Color unwrap(Color c) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   684
            if (c instanceof UIResource) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   685
                return new Color(c.getRGB());
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   686
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   687
            return c;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   688
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   689
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        public boolean isOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            return isRowSelected ? true : super.isOpaque();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    private class SynthTableCellRenderer extends DefaultTableCellRenderer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        private Object numberFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        private Object dateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        private boolean opaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        public void setOpaque(boolean isOpaque) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            opaque = isOpaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        public boolean isOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            return opaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            String name = super.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                return "Table.cellRenderer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        public void setBorder(Border b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            if (useUIBorder || b instanceof SynthBorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                super.setBorder(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        public Component getTableCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                  JTable table, Object value, boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                  boolean hasFocus, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            if (!useTableColors && (isSelected || hasFocus)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                SynthLookAndFeel.setSelectedUI((SynthLabelUI)SynthLookAndFeel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                             getUIOfType(getUI(), SynthLabelUI.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                                   isSelected, hasFocus, table.isEnabled(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                SynthLookAndFeel.resetSelectedUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            super.getTableCellRendererComponent(table, value, isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                                                hasFocus, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            setIcon(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            Class columnClass = table.getColumnClass(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            configureValue(value, columnClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        private void configureValue(Object value, Class columnClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            if (columnClass == Object.class || columnClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                setHorizontalAlignment(JLabel.LEADING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            } else if (columnClass == Float.class || columnClass == Double.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                if (numberFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    numberFormat = NumberFormat.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                setHorizontalAlignment(JLabel.TRAILING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                setText((value == null) ? "" : ((NumberFormat)numberFormat).format(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            else if (columnClass == Number.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                setHorizontalAlignment(JLabel.TRAILING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                // Super will have set value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            else if (columnClass == Icon.class || columnClass == ImageIcon.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                setHorizontalAlignment(JLabel.CENTER);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   759
                setIcon((value instanceof Icon) ? (Icon)value : null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                setText("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            else if (columnClass == Date.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                if (dateFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    dateFormat = DateFormat.getDateInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                setHorizontalAlignment(JLabel.LEADING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                setText((value == null) ? "" : ((Format)dateFormat).format(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                configureValue(value, columnClass.getSuperclass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            SynthLookAndFeel.resetSelectedUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
}