J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.awt; |
| 27 | |
| 28 | import java.awt.ScrollPane; |
| 29 | import java.awt.Insets; |
| 30 | import java.awt.Adjustable; |
| 31 | import java.awt.event.MouseWheelEvent; |
| 32 | |
| 33 | import java.util.logging.*; |
| 34 | |
| 35 | /* |
| 36 | * ScrollPaneWheelScroller is a helper class for implmenenting mouse wheel |
| 37 | * scrolling on a java.awt.ScrollPane. It contains only static methods. |
| 38 | * No objects of this class may be instantiated, thus it is declared abstract. |
| 39 | */ |
| 40 | public abstract class ScrollPaneWheelScroller { |
| 41 | |
| 42 | private static final Logger log = Logger.getLogger("sun.awt.ScrollPaneWheelScroller"); |
| 43 | |
| 44 | private ScrollPaneWheelScroller() {} |
| 45 | |
| 46 | /* |
| 47 | * Called from ScrollPane.processMouseWheelEvent() |
| 48 | */ |
| 49 | public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) { |
| 50 | if (log.isLoggable(Level.FINER)) { |
| 51 | log.log(Level.FINER, "x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource()); |
| 52 | } |
| 53 | int increment = 0; |
| 54 | |
| 55 | if (sp != null && e.getScrollAmount() != 0) { |
| 56 | Adjustable adj = getAdjustableToScroll(sp); |
| 57 | if (adj != null) { |
| 58 | increment = getIncrementFromAdjustable(adj, e); |
| 59 | if (log.isLoggable(Level.FINER)) { |
| 60 | log.log(Level.FINER, "increment from adjustable(" + adj.getClass() + ") : " + increment); |
| 61 | } |
| 62 | scrollAdjustable(adj, increment); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Given a ScrollPane, determine which Scrollbar should be scrolled by the |
| 69 | * mouse wheel, if any. |
| 70 | */ |
| 71 | public static Adjustable getAdjustableToScroll(ScrollPane sp) { |
| 72 | int policy = sp.getScrollbarDisplayPolicy(); |
| 73 | |
| 74 | // if policy is display always or never, use vert |
| 75 | if (policy == ScrollPane.SCROLLBARS_ALWAYS || |
| 76 | policy == ScrollPane.SCROLLBARS_NEVER) { |
| 77 | if (log.isLoggable(Level.FINER)) { |
| 78 | log.log(Level.FINER, "using vertical scrolling due to scrollbar policy"); |
| 79 | } |
| 80 | return sp.getVAdjustable(); |
| 81 | |
| 82 | } |
| 83 | else { |
| 84 | |
| 85 | Insets ins = sp.getInsets(); |
| 86 | int vertScrollWidth = sp.getVScrollbarWidth(); |
| 87 | |
| 88 | if (log.isLoggable(Level.FINER)) { |
| 89 | log.log(Level.FINER, "insets: l = " + ins.left + ", r = " + ins.right + |
| 90 | ", t = " + ins.top + ", b = " + ins.bottom); |
| 91 | log.log(Level.FINER, "vertScrollWidth = " + vertScrollWidth); |
| 92 | } |
| 93 | |
| 94 | // Check if scrollbar is showing by examining insets of the |
| 95 | // ScrollPane |
| 96 | if (ins.right >= vertScrollWidth) { |
| 97 | if (log.isLoggable(Level.FINER)) { |
| 98 | log.log(Level.FINER, "using vertical scrolling because scrollbar is present"); |
| 99 | } |
| 100 | return sp.getVAdjustable(); |
| 101 | } |
| 102 | else { |
| 103 | int horizScrollHeight = sp.getHScrollbarHeight(); |
| 104 | if (ins.bottom >= horizScrollHeight) { |
| 105 | if (log.isLoggable(Level.FINER)) { |
| 106 | log.log(Level.FINER, "using horiz scrolling because scrollbar is present"); |
| 107 | } |
| 108 | return sp.getHAdjustable(); |
| 109 | } |
| 110 | else { |
| 111 | if (log.isLoggable(Level.FINER)) { |
| 112 | log.log(Level.FINER, "using NO scrollbar becsause neither is present"); |
| 113 | } |
| 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 | if (log.isLoggable(Level.FINE)) { |
| 128 | if (adj == null) { |
| 129 | log.log(Level.FINE, "Assertion (adj != null) failed"); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | int increment = 0; |
| 134 | |
| 135 | if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) { |
| 136 | increment = e.getUnitsToScroll() * adj.getUnitIncrement(); |
| 137 | } |
| 138 | else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) { |
| 139 | increment = adj.getBlockIncrement() * e.getWheelRotation(); |
| 140 | } |
| 141 | return increment; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Scroll the given Adjustable by the given amount. Checks the Adjustable's |
| 146 | * bounds and sets the new value to the Adjustable. |
| 147 | */ |
| 148 | public static void scrollAdjustable(Adjustable adj, int amount) { |
| 149 | if (log.isLoggable(Level.FINE)) { |
| 150 | if (adj == null) { |
| 151 | log.log(Level.FINE, "Assertion (adj != null) failed"); |
| 152 | } |
| 153 | if (amount == 0) { |
| 154 | log.log(Level.FINE, "Assertion (amount != 0) failed"); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | int current = adj.getValue(); |
| 159 | int upperLimit = adj.getMaximum() - adj.getVisibleAmount(); |
| 160 | if (log.isLoggable(Level.FINER)) { |
| 161 | log.log(Level.FINER, "doScrolling by " + amount); |
| 162 | } |
| 163 | |
| 164 | if (amount > 0 && current < upperLimit) { // still some room to scroll |
| 165 | // down |
| 166 | if (current + amount < upperLimit) { |
| 167 | adj.setValue(current + amount); |
| 168 | return; |
| 169 | } |
| 170 | else { |
| 171 | adj.setValue(upperLimit); |
| 172 | return; |
| 173 | } |
| 174 | } |
| 175 | else if (amount < 0 && current > adj.getMinimum()) { // still some room |
| 176 | // to scroll up |
| 177 | if (current + amount > adj.getMinimum()) { |
| 178 | adj.setValue(current + amount); |
| 179 | return; |
| 180 | } |
| 181 | else { |
| 182 | adj.setValue(adj.getMinimum()); |
| 183 | return; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |