author | avstepan |
Mon, 11 Jan 2016 17:51:54 +0300 | |
changeset 35667 | ed476aba94de |
parent 26749 | b6598aa90114 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22584
eed64ee05369
8032733: Fix cast lint warnings in client libraries
darcy
parents:
21278
diff
changeset
|
2 |
* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package java.awt; |
|
26 |
||
10097
67449ffe5991
6642728: Use reflection to access ScrollPane's private method from within sun.awt package
serb
parents:
5506
diff
changeset
|
27 |
import sun.awt.AWTAccessor; |
67449ffe5991
6642728: Use reflection to access ScrollPane's private method from within sun.awt package
serb
parents:
5506
diff
changeset
|
28 |
|
2 | 29 |
import java.awt.event.AdjustmentEvent; |
30 |
import java.awt.event.AdjustmentListener; |
|
31 |
import java.awt.peer.ScrollPanePeer; |
|
32 |
import java.io.Serializable; |
|
33 |
||
34 |
||
35 |
/** |
|
36 |
* This class represents the state of a horizontal or vertical |
|
35667 | 37 |
* scrollbar of a {@code ScrollPane}. Objects of this class are |
38 |
* returned by {@code ScrollPane} methods. |
|
2 | 39 |
* |
40 |
* @since 1.4 |
|
41 |
*/ |
|
42 |
public class ScrollPaneAdjustable implements Adjustable, Serializable { |
|
43 |
||
44 |
/** |
|
35667 | 45 |
* The {@code ScrollPane} this object is a scrollbar of. |
2 | 46 |
* @serial |
47 |
*/ |
|
48 |
private ScrollPane sp; |
|
49 |
||
50 |
/** |
|
51 |
* Orientation of this scrollbar. |
|
52 |
* |
|
53 |
* @serial |
|
54 |
* @see #getOrientation |
|
55 |
* @see java.awt.Adjustable#HORIZONTAL |
|
56 |
* @see java.awt.Adjustable#VERTICAL |
|
57 |
*/ |
|
58 |
private int orientation; |
|
59 |
||
60 |
/** |
|
61 |
* The value of this scrollbar. |
|
35667 | 62 |
* {@code value} should be greater than {@code minimum} |
63 |
* and less than {@code maximum} |
|
2 | 64 |
* |
65 |
* @serial |
|
66 |
* @see #getValue |
|
67 |
* @see #setValue |
|
68 |
*/ |
|
69 |
private int value; |
|
70 |
||
71 |
/** |
|
72 |
* The minimum value of this scrollbar. |
|
35667 | 73 |
* This value can only be set by the {@code ScrollPane}. |
2 | 74 |
* <p> |
75 |
* <strong>ATTN:</strong> In current implementation |
|
35667 | 76 |
* {@code minimum} is always {@code 0}. This field can |
77 |
* only be altered via {@code setSpan} method and |
|
78 |
* {@code ScrollPane} always calls that method with |
|
79 |
* {@code 0} for the minimum. {@code getMinimum} method |
|
80 |
* always returns {@code 0} without checking this field. |
|
2 | 81 |
* |
82 |
* @serial |
|
83 |
* @see #getMinimum |
|
84 |
* @see #setSpan(int, int, int) |
|
85 |
*/ |
|
86 |
private int minimum; |
|
87 |
||
88 |
/** |
|
89 |
* The maximum value of this scrollbar. |
|
35667 | 90 |
* This value can only be set by the {@code ScrollPane}. |
2 | 91 |
* |
92 |
* @serial |
|
93 |
* @see #getMaximum |
|
94 |
* @see #setSpan(int, int, int) |
|
95 |
*/ |
|
96 |
private int maximum; |
|
97 |
||
98 |
/** |
|
99 |
* The size of the visible portion of this scrollbar. |
|
35667 | 100 |
* This value can only be set by the {@code ScrollPane}. |
2 | 101 |
* |
102 |
* @serial |
|
103 |
* @see #getVisibleAmount |
|
104 |
* @see #setSpan(int, int, int) |
|
105 |
*/ |
|
106 |
private int visibleAmount; |
|
107 |
||
108 |
/** |
|
35667 | 109 |
* The adjusting status of the {@code Scrollbar}. |
2 | 110 |
* True if the value is in the process of changing as a result of |
111 |
* actions being taken by the user. |
|
112 |
* |
|
113 |
* @see #getValueIsAdjusting |
|
114 |
* @see #setValueIsAdjusting |
|
115 |
* @since 1.4 |
|
116 |
*/ |
|
117 |
private transient boolean isAdjusting; |
|
118 |
||
119 |
/** |
|
120 |
* The amount by which the scrollbar value will change when going |
|
121 |
* up or down by a line. |
|
122 |
* This value should be a non negative integer. |
|
123 |
* |
|
124 |
* @serial |
|
125 |
* @see #getUnitIncrement |
|
126 |
* @see #setUnitIncrement |
|
127 |
*/ |
|
128 |
private int unitIncrement = 1; |
|
129 |
||
130 |
/** |
|
131 |
* The amount by which the scrollbar value will change when going |
|
132 |
* up or down by a page. |
|
133 |
* This value should be a non negative integer. |
|
134 |
* |
|
135 |
* @serial |
|
136 |
* @see #getBlockIncrement |
|
137 |
* @see #setBlockIncrement |
|
138 |
*/ |
|
139 |
private int blockIncrement = 1; |
|
140 |
||
141 |
private AdjustmentListener adjustmentListener; |
|
142 |
||
143 |
/** |
|
35667 | 144 |
* Error message for {@code AWTError} reported when one of |
2 | 145 |
* the public but unsupported methods is called. |
146 |
*/ |
|
147 |
private static final String SCROLLPANE_ONLY = |
|
148 |
"Can be set by scrollpane only"; |
|
149 |
||
150 |
||
151 |
/** |
|
152 |
* Initialize JNI field and method ids. |
|
153 |
*/ |
|
154 |
private static native void initIDs(); |
|
155 |
||
156 |
static { |
|
157 |
Toolkit.loadLibraries(); |
|
158 |
if (!GraphicsEnvironment.isHeadless()) { |
|
159 |
initIDs(); |
|
160 |
} |
|
10097
67449ffe5991
6642728: Use reflection to access ScrollPane's private method from within sun.awt package
serb
parents:
5506
diff
changeset
|
161 |
AWTAccessor.setScrollPaneAdjustableAccessor(new AWTAccessor.ScrollPaneAdjustableAccessor() { |
67449ffe5991
6642728: Use reflection to access ScrollPane's private method from within sun.awt package
serb
parents:
5506
diff
changeset
|
162 |
public void setTypedValue(final ScrollPaneAdjustable adj, |
67449ffe5991
6642728: Use reflection to access ScrollPane's private method from within sun.awt package
serb
parents:
5506
diff
changeset
|
163 |
final int v, final int type) { |
67449ffe5991
6642728: Use reflection to access ScrollPane's private method from within sun.awt package
serb
parents:
5506
diff
changeset
|
164 |
adj.setTypedValue(v, type); |
67449ffe5991
6642728: Use reflection to access ScrollPane's private method from within sun.awt package
serb
parents:
5506
diff
changeset
|
165 |
} |
67449ffe5991
6642728: Use reflection to access ScrollPane's private method from within sun.awt package
serb
parents:
5506
diff
changeset
|
166 |
}); |
2 | 167 |
} |
168 |
||
169 |
/** |
|
170 |
* JDK 1.1 serialVersionUID. |
|
171 |
*/ |
|
172 |
private static final long serialVersionUID = -3359745691033257079L; |
|
173 |
||
174 |
||
175 |
/** |
|
176 |
* Constructs a new object to represent specified scrollabar |
|
35667 | 177 |
* of the specified {@code ScrollPane}. |
2 | 178 |
* Only ScrollPane creates instances of this class. |
35667 | 179 |
* @param sp {@code ScrollPane} |
180 |
* @param l {@code AdjustmentListener} to add upon creation. |
|
2 | 181 |
* @param orientation specifies which scrollbar this object represents, |
35667 | 182 |
* can be either {@code Adjustable.HORIZONTAL} |
183 |
* or {@code Adjustable.VERTICAL}. |
|
2 | 184 |
*/ |
185 |
ScrollPaneAdjustable(ScrollPane sp, AdjustmentListener l, int orientation) { |
|
186 |
this.sp = sp; |
|
187 |
this.orientation = orientation; |
|
188 |
addAdjustmentListener(l); |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* This is called by the scrollpane itself to update the |
|
35667 | 193 |
* {@code minimum}, {@code maximum} and |
194 |
* {@code visible} values. The scrollpane is the only one |
|
2 | 195 |
* that should be changing these since it is the source of these |
196 |
* values. |
|
197 |
*/ |
|
198 |
void setSpan(int min, int max, int visible) { |
|
199 |
// adjust the values to be reasonable |
|
200 |
minimum = min; |
|
201 |
maximum = Math.max(max, minimum + 1); |
|
202 |
visibleAmount = Math.min(visible, maximum - minimum); |
|
203 |
visibleAmount = Math.max(visibleAmount, 1); |
|
204 |
blockIncrement = Math.max((int)(visible * .90), 1); |
|
205 |
setValue(value); |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Returns the orientation of this scrollbar. |
|
210 |
* @return the orientation of this scrollbar, either |
|
35667 | 211 |
* {@code Adjustable.HORIZONTAL} or |
212 |
* {@code Adjustable.VERTICAL} |
|
2 | 213 |
*/ |
214 |
public int getOrientation() { |
|
215 |
return orientation; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* This method should <strong>NOT</strong> be called by user code. |
|
220 |
* This method is public for this class to properly implement |
|
35667 | 221 |
* {@code Adjustable} interface. |
2 | 222 |
* |
20172 | 223 |
* @throws AWTError Always throws an error when called. |
2 | 224 |
*/ |
225 |
public void setMinimum(int min) { |
|
226 |
throw new AWTError(SCROLLPANE_ONLY); |
|
227 |
} |
|
228 |
||
229 |
public int getMinimum() { |
|
230 |
// XXX: This relies on setSpan always being called with 0 for |
|
231 |
// the minimum (which is currently true). |
|
232 |
return 0; |
|
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* This method should <strong>NOT</strong> be called by user code. |
|
237 |
* This method is public for this class to properly implement |
|
35667 | 238 |
* {@code Adjustable} interface. |
2 | 239 |
* |
20172 | 240 |
* @throws AWTError Always throws an error when called. |
2 | 241 |
*/ |
242 |
public void setMaximum(int max) { |
|
243 |
throw new AWTError(SCROLLPANE_ONLY); |
|
244 |
} |
|
245 |
||
246 |
public int getMaximum() { |
|
247 |
return maximum; |
|
248 |
} |
|
249 |
||
250 |
public synchronized void setUnitIncrement(int u) { |
|
251 |
if (u != unitIncrement) { |
|
252 |
unitIncrement = u; |
|
253 |
if (sp.peer != null) { |
|
254 |
ScrollPanePeer peer = (ScrollPanePeer) sp.peer; |
|
255 |
peer.setUnitIncrement(this, u); |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
public int getUnitIncrement() { |
|
261 |
return unitIncrement; |
|
262 |
} |
|
263 |
||
264 |
public synchronized void setBlockIncrement(int b) { |
|
265 |
blockIncrement = b; |
|
266 |
} |
|
267 |
||
268 |
public int getBlockIncrement() { |
|
269 |
return blockIncrement; |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* This method should <strong>NOT</strong> be called by user code. |
|
274 |
* This method is public for this class to properly implement |
|
35667 | 275 |
* {@code Adjustable} interface. |
2 | 276 |
* |
20172 | 277 |
* @throws AWTError Always throws an error when called. |
2 | 278 |
*/ |
279 |
public void setVisibleAmount(int v) { |
|
280 |
throw new AWTError(SCROLLPANE_ONLY); |
|
281 |
} |
|
282 |
||
283 |
public int getVisibleAmount() { |
|
284 |
return visibleAmount; |
|
285 |
} |
|
286 |
||
287 |
||
288 |
/** |
|
35667 | 289 |
* Sets the {@code valueIsAdjusting} property. |
2 | 290 |
* |
291 |
* @param b new adjustment-in-progress status |
|
292 |
* @see #getValueIsAdjusting |
|
293 |
* @since 1.4 |
|
294 |
*/ |
|
295 |
public void setValueIsAdjusting(boolean b) { |
|
296 |
if (isAdjusting != b) { |
|
297 |
isAdjusting = b; |
|
298 |
AdjustmentEvent e = |
|
299 |
new AdjustmentEvent(this, |
|
300 |
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, |
|
301 |
AdjustmentEvent.TRACK, value, b); |
|
302 |
adjustmentListener.adjustmentValueChanged(e); |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* Returns true if the value is in the process of changing as a |
|
308 |
* result of actions being taken by the user. |
|
309 |
* |
|
35667 | 310 |
* @return the value of the {@code valueIsAdjusting} property |
2 | 311 |
* @see #setValueIsAdjusting |
312 |
*/ |
|
313 |
public boolean getValueIsAdjusting() { |
|
314 |
return isAdjusting; |
|
315 |
} |
|
316 |
||
317 |
/** |
|
318 |
* Sets the value of this scrollbar to the specified value. |
|
319 |
* <p> |
|
320 |
* If the value supplied is less than the current minimum or |
|
321 |
* greater than the current maximum, then one of those values is |
|
322 |
* substituted, as appropriate. |
|
323 |
* |
|
324 |
* @param v the new value of the scrollbar |
|
325 |
*/ |
|
326 |
public void setValue(int v) { |
|
327 |
setTypedValue(v, AdjustmentEvent.TRACK); |
|
328 |
} |
|
329 |
||
330 |
/** |
|
331 |
* Sets the value of this scrollbar to the specified value. |
|
332 |
* <p> |
|
333 |
* If the value supplied is less than the current minimum or |
|
334 |
* greater than the current maximum, then one of those values is |
|
335 |
* substituted, as appropriate. Also, creates and dispatches |
|
26749 | 336 |
* the AdjustmentEvent with specified type and value. |
2 | 337 |
* |
338 |
* @param v the new value of the scrollbar |
|
21278 | 339 |
* @param type the type of the scrolling operation occurred |
2 | 340 |
*/ |
341 |
private void setTypedValue(int v, int type) { |
|
342 |
v = Math.max(v, minimum); |
|
343 |
v = Math.min(v, maximum - visibleAmount); |
|
344 |
||
345 |
if (v != value) { |
|
346 |
value = v; |
|
347 |
// Synchronously notify the listeners so that they are |
|
348 |
// guaranteed to be up-to-date with the Adjustable before |
|
349 |
// it is mutated again. |
|
350 |
AdjustmentEvent e = |
|
351 |
new AdjustmentEvent(this, |
|
352 |
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, |
|
353 |
type, value, isAdjusting); |
|
354 |
adjustmentListener.adjustmentValueChanged(e); |
|
355 |
} |
|
356 |
} |
|
357 |
||
358 |
public int getValue() { |
|
359 |
return value; |
|
360 |
} |
|
361 |
||
362 |
/** |
|
363 |
* Adds the specified adjustment listener to receive adjustment |
|
35667 | 364 |
* events from this {@code ScrollPaneAdjustable}. |
365 |
* If {@code l} is {@code null}, no exception is thrown |
|
2 | 366 |
* and no action is performed. |
367 |
* <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" |
|
368 |
* >AWT Threading Issues</a> for details on AWT's threading model. |
|
369 |
* |
|
370 |
* @param l the adjustment listener. |
|
371 |
* @see #removeAdjustmentListener |
|
372 |
* @see #getAdjustmentListeners |
|
373 |
* @see java.awt.event.AdjustmentListener |
|
374 |
* @see java.awt.event.AdjustmentEvent |
|
375 |
*/ |
|
376 |
public synchronized void addAdjustmentListener(AdjustmentListener l) { |
|
377 |
if (l == null) { |
|
378 |
return; |
|
379 |
} |
|
380 |
adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l); |
|
381 |
} |
|
382 |
||
383 |
/** |
|
384 |
* Removes the specified adjustment listener so that it no longer |
|
35667 | 385 |
* receives adjustment events from this {@code ScrollPaneAdjustable}. |
386 |
* If {@code l} is {@code null}, no exception is thrown |
|
2 | 387 |
* and no action is performed. |
388 |
* <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" |
|
389 |
* >AWT Threading Issues</a> for details on AWT's threading model. |
|
390 |
* |
|
391 |
* @param l the adjustment listener. |
|
392 |
* @see #addAdjustmentListener |
|
393 |
* @see #getAdjustmentListeners |
|
394 |
* @see java.awt.event.AdjustmentListener |
|
395 |
* @see java.awt.event.AdjustmentEvent |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
22584
diff
changeset
|
396 |
* @since 1.1 |
2 | 397 |
*/ |
398 |
public synchronized void removeAdjustmentListener(AdjustmentListener l){ |
|
399 |
if (l == null) { |
|
400 |
return; |
|
401 |
} |
|
402 |
adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l); |
|
403 |
} |
|
404 |
||
405 |
/** |
|
406 |
* Returns an array of all the adjustment listeners |
|
35667 | 407 |
* registered on this {@code ScrollPaneAdjustable}. |
2 | 408 |
* |
35667 | 409 |
* @return all of this {@code ScrollPaneAdjustable}'s |
410 |
* {@code AdjustmentListener}s |
|
2 | 411 |
* or an empty array if no adjustment |
412 |
* listeners are currently registered |
|
413 |
* |
|
414 |
* @see #addAdjustmentListener |
|
415 |
* @see #removeAdjustmentListener |
|
416 |
* @see java.awt.event.AdjustmentListener |
|
417 |
* @see java.awt.event.AdjustmentEvent |
|
418 |
* @since 1.4 |
|
419 |
*/ |
|
420 |
public synchronized AdjustmentListener[] getAdjustmentListeners() { |
|
22584
eed64ee05369
8032733: Fix cast lint warnings in client libraries
darcy
parents:
21278
diff
changeset
|
421 |
return AWTEventMulticaster.getListeners(adjustmentListener, |
eed64ee05369
8032733: Fix cast lint warnings in client libraries
darcy
parents:
21278
diff
changeset
|
422 |
AdjustmentListener.class); |
2 | 423 |
} |
424 |
||
425 |
/** |
|
426 |
* Returns a string representation of this scrollbar and its values. |
|
427 |
* @return a string representation of this scrollbar. |
|
428 |
*/ |
|
429 |
public String toString() { |
|
430 |
return getClass().getName() + "[" + paramString() + "]"; |
|
431 |
} |
|
432 |
||
433 |
/** |
|
434 |
* Returns a string representing the state of this scrollbar. |
|
435 |
* This method is intended to be used only for debugging purposes, |
|
436 |
* and the content and format of the returned string may vary |
|
437 |
* between implementations. The returned string may be empty but |
|
35667 | 438 |
* may not be {@code null}. |
2 | 439 |
* |
440 |
* @return the parameter string of this scrollbar. |
|
441 |
*/ |
|
442 |
public String paramString() { |
|
443 |
return ((orientation == Adjustable.VERTICAL ? "vertical," |
|
444 |
:"horizontal,") |
|
445 |
+ "[0.."+maximum+"]" |
|
446 |
+ ",val=" + value |
|
447 |
+ ",vis=" + visibleAmount |
|
448 |
+ ",unit=" + unitIncrement |
|
449 |
+ ",block=" + blockIncrement |
|
450 |
+ ",isAdjusting=" + isAdjusting); |
|
451 |
} |
|
452 |
} |