001: /*
002: * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt;
027:
028: import java.awt.ScrollPane;
029: import java.awt.Insets;
030: import java.awt.Adjustable;
031: import java.awt.event.MouseWheelEvent;
032:
033: import sun.awt.DebugHelper;
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 static final DebugHelper dbg = DebugHelper
042: .create(ScrollPaneWheelScroller.class);
043:
044: private ScrollPaneWheelScroller() {
045: }
046:
047: /*
048: * Called from ScrollPane.processMouseWheelEvent()
049: */
050: public static void handleWheelScrolling(ScrollPane sp,
051: MouseWheelEvent e) {
052: if (dbg.on)
053: dbg.println("SPWS.hWS(): x=" + e.getX() + ",y=" + e.getY()
054: + " src is " + e.getSource());
055: int increment = 0;
056:
057: if (sp != null && e.getScrollAmount() != 0) {
058: Adjustable adj = getAdjustableToScroll(sp);
059: if (adj != null) {
060: increment = getIncrementFromAdjustable(adj, e);
061: if (dbg.on) {
062: dbg.println("increment from adjustable("
063: + adj.getClass() + " : " + increment);
064: }
065: scrollAdjustable(adj, increment);
066: }
067: }
068: }
069:
070: /*
071: * Given a ScrollPane, determine which Scrollbar should be scrolled by the
072: * mouse wheel, if any.
073: */
074: public static Adjustable getAdjustableToScroll(ScrollPane sp) {
075: int policy = sp.getScrollbarDisplayPolicy();
076:
077: // if policy is display always or never, use vert
078: if (policy == ScrollPane.SCROLLBARS_ALWAYS
079: || policy == ScrollPane.SCROLLBARS_NEVER) {
080: if (dbg.on)
081: dbg
082: .println(" using vertical scrolling due to scrollbar policy");
083: return sp.getVAdjustable();
084:
085: } else {
086:
087: Insets ins = sp.getInsets();
088: int vertScrollWidth = sp.getVScrollbarWidth();
089:
090: if (dbg.on) {
091: dbg.println("insets: l:" + ins.left + " r:" + ins.right
092: + " t:" + ins.top + " b:" + ins.bottom);
093: dbg.println("vertScrollWidth = " + vertScrollWidth);
094: }
095:
096: // Check if scrollbar is showing by examining insets of the
097: // ScrollPane
098: if (ins.right >= vertScrollWidth) {
099: if (dbg.on)
100: dbg
101: .println(" using vertical scrolling because scrollbar is present");
102: return sp.getVAdjustable();
103: } else {
104: int horizScrollHeight = sp.getHScrollbarHeight();
105: if (ins.bottom >= horizScrollHeight) {
106: if (dbg.on)
107: dbg
108: .println(" using horiz scrolling because scrollbar is present");
109: return sp.getHAdjustable();
110: } else {
111: if (dbg.on)
112: dbg
113: .println(" using NO scrollbar becsause neither is present");
114: return null;
115: }
116: }
117: }
118: }
119:
120: /*
121: * Given the info in a MouseWheelEvent and an Adjustable to scroll, return
122: * the amount by which the Adjustable should be adjusted. This value may
123: * be positive or negative.
124: */
125: public static int getIncrementFromAdjustable(Adjustable adj,
126: MouseWheelEvent e) {
127: // ASSUME: adj != null
128: int increment = 0;
129:
130: if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
131: increment = e.getUnitsToScroll() * adj.getUnitIncrement();
132: } else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
133: increment = adj.getBlockIncrement() * e.getWheelRotation();
134: }
135: return increment;
136: }
137:
138: /*
139: * Scroll the given Adjustable by the given amount. Checks the Adjustable's
140: * bounds and sets the new value to the Adjustable.
141: */
142: public static void scrollAdjustable(Adjustable adj, int amount) {
143: // ASSUME adj != null
144: // ASSUME amount != 0
145:
146: int current = adj.getValue();
147: int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
148: if (dbg.on)
149: dbg.println(" doScrolling by " + amount);
150:
151: if (amount > 0 && current < upperLimit) { // still some room to scroll
152: // down
153: if (current + amount < upperLimit) {
154: adj.setValue(current + amount);
155: return;
156: } else {
157: adj.setValue(upperLimit);
158: return;
159: }
160: } else if (amount < 0 && current > adj.getMinimum()) { // still some room
161: // to scroll up
162: if (current + amount > adj.getMinimum()) {
163: adj.setValue(current + amount);
164: return;
165: } else {
166: adj.setValue(adj.getMinimum());
167: return;
168: }
169: }
170: }
171: }
|