jdk/src/share/classes/sun/awt/ScrollPaneWheelScroller.java
author mchung
Tue, 29 Sep 2009 16:03:03 -0700
changeset 3938 ef327bd847c0
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes Summary: Replace calls to Logger with sun.util.logging.PlatformLogger Reviewed-by: prr, art, alexp, dcherepanov, igor, dav, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.ScrollPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Adjustable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.MouseWheelEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    33
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * ScrollPaneWheelScroller is a helper class for implmenenting mouse wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * scrolling on a java.awt.ScrollPane.  It contains only static methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * No objects of this class may be instantiated, thus it is declared abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public abstract class ScrollPaneWheelScroller {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    42
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.ScrollPaneWheelScroller");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private ScrollPaneWheelScroller() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Called from ScrollPane.processMouseWheelEvent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    50
        if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    51
            log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int increment = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (sp != null && e.getScrollAmount() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            Adjustable adj = getAdjustableToScroll(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (adj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                increment = getIncrementFromAdjustable(adj, e);
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    59
                if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    60
                    log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                scrollAdjustable(adj, increment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Given a ScrollPane, determine which Scrollbar should be scrolled by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * mouse wheel, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static Adjustable getAdjustableToScroll(ScrollPane sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int policy = sp.getScrollbarDisplayPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // if policy is display always or never, use vert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (policy == ScrollPane.SCROLLBARS_ALWAYS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            policy == ScrollPane.SCROLLBARS_NEVER) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    77
            if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    78
                log.finer("using vertical scrolling due to scrollbar policy");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return sp.getVAdjustable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            Insets ins = sp.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            int vertScrollWidth = sp.getVScrollbarWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    88
            if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    89
                log.finer("insets: l = " + ins.left + ", r = " + ins.right +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                 ", t = " + ins.top + ", b = " + ins.bottom);
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    91
                log.finer("vertScrollWidth = " + vertScrollWidth);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // Check if scrollbar is showing by examining insets of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // ScrollPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (ins.right >= vertScrollWidth) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    97
                if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    98
                    log.finer("using vertical scrolling because scrollbar is present");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return sp.getVAdjustable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                int horizScrollHeight = sp.getHScrollbarHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                if (ins.bottom >= horizScrollHeight) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   105
                    if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   106
                        log.finer("using horiz scrolling because scrollbar is present");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    return sp.getHAdjustable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                else {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   111
                    if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   112
                        log.finer("using NO scrollbar becsause neither is present");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    return null;
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Given the info in a MouseWheelEvent and an Adjustable to scroll, return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * the amount by which the Adjustable should be adjusted.  This value may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * be positive or negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static int getIncrementFromAdjustable(Adjustable adj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                 MouseWheelEvent e) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   127
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (adj == null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   129
                log.fine("Assertion (adj != null) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int increment = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            increment = e.getUnitsToScroll() * adj.getUnitIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            increment = adj.getBlockIncrement() * e.getWheelRotation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Scroll the given Adjustable by the given amount.  Checks the Adjustable's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * bounds and sets the new value to the Adjustable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static void scrollAdjustable(Adjustable adj, int amount) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   149
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (adj == null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   151
                log.fine("Assertion (adj != null) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (amount == 0) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   154
                log.fine("Assertion (amount != 0) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int current = adj.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   160
        if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   161
            log.finer("doScrolling by " + amount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (amount > 0 && current < upperLimit) { // still some room to scroll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                                  // down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (current + amount < upperLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                adj.setValue(current + amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                adj.setValue(upperLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        else if (amount < 0 && current > adj.getMinimum()) { // still some room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                                             // to scroll up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (current + amount > adj.getMinimum()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                adj.setValue(current + amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                adj.setValue(adj.getMinimum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}