jdk/src/solaris/classes/sun/awt/X11/XListPeer.java
author mchung
Tue, 29 Sep 2009 16:03:03 -0700
changeset 3938 ef327bd847c0
parent 445 6f717a8cacfb
child 5506 202f599c92aa
permissions -rw-r--r--
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes Summary: Replace calls to Logger with sun.util.logging.PlatformLogger Reviewed-by: prr, art, alexp, dcherepanov, igor, dav, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
// Very much based on XListPeer from javaos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.peer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.geom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.*;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
    37
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
// TODO: some input actions should do nothing if Shift or Control are down
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class XListPeer extends XComponentPeer implements ListPeer, XScrollbarClient {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
    43
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XListPeer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public final static int     MARGIN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public final static int     SPACE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public final static int     SCROLLBAR_AREA = 17;  // Area reserved for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                                      // scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public final static int     SCROLLBAR_WIDTH = 13; // Actual width of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                                      // scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public final static int     NONE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public final static int     WINDOW = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public final static int     VERSCROLLBAR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public final static int     HORSCROLLBAR = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public final static int     DEFAULT_VISIBLE_ROWS = 4; // From java.awt.List,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public final static int     HORIZ_SCROLL_AMT = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    58
    private final static int    PAINT_VSCROLL = 2;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    59
    private final static int    PAINT_HSCROLL = 4;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    60
    private final static int    PAINT_ITEMS = 8;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    61
    private final static int    PAINT_FOCUS = 16;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    62
    private final static int    PAINT_BACKGROUND = 32;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    63
    private final static int    PAINT_HIDEFOCUS = 64;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    64
    private final static int    PAINT_ALL =
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    65
        PAINT_VSCROLL | PAINT_HSCROLL | PAINT_ITEMS | PAINT_FOCUS | PAINT_BACKGROUND;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
    66
    private final static int    COPY_AREA = 128;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    XVerticalScrollbar       vsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    XHorizontalScrollbar     hsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    ListPainter painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // TODO: ick - Vector?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    Vector                      items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    boolean                     multipleSelections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int                         active = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // Holds the array of the indexes of the elements which is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // This array should be kept sorted, low to high.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    int                         selected[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int                         fontHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int                         fontAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int                         fontLeading;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Holds the index of the item used in the previous operation (selectItem, deselectItem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Adding of an item or clearing of the list sets this index to -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // The index is used at the moment of the post of ACTION_PERFORMED event after the mouse double click event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int                         currentIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // Used for tracking selection/deselection between mousePress/Release
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // and for ItemEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    int                         eventIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    int                         eventType = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // Holds the index of the item that receive focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // This variable is reasonable only for multiple list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // since 'focusIndex' and 'selected[0]' are equal for single-selection list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    int                         focusIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    int                         maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    boolean                     vsbVis;  // visibility of scrollbars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    boolean                     hsbVis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int                         listWidth;  // Width of list portion of List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int                         listHeight; // Height of list portion of List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // (i.e. without scrollbars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private int firstTimeVisibleIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // Motif Lists don't seem to inherit the background color from their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // parent when an app is first started up.  So, we track if the colors have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // been set.  See getListBackground()/getListForeground().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    boolean bgColorSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    boolean fgColorSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // Holds the true if mouse is dragging outside of the area of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // The flag is used at the moment of the dragging and releasing mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    boolean mouseDraggedOutHorizontally = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    boolean mouseDraggedOutVertically = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // Holds the true if a mouse event was originated on the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // See 6300527 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    boolean isScrollBarOriginated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // This variable is set to true after the "mouse pressed" event and to false after the "mouse released" event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // Fixed 6293432: Key events ('SPACE', 'UP', 'DOWN') aren't blocked if mouse is kept in 'PRESSED' state for List, XAWT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    boolean isMousePressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Create a list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    XListPeer(List target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        super(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Overridden from XWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void preInit(XCreateWindowParams params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        super.preInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // Stuff that must be initialized before layout() is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        items = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        createVerScrollbar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        createHorScrollbar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        painter = new ListPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // See 6246467 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        bgColorSet = target.isBackgroundSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        fgColorSet = target.isForegroundSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void postInit(XCreateWindowParams params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        super.postInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        initFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // TODO: more efficient way?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        //       do we really want/need a copy of all the items?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // get all items from target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        List l = (List)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int stop = l.getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (int i = 0 ; i < stop; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            items.addElement(l.getItem(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /* make the visible position visible. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int index = l.getVisibleIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // Can't call makeVisible since it check scroll bar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // initialize scroll bar instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            vsb.setValues(index, 0, 0, items.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // NOTE: needs to have target set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        maxLength = maxLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // get the index containing all indexes to selected items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        int sel[] = l.getSelectedIndexes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        selected = new int[sel.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // TODO: shouldn't this be arraycopy()?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        for (int i = 0 ; i < sel.length ; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            selected[i] = sel[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // The select()ed item should become the focused item, but we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // get the select() call because the peer generally hasn't yet been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // created during app initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // TODO: For multi-select lists, it should be the highest selected index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (sel.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            setFocusIndex(sel[sel.length - 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            setFocusIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        multipleSelections = l.isMultipleMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * add Vertical Scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    void createVerScrollbar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        vsb = new XVerticalScrollbar(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        vsb.setValues(0, 0, 0, 0, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * add Horizontal scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    void createHorScrollbar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        hsb = new XHorizontalScrollbar(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        hsb.setValues(0, 0, 0, 0, HORIZ_SCROLL_AMT, HORIZ_SCROLL_AMT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /* New method name for 1.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void add(String item, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        addItem(item, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /* New method name for 1.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void removeAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /* New method name for 1.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void setMultipleMode (boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        setMultipleSelections(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /* New method name for 1.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public Dimension getPreferredSize(int rows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return preferredSize(rows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /* New method name for 1.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public Dimension getMinimumSize(int rows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return minimumSize(rows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public Dimension minimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return minimumSize(DEFAULT_VISIBLE_ROWS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * return the preferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public Dimension preferredSize(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return minimumSize(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * return the minimumsize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public Dimension minimumSize(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        FontMetrics fm = getFontMetrics(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        initFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return new Dimension(20 + fm.stringWidth("0123456789abcde"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                             getItemHeight() * v + (2*MARGIN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Calculate font metrics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    void initFontMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        FontMetrics fm = getFontMetrics(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        fontHeight = fm.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        fontAscent = fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        fontLeading = fm.getLeading();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * return the length of the largest item in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    int maxLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        FontMetrics fm = getFontMetrics(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        int m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        int end = items.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        for(int i = 0 ; i < end ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            int l = fm.stringWidth(((String)items.elementAt(i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            m = Math.max(m, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Calculates the width of item's label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    int getItemWidth(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        FontMetrics fm = getFontMetrics(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return fm.stringWidth((String)items.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * return the on-screen width of the given string "str"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    int stringLength(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        FontMetrics fm = getFontMetrics(target.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return fm.stringWidth(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public void setForeground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        fgColorSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        super.setForeground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public void setBackground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        bgColorSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        super.setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Returns the color that should be used to paint the background of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * the list of items.  Note that this is not the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * target.getBackground() which is the color of the scrollbars, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * lower-right corner of the Component when the scrollbars are displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private Color getListBackground(Color[] colors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (bgColorSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return colors[BACKGROUND_COLOR];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return SystemColor.text;
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
     * Returns the color that should be used to paint the list item text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private Color getListForeground(Color[] colors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (fgColorSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return colors[FOREGROUND_COLOR];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return SystemColor.textText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    Rectangle getVScrollBarRec() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return new Rectangle(width - (SCROLLBAR_WIDTH), 0, SCROLLBAR_WIDTH+1, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    Rectangle getHScrollBarRec() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return new Rectangle(0, height - SCROLLBAR_WIDTH, width, SCROLLBAR_WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    int getFirstVisibleItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    int getLastVisibleItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return Math.min(items.size()-1, vsb.getValue() + itemsInWindow() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return Math.min(items.size()-1, itemsInWindow()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public void repaintScrollbarRequest(XScrollbar scrollbar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (scrollbar == hsb)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            repaint(PAINT_HSCROLL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        else if (scrollbar == vsb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            repaint(PAINT_VSCROLL);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Overridden for performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public void repaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        repaint(getFirstVisibleItem(), getLastVisibleItem(), PAINT_ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   386
    private void repaint(int options) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        repaint(getFirstVisibleItem(), getLastVisibleItem(), options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   390
    private void repaint(int firstItem, int lastItem, int options) {
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   391
        repaint(firstItem, lastItem, options, null, null);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   392
    }
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   393
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   394
    /**
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   395
     * In most cases the entire area of the component doesn't have
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   396
     * to be repainted. The method repaints the particular areas of
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   397
     * the component. The areas to repaint is specified by the option
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   398
     * parameter. The possible values of the option parameter are:
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   399
     * PAINT_VSCROLL, PAINT_HSCROLL, PAINT_ITEMS, PAINT_FOCUS,
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   400
     * PAINT_HIDEFOCUS, PAINT_BACKGROUND, PAINT_ALL, COPY_AREA.
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   401
     *
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   402
     * Note that the COPY_AREA value initiates copy of a source area
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   403
     * of the component by a distance by means of the copyArea method
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   404
     * of the Graphics class.
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   405
     *
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   406
     * @param firstItem the position of the first item of the range to repaint
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   407
     * @param lastItem the position of the last item of the range to repaint
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   408
     * @param options specifies the particular area of the component to repaint
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   409
     * @param source the area of the component to copy
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   410
     * @param distance the distance to copy the source area
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   411
     */
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   412
    private void repaint(int firstItem, int lastItem, int options, Rectangle source, Point distance) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        try {
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
   415
            painter.paint(g, firstItem, lastItem, options, source, distance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        painter.paint(g, getFirstVisibleItem(), getLastVisibleItem(), PAINT_ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public boolean isFocusable() { return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    // TODO: share/promote the Focus methods?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        super.focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        repaint(PAINT_FOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        super.focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        repaint(PAINT_FOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Layout the sub-components of the List - that is, the scrollbars and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * list of items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public void layout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        int vis, maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        boolean vsbWasVisible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int origVSBVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        assert(target != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // Start with assumption there is not a horizontal scrollbar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // see if we need a vertical scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        // Bug: If the list DOES have a horiz scrollbar and the value is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // the very bottom value, value is reset in setValues() because it isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // a valid value for cases when the list DOESN'T have a horiz scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // This is currently worked-around with origVSGVal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        origVSBVal = vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        vis = itemsInWindow(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        maximum = items.size() < vis ? vis : items.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        vsb.setValues(vsb.getValue(), vis, vsb.getMinimum(), maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        vsbVis = vsbWasVisible = vsbIsVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        listHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // now see if we need a horizontal scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        listWidth = getListWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        vis = listWidth - ((2 * SPACE) + (2 * MARGIN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        maximum = maxLength < vis ? vis : maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        hsb.setValues(hsb.getValue(), vis, hsb.getMinimum(), maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        hsbVis = hsbIsVisible(vsbVis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (hsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // do need a horizontal scrollbar, so recalculate height of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            // vertical s crollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            listHeight = height - SCROLLBAR_AREA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            vis = itemsInWindow(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            maximum = items.size() < vis ? vis : items.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            vsb.setValues(origVSBVal, vis, vsb.getMinimum(), maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            vsbVis = vsbIsVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // now check to make sure we haven't changed need for vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // scrollbar - if we have, we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        // recalculate horizontal scrollbar width - then we're done...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (vsbWasVisible != vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            listWidth = getListWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            vis = listWidth - ((2 * SPACE) + (2 * MARGIN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            maximum = maxLength < vis ? 0 : maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            hsb.setValues(hsb.getValue(), vis, hsb.getMinimum(), maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            hsbVis = hsbIsVisible(vsbVis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        vsb.setSize(SCROLLBAR_WIDTH, listHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        hsb.setSize(listWidth, SCROLLBAR_WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        vsb.setBlockIncrement(itemsInWindow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        hsb.setBlockIncrement(width - ((2 * SPACE) + (2 * MARGIN) + (vsbVis ? SCROLLBAR_AREA : 0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    int getItemWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return width - ((2 * MARGIN) + (vsbVis ? SCROLLBAR_AREA : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /* Returns height of an item in the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    int getItemHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return (fontHeight - fontLeading) + (2*SPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    int getItemX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        return MARGIN + SPACE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    int getItemY(int item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return index2y(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    int getFocusIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return focusIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    void setFocusIndex(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        focusIndex = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Update and return the focus rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Focus is around the focused item, if it is visible, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * around the border of the list if the focused item is scrolled off the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * or bottom of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    Rectangle getFocusRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        Rectangle focusRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // width is always only based on presence of vert sb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        focusRect.x = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        focusRect.width = getListWidth() - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        // if focused item is not currently displayed in the list,  paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // focus around entire list (not including scrollbars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (isIndexDisplayed(getFocusIndex())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            // focus rect is around the item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            focusRect.y = index2y(getFocusIndex()) - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            focusRect.height = getItemHeight()+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            // focus rect is around the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            focusRect.y = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            focusRect.height = hsbVis ? height - SCROLLBAR_AREA : height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            focusRect.height -= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return focusRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public void handleConfigureNotifyEvent(XEvent xev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        super.handleConfigureNotifyEvent(xev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        // Update buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        painter.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    public boolean handlesWheelScrolling() { return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    // FIXME: need to support MouseWheel scrolling, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    void handleJavaMouseEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        super.handleJavaMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        int i = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
          case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
              mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
          case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
              mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
          case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
              mouseDragged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    void handleJavaMouseWheelEvent(MouseWheelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (ListHelper.doWheelScroll(vsbVis ? vsb : null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                     hsbVis ? hsb : null, e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    void mousePressed(MouseEvent mouseEvent) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
   581
        if (log.isLoggable(PlatformLogger.FINER)) log.finer(mouseEvent.toString() + ", hsb " + hsbVis + ", vsb " + vsbVis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (isEnabled() && mouseEvent.getButton() == MouseEvent.BUTTON1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            if (inWindow(mouseEvent.getX(), mouseEvent.getY())) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
   584
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("Mouse press in items area");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                active = WINDOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                int i = y2index(mouseEvent.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    if (multipleSelections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        if (isSelected(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                            // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                            deselectItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                            eventIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                            eventType = ItemEvent.DESELECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                            selectItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                            eventIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            eventType = ItemEvent.SELECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    // Backward-compatible bug: even if a single-select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    // item is already selected, we send an ITEM_STATE_CHANGED/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    // SELECTED event.  Engineer's Toolbox appears to rely on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    // this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    //else if (!isSelected(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        selectItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        eventIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        eventType = ItemEvent.SELECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    // Restoring Windows behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    // We should update focus index after "mouse pressed" event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    setFocusIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    repaint(PAINT_FOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    // 6426186: reset variable to prevent action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    // if user clicks on unoccupied area of list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    currentIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            } else if (inVerticalScrollbar(mouseEvent.getX(), mouseEvent.getY())) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
   621
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("Mouse press in vertical scrollbar");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                active = VERSCROLLBAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                vsb.handleMouseEvent(mouseEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                     mouseEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                     mouseEvent.getX() - (width - SCROLLBAR_WIDTH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                     mouseEvent.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            } else if (inHorizontalScrollbar(mouseEvent.getX(), mouseEvent.getY())) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
   628
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("Mouse press in horizontal scrollbar");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                active = HORSCROLLBAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                hsb.handleMouseEvent(mouseEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                     mouseEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                     mouseEvent.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                                     mouseEvent.getY() - (height - SCROLLBAR_WIDTH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            isMousePressed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    void mouseReleased(MouseEvent mouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (isEnabled() && mouseEvent.getButton() == MouseEvent.BUTTON1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            //winReleaseCursorFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            int clickCount = mouseEvent.getClickCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (active == VERSCROLLBAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                vsb.handleMouseEvent(mouseEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                     mouseEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                     mouseEvent.getX()-(width-SCROLLBAR_WIDTH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                     mouseEvent.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            } else if(active == HORSCROLLBAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                hsb.handleMouseEvent(mouseEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                                     mouseEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                     mouseEvent.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                     mouseEvent.getY()-(height-SCROLLBAR_WIDTH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            } else if ( ( currentIndex >= 0 ) && ( clickCount >= 2 ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        ( clickCount % 2 == 0 ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                postEvent(new ActionEvent(target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                          ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                          (String)items.elementAt(currentIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                          mouseEvent.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                          mouseEvent.getModifiers()));  // No ext mods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            } else if (active == WINDOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                trackMouseReleasedScroll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                if (eventType == ItemEvent.DESELECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    assert multipleSelections : "Shouldn't get a deselect for a single-select List";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    // Paint deselection the release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    deselectItem(eventIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                if (eventType != NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                                ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                                Integer.valueOf(eventIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                eventType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            active = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            eventIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            eventType = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            isMousePressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    void mouseDragged(MouseEvent mouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        // TODO: can you drag w/ any other buttons?  what about multiple buttons?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (isEnabled() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            (mouseEvent.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            if ((active == VERSCROLLBAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                vsb.handleMouseEvent(mouseEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                     mouseEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                                     mouseEvent.getX()-(width-SCROLLBAR_WIDTH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                     mouseEvent.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            } else if ((active == HORSCROLLBAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                hsb.handleMouseEvent(mouseEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                                     mouseEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                     mouseEvent.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                                     mouseEvent.getY()-(height-SCROLLBAR_WIDTH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            } else if (active == WINDOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                int i = y2index(mouseEvent.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                if (multipleSelections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    // Multi-select only:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    // If a selected item was pressed on and then dragged off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    // of, cancel the pending deselect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    if (eventType == ItemEvent.DESELECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        if (i != eventIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            eventType = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                            eventIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                else if (eventType == ItemEvent.SELECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    // Single-select only:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    // If an unselected item was pressed on, track the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    // and select the item under the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    trackMouseDraggedScroll(mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    if (i >= 0 && !isSelected(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        int oldSel = eventIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                        selectItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        eventIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        repaint(oldSel, eventIndex, PAINT_ITEMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                // Restoring Windows behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                // We should update focus index after "mouse dragged" event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    setFocusIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    repaint(PAINT_FOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Helper method for XListPeer with integrated vertical scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Start or stop vertical scrolling when mouse dragged in / out the area of the list if it's required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Restoring Motif behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    void trackMouseDraggedScroll(MouseEvent mouseEvent){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        if (vsb.beforeThumb(mouseEvent.getX(), mouseEvent.getY())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            vsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            vsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if(mouseEvent.getY() < 0 || mouseEvent.getY() >= listHeight){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            if (!mouseDraggedOutVertically){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                mouseDraggedOutVertically = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                vsb.startScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (mouseDraggedOutVertically){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                mouseDraggedOutVertically = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                vsb.stopScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (hsb.beforeThumb(mouseEvent.getX(), mouseEvent.getY())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            hsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            hsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (mouseEvent.getX() < 0 || mouseEvent.getX() >= listWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            if (!mouseDraggedOutHorizontally){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                mouseDraggedOutHorizontally = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                hsb.startScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            if (mouseDraggedOutHorizontally){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                mouseDraggedOutHorizontally = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                hsb.stopScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * Helper method for XListPeer with integrated vertical scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Stop vertical scrolling when mouse released in / out the area of the list if it's required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * Restoring Motif behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * see 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    void trackMouseReleasedScroll(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (mouseDraggedOutVertically){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            mouseDraggedOutVertically = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            vsb.stopScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (mouseDraggedOutHorizontally){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            mouseDraggedOutHorizontally = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            hsb.stopScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    void handleJavaKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        switch(e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
          case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
              if (!isMousePressed){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                  keyPressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    void keyPressed(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        int keyCode = e.getKeyCode();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
   811
        if (log.isLoggable(PlatformLogger.FINE)) log.fine(e.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        switch(keyCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
          case KeyEvent.VK_UP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
          case KeyEvent.VK_KP_UP: // TODO: I assume we also want this, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
              if (getFocusIndex() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                  setFocusIndex(getFocusIndex()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                  repaint(PAINT_HIDEFOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                  // If single-select, select the item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                  if (!multipleSelections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                      selectItem(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                      postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                              ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                                              Integer.valueOf(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                                              ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                  if (isItemHidden(getFocusIndex())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                      makeVisible(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                      repaint(PAINT_FOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
          case KeyEvent.VK_DOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
          case KeyEvent.VK_KP_DOWN: // TODO: I assume we also want this, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
              if (getFocusIndex() < items.size() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                  setFocusIndex(getFocusIndex()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                  repaint(PAINT_HIDEFOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                  // If single-select, select the item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                  if (!multipleSelections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                      selectItem(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                      postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                                              ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                              Integer.valueOf(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                              ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                  if (isItemHidden(getFocusIndex())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                      makeVisible(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                      repaint(PAINT_FOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
          case KeyEvent.VK_PAGE_UP: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
              // Assumes that scrollbar does its own bounds-checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
              int previousValue = vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
              vsb.setValue(vsb.getValue() - vsb.getBlockIncrement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
              int currentValue = vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
              // 6190768 pressing pg-up on AWT multiple selection lists the items but no item event is triggered, on XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
              // Restoring Motif behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
              if (previousValue!=currentValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                  setFocusIndex(Math.max(getFocusIndex()-itemsInWindow(), 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                  if (!multipleSelections){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                      selectItem(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                      postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                              ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                                              Integer.valueOf(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                                              ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
              repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
          case KeyEvent.VK_PAGE_DOWN: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
              // Assumes that scrollbar does its own bounds-checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
              int previousValue = vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
              vsb.setValue(vsb.getValue() + vsb.getBlockIncrement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
              int currentValue = vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
              // 6190768 pressing pg-down on AWT multiple selection list selects the items but no item event is triggered, on XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
              // Restoring Motif behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
              if (previousValue!=currentValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                  setFocusIndex(Math.min(getFocusIndex() + itemsInWindow(), items.size()-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                  if (!multipleSelections){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                      selectItem(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                      postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                                              ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                                              Integer.valueOf(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                                              ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
              repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
          case KeyEvent.VK_LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
          case KeyEvent.VK_KP_LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
              if (hsbVis & hsb.getValue() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                  hsb.setValue(hsb.getValue() - HORIZ_SCROLL_AMT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                  repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
          case KeyEvent.VK_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
          case KeyEvent.VK_KP_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
              if (hsbVis) { // Should check if already at end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                  hsb.setValue(hsb.getValue() + HORIZ_SCROLL_AMT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                  repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
          // 6190778 CTRL + HOME, CTRL + END keys do not work properly for list on XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
          // Restoring Motif behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
          case KeyEvent.VK_HOME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
              if (!e.isControlDown() || ((List)target).getItemCount() <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
              if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                  vsb.setValue(vsb.getMinimum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
              setFocusIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
              if (!multipleSelections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                  selectItem(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                  postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                                          ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                                          Integer.valueOf(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                                          ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
              repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
          case KeyEvent.VK_END:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
              if (!e.isControlDown() || ((List)target).getItemCount() <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
              if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                  vsb.setValue(vsb.getMaximum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
              setFocusIndex(items.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
              if (!multipleSelections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                  selectItem(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                  postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                          ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                                          Integer.valueOf(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                                          ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
              repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
          case KeyEvent.VK_SPACE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
              // Fixed 6299853: XToolkit: Pressing space triggers ItemStateChanged event after List.removeAll called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
              // If getFocusIndex() is less than 0, the event will not be triggered when space pressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
              if (getFocusIndex() < 0 || ((List)target).getItemCount() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
              boolean isSelected = isSelected(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
              // Spacebar only deselects for multi-select Lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
              if (multipleSelections && isSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                  deselectItem(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                  postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                                          ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                                          Integer.valueOf(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                                          ItemEvent.DESELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
              else if (!isSelected) { // Note: this changes the Solaris/Linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                  // behavior to match that of win32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                  // That is, pressing space bar on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                  // single-select list when the focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                  // item is already selected does NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                  // send an ItemEvent.SELECTED event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                  selectItem(getFocusIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                  postEvent(new ItemEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                                          ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                                          Integer.valueOf(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                                          ItemEvent.SELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
          case KeyEvent.VK_ENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
              // It looks to me like there are bugs as well as inconsistencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
              // in the way the Enter key is handled by both Solaris and Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
              // So for now in XAWT, I'm going to simply go by what the List docs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
              // say: "AWT also generates an action event when the user presses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
              // the return key while an item in the list is selected."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
              if (selected.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                  postEvent(new ActionEvent((List)target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                                            ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                                            (String)items.elementAt(getFocusIndex()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                                            e.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                                            e.getModifiers()));  // ActionEvent doesn't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                  // extended modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * return value from the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
   996
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("Notify value changed on " + obj + " to " + v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        int value = obj.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        if (obj == vsb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            scrollVertical(v - value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            int oldSel = eventIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            int newSel = eventIndex+v-value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            if (mouseDraggedOutVertically && !isSelected(newSel)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                selectItem(newSel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                eventIndex = newSel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                repaint(oldSel, eventIndex, PAINT_ITEMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                // Scrolling select() should also set the focus index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                // Otherwise, the updating of the 'focusIndex' variable will be incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                // if user drag mouse out of the area of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                setFocusIndex(newSel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                repaint(PAINT_FOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        } else if ((XHorizontalScrollbar)obj == hsb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            scrollHorizontal(v - value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * deselect all items in List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    private void deselectAllItems() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        selected = new int [0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        repaint(PAINT_ITEMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * set multiple selections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    public void setMultipleSelections(boolean v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        if (multipleSelections != v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            if ( !v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                int selPos = ( isSelected( focusIndex )) ? focusIndex: -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                deselectAllItems();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                if (selPos != -1){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                    selectItem(selPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            multipleSelections = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * add an item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * if the index of the item is < 0 or >= than items.size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * then add the item to the end of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    public void addItem(String item, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        int oldMaxLength = maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        boolean hsbWasVis = hsbVis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        boolean vsbWasVis = vsbVis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        int addedIndex = 0; // Index where the new item ended up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if (i < 0 || i >= items.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            i = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        // Why we set this variable to -1 in spite of the fact that selected[] is changed in other way?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        // It's not clear how to reproduce incorrect behaviour based on this assignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        // since before using this variable (mouseReleased) we certainly update it to correct value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        // So we don't modify this behaviour now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        currentIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        if (i == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            items.addElement(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            i = 0;              // fix the math for the paintItems test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            addedIndex = items.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            items.insertElementAt(item, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            addedIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            for (int j = 0 ; j < selected.length ; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                if (selected[j] >= i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    selected[j] += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1079
        if (log.isLoggable(PlatformLogger.FINER)) log.finer("Adding item '" + item + "' to " + addedIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        // Update maxLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        boolean repaintItems = !isItemHidden(addedIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        maxLength = Math.max(maxLength, getItemWidth(addedIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        int options = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (vsbVis != vsbWasVis || hsbVis != hsbWasVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            // Scrollbars are being added or removed, so we must repaint all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            options = PAINT_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            options = (repaintItems ? (PAINT_ITEMS):0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                | ((maxLength != oldMaxLength || (hsbWasVis ^ hsbVis))?(PAINT_HSCROLL):0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                | ((vsb.needsRepaint())?(PAINT_VSCROLL):0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1097
        if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Last visible: " + getLastVisibleItem() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                                                     ", hsb changed : " + (hsbWasVis ^ hsbVis) + ", items changed " + repaintItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        repaint(addedIndex, getLastVisibleItem(), options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * delete items starting with s (start position) to e (end position) including s and e
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * if s < 0 then s = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * if e >= items.size() then e = items.size() - 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    public void delItems(int s, int e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        // save the current state of the scrollbars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        boolean hsbWasVisible = hsbVis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        boolean vsbWasVisible = vsbVis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        int oldLastDisplayed = lastItemDisplayed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1113
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("Deleting from " + s + " to " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1115
        if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Last displayed item: " + oldLastDisplayed + ", items in window " + itemsInWindow() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                                                     ", size " + items.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if (items.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        // if user passed in flipped args, reverse them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        if (s > e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            int tmp = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            s = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            e = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        // check for starting point less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if (s < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            s = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        // check for end point greater than the size of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        if (e >= items.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            e = items.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        // determine whether we're going to delete any visible elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        // repaint must also be done if scrollbars appear/disappear, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        // can happen from removing a non-showing list item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
          boolean repaintNeeded =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
          ((s <= lastItemDisplayed()) && (e >= vsb.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        boolean repaintNeeded = (s >= getFirstVisibleItem() && s <= getLastVisibleItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        // delete the items out of the items list and out of the selected list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        for (int i = s ; i <= e ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            items.removeElementAt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            int j = posInSel(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            if (j != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                int newsel[] = new int[selected.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                System.arraycopy(selected, 0, newsel, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                System.arraycopy(selected, j + 1, newsel, j, selected.length - (j + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                selected = newsel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        // update the indexes in the selected array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        int diff = (e - s) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        for (int i = 0 ; i < selected.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            if (selected[i] > e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                selected[i] -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        int options = PAINT_VSCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        // focusedIndex updating according to native (Window, Motif) behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if (getFocusIndex() > e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            setFocusIndex(getFocusIndex() - (e - s + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            options |= PAINT_FOCUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        } else if (getFocusIndex() >= s && getFocusIndex() <= e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            // Fixed 6299858: PIT. Focused border not shown on List if selected item is removed, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            // We should set focus to new first item if the current first item was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            // except if the list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            int focusBound = (items.size() > 0) ? 0 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            setFocusIndex(Math.max(s-1, focusBound));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            options |= PAINT_FOCUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1183
        if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Multiple selections: " + multipleSelections);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        // update vsb.val
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        if (vsb.getValue() >= s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            if (vsb.getValue() <= e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                vsb.setValue(e+1 - diff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                vsb.setValue(vsb.getValue() - diff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        int oldMaxLength = maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        maxLength = maxLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        if (maxLength != oldMaxLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            // Width of the items changed affecting the range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            // horizontal scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            options |= PAINT_HSCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        repaintNeeded |= (vsbWasVisible ^ vsbVis) || (hsbWasVisible ^ hsbVis); // If scrollbars visibility changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        if (repaintNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            options |= PAINT_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        repaint(s, oldLastDisplayed, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * ListPeer method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    public void select(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        // Programmatic select() should also set the focus index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        setFocusIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        repaint(PAINT_FOCUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        selectItem(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * select the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * redraw the list to the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    void selectItem(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        // NOTE: instead of recalculating and the calling repaint(), painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        // is done immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        // 6190746 List does not trigger ActionEvent when double clicking a programmatically selected item, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        // If we invoke select(int) before setVisible(boolean), then variable currentIndex will equals -1. At the same time isSelected may be true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // Restoring Motif behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        currentIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if (isSelected(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        if (!multipleSelections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            if (selected.length == 0) { // No current selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                selected = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                selected[0] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                int oldSel = selected[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                selected[0] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                if (!isItemHidden(oldSel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                    // Only bother painting if item is visible (4895367)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                    repaint(oldSel, oldSel, PAINT_ITEMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            // insert "index" into the selection array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            int newsel[] = new int[selected.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            while (i < selected.length && index > selected[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                newsel[i] = selected[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            newsel[i] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            System.arraycopy(selected, i, newsel, i+1, selected.length - i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            selected = newsel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        if (!isItemHidden(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            // Only bother painting if item is visible (4895367)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            repaint(index, index, PAINT_ITEMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * ListPeer method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * focusedIndex isn't updated according to native (Window, Motif) behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    public void deselect(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        deselectItem(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * deselect the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * redraw the list to the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    void deselectItem(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        if (!isSelected(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        if (!multipleSelections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            // TODO: keep an int[0] and int[1] around and just use them instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            // creating new ones all the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            selected = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            int i = posInSel(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            int newsel[] = new int[selected.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            System.arraycopy(selected, 0, newsel, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            System.arraycopy(selected, i+1, newsel, i, selected.length - (i+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            selected = newsel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        currentIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        if (!isItemHidden(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            // Only bother repainting if item is visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            repaint(index, index, PAINT_ITEMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * ensure that the given index is visible, scrolling the List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * if necessary, or doing nothing if the item is already visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * The List must be repainted for changes to be visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    public void makeVisible(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        if (index < 0 || index >= items.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        if (isItemHidden(index)) {  // Do I really need to call this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            // If index is above the top, scroll up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            if (index < vsb.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                scrollVertical(index - vsb.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            // If index is below the bottom, scroll down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            else if (index > lastItemDisplayed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                int val = index - lastItemDisplayed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                scrollVertical(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * clear
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        selected = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        items = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        currentIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        // Fixed 6291736: ITEM_STATE_CHANGED triggered after List.removeAll(), XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        // We should update 'focusIndex' variable more carefully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        setFocusIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        vsb.setValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * return the selected indexes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    public int[] getSelectedIndexes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        return selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * return the y value of the given index "i".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * the y value represents the top of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * NOTE: index can be larger than items.size as long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * as it can fit the window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    int index2y(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        int h = getItemHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        //if (index < vsb.getValue() || index > vsb.getValue() + itemsInWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        return MARGIN + ((index - vsb.getValue()) * h) + SPACE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    /* return true if the y is a valid y coordinate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *  a VISIBLE list item, otherwise returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    boolean validY(int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        int shown = itemsDisplayed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        int lastY = shown * getItemHeight() + MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        if (shown == itemsInWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            lastY += MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        if (y < 0 || y >= lastY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * return the position of the index in the selected array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * if the index isn't in the array selected return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    int posInSel(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        for (int i = 0 ; i < selected.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            if (index == selected[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    boolean isIndexDisplayed(int idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        int lastDisplayed = lastItemDisplayed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        return idx <= lastDisplayed &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            idx >= Math.max(0, lastDisplayed - itemsInWindow() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * returns index of last item displayed in the List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    int lastItemDisplayed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        int n = itemsInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        return (Math.min(items.size() - 1, (vsb.getValue() + n) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * returns whether the given index is currently scrolled off the top or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * bottom of the List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    boolean isItemHidden(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        return index < vsb.getValue() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            index >= vsb.getValue() + itemsInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * returns the width of the list portion of the component (accounts for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * presence of vertical scrollbar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    int getListWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        return vsbVis ? width - SCROLLBAR_AREA : width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * returns number of  items actually displayed in the List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    int itemsDisplayed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        return (Math.min(items.size()-vsb.getValue(), itemsInWindow()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * scrollVertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * y is the number of items to scroll
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    void scrollVertical(int y) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1436
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("Scrolling vertically by " + y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        int itemsInWin = itemsInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        int h = getItemHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        int pixelsToScroll = y * h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        if (vsb.getValue() < -y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            y = -vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        vsb.setValue(vsb.getValue() + y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1446
        Rectangle source = null;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1447
        Point distance = null;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1448
        int firstItem = 0, lastItem = 0;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1449
        int options = PAINT_HIDEFOCUS | PAINT_ITEMS | PAINT_VSCROLL | PAINT_FOCUS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        if (y > 0) {
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1451
            if (y < itemsInWin) {
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1452
                source = new Rectangle(MARGIN, MARGIN + pixelsToScroll, width - SCROLLBAR_AREA, h * (itemsInWin - y - 1)-1);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1453
                distance = new Point(0, -pixelsToScroll);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1454
                options |= COPY_AREA;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            }
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1456
            firstItem = vsb.getValue() + itemsInWin - y - 1;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1457
            lastItem = vsb.getValue() + itemsInWin - 1;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1458
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1459
        } else if (y < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            if (y + itemsInWindow() > 0) {
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1461
                source = new Rectangle(MARGIN, MARGIN, width - SCROLLBAR_AREA, h * (itemsInWin + y));
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1462
                distance = new Point(0, -pixelsToScroll);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1463
                options |= COPY_AREA;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            }
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1465
            firstItem = vsb.getValue();
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1466
            lastItem = Math.min(getLastVisibleItem(), vsb.getValue() + -y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1468
        repaint(firstItem, lastItem, options, source, distance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * scrollHorizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * x is the number of pixels to scroll
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    void scrollHorizontal(int x) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1476
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("Scrolling horizontally by " + y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        int w = getListWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        w -= ((2 * SPACE) + (2 * MARGIN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        int h = height - (SCROLLBAR_AREA + (2 * MARGIN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        hsb.setValue(hsb.getValue() + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1482
        Rectangle source = null;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1483
        Point distance = null;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1484
        if (x < 0) {
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1485
            source = new Rectangle(MARGIN + SPACE, MARGIN, w + x, h);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1486
            distance = new Point(-x, 0);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1487
        } else if (x > 0) {
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1488
            source = new Rectangle(MARGIN + SPACE + x, MARGIN, w - x, h);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1489
            distance = new Point(-x, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        }
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1491
        int options = COPY_AREA | PAINT_ITEMS | PAINT_HSCROLL;
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1492
        repaint(vsb.getValue(), lastItemDisplayed(), options, source, distance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * return the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    int y2index(int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        if (!validY(y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        int i = (y - MARGIN) / getItemHeight() + vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        int last = lastItemDisplayed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        if (i > last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            i = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * is the index "index" selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    boolean isSelected(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        if (eventType == ItemEvent.SELECTED && index == eventIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        for (int i = 0 ; i < selected.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            if (selected[i] == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * return the number of items that can fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * in the current window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    int itemsInWindow(boolean scrollbarVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        int h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        if (scrollbarVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            h = height - ((2 * MARGIN) + SCROLLBAR_AREA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            h = height - 2*MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        return (h / getItemHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    int itemsInWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        return itemsInWindow(hsbVis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * return true if the x and y position is in the horizontal scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    boolean inHorizontalScrollbar(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        int w = getListWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        int h = height - SCROLLBAR_WIDTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        return (hsbVis &&  (x >= 0) && (x <= w) && (y > h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * return true if the x and y position is in the verticalscrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    boolean inVerticalScrollbar(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        int w = width - SCROLLBAR_WIDTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        int h = hsbVis ? height - SCROLLBAR_AREA : height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        return (vsbVis && (x > w) && (y >= 0) && (y <= h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * return true if the x and y position is in the window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    boolean inWindow(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        int w = getListWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        int h = hsbVis ? height - SCROLLBAR_AREA : height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        return ((x >= 0) && (x <= w)) && ((y >= 0) && (y <= h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * return true if vertical scrollbar is visible and false otherwise;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * hsbVisible is the visibility of the horizontal scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    boolean vsbIsVisible(boolean hsbVisible){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        return (items.size() > itemsInWindow(hsbVisible));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * return true if horizontal scrollbar is visible and false otherwise;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * vsbVisible is the visibility of the vertical scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    boolean hsbIsVisible(boolean vsbVisible){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        int w = width - ((2*SPACE) + (2*MARGIN) + (vsbVisible ? SCROLLBAR_AREA : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        return (maxLength > w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * Returns true if the event has been handled and should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * posted to Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    boolean prePostEvent(final AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        if (e instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            return prePostMouseEvent((MouseEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        return super.prePostEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * Fixed 6240151: XToolkit: Dragging the List scrollbar initiates DnD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * To be compatible with Motif, MouseEvent originated on the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * should be sent into Java in this way:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * - post: MOUSE_ENTERED, MOUSE_EXITED, MOUSE_MOVED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * - don't post: MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_CLICKED, MOUSE_DRAGGED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    boolean prePostMouseEvent(final MouseEvent me){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        if (getToplevelXWindow().isModalBlocked()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        int eventId = me.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        if (eventId == MouseEvent.MOUSE_MOVED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            // only for performance improvement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        }else if((eventId == MouseEvent.MOUSE_DRAGGED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                  eventId == MouseEvent.MOUSE_RELEASED) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                 isScrollBarOriginated)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            if (eventId == MouseEvent.MOUSE_RELEASED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                isScrollBarOriginated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            handleJavaMouseEventOnEDT(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }else if ((eventId == MouseEvent.MOUSE_PRESSED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                   eventId == MouseEvent.MOUSE_CLICKED) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                  (inVerticalScrollbar(me.getX(), me.getY()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                   inHorizontalScrollbar(me.getX(), me.getY())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            if (eventId == MouseEvent.MOUSE_PRESSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                isScrollBarOriginated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            handleJavaMouseEventOnEDT(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * Do handleJavaMouseEvent on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    void handleJavaMouseEventOnEDT(final MouseEvent me){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                    handleJavaMouseEvent(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * Fixed 5010944: List's rows overlap one another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * The bug is due to incorrent caching of the list item size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * So we should recalculate font metrics on setFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    public void setFont(Font f){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        super.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        initFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * Sometimes painter is called on Toolkit thread, so the lock sequence is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     *     awtLock -> Painter -> awtLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * Sometimes it is called on other threads:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     *     Painter -> awtLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * Since we can't guarantee the sequence, use awtLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    class ListPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        VolatileImage buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        Color[] colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        private Color getListForeground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            if (fgColorSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                return colors[FOREGROUND_COLOR];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            return SystemColor.textText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        private Color getListBackground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            if (bgColorSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                return colors[BACKGROUND_COLOR];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                return SystemColor.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        private Color getDisabledColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            Color backgroundColor = getListBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            Color foregroundColor = getListForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            return (backgroundColor.equals(Color.BLACK)) ? foregroundColor.darker() : backgroundColor.darker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        private boolean createBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            VolatileImage localBuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                localBuffer = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            if (localBuffer == null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1709
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("Creating buffer " + width + "x" + height);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                // use GraphicsConfig.cCVI() instead of Component.cVI(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                // because the latter may cause a deadlock with the tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                localBuffer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                    graphicsConfig.createCompatibleVolatileImage(width+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                                                                 height+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                if (buffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                    buffer = localBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        public void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                if (buffer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                    buffer.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        private void paint(Graphics listG, int firstItem, int lastItem, int options) {
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1741
            paint(listG, firstItem, lastItem, options, null, null);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1742
        }
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1743
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1744
        private void paint(Graphics listG, int firstItem, int lastItem, int options,
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1745
                           Rectangle source, Point distance) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1746
            if (log.isLoggable(PlatformLogger.FINER)) log.finer("Repaint from " + firstItem + " to " + lastItem + " options " + options);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            if (firstItem > lastItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                int t = lastItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                lastItem = firstItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                firstItem = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            if (firstItem < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                firstItem = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            colors = getGUIcolors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            VolatileImage localBuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                    if (createBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                        // First time created buffer should be painted over at full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                        options = PAINT_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                    localBuffer = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                switch (localBuffer.validate(getGraphicsConfiguration())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                  case VolatileImage.IMAGE_INCOMPATIBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                      invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                      options = PAINT_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                      continue;
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1773
                  case VolatileImage.IMAGE_RESTORED:
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1774
                      options = PAINT_ALL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                Graphics g = localBuffer.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1778
                // Note that the order of the following painting operations
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1779
                // should not be modified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                    g.setFont(getFont());
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1782
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1783
                    // hiding the focus rectangle must be done prior to copying
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1784
                    // area and so this is the first action to be performed
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1785
                    if ((options & (PAINT_HIDEFOCUS)) != 0) {
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1786
                        paintFocus(g, PAINT_HIDEFOCUS);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1787
                    }
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1788
                    /*
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1789
                     * The shift of the component contents occurs while someone
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1790
                     * scrolls the component, the only purpose of the shift is to
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1791
                     * increase the painting performance. The shift should be done
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1792
                     * prior to painting any area (except hiding focus) and actually
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1793
                     * it should never be done jointly with erase background.
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1794
                     */
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1795
                    if ((options & COPY_AREA) != 0) {
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1796
                        g.copyArea(source.x, source.y, source.width, source.height,
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1797
                            distance.x, distance.y);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1798
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                    if ((options & PAINT_BACKGROUND) != 0) {
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1800
                        paintBackground(g);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                        // Since we made full erase update items
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                        firstItem = getFirstVisibleItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                        lastItem = getLastVisibleItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                    if ((options & PAINT_ITEMS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                        paintItems(g, firstItem, lastItem, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                    if ((options & PAINT_VSCROLL) != 0 && vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                        g.setClip(getVScrollBarRec());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                        paintVerScrollbar(g, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                    if ((options & PAINT_HSCROLL) != 0 && hsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                        g.setClip(getHScrollBarRec());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                        paintHorScrollbar(g, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                    }
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1816
                    if ((options & (PAINT_FOCUS)) != 0) {
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1817
                        paintFocus(g, PAINT_FOCUS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                    g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            } while (localBuffer.contentsLost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            listG.drawImage(localBuffer, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
445
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1826
        private void paintBackground(Graphics g) {
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1827
            g.setColor(SystemColor.window);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1828
            g.fillRect(0, 0, width, height);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1829
            g.setColor(getListBackground());
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1830
            g.fillRect(0, 0, listWidth, listHeight);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1831
            draw3DRect(g, getSystemColors(), 0, 0, listWidth - 1, listHeight - 1, false);
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1832
        }
6f717a8cacfb 6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents: 2
diff changeset
  1833
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        private void paintItems(Graphics g, int firstItem, int lastItem, int options) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1835
            if (log.isLoggable(PlatformLogger.FINER)) log.finer("Painting items from " + firstItem + " to " + lastItem + ", focused " + focusIndex + ", first " + getFirstVisibleItem() + ", last " + getLastVisibleItem());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            firstItem = Math.max(getFirstVisibleItem(), firstItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            if (firstItem > lastItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                int t = lastItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                lastItem = firstItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                firstItem = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            firstItem = Math.max(getFirstVisibleItem(), firstItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            lastItem = Math.min(lastItem, items.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1846
            if (log.isLoggable(PlatformLogger.FINER)) log.finer("Actually painting items from " + firstItem + " to " + lastItem +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                                                       ", items in window " + itemsInWindow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            for (int i = firstItem; i <= lastItem; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                paintItem(g, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        private void paintItem(Graphics g, int index) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1854
            if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Painting item " + index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            // 4895367 - only paint items which are visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            if (!isItemHidden(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                Shape clip = g.getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                int w = getItemWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                int h = getItemHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                int y = getItemY(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                int x = getItemX();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1862
                if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Setting clip " + new Rectangle(x, y, w - (SPACE*2), h-(SPACE*2)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                g.setClip(x, y, w - (SPACE*2), h-(SPACE*2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                // Always paint the background so that focus is unpainted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                // multiselect mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                if (isSelected(index)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1868
                    if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Painted item is selected");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                    g.setColor(getListForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                    g.setColor(getListBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1873
                if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Filling " + new Rectangle(x, y, w, h));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                g.fillRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                if (index <= getLastVisibleItem() && index < items.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                    if (!isEnabled()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                        g.setColor(getDisabledColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    } else if (isSelected(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                        g.setColor(getListBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                        g.setColor(getListForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                    String str = (String)items.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    g.drawString(str, x - hsb.getValue(), y + fontAscent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                    // Clear the remaining area around the item - focus area and the rest of border
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                    g.setClip(x, y, listWidth, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                    g.setColor(getListBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                    g.fillRect(x, y, listWidth, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                g.setClip(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        void paintScrollBar(XScrollbar scr, Graphics g, int x, int y, int width, int height, boolean paintAll) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1897
            if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Painting scrollbar " + scr + " width " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                                                         width + " height " + height + ", paintAll " + paintAll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            scr.paint(g, getSystemColors(), paintAll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
         * Paint the horizontal scrollbar to the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
         * @param g the graphics context to draw into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
         * @param colors the colors used to draw the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
         * @param paintAll paint the whole scrollbar if true, just the thumb if false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        void paintHorScrollbar(Graphics g, boolean paintAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            int w = getListWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            paintScrollBar(hsb, g, 0, height - (SCROLLBAR_WIDTH), w, SCROLLBAR_WIDTH, paintAll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
         * Paint the vertical scrollbar to the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
         * @param g the graphics context to draw into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
         * @param colors the colors used to draw the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
         * @param paintAll paint the whole scrollbar if true, just the thumb if false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        void paintVerScrollbar(Graphics g, boolean paintAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            int h = height - (hsbVis ? (SCROLLBAR_AREA-2) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            paintScrollBar(vsb, g, width - SCROLLBAR_WIDTH, 0, SCROLLBAR_WIDTH - 2, h, paintAll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        private Rectangle prevFocusRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        private void paintFocus(Graphics g, int options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            boolean paintFocus = (options & PAINT_FOCUS) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            if (paintFocus && !hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                paintFocus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1935
            if (log.isLoggable(PlatformLogger.FINE)) log.fine("Painting focus, focus index " + getFocusIndex() + ", focus is " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                                                     (isItemHidden(getFocusIndex())?("invisible"):("visible")) + ", paint focus is " + paintFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            Shape clip = g.getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            g.setClip(0, 0, listWidth, listHeight);
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1939
            if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Setting focus clip " + new Rectangle(0, 0, listWidth, listHeight));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            Rectangle rect = getFocusRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            if (prevFocusRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                // Erase focus rect
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1943
                if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Erasing previous focus rect " + prevFocusRect);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                g.setColor(getListBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                g.drawRect(prevFocusRect.x, prevFocusRect.y, prevFocusRect.width, prevFocusRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                prevFocusRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            if (paintFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                // Paint new
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 445
diff changeset
  1950
                if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Painting focus rect " + rect);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                g.setColor(getListForeground());  // Focus color is always black on Linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                g.drawRect(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                prevFocusRect = rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            g.setClip(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
}