jdk/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java
author alitvinov
Mon, 03 Jun 2013 16:37:13 +0400
changeset 17902 14a0ae3ca973
parent 15637 2c226ebab6a6
child 20098 7b11536bf6b3
permissions -rw-r--r--
6337518: Null Arrow Button Throws Exception in BasicComboBoxUI Reviewed-by: alexp, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
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 javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Basic UI implementation for JComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The combo box is a compound component which means that it is an agregate of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * many simpler components. This class creates and manages the listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * on the combo box and the combo box model. These listeners update the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * interface in response to changes in the properties and state of the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * All event handling is handled by listener classes created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>createxxxListener()</code> methods and internal classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * You can change the behavior of this class by overriding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>createxxxListener()</code> methods and supplying your own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * event listeners or subclassing from the ones supplied in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * For adding specific actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * overide <code>installKeyboardActions</code> to add actions in response to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * KeyStroke bindings. See the article <a href="http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html">Keyboard Bindings in Swing</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * at <a href="http://java.sun.com/products/jfc/tsc"><em>The Swing Connection</em></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Mark Davidson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class BasicComboBoxUI extends ComboBoxUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected JComboBox comboBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected boolean   hasFocus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Control the selection behavior of the JComboBox when it is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // in the JTable DefaultCellEditor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private boolean isTableCellEditor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final String IS_TABLE_CELL_EDITOR = "JComboBox.isTableCellEditor";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // This list is for drawing the current item in the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected JList   listBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Used to render the currently selected item in the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // It doesn't have anything to do with the popup's rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected CellRendererPane currentValuePane = new CellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // The implementation of ComboPopup that is used to show the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected ComboPopup popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // The Component that the ComboBoxEditor uses for editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected Component editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // The arrow button that invokes the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected JButton   arrowButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // Listeners that are attached to the JComboBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * or override. Override the listener construction method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see #createKeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected KeyListener keyListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * or override. Override the listener construction method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @see #createFocusListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected FocusListener focusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * or override. Override the listener construction method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * or override. Override the listener construction method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see #createItemListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected ItemListener itemListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // Listeners that the ComboPopup produces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected MouseListener popupMouseListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected MouseMotionListener popupMouseMotionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected KeyListener popupKeyListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // This is used for knowing when to cache the minimum preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // If the data in the list changes, the cached value get marked for recalc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // Added to the current JComboBox model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * or override. Override the listener construction method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see #createListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected ListDataListener listDataListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Implements all the Listeners needed by this class, all existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * listeners redirect to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * The time factor to treate the series of typed alphanumeric key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * as prefix for first letter navigation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private long timeFactor = 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * This is tricky, this variables is needed for DefaultKeySelectionManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * to take into account time factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private long lastTime = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private long time = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * The default key selection manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    JComboBox.KeySelectionManager keySelectionManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // Flag for recalculating the minimum preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected boolean isMinimumSizeDirty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // Cached minimum preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected Dimension cachedMinimumSize = new Dimension( 0, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // Flag for calculating the display size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private boolean isDisplaySizeDirty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // Cached the size that the display needs to render the largest item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private Dimension cachedDisplaySize = new Dimension( 0, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // Key used for lookup of the DefaultListCellRenderer in the AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private static final Object COMBO_UI_LIST_CELL_RENDERER_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        new StringBuffer("DefaultListCellRendererKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    static final StringBuffer HIDE_POPUP_KEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                  = new StringBuffer("HidePopupKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Whether or not all cells have the same baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private boolean sameBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   188
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   189
     * Indicates whether or not the combo box button should be square.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   190
     * If square, then the width and height are equal, and are both set to
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   191
     * the height of the combo minus appropriate insets.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   192
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   193
     * @since 1.7
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   194
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   195
    protected boolean squareButton = true;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   196
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   197
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   198
     * If specified, these insets act as padding around the cell renderer when
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   199
     * laying out and painting the "selected" item in the combo box. These
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   200
     * insets add to those specified by the cell renderer.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   201
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   202
     * @since 1.7
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   203
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   204
    protected Insets padding;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   205
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    // Used for calculating the default size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private static ListCellRenderer getDefaultListCellRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        ListCellRenderer renderer = (ListCellRenderer)AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                         getAppContext().get(COMBO_UI_LIST_CELL_RENDERER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (renderer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            renderer = new DefaultListCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            AppContext.getAppContext().put(COMBO_UI_LIST_CELL_RENDERER_KEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                           new DefaultListCellRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return renderer;
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
     * Populates ComboBox's actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        map.put(new Actions(Actions.HIDE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        map.put(new Actions(Actions.PAGE_DOWN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        map.put(new Actions(Actions.PAGE_UP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        map.put(new Actions(Actions.HOME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        map.put(new Actions(Actions.END));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        map.put(new Actions(Actions.DOWN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        map.put(new Actions(Actions.DOWN_2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        map.put(new Actions(Actions.TOGGLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        map.put(new Actions(Actions.TOGGLE_2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        map.put(new Actions(Actions.UP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        map.put(new Actions(Actions.UP_2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        map.put(new Actions(Actions.ENTER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    //========================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // begin UI Initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return new BasicComboBoxUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   245
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public void installUI( JComponent c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        isMinimumSizeDirty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        comboBox = (JComboBox)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        popup = createPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        listBox = popup.getList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // Is this combo box a cell editor?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        Boolean inTable = (Boolean)c.getClientProperty(IS_TABLE_CELL_EDITOR );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (inTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if ( comboBox.getRenderer() == null || comboBox.getRenderer() instanceof UIResource ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            comboBox.setRenderer( createRenderer() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if ( comboBox.getEditor() == null || comboBox.getEditor() instanceof UIResource ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            comboBox.setEditor( createEditor() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        installComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        comboBox.setLayout( createLayoutManager() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        comboBox.setRequestFocusEnabled( true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        comboBox.putClientProperty("doNotCancelPopup", HIDE_POPUP_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (keySelectionManager == null || keySelectionManager instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            keySelectionManager = new DefaultKeySelectionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        comboBox.setKeySelectionManager(keySelectionManager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   285
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public void uninstallUI( JComponent c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        setPopupVisible( comboBox, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        popup.uninstallingUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        comboBox.setLayout( null );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        uninstallComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if ( comboBox.getRenderer() == null || comboBox.getRenderer() instanceof UIResource ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            comboBox.setRenderer( null );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        ComboBoxEditor comboBoxEditor = comboBox.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (comboBoxEditor instanceof UIResource ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (comboBoxEditor.getEditorComponent().hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                // Leave focus in JComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                comboBox.requestFocusInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            comboBox.setEditor( null );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (keySelectionManager instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            comboBox.setKeySelectionManager(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        keyListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        focusListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        listDataListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        popup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        listBox = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        comboBox = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Installs the default colors, default font, default renderer, and default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * editor into the JComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        LookAndFeel.installColorsAndFont( comboBox,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                          "ComboBox.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                          "ComboBox.foreground",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                          "ComboBox.font" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        LookAndFeel.installBorder( comboBox, "ComboBox.border" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        LookAndFeel.installProperty( comboBox, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Long l = (Long)UIManager.get("ComboBox.timeFactor");
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   338
        timeFactor = l == null ? 1000L : l.longValue();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   339
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   340
        //NOTE: this needs to default to true if not specified
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   341
        Boolean b = (Boolean)UIManager.get("ComboBox.squareButton");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   342
        squareButton = b == null ? true : b;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   343
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   344
        padding = UIManager.getInsets("ComboBox.padding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   348
     * Creates and installs listeners for the combo box and its model.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * This method is called when the UI is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if ( (itemListener = createItemListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            comboBox.addItemListener( itemListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if ( (propertyChangeListener = createPropertyChangeListener()) != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            comboBox.addPropertyChangeListener( propertyChangeListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if ( (keyListener = createKeyListener()) != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            comboBox.addKeyListener( keyListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if ( (focusListener = createFocusListener()) != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            comboBox.addFocusListener( focusListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if ((popupMouseListener = popup.getMouseListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            comboBox.addMouseListener( popupMouseListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if ((popupMouseMotionListener = popup.getMouseMotionListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            comboBox.addMouseMotionListener( popupMouseMotionListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if ((popupKeyListener = popup.getKeyListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            comboBox.addKeyListener(popupKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if ( comboBox.getModel() != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if ( (listDataListener = createListDataListener()) != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                comboBox.getModel().addListDataListener( listDataListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   382
     * Uninstalls the default colors, default font, default renderer,
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   383
     * and default editor from the combo box.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        LookAndFeel.installColorsAndFont( comboBox,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                          "ComboBox.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                          "ComboBox.foreground",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                          "ComboBox.font" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        LookAndFeel.uninstallBorder( comboBox );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   394
     * Removes the installed listeners from the combo box and its model.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * The number and types of listeners removed and in this method should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * the same that was added in <code>installListeners</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if ( keyListener != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            comboBox.removeKeyListener( keyListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if ( itemListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            comboBox.removeItemListener( itemListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if ( propertyChangeListener != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            comboBox.removePropertyChangeListener( propertyChangeListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if ( focusListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            comboBox.removeFocusListener( focusListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if ( popupMouseListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            comboBox.removeMouseListener( popupMouseListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if ( popupMouseMotionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            comboBox.removeMouseMotionListener( popupMouseMotionListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (popupKeyListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            comboBox.removeKeyListener(popupKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if ( comboBox.getModel() != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if ( listDataListener != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                comboBox.getModel().removeListDataListener( listDataListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Creates the popup portion of the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return an instance of <code>ComboPopup</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @see ComboPopup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    protected ComboPopup createPopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return new BasicComboPopup( comboBox );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Creates a <code>KeyListener</code> which will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * combo box. If this method returns null then it will not be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @return an instance <code>KeyListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    protected KeyListener createKeyListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Creates a <code>FocusListener</code> which will be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * If this method returns null then it will not be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @return an instance of a <code>FocusListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    protected FocusListener createFocusListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Creates a list data listener which will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * <code>ComboBoxModel</code>. If this method returns null then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * it will not be added to the combo box model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @return an instance of a <code>ListDataListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    protected ListDataListener createListDataListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Creates an <code>ItemListener</code> which will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * combo box. If this method returns null then it will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Subclasses may override this method to return instances of their own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * ItemEvent handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @return an instance of an <code>ItemListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    protected ItemListener createItemListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Creates a <code>PropertyChangeListener</code> which will be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * the combo box. If this method returns null then it will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return an instance of a <code>PropertyChangeListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Creates a layout manager for managing the components which make up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @return an instance of a layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    protected LayoutManager createLayoutManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Creates the default renderer that will be used in a non-editiable combo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * box. A default renderer will used only if a renderer has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * explicitly set with <code>setRenderer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @return a <code>ListCellRender</code> used for the combo box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @see javax.swing.JComboBox#setRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    protected ListCellRenderer createRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        return new BasicComboBoxRenderer.UIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Creates the default editor that will be used in editable combo boxes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * A default editor will be used only if an editor has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * explicitly set with <code>setEditor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return a <code>ComboBoxEditor</code> used for the combo box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @see javax.swing.JComboBox#setEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    protected ComboBoxEditor createEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        return new BasicComboBoxEditor.UIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Returns the shared listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    // end UI Initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    //======================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    //======================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    // begin Inner classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * This listener checks to see if the key event isn't a navigation key.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * it finds a key event that wasn't a navigation key it dispatches it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * JComboBox.selectWithKeyChar() so that it can do type-ahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * This public inner class should be treated as protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Instantiate it only within subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <code>BasicComboBoxUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public class KeyHandler extends KeyAdapter {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   557
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        public void keyPressed( KeyEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            getHandler().keyPressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * This listener hides the popup when the focus is lost.  It also repaints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * when focus is gained or lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * This public inner class should be treated as protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Instantiate it only within subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <code>BasicComboBoxUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    public class FocusHandler implements FocusListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        public void focusGained( FocusEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            getHandler().focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        public void focusLost( FocusEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            getHandler().focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * This listener watches for changes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <code>ComboBoxModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * This public inner class should be treated as protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Instantiate it only within subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <code>BasicComboBoxUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @see #createListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public class ListDataHandler implements ListDataListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        public void contentsChanged( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            getHandler().contentsChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        public void intervalAdded( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            getHandler().intervalAdded(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        public void intervalRemoved( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            getHandler().intervalRemoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * This listener watches for changes to the selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * This public inner class should be treated as protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Instantiate it only within subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <code>BasicComboBoxUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @see #createItemListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public class ItemHandler implements ItemListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        // This class used to implement behavior which is now redundant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        public void itemStateChanged(ItemEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * This listener watches for bound properties that have changed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Subclasses which wish to listen to combo box property changes should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * call the superclass methods to ensure that the combo box ui correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * handles property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * This public inner class should be treated as protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Instantiate it only within subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <code>BasicComboBoxUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public class PropertyChangeHandler implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    // Syncronizes the ToolTip text for the components within the combo box to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    // same value as the combo box ToolTip text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    private void updateToolTipTextForChildren() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        Component[] children = comboBox.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        for ( int i = 0; i < children.length; ++i ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if ( children[i] instanceof JComponent ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                ((JComponent)children[i]).setToolTipText( comboBox.getToolTipText() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * This layout manager handles the 'standard' layout of combo boxes.  It puts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * the arrow button to the right and the editor to the left.  If there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * editor it still keeps the arrow button to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * This public inner class should be treated as protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Instantiate it only within subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <code>BasicComboBoxUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public class ComboBoxLayoutManager implements LayoutManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        public void addLayoutComponent(String name, Component comp) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        public void removeLayoutComponent(Component comp) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            return getHandler().preferredLayoutSize(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            return getHandler().minimumLayoutSize(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            getHandler().layoutContainer(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    // end Inner classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    //====================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    //===============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    // begin Sub-Component Management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Creates and initializes the components which make up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * aggregate combo box. This method is called as part of the UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * installation process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    protected void installComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        arrowButton = createArrowButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (arrowButton != null)  {
17902
14a0ae3ca973 6337518: Null Arrow Button Throws Exception in BasicComboBoxUI
alitvinov
parents: 15637
diff changeset
   697
            comboBox.add(arrowButton);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            configureArrowButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if ( comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            addEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        comboBox.add( currentValuePane );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * The aggregate components which compise the combo box are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * unregistered and uninitialized. This method is called as part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * UI uninstallation process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    protected void uninstallComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if ( arrowButton != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            unconfigureArrowButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if ( editor != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            unconfigureEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        comboBox.removeAll(); // Just to be safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * This public method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * do not call or override. To implement a specific editor create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * custom <code>ComboBoxEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @see #createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @see javax.swing.JComboBox#setEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @see javax.swing.ComboBoxEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    public void addEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        removeEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        editor = comboBox.getEditor().getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if ( editor != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            configureEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            comboBox.add(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if(comboBox.isFocusOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                // Switch focus to the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                editor.requestFocusInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * This public method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @see #addEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public void removeEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if ( editor != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            unconfigureEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            comboBox.remove( editor );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @see #addEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    protected void configureEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        // Should be in the same state as the combobox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        editor.setEnabled(comboBox.isEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        editor.setFocusable(comboBox.isFocusable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        editor.setFont( comboBox.getFont() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (focusListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            editor.addFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        editor.addFocusListener( getHandler() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        comboBox.getEditor().addActionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if(editor instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            ((JComponent)editor).putClientProperty("doNotCancelPopup",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                                                   HIDE_POPUP_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            ((JComponent)editor).setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        comboBox.configureEditor(comboBox.getEditor(),comboBox.getSelectedItem());
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   789
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   790
        editor.addPropertyChangeListener(propertyChangeListener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @see #addEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    protected void unconfigureEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        if (focusListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            editor.removeFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   804
        editor.removePropertyChangeListener(propertyChangeListener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        editor.removeFocusListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        comboBox.getEditor().removeActionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * This public method is implementation specific and should be private. Do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @see #createArrowButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public void configureArrowButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if ( arrowButton != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            arrowButton.setEnabled( comboBox.isEnabled() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            arrowButton.setFocusable(comboBox.isFocusable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            arrowButton.setRequestFocusEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            arrowButton.addMouseListener( popup.getMouseListener() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            arrowButton.addMouseMotionListener( popup.getMouseMotionListener() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            arrowButton.resetKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            arrowButton.putClientProperty("doNotCancelPopup", HIDE_POPUP_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            arrowButton.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * This public method is implementation specific and should be private. Do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @see #createArrowButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    public void unconfigureArrowButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        if ( arrowButton != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            arrowButton.removeMouseListener( popup.getMouseListener() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            arrowButton.removeMouseMotionListener( popup.getMouseMotionListener() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   842
     * Creates a button which will be used as the control to show or hide
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * the popup portion of the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @return a button which represents the popup control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    protected JButton createArrowButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        JButton button = new BasicArrowButton(BasicArrowButton.SOUTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                    UIManager.getColor("ComboBox.buttonBackground"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                                    UIManager.getColor("ComboBox.buttonShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                    UIManager.getColor("ComboBox.buttonDarkShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                                    UIManager.getColor("ComboBox.buttonHighlight"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        button.setName("ComboBox.arrowButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        return button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    // end Sub-Component Management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    //===============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    //================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    // begin ComboBoxUI Implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Tells if the popup is visible or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    public boolean isPopupVisible( JComboBox c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        return popup.isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Hides the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    public void setPopupVisible( JComboBox c, boolean v ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        if ( v ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            popup.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            popup.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Determines if the JComboBox is focus traversable.  If the JComboBox is editable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * this returns false, otherwise it returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    public boolean isFocusTraversable( JComboBox c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        return !comboBox.isEditable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    // end ComboBoxUI Implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    //==============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    //=================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    // begin ComponentUI Implementation
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   899
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    public void paint( Graphics g, JComponent c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        hasFocus = comboBox.hasFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if ( !comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            Rectangle r = rectangleForCurrentValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            paintCurrentValueBackground(g,r,hasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            paintCurrentValue(g,r,hasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   909
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    public Dimension getPreferredSize( JComponent c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        return getMinimumSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * The minumum size is the size of the display area plus insets plus the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   917
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    public Dimension getMinimumSize( JComponent c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if ( !isMinimumSizeDirty ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            return new Dimension(cachedMinimumSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        Dimension size = getDisplaySize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        Insets insets = getInsets();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   924
        //calculate the width and height of the button
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   925
        int buttonHeight = size.height;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   926
        int buttonWidth = squareButton ? buttonHeight : arrowButton.getPreferredSize().width;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   927
        //adjust the size based on the button width
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        size.height += insets.top + insets.bottom;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   929
        size.width +=  insets.left + insets.right + buttonWidth;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        cachedMinimumSize.setSize( size.width, size.height );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        isMinimumSizeDirty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        return new Dimension(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   937
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    public Dimension getMaximumSize( JComponent c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   950
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        int baseline = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        // force sameBaseline to be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        getDisplaySize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        if (sameBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            Insets insets = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            height = height - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            if (!comboBox.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                ListCellRenderer renderer = comboBox.getRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                if (renderer == null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    renderer = new DefaultListCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                Object prototypeValue = comboBox.getPrototypeDisplayValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                if (prototypeValue != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    value = prototypeValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                else if (comboBox.getModel().getSize() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    // Note, we're assuming the baseline is the same for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    // cells, if not, this needs to loop through all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    value = comboBox.getModel().getElementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    value = " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                } else if (value instanceof String && "".equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    value = " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                Component component = renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                        getListCellRendererComponent(listBox, value, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                                                     false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                if (component instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    component.setFont(comboBox.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                baseline = component.getBaseline(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                baseline = editor.getBaseline(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            if (baseline > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                baseline += insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1005
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        // Force sameBaseline to be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        getDisplaySize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        if (comboBox.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            return editor.getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        else if (sameBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            ListCellRenderer renderer = comboBox.getRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            if (renderer == null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                renderer = new DefaultListCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            Object prototypeValue = comboBox.getPrototypeDisplayValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            if (prototypeValue != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                value = prototypeValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            else if (comboBox.getModel().getSize() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                // Note, we're assuming the baseline is the same for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                // cells, if not, this needs to loop through all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                value = comboBox.getModel().getElementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                Component component = renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                        getListCellRendererComponent(listBox, value, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                                                     false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                return component.getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    // This is currently hacky...
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1040
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    public int getAccessibleChildrenCount(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if ( comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    // This is currently hacky...
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1051
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    public Accessible getAccessibleChild(JComponent c, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        // 0 = the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        // 1 = the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        switch ( i ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            if ( popup instanceof Accessible ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                AccessibleContext ac = ((Accessible) popup).getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                ac.setAccessibleParent(comboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                return(Accessible) popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            if ( comboBox.isEditable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                 && (editor instanceof Accessible) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                AccessibleContext ac = ((Accessible) editor).getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                ac.setAccessibleParent(comboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                return(Accessible) editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    // end ComponentUI Implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    //===============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    //======================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    // begin Utility Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * Returns whether or not the supplied keyCode maps to a key that is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * navigation.  This is used for optimizing key input by only passing non-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * navigation keys to the type-ahead mechanism.  Subclasses should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * if they change the navigation keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    protected boolean isNavigationKey( int keyCode ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        return keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
               keyCode == KeyEvent.VK_KP_UP || keyCode == KeyEvent.VK_KP_DOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    private boolean isNavigationKey(int keyCode, int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        InputMap inputMap = comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        KeyStroke key = KeyStroke.getKeyStroke(keyCode, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (inputMap != null && inputMap.get(key) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * Selects the next item in the list.  It won't change the selection if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * currently selected item is already the last item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    protected void selectNextPossibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        int si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        if ( comboBox.isPopupVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            si = listBox.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            si = comboBox.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        if ( si < comboBox.getModel().getSize() - 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            listBox.setSelectedIndex( si + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            listBox.ensureIndexIsVisible( si + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            if ( !isTableCellEditor ) {
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1123
                if (!(UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && comboBox.isPopupVisible())) {
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1124
                    comboBox.setSelectedIndex(si+1);
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1125
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            comboBox.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Selects the previous item in the list.  It won't change the selection if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * currently selected item is already the first item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    protected void selectPreviousPossibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        int si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        if ( comboBox.isPopupVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            si = listBox.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            si = comboBox.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        if ( si > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            listBox.setSelectedIndex( si - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            listBox.ensureIndexIsVisible( si - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            if ( !isTableCellEditor ) {
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1149
                if (!(UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && comboBox.isPopupVisible())) {
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1150
                    comboBox.setSelectedIndex(si-1);
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1151
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            comboBox.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Hides the popup if it is showing and shows the popup if it is hidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    protected void toggleOpenClose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        setPopupVisible(comboBox, !isPopupVisible(comboBox));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * Returns the area that is reserved for drawing the currently selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    protected Rectangle rectangleForCurrentValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        int width = comboBox.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        int height = comboBox.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        int buttonSize = height - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        if ( arrowButton != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            buttonSize = arrowButton.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        if(BasicGraphicsUtils.isLeftToRight(comboBox)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            return new Rectangle(insets.left, insets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                             width - (insets.left + insets.right + buttonSize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                             height - (insets.top + insets.bottom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            return new Rectangle(insets.left + buttonSize, insets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                             width - (insets.left + insets.right + buttonSize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                             height - (insets.top + insets.bottom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * Gets the insets from the JComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    protected Insets getInsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        return comboBox.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    // end Utility Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    //====================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    //===============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    // begin Painting Utility Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * Paints the currently selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        ListCellRenderer renderer = comboBox.getRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        if ( hasFocus && !isPopupVisible(comboBox) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            c = renderer.getListCellRendererComponent( listBox,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                                                       comboBox.getSelectedItem(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                                                       -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                                                       true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                                                       false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            c = renderer.getListCellRendererComponent( listBox,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                                                       comboBox.getSelectedItem(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                                                       -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                                                       false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                                                       false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            c.setBackground(UIManager.getColor("ComboBox.background"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        c.setFont(comboBox.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        if ( hasFocus && !isPopupVisible(comboBox) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            c.setForeground(listBox.getSelectionForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            c.setBackground(listBox.getSelectionBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            if ( comboBox.isEnabled() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                c.setForeground(comboBox.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                c.setBackground(comboBox.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                c.setForeground(DefaultLookup.getColor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                         comboBox, this, "ComboBox.disabledForeground", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                c.setBackground(DefaultLookup.getColor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                         comboBox, this, "ComboBox.disabledBackground", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        // Fix for 4238829: should lay out the JPanel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        boolean shouldValidate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        if (c instanceof JPanel)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            shouldValidate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1249
        int x = bounds.x, y = bounds.y, w = bounds.width, h = bounds.height;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1250
        if (padding != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1251
            x = bounds.x + padding.left;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1252
            y = bounds.y + padding.top;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1253
            w = bounds.width - (padding.left + padding.right);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1254
            h = bounds.height - (padding.top + padding.bottom);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1255
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1256
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1257
        currentValuePane.paintComponent(g,c,comboBox,x,y,w,h,shouldValidate);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * Paints the background of the currently selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        Color t = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        if ( comboBox.isEnabled() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            g.setColor(DefaultLookup.getColor(comboBox, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                                              "ComboBox.background", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            g.setColor(DefaultLookup.getColor(comboBox, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                                     "ComboBox.disabledBackground", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        g.setColor(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * Repaint the currently selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    void repaintCurrentValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        Rectangle r = rectangleForCurrentValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        comboBox.repaint(r.x,r.y,r.width,r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    // end Painting Utility Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    //=============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    //===============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    // begin Size Utility Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * Return the default size of an empty display area of the combo box using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * the current renderer and font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @return the size of an empty display area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @see #getDisplaySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    protected Dimension getDefaultSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        // Calculates the height and width using the default text renderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        Dimension d = getSizeForComponent(getDefaultListCellRenderer().getListCellRendererComponent(listBox, " ", -1, false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        return new Dimension(d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * Returns the calculated size of the display area. The display area is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * portion of the combo box in which the selected item is displayed. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * method will use the prototype display value if it has been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * For combo boxes with a non trivial number of items, it is recommended to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * use a prototype display value to significantly speed up the display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * size calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * @return the size of the display area calculated from the combo box items
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * @see javax.swing.JComboBox#setPrototypeDisplayValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    protected Dimension getDisplaySize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        if (!isDisplaySizeDirty)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            return new Dimension(cachedDisplaySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        Dimension result = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        ListCellRenderer renderer = comboBox.getRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        if (renderer == null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            renderer = new DefaultListCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        sameBaseline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        Object prototypeValue = comboBox.getPrototypeDisplayValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        if (prototypeValue != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            // Calculates the dimension based on the prototype value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            result = getSizeForComponent(renderer.getListCellRendererComponent(listBox,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                                                                               prototypeValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                                                                               -1, false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            // Calculate the dimension by iterating over all the elements in the combo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            // box list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            ComboBoxModel model = comboBox.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            int modelSize = model.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            int baseline = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            Dimension d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            Component cpn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            if (modelSize > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                for (int i = 0; i < modelSize ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                    // Calculates the maximum height and width based on the largest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    // element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                    Object value = model.getElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    Component c = renderer.getListCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                            listBox, value, -1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                    d = getSizeForComponent(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                    if (sameBaseline && value != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                            (!(value instanceof String) || !"".equals(value))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                        int newBaseline = c.getBaseline(d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                        if (newBaseline == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                            sameBaseline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                        else if (baseline == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                            baseline = newBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                        else if (baseline != newBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                            sameBaseline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                    result.width = Math.max(result.width,d.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                    result.height = Math.max(result.height,d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                result = getDefaultSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                if (comboBox.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                    result.width = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        if ( comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            Dimension d = editor.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            result.width = Math.max(result.width,d.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            result.height = Math.max(result.height,d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1385
        // calculate in the padding
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1386
        if (padding != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1387
            result.width += padding.left + padding.right;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1388
            result.height += padding.top + padding.bottom;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1389
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1390
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        // Set the cached value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        cachedDisplaySize.setSize(result.width, result.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        isDisplaySizeDirty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1399
     * Returns the size a component would have if used as a cell renderer.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1400
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1401
     * @param comp a {@code Component} to check
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1402
     * @return size of the component
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1403
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1405
    protected Dimension getSizeForComponent(Component comp) {
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1406
        // This has been refactored out in hopes that it may be investigated and
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1407
        // simplified for the next major release. adding/removing
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1408
        // the component to the currentValuePane and changing the font may be
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1409
        // redundant operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        currentValuePane.add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        comp.setFont(comboBox.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        Dimension d = comp.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        currentValuePane.remove(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    // end Size Utility Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    //=============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    //=================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    // begin Keyboard Action Management
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * Adds keyboard actions to the JComboBox.  Actions on enter and esc are already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * supplied.  Add more actions as you need them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        InputMap km = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        SwingUtilities.replaceUIInputMap(comboBox, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                             WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        LazyActionMap.installLazyActionMap(comboBox, BasicComboBoxUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                                           "ComboBox.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            return (InputMap)DefaultLookup.get(comboBox, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                                               "ComboBox.ancestorInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    boolean isTableCellEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        return isTableCellEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * Removes the focus InputMap and ActionMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        SwingUtilities.replaceUIInputMap(comboBox, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                                 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        SwingUtilities.replaceUIActionMap(comboBox, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    // Actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        private static final String HIDE = "hidePopup";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        private static final String DOWN = "selectNext";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        private static final String DOWN_2 = "selectNext2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        private static final String TOGGLE = "togglePopup";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        private static final String TOGGLE_2 = "spacePopup";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        private static final String UP = "selectPrevious";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        private static final String UP_2 = "selectPrevious2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        private static final String ENTER = "enterPressed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        private static final String PAGE_DOWN = "pageDownPassThrough";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        private static final String PAGE_UP = "pageUpPassThrough";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        private static final String HOME = "homePassThrough";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        private static final String END = "endPassThrough";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        Actions(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        public void actionPerformed( ActionEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            JComboBox comboBox = (JComboBox)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            BasicComboBoxUI ui = (BasicComboBoxUI)BasicLookAndFeel.getUIOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                                  comboBox.getUI(), BasicComboBoxUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            if (key == HIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                comboBox.firePopupMenuCanceled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                comboBox.setPopupVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            else if (key == PAGE_DOWN || key == PAGE_UP ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                     key == HOME || key == END) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                int index = getNextIndex(comboBox, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                if (index >= 0 && index < comboBox.getItemCount()) {
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1497
                    if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && comboBox.isPopupVisible()) {
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1498
                        ui.listBox.setSelectedIndex(index);
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1499
                        ui.listBox.ensureIndexIsVisible(index);
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1500
                        comboBox.repaint();
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1501
                    } else {
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1502
                        comboBox.setSelectedIndex(index);
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1503
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            else if (key == DOWN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                if (comboBox.isShowing() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                    if ( comboBox.isPopupVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                        if (ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                            ui.selectNextPossibleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                        comboBox.setPopupVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            else if (key == DOWN_2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                // Special case in which pressing the arrow keys will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                // make the popup appear - except for editable combo boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                // and combo boxes inside a table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                if (comboBox.isShowing() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                    if ( (comboBox.isEditable() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                            (ui != null && ui.isTableCellEditor()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                         && !comboBox.isPopupVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                        comboBox.setPopupVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                        if (ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                            ui.selectNextPossibleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            else if (key == TOGGLE || key == TOGGLE_2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                if (ui != null && (key == TOGGLE || !comboBox.isEditable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                    if ( ui.isTableCellEditor() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                        // Forces the selection of the list item if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                        // combo box is in a JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                        comboBox.setSelectedIndex(ui.popup.getList().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                                                  getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                        comboBox.setPopupVisible(!comboBox.isPopupVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            else if (key == UP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                if (ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    if (ui.isPopupVisible(comboBox)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                        ui.selectPreviousPossibleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                    else if (DefaultLookup.getBoolean(comboBox, ui,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                                    "ComboBox.showPopupOnNavigation", false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                        ui.setPopupVisible(comboBox, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            else if (key == UP_2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                 // Special case in which pressing the arrow keys will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                 // make the popup appear - except for editable combo boxes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                 if (comboBox.isShowing() && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                     if ( comboBox.isEditable() && !comboBox.isPopupVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                         comboBox.setPopupVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                     } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                         ui.selectPreviousPossibleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            else if (key == ENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                if (comboBox.isPopupVisible()) {
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1571
                    // If ComboBox.noActionOnKeyNavigation is set,
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1572
                    // forse selection of list item
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1573
                    if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                        Object listItem = ui.popup.getList().getSelectedValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                        if (listItem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                            comboBox.getEditor().setItem(listItem);
1284
39d9a711aad4 6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents: 2
diff changeset
  1577
                            comboBox.setSelectedItem(listItem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                        }
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1579
                        comboBox.setPopupVisible(false);
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1580
                    } else {
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1581
                        // Forces the selection of the list item
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1582
                        boolean isEnterSelectablePopup =
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1583
                                UIManager.getBoolean("ComboBox.isEnterSelectablePopup");
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1584
                        if (!comboBox.isEditable() || isEnterSelectablePopup
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1585
                                || ui.isTableCellEditor) {
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1586
                            Object listItem = ui.popup.getList().getSelectedValue();
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1587
                            if (listItem != null) {
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1588
                                // Use the selected value from popup
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1589
                                // to set the selected item in combo box,
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1590
                                // but ensure before that JComboBox.actionPerformed()
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1591
                                // won't use editor's value to set the selected item
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1592
                                comboBox.getEditor().setItem(listItem);
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1593
                                comboBox.setSelectedItem(listItem);
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1594
                            }
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1595
                        }
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1596
                        comboBox.setPopupVisible(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                else {
1284
39d9a711aad4 6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents: 2
diff changeset
  1600
                    // Hide combo box if it is a table cell editor
39d9a711aad4 6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents: 2
diff changeset
  1601
                    if (ui.isTableCellEditor && !comboBox.isEditable()) {
39d9a711aad4 6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents: 2
diff changeset
  1602
                        comboBox.setSelectedItem(comboBox.getSelectedItem());
39d9a711aad4 6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents: 2
diff changeset
  1603
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                    // Call the default button binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                    // This is a pretty messy way of passing an event through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                    // to the root pane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                    JRootPane root = SwingUtilities.getRootPane(comboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                    if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                        InputMap im = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                        ActionMap am = root.getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                        if (im != null && am != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                            Object obj = im.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                            if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                                Action action = am.get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                                if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                                    action.actionPerformed(new ActionEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                                     root, e.getID(), e.getActionCommand(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                                     e.getWhen(), e.getModifiers()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        private int getNextIndex(JComboBox comboBox, String key) {
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1628
            int listHeight = comboBox.getMaximumRowCount();
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1629
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1630
            int selectedIndex = comboBox.getSelectedIndex();
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1631
            if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation")
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1632
                    && (comboBox.getUI() instanceof BasicComboBoxUI)) {
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1633
                selectedIndex = ((BasicComboBoxUI) comboBox.getUI()).listBox.getSelectedIndex();
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1634
            }
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1635
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            if (key == PAGE_UP) {
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1637
                int index = selectedIndex - listHeight;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                return (index < 0 ? 0: index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            else if (key == PAGE_DOWN) {
15637
2c226ebab6a6 4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents: 9035
diff changeset
  1641
                int index = selectedIndex + listHeight;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                int max = comboBox.getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                return (index < max ? index: max-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            else if (key == HOME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            else if (key == END) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                return comboBox.getItemCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            return comboBox.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        public boolean isEnabled(Object c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            if (getName() == HIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                return (c != null && ((JComboBox)c).isPopupVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    // end Keyboard Action Management
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    //===============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    // Shared Handler, implements all listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    private class Handler implements ActionListener, FocusListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                                     KeyListener, LayoutManager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                                     ListDataListener, PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            String propertyName = e.getPropertyName();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1677
            if (e.getSource() == editor){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1678
                // If the border of the editor changes then this can effect
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1679
                // the size of the editor which can cause the combo's size to
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1680
                // become invalid so we need to clear size caches
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1681
                if ("border".equals(propertyName)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1682
                    isMinimumSizeDirty = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1683
                    isDisplaySizeDirty = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1684
                    comboBox.revalidate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1686
            } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1687
                JComboBox comboBox = (JComboBox)e.getSource();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1688
                if ( propertyName == "model" ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1689
                    ComboBoxModel newModel = (ComboBoxModel)e.getNewValue();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1690
                    ComboBoxModel oldModel = (ComboBoxModel)e.getOldValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1692
                    if ( oldModel != null && listDataListener != null ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1693
                        oldModel.removeListDataListener( listDataListener );
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1694
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1695
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1696
                    if ( newModel != null && listDataListener != null ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1697
                        newModel.addListDataListener( listDataListener );
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1698
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1700
                    if ( editor != null ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1701
                        comboBox.configureEditor( comboBox.getEditor(), comboBox.getSelectedItem() );
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1702
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1703
                    isMinimumSizeDirty = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1704
                    isDisplaySizeDirty = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1705
                    comboBox.revalidate();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1706
                    comboBox.repaint();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1707
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1708
                else if ( propertyName == "editor" && comboBox.isEditable() ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1709
                    addEditor();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1710
                    comboBox.revalidate();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1711
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1712
                else if ( propertyName == "editable" ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1713
                    if ( comboBox.isEditable() ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1714
                        comboBox.setRequestFocusEnabled( false );
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1715
                        addEditor();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1716
                    } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1717
                        comboBox.setRequestFocusEnabled( true );
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1718
                        removeEditor();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1719
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1720
                    updateToolTipTextForChildren();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1721
                    comboBox.revalidate();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1722
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1723
                else if ( propertyName == "enabled" ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1724
                    boolean enabled = comboBox.isEnabled();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1725
                    if ( editor != null )
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1726
                        editor.setEnabled(enabled);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1727
                    if ( arrowButton != null )
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1728
                        arrowButton.setEnabled(enabled);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1729
                    comboBox.repaint();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1731
                else if ( propertyName == "focusable" ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1732
                    boolean focusable = comboBox.isFocusable();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1733
                    if ( editor != null )
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1734
                        editor.setFocusable(focusable);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1735
                    if ( arrowButton != null )
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1736
                        arrowButton.setFocusable(focusable);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1737
                    comboBox.repaint();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1738
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1739
                else if ( propertyName == "maximumRowCount" ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1740
                    if ( isPopupVisible( comboBox ) ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1741
                        setPopupVisible(comboBox, false);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1742
                        setPopupVisible(comboBox, true);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1743
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1745
                else if ( propertyName == "font" ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1746
                    listBox.setFont( comboBox.getFont() );
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1747
                    if ( editor != null ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1748
                        editor.setFont( comboBox.getFont() );
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1749
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1750
                    isMinimumSizeDirty = true;
8143
1b0feb422660 6988168: Press the "Toggle Font" button.The size of the combo box didn't change.
rupashka
parents: 5506
diff changeset
  1751
                    isDisplaySizeDirty = true;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1752
                    comboBox.validate();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1753
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1754
                else if ( propertyName == JComponent.TOOL_TIP_TEXT_KEY ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1755
                    updateToolTipTextForChildren();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1756
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1757
                else if ( propertyName == BasicComboBoxUI.IS_TABLE_CELL_EDITOR ) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1758
                    Boolean inTable = (Boolean)e.getNewValue();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1759
                    isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1760
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1761
                else if (propertyName == "prototypeDisplayValue") {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1762
                    isMinimumSizeDirty = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1763
                    isDisplaySizeDirty = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1764
                    comboBox.revalidate();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1765
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1766
                else if (propertyName == "renderer") {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1767
                    isMinimumSizeDirty = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1768
                    isDisplaySizeDirty = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1769
                    comboBox.revalidate();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1770
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        // KeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        // This listener checks to see if the key event isn't a navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        // key.  If it finds a key event that wasn't a navigation key it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        // dispatches it to JComboBox.selectWithKeyChar() so that it can do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        // type-ahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        public void keyPressed( KeyEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            if ( isNavigationKey(e.getKeyCode(), e.getModifiers()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                lastTime = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            } else if ( comboBox.isEnabled() && comboBox.getModel().getSize()!=0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                        isTypeAheadKey( e ) && e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                time = e.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                if ( comboBox.selectWithKeyChar(e.getKeyChar()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                    e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        public void keyReleased(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        private boolean isTypeAheadKey( KeyEvent e ) {
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1802
            return !e.isAltDown() && !BasicGraphicsUtils.isMenuShortcutKeyDown(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        // FocusListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        // NOTE: The class is added to both the Editor and ComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        // The combo box listener hides the popup when the focus is lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        // It also repaints when focus is gained or lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        public void focusGained( FocusEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            ComboBoxEditor comboBoxEditor = comboBox.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            if ( (comboBoxEditor != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                 (e.getSource() == comboBoxEditor.getEditorComponent()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            hasFocus = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            comboBox.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            if (comboBox.isEditable() && editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                editor.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        public void focusLost( FocusEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            ComboBoxEditor editor = comboBox.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            if ( (editor != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                 (e.getSource() == editor.getEditorComponent()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                Object item = editor.getItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                Object selectedItem = comboBox.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                if (!e.isTemporary() && item != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                    !item.equals((selectedItem == null) ? "" : selectedItem )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    comboBox.actionPerformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                        (new ActionEvent(editor, 0, "",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                                      EventQueue.getMostRecentEventTime(), 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            hasFocus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            if (!e.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                setPopupVisible(comboBox, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            comboBox.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        // ListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        // This listener watches for changes in the ComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        public void contentsChanged( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            if ( !(e.getIndex0() == -1 && e.getIndex1() == -1) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                isMinimumSizeDirty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                comboBox.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            // set the editor with the selected item since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            // is the event handler for a selected item change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            if (comboBox.isEditable() && editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                comboBox.configureEditor( comboBox.getEditor(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                                          comboBox.getSelectedItem() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            isDisplaySizeDirty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            comboBox.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        public void intervalAdded( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            contentsChanged( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        public void intervalRemoved( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            contentsChanged( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        // LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        // This layout manager handles the 'standard' layout of combo boxes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        // It puts the arrow button to the right and the editor to the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        // If there is no editor it still keeps the arrow button to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        public void addLayoutComponent(String name, Component comp) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        public void removeLayoutComponent(Component comp) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            return parent.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            return parent.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            JComboBox cb = (JComboBox)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            int width = cb.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            int height = cb.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            Insets insets = getInsets();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1904
            int buttonHeight = height - (insets.top + insets.bottom);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1905
            int buttonWidth = buttonHeight;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1906
            if (arrowButton != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1907
                Insets arrowInsets = arrowButton.getInsets();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1908
                buttonWidth = squareButton ?
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1909
                    buttonHeight :
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1910
                    arrowButton.getPreferredSize().width + arrowInsets.left + arrowInsets.right;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1911
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            Rectangle cvb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1914
            if (arrowButton != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1915
                if (BasicGraphicsUtils.isLeftToRight(cb)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1916
                    arrowButton.setBounds(width - (insets.right + buttonWidth),
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1917
                            insets.top, buttonWidth, buttonHeight);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1918
                } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1919
                    arrowButton.setBounds(insets.left, insets.top,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  1920
                            buttonWidth, buttonHeight);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            if ( editor != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                cvb = rectangleForCurrentValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                editor.setBounds(cvb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        // ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        // Fix for 4515752: Forward the Enter pressed on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        // editable combo box to the default button
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        // Note: This could depend on event ordering. The first ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        // from the editor may be handled by the JComboBox in which case, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        // enterPressed action will always be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            Object item = comboBox.getEditor().getItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            if (item != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
             if(!comboBox.isPopupVisible() && !item.equals(comboBox.getSelectedItem())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
              comboBox.setSelectedItem(comboBox.getEditor().getItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
             ActionMap am = comboBox.getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
             if (am != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                Action action = am.get("enterPressed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                    action.actionPerformed(new ActionEvent(comboBox, evt.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                                           evt.getActionCommand(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                                           evt.getModifiers()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    class DefaultKeySelectionManager implements JComboBox.KeySelectionManager, UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        private String prefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        private String typedString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        public int selectionForKey(char aKey,ComboBoxModel aModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            if (lastTime == 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                prefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                typedString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            boolean startingFromSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            int startIndex = comboBox.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            if (time - lastTime < timeFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                typedString += aKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                if((prefix.length() == 1) && (aKey == prefix.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                    // Subsequent same key presses move the keyboard focus to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                    // object that starts with the same letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                    startIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                    prefix = typedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                startIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                typedString = "" + aKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                prefix = typedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            lastTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
            if (startIndex < 0 || startIndex >= aModel.getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                startingFromSelection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            int index = listBox.getNextMatch(prefix, startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                                             Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            if (index < 0 && startingFromSelection) { // wrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                index = listBox.getNextMatch(prefix, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                                             Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
}