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