author | mchung |
Tue, 29 Sep 2009 16:03:03 -0700 | |
changeset 3938 | ef327bd847c0 |
parent 2 | 90ce3da70b43 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 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 |
||
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
33 |
import sun.util.logging.PlatformLogger; |
2 | 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 |
||
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
42 |
private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.ScrollPaneWheelScroller"); |
2 | 43 |
|
44 |
private ScrollPaneWheelScroller() {} |
|
45 |
||
46 |
/* |
|
47 |
* Called from ScrollPane.processMouseWheelEvent() |
|
48 |
*/ |
|
49 |
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
50 |
if (log.isLoggable(PlatformLogger.FINER)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
51 |
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource()); |
2 | 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); |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
59 |
if (log.isLoggable(PlatformLogger.FINER)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
60 |
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment); |
2 | 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) { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
77 |
if (log.isLoggable(PlatformLogger.FINER)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
78 |
log.finer("using vertical scrolling due to scrollbar policy"); |
2 | 79 |
} |
80 |
return sp.getVAdjustable(); |
|
81 |
||
82 |
} |
|
83 |
else { |
|
84 |
||
85 |
Insets ins = sp.getInsets(); |
|
86 |
int vertScrollWidth = sp.getVScrollbarWidth(); |
|
87 |
||
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
88 |
if (log.isLoggable(PlatformLogger.FINER)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
89 |
log.finer("insets: l = " + ins.left + ", r = " + ins.right + |
2 | 90 |
", t = " + ins.top + ", b = " + ins.bottom); |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
91 |
log.finer("vertScrollWidth = " + vertScrollWidth); |
2 | 92 |
} |
93 |
||
94 |
// Check if scrollbar is showing by examining insets of the |
|
95 |
// ScrollPane |
|
96 |
if (ins.right >= vertScrollWidth) { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
97 |
if (log.isLoggable(PlatformLogger.FINER)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
98 |
log.finer("using vertical scrolling because scrollbar is present"); |
2 | 99 |
} |
100 |
return sp.getVAdjustable(); |
|
101 |
} |
|
102 |
else { |
|
103 |
int horizScrollHeight = sp.getHScrollbarHeight(); |
|
104 |
if (ins.bottom >= horizScrollHeight) { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
105 |
if (log.isLoggable(PlatformLogger.FINER)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
106 |
log.finer("using horiz scrolling because scrollbar is present"); |
2 | 107 |
} |
108 |
return sp.getHAdjustable(); |
|
109 |
} |
|
110 |
else { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
111 |
if (log.isLoggable(PlatformLogger.FINER)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
112 |
log.finer("using NO scrollbar becsause neither is present"); |
2 | 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) { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
127 |
if (log.isLoggable(PlatformLogger.FINE)) { |
2 | 128 |
if (adj == null) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
129 |
log.fine("Assertion (adj != null) failed"); |
2 | 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) { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
149 |
if (log.isLoggable(PlatformLogger.FINE)) { |
2 | 150 |
if (adj == null) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
151 |
log.fine("Assertion (adj != null) failed"); |
2 | 152 |
} |
153 |
if (amount == 0) { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
154 |
log.fine("Assertion (amount != 0) failed"); |
2 | 155 |
} |
156 |
} |
|
157 |
||
158 |
int current = adj.getValue(); |
|
159 |
int upperLimit = adj.getMaximum() - adj.getVisibleAmount(); |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
160 |
if (log.isLoggable(PlatformLogger.FINER)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
161 |
log.finer("doScrolling by " + amount); |
2 | 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 |
} |