jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboPopup.java
author weijun
Thu, 11 Dec 2014 15:19:12 +0800
changeset 28228 be83f404724d
parent 25859 3317bb8137f4
child 28059 e576535359cc
child 28231 b608ffcaed74
permissions -rw-r--r--
8067207: Replace concat String to append in StringBuilder parameters (client) Reviewed-by: serb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1998, 2014, 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: 3977
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: 3977
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: 3977
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
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 javax.accessibility.AccessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.border.Border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.border.LineBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.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 java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This is a basic implementation of the <code>ComboPopup</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class represents the ui for the popup portion of the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * All event handling is handled by listener classes created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>createxxxListener()</code> methods and internal classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * You can change the behavior of this class by overriding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>createxxxListener()</code> methods and supplying your own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * event listeners or subclassing from the ones supplied in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 10100
diff changeset
    56
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Mark Davidson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    63
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class BasicComboPopup extends JPopupMenu implements ComboPopup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // An empty ListMode, this is used when the UI changes to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // the JList to be gc'ed.
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
    67
    private static class EmptyListModelClass implements ListModel<Object>, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public int getSize() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public Object getElementAt(int index) { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public void addListDataListener(ListDataListener l) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public void removeListDataListener(ListDataListener l) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
    74
    static final ListModel<Object> EmptyListModel = new EmptyListModelClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static Border LIST_BORDER = new LineBorder(Color.BLACK, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
    78
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
    79
     * The instance of {@code JComboBox}.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
    80
     */
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
    81
    protected JComboBox<Object>             comboBox;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * or override. Use the accessor methods instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @see #getList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see #createList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
    89
    protected JList<Object>                 list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * or override. Use the create method instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see #createScroller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected JScrollPane              scroller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * As of Java 2 platform v1.4 this previously undocumented field is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * longer used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected boolean                  valueIsAdjusting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // Listeners that are required by the ComboPopup interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Implementation of all the listener classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * or override. Use the accessor or create methods instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @see #getMouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @see #createMouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    protected MouseMotionListener      mouseMotionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * or override. Use the accessor or create methods instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see #getMouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see #createMouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected MouseListener            mouseListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * or override. Use the accessor or create methods instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see #getKeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see #createKeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected KeyListener              keyListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * or override. Use the create method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see #createListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected ListSelectionListener    listSelectionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // Listeners that are attached to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * or override. Use the create method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see #createListMouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected MouseListener            listMouseListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * or override. Use the create method instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see #createListMouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected MouseMotionListener      listMouseMotionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // Added to the combo box for bound properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * or override. Use the create method instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected PropertyChangeListener   propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // Added to the combo box model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * or override. Use the create method instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see #createListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected ListDataListener         listDataListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * or override. Use the create method instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see #createItemListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    protected ItemListener             itemListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This protected field is implementation specific. Do not access directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected Timer                    autoscrollTimer;
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   192
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   193
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   194
     * {@code true} if the mouse cursor is in the popup.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   195
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    protected boolean                  hasEntered = false;
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   197
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   198
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   199
     * If {@code true} the auto-scrolling is enabled.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   200
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected boolean                  isAutoScrolling = false;
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   202
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   203
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   204
     * The direction of scrolling.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   205
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected int                      scrollDirection = SCROLL_UP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   208
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   209
     * The direction of scrolling up.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   210
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    protected static final int         SCROLL_UP = 0;
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   212
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   213
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   214
     * The direction of scrolling down.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   215
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    protected static final int         SCROLL_DOWN = 1;
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
    // begin ComboPopup method implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Implementation of ComboPopup.show().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void show() {
6103
a2498232c8f2 4743225: Size of JComboBox list is wrong when list is populated via PopupMenuListener
alexp
parents: 5506
diff changeset
   227
        comboBox.firePopupMenuWillBecomeVisible();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        setListSelection(comboBox.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Point location = getPopupLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        show( comboBox, location.x, location.y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Implementation of ComboPopup.hide().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void hide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        MenuSelectionManager manager = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        MenuElement [] selection = manager.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        for ( int i = 0 ; i < selection.length ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if ( selection[i] == this ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                manager.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (selection.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            comboBox.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Implementation of ComboPopup.getList().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   254
    public JList<Object> getList() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Implementation of ComboPopup.getMouseListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return a <code>MouseListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @see ComboPopup#getMouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public MouseListener getMouseListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (mouseListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            mouseListener = createMouseListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return mouseListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Implementation of ComboPopup.getMouseMotionListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return a <code>MouseMotionListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see ComboPopup#getMouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public MouseMotionListener getMouseMotionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (mouseMotionListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            mouseMotionListener = createMouseMotionListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return mouseMotionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Implementation of ComboPopup.getKeyListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @return a <code>KeyListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @see ComboPopup#getKeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public KeyListener getKeyListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (keyListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            keyListener = createKeyListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return keyListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Called when the UI is uninstalling.  Since this popup isn't in the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * tree, it won't get it's uninstallUI() called.  It removes the listeners that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * were added in addComboBoxListeners().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public void uninstallingUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (propertyChangeListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            comboBox.removePropertyChangeListener( propertyChangeListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (itemListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            comboBox.removeItemListener( itemListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        uninstallComboBoxModelListeners(comboBox.getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        uninstallListListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // We do this, otherwise the listener the ui installs on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // the model (the combobox model in this case) will keep a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // reference to the list, causing the list (and us) to never get gced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        list.setModel(EmptyListModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    // end ComboPopup method implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    //======================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Removes the listeners from the combo box model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param model The combo box model to install listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see #installComboBoxModelListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   328
    protected void uninstallComboBoxModelListeners( ComboBoxModel<?> model ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (model != null && listDataListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            model.removeListDataListener(listDataListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   334
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   335
     * Unregisters keyboard actions.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   336
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // XXX - shouldn't call this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
//        comboBox.unregisterKeyboardAction( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    //===================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    // begin Initialization routines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    //
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   347
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   348
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   349
     * Constructs a new instance of {@code BasicComboPopup}.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   350
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   351
     * @param combo an instance of {@code JComboBox}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   352
     */
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   353
    public BasicComboPopup( JComboBox<Object> combo ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        setName("ComboPopup.popup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        comboBox = combo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        // UI construction of the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        list = createList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        list.setName("ComboBox.list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        configureList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        scroller = createScroller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        scroller.setName("ComboBox.scrollPane");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        configureScroller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        configurePopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        installComboBoxListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    // Overriden PopupMenuListener notification methods to inform combo box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    // PopupMenuListeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    protected void firePopupMenuWillBecomeVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        super.firePopupMenuWillBecomeVisible();
6103
a2498232c8f2 4743225: Size of JComboBox list is wrong when list is populated via PopupMenuListener
alexp
parents: 5506
diff changeset
   378
        // comboBox.firePopupMenuWillBecomeVisible() is called from BasicComboPopup.show() method
a2498232c8f2 4743225: Size of JComboBox list is wrong when list is populated via PopupMenuListener
alexp
parents: 5506
diff changeset
   379
        // to let the user change the popup menu from the PopupMenuListener.popupMenuWillBecomeVisible()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    protected void firePopupMenuWillBecomeInvisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        super.firePopupMenuWillBecomeInvisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        comboBox.firePopupMenuWillBecomeInvisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    protected void firePopupMenuCanceled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        super.firePopupMenuCanceled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        comboBox.firePopupMenuCanceled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Creates a listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * that will watch for mouse-press and release events on the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * When overriding this method, make sure to maintain the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return a <code>MouseListener</code> which will be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * the combo box or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    protected MouseListener createMouseListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Creates the mouse motion listener which will be added to the combo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * When overriding this method, make sure to maintain the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @return a <code>MouseMotionListener</code> which will be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *         the combo box or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    protected MouseMotionListener createMouseMotionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Creates the key listener that will be added to the combo box. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * this method returns null then it will not be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @return a <code>KeyListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    protected KeyListener createKeyListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Creates a list selection listener that watches for selection changes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * the popup's list.  If this method returns null then it will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * be added to the popup list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @return an instance of a <code>ListSelectionListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    protected ListSelectionListener createListSelectionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Creates a list data listener which will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <code>ComboBoxModel</code>. If this method returns null then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * it will not be added to the combo box model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @return an instance of a <code>ListDataListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    protected ListDataListener createListDataListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Creates a mouse listener that watches for mouse events in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * the popup's list. If this method returns null then it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * not be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @return an instance of a <code>MouseListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    protected MouseListener createListMouseListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Creates a mouse motion listener that watches for mouse motion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * events in the popup's list. If this method returns null then it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * not be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @return an instance of a <code>MouseMotionListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    protected MouseMotionListener createListMouseMotionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Creates a <code>PropertyChangeListener</code> which will be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * the combo box. If this method returns null then it will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @return an instance of a <code>PropertyChangeListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Creates an <code>ItemListener</code> which will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * combo box. If this method returns null then it will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Subclasses may override this method to return instances of their own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * ItemEvent handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @return an instance of an <code>ItemListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    protected ItemListener createItemListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Creates the JList used in the popup to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * the items in the combo box model. This method is called when the UI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @return a <code>JList</code> used to display the combo box items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   515
    protected JList<Object> createList() {
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   516
        return new JList<Object>( comboBox.getModel() ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            public void processMouseEvent(MouseEvent e)  {
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2
diff changeset
   518
                if (BasicGraphicsUtils.isMenuShortcutKeyDown(e))  {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    // Fix for 4234053. Filter out the Control Key from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    // ie., don't allow CTRL key deselection.
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2
diff changeset
   521
                    Toolkit toolkit = Toolkit.getDefaultToolkit();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    e = new MouseEvent((Component)e.getSource(), e.getID(), e.getWhen(),
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2
diff changeset
   523
                                       e.getModifiers() ^ toolkit.getMenuShortcutKeyMask(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                       e.getX(), e.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                       e.getXOnScreen(), e.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                       e.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                       e.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                       MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                super.processMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Configures the list which is used to hold the combo box items in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * popup. This method is called when the UI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @see #createList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    protected void configureList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        list.setFont( comboBox.getFont() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        list.setForeground( comboBox.getForeground() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        list.setBackground( comboBox.getBackground() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        list.setSelectionForeground( UIManager.getColor( "ComboBox.selectionForeground" ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        list.setSelectionBackground( UIManager.getColor( "ComboBox.selectionBackground" ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        list.setBorder( null );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        list.setCellRenderer( comboBox.getRenderer() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        list.setFocusable( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        setListSelection( comboBox.getSelectedIndex() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        installListListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Adds the listeners to the list control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    protected void installListListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if ((listMouseListener = createListMouseListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            list.addMouseListener( listMouseListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if ((listMouseMotionListener = createListMouseMotionListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            list.addMouseMotionListener( listMouseMotionListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if ((listSelectionListener = createListSelectionListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            list.addListSelectionListener( listSelectionListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    void uninstallListListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (listMouseListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            list.removeMouseListener(listMouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            listMouseListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (listMouseMotionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            list.removeMouseMotionListener(listMouseMotionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            listMouseMotionListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (listSelectionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            list.removeListSelectionListener(listSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            listSelectionListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Creates the scroll pane which houses the scrollable list.
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   589
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   590
     * @return the scroll pane which houses the scrollable list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    protected JScrollPane createScroller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        JScrollPane sp = new JScrollPane( list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        sp.setHorizontalScrollBar(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Configures the scrollable portion which holds the list within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * the combo box popup. This method is called when the UI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    protected void configureScroller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        scroller.setFocusable( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        scroller.getVerticalScrollBar().setFocusable( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        scroller.setBorder( null );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Configures the popup portion of the combo box. This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * when the UI class is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    protected void configurePopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        setBorderPainted( true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        setBorder(LIST_BORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        setOpaque( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        add( scroller );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        setDoubleBuffered( true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        setFocusable( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * This method adds the necessary listeners to the JComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    protected void installComboBoxListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if ((propertyChangeListener = createPropertyChangeListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            comboBox.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if ((itemListener = createItemListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            comboBox.addItemListener(itemListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        installComboBoxModelListeners(comboBox.getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * Installs the listeners on the combo box model. Any listeners installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * on the combo box model should be removed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <code>uninstallComboBoxModelListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param model The combo box model to install listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @see #uninstallComboBoxModelListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     */
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   646
    protected void installComboBoxModelListeners( ComboBoxModel<?> model ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (model != null && (listDataListener = createListDataListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            model.addListDataListener(listDataListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   652
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   653
     * Registers keyboard actions.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
   654
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        /* XXX - shouldn't call this method. take it out for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        ActionListener action = new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            public void actionPerformed(ActionEvent e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        comboBox.registerKeyboardAction( action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                         KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                                         JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    // end Initialization routines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    //=================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    //===================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    // begin Event Listenters
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
     * A listener to be registered upon the combo box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * (<em>not</em> its popup menu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * to handle mouse events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * that affect the state of the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * The main purpose of this listener is to make the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * appear and disappear.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * This listener also helps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * with click-and-drag scenarios by setting the selection if the mouse was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * released over the list during a drag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * We recommend that you <em>not</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * create subclasses of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * If you absolutely must create a subclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * be sure to invoke the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * version of each method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @see BasicComboPopup#createMouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    protected class InvocationMouseHandler extends MouseAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         * Responds to mouse-pressed events on the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         * @param e the mouse-press event to be handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        public void mousePressed( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            getHandler().mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         * Responds to the user terminating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         * a click or drag that began on the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         * @param e the mouse-release event to be handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        public void mouseReleased( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            getHandler().mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * This listener watches for dragging and updates the current selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * list if it is dragging over the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    protected class InvocationMouseMotionHandler extends MouseMotionAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        public void mouseDragged( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            getHandler().mouseDragged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * As of Java 2 platform v 1.4, this class is now obsolete and is only included for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * backwards API compatibility. Do not instantiate or subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * All the functionality of this class has been included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * BasicComboBoxUI ActionMap/InputMap methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    public class InvocationKeyHandler extends KeyAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        public void keyReleased( KeyEvent e ) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * As of Java 2 platform v 1.4, this class is now obsolete, doesn't do anything, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * is only included for backwards API compatibility. Do not call or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    protected class ListSelectionHandler implements ListSelectionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        public void valueChanged( ListSelectionEvent e ) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * As of 1.4, this class is now obsolete, doesn't do anything, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * is only included for backwards API compatibility. Do not call or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * The functionality has been migrated into <code>ItemHandler</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @see #createItemListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public class ListDataHandler implements ListDataListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        public void contentsChanged( ListDataEvent e ) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        public void intervalAdded( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        public void intervalRemoved( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * This listener hides the popup when the mouse is released in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    protected class ListMouseHandler extends MouseAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        public void mousePressed( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        public void mouseReleased(MouseEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            getHandler().mouseReleased(anEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * This listener changes the selected item as you move the mouse over the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * The selection change is not committed to the model, this is for user feedback only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    protected class ListMouseMotionHandler extends MouseMotionAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        public void mouseMoved( MouseEvent anEvent ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            getHandler().mouseMoved(anEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * This listener watches for changes to the selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    protected class ItemHandler implements ItemListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        public void itemStateChanged( ItemEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            getHandler().itemStateChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * This listener watches for bound properties that have changed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Subclasses which wish to listen to combo box property changes should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * call the superclass methods to ensure that the combo popup correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * handles property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    protected class PropertyChangeHandler implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        public void propertyChange( PropertyChangeEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    private class AutoScrollActionHandler implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        private int direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        AutoScrollActionHandler(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            if (direction == SCROLL_UP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                autoScrollUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                autoScrollDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    private class Handler implements ItemListener, MouseListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                          MouseMotionListener, PropertyChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                          Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        // MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // NOTE: this is added to both the JList and JComboBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            if (e.getSource() == list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            if ( comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                Component comp = comboBox.getEditor().getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                if ((!(comp instanceof JComponent)) || ((JComponent)comp).isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    comp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            else if (comboBox.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                comboBox.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            togglePopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            if (e.getSource() == list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                if (list.getModel().getSize() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    // JList mouse listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    if (comboBox.getSelectedIndex() == list.getSelectedIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                        comboBox.getEditor().setItem(list.getSelectedValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    comboBox.setSelectedIndex(list.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                comboBox.setPopupVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                // workaround for cancelling an edited item (bug 4530953)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                if (comboBox.isEditable() && comboBox.getEditor() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                    comboBox.configureEditor(comboBox.getEditor(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                                             comboBox.getSelectedItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            // JComboBox mouse listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            Component source = (Component)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            Dimension size = source.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            Rectangle bounds = new Rectangle( 0, 0, size.width - 1, size.height - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            if ( !bounds.contains( e.getPoint() ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                MouseEvent newEvent = convertMouseEvent( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                Point location = newEvent.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                Rectangle r = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                list.computeVisibleRect( r );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                if ( r.contains( location ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                    if (comboBox.getSelectedIndex() == list.getSelectedIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                        comboBox.getEditor().setItem(list.getSelectedValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    comboBox.setSelectedIndex(list.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                comboBox.setPopupVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            hasEntered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            stopAutoScrolling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        // MouseMotionListener:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        // NOTE: this is added to both the List and ComboBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        public void mouseMoved(MouseEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            if (anEvent.getSource() == list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                Point location = anEvent.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                Rectangle r = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                list.computeVisibleRect( r );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                if ( r.contains( location ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    updateListBoxSelectionForEvent( anEvent, false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        public void mouseDragged( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            if (e.getSource() == list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            if ( isVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                MouseEvent newEvent = convertMouseEvent( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                Rectangle r = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                list.computeVisibleRect( r );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                if ( newEvent.getPoint().y >= r.y && newEvent.getPoint().y <= r.y + r.height - 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    hasEntered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    if ( isAutoScrolling ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                        stopAutoScrolling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    Point location = newEvent.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    if ( r.contains( location ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                        updateListBoxSelectionForEvent( newEvent, false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    if ( hasEntered ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                        int directionToScroll = newEvent.getPoint().y < r.y ? SCROLL_UP : SCROLL_DOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                        if ( isAutoScrolling && scrollDirection != directionToScroll ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                            stopAutoScrolling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                            startAutoScrolling( directionToScroll );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        else if ( !isAutoScrolling ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                            startAutoScrolling( directionToScroll );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                        if ( e.getPoint().y < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                            hasEntered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                            startAutoScrolling( SCROLL_UP );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        public void propertyChange(PropertyChangeEvent e) {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   967
            @SuppressWarnings("unchecked")
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   968
            JComboBox<Object> comboBox = (JComboBox)e.getSource();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            if ( propertyName == "model" ) {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   972
                @SuppressWarnings("unchecked")
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   973
                ComboBoxModel<Object> oldModel = (ComboBoxModel)e.getOldValue();
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   974
                @SuppressWarnings("unchecked")
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   975
                ComboBoxModel<Object> newModel = (ComboBoxModel)e.getNewValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                uninstallComboBoxModelListeners(oldModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                installComboBoxModelListeners(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                list.setModel(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                if ( isVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                    hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            else if ( propertyName == "renderer" ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                list.setCellRenderer( comboBox.getRenderer() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                if ( isVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                    hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            else if (propertyName == "componentOrientation") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                // Pass along the new component orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                // to the list and the scroller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                ComponentOrientation o =(ComponentOrientation)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
   997
                JList<?> list = getList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                if (list!=null && list.getComponentOrientation()!=o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    list.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                if (scroller!=null && scroller.getComponentOrientation()!=o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                    scroller.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                if (o!=getComponentOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            else if (propertyName == "lightWeightPopupEnabled") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        // ItemListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        public void itemStateChanged( ItemEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            if (e.getStateChange() == ItemEvent.SELECTED) {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
  1020
                @SuppressWarnings("unchecked")
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
  1021
                JComboBox<Object> comboBox = (JComboBox)e.getSource();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                setListSelection(comboBox.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    // end Event Listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    //=================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * Overridden to unconditionally return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    public boolean isFocusTraversable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    //===================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    // begin Autoscroll methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * do not call or override.
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1046
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1047
     * @param direction the direction of scrolling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    protected void startAutoScrolling( int direction ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        // XXX - should be a private method within InvocationMouseMotionHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        // if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if ( isAutoScrolling ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            autoscrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        isAutoScrolling = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        if ( direction == SCROLL_UP ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            scrollDirection = SCROLL_UP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            Point convertedPoint = SwingUtilities.convertPoint( scroller, new Point( 1, 1 ), list );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            int top = list.locationToIndex( convertedPoint );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            list.setSelectedIndex( top );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            autoscrollTimer = new Timer( 100, new AutoScrollActionHandler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                                             SCROLL_UP) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        else if ( direction == SCROLL_DOWN ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            scrollDirection = SCROLL_DOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            Dimension size = scroller.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            Point convertedPoint = SwingUtilities.convertPoint( scroller,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                                                                new Point( 1, (size.height - 1) - 2 ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                                                                list );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            int bottom = list.locationToIndex( convertedPoint );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            list.setSelectedIndex( bottom );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            autoscrollTimer = new Timer(100, new AutoScrollActionHandler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                                            SCROLL_DOWN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        autoscrollTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    protected void stopAutoScrolling() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        isAutoScrolling = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if ( autoscrollTimer != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            autoscrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            autoscrollTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    protected void autoScrollUp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        int index = list.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if ( index > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            list.setSelectedIndex( index - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            list.ensureIndexIsVisible( index - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    protected void autoScrollDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        int index = list.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        int lastItem = list.getModel().getSize() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        if ( index < lastItem ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            list.setSelectedIndex( index + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            list.ensureIndexIsVisible( index + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    // end Autoscroll methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    //=================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    //===================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    // begin Utility methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * Gets the AccessibleContext associated with this BasicComboPopup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * The AccessibleContext will have its parent set to the ComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * @return an AccessibleContext for the BasicComboPopup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        AccessibleContext context = super.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        context.setAccessibleParent(comboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * This is is a utility method that helps event handlers figure out where to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * send the focus when the popup is brought up.  The standard implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * delegates the focus to the editor (if the combo box is editable) or to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * the JComboBox if it is not editable.
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1148
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1149
     * @param e a mouse event
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    protected void delegateFocus( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        if ( comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            Component comp = comboBox.getEditor().getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            if ((!(comp instanceof JComponent)) || ((JComponent)comp).isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                comp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        else if (comboBox.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            comboBox.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * Makes the popup visible if it is hidden and makes it hidden if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    protected void togglePopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        if ( isVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * Sets the list selection index to the selectedIndex. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * method is used to synchronize the list selection with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * combo box selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @param selectedIndex the index to set the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    private void setListSelection(int selectedIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if ( selectedIndex == -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            list.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            list.setSelectedIndex( selectedIndex );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            list.ensureIndexIsVisible( selectedIndex );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1193
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1194
     * Converts mouse event.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1195
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1196
     * @param e a mouse event
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1197
     * @return converted mouse event
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1198
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    protected MouseEvent convertMouseEvent( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        Point convertedPoint = SwingUtilities.convertPoint( (Component)e.getSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                                                            e.getPoint(), list );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        MouseEvent newEvent = new MouseEvent( (Component)e.getSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                                              e.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                                              e.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                                              e.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                                              convertedPoint.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                                              convertedPoint.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                                              e.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                              e.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                              e.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                                              e.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                                              MouseEvent.NOBUTTON );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        return newEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * Retrieves the height of the popup based on the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * ListCellRenderer and the maximum row count.
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1220
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1221
     * @param maxRowCount the row count
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1222
     * @return the height of the popup
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    protected int getPopupHeightForRowCount(int maxRowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        // Set the cached value of the minimum row count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        int minRowCount = Math.min( maxRowCount, comboBox.getItemCount() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        int height = 0;
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 22574
diff changeset
  1228
        ListCellRenderer<Object> renderer = list.getCellRenderer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        for ( int i = 0; i < minRowCount; ++i ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            value = list.getModel().getElementAt( i );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            Component c = renderer.getListCellRendererComponent( list, value, i, false, false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            height += c.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        if (height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            height = comboBox.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        Border border = scroller.getViewportBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            Insets insets = border.getBorderInsets(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        border = scroller.getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            Insets insets = border.getBorderInsets(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * Calculate the placement and size of the popup portion of the combo box based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * on the combo box location and the enclosing screen bounds. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * no transformations are required, then the returned rectangle will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * have the same values as the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * @param px starting x location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * @param py starting y location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @param pw starting width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * @param ph starting height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * @return a rectangle which represents the placement and size of the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    protected Rectangle computePopupBounds(int px,int py,int pw,int ph) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        Rectangle screenBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        // Calculate the desktop dimensions relative to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        GraphicsConfiguration gc = comboBox.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        Point p = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        SwingUtilities.convertPointFromScreen(p, comboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        if (gc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            Insets screenInsets = toolkit.getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            screenBounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            screenBounds.width -= (screenInsets.left + screenInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            screenBounds.height -= (screenInsets.top + screenInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            screenBounds.x += (p.x + screenInsets.left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            screenBounds.y += (p.y + screenInsets.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            screenBounds = new Rectangle(p, toolkit.getScreenSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        Rectangle rect = new Rectangle(px,py,pw,ph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        if (py+ph > screenBounds.y+screenBounds.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            && ph < screenBounds.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            rect.y = -rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * Calculates the upper left location of the Popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    private Point getPopupLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        Dimension popupSize = comboBox.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        // reduce the width of the scrollpane by the insets so that the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        // is the same width as the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        popupSize.setSize(popupSize.width - (insets.right + insets.left),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                          getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        Rectangle popupBounds = computePopupBounds( 0, comboBox.getBounds().height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                                                    popupSize.width, popupSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        Dimension scrollSize = popupBounds.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        Point popupLocation = popupBounds.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        scroller.setMaximumSize( scrollSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        scroller.setPreferredSize( scrollSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        scroller.setMinimumSize( scrollSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        list.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        return popupLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * A utility method used by the event listeners.  Given a mouse event, it changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * the list selection to the list item below the mouse.
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1324
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1325
     * @param anEvent a mouse event
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 25565
diff changeset
  1326
     * @param shouldScroll if {@code true} list should be scrolled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    protected void updateListBoxSelectionForEvent(MouseEvent anEvent,boolean shouldScroll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        // XXX - only seems to be called from this class. shouldScroll flag is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        // never true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        Point location = anEvent.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        if ( list == null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        int index = list.locationToIndex(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        if ( index == -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            if ( location.y < 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                index = comboBox.getModel().getSize() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        if ( list.getSelectedIndex() != index ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            list.setSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            if ( shouldScroll )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                list.ensureIndexIsVisible(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    // end Utility methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    //=================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
}