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