source: trunk/src/gcc/libjava/java/awt/Scrollbar.java@ 736

Last change on this file since 736 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 17.9 KB
Line 
1/* Scrollbar.java -- AWT Scrollbar widget
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.peer.ScrollbarPeer;
42import java.awt.peer.ComponentPeer;
43
44import java.awt.event.AdjustmentListener;
45import java.awt.event.AdjustmentEvent;
46
47/**
48 * This class implements a scrollbar widget.
49 *
50 * @author Aaron M. Renn ([email protected])
51 * @author Tom Tromey <[email protected]>
52 */
53public class Scrollbar extends Component implements Adjustable,
54 java.io.Serializable
55{
56
57// FIXME: Serialization readObject/writeObject
58
59/*
60 * Static Variables
61 */
62
63/**
64 * Constant indicating that a scrollbar is horizontal.
65 */
66public static final int HORIZONTAL = 0;
67
68/**
69 * Constant indicating that a scrollbar is vertical.
70 */
71public static final int VERTICAL = 1;
72
73// Serialization Constant
74private static final long serialVersionUID = 8451667562882310543L;
75
76/*************************************************************************/
77
78/**
79 * @serial The amount by which the value of the scrollbar is changed
80 * when incrementing in line mode.
81 */
82private int lineIncrement;
83
84/**
85 * @serial The amount by which the value of the scrollbar is changed
86 * when incrementing in page mode.
87 */
88private int pageIncrement;
89
90/**
91 * @serial The maximum value for this scrollbar
92 */
93private int maximum;
94
95/**
96 * @serial The minimum value for this scrollbar
97 */
98private int minimum;
99
100/**
101 * @serial The orientation of this scrollbar, which will be either
102 * the <code>HORIZONTAL</code> or <code>VERTICAL</code> constant
103 * from this class.
104 */
105private int orientation;
106
107/**
108 * @serial The current value of this scrollbar.
109 */
110private int value;
111
112/**
113 * @serial The width of the scrollbar's thumb, which is relative
114 * to the minimum and maximum value of the scrollbar.
115 */
116private int visibleAmount;
117
118// List of AdjustmentListener's.
119private AdjustmentListener adjustment_listeners;
120
121/*************************************************************************/
122
123/*
124 * Constructors
125 */
126
127/**
128 * Initializes a new instance of <code>Scrollbar</code> with a
129 * veritical orientation and default values for all other parameters.
130 */
131public
132Scrollbar()
133{
134 this(VERTICAL);
135}
136
137/*************************************************************************/
138
139/**
140 * Initializes a new instance of <code>Scrollbar</code> with the
141 * specified orientation and default values for all other parameters.
142 * The orientation must be either the constant <code>HORIZONTAL</code> or
143 * <code>VERTICAL</code> from this class. An incorrect value will throw
144 * an exception.
145 *
146 * @param orientation The orientation of this scrollbar.
147 *
148 * @exception IllegalArgumentException If the orientation value is not valid.
149 */
150public
151Scrollbar(int orientation) throws IllegalArgumentException
152{
153 this(orientation, 0, 10, 0, 100);
154}
155
156/*************************************************************************/
157
158/**
159 * Initializes a new instance of <code>Scrollbar</code> with the
160 * specified parameters. The orientation must be either the constant
161 * <code>HORIZONTAL</code> or <code>VERTICAL</code>. An incorrect value
162 * will throw an exception. Inconsistent values for other parameters
163 * are silently corrected to valid values.
164 *
165 * @param orientation The orientation of this scrollbar.
166 * @param value The initial value of the scrollbar.
167 * @param visibleAmount The width of the scrollbar thumb.
168 * @param minimum The minimum value of the scrollbar.
169 * @param maximum The maximum value of the scrollbar.
170 *
171 * @exception IllegalArgumentException If the orientation value is not valid.
172 */
173public
174Scrollbar(int orientation, int value, int visibleAmount, int minimum,
175 int maximum) throws IllegalArgumentException
176{
177 if ((orientation != HORIZONTAL) && (orientation != VERTICAL))
178 throw new IllegalArgumentException("Bad orientation value: "
179 + orientation);
180
181 this.orientation = orientation;
182
183 setValues(value, visibleAmount, minimum, maximum);
184
185 // Default is 1 according to online docs.
186 lineIncrement = 1;
187
188 pageIncrement = (maximum - minimum) / 5;
189 if (pageIncrement == 0)
190 pageIncrement = 1;
191}
192
193/*************************************************************************/
194
195/*
196 * Instance Methods
197 */
198
199/**
200 * Returns the orientation constant for this object.
201 *
202 * @return The orientation constant for this object.
203 */
204public int
205getOrientation()
206{
207 return(orientation);
208}
209
210/*************************************************************************/
211
212/**
213 * Sets the orientation of this scrollbar to the specified value. This
214 * value must be either the constant <code>HORIZONTAL</code> or
215 * <code>VERTICAL</code> from this class or an exception will be thrown.
216 *
217 * @param orientation The new orientation value.
218 *
219 * @exception IllegalArgumentException If the orientation value is not valid.
220 */
221public void
222setOrientation(int orientation)
223{
224 if ((orientation != HORIZONTAL) && (orientation != VERTICAL))
225 throw new IllegalArgumentException("Bad orientation value: "
226 + orientation);
227
228 // FIXME: Communicate to peer? Or must this be called before peer creation?
229 this.orientation = orientation;
230}
231
232/*************************************************************************/
233
234/**
235 * Returns the current value for this scrollbar.
236 *
237 * @return The current value for this scrollbar.
238 */
239public int
240getValue()
241{
242 return(value);
243}
244
245/*************************************************************************/
246
247/**
248 * Sets the current value for this scrollbar to the specified value.
249 * If this is inconsistent with the minimum and maximum values for this
250 * scrollbar, the value is silently adjusted.
251 *
252 * @param value The new value for this scrollbar.
253 */
254public void
255setValue(int value)
256{
257 setValues(value, visibleAmount, minimum, maximum);
258}
259
260/*************************************************************************/
261
262/**
263 * Returns the maximum value for this scrollbar.
264 *
265 * @return The maximum value for this scrollbar.
266 */
267public int
268getMaximum()
269{
270 return(maximum);
271}
272
273/*************************************************************************/
274
275/**
276 * Sets the maximum value for this scrollbar to the specified value.
277 * If the value is less than the current minimum value, it is silent
278 * set to equal the minimum value.
279 *
280 * @param maximum The new maximum value for this scrollbar.
281 */
282public void
283setMaximum(int maximum)
284{
285 setValues(value, visibleAmount, minimum, maximum);
286}
287
288/*************************************************************************/
289
290/**
291 * Returns the minimum value for this scrollbar.
292 *
293 * @return The minimum value for this scrollbar.
294 */
295public int
296getMinimum()
297{
298 return(minimum);
299}
300
301/*************************************************************************/
302
303/**
304 * Sets the minimum value for this scrollbar to the specified value. If
305 * this is not consistent with the current value and maximum, it is
306 * silently adjusted to be consistent.
307 *
308 * @param minimum The new minimum value for this scrollbar.
309 */
310public void
311setMinimum(int minimum)
312{
313 setValues(value, visibleAmount, minimum, maximum);
314}
315
316/*************************************************************************/
317
318/**
319 * Returns the width of the scrollbar's thumb, in units relative to the
320 * maximum and minimum value of the scrollbar.
321 *
322 * @return The width of the scrollbar's thumb.
323 */
324public int
325getVisibleAmount()
326{
327 return(visibleAmount);
328}
329
330/*************************************************************************/
331
332/**
333 * Returns the width of the scrollbar's thumb, in units relative to the
334 * maximum and minimum value of the scrollbar.
335 *
336 * @return The width of the scrollbar's thumb.
337 *
338 * @deprecated This method is deprecated in favor of
339 * <code>getVisibleAmount()</code>.
340 */
341public int
342getVisible()
343{
344 return(getVisibleAmount());
345}
346
347/*************************************************************************/
348
349/**
350 * Sets the width of the scrollbar's thumb, in units relative to the
351 * maximum and minimum value of the scrollbar.
352 *
353 * @param visibileAmount The new visible amount value of the scrollbar.
354 */
355public void
356setVisibleAmount(int visibleAmount)
357{
358 setValues(value, visibleAmount, minimum, maximum);
359}
360
361/*************************************************************************/
362
363/**
364 * Sets the current value, visible amount, minimum, and maximum for this
365 * scrollbar. These values are adjusted to be internally consistent
366 * if necessary.
367 *
368 * @param value The new value for this scrollbar.
369 * @param visibleAmount The new visible amount for this scrollbar.
370 * @param minimum The new minimum value for this scrollbar.
371 * @param maximum The new maximum value for this scrollbar.
372 */
373public synchronized void
374setValues(int value, int visibleAmount, int minimum, int maximum)
375{
376 if (maximum < minimum)
377 maximum = minimum;
378
379 if (value < minimum)
380 value = minimum;
381
382 if (value > maximum)
383 value = maximum;
384
385 if (visibleAmount > value)
386 visibleAmount = value;
387
388 this.value = value;
389 this.visibleAmount = visibleAmount;
390 this.minimum = minimum;
391 this.maximum = maximum;
392
393 ScrollbarPeer sp = (ScrollbarPeer)getPeer();
394 if (sp != null)
395 sp.setValues(value, visibleAmount, minimum, maximum);
396
397 int range = maximum - minimum;
398 if (lineIncrement > range)
399 {
400 if (range == 0)
401 lineIncrement = 1;
402 else
403 lineIncrement = range;
404
405 if (sp != null)
406 sp.setLineIncrement(lineIncrement);
407 }
408
409 if (pageIncrement > range)
410 {
411 if (range == 0)
412 pageIncrement = 1;
413 else
414 pageIncrement = range;
415
416 if (sp != null)
417 sp.setPageIncrement(pageIncrement);
418 }
419}
420
421/*************************************************************************/
422
423/**
424 * Returns the value added or subtracted when the user activates the scrollbar
425 * scroll by a "unit" amount.
426 *
427 * @return The unit increment value.
428 */
429public int
430getUnitIncrement()
431{
432 return(lineIncrement);
433}
434
435/*************************************************************************/
436
437/**
438 * Returns the value added or subtracted when the user selects the scrollbar
439 * scroll by a "unit" amount control.
440 *
441 * @return The unit increment value.
442 *
443 * @deprecated This method is deprecated in favor of
444 * <code>getUnitIncrement()</code>.
445 */
446public int
447getLineIncrement()
448{
449 return(lineIncrement);
450}
451
452/*************************************************************************/
453
454/**
455 * Sets the value added or subtracted to the scrollbar value when the
456 * user selects the scroll by a "unit" amount control.
457 *
458 * @param unitIncrement The new unit increment amount.
459 */
460public synchronized void
461setUnitIncrement(int unitIncrement)
462{
463 if (unitIncrement < 0)
464 throw new IllegalArgumentException("Unit increment less than zero.");
465
466 int range = maximum - minimum;
467 if (unitIncrement > range)
468 {
469 if (range == 0)
470 unitIncrement = 1;
471 else
472 unitIncrement = range;
473 }
474
475 if (unitIncrement == lineIncrement)
476 return;
477
478 lineIncrement = unitIncrement;
479
480 ScrollbarPeer sp = (ScrollbarPeer)getPeer();
481 if (sp != null)
482 sp.setLineIncrement(lineIncrement);
483}
484
485/*************************************************************************/
486
487/**
488 * Sets the value added or subtracted to the scrollbar value when the
489 * user selects the scroll by a "unit" amount control.
490 *
491 * @param lineIncrement The new unit increment amount.
492 *
493 * @deprecated This method is deprecated in favor of
494 * <code>setUnitIncrement()</code>.
495 */
496public void
497setLineIncrement(int lineIncrement)
498{
499 setUnitIncrement(lineIncrement);
500}
501
502/*************************************************************************/
503
504/**
505 * Returns the value added or subtracted when the user activates the scrollbar
506 * scroll by a "block" amount.
507 *
508 * @return The block increment value.
509 */
510public int
511getBlockIncrement()
512{
513 return(pageIncrement);
514}
515
516/*************************************************************************/
517
518/**
519 * Returns the value added or subtracted when the user selects the scrollbar
520 * scroll by a "block" amount control.
521 *
522 * @return The block increment value.
523 *
524 * @deprecated This method is deprecated in favor of
525 * <code>getBlockIncrement()</code>.
526 */
527public int
528getPageIncrement()
529{
530 return(pageIncrement);
531}
532
533/*************************************************************************/
534
535/**
536 * Sets the value added or subtracted to the scrollbar value when the
537 * user selects the scroll by a "block" amount control.
538 *
539 * @param blockIncrement The new block increment amount.
540 */
541public synchronized void
542setBlockIncrement(int blockIncrement)
543{
544 if (blockIncrement < 0)
545 throw new IllegalArgumentException("Block increment less than zero.");
546
547 int range = maximum - minimum;
548 if (blockIncrement > range)
549 {
550 if (range == 0)
551 blockIncrement = 1;
552 else
553 blockIncrement = range;
554 }
555
556 if (blockIncrement == pageIncrement)
557 return;
558
559 pageIncrement = blockIncrement;
560
561 ScrollbarPeer sp = (ScrollbarPeer)getPeer();
562 if (sp != null)
563 sp.setPageIncrement(pageIncrement);
564}
565
566/*************************************************************************/
567
568/**
569 * Sets the value added or subtracted to the scrollbar value when the
570 * user selects the scroll by a "block" amount control.
571 *
572 * @param pageIncrement The new block increment amount.
573 *
574 * @deprecated This method is deprecated in favor of
575 * <code>setBlockIncrement()</code>.
576 */
577public void
578setPageIncrement(int pageIncrement)
579{
580 setBlockIncrement(pageIncrement);
581}
582
583/*************************************************************************/
584
585/**
586 * Notifies this object to create its native peer.
587 */
588public synchronized void
589addNotify()
590{
591 if (peer == null)
592 peer = getToolkit ().createScrollbar (this);
593 super.addNotify ();
594}
595
596/*************************************************************************/
597
598/**
599 * Adds a new adjustment listener to the list of registered listeners
600 * for this object.
601 *
602 * @param listener The listener to add.
603 */
604public synchronized void
605addAdjustmentListener(AdjustmentListener listener)
606{
607 adjustment_listeners = AWTEventMulticaster.add(adjustment_listeners, listener);
608 enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK);
609}
610
611/*************************************************************************/
612
613/**
614 * Removes the specified listener from the list of registered listeners
615 * for this object.
616 *
617 * @param listener The listener to remove.
618 */
619public synchronized void
620removeAdjustmentListener(AdjustmentListener listener)
621{
622 adjustment_listeners = AWTEventMulticaster.remove(adjustment_listeners,
623 listener);
624}
625
626/*************************************************************************/
627
628/**
629 * Processes events for this scrollbar. It does this by calling
630 * <code>processAdjustmentEvent()</code> if the event is an instance of
631 * <code>AdjustmentEvent</code>, otherwise it calls the superclass to
632 * process the event.
633 *
634 * @param event The event to process.
635 */
636protected void
637processEvent(AWTEvent event)
638{
639 if (event instanceof AdjustmentEvent)
640 processAdjustmentEvent((AdjustmentEvent)event);
641 else
642 super.processEvent(event);
643}
644
645/*************************************************************************/
646
647/**
648 * Processes adjustment events for this object by dispatching them to
649 * any registered listeners. Note that this method will only be called
650 * if adjustment events are enabled. This will happen automatically if
651 * any listeners are registered. Otherwise, it can be enabled by a
652 * call to <code>enableEvents()</code>.
653 *
654 * @param event The event to process.
655 */
656protected void
657processAdjustmentEvent(AdjustmentEvent event)
658{
659 if (adjustment_listeners != null)
660 adjustment_listeners.adjustmentValueChanged(event);
661}
662
663/*************************************************************************/
664
665/**
666 * Returns a debugging string for this object.
667 *
668 * @return A debugging string for this object.
669 */
670protected String
671paramString()
672{
673 return("value=" + getValue() + ",visibleAmount=" +
674 getVisibleAmount() + ",minimum=" + getMinimum()
675 + ",maximum=" + getMaximum()
676 + ",pageIncrement=" + pageIncrement
677 + ",lineIncrement=" + lineIncrement
678 + ",orientation=" + (orientation == HORIZONTAL
679 ? "HORIZONTAL" : "VERTICAL")
680 + super.paramString());
681}
682
683} // class Scrollbar
684
Note: See TracBrowser for help on using the repository browser.