libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / awt / Scrollbar.java
blob0cba512f605d45028632f5d97f5af53aa7260105
1 /* Scrollbar.java -- AWT Scrollbar widget
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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)
10 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
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
25 combination.
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. */
40 package java.awt;
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;
54 /**
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
65 /**
66 * Constant indicating that a scrollbar is horizontal.
68 public static final int HORIZONTAL = 0;
70 /**
71 * Constant indicating that a scrollbar is vertical.
73 public static final int VERTICAL = 1;
75 /**
76 * Serialization Constant.
78 private static final long serialVersionUID = 8451667562882310543L;
80 /**
81 * @serial The amount by which the value of the scrollbar is changed
82 * when incrementing in line mode.
84 private int lineIncrement;
86 /**
87 * @serial The amount by which the value of the scrollbar is changed
88 * when incrementing in page mode.
90 private int pageIncrement;
92 /**
93 * @serial The maximum value for this scrollbar
95 private int maximum;
97 /**
98 * @serial The minimum value for this scrollbar
100 private int minimum;
103 * @serial The orientation of this scrollbar, which will be either
104 * the <code>HORIZONTAL</code> or <code>VERTICAL</code> constant
105 * from this class.
107 private int orientation;
110 * @serial The current value of this scrollbar.
112 private int value;
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,
141 public Scrollbar()
143 this(VERTICAL);
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
151 * an exception.
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: "
187 + orientation);
189 this.orientation = orientation;
191 setValues(value, visibleAmount, minimum, maximum);
193 // Default is 1 according to online docs.
194 lineIncrement = 1;
196 // Default is 10 according to javadocs.
197 pageIncrement = 10;
201 * Returns the orientation constant for this object.
203 * @return The orientation constant for this object.
205 public int getOrientation()
207 return orientation;
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: "
223 + orientation);
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()
236 return value;
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()
258 return maximum;
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()
280 return minimum;
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()
303 return getVisible();
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
334 * if necessary.
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 (visibleAmount <= 0)
345 visibleAmount = 1;
347 if (maximum <= minimum)
348 maximum = minimum + 1;
350 if (value < minimum)
351 value = minimum;
353 if (visibleAmount > maximum - minimum)
354 visibleAmount = maximum - minimum;
356 // According to documentation, the actual maximum
357 // value is (maximum - visibleAmount)
358 if (value > maximum - visibleAmount)
359 value = maximum - visibleAmount;
361 ScrollbarPeer peer = (ScrollbarPeer) getPeer();
362 if (peer != null
363 && (this.value != value || this.visibleAmount != visibleAmount
364 || this.minimum != minimum || this.maximum != maximum))
365 peer.setValues(value, visibleAmount, minimum, maximum);
367 this.value = value;
368 this.visibleAmount = visibleAmount;
369 this.minimum = minimum;
370 this.maximum = maximum;
374 * Returns the value added or subtracted when the user activates the scrollbar
375 * scroll by a "unit" amount.
377 * @return The unit increment value.
379 public int getUnitIncrement()
381 return getLineIncrement();
385 * Returns the value added or subtracted when the user selects the scrollbar
386 * scroll by a "unit" amount control.
388 * @return The unit increment value.
390 * @deprecated This method is deprecated in favor of
391 * <code>getUnitIncrement()</code>.
393 public int getLineIncrement()
395 return lineIncrement;
399 * Sets the value added or subtracted to the scrollbar value when the
400 * user selects the scroll by a "unit" amount control.
402 * @param unitIncrement The new unit increment amount.
404 public synchronized void setUnitIncrement(int unitIncrement)
406 setLineIncrement(unitIncrement);
410 * Sets the value added or subtracted to the scrollbar value when the
411 * user selects the scroll by a "unit" amount control.
413 * @param lineIncrement The new unit increment amount.
415 * @deprecated This method is deprecated in favor of
416 * <code>setUnitIncrement()</code>.
418 public void setLineIncrement(int lineIncrement)
420 if (lineIncrement < 0)
421 throw new IllegalArgumentException("Unit increment less than zero.");
423 if (lineIncrement == 0)
424 lineIncrement = 1;
426 if (lineIncrement == this.lineIncrement)
427 return;
429 this.lineIncrement = lineIncrement;
431 ScrollbarPeer peer = (ScrollbarPeer) getPeer();
432 if (peer != null)
433 peer.setLineIncrement(this.lineIncrement);
437 * Returns the value added or subtracted when the user activates the scrollbar
438 * scroll by a "block" amount.
440 * @return The block increment value.
442 public int getBlockIncrement()
444 return getPageIncrement();
448 * Returns the value added or subtracted when the user selects the scrollbar
449 * scroll by a "block" amount control.
451 * @return The block increment value.
453 * @deprecated This method is deprecated in favor of
454 * <code>getBlockIncrement()</code>.
456 public int getPageIncrement()
458 return pageIncrement;
462 * Sets the value added or subtracted to the scrollbar value when the
463 * user selects the scroll by a "block" amount control.
465 * @param blockIncrement The new block increment amount.
467 public synchronized void setBlockIncrement(int blockIncrement)
469 setPageIncrement(blockIncrement);
473 * Sets the value added or subtracted to the scrollbar value when the
474 * user selects the scroll by a "block" amount control.
476 * @param pageIncrement The new block increment amount.
478 * @deprecated This method is deprecated in favor of
479 * <code>setBlockIncrement()</code>.
481 public void setPageIncrement(int pageIncrement)
483 if (pageIncrement < 0)
484 throw new IllegalArgumentException("Block increment less than zero.");
486 if (pageIncrement == 0)
487 pageIncrement = 1;
489 if (pageIncrement == this.pageIncrement)
490 return;
492 this.pageIncrement = pageIncrement;
494 ScrollbarPeer peer = (ScrollbarPeer) getPeer();
495 if (peer != null)
496 peer.setPageIncrement(this.pageIncrement);
500 * Notifies this object to create its native peer.
502 public synchronized void addNotify()
504 if (peer == null)
505 peer = getToolkit().createScrollbar(this);
506 super.addNotify();
510 * Adds a new adjustment listener to the list of registered listeners
511 * for this object.
513 * @param listener The listener to add.
515 public synchronized void addAdjustmentListener(AdjustmentListener listener)
517 adjustment_listeners = AWTEventMulticaster.add(adjustment_listeners,
518 listener);
519 enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK);
523 * Removes the specified listener from the list of registered listeners
524 * for this object.
526 * @param listener The listener to remove.
528 public synchronized void removeAdjustmentListener(AdjustmentListener listener)
530 adjustment_listeners = AWTEventMulticaster.remove(adjustment_listeners,
531 listener);
535 * Processes events for this scrollbar. It does this by calling
536 * <code>processAdjustmentEvent()</code> if the event is an instance of
537 * <code>AdjustmentEvent</code>, otherwise it calls the superclass to
538 * process the event.
540 * @param event The event to process.
542 protected void processEvent(AWTEvent event)
544 if (event instanceof AdjustmentEvent)
545 processAdjustmentEvent((AdjustmentEvent) event);
546 else
547 super.processEvent(event);
551 * Processes adjustment events for this object by dispatching them to
552 * any registered listeners. Note that this method will only be called
553 * if adjustment events are enabled. This will happen automatically if
554 * any listeners are registered. Otherwise, it can be enabled by a
555 * call to <code>enableEvents()</code>.
557 * @param event The event to process.
559 protected void processAdjustmentEvent(AdjustmentEvent event)
561 value = event.getValue();
562 if (adjustment_listeners != null)
563 adjustment_listeners.adjustmentValueChanged(event);
567 * Package private method to determine whether to call
568 * processEvent() or not. Will handle events from peer and update
569 * the current value.
571 void dispatchEventImpl(AWTEvent e)
573 if (e.id <= AdjustmentEvent.ADJUSTMENT_LAST
574 && e.id >= AdjustmentEvent.ADJUSTMENT_FIRST)
576 AdjustmentEvent ae = (AdjustmentEvent) e;
577 boolean adjusting = ae.getValueIsAdjusting();
578 if (adjusting)
579 setValueIsAdjusting(true);
582 setValue(((AdjustmentEvent) e).getValue());
583 if (adjustment_listeners != null
584 || (eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0)
585 processEvent(e);
587 finally
589 if (adjusting)
590 setValueIsAdjusting(false);
593 else
594 super.dispatchEventImpl(e);
598 * Returns a debugging string for this object.
600 * @return A debugging string for this object.
602 protected String paramString()
604 return ("value=" + getValue() + ",visibleAmount=" + getVisibleAmount()
605 + ",minimum=" + getMinimum() + ",maximum=" + getMaximum()
606 + ",pageIncrement=" + pageIncrement + ",lineIncrement="
607 + lineIncrement + ",orientation="
608 + (orientation == HORIZONTAL ? "HORIZONTAL" : "VERTICAL")
609 + super.paramString());
613 * Returns an array of all the objects currently registered as FooListeners
614 * upon this <code>Scrollbar</code>. FooListeners are registered using the
615 * addFooListener method.
617 * @exception ClassCastException If listenerType doesn't specify a class or
618 * interface that implements java.util.EventListener.
620 public <T extends EventListener> T[] getListeners(Class<T> listenerType)
622 if (listenerType == AdjustmentListener.class)
623 return AWTEventMulticaster.getListeners(adjustment_listeners,
624 listenerType);
626 return super.getListeners(listenerType);
630 * Returns an array of all registered adjustment listeners.
632 public AdjustmentListener[] getAdjustmentListeners()
634 return (AdjustmentListener[]) getListeners(AdjustmentListener.class);
638 * Returns true if the value is in the process of changing.
640 * @since 1.4
642 public boolean getValueIsAdjusting()
644 return valueIsAdjusting;
648 * Sets the value of valueIsAdjusting.
650 * @since 1.4
652 public void setValueIsAdjusting(boolean valueIsAdjusting)
654 this.valueIsAdjusting = valueIsAdjusting;
658 * Generate a unique name for this scroll bar.
660 * @return A unique name for this scroll bar.
662 String generateName()
664 return "scrollbar" + getUniqueLong();
667 private static synchronized long getUniqueLong()
669 return next_scrollbar_number++;
673 * This class provides accessibility support for the
674 * scrollbar.
676 * @author Jerry Quinn (jlquinn@optonline.net)
677 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
679 protected class AccessibleAWTScrollBar extends AccessibleAWTComponent
680 implements AccessibleValue
683 * Serialization constant to match JDK 1.5
685 private static final long serialVersionUID = -344337268523697807L;
688 * Returns the role of this accessible object.
690 * @return the instance of <code>AccessibleRole</code>,
691 * which describes this object.
693 * @see javax.accessibility.AccessibleRole
695 public AccessibleRole getAccessibleRole()
697 return AccessibleRole.SCROLL_BAR;
701 * Returns the state set of this accessible object.
703 * @return a set of <code>AccessibleState</code>s which
704 * represent the current state of the accessible object.
706 * @see javax.accessibility.AccessibleState
707 * @see javax.accessibility.AccessibleStateSet
709 public AccessibleStateSet getAccessibleStateSet()
711 AccessibleStateSet states = super.getAccessibleStateSet();
712 if (getOrientation() == HORIZONTAL)
713 states.add(AccessibleState.HORIZONTAL);
714 else
715 states.add(AccessibleState.VERTICAL);
716 if (getValueIsAdjusting())
717 states.add(AccessibleState.BUSY);
718 return states;
722 * Returns an implementation of the <code>AccessibleValue</code>
723 * interface for this accessible object. In this case, the
724 * current instance is simply returned (with a more appropriate
725 * type), as it also implements the accessible value as well as
726 * the context.
728 * @return the accessible value associated with this context.
730 * @see javax.accessibility.AccessibleValue
732 public AccessibleValue getAccessibleValue()
734 return this;
738 * Returns the current value of this accessible object.
739 * In this case, this is the same as the value for
740 * the scrollbar, wrapped in an <code>Integer</code>
741 * object.
743 * @return the numeric value of this scrollbar.
745 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
747 public Number getCurrentAccessibleValue()
749 return new Integer(getValue());
753 * Sets the current value of this accessible object
754 * to that supplied. In this case, the value of the
755 * scrollbar is set, and this method always returns
756 * true.
758 * @param number the new accessible value.
760 * @return true if the value was set.
762 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
764 public boolean setCurrentAccessibleValue(Number number)
766 setValue(number.intValue());
767 return true;
771 * Returns the minimum acceptable accessible value used
772 * by this object. In this case, this is the same as
773 * the minimum value of the scrollbar, wrapped in an
774 * object.
776 * @return the minimum value of this scrollbar.
778 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
780 public Number getMinimumAccessibleValue()
782 return new Integer(getMinimum());
786 * Returns the maximum acceptable accessible value used
787 * by this object. In this case, this is the same as
788 * the maximum value of the scrollbar, wrapped in an
789 * object.
791 * @return the maximum value of this scrollbar.
793 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
795 public Number getMaximumAccessibleValue()
797 return new Integer(getMaximum());
802 * Gets the AccessibleContext associated with this <code>Scrollbar</code>.
803 * The context is created, if necessary.
805 * @return the associated context
807 public AccessibleContext getAccessibleContext()
809 /* Create the context if this is the first request */
810 if (accessibleContext == null)
811 accessibleContext = new AccessibleAWTScrollBar();
813 return accessibleContext;