Path: blob/master/src/java.desktop/share/classes/sun/awt/ScrollPaneWheelScroller.java
41152 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt;2627import java.awt.ScrollPane;28import java.awt.Insets;29import java.awt.Adjustable;30import java.awt.event.MouseWheelEvent;3132import sun.util.logging.PlatformLogger;3334/*35* ScrollPaneWheelScroller is a helper class for implmenenting mouse wheel36* scrolling on a java.awt.ScrollPane. It contains only static methods.37* No objects of this class may be instantiated, thus it is declared abstract.38*/39public abstract class ScrollPaneWheelScroller {4041private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.ScrollPaneWheelScroller");4243private ScrollPaneWheelScroller() {}4445/*46* Called from ScrollPane.processMouseWheelEvent()47*/48public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {49if (log.isLoggable(PlatformLogger.Level.FINER)) {50log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());51}52int increment = 0;5354if (sp != null && e.getScrollAmount() != 0) {55Adjustable adj = getAdjustableToScroll(sp);56if (adj != null) {57increment = getIncrementFromAdjustable(adj, e);58if (log.isLoggable(PlatformLogger.Level.FINER)) {59log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);60}61scrollAdjustable(adj, increment);62}63}64}6566/*67* Given a ScrollPane, determine which Scrollbar should be scrolled by the68* mouse wheel, if any.69*/70public static Adjustable getAdjustableToScroll(ScrollPane sp) {71int policy = sp.getScrollbarDisplayPolicy();7273// if policy is display always or never, use vert74if (policy == ScrollPane.SCROLLBARS_ALWAYS ||75policy == ScrollPane.SCROLLBARS_NEVER) {76if (log.isLoggable(PlatformLogger.Level.FINER)) {77log.finer("using vertical scrolling due to scrollbar policy");78}79return sp.getVAdjustable();8081}82else {8384Insets ins = sp.getInsets();85int vertScrollWidth = sp.getVScrollbarWidth();8687if (log.isLoggable(PlatformLogger.Level.FINER)) {88log.finer("insets: l = " + ins.left + ", r = " + ins.right +89", t = " + ins.top + ", b = " + ins.bottom);90log.finer("vertScrollWidth = " + vertScrollWidth);91}9293// Check if scrollbar is showing by examining insets of the94// ScrollPane95if (ins.right >= vertScrollWidth) {96if (log.isLoggable(PlatformLogger.Level.FINER)) {97log.finer("using vertical scrolling because scrollbar is present");98}99return sp.getVAdjustable();100}101else {102int horizScrollHeight = sp.getHScrollbarHeight();103if (ins.bottom >= horizScrollHeight) {104if (log.isLoggable(PlatformLogger.Level.FINER)) {105log.finer("using horiz scrolling because scrollbar is present");106}107return sp.getHAdjustable();108}109else {110if (log.isLoggable(PlatformLogger.Level.FINER)) {111log.finer("using NO scrollbar becsause neither is present");112}113return null;114}115}116}117}118119/*120* Given the info in a MouseWheelEvent and an Adjustable to scroll, return121* the amount by which the Adjustable should be adjusted. This value may122* be positive or negative.123*/124public static int getIncrementFromAdjustable(Adjustable adj,125MouseWheelEvent e) {126if (log.isLoggable(PlatformLogger.Level.FINE)) {127if (adj == null) {128log.fine("Assertion (adj != null) failed");129}130}131132int increment = 0;133134if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {135increment = e.getUnitsToScroll() * adj.getUnitIncrement();136}137else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {138increment = adj.getBlockIncrement() * e.getWheelRotation();139}140return increment;141}142143/*144* Scroll the given Adjustable by the given amount. Checks the Adjustable's145* bounds and sets the new value to the Adjustable.146*/147public static void scrollAdjustable(Adjustable adj, int amount) {148if (log.isLoggable(PlatformLogger.Level.FINE)) {149if (adj == null) {150log.fine("Assertion (adj != null) failed");151}152if (amount == 0) {153log.fine("Assertion (amount != 0) failed");154}155}156157int current = adj.getValue();158int upperLimit = adj.getMaximum() - adj.getVisibleAmount();159if (log.isLoggable(PlatformLogger.Level.FINER)) {160log.finer("doScrolling by " + amount);161}162163if (amount > 0 && current < upperLimit) { // still some room to scroll164// down165if (current + amount < upperLimit) {166adj.setValue(current + amount);167return;168}169else {170adj.setValue(upperLimit);171return;172}173}174else if (amount < 0 && current > adj.getMinimum()) { // still some room175// to scroll up176if (current + amount > adj.getMinimum()) {177adj.setValue(current + amount);178return;179}180else {181adj.setValue(adj.getMinimum());182return;183}184}185}186}187188189