jdk/src/solaris/classes/sun/awt/X11/XChoicePeer.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 21267 fe3928f5591f
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18520
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
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: 3938
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: 3938
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
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 sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.peer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    31
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
// FIXME: tab traversal should be disabled when mouse is captured (4816336)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
// FIXME: key and mouse events should not be delivered to listeners when the Choice is unfurled.  Must override handleNativeKey/MouseEvent (4816336)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
// FIXME: test programmatic add/remove/clear/etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
// FIXME: account for unfurling at the edge of the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// Note: can't set x,y on layout(), 'cause moving the top-level to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
// edge of the screen won't call layout().  Just do it on paint, I guess
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
// TODO: make painting more efficient (i.e. when down arrow is pressed, only two items should need to be repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class XChoicePeer extends XComponentPeer implements ChoicePeer, ToplevelStateListener {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    46
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XChoicePeer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int MAX_UNFURLED_ITEMS = 10;  // Maximum number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // items to be displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // at a time in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // unfurled Choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Description of these constants in ListHelper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public final static int TEXT_SPACE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public final static int BORDER_WIDTH = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public final static int ITEM_MARGIN = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public final static int SCROLLBAR_WIDTH = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // SHARE THESE!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final Insets focusInsets = new Insets(0,0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final int WIDGET_OFFSET = 18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Stolen from Tiny
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final int            TEXT_XPAD = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final int            TEXT_YPAD = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // FIXME: Motif uses a different focus color for the item within
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // the unfurled Choice list and for when the Choice itself is focused and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // popped up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static final Color focusColor = Color.black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // TODO: there is a time value that the mouse is held down.  If short
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // enough,  the Choice stays popped down.  If long enough, Choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // is furled when the mouse is released
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean unfurled = false;        // Choice list is popped down
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean dragging = false;        // Mouse was pressed and is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                             // dragged over the (unfurled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                             // Choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private boolean mouseInSB = false;       // Mouse is interacting with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                             // scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private boolean firstPress = false;      // mouse was pressed on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                             // furled Choice so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                             // not need to furl the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21267
diff changeset
    90
                                             // Choice when MOUSE_RELEASED occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // 6425067. Mouse was pressed on furled choice and dropdown list appeared over Choice itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // and then there were no mouse movements until MOUSE_RELEASE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // This scenario leads to ItemStateChanged as the choice logic uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // MouseReleased event to send ItemStateChanged. To prevent it we should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // use a combination of firstPress and wasDragged variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // The only difference in dragging and wasDragged is: last one will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // set to false on mouse ungrab. It become false after MouseRelased() finishes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private boolean wasDragged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private ListHelper helper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private UnfurledChoice unfurledChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // TODO: Choice remembers where it was scrolled to when unfurled - it's not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // always to the currently selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // Indicates whether or not to paint selected item in the choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // Default is to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private boolean drawSelectedItem = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // If set, indicates components under which choice popup should be showed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // The choice's popup width and location should be adjust to appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // under both choice and alignUnder component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private Component alignUnder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // If cursor is outside of an unfurled Choice when the mouse is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // released, Choice item should NOT be updated.  Remember the proper index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private int dragStartIdx = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // Holds the listener (XFileDialogPeer) which the processing events from the choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private XChoicePeerListener choiceListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    XChoicePeer(Choice target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        super(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    void preInit(XCreateWindowParams params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        super.preInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Choice target = (Choice)this.target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int numItems = target.getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        unfurledChoice = new UnfurledChoice(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        getToplevelXWindow().addToplevelStateListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        helper = new ListHelper(unfurledChoice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                getGUIcolors(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                numItems,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                target.getFont(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                MAX_UNFURLED_ITEMS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                TEXT_SPACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                ITEM_MARGIN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                BORDER_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                SCROLLBAR_WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    void postInit(XCreateWindowParams params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        super.postInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        Choice target = (Choice)this.target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int numItems = target.getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // Add all items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        for (int i = 0; i < numItems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            helper.add(target.getItem(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!helper.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            helper.select(target.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            helper.setFocusedIndex(target.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        helper.updateColors(getGUIcolors());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        updateMotifColors(getPeerBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public boolean isFocusable() { return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // 6399679. check if super.setBounds() actually changes the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // component and then compare current Choice size with a new one. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // they differs then hide dropdown menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void setBounds(int x, int y, int width, int height, int op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int oldX = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int oldY = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int oldWidth = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int oldHeight = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        super.setBounds(x, y, width, height, op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (unfurled && (oldX != this.x || oldY != this.y || oldWidth != this.width || oldHeight != this.height) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // TODO: only need to paint the focus bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        super.focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Fix for 6246503 : Disabling a choice after selection locks keyboard, mouse and makes the system unusable, Xtoolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * if setEnabled(false) invoked we should close opened choice in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * order to prevent keyboard/mouse lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public void setEnabled(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        super.setEnabled(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        helper.updateColors(getGUIcolors());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (!value && unfurled){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // TODO: only need to paint the focus bit?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        super.focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    void ungrabInputImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (unfurled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            unfurled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            dragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            mouseInSB = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            unfurledChoice.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        super.ungrabInputImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    void handleJavaKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (e.getID() == KeyEvent.KEY_PRESSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            keyPressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void keyPressed(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        switch(e.getKeyCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // UP & DOWN are same if furled or unfurled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
          case KeyEvent.VK_DOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
          case KeyEvent.VK_KP_DOWN: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
              if (helper.getItemCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                  helper.down();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                  int newIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                  ((Choice)target).select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                  postEvent(new ItemEvent((Choice)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                          ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                          ((Choice)target).getItem(newIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                          ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                  repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
          case KeyEvent.VK_UP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
          case KeyEvent.VK_KP_UP: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
              if (helper.getItemCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                  helper.up();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                  int newIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                  ((Choice)target).select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                  postEvent(new ItemEvent((Choice)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                          ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                          ((Choice)target).getItem(newIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                          ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                  repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
          case KeyEvent.VK_PAGE_DOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
              if (unfurled && !dragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                  int oldIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                  helper.pageDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                  int newIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                  if (oldIdx != newIdx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                      ((Choice)target).select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                      postEvent(new ItemEvent((Choice)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                              ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                              ((Choice)target).getItem(newIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                              ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                      repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
          case KeyEvent.VK_PAGE_UP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
              if (unfurled && !dragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                  int oldIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                  helper.pageUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                  int newIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                  if (oldIdx != newIdx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                      ((Choice)target).select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                      postEvent(new ItemEvent((Choice)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                              ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                              ((Choice)target).getItem(newIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                              ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                      repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
          case KeyEvent.VK_ESCAPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
          case KeyEvent.VK_ENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
              if (unfurled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                  if (dragging){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                      if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                          //This also happens on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                          // - MouseButton2,3, etc. press
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                          // - ENTER press
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                          helper.select(dragStartIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                      } else { //KeyEvent.VK_ENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                          int newIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                          ((Choice)target).select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                          postEvent(new ItemEvent((Choice)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                                  ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                                  ((Choice)target).getItem(newIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                                  ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                  hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                  dragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                  wasDragged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                  mouseInSB = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                  // See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                  if (choiceListener != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                      choiceListener.unfurledChoiceClosing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
              if (unfurled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                  Toolkit.getDefaultToolkit().beep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public boolean handlesWheelScrolling() { return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    void handleJavaMouseWheelEvent(MouseWheelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (unfurled && helper.isVSBVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (ListHelper.doWheelScroll(helper.getVSB(), null, e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    void handleJavaMouseEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        super.handleJavaMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int i = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
          case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
              mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
          case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
              mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
          case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
              mouseDragged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * fix for 5003166: a Choice on XAWT shouldn't react to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * mouse button presses except left. This involves presses on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         * Choice but not on opened part of choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (e.getButton() == MouseEvent.BUTTON1){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            dragStartIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (unfurled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                //fix 6259328: PIT: Choice scrolls when dragging the parent frame while drop-down is active, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                if (! (isMouseEventInChoice(e) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                       unfurledChoice.isMouseEventInside(e)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                // Press on unfurled Choice.  Highlight the item under the cursor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                // but don't send item event or set the text on the button yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                unfurledChoice.trackMouse(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                // Choice is up - unfurl it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                grabInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                unfurledChoice.toFront();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                firstPress = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                wasDragged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                unfurled = true;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * helper method for mouseReleased routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    void hidePopdownMenu(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        ungrabInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        unfurledChoice.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        unfurled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (unfurled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (mouseInSB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                unfurledChoice.trackMouse(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                // We pressed and dragged onto the Choice, or, this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                // second release after clicking to make the Choice "stick"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                // unfurled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                // This release should ungrab/furl, and set the new item if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                // release was over the unfurled Choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                // Fix for 6239944 : Choice shouldn't close its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                // pop-down menu if user presses Mouse on Choice's Scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                // some additional cases like releasing mouse outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                // of Choice are considered too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                boolean isMouseEventInside = unfurledChoice.isMouseEventInside( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                boolean isMouseInListArea = unfurledChoice.isMouseInListArea( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                // Fixed 6318746: REG: File Selection is failing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                // We shouldn't restore the selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                // if the mouse was dragged outside the drop-down choice area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                if (!helper.isEmpty() && !isMouseInListArea && dragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    // Set the selected item back how it was.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    ((Choice)target).select(dragStartIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                // Choice must be closed if user releases mouse on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                // pop-down menu on the second click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                if ( !firstPress && isMouseInListArea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                // Choice must be closed if user releases mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                // outside of Choice's pop-down menu  on the second click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if ( !firstPress && !isMouseEventInside) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                //if user drags Mouse on pop-down menu, Scrollbar or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                // outside the Choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                if ( firstPress && dragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                /* this could happen when user has opened a Choice and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                 * released mouse button. Then he drags mouse on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                 * Scrollbar and releases mouse again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if ( !firstPress && !isMouseInListArea &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                     isMouseEventInside && dragging)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (!helper.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    // Only update the Choice if the mouse button is released
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    // over the list of items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    if (unfurledChoice.isMouseInListArea(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        int newIdx = helper.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        if (newIdx >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            // Update the selected item in the target now that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                            // the mouse selection is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                            if (newIdx != dragStartIdx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                ((Choice)target).select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                // NOTE: We get a repaint when Choice.select()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                // calls our peer.select().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                            if (wasDragged && e.getButton() != MouseEvent.BUTTON1){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                ((Choice)target).select(dragStartIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                            /*fix for 6239941 : Choice triggers ItemEvent when selecting an item with right mouse button, Xtoolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            * We should generate ItemEvent if only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            * LeftMouseButton used */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            if (e.getButton() == MouseEvent.BUTTON1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                (!firstPress || wasDragged ))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                postEvent(new ItemEvent((Choice)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                                        ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                                        ((Choice)target).getItem(newIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                                        ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            // see 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                            if (choiceListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                                choiceListener.unfurledChoiceClosing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                unfurledChoice.trackMouse(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        dragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        wasDragged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        firstPress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        dragStartIdx = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * fix for 5003166. On Motif user are unable to drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * mouse inside opened Choice if he drags the mouse with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * different from LEFT mouse button ( e.g. RIGHT or MIDDLE).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * This fix make impossible to drag mouse inside opened choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * with other mouse buttons rather then LEFT one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if ( e.getModifiers() == MouseEvent.BUTTON1_MASK ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            dragging = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            wasDragged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            unfurledChoice.trackMouse(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    // Stolen from TinyChoicePeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        // TODO: move this impl into ListHelper?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        FontMetrics fm = getFontMetrics(target.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        Choice c = (Choice)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        int w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        for (int i = c.countItems() ; i-- > 0 ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            w = Math.max(fm.stringWidth(c.getItem(i)), w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return new Dimension(w + TEXT_XPAD + WIDGET_OFFSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                             fm.getMaxAscent() + fm.getMaxDescent() + TEXT_YPAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Layout the...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public void layout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
          Dimension size = target.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
          Font f = target.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
          FontMetrics fm = target.getFontMetrics(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
          String text = ((Choice)target).getLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
          textRect.height = fm.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
          checkBoxSize = getChoiceSize(fm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
          // Note - Motif appears to use an left inset that is slightly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
          // scaled to the checkbox/font size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
          cbX = borderInsets.left + checkBoxInsetFromText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
          cbY = size.height / 2 - checkBoxSize / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
          int minTextX = borderInsets.left + 2 * checkBoxInsetFromText + checkBoxSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
          // FIXME: will need to account for alignment?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
          // FIXME: call layout() on alignment changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
          //textRect.width = fm.stringWidth(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
          textRect.width = fm.stringWidth(text == null ? "" : text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
          textRect.x = Math.max(minTextX, size.width / 2 - textRect.width / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
          textRect.y = size.height / 2 - textRect.height / 2 + borderInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
          focusRect.x = focusInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
          focusRect.y = focusInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
          focusRect.width = size.width-(focusInsets.left+focusInsets.right)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
          focusRect.height = size.height-(focusInsets.top+focusInsets.bottom)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
          myCheckMark = AffineTransform.getScaleInstance((double)target.getFont().getSize() / MASTER_SIZE, (double)target.getFont().getSize() / MASTER_SIZE).createTransformedShape(MASTER_CHECKMARK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Paint the choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
10096
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   553
    @Override
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   554
    void paintPeer(final Graphics g) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        Dimension size = getPeerSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        // TODO: when mouse is down over button, widget should be drawn depressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        g.setColor(getPeerBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        g.fillRect(0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        drawMotif3DRect(g, 1, 1, width-2, height-2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        drawMotif3DRect(g, width - WIDGET_OFFSET, (height / 2) - 3, 12, 6, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (!helper.isEmpty() && helper.getSelectedIndex() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            g.setFont(getPeerFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            String lbl = helper.getItem(helper.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (lbl != null && drawSelectedItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                g.setClip(1, 1, width - WIDGET_OFFSET - 2, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                if (isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    g.setColor(getPeerForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    g.drawString(lbl, 5, (height + fm.getMaxAscent()-fm.getMaxDescent())/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    g.setColor(getPeerBackground().brighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    g.drawString(lbl, 5, (height + fm.getMaxAscent()-fm.getMaxDescent())/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    g.setColor(getPeerBackground().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    g.drawString(lbl, 4, ((height + fm.getMaxAscent()-fm.getMaxDescent())/2)-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                g.setClip(0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            paintFocus(g,focusInsets.left,focusInsets.top,size.width-(focusInsets.left+focusInsets.right)-1,size.height-(focusInsets.top+focusInsets.bottom)-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (unfurled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            unfurledChoice.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    protected void paintFocus(Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                              int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        g.setColor(focusColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        g.drawRect(x,y,w,h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * ChoicePeer methods stolen from TinyChoicePeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public void select(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        helper.select(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        helper.setFocusedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public void add(String item, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        helper.add(item, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public void remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        boolean selected = (index == helper.getSelectedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        boolean visibled = (index >= helper.firstDisplayedIndex() && index <= helper.lastDisplayedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        helper.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (helper.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                helper.select(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                helper.select(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * Fix for 6248016
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * After removing the item of the choice we need to reshape unfurled choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * in order to keep actual bounds of the choice
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
         * condition added only for performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (!unfurled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // Fix 6292186: PIT: Choice is not refreshed properly when the last item gets removed, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            // We should take into account that there is no 'select' invoking (hence 'repaint')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            // if the choice is empty (see Choice.java method removeNoInvalidate())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // The condition isn't 'visibled' since it would be cause of the twice repainting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (helper.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         * condition added only for performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * the count of the visible items changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (visibled){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            Rectangle r = unfurledChoice.placeOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            unfurledChoice.reshape(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * condition added only for performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         * the structure of visible items changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         * if removable item is non visible and non selected then there is no repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (visibled || selected){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public void removeAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        helper.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        helper.select(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         * Fix for 6248016
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * After removing the item of the choice we need to reshape unfurled choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * in order to keep actual bounds of the choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        Rectangle r = unfurledChoice.placeOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        unfurledChoice.reshape(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * DEPRECATED: Replaced by add(String, int).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public void addItem(String item, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        add(item, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public void setFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        super.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        helper.setFont(this.font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    public void setForeground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        super.setForeground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        helper.updateColors(getGUIcolors());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public void setBackground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        super.setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        unfurledChoice.setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        helper.updateColors(getGUIcolors());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        updateMotifColors(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public void setDrawSelectedItem(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        drawSelectedItem = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public void setAlignUnder(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        alignUnder = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    // see 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    public void addXChoicePeerListener(XChoicePeerListener l){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        choiceListener = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    // see 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    public void removeXChoicePeerListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        choiceListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public boolean isUnfurled(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return unfurled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /* fix for 6261352. We should detect if current parent Window (containing a Choice) become iconified and hide pop-down menu with grab release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * In this case we should hide pop-down menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    //calls from XWindowPeer. Could accept X-styled state events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    public void stateChangedICCCM(int oldState, int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (unfurled && oldState != newState){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    //calls from XFramePeer. Could accept Frame's states.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    public void stateChangedJava(int oldState, int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (unfurled && oldState != newState){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            hidePopdownMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    /**************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /* Common functionality between List & Choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
       /**************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Inner class for the unfurled Choice list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Much, much more docs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    class UnfurledChoice extends XWindow /*implements XScrollbarClient*/ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // First try - use Choice as the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        public UnfurledChoice(Component target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            super(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // Override so we can do our own create()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        public void preInit(XCreateWindowParams params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // A parent of this window is the target, at this point: wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // Remove parent window; in the following preInit() call we'll calculate as a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            // a correct root window which is the proper parent for override redirect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            params.delete(PARENT_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            super.preInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            // Reset bounds(we'll set them later), set overrideRedirect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            params.remove(BOUNDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            params.add(OVERRIDE_REDIRECT, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        // Generally, bounds should be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        //  x = target.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        //  y = target.y + target.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        //  w = Max(target.width, getLongestItemWidth) + possible vertScrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        //  h = Min(MAX_UNFURLED_ITEMS, target.getItemCount()) * itemHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        Rectangle placeOnScreen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            int numItemsDisplayed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // Motif paints an empty Choice the same size as a single item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (helper.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                numItemsDisplayed = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                int numItems = helper.getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                numItemsDisplayed = Math.min(MAX_UNFURLED_ITEMS, numItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            Point global = XChoicePeer.this.toGlobal(0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (alignUnder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                Rectangle choiceRec = XChoicePeer.this.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                choiceRec.setLocation(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                choiceRec = XChoicePeer.this.toGlobal(choiceRec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                Rectangle alignUnderRec = new Rectangle(alignUnder.getLocationOnScreen(), alignUnder.getSize()); // TODO: Security?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                Rectangle result = choiceRec.union(alignUnderRec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                // we've got the left and width, calculate top and height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                width = result.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                x = result.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                y = result.y + result.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                height = 2*BORDER_WIDTH +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    numItemsDisplayed*(helper.getItemHeight()+2*ITEM_MARGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                x = global.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                y = global.y + XChoicePeer.this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                width = Math.max(XChoicePeer.this.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                 helper.getMaxItemWidth() + 2 * (BORDER_WIDTH + ITEM_MARGIN + TEXT_SPACE) + (helper.isVSBVisible() ? SCROLLBAR_WIDTH : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                height = 2*BORDER_WIDTH +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    numItemsDisplayed*(helper.getItemHeight()+2*ITEM_MARGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            // Don't run off the edge of the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            else if (x + width > screen.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                x = screen.width - width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
21267
fe3928f5591f 8026476: Choice does not get mouse events if it does not have enough place for popup menu
serb
parents: 18525
diff changeset
   817
            if (y + height > screen.height) {
fe3928f5591f 8026476: Choice does not get mouse events if it does not have enough place for popup menu
serb
parents: 18525
diff changeset
   818
                y = global.y - height;
fe3928f5591f 8026476: Choice does not get mouse events if it does not have enough place for popup menu
serb
parents: 18525
diff changeset
   819
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return new Rectangle(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        public void toFront() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            // see 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            if (choiceListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                choiceListener.unfurledChoiceOpening(helper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            Rectangle r = placeOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            reshape(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            super.toFront();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * Track a MouseEvent (either a drag or a press) and paint a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * selected item, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        // FIXME: first unfurl after move is not at edge of the screen  onto second monitor doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // track mouse correctly.  Problem is w/ UnfurledChoice coords
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        public void trackMouse(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            // Event coords are relative to the button, so translate a bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            Point local = toLocalCoords(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            // If x,y is over unfurled Choice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            // highlight item under cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
              case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                  // FIXME: If the Choice is unfurled and the mouse is pressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                  // outside of the Choice, the mouse should ungrab on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                  // the press, not the release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                  if (helper.isInVertSB(getBounds(), local.x, local.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                      mouseInSB = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                      helper.handleVSBEvent(e, getBounds(), local.x, local.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                      trackSelection(local.x, local.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
              case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                  if (mouseInSB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                      mouseInSB = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                      helper.handleVSBEvent(e, getBounds(), local.x, local.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                  }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                      // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                      helper.trackMouseReleasedScroll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                    trackSelection(local.x, local.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
              case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                  if (mouseInSB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                      helper.handleVSBEvent(e, getBounds(), local.x, local.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                      // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                      helper.trackMouseDraggedScroll(local.x, local.y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                      trackSelection(local.x, local.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        private void trackSelection(int transX, int transY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if (!helper.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                if (transX > 0 && transX < width &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    transY > 0 && transY < height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    int newIdx = helper.y2index(transY);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 14330
diff changeset
   895
                    if (log.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        log.fine("transX=" + transX + ", transY=" + transY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                                 + ",width=" + width + ", height=" + height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                                 + ", newIdx=" + newIdx + " on " + target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                    if ((newIdx >=0) && (newIdx < helper.getItemCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                        && (newIdx != helper.getSelectedIndex()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        helper.select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                        unfurledChoice.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            // FIXME: If dragged off top or bottom, scroll if there's a vsb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            // (ICK - we'll need a timer or our own event or something)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
         * fillRect with current Background color on the whole dropdown list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
         */
10096
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   915
        public void paintBackground() {
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   916
            final Graphics g = getGraphics();
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   917
            if (g != null) {
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   918
                try {
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   919
                    g.setColor(getPeerBackground());
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   920
                    g.fillRect(0, 0, width, height);
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   921
                } finally {
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   922
                    g.dispose();
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   923
                }
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   924
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * 6405689. In some cases we should erase background to eliminate painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * artefacts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         */
10096
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   930
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        public void repaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            if (!isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            if (helper.checkVsbVisibilityChangedAndReset()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                paintBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            super.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
10096
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   940
        @Override
f9ac9a52952d 6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents: 5506
diff changeset
   941
        public void paintPeer(Graphics g) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            //System.out.println("UC.paint()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            Choice choice = (Choice)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            Color colors[] = XChoicePeer.this.getGUIcolors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            draw3DRect(g, getSystemColors(), 0, 0, width - 1, height - 1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            draw3DRect(g, getSystemColors(), 1, 1, width - 3, height - 3, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            helper.paintAllItems(g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                                 colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                                 getBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        public void setVisible(boolean vis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            xSetVisible(vis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            if (!vis && alignUnder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                alignUnder.requestFocusInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         * Return a MouseEvent's Point in coordinates relative to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * UnfurledChoice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        private Point toLocalCoords(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            // Event coords are relative to the button, so translate a bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            Point global = e.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            global.x -= x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            global.y -= y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            return global;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        /* Returns true if the MouseEvent coords (which are based on the Choice)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
         * are inside of the UnfurledChoice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        private boolean isMouseEventInside(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            Point local = toLocalCoords(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            if (local.x > 0 && local.x < width &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                local.y > 0 && local.y < height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            return false;
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
         * Tests if the mouse cursor is in the Unfurled Choice, yet not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         * in the vertical scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        private boolean isMouseInListArea(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            if (isMouseEventInside(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                Point local = toLocalCoords(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                Rectangle bounds = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                if (!helper.isInVertSB(bounds, local.x, local.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         * Overridden from XWindow() because we don't want to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
         * ComponentEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        public void handleConfigureNotifyEvent(XEvent xev) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        public void handleMapNotifyEvent(XEvent xev) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        public void handleUnmapNotifyEvent(XEvent xev) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    } //UnfurledChoice
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        if (unfurledChoice != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            unfurledChoice.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        super.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * fix for 6239938 : Choice drop-down does not disappear when it loses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * focus, on XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * We are able to handle all _Key_ events received by Choice when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * it is in opened state without sending it to EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * If Choice is in closed state we should behave like before: send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * all events to EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * To be compatible with Motif we should handle all KeyEvents in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * Choice if it is opened. KeyEvents should be sent into Java if Choice is not opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    boolean prePostEvent(final AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (unfurled){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            // fix for 6253211: PIT: MouseWheel events not triggered for Choice drop down in XAWT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            if (e instanceof MouseWheelEvent){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                return super.prePostEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            //fix 6252982: PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            if (e instanceof KeyEvent){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                // notify XWindow that this event had been already handled and no need to post it again
18520
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1036
                InvocationEvent ev = new InvocationEvent(target, new Runnable() {
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1037
                    public void run() {
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1038
                        if(target.isFocusable() &&
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1039
                                getParentTopLevel().isFocusableWindow() )
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1040
                        {
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1041
                            handleJavaKeyEvent((KeyEvent)e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                        }
18520
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1043
                    }
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1044
                });
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1045
                postEvent(ev);
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1046
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                if (e instanceof MouseEvent){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    // Fix for 6240046 : REG:Choice's Drop-down does not disappear when clicking somewhere, after popup menu is disposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                    // if user presses Right Mouse Button on opened (unfurled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                    // Choice then we mustn't open a popup menu. We could filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    // Mouse Events and handle them in XChoicePeer if Choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    // currently in opened state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    MouseEvent me = (MouseEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    int eventId = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    // fix 6251983: PIT: MouseDragged events not triggered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    // fix 6251988: PIT: Choice consumes MouseReleased, MouseClicked events when clicking it with left button,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    if ((unfurledChoice.isMouseEventInside(me) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                         (!firstPress && eventId == MouseEvent.MOUSE_DRAGGED)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                        return handleMouseEventByChoice(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    // MouseMoved events should be fired in Choice's comp if it's not opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                    // Shouldn't generate Moved Events. CR : 6251995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    if (eventId == MouseEvent.MOUSE_MOVED){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        return handleMouseEventByChoice(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                    //fix for 6272965: PIT: Choice triggers MousePressed when pressing mouse outside comp while drop-down is active, XTkt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    if (  !firstPress && !( isMouseEventInChoice(me) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                             unfurledChoice.isMouseEventInside(me)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                             ( eventId == MouseEvent.MOUSE_PRESSED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                               eventId == MouseEvent.MOUSE_RELEASED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                               eventId == MouseEvent.MOUSE_CLICKED )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                          )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        return handleMouseEventByChoice(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            }//else KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }//if unfurled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        return super.prePostEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    //convenient method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    //do not generate this kind of Events
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    public boolean handleMouseEventByChoice(final MouseEvent me){
18520
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1088
        InvocationEvent ev = new InvocationEvent(target, new Runnable() {
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1089
            public void run() {
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1090
                handleJavaMouseEvent(me);
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1091
            }
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1092
        });
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1093
        postEvent(ev);
7fad3f87d731 8014264: The applet pathguy_TimeDead throws java.lang.NullPointerException in java console once click drop-down check box.
leonidr
parents: 14330
diff changeset
  1094
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /* Returns true if the MouseEvent coords
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * are inside of the Choice itself (it doesnt's depends on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * if this choice opened or not).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    private boolean isMouseEventInChoice(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        int x = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        int y = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        Rectangle choiceRect = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (x < 0 || x > choiceRect.width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            y < 0 || y > choiceRect.height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
}