jdk/src/solaris/classes/sun/awt/X11/ListHelper.java
author ant
Fri, 22 Feb 2013 15:13:13 +0400
changeset 15983 26a673dec5b2
parent 11093 e753252dc8a9
child 18178 ee71c923891d
permissions -rw-r--r--
8006406: lightweight embedding in other Java UI toolkits Reviewed-by: serb, anthony, art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     2
 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.MouseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.MouseWheelEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.AdjustmentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Iterator;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    34
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
// FIXME: implement multi-select
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Class to paint a list of items, possibly with scrollbars
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class paints all items with the same font
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * For now, this class manages the list of items and painting thereof, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * posting of Item or ActionEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class ListHelper implements XScrollbarClient {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    44
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.ListHelper");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final int FOCUS_INSET = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final int BORDER_WIDTH; // Width of border drawn around the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                    // of items
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final int ITEM_MARGIN;  // Margin between the border of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                    // of items and and item's bg, and between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                    // items
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final int TEXT_SPACE;   // Space between the edge of an item and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                    // the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final int SCROLLBAR_WIDTH;  // Width of a scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private java.util.List items;        // List of items
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // TODO: maybe this would be better as a simple int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private java.util.List selected;     // List of selected items
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean multiSelect;         // Can multiple items be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                         // at once?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private int focusedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int maxVisItems;             // # items visible without a vsb
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private XVerticalScrollbar vsb;      // null if unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private boolean vsbVis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private XHorizontalScrollbar hsb;    // null if unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private boolean hsbVis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private FontMetrics fm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private XWindow peer;   // So far, only needed for painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                            // on notifyValue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private Color[] colors; // Passed in for painting on notifyValue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Holds the true if mouse is dragging outside of the area of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // The flag is used at the moment of the dragging and releasing mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    boolean mouseDraggedOutVertically = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private volatile boolean vsbVisibilityChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public ListHelper(XWindow peer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                      Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                      int initialSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                      boolean multiSelect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                      boolean scrollVert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                      boolean scrollHoriz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                      Font font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                      int maxVisItems,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                      int SPACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                      int MARGIN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                      int BORDER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                      int SCROLLBAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.peer = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.colors = colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.multiSelect = multiSelect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        items = new ArrayList(initialSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        selected = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        selected.add(Integer.valueOf(-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.maxVisItems = maxVisItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (scrollVert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            vsb = new XVerticalScrollbar(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            vsb.setValues(0, 0, 0, 0, 1, maxVisItems - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (scrollHoriz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            hsb = new XHorizontalScrollbar(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            hsb.setValues(0, 0, 0, 0, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        TEXT_SPACE = SPACE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        ITEM_MARGIN = MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        BORDER_WIDTH = BORDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        SCROLLBAR_WIDTH = SCROLLBAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Component getEventSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return peer.getEventSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**********************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /* List management methods                                            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**********************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void add(String item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        items.add(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        updateScrollbars();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public void add(String item, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        items.add(index, item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        updateScrollbars();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void remove(String item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // FIXME: need to clean up select list, too?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        items.remove(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        updateScrollbars();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // Is vsb visible now?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public void remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // FIXME: need to clean up select list, too?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        items.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        updateScrollbars();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Is vsb visible now?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public void removeAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        items.removeAll(items);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        updateScrollbars();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void setMultiSelect(boolean ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        multiSelect = ms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * docs.....definitely docs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * merely keeps internal track of which items are selected for painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * dealing with target Components happens elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void select(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (index > getItemCount() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            index = (isEmpty() ? -1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (multiSelect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            assert false : "Implement ListHelper.select() for multiselect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        else if (getSelectedIndex() != index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            selected.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            selected.add(Integer.valueOf(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            makeVisible(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /* docs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void deselect(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /* docs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /* if called for multiselect, return -1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public int getSelectedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (!multiSelect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            Integer val = (Integer)selected.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return val.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    int[] getSelectedIndexes() { assert(false); return null;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * A getter method for XChoicePeer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Returns vsbVisiblityChanged value and sets it to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public boolean checkVsbVisibilityChangedAndReset(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        boolean returnVal = vsbVisibilityChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        vsbVisibilityChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return returnVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return items.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public int getItemCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return items.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public String getItem(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return (String) items.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**********************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /* GUI-related methods                                                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**********************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void setFocusedIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        focusedIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public boolean isFocusedIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return index == focusedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void setFont(Font newFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (newFont != font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            font = newFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            // Also cache stuff like fontHeight?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Returns width of the text of the longest item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public int getMaxItemWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int end = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        for(int i = 0 ; i < end ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int l = fm.stringWidth(getItem(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            m = Math.max(m, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Height of an item (this doesn't include ITEM_MARGIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    int getItemHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return fm.getHeight() + (2*TEXT_SPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public int y2index(int y) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   264
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            log.fine("y=" + y +", firstIdx=" + firstDisplayedIndex() +", itemHeight=" + getItemHeight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                     + ",item_margin=" + ITEM_MARGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int newIdx = firstDisplayedIndex() + ((y - 2*ITEM_MARGIN) / (getItemHeight() + 2*ITEM_MARGIN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return newIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /* write these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    int index2y(int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public int numItemsDisplayed() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public int firstDisplayedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public int lastDisplayedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // FIXME: need to account for horiz scroll bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (hsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            assert false : "Implement for horiz scroll bar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return vsbVis ? vsb.getValue() + maxVisItems - 1: getItemCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * If the given index is not visible in the List, scroll so that it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public void makeVisible(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (index < firstDisplayedIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                vsb.setValue(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            else if (index > lastDisplayedIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                vsb.setValue(index - maxVisItems + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    // FIXME: multi-select needs separate focused index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void up() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        int curIdx = getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        int numItems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int newIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        assert curIdx >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (curIdx == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            newIdx = numItems - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            newIdx = --curIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        // focus(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public void down() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int newIdx = (getSelectedIndex() + 1) % getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        select(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public void pageUp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // FIXME: for multi-select, move the focused item, not the selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (vsbVis && firstDisplayedIndex() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (multiSelect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                assert false : "Implement pageUp() for multiSelect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                int selectionOffset = getSelectedIndex() - firstDisplayedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                // the vsb does bounds checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                int newIdx = firstDisplayedIndex() - vsb.getBlockIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                vsb.setValue(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                select(firstDisplayedIndex() + selectionOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public void pageDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (vsbVis && lastDisplayedIndex() < getItemCount() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (multiSelect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                assert false : "Implement pageDown() for multiSelect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                int selectionOffset = getSelectedIndex() - firstDisplayedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                // the vsb does bounds checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                int newIdx = lastDisplayedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                vsb.setValue(newIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                select(firstDisplayedIndex() + selectionOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public void home() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public void end() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public boolean isVSBVisible() { return vsbVis; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public boolean isHSBVisible() { return hsbVis; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public XVerticalScrollbar getVSB() { return vsb; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public XHorizontalScrollbar getHSB() { return hsb; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public boolean isInVertSB(Rectangle bounds, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            assert vsb != null : "Vert scrollbar is visible, yet is null?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            int sbHeight = hsbVis ? bounds.height - SCROLLBAR_WIDTH : bounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return (x <= bounds.width) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                   (x >= bounds.width - SCROLLBAR_WIDTH) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                   (y >= 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                   (y <= sbHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public boolean isInHorizSB(Rectangle bounds, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (hsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            assert hsb != null : "Horiz scrollbar is visible, yet is null?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            int sbWidth = vsbVis ? bounds.width - SCROLLBAR_WIDTH : bounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return (x <= sbWidth) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                   (x >= 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                   (y >= bounds.height - SCROLLBAR_WIDTH) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                   (y <= bounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void handleVSBEvent(MouseEvent e, Rectangle bounds, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        int sbHeight = hsbVis ? bounds.height - SCROLLBAR_WIDTH : bounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        vsb.handleMouseEvent(e.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                             e.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                             x - (bounds.width - SCROLLBAR_WIDTH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                             y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Called when items are added/removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Update whether the scrollbar is visible or not, scrollbar values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    void updateScrollbars() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        boolean oldVsbVis = vsbVis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        vsbVis = vsb != null && items.size() > maxVisItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            vsb.setValues(vsb.getValue(), getNumItemsDisplayed(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                          vsb.getMinimum(), items.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        // 6405689. If Vert Scrollbar gets disappeared from the dropdown menu we should repaint whole dropdown even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // no actual resize gets invoked. This is needed because some painting artifacts remained between dropdown items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // but draw3DRect doesn't clear the area inside. Instead it just paints lines as borders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        vsbVisibilityChanged = (vsbVis != oldVsbVis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        // FIXME: check if added item makes a hsb necessary (if supported, that of course)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public int getNumItemsDisplayed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return items.size() > maxVisItems ? maxVisItems : items.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public void repaintScrollbarRequest(XScrollbar sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        Graphics g = peer.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        Rectangle bounds = peer.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if ((sb == vsb) && vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            paintVSB(g, XComponentPeer.getSystemColors(), bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        else if ((sb == hsb) && hsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            paintHSB(g, XComponentPeer.getSystemColors(), bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (obj == vsb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            int oldScrollValue = vsb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            vsb.setValue(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            boolean needRepaint = (oldScrollValue != vsb.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            // See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (mouseDraggedOutVertically){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                int oldItemValue = getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                int newItemValue = getSelectedIndex() + v - oldScrollValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                select(newItemValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                needRepaint = needRepaint || (getSelectedIndex() != oldItemValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            // FIXME: how are we going to paint!?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            Graphics g = peer.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            Rectangle bounds = peer.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            int first = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            int last = Math.min(getItemCount() - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                v + maxVisItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (needRepaint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                paintItems(g, colors, bounds, first, last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        else if ((XHorizontalScrollbar)obj == hsb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            hsb.setValue(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            // FIXME: how are we going to paint!?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public void updateColors(Color[] newColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        colors = newColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public void paintItems(Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                           Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                           Rectangle bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                           Font font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                           int first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                           int last,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                           XVerticalScrollbar vsb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                           XHorizontalScrollbar hsb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public void paintItems(Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                           Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                           Rectangle bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // paint border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        // paint items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // paint scrollbars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        // paint focus?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public void paintAllItems(Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                           Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                           Rectangle bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        paintItems(g, colors, bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                   firstDisplayedIndex(), lastDisplayedIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public void paintItems(Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                           Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                           Rectangle bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                           int first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                           int last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        peer.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        int x = BORDER_WIDTH + ITEM_MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        int width = bounds.width - 2*ITEM_MARGIN - 2*BORDER_WIDTH - (vsbVis ? SCROLLBAR_WIDTH : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        int height = getItemHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        int y = BORDER_WIDTH + ITEM_MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        for (int i = first; i <= last ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            paintItem(g, colors, getItem(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                      x, y, width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                      isItemSelected(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                      isFocusedIndex(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            y += height + 2*ITEM_MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (vsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            paintVSB(g, XComponentPeer.getSystemColors(), bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (hsbVis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            paintHSB(g, XComponentPeer.getSystemColors(), bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        peer.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // FIXME: if none of the items were focused, paint focus around the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // entire list.  This is how java.awt.List should work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * comment about what is painted (i.e. the focus rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public void paintItem(Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                          Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                          String string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                          int x, int y, int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                          boolean selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                          boolean focused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        //System.out.println("LP.pI(): x="+x+" y="+y+" w="+width+" h="+height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        //g.setColor(colors[BACKGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        // FIXME: items shouldn't draw into the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            g.setColor(colors[XComponentPeer.FOREGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        g.fillRect(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (focused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            //g.setColor(colors[XComponentPeer.FOREGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            g.setColor(Color.BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            g.drawRect(x + FOCUS_INSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                       y + FOCUS_INSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                       width - 2*FOCUS_INSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                       height - 2*FOCUS_INSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            g.setColor(colors[XComponentPeer.FOREGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        g.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        //Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        //g.clipRect(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        //g.drawString(string, x + TEXT_SPACE, y + TEXT_SPACE + ITEM_MARGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        int fontAscent = fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        int fontDescent = fm.getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        g.drawString(string, x + TEXT_SPACE, y + (height + fm.getMaxAscent() - fm.getMaxDescent())/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        //g.clipRect(clip.x, clip.y, clip.width, clip.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    boolean isItemSelected(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        Iterator itr = selected.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        while (itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            Integer val = (Integer)itr.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            if (val.intValue() == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public void paintVSB(Graphics g, Color colors[], Rectangle bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        int height = bounds.height - 2*BORDER_WIDTH - (hsbVis ? (SCROLLBAR_WIDTH-2) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        Graphics ng = g.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            ng.translate(bounds.width - BORDER_WIDTH - SCROLLBAR_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                         BORDER_WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            // Update scrollbar's size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            vsb.setSize(SCROLLBAR_WIDTH, bounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            vsb.paint(ng, colors, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            ng.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public void paintHSB(Graphics g, Color colors[], Rectangle bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Helper method for Components with integrated scrollbars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Pass in the vertical and horizontal scroll bar (or null for none/hidden)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * and the MouseWheelEvent, and the appropriate scrollbar will be scrolled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Returns whether or not scrolling actually took place.  This will indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * whether or not repainting is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    static boolean doWheelScroll(XVerticalScrollbar vsb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                                     XHorizontalScrollbar hsb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                     MouseWheelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        XScrollbar scroll = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        int wheelRotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        // Determine which, if any, sb to scroll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (vsb != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            scroll = vsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        else if (hsb != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            scroll = hsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        else { // Neither scrollbar is showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        wheelRotation = e.getWheelRotation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        // Check if scroll is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if ((wheelRotation < 0 && scroll.getValue() > scroll.getMinimum()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            (wheelRotation > 0 && scroll.getValue() < scroll.getMaximum()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            wheelRotation != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            int type = e.getScrollType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            int incr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (type == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                incr = wheelRotation * scroll.getBlockIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            else { // type is WHEEL_UNIT_SCROLL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                incr = e.getUnitsToScroll() * scroll.getUnitIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            scroll.setValue(scroll.getValue() + incr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Helper method for XChoicePeer with integrated vertical scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * 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
   658
     * Restoring Motif behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    void trackMouseDraggedScroll(int mouseX, int mouseY, int listWidth, int listHeight){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (!mouseDraggedOutVertically){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            if (vsb.beforeThumb(mouseX, mouseY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                vsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                vsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if(!mouseDraggedOutVertically && (mouseY < 0 || mouseY >= listHeight)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            mouseDraggedOutVertically = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            vsb.startScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (mouseDraggedOutVertically && mouseY >= 0 && mouseY < listHeight && mouseX >= 0 && mouseX < listWidth){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            mouseDraggedOutVertically = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            vsb.stopScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
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
     * Helper method for XChoicePeer with integrated vertical scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Stop vertical scrolling when mouse released in / out the area of the list if it's required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Restoring Motif behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * see 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    void trackMouseReleasedScroll(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (mouseDraggedOutVertically){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            mouseDraggedOutVertically = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            vsb.stopScrollingInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
}