jdk/src/share/classes/javax/swing/plaf/synth/SynthComboBoxUI.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1290 da8902cd496c
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.FocusManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.swing.plaf.synth.SynthUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Synth's ComboBoxUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class SynthComboBoxUI extends BasicComboBoxUI implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                              PropertyChangeListener, SynthUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private SynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean useListColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return new SynthComboBoxUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        updateStyle(comboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private void updateStyle(JComboBox comboBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        SynthStyle oldStyle = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        SynthContext context = getContext(comboBox, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        style = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (style != oldStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            useListColors = style.getBoolean(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                  "ComboBox.rendererUseListColors", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if(listBox != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            SynthLookAndFeel.updateStyles(listBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        comboBox.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        SynthContext context = getContext(comboBox, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        style.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        comboBox.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public SynthContext getContext(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return getContext(c, getComponentState(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private SynthContext getContext(JComponent c, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return SynthContext.getContext(SynthContext.class, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    SynthLookAndFeel.getRegion(c), style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private Region getRegion(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return SynthLookAndFeel.getRegion(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private int getComponentState(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return SynthLookAndFeel.getComponentState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected ComboPopup createPopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        SynthComboPopup popup = new SynthComboPopup( comboBox );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected ListCellRenderer createRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return new SynthComboBoxRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected ComboBoxEditor createEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return new SynthComboBoxEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // end UI Initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    //======================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (SynthLookAndFeel.shouldUpdateStyle(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            updateStyle(comboBox);
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
    protected JButton createArrowButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        SynthArrowButton button = new SynthArrowButton(SwingConstants.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        button.setName("ComboBox.arrowButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    //=================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // begin ComponentUI Implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        SynthLookAndFeel.update(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        context.getPainter().paintComboBoxBackground(context, g, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                                  c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        hasFocus = comboBox.hasFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if ( !comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Rectangle r = rectangleForCurrentValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            paintCurrentValue(g,r,hasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        context.getPainter().paintComboBoxBorder(context, g, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Paints the currently selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        ListCellRenderer renderer = comboBox.getRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if ( hasFocus && !isPopupVisible(comboBox) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            c = renderer.getListCellRendererComponent( listBox,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                                       comboBox.getSelectedItem(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                                       -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                                       false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                                       false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            c = renderer.getListCellRendererComponent( listBox,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                                       comboBox.getSelectedItem(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                                       -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                                       false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                                       false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // Fix for 4238829: should lay out the JPanel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        boolean shouldValidate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (c instanceof JPanel)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            shouldValidate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (c instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            c.setName("ComboBox.renderer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                        bounds.width,bounds.height, shouldValidate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                        bounds.width,bounds.height, shouldValidate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * From BasicComboBoxRenderer v 1.18.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private class SynthComboBoxRenderer extends JLabel implements ListCellRenderer, UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        public SynthComboBoxRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            setText(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            // As SynthComboBoxRenderer's are asked for a size BEFORE they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // are parented getName is overriden to force the name to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // ComboBox.renderer if it isn't set. If we didn't do this the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            // wrong style could be used for size calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            String name = super.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                return "ComboBox.renderer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        public Component getListCellRendererComponent(JList list, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                         int index, boolean isSelected, boolean cellHasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            setName("ComboBox.listRenderer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            SynthLookAndFeel.resetSelectedUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (isSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                setBackground(list.getSelectionBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                setForeground(list.getSelectionForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (!useListColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    SynthLookAndFeel.setSelectedUI(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                         (SynthLabelUI)SynthLookAndFeel.getUIOfType(getUI(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                         SynthLabelUI.class), isSelected, cellHasFocus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                         list.isEnabled(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                setBackground(list.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                setForeground(list.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            setFont(list.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (value instanceof Icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                setIcon((Icon)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                setText("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                String text = (value == null) ? " " : value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if ("".equals(text)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    text = " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // The renderer component should inherit the enabled and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // orientation state of its parent combobox.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // especially needed for GTK comboboxes, where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // ListCellRenderer's state determines the visual state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // of the combobox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            setEnabled(comboBox.isEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            setComponentOrientation(comboBox.getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            SynthLookAndFeel.resetSelectedUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * From BasicCombBoxEditor v 1.24.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private static class SynthComboBoxEditor implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                              ComboBoxEditor, UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        protected JTextField editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        private Object oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        public SynthComboBoxEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            editor = new JTextField("",9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            editor.setName("ComboBox.textField");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        public Component getEditorComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * Sets the item that should be edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * @param anObject the displayed value of the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        public void setItem(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if ( anObject != null )  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                text = anObject.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                oldValue = anObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // workaround for 4530952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (!text.equals(editor.getText())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                editor.setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        public Object getItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            Object newValue = editor.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (oldValue != null && !(oldValue instanceof String))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                // The original value is not a string. Should return the value in it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // original type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (newValue.equals(oldValue.toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    // Must take the value from the editor and get the value and cast it to the new type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    Class cls = oldValue.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        Method method = cls.getMethod("valueOf", new Class[]{String.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        newValue = method.invoke(oldValue, new Object[] { editor.getText()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        // Fail silently and return the newValue (a String object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        public void selectAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            editor.selectAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            editor.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        public void addActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            editor.addActionListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        public void removeActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            editor.removeActionListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}