1 /* Scrollbar.java -- AWT Scrollbar widget
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.peer.ScrollbarPeer;
42 import java.awt.peer.ComponentPeer;
44 import java.awt.event.AdjustmentListener;
45 import java.awt.event.AdjustmentEvent;
48 * This class implements a scrollbar widget.
50 * @author Aaron M. Renn (arenn@urbanophile.com)
51 * @author Tom Tromey <tromey@cygnus.com>
53 public class Scrollbar extends Component implements Adjustable,
57 // FIXME: Serialization readObject/writeObject
64 * Constant indicating that a scrollbar is horizontal.
66 public static final int HORIZONTAL = 0;
69 * Constant indicating that a scrollbar is vertical.
71 public static final int VERTICAL = 1;
73 // Serialization Constant
74 private static final long serialVersionUID = 8451667562882310543L;
76 /*************************************************************************/
79 * @serial The amount by which the value of the scrollbar is changed
80 * when incrementing in line mode.
82 private int lineIncrement;
85 * @serial The amount by which the value of the scrollbar is changed
86 * when incrementing in page mode.
88 private int pageIncrement;
91 * @serial The maximum value for this scrollbar
96 * @serial The minimum value for this scrollbar
101 * @serial The orientation of this scrollbar, which will be either
102 * the <code>HORIZONTAL</code> or <code>VERTICAL</code> constant
105 private int orientation;
108 * @serial The current value of this scrollbar.
113 * @serial The width of the scrollbar's thumb, which is relative
114 * to the minimum and maximum value of the scrollbar.
116 private int visibleAmount;
118 // List of AdjustmentListener's.
119 private AdjustmentListener adjustment_listeners;
121 /*************************************************************************/
128 * Initializes a new instance of <code>Scrollbar</code> with a
129 * veritical orientation and default values for all other parameters.
137 /*************************************************************************/
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
146 * @param orientation The orientation of this scrollbar.
148 * @exception IllegalArgumentException If the orientation value is not valid.
151 Scrollbar(int orientation) throws IllegalArgumentException
153 this(orientation, 0, 10, 0, 100);
156 /*************************************************************************/
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.
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.
171 * @exception IllegalArgumentException If the orientation value is not valid.
174 Scrollbar(int orientation, int value, int visibleAmount, int minimum,
175 int maximum) throws IllegalArgumentException
177 if ((orientation != HORIZONTAL) && (orientation != VERTICAL))
178 throw new IllegalArgumentException("Bad orientation value: "
181 this.orientation = orientation;
183 setValues(value, visibleAmount, minimum, maximum);
185 // Default is 1 according to online docs.
188 pageIncrement = (maximum - minimum) / 5;
189 if (pageIncrement == 0)
193 /*************************************************************************/
200 * Returns the orientation constant for this object.
202 * @return The orientation constant for this object.
210 /*************************************************************************/
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.
217 * @param orientation The new orientation value.
219 * @exception IllegalArgumentException If the orientation value is not valid.
222 setOrientation(int orientation)
224 if ((orientation != HORIZONTAL) && (orientation != VERTICAL))
225 throw new IllegalArgumentException("Bad orientation value: "
228 // FIXME: Communicate to peer? Or must this be called before peer creation?
229 this.orientation = orientation;
232 /*************************************************************************/
235 * Returns the current value for this scrollbar.
237 * @return The current value for this scrollbar.
245 /*************************************************************************/
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.
252 * @param value The new value for this scrollbar.
257 setValues(value, visibleAmount, minimum, maximum);
260 /*************************************************************************/
263 * Returns the maximum value for this scrollbar.
265 * @return The maximum value for this scrollbar.
273 /*************************************************************************/
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.
280 * @param maximum The new maximum value for this scrollbar.
283 setMaximum(int maximum)
285 setValues(value, visibleAmount, minimum, maximum);
288 /*************************************************************************/
291 * Returns the minimum value for this scrollbar.
293 * @return The minimum value for this scrollbar.
301 /*************************************************************************/
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.
308 * @param minimum The new minimum value for this scrollbar.
311 setMinimum(int minimum)
313 setValues(value, visibleAmount, minimum, maximum);
316 /*************************************************************************/
319 * Returns the width of the scrollbar's thumb, in units relative to the
320 * maximum and minimum value of the scrollbar.
322 * @return The width of the scrollbar's thumb.
327 return(visibleAmount);
330 /*************************************************************************/
333 * Returns the width of the scrollbar's thumb, in units relative to the
334 * maximum and minimum value of the scrollbar.
336 * @return The width of the scrollbar's thumb.
338 * @deprecated This method is deprecated in favor of
339 * <code>getVisibleAmount()</code>.
344 return(getVisibleAmount());
347 /*************************************************************************/
350 * Sets the width of the scrollbar's thumb, in units relative to the
351 * maximum and minimum value of the scrollbar.
353 * @param visibileAmount The new visible amount value of the scrollbar.
356 setVisibleAmount(int visibleAmount)
358 setValues(value, visibleAmount, minimum, maximum);
361 /*************************************************************************/
364 * Sets the current value, visible amount, minimum, and maximum for this
365 * scrollbar. These values are adjusted to be internally consistent
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.
373 public synchronized void
374 setValues(int value, int visibleAmount, int minimum, int maximum)
376 if (maximum < minimum)
385 if (visibleAmount > value)
386 visibleAmount = value;
389 this.visibleAmount = visibleAmount;
390 this.minimum = minimum;
391 this.maximum = maximum;
393 ScrollbarPeer sp = (ScrollbarPeer)getPeer();
395 sp.setValues(value, visibleAmount, minimum, maximum);
397 int range = maximum - minimum;
398 if (lineIncrement > range)
403 lineIncrement = range;
406 sp.setLineIncrement(lineIncrement);
409 if (pageIncrement > range)
414 pageIncrement = range;
417 sp.setPageIncrement(pageIncrement);
421 /*************************************************************************/
424 * Returns the value added or subtracted when the user activates the scrollbar
425 * scroll by a "unit" amount.
427 * @return The unit increment value.
432 return(lineIncrement);
435 /*************************************************************************/
438 * Returns the value added or subtracted when the user selects the scrollbar
439 * scroll by a "unit" amount control.
441 * @return The unit increment value.
443 * @deprecated This method is deprecated in favor of
444 * <code>getUnitIncrement()</code>.
449 return(lineIncrement);
452 /*************************************************************************/
455 * Sets the value added or subtracted to the scrollbar value when the
456 * user selects the scroll by a "unit" amount control.
458 * @param unitIncrement The new unit increment amount.
460 public synchronized void
461 setUnitIncrement(int unitIncrement)
463 if (unitIncrement < 0)
464 throw new IllegalArgumentException("Unit increment less than zero.");
466 int range = maximum - minimum;
467 if (unitIncrement > range)
472 unitIncrement = range;
475 if (unitIncrement == lineIncrement)
478 lineIncrement = unitIncrement;
480 ScrollbarPeer sp = (ScrollbarPeer)getPeer();
482 sp.setLineIncrement(lineIncrement);
485 /*************************************************************************/
488 * Sets the value added or subtracted to the scrollbar value when the
489 * user selects the scroll by a "unit" amount control.
491 * @param lineIncrement The new unit increment amount.
493 * @deprecated This method is deprecated in favor of
494 * <code>setUnitIncrement()</code>.
497 setLineIncrement(int lineIncrement)
499 setUnitIncrement(lineIncrement);
502 /*************************************************************************/
505 * Returns the value added or subtracted when the user activates the scrollbar
506 * scroll by a "block" amount.
508 * @return The block increment value.
513 return(pageIncrement);
516 /*************************************************************************/
519 * Returns the value added or subtracted when the user selects the scrollbar
520 * scroll by a "block" amount control.
522 * @return The block increment value.
524 * @deprecated This method is deprecated in favor of
525 * <code>getBlockIncrement()</code>.
530 return(pageIncrement);
533 /*************************************************************************/
536 * Sets the value added or subtracted to the scrollbar value when the
537 * user selects the scroll by a "block" amount control.
539 * @param blockIncrement The new block increment amount.
541 public synchronized void
542 setBlockIncrement(int blockIncrement)
544 if (blockIncrement < 0)
545 throw new IllegalArgumentException("Block increment less than zero.");
547 int range = maximum - minimum;
548 if (blockIncrement > range)
553 blockIncrement = range;
556 if (blockIncrement == pageIncrement)
559 pageIncrement = blockIncrement;
561 ScrollbarPeer sp = (ScrollbarPeer)getPeer();
563 sp.setPageIncrement(pageIncrement);
566 /*************************************************************************/
569 * Sets the value added or subtracted to the scrollbar value when the
570 * user selects the scroll by a "block" amount control.
572 * @param pageIncrement The new block increment amount.
574 * @deprecated This method is deprecated in favor of
575 * <code>setBlockIncrement()</code>.
578 setPageIncrement(int pageIncrement)
580 setBlockIncrement(pageIncrement);
583 /*************************************************************************/
586 * Notifies this object to create its native peer.
588 public synchronized void
592 peer = getToolkit ().createScrollbar (this);
596 /*************************************************************************/
599 * Adds a new adjustment listener to the list of registered listeners
602 * @param listener The listener to add.
604 public synchronized void
605 addAdjustmentListener(AdjustmentListener listener)
607 adjustment_listeners = AWTEventMulticaster.add(adjustment_listeners, listener);
608 enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK);
611 /*************************************************************************/
614 * Removes the specified listener from the list of registered listeners
617 * @param listener The listener to remove.
619 public synchronized void
620 removeAdjustmentListener(AdjustmentListener listener)
622 adjustment_listeners = AWTEventMulticaster.remove(adjustment_listeners,
626 /*************************************************************************/
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
634 * @param event The event to process.
637 processEvent(AWTEvent event)
639 if (event instanceof AdjustmentEvent)
640 processAdjustmentEvent((AdjustmentEvent)event);
642 super.processEvent(event);
645 /*************************************************************************/
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>.
654 * @param event The event to process.
657 processAdjustmentEvent(AdjustmentEvent event)
659 if (adjustment_listeners != null)
660 adjustment_listeners.adjustmentValueChanged(event);
664 dispatchEventImpl(AWTEvent e)
666 if (e.id <= AdjustmentEvent.ADJUSTMENT_LAST
667 && e.id >= AdjustmentEvent.ADJUSTMENT_FIRST
668 && (adjustment_listeners != null
669 || (eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0))
672 super.dispatchEventImpl(e);
675 /*************************************************************************/
678 * Returns a debugging string for this object.
680 * @return A debugging string for this object.
685 return("value=" + getValue() + ",visibleAmount=" +
686 getVisibleAmount() + ",minimum=" + getMinimum()
687 + ",maximum=" + getMaximum()
688 + ",pageIncrement=" + pageIncrement
689 + ",lineIncrement=" + lineIncrement
690 + ",orientation=" + (orientation == HORIZONTAL
691 ? "HORIZONTAL" : "VERTICAL")
692 + super.paramString());