jdk/src/share/classes/javax/swing/plaf/basic/BasicComboPopup.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6103 a2498232c8f2
child 10100 c525c5fbb86c
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6103
diff changeset
     2
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.accessibility.AccessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.border.Border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.border.LineBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This is a basic implementation of the <code>ComboPopup</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class represents the ui for the popup portion of the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * All event handling is handled by listener classes created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>createxxxListener()</code> methods and internal classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * You can change the behavior of this class by overriding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>createxxxListener()</code> methods and supplying your own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * event listeners or subclassing from the ones supplied in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the same version of Swing.  As of 1.4, support for long term storage
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() {
6103
a2498232c8f2 4743225: Size of JComboBox list is wrong when list is populated via PopupMenuListener
alexp
parents: 5506
diff changeset
   205
        comboBox.firePopupMenuWillBecomeVisible();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        setListSelection(comboBox.getSelectedIndex());
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();
6103
a2498232c8f2 4743225: Size of JComboBox list is wrong when list is populated via PopupMenuListener
alexp
parents: 5506
diff changeset
   347
        // comboBox.firePopupMenuWillBecomeVisible() is called from BasicComboPopup.show() method
a2498232c8f2 4743225: Size of JComboBox list is wrong when list is populated via PopupMenuListener
alexp
parents: 5506
diff changeset
   348
        // to let the user change the popup menu from the PopupMenuListener.popupMenuWillBecomeVisible()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    protected void firePopupMenuWillBecomeInvisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        super.firePopupMenuWillBecomeInvisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        comboBox.firePopupMenuWillBecomeInvisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    protected void firePopupMenuCanceled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        super.firePopupMenuCanceled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        comboBox.firePopupMenuCanceled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Creates a listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * that will watch for mouse-press and release events on the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * When overriding this method, make sure to maintain the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return a <code>MouseListener</code> which will be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * the combo box or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    protected MouseListener createMouseListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Creates the mouse motion listener which will be added to the combo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * When overriding this method, make sure to maintain the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return a <code>MouseMotionListener</code> which will be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *         the combo box or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    protected MouseMotionListener createMouseMotionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Creates the key listener that will be added to the combo box. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * this method returns null then it will not be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return a <code>KeyListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    protected KeyListener createKeyListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Creates a list selection listener that watches for selection changes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * the popup's list.  If this method returns null then it will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * be added to the popup list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @return an instance of a <code>ListSelectionListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    protected ListSelectionListener createListSelectionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Creates a list data listener which will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * <code>ComboBoxModel</code>. If this method returns null then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * it will not be added to the combo box model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return an instance of a <code>ListDataListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    protected ListDataListener createListDataListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Creates a mouse listener that watches for mouse events in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * the popup's list. If this method returns null then it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * not be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @return an instance of a <code>MouseListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    protected MouseListener createListMouseListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Creates a mouse motion listener that watches for mouse motion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * events in the popup's list. If this method returns null then it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * not be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return an instance of a <code>MouseMotionListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    protected MouseMotionListener createListMouseMotionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Creates a <code>PropertyChangeListener</code> which will be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * the combo box. If this method returns null then it will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @return an instance of a <code>PropertyChangeListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Creates an <code>ItemListener</code> which will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * combo box. If this method returns null then it will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * be added to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Subclasses may override this method to return instances of their own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * ItemEvent handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @return an instance of an <code>ItemListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    protected ItemListener createItemListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Creates the JList used in the popup to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * the items in the combo box model. This method is called when the UI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @return a <code>JList</code> used to display the combo box items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    protected JList createList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return new JList( comboBox.getModel() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            public void processMouseEvent(MouseEvent e)  {
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2
diff changeset
   487
                if (BasicGraphicsUtils.isMenuShortcutKeyDown(e))  {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    // Fix for 4234053. Filter out the Control Key from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    // ie., don't allow CTRL key deselection.
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2
diff changeset
   490
                    Toolkit toolkit = Toolkit.getDefaultToolkit();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    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
   492
                                       e.getModifiers() ^ toolkit.getMenuShortcutKeyMask(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                       e.getX(), e.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                       e.getXOnScreen(), e.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                       e.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                       e.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                       MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                super.processMouseEvent(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Configures the list which is used to hold the combo box items in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * popup. This method is called when the UI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @see #createList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    protected void configureList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        list.setFont( comboBox.getFont() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        list.setForeground( comboBox.getForeground() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        list.setBackground( comboBox.getBackground() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        list.setSelectionForeground( UIManager.getColor( "ComboBox.selectionForeground" ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        list.setSelectionBackground( UIManager.getColor( "ComboBox.selectionBackground" ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        list.setBorder( null );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        list.setCellRenderer( comboBox.getRenderer() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        list.setFocusable( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        setListSelection( comboBox.getSelectedIndex() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        installListListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Adds the listeners to the list control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    protected void installListListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if ((listMouseListener = createListMouseListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            list.addMouseListener( listMouseListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if ((listMouseMotionListener = createListMouseMotionListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            list.addMouseMotionListener( listMouseMotionListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if ((listSelectionListener = createListSelectionListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            list.addListSelectionListener( listSelectionListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    void uninstallListListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (listMouseListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            list.removeMouseListener(listMouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            listMouseListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (listMouseMotionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            list.removeMouseMotionListener(listMouseMotionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            listMouseMotionListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (listSelectionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            list.removeListSelectionListener(listSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            listSelectionListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Creates the scroll pane which houses the scrollable list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    protected JScrollPane createScroller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        JScrollPane sp = new JScrollPane( list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        sp.setHorizontalScrollBar(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        return sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Configures the scrollable portion which holds the list within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * the combo box popup. This method is called when the UI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    protected void configureScroller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        scroller.setFocusable( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        scroller.getVerticalScrollBar().setFocusable( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        scroller.setBorder( null );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Configures the popup portion of the combo box. This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * when the UI class is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    protected void configurePopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        setBorderPainted( true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        setBorder(LIST_BORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        setOpaque( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        add( scroller );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        setDoubleBuffered( true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        setFocusable( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * This method adds the necessary listeners to the JComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    protected void installComboBoxListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if ((propertyChangeListener = createPropertyChangeListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            comboBox.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if ((itemListener = createItemListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            comboBox.addItemListener(itemListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        installComboBoxModelListeners(comboBox.getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Installs the listeners on the combo box model. Any listeners installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * on the combo box model should be removed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <code>uninstallComboBoxModelListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @param model The combo box model to install listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @see #uninstallComboBoxModelListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    protected void installComboBoxModelListeners( ComboBoxModel model ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (model != null && (listDataListener = createListDataListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            model.addListDataListener(listDataListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        /* XXX - shouldn't call this method. take it out for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        ActionListener action = new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            public void actionPerformed(ActionEvent e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        comboBox.registerKeyboardAction( action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                         KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                         JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); */
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
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    // end Initialization routines
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
    //===================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    // begin Event Listenters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * A listener to be registered upon the combo box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * (<em>not</em> its popup menu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * to handle mouse events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * that affect the state of the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * The main purpose of this listener is to make the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * appear and disappear.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * This listener also helps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * with click-and-drag scenarios by setting the selection if the mouse was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * released over the list during a drag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * We recommend that you <em>not</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * create subclasses of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * If you absolutely must create a subclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * be sure to invoke the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * version of each method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @see BasicComboPopup#createMouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    protected class InvocationMouseHandler extends MouseAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         * Responds to mouse-pressed events on the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         * @param e the mouse-press event to be handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        public void mousePressed( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            getHandler().mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * Responds to the user terminating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         * a click or drag that began on the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * @param e the mouse-release event to be handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        public void mouseReleased( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            getHandler().mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * This listener watches for dragging and updates the current selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * list if it is dragging over the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    protected class InvocationMouseMotionHandler extends MouseMotionAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        public void mouseDragged( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            getHandler().mouseDragged(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * 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
   696
     * backwards API compatibility. Do not instantiate or subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * All the functionality of this class has been included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * BasicComboBoxUI ActionMap/InputMap methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public class InvocationKeyHandler extends KeyAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        public void keyReleased( KeyEvent e ) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * 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
   707
     * is only included for backwards API compatibility. Do not call or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    protected class ListSelectionHandler implements ListSelectionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        public void valueChanged( ListSelectionEvent e ) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * As of 1.4, this class is now obsolete, doesn't do anything, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * is only included for backwards API compatibility. Do not call or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * The functionality has been migrated into <code>ItemHandler</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @see #createItemListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    public class ListDataHandler implements ListDataListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        public void contentsChanged( ListDataEvent e ) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        public void intervalAdded( ListDataEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        public void intervalRemoved( ListDataEvent e ) {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * This listener hides the popup when the mouse is released in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    protected class ListMouseHandler extends MouseAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        public void mousePressed( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        public void mouseReleased(MouseEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            getHandler().mouseReleased(anEvent);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * This listener changes the selected item as you move the mouse over the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * The selection change is not committed to the model, this is for user feedback only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    protected class ListMouseMotionHandler extends MouseMotionAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        public void mouseMoved( MouseEvent anEvent ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            getHandler().mouseMoved(anEvent);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * This listener watches for changes to the selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    protected class ItemHandler implements ItemListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        public void itemStateChanged( ItemEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            getHandler().itemStateChanged(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * This listener watches for bound properties that have changed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * Subclasses which wish to listen to combo box property changes should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * call the superclass methods to ensure that the combo popup correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * handles property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    protected class PropertyChangeHandler implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        public void propertyChange( PropertyChangeEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    private class AutoScrollActionHandler implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        private int direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        AutoScrollActionHandler(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (direction == SCROLL_UP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                autoScrollUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                autoScrollDown();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    private class Handler implements ItemListener, MouseListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                          MouseMotionListener, PropertyChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                          Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        // MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // NOTE: this is added to both the JList and JComboBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (e.getSource() == list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            if (!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            if ( comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                Component comp = comboBox.getEditor().getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                if ((!(comp instanceof JComponent)) || ((JComponent)comp).isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    comp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            else if (comboBox.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                comboBox.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            togglePopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            if (e.getSource() == list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                if (list.getModel().getSize() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    // JList mouse listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    if (comboBox.getSelectedIndex() == list.getSelectedIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                        comboBox.getEditor().setItem(list.getSelectedValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    comboBox.setSelectedIndex(list.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                comboBox.setPopupVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                // workaround for cancelling an edited item (bug 4530953)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                if (comboBox.isEditable() && comboBox.getEditor() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    comboBox.configureEditor(comboBox.getEditor(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                                             comboBox.getSelectedItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            // JComboBox mouse listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            Component source = (Component)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            Dimension size = source.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            Rectangle bounds = new Rectangle( 0, 0, size.width - 1, size.height - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if ( !bounds.contains( e.getPoint() ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                MouseEvent newEvent = convertMouseEvent( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                Point location = newEvent.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                Rectangle r = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                list.computeVisibleRect( r );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                if ( r.contains( location ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    if (comboBox.getSelectedIndex() == list.getSelectedIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        comboBox.getEditor().setItem(list.getSelectedValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    comboBox.setSelectedIndex(list.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                comboBox.setPopupVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            hasEntered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            stopAutoScrolling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        // MouseMotionListener:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        // NOTE: this is added to both the List and ComboBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        public void mouseMoved(MouseEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            if (anEvent.getSource() == list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                Point location = anEvent.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                Rectangle r = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                list.computeVisibleRect( r );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                if ( r.contains( location ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                    updateListBoxSelectionForEvent( anEvent, false );
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        public void mouseDragged( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            if (e.getSource() == list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if ( isVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                MouseEvent newEvent = convertMouseEvent( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                Rectangle r = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                list.computeVisibleRect( r );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                if ( newEvent.getPoint().y >= r.y && newEvent.getPoint().y <= r.y + r.height - 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    hasEntered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    if ( isAutoScrolling ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                        stopAutoScrolling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    Point location = newEvent.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    if ( r.contains( location ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        updateListBoxSelectionForEvent( newEvent, false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    if ( hasEntered ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        int directionToScroll = newEvent.getPoint().y < r.y ? SCROLL_UP : SCROLL_DOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        if ( isAutoScrolling && scrollDirection != directionToScroll ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                            stopAutoScrolling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                            startAutoScrolling( directionToScroll );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        else if ( !isAutoScrolling ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                            startAutoScrolling( directionToScroll );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                        if ( e.getPoint().y < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                            hasEntered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                            startAutoScrolling( SCROLL_UP );
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
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            JComboBox comboBox = (JComboBox)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            if ( propertyName == "model" ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                ComboBoxModel oldModel = (ComboBoxModel)e.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                ComboBoxModel newModel = (ComboBoxModel)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                uninstallComboBoxModelListeners(oldModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                installComboBoxModelListeners(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                list.setModel(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                if ( isVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            else if ( propertyName == "renderer" ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                list.setCellRenderer( comboBox.getRenderer() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                if ( isVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            else if (propertyName == "componentOrientation") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                // Pass along the new component orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                // to the list and the scroller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                ComponentOrientation o =(ComponentOrientation)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                JList list = getList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                if (list!=null && list.getComponentOrientation()!=o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    list.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                if (scroller!=null && scroller.getComponentOrientation()!=o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    scroller.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                if (o!=getComponentOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            else if (propertyName == "lightWeightPopupEnabled") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled());
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
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        // ItemListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        public void itemStateChanged( ItemEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            if (e.getStateChange() == ItemEvent.SELECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                JComboBox comboBox = (JComboBox)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                setListSelection(comboBox.getSelectedIndex());
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
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    // end Event Listeners
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * Overridden to unconditionally return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    public boolean isFocusTraversable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    //===================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    // begin Autoscroll methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    protected void startAutoScrolling( int direction ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        // XXX - should be a private method within InvocationMouseMotionHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        // if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        if ( isAutoScrolling ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            autoscrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        isAutoScrolling = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        if ( direction == SCROLL_UP ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            scrollDirection = SCROLL_UP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            Point convertedPoint = SwingUtilities.convertPoint( scroller, new Point( 1, 1 ), list );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            int top = list.locationToIndex( convertedPoint );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            list.setSelectedIndex( top );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            autoscrollTimer = new Timer( 100, new AutoScrollActionHandler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                                             SCROLL_UP) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        else if ( direction == SCROLL_DOWN ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            scrollDirection = SCROLL_DOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            Dimension size = scroller.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            Point convertedPoint = SwingUtilities.convertPoint( scroller,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                                                                new Point( 1, (size.height - 1) - 2 ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                                                                list );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            int bottom = list.locationToIndex( convertedPoint );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            list.setSelectedIndex( bottom );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            autoscrollTimer = new Timer(100, new AutoScrollActionHandler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                                            SCROLL_DOWN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        autoscrollTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    protected void stopAutoScrolling() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        isAutoScrolling = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if ( autoscrollTimer != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            autoscrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            autoscrollTimer = null;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    protected void autoScrollUp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        int index = list.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        if ( index > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            list.setSelectedIndex( index - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            list.ensureIndexIsVisible( index - 1 );
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * This protected method is implementation specific and should be private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * do not call or override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    protected void autoScrollDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        int index = list.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        int lastItem = list.getModel().getSize() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        if ( index < lastItem ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            list.setSelectedIndex( index + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            list.ensureIndexIsVisible( index + 1 );
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
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    // end Autoscroll methods
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
    //===================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    // begin Utility methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * Gets the AccessibleContext associated with this BasicComboPopup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * The AccessibleContext will have its parent set to the ComboBox.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @return an AccessibleContext for the BasicComboPopup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        AccessibleContext context = super.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        context.setAccessibleParent(comboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        return context;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * This is is a utility method that helps event handlers figure out where to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * send the focus when the popup is brought up.  The standard implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * delegates the focus to the editor (if the combo box is editable) or to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * the JComboBox if it is not editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    protected void delegateFocus( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        if ( comboBox.isEditable() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            Component comp = comboBox.getEditor().getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            if ((!(comp instanceof JComponent)) || ((JComponent)comp).isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                comp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        else if (comboBox.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            comboBox.requestFocus();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * Makes the popup visible if it is hidden and makes it hidden if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    protected void togglePopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        if ( isVisible() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            show();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * Sets the list selection index to the selectedIndex. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * method is used to synchronize the list selection with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * combo box selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * @param selectedIndex the index to set the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    private void setListSelection(int selectedIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        if ( selectedIndex == -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            list.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            list.setSelectedIndex( selectedIndex );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            list.ensureIndexIsVisible( selectedIndex );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    protected MouseEvent convertMouseEvent( MouseEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        Point convertedPoint = SwingUtilities.convertPoint( (Component)e.getSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                                                            e.getPoint(), list );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        MouseEvent newEvent = new MouseEvent( (Component)e.getSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                                              e.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                                              e.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                                              e.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                                              convertedPoint.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                                              convertedPoint.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                                              e.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                                              e.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                                              e.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                                              e.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                                              MouseEvent.NOBUTTON );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        return newEvent;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * Retrieves the height of the popup based on the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * ListCellRenderer and the maximum row count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    protected int getPopupHeightForRowCount(int maxRowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // Set the cached value of the minimum row count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        int minRowCount = Math.min( maxRowCount, comboBox.getItemCount() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        ListCellRenderer renderer = list.getCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        for ( int i = 0; i < minRowCount; ++i ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            value = list.getModel().getElementAt( i );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            Component c = renderer.getListCellRendererComponent( list, value, i, false, false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            height += c.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if (height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            height = comboBox.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        Border border = scroller.getViewportBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            Insets insets = border.getBorderInsets(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        border = scroller.getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            Insets insets = border.getBorderInsets(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * Calculate the placement and size of the popup portion of the combo box based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * on the combo box location and the enclosing screen bounds. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * no transformations are required, then the returned rectangle will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * have the same values as the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @param px starting x location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @param py starting y location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @param pw starting width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * @param ph starting height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * @return a rectangle which represents the placement and size of the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    protected Rectangle computePopupBounds(int px,int py,int pw,int ph) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        Rectangle screenBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        // Calculate the desktop dimensions relative to the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        GraphicsConfiguration gc = comboBox.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        Point p = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        SwingUtilities.convertPointFromScreen(p, comboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        if (gc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            Insets screenInsets = toolkit.getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            screenBounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            screenBounds.width -= (screenInsets.left + screenInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            screenBounds.height -= (screenInsets.top + screenInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            screenBounds.x += (p.x + screenInsets.left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            screenBounds.y += (p.y + screenInsets.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            screenBounds = new Rectangle(p, toolkit.getScreenSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        Rectangle rect = new Rectangle(px,py,pw,ph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        if (py+ph > screenBounds.y+screenBounds.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            && ph < screenBounds.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            rect.y = -rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * Calculates the upper left location of the Popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    private Point getPopupLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        Dimension popupSize = comboBox.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        // reduce the width of the scrollpane by the insets so that the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        // is the same width as the combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        popupSize.setSize(popupSize.width - (insets.right + insets.left),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                          getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        Rectangle popupBounds = computePopupBounds( 0, comboBox.getBounds().height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                                                    popupSize.width, popupSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        Dimension scrollSize = popupBounds.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        Point popupLocation = popupBounds.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        scroller.setMaximumSize( scrollSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        scroller.setPreferredSize( scrollSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        scroller.setMinimumSize( scrollSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        list.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        return popupLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * A utility method used by the event listeners.  Given a mouse event, it changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * the list selection to the list item below the mouse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    protected void updateListBoxSelectionForEvent(MouseEvent anEvent,boolean shouldScroll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        // XXX - only seems to be called from this class. shouldScroll flag is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        // never true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        Point location = anEvent.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        if ( list == null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        int index = list.locationToIndex(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        if ( index == -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            if ( location.y < 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                index = comboBox.getModel().getSize() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        if ( list.getSelectedIndex() != index ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            list.setSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            if ( shouldScroll )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                list.ensureIndexIsVisible(index);
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
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    // end Utility methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    //=================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
}