0N/A/*
2362N/A * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt;
0N/A
0N/Aimport java.awt.ScrollPane;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.Adjustable;
0N/Aimport java.awt.event.MouseWheelEvent;
0N/A
1696N/Aimport sun.util.logging.PlatformLogger;
0N/A
0N/A/*
0N/A * ScrollPaneWheelScroller is a helper class for implmenenting mouse wheel
0N/A * scrolling on a java.awt.ScrollPane. It contains only static methods.
0N/A * No objects of this class may be instantiated, thus it is declared abstract.
0N/A */
0N/Apublic abstract class ScrollPaneWheelScroller {
0N/A
1696N/A private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.ScrollPaneWheelScroller");
0N/A
0N/A private ScrollPaneWheelScroller() {}
0N/A
0N/A /*
0N/A * Called from ScrollPane.processMouseWheelEvent()
0N/A */
0N/A public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
1696N/A log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
0N/A }
0N/A int increment = 0;
0N/A
0N/A if (sp != null && e.getScrollAmount() != 0) {
0N/A Adjustable adj = getAdjustableToScroll(sp);
0N/A if (adj != null) {
0N/A increment = getIncrementFromAdjustable(adj, e);
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
1696N/A log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
0N/A }
0N/A scrollAdjustable(adj, increment);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Given a ScrollPane, determine which Scrollbar should be scrolled by the
0N/A * mouse wheel, if any.
0N/A */
0N/A public static Adjustable getAdjustableToScroll(ScrollPane sp) {
0N/A int policy = sp.getScrollbarDisplayPolicy();
0N/A
0N/A // if policy is display always or never, use vert
0N/A if (policy == ScrollPane.SCROLLBARS_ALWAYS ||
0N/A policy == ScrollPane.SCROLLBARS_NEVER) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
1696N/A log.finer("using vertical scrolling due to scrollbar policy");
0N/A }
0N/A return sp.getVAdjustable();
0N/A
0N/A }
0N/A else {
0N/A
0N/A Insets ins = sp.getInsets();
0N/A int vertScrollWidth = sp.getVScrollbarWidth();
0N/A
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
1696N/A log.finer("insets: l = " + ins.left + ", r = " + ins.right +
0N/A ", t = " + ins.top + ", b = " + ins.bottom);
1696N/A log.finer("vertScrollWidth = " + vertScrollWidth);
0N/A }
0N/A
0N/A // Check if scrollbar is showing by examining insets of the
0N/A // ScrollPane
0N/A if (ins.right >= vertScrollWidth) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
1696N/A log.finer("using vertical scrolling because scrollbar is present");
0N/A }
0N/A return sp.getVAdjustable();
0N/A }
0N/A else {
0N/A int horizScrollHeight = sp.getHScrollbarHeight();
0N/A if (ins.bottom >= horizScrollHeight) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
1696N/A log.finer("using horiz scrolling because scrollbar is present");
0N/A }
0N/A return sp.getHAdjustable();
0N/A }
0N/A else {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
1696N/A log.finer("using NO scrollbar becsause neither is present");
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Given the info in a MouseWheelEvent and an Adjustable to scroll, return
0N/A * the amount by which the Adjustable should be adjusted. This value may
0N/A * be positive or negative.
0N/A */
0N/A public static int getIncrementFromAdjustable(Adjustable adj,
0N/A MouseWheelEvent e) {
1696N/A if (log.isLoggable(PlatformLogger.FINE)) {
0N/A if (adj == null) {
1696N/A log.fine("Assertion (adj != null) failed");
0N/A }
0N/A }
0N/A
0N/A int increment = 0;
0N/A
0N/A if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
0N/A increment = e.getUnitsToScroll() * adj.getUnitIncrement();
0N/A }
0N/A else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
0N/A increment = adj.getBlockIncrement() * e.getWheelRotation();
0N/A }
0N/A return increment;
0N/A }
0N/A
0N/A /*
0N/A * Scroll the given Adjustable by the given amount. Checks the Adjustable's
0N/A * bounds and sets the new value to the Adjustable.
0N/A */
0N/A public static void scrollAdjustable(Adjustable adj, int amount) {
1696N/A if (log.isLoggable(PlatformLogger.FINE)) {
0N/A if (adj == null) {
1696N/A log.fine("Assertion (adj != null) failed");
0N/A }
0N/A if (amount == 0) {
1696N/A log.fine("Assertion (amount != 0) failed");
0N/A }
0N/A }
0N/A
0N/A int current = adj.getValue();
0N/A int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
1696N/A log.finer("doScrolling by " + amount);
0N/A }
0N/A
0N/A if (amount > 0 && current < upperLimit) { // still some room to scroll
0N/A // down
0N/A if (current + amount < upperLimit) {
0N/A adj.setValue(current + amount);
0N/A return;
0N/A }
0N/A else {
0N/A adj.setValue(upperLimit);
0N/A return;
0N/A }
0N/A }
0N/A else if (amount < 0 && current > adj.getMinimum()) { // still some room
0N/A // to scroll up
0N/A if (current + amount > adj.getMinimum()) {
0N/A adj.setValue(current + amount);
0N/A return;
0N/A }
0N/A else {
0N/A adj.setValue(adj.getMinimum());
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A}