jdk/src/solaris/classes/sun/awt/X11/XScrollbar.java
author yan
Mon, 06 Oct 2008 16:45:00 +0400
changeset 1966 12a51fb0db0d
parent 715 f16baef3a20e
child 3938 ef327bd847c0
permissions -rw-r--r--
5100701: Toolkit.getLockingKeyState() does not work on XToolkit, but works on Motif Summary: Does not work on Motif but works on XToolkit now; implemented using XQueryPointer. Reviewed-by: anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.awt.X11GraphicsConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
* A simple vertical scroll bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
abstract class XScrollbar {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Logger log = Logger.getLogger("sun.awt.X11.XScrollbar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * The thread that asynchronously tells the scrollbar to scroll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * @see #startScrolling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static XScrollRepeater scroller = new XScrollRepeater(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The repeater that used for concurrent scrolling of the vertical and horizontal scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * And so there is not static keyword
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private XScrollRepeater i_scroller = new XScrollRepeater(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Thumb length is always >= MIN_THUMB_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final static int MIN_THUMB_H = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int ARROW_IND = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    XScrollbarClient sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    //Use set methods to set scrollbar parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private int vis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int page;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private boolean needsRepaint = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private boolean pressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private boolean dragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    Polygon firstArrow, secondArrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    int width, height; // Dimensions of the visible part of the parent window
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int barWidth, barLength; // Rotation-independent values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                             // equal to (width, height) for vertical,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                             // rotated by 90 for horizontal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                             // That is, barLength is always the length between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                             // the tips of the arrows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int arrowArea;     // The area reserved for the scroll arrows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int ALIGNMENT_VERTICAL = 1, ALIGNMENT_HORIZONTAL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    Point thumbOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Rectangle prevThumb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public XScrollbar(int alignment, XScrollbarClient sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.sb = sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.alignment = alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public boolean needsRepaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return needsRepaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    void notifyValue(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        notifyValue(v, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    void notifyValue(int v, final boolean isAdjusting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (v < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            v = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else if (v > max - vis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            v = max - vis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        final int value = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        final int mode = this.mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if ((sb != null) && ((value != val)||(!pressed))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            SunToolkit.executeOnEventHandlerThread(sb.getEventSource(), new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        sb.notifyValue(XScrollbar.this, mode, value, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    abstract protected void rebuildArrows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void setSize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (log.isLoggable(Level.FINER)) log.finer("Setting scroll bar " + this + " size to " + width + "x" + height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Creates oriented directed arrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    protected Polygon createArrowShape(boolean vertical, boolean up) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Polygon arrow = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // TODO: this should be done polymorphically in subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // FIXME: arrows overlap the thumb for very wide scrollbars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (vertical) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int x = width / 2 - getArrowWidth()/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int y1 = (up ? ARROW_IND : barLength - ARROW_IND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int y2 = (up ? getArrowWidth() : barLength - getArrowWidth() - ARROW_IND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            arrow.addPoint(x + getArrowWidth()/2, y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            arrow.addPoint(x + getArrowWidth(), y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            arrow.addPoint(x, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            arrow.addPoint(x + getArrowWidth()/2, y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int y = height / 2 - getArrowWidth()/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int x1 = (up ? ARROW_IND : barLength - ARROW_IND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            int x2 = (up ? getArrowWidth() : barLength - getArrowWidth() - ARROW_IND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            arrow.addPoint(x1, y + getArrowWidth()/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            arrow.addPoint(x2, y + getArrowWidth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            arrow.addPoint(x2, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            arrow.addPoint(x1, y + getArrowWidth()/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return arrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Gets the area of the scroll track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected abstract Rectangle getThumbArea();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * paint the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param g the graphics context to paint into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param colors the colors to use when painting the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param width the width of the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param height the height of the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param paintAll paint the whole scrollbar if true, just the thumb is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    void paint(Graphics g, Color colors[], boolean paintAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (log.isLoggable(Level.FINER)) log.finer("Painting scrollbar " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        boolean useBufferedImage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Graphics2D g2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        BufferedImage buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (!(g instanceof Graphics2D)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // Fix for 5045936, 5055171. While printing, g is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            //   of sun.print.ProxyPrintGraphics which extends Graphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            //   So we use a separate buffered image and its graphics is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            //   always Graphics2D instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            X11GraphicsConfig graphicsConfig = (X11GraphicsConfig)(sb.getEventSource().getGraphicsConfiguration());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            buffer = graphicsConfig.createCompatibleImage(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            g2 = buffer.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            useBufferedImage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            g2 = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            Rectangle thumbRect = calculateThumbRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
//              if (prevH == thumbH && prevY == thumbPosY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
//                  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
//              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            prevThumb = thumbRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // TODO: Share Motif colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            Color back = colors[XComponentPeer.BACKGROUND_COLOR];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            Color selectColor = new Color(MotifColorUtilities.calculateSelectFromBackground(back.getRed(),back.getGreen(),back.getBlue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            Color darkShadow = new Color(MotifColorUtilities.calculateBottomShadowFromBackground(back.getRed(),back.getGreen(),back.getBlue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            Color lightShadow = new Color(MotifColorUtilities.calculateTopShadowFromBackground(back.getRed(),back.getGreen(),back.getBlue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                XlibWrapper.XFlush(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            /* paint the background slightly darker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (paintAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // fill the entire background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                g2.setColor(selectColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (alignment == ALIGNMENT_HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    g2.fillRect(0, 0, thumbRect.x, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    g2.fillRect(thumbRect.x + thumbRect.width , 0, width - (thumbRect.x + thumbRect.width), height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    g2.fillRect(0, 0, width, thumbRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    g2.fillRect(0, thumbRect.y + thumbRect.height, width, height - (thumbRect.y + thumbRect.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                // Paint edges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                // TODO: Share Motif 3d rect drawing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                g2.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                g2.drawLine(0, 0, width-1, 0);           // top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                g2.drawLine(0, 0, 0, height-1);          // left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                g2.setColor(lightShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                g2.drawLine(1, height-1, width-1, height-1); // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                g2.drawLine(width-1, 1, width-1, height-1);  // right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                // Clear all thumb area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                g2.setColor(selectColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                Rectangle thumbArea = getThumbArea();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                g2.fill(thumbArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (paintAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                // ************ paint the arrows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                 paintArrows(g2, colors[XComponentPeer.BACKGROUND_COLOR], darkShadow, lightShadow );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // Thumb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            g2.setColor(colors[XComponentPeer.BACKGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            g2.fillRect(thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            g2.setColor(lightShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            g2.drawLine(thumbRect.x, thumbRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                       thumbRect.x + thumbRect.width, thumbRect.y); // top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            g2.drawLine(thumbRect.x, thumbRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                       thumbRect.x, thumbRect.y+thumbRect.height); // left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            g2.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            g2.drawLine(thumbRect.x+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                       thumbRect.y+thumbRect.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                       thumbRect.x+thumbRect.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                       thumbRect.y+thumbRect.height);  // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            g2.drawLine(thumbRect.x+thumbRect.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                       thumbRect.y+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                       thumbRect.x+thumbRect.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                       thumbRect.y+thumbRect.height); // right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (useBufferedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                g2.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (useBufferedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            g.drawImage(buffer, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            XlibWrapper.XFlush(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
      void paintArrows(Graphics2D g, Color background, Color darkShadow, Color lightShadow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
          g.setColor(background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // paint firstArrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (pressed && (mode == AdjustmentEvent.UNIT_DECREMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            g.fill(firstArrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            g.setColor(lightShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            g.drawLine(firstArrow.xpoints[0],firstArrow.ypoints[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    firstArrow.xpoints[1],firstArrow.ypoints[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            g.drawLine(firstArrow.xpoints[1],firstArrow.ypoints[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    firstArrow.xpoints[2],firstArrow.ypoints[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            g.drawLine(firstArrow.xpoints[2],firstArrow.ypoints[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    firstArrow.xpoints[0],firstArrow.ypoints[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            g.fill(firstArrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            g.drawLine(firstArrow.xpoints[0],firstArrow.ypoints[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    firstArrow.xpoints[1],firstArrow.ypoints[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            g.drawLine(firstArrow.xpoints[1],firstArrow.ypoints[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    firstArrow.xpoints[2],firstArrow.ypoints[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            g.setColor(lightShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            g.drawLine(firstArrow.xpoints[2],firstArrow.ypoints[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    firstArrow.xpoints[0],firstArrow.ypoints[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        g.setColor(background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // paint second Arrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (pressed && (mode == AdjustmentEvent.UNIT_INCREMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            g.fill(secondArrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            g.setColor(lightShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            g.drawLine(secondArrow.xpoints[0],secondArrow.ypoints[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    secondArrow.xpoints[1],secondArrow.ypoints[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            g.drawLine(secondArrow.xpoints[1],secondArrow.ypoints[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    secondArrow.xpoints[2],secondArrow.ypoints[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            g.drawLine(secondArrow.xpoints[2],secondArrow.ypoints[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    secondArrow.xpoints[0],secondArrow.ypoints[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
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
            g.fill(secondArrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            g.drawLine(secondArrow.xpoints[0],secondArrow.ypoints[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    secondArrow.xpoints[1],secondArrow.ypoints[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            g.setColor(lightShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            g.drawLine(secondArrow.xpoints[1],secondArrow.ypoints[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    secondArrow.xpoints[2],secondArrow.ypoints[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            g.drawLine(secondArrow.xpoints[2],secondArrow.ypoints[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    secondArrow.xpoints[0],secondArrow.ypoints[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Tell the scroller to start scrolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    void startScrolling() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        log.finer("Start scrolling on " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // Make sure that we scroll at least once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        scroll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // wake up the scroll repeater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (scroller == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            // If there isn't a scroller, then create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // one and start it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            scroller = new XScrollRepeater(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            scroller.setScrollbar(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        scroller.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Tell the instance scroller to start scrolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    void startScrollingInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        log.finer("Start scrolling on " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // Make sure that we scroll at least once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        scroll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        i_scroller.setScrollbar(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        i_scroller.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Tell the instance scroller to stop scrolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    void stopScrollingInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        log.finer("Stop scrolling on " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        i_scroller.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * The set method for mode property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * See 6243382 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public void setMode(int mode){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        this.mode = mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Scroll one unit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @see notifyValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    void scroll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
          case AdjustmentEvent.UNIT_DECREMENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
              notifyValue(val - line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
          case AdjustmentEvent.UNIT_INCREMENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
              notifyValue(val + line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
          case AdjustmentEvent.BLOCK_DECREMENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
              notifyValue(val - page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
          case AdjustmentEvent.BLOCK_INCREMENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
              notifyValue(val + page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    boolean isInArrow(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // Mouse is considered to be in the arrow if it is anywhere in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // arrow area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int coord = (alignment == ALIGNMENT_HORIZONTAL ? x : y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int arrAreaH = getArrowAreaWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (coord < arrAreaH || coord > barLength - arrAreaH + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Is x,y in the scroll thumb?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * If we ever cache the thumb rect, we may need to clone the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * calculateThumbRect().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    boolean isInThumb(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        Rectangle thumbRect = calculateThumbRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        // If the mouse is in the shadow of the thumb or the shadow of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // scroll track, treat it as hitting the thumb.  So, slightly enlarge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // our rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        thumbRect.x -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        thumbRect.width += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        thumbRect.height += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return thumbRect.contains(x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    abstract boolean beforeThumb(int x, int y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @see java.awt.event.MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * MouseEvent.MOUSE_CLICKED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * MouseEvent.MOUSE_PRESSED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * MouseEvent.MOUSE_RELEASED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * MouseEvent.MOUSE_MOVED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * MouseEvent.MOUSE_ENTERED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * MouseEvent.MOUSE_EXITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * MouseEvent.MOUSE_DRAGGED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public void handleMouseEvent(int id, int modifiers, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if ((modifiers & InputEvent.BUTTON1_MASK) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (log.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
             String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
             switch (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                case MouseEvent.MOUSE_PRESSED:
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   461
                    type = "press";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                case MouseEvent.MOUSE_RELEASED:
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   464
                    type = "release";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                case MouseEvent.MOUSE_DRAGGED:
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   467
                    type = "drag";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                default:
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   470
                    type = "other";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
             log.finer("Mouse " + type + " event in scroll bar " + this +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                                   "x = " + x + ", y = " + y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                                   ", on arrow: " + isInArrow(x, y) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                                   ", on thumb: " + isInThumb(x, y) + ", before thumb: " + beforeThumb(x, y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                                   + ", thumb rect" + calculateThumbRect());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        switch (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
          case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
              if (isInArrow(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                  pressed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                  if (beforeThumb(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                      mode = AdjustmentEvent.UNIT_DECREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                      mode = AdjustmentEvent.UNIT_INCREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                  sb.repaintScrollbarRequest(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                  startScrolling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
              if (isInThumb(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                  mode = AdjustmentEvent.TRACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
              } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                  if (beforeThumb(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                      mode = AdjustmentEvent.BLOCK_DECREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                      mode = AdjustmentEvent.BLOCK_INCREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                  startScrolling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
              Rectangle pos = calculateThumbRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
              thumbOffset = new Point(x - pos.x, y - pos.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
          case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
              pressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
              sb.repaintScrollbarRequest(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
              scroller.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
              if(dragging){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                  handleTrackEvent(x, y, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                  dragging=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
          case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
              dragging = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
              handleTrackEvent(x, y, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    private void handleTrackEvent(int x, int y, boolean isAdjusting){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (mode == AdjustmentEvent.TRACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            notifyValue(calculateCursorOffset(x, y), isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    private int calculateCursorOffset(int x, int y){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (alignment == ALIGNMENT_HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (dragging)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                return Math.max(0,(int)((x - (thumbOffset.x + getArrowAreaWidth()))/getScaleFactor())) + min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                return Math.max(0,(int)((x - (getArrowAreaWidth()))/getScaleFactor())) + min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (dragging)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                return Math.max(0,(int)((y - (thumbOffset.y + getArrowAreaWidth()))/getScaleFactor())) + min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                return Math.max(0,(int)((y - (getArrowAreaWidth()))/getScaleFactor())) + min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
  private void updateNeedsRepaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        Rectangle thumbRect = calculateThumbRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (!prevThumb.equals(thumbRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            needsRepaint = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        prevThumb = thumbRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Sets the values for this Scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * This method enforces the same constraints as in java.awt.Scrollbar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <LI> The maximum must be greater than the minimum </LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <LI> The value must be greater than or equal to the minumum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *      and less than or equal to the maximum minus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *      visible amount </LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * <LI> The visible amount must be greater than 1 and less than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *      to the difference between the maximum and minimum values. </LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Values which do not meet these criteria are quietly coerced to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * appropriate boundary value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param value is the position in the current window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @param visible is the amount visible per page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @param minimum is the minimum value of the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @param maximum is the maximum value of the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    synchronized void setValues(int value, int visible, int minimum, int maximum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (maximum <= minimum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            maximum = minimum + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (visible > maximum - minimum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            visible = maximum - minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (visible < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            visible = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (value < minimum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            value = minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (value > maximum - visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            value = maximum - visible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        this.val = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        this.vis = visible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        this.min = minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        this.max = maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * Sets param of this Scrollbar to the specified values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param value is the position in the current window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param visible is the amount visible per page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @param minimum is the minimum value of the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @param maximum is the maximum value of the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param unitSize is the unit size for increment or decrement of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param page is the block size for increment or decrement of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @see #setValues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    synchronized void setValues(int value, int visible, int minimum, int maximum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                int unitSize, int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        /* Use setValues so that a consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * relating minimum, maximum, and value is enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        setValues(value, visible, minimum, maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        setUnitIncrement(unitSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        setBlockIncrement(blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Returns the current value of this Scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @see #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @see #getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Sets the value of this Scrollbar to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @param value the new value of the Scrollbar. If this value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * below the current minimum or above the current maximum minus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * the visible amount, it becomes the new one of those values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    synchronized void setValue(int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        /* Use setValues so that a consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * relating minimum, maximum, and value is enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        setValues(newValue, vis, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Returns the minimum value of this Scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @see #getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    int getMinimum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Sets the minimum value for this Scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param minimum the minimum value of the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    synchronized void setMinimum(int newMinimum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        /* Use setValues so that a consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         * relating minimum, maximum, and value is enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        setValues(val, vis, newMinimum, max);
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
     * Returns the maximum value of this Scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @see #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    int getMaximum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * Sets the maximum value for this Scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @param maximum the maximum value of the scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    synchronized void setMaximum(int newMaximum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        /* Use setValues so that a consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * relating minimum, maximum, and value is enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        setValues(val, vis, min, newMaximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Returns the visible amount of this Scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    int getVisibleAmount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        return vis;
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
     * Sets the visible amount of this Scrollbar, which is the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * of values represented by the width of the scroll bar's bubble.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @param visible the amount visible per page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    synchronized void setVisibleAmount(int newAmount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        setValues(val, newAmount, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Sets the unit increment for this scrollbar. This is the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * that will be added (subtracted) when the user hits the unit down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * (up) gadgets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @param unitSize is the unit size for increment or decrement of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    synchronized void setUnitIncrement(int unitSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        line = unitSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Gets the unit increment for this scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    int getUnitIncrement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Sets the block increment for this scrollbar. This is the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * that will be added (subtracted) when the user hits the block down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * (up) gadgets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @param blockSize is the block size for increment or decrement of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    synchronized void setBlockIncrement(int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        page = blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * Gets the block increment for this scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    int getBlockIncrement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return page;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * Width of the arrow image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    int getArrowWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return getArrowAreaWidth() - 2*ARROW_IND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Width of the area reserved for arrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    int getArrowAreaWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        return arrowArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    void calculateArrowWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        if (barLength < 2*barWidth + MIN_THUMB_H + 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            arrowArea = (barLength - MIN_THUMB_H + 2*ARROW_IND)/2 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            arrowArea = barWidth - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Returns the scale factor for the thumbArea ( thumbAreaH / (max - min)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @see #getArrowAreaSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    private double getScaleFactor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        double f = (double)(barLength - 2*getArrowAreaWidth()) / Math.max(1,(max - min));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Method to calculate the scroll thumb's size and position.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * based on CalcSliderRect in ScrollBar.c of Motif source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * If we ever cache the thumb rect, we'll need to use a clone in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * isInThumb().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    protected Rectangle calculateThumbRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        float range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        float trueSize;  // Area of scroll track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        float factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        float slideSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        int minSliderWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        int minSliderHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        int hitTheWall = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        int arrAreaH = getArrowAreaWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        Rectangle retVal = new Rectangle(0,0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        trueSize = barLength - 2*arrAreaH - 1;  // Same if vert or horiz
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (alignment == ALIGNMENT_HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            minSliderWidth = MIN_THUMB_H ;  // Base on user-set vis?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            minSliderHeight = height - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        else {  // Vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            minSliderWidth = width - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            minSliderHeight = MIN_THUMB_H ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        // Total number of user units displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            range = max - min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        // A naive notion of pixels per user unit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            factor = trueSize / range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            // A naive notion of the size of the slider in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            // in thermo, slider_size is 0 ans is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            slideSize = vis * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (alignment == ALIGNMENT_HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // Simulating MAX_SCROLLBAR_DIMENSION macro
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            int localVal = (int) (slideSize + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            int localMin = minSliderWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (localVal > localMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                retVal.width = localVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                retVal.width = localMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                hitTheWall = localMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            retVal.height = minSliderHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        else {  // Vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            retVal.width = minSliderWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            // Simulating MAX_SCROLLBAR_DIMENSION macro
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            int localVal = (int) (slideSize + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            int localMin = minSliderHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            if (localVal > localMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                retVal.height = localVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                retVal.height = localMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                hitTheWall = localMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if (hitTheWall != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            trueSize -= hitTheWall;  // Actual pixels available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            range -= vis;            // Actual range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            factor = trueSize / range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if (alignment == ALIGNMENT_HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    retVal.x = ((int) (((((float) val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        - ((float) min)) * factor) + 0.5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        + arrAreaH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    retVal.y = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            retVal.x = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    retVal.y = ((int) (((((float) val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        - ((float) min)) * factor) + 0.5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        + arrAreaH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        // There was one final adjustment here in the Motif function, which was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        // noted to be for backward-compatiblity.  It has been left out for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        return getClass() + "[" + width + "x" + height + "," + barWidth + "x" + barLength + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
class XScrollRepeater implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * Time to pause before the first scroll repeat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    static int beginPause = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    // Reminder - make this a user definable property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Time to pause between each scroll repeat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    static int repeatPause = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    // Reminder - make this a user definable property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * The scrollbar that we sending scrolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    XScrollbar sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * newScroll gets reset when a new scrollbar gets set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    boolean newScroll;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    boolean shouldSkip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Creates a new scroll repeater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param sb the scrollbar that this thread will scroll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    XScrollRepeater(XScrollbar sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        this.setScrollbar(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        newScroll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        shouldSkip = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        XToolkit.schedule(this, beginPause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            shouldSkip = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        XToolkit.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * Sets the scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @param sb the scrollbar that this thread will scroll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    public synchronized void setScrollbar(XScrollbar sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        this.sb = sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        newScroll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            if (shouldSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        sb.scroll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        XToolkit.schedule(this, repeatPause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
}