1 /* Scrollbar.java -- AWT Scrollbar widget
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import java
.awt
.event
.AdjustmentEvent
;
43 import java
.awt
.event
.AdjustmentListener
;
44 import java
.awt
.peer
.ScrollbarPeer
;
45 import java
.util
.EventListener
;
47 import javax
.accessibility
.Accessible
;
48 import javax
.accessibility
.AccessibleContext
;
49 import javax
.accessibility
.AccessibleRole
;
50 import javax
.accessibility
.AccessibleState
;
51 import javax
.accessibility
.AccessibleStateSet
;
52 import javax
.accessibility
.AccessibleValue
;
55 * This class implements a scrollbar widget.
57 * @author Aaron M. Renn (arenn@urbanophile.com)
58 * @author Tom Tromey (tromey@cygnus.com)
59 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
61 public class Scrollbar
extends Component
implements Accessible
, Adjustable
63 // FIXME: Serialization readObject/writeObject
66 * Constant indicating that a scrollbar is horizontal.
68 public static final int HORIZONTAL
= 0;
71 * Constant indicating that a scrollbar is vertical.
73 public static final int VERTICAL
= 1;
76 * Serialization Constant.
78 private static final long serialVersionUID
= 8451667562882310543L;
81 * @serial The amount by which the value of the scrollbar is changed
82 * when incrementing in line mode.
84 private int lineIncrement
;
87 * @serial The amount by which the value of the scrollbar is changed
88 * when incrementing in page mode.
90 private int pageIncrement
;
93 * @serial The maximum value for this scrollbar
98 * @serial The minimum value for this scrollbar
103 * @serial The orientation of this scrollbar, which will be either
104 * the <code>HORIZONTAL</code> or <code>VERTICAL</code> constant
107 private int orientation
;
110 * @serial The current value of this scrollbar.
115 * @serial The width of the scrollbar's thumb, which is relative
116 * to the minimum and maximum value of the scrollbar.
118 private int visibleAmount
;
121 * List of AdjustmentListener's.
123 private AdjustmentListener adjustment_listeners
;
126 * true if the scrollbar is adjusting, false otherwise.
128 private transient boolean valueIsAdjusting
= false;
131 * The number used to generate the name returned by getName.
133 private static transient long next_scrollbar_number
;
136 * Initializes a new instance of <code>Scrollbar</code> with a
137 * vertical orientation and default values for all other parameters.
139 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
147 * Initializes a new instance of <code>Scrollbar</code> with the
148 * specified orientation and default values for all other parameters.
149 * The orientation must be either the constant <code>HORIZONTAL</code> or
150 * <code>VERTICAL</code> from this class. An incorrect value will throw
153 * @param orientation The orientation of this scrollbar.
155 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
156 * @exception IllegalArgumentException If the orientation value is not valid.
158 public Scrollbar(int orientation
) throws IllegalArgumentException
160 this(orientation
, 0, 10, 0, 100);
164 * Initializes a new instance of <code>Scrollbar</code> with the
165 * specified parameters. The orientation must be either the constant
166 * <code>HORIZONTAL</code> or <code>VERTICAL</code>. An incorrect value
167 * will throw an exception. Inconsistent values for other parameters
168 * are silently corrected to valid values.
170 * @param orientation The orientation of this scrollbar.
171 * @param value The initial value of the scrollbar.
172 * @param visibleAmount The width of the scrollbar thumb.
173 * @param minimum The minimum value of the scrollbar.
174 * @param maximum The maximum value of the scrollbar.
176 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
177 * @exception IllegalArgumentException If the orientation value is not valid.
179 public Scrollbar(int orientation
, int value
, int visibleAmount
, int minimum
,
180 int maximum
) throws IllegalArgumentException
182 if (GraphicsEnvironment
.isHeadless())
183 throw new HeadlessException();
185 if ((orientation
!= HORIZONTAL
) && (orientation
!= VERTICAL
))
186 throw new IllegalArgumentException("Bad orientation value: "
189 this.orientation
= orientation
;
191 setValues(value
, visibleAmount
, minimum
, maximum
);
193 // Default is 1 according to online docs.
196 // Default is 10 according to javadocs.
201 * Returns the orientation constant for this object.
203 * @return The orientation constant for this object.
205 public int getOrientation()
211 * Sets the orientation of this scrollbar to the specified value. This
212 * value must be either the constant <code>HORIZONTAL</code> or
213 * <code>VERTICAL</code> from this class or an exception will be thrown.
215 * @param orientation The new orientation value.
217 * @exception IllegalArgumentException If the orientation value is not valid.
219 public void setOrientation(int orientation
)
221 if ((orientation
!= HORIZONTAL
) && (orientation
!= VERTICAL
))
222 throw new IllegalArgumentException("Bad orientation value: "
225 // FIXME: Communicate to peer? Or must this be called before peer creation?
226 this.orientation
= orientation
;
230 * Returns the current value for this scrollbar.
232 * @return The current value for this scrollbar.
234 public int getValue()
240 * Sets the current value for this scrollbar to the specified value.
241 * If this is inconsistent with the minimum and maximum values for this
242 * scrollbar, the value is silently adjusted.
244 * @param value The new value for this scrollbar.
246 public void setValue(int value
)
248 setValues(value
, visibleAmount
, minimum
, maximum
);
252 * Returns the maximum value for this scrollbar.
254 * @return The maximum value for this scrollbar.
256 public int getMaximum()
262 * Sets the maximum value for this scrollbar to the specified value.
263 * If the value is less than the current minimum value, it is silent
264 * set to equal the minimum value.
266 * @param maximum The new maximum value for this scrollbar.
268 public void setMaximum(int maximum
)
270 setValues(value
, visibleAmount
, minimum
, maximum
);
274 * Returns the minimum value for this scrollbar.
276 * @return The minimum value for this scrollbar.
278 public int getMinimum()
284 * Sets the minimum value for this scrollbar to the specified value. If
285 * this is not consistent with the current value and maximum, it is
286 * silently adjusted to be consistent.
288 * @param minimum The new minimum value for this scrollbar.
290 public void setMinimum(int minimum
)
292 setValues(value
, visibleAmount
, minimum
, maximum
);
296 * Returns the width of the scrollbar's thumb, in units relative to the
297 * maximum and minimum value of the scrollbar.
299 * @return The width of the scrollbar's thumb.
301 public int getVisibleAmount()
307 * Returns the width of the scrollbar's thumb, in units relative to the
308 * maximum and minimum value of the scrollbar.
310 * @return The width of the scrollbar's thumb.
312 * @deprecated This method is deprecated in favor of
313 * <code>getVisibleAmount()</code>.
315 public int getVisible()
317 return visibleAmount
;
321 * Sets the width of the scrollbar's thumb, in units relative to the
322 * maximum and minimum value of the scrollbar.
324 * @param visibleAmount The new visible amount value of the scrollbar.
326 public void setVisibleAmount(int visibleAmount
)
328 setValues(value
, visibleAmount
, minimum
, maximum
);
332 * Sets the current value, visible amount, minimum, and maximum for this
333 * scrollbar. These values are adjusted to be internally consistent
336 * @param value The new value for this scrollbar.
337 * @param visibleAmount The new visible amount for this scrollbar.
338 * @param minimum The new minimum value for this scrollbar.
339 * @param maximum The new maximum value for this scrollbar.
341 public synchronized void setValues(int value
, int visibleAmount
,
342 int minimum
, int maximum
)
344 if (maximum
< minimum
)
353 if (visibleAmount
> maximum
- minimum
)
354 visibleAmount
= maximum
- minimum
;
356 ScrollbarPeer peer
= (ScrollbarPeer
) getPeer();
358 && (this.value
!= value
|| this.visibleAmount
!= visibleAmount
359 || this.minimum
!= minimum
|| this.maximum
!= maximum
))
360 peer
.setValues(value
, visibleAmount
, minimum
, maximum
);
363 this.visibleAmount
= visibleAmount
;
364 this.minimum
= minimum
;
365 this.maximum
= maximum
;
367 int range
= maximum
- minimum
;
368 if (lineIncrement
> range
)
373 lineIncrement
= range
;
376 peer
.setLineIncrement(lineIncrement
);
379 if (pageIncrement
> range
)
384 pageIncrement
= range
;
387 peer
.setPageIncrement(pageIncrement
);
392 * Returns the value added or subtracted when the user activates the scrollbar
393 * scroll by a "unit" amount.
395 * @return The unit increment value.
397 public int getUnitIncrement()
399 return getLineIncrement();
403 * Returns the value added or subtracted when the user selects the scrollbar
404 * scroll by a "unit" amount control.
406 * @return The unit increment value.
408 * @deprecated This method is deprecated in favor of
409 * <code>getUnitIncrement()</code>.
411 public int getLineIncrement()
413 return lineIncrement
;
417 * Sets the value added or subtracted to the scrollbar value when the
418 * user selects the scroll by a "unit" amount control.
420 * @param unitIncrement The new unit increment amount.
422 public synchronized void setUnitIncrement(int unitIncrement
)
424 setLineIncrement(unitIncrement
);
428 * Sets the value added or subtracted to the scrollbar value when the
429 * user selects the scroll by a "unit" amount control.
431 * @param lineIncrement The new unit increment amount.
433 * @deprecated This method is deprecated in favor of
434 * <code>setUnitIncrement()</code>.
436 public void setLineIncrement(int lineIncrement
)
438 if (lineIncrement
< 0)
439 throw new IllegalArgumentException("Unit increment less than zero.");
441 int range
= maximum
- minimum
;
442 if (lineIncrement
> range
)
447 lineIncrement
= range
;
450 if (lineIncrement
== this.lineIncrement
)
453 this.lineIncrement
= lineIncrement
;
455 ScrollbarPeer peer
= (ScrollbarPeer
) getPeer();
457 peer
.setLineIncrement(this.lineIncrement
);
461 * Returns the value added or subtracted when the user activates the scrollbar
462 * scroll by a "block" amount.
464 * @return The block increment value.
466 public int getBlockIncrement()
468 return getPageIncrement();
472 * Returns the value added or subtracted when the user selects the scrollbar
473 * scroll by a "block" amount control.
475 * @return The block increment value.
477 * @deprecated This method is deprecated in favor of
478 * <code>getBlockIncrement()</code>.
480 public int getPageIncrement()
482 return pageIncrement
;
486 * Sets the value added or subtracted to the scrollbar value when the
487 * user selects the scroll by a "block" amount control.
489 * @param blockIncrement The new block increment amount.
491 public synchronized void setBlockIncrement(int blockIncrement
)
493 setPageIncrement(blockIncrement
);
497 * Sets the value added or subtracted to the scrollbar value when the
498 * user selects the scroll by a "block" amount control.
500 * @param pageIncrement The new block increment amount.
502 * @deprecated This method is deprecated in favor of
503 * <code>setBlockIncrement()</code>.
505 public void setPageIncrement(int pageIncrement
)
507 if (pageIncrement
< 0)
508 throw new IllegalArgumentException("Block increment less than zero.");
510 int range
= maximum
- minimum
;
511 if (pageIncrement
> range
)
516 pageIncrement
= range
;
519 if (pageIncrement
== this.pageIncrement
)
522 this.pageIncrement
= pageIncrement
;
524 ScrollbarPeer peer
= (ScrollbarPeer
) getPeer();
526 peer
.setPageIncrement(this.pageIncrement
);
530 * Notifies this object to create its native peer.
532 public synchronized void addNotify()
535 peer
= getToolkit().createScrollbar(this);
540 * Adds a new adjustment listener to the list of registered listeners
543 * @param listener The listener to add.
545 public synchronized void addAdjustmentListener(AdjustmentListener listener
)
547 adjustment_listeners
= AWTEventMulticaster
.add(adjustment_listeners
,
549 enableEvents(AWTEvent
.ADJUSTMENT_EVENT_MASK
);
553 * Removes the specified listener from the list of registered listeners
556 * @param listener The listener to remove.
558 public synchronized void removeAdjustmentListener(AdjustmentListener listener
)
560 adjustment_listeners
= AWTEventMulticaster
.remove(adjustment_listeners
,
565 * Processes events for this scrollbar. It does this by calling
566 * <code>processAdjustmentEvent()</code> if the event is an instance of
567 * <code>AdjustmentEvent</code>, otherwise it calls the superclass to
570 * @param event The event to process.
572 protected void processEvent(AWTEvent event
)
574 if (event
instanceof AdjustmentEvent
)
575 processAdjustmentEvent((AdjustmentEvent
) event
);
577 super.processEvent(event
);
581 * Processes adjustment events for this object by dispatching them to
582 * any registered listeners. Note that this method will only be called
583 * if adjustment events are enabled. This will happen automatically if
584 * any listeners are registered. Otherwise, it can be enabled by a
585 * call to <code>enableEvents()</code>.
587 * @param event The event to process.
589 protected void processAdjustmentEvent(AdjustmentEvent event
)
591 value
= event
.getValue();
592 if (adjustment_listeners
!= null)
593 adjustment_listeners
.adjustmentValueChanged(event
);
596 void dispatchEventImpl(AWTEvent e
)
598 if (e
.id
<= AdjustmentEvent
.ADJUSTMENT_LAST
599 && e
.id
>= AdjustmentEvent
.ADJUSTMENT_FIRST
600 && (adjustment_listeners
!= null
601 || (eventMask
& AWTEvent
.ADJUSTMENT_EVENT_MASK
) != 0))
604 super.dispatchEventImpl(e
);
608 * Returns a debugging string for this object.
610 * @return A debugging string for this object.
612 protected String
paramString()
614 return ("value=" + getValue() + ",visibleAmount=" + getVisibleAmount()
615 + ",minimum=" + getMinimum() + ",maximum=" + getMaximum()
616 + ",pageIncrement=" + pageIncrement
+ ",lineIncrement="
617 + lineIncrement
+ ",orientation="
618 + (orientation
== HORIZONTAL ?
"HORIZONTAL" : "VERTICAL")
619 + super.paramString());
623 * Returns an array of all the objects currently registered as FooListeners
624 * upon this <code>Scrollbar</code>. FooListeners are registered using the
625 * addFooListener method.
627 * @exception ClassCastException If listenerType doesn't specify a class or
628 * interface that implements java.util.EventListener.
630 public EventListener
[] getListeners(Class listenerType
)
632 if (listenerType
== AdjustmentListener
.class)
633 return AWTEventMulticaster
.getListeners(adjustment_listeners
,
636 return super.getListeners(listenerType
);
640 * Returns an array of all registered adjustment listeners.
642 public AdjustmentListener
[] getAdjustmentListeners()
644 return (AdjustmentListener
[]) getListeners(AdjustmentListener
.class);
648 * Returns true if the value is in the process of changing.
652 public boolean getValueIsAdjusting()
654 return valueIsAdjusting
;
658 * Sets the value of valueIsAdjusting.
662 public void setValueIsAdjusting(boolean valueIsAdjusting
)
664 this.valueIsAdjusting
= valueIsAdjusting
;
668 * Generate a unique name for this scroll bar.
670 * @return A unique name for this scroll bar.
672 String
generateName()
674 return "scrollbar" + getUniqueLong();
677 private static synchronized long getUniqueLong()
679 return next_scrollbar_number
++;
683 * This class provides accessibility support for the
686 * @author Jerry Quinn (jlquinn@optonline.net)
687 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
689 protected class AccessibleAWTScrollBar
extends AccessibleAWTComponent
690 implements AccessibleValue
693 * Serialization constant to match JDK 1.5
695 private static final long serialVersionUID
= -344337268523697807L;
698 * Returns the role of this accessible object.
700 * @return the instance of <code>AccessibleRole</code>,
701 * which describes this object.
703 * @see javax.accessibility.AccessibleRole
705 public AccessibleRole
getAccessibleRole()
707 return AccessibleRole
.SCROLL_BAR
;
711 * Returns the state set of this accessible object.
713 * @return a set of <code>AccessibleState</code>s which
714 * represent the current state of the accessible object.
716 * @see javax.accessibility.AccessibleState
717 * @see javax.accessibility.AccessibleStateSet
719 public AccessibleStateSet
getAccessibleStateSet()
721 AccessibleStateSet states
= super.getAccessibleStateSet();
722 if (getOrientation() == HORIZONTAL
)
723 states
.add(AccessibleState
.HORIZONTAL
);
725 states
.add(AccessibleState
.VERTICAL
);
726 if (getValueIsAdjusting())
727 states
.add(AccessibleState
.BUSY
);
732 * Returns an implementation of the <code>AccessibleValue</code>
733 * interface for this accessible object. In this case, the
734 * current instance is simply returned (with a more appropriate
735 * type), as it also implements the accessible value as well as
738 * @return the accessible value associated with this context.
740 * @see javax.accessibility.AccessibleValue
742 public AccessibleValue
getAccessibleValue()
748 * Returns the current value of this accessible object.
749 * In this case, this is the same as the value for
750 * the scrollbar, wrapped in an <code>Integer</code>
753 * @return the numeric value of this scrollbar.
755 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
757 public Number
getCurrentAccessibleValue()
759 return new Integer(getValue());
763 * Sets the current value of this accessible object
764 * to that supplied. In this case, the value of the
765 * scrollbar is set, and this method always returns
768 * @param number the new accessible value.
770 * @return true if the value was set.
772 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
774 public boolean setCurrentAccessibleValue(Number number
)
776 setValue(number
.intValue());
781 * Returns the minimum acceptable accessible value used
782 * by this object. In this case, this is the same as
783 * the minimum value of the scrollbar, wrapped in an
786 * @return the minimum value of this scrollbar.
788 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
790 public Number
getMinimumAccessibleValue()
792 return new Integer(getMinimum());
796 * Returns the maximum acceptable accessible value used
797 * by this object. In this case, this is the same as
798 * the maximum value of the scrollbar, wrapped in an
801 * @return the maximum value of this scrollbar.
803 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
805 public Number
getMaximumAccessibleValue()
807 return new Integer(getMaximum());
812 * Gets the AccessibleContext associated with this <code>Scrollbar</code>.
813 * The context is created, if necessary.
815 * @return the associated context
817 public AccessibleContext
getAccessibleContext()
819 /* Create the context if this is the first request */
820 if (accessibleContext
== null)
821 accessibleContext
= new AccessibleAWTScrollBar();
823 return accessibleContext
;