001: /*
002: * @(#)ScrollPaneWheelScroller.java 1.5 03/01/23
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.awt;
028:
029: import java.awt.Toolkit;
030: import java.awt.ScrollPane;
031: import java.awt.Insets;
032: import java.awt.Adjustable;
033: import java.awt.event.MouseWheelEvent;
034:
035: /*
036: * ScrollPaneWheelScroller is a helper class for implmenenting mouse wheel
037: * scrolling on a java.awt.ScrollPane. It contains only static methods.
038: * No objects of this class may be instantiated, thus it is declared abstract.
039: */
040: public abstract class ScrollPaneWheelScroller {
041: private ScrollPaneWheelScroller() {
042: }
043:
044: /*
045: * Called from ScrollPane.processMouseWheelEvent()
046: */
047: public static void handleWheelScrolling(ScrollPane sp,
048: MouseWheelEvent e) {
049: int increment = 0;
050:
051: if (sp != null && e.getScrollAmount() != 0) {
052: Adjustable adj = getAdjustableToScroll(sp);
053: if (adj != null) {
054: increment = getIncrementFromAdjustable(adj, e);
055: scrollAdjustable(adj, increment);
056: }
057: }
058: }
059:
060: /*
061: * Given a ScrollPane, determine which Scrollbar should be scrolled by the
062: * mouse wheel, if any.
063: */
064: public static Adjustable getAdjustableToScroll(ScrollPane sp) {
065: int policy = sp.getScrollbarDisplayPolicy();
066:
067: // if policy is display always or never, use vert
068: if (policy == ScrollPane.SCROLLBARS_ALWAYS
069: || policy == ScrollPane.SCROLLBARS_NEVER) {
070: return sp.getVAdjustable();
071:
072: } else {
073:
074: Insets ins = sp.getInsets();
075: int vertScrollWidth = sp.getVScrollbarWidth();
076:
077: // Check if scrollbar is showing by examining insets of the
078: // ScrollPane
079: if (ins.right >= vertScrollWidth) {
080: return sp.getVAdjustable();
081: } else {
082: int horizScrollHeight = sp.getHScrollbarHeight();
083: if (ins.bottom >= horizScrollHeight) {
084: return sp.getHAdjustable();
085: } else {
086: return null;
087: }
088: }
089: }
090: }
091:
092: /*
093: * Given the info in a MouseWheelEvent and an Adjustable to scroll, return
094: * the amount by which the Adjustable should be adjusted. This value may
095: * be positive or negative.
096: */
097: public static int getIncrementFromAdjustable(Adjustable adj,
098: MouseWheelEvent e) {
099: // ASSUME: adj != null
100: int increment = 0;
101:
102: if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
103: increment = e.getUnitsToScroll() * adj.getUnitIncrement();
104: } else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
105: increment = adj.getBlockIncrement() * e.getWheelRotation();
106: }
107: return increment;
108: }
109:
110: /*
111: * Scroll the given Adjustable by the given amount. Checks the Adjustable's
112: * bounds and sets the new value to the Adjustable.
113: */
114: public static void scrollAdjustable(Adjustable adj, int amount) {
115: // ASSUME adj != null
116: // ASSUME amount != 0
117:
118: int current = adj.getValue();
119: int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
120:
121: if (amount > 0 && current < upperLimit) { // still some room to scroll
122: // down
123: if (current + amount < upperLimit) {
124: adj.setValue(current + amount);
125: return;
126: } else {
127: adj.setValue(upperLimit);
128: return;
129: }
130: } else if (amount < 0 && current > adj.getMinimum()) { // still some room
131: // to scroll up
132: if (current + amount > adj.getMinimum()) {
133: adj.setValue(current + amount);
134: return;
135: } else {
136: adj.setValue(adj.getMinimum());
137: return;
138: }
139: }
140: }
141: }
|