home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / Scrollbar.java < prev    next >
Text File  |  1997-08-30  |  26KB  |  754 lines

  1. /*
  2.  * @(#)Scrollbar.java    1.50 97/06/17
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22. package java.awt;
  23.  
  24. import java.awt.peer.ScrollbarPeer;
  25. import java.awt.event.*;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * The <code>Scrollbar</code> class embodies a scroll bar, a 
  33.  * familiar user-interface object. A scroll bar provides a 
  34.  * convenient means for allowing a user to select from a 
  35.  * range of values. The following three vertical
  36.  * scroll bars could be used as slider controls to pick 
  37.  * the red, green, and blue components of a color:
  38.  * <p>
  39.  * <img src="images-awt/Scrollbar-1.gif"
  40.  * ALIGN=center HSPACE=10 VSPACE=7> 
  41.  * <p>
  42.  * Each scroll bar in this example could be created with 
  43.  * code similar to the following: 
  44.  * <p>
  45.  * <hr><blockquote><pre>
  46.  * redSlider=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
  47.  * add(redSlider);
  48.  * </pre></blockquote><hr>
  49.  * <p>
  50.  * Alternatively, a scroll bar can represent a range of values. For 
  51.  * example, if a scroll bar is used for scrolling through text, the 
  52.  * width of the "bubble" or "thumb" can represent the amount of text 
  53.  * that is visible. Here is an example of a scroll bar that 
  54.  * represents a range:
  55.  * <p>
  56.  * <img src="images-awt/Scrollbar-2.gif" 
  57.  * ALIGN=center HSPACE=10 VSPACE=7> 
  58.  * <p>
  59.  * The value range represented by the bubble is the <em>visible</em> 
  60.  * range of the scroll bar. The horizontal scroll bar in this 
  61.  * example could be created with code like the following: 
  62.  * <p>
  63.  * <hr><blockquote><pre>
  64.  * ranger = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, 0, 255);
  65.  * add(ranger);
  66.  * </pre></blockquote><hr>
  67.  * <p>
  68.  * Note that the maximum value above, 255, is the maximum value for 
  69.  * the scroll bar's bubble. The actual width of the 
  70.  * scroll bar's track is 255 + 64. When the scroll bar
  71.  * is set to its maximum value, the left side of the bubble
  72.  * is at 255, and the right side is at 255 + 64.
  73.  * <p>
  74.  * Normally, the user changes the value of the scroll bar by 
  75.  * making a gesture with the mouse. For example, the user can
  76.  * drag the scroll bar's bubble up and down, or click in the 
  77.  * scroll bar's unit increment or block increment areas. Keyboard
  78.  * gestures can also be mapped to the scroll bar. By convention,
  79.  * the <b>Page Up</b> and <b>Page Down</b> 
  80.  * keys are equivalent to clicking in the scroll bar's block
  81.  * increment and block decrement areas.
  82.  * <p>
  83.  * When the user changes the value of the scroll bar, the scroll bar
  84.  * receives an instance of <code>AdjustmentEvent</code>. 
  85.  * The scroll bar processes this event, passing it along to 
  86.  * any registered listeners. 
  87.  * <p>
  88.  * Any object that wishes to be notified of changes to the 
  89.  * scroll bar's value should implement 
  90.  * <code>AdjustmentListener</code>, an interface defined in
  91.  * the package <code>java.awt.event</code>. 
  92.  * Listeners can be added and removed dynamically by calling 
  93.  * the methods <code>addAdjustmentListener</code> and
  94.  * <code>removeAdjustmentListener</code>.
  95.  * <p>
  96.  * The <code>AdjustmentEvent</code> class defines five types 
  97.  * of adjustment event, listed here: 
  98.  * <p>
  99.  * <ul>
  100.  * <li><code>AdjustmentEvent.TRACK</code> is sent out when the 
  101.  * user drags the scroll bar's bubble.
  102.  * <li><code>AdjustmentEvent.UNIT_INCREMENT</code> is sent out
  103.  * when the user clicks in the left arrow of a horizontal scroll 
  104.  * bar, or the top arrow of a vertical scroll bar, or makes the 
  105.  * equivalent gesture from the keyboard.
  106.  * <li><code>AdjustmentEvent.UNIT_DECREMENT</code> is sent out
  107.  * when the user clicks in the right arrow of a horizontal scroll 
  108.  * bar, or the bottom arrow of a vertical scroll bar, or makes the 
  109.  * equivalent gesture from the keyboard.
  110.  * <li><code>AdjustmentEvent.BLOCK_INCREMENT</code> is sent out
  111.  * when the user clicks in the track, to the left of the bubble
  112.  * on a horizontal scroll bar, or above the bubble on a vertical
  113.  * scroll bar. By convention, the <b>Page Up</b> 
  114.  * key is equivalent, if the user is using a keyboard that 
  115.  * defines a <b>Page Up</b> key.
  116.  * <li><code>AdjustmentEvent.BLOCK_DECREMENT</code> is sent out
  117.  * when the user clicks in the track, to the right of the bubble
  118.  * on a horizontal scroll bar, or below the bubble on a vertical
  119.  * scroll bar. By convention, the <b>Page Down</b> 
  120.  * key is equivalent, if the user is using a keyboard that 
  121.  * defines a <b>Page Down</b> key.
  122.  * </ul>
  123.  * <p>
  124.  * The JDK 1.0 event system is supported for backwards
  125.  * compatibility, but its use with newer versions of JDK is 
  126.  * discouraged. The fives types of adjustment event introduced
  127.  * with JDK 1.1 correspond to the five event types 
  128.  * that are associated with scroll bars in previous JDK versions.
  129.  * The following list gives the adjustment event type, 
  130.  * and the corresponding JDK 1.0 event type it replaces.
  131.  * <p>
  132.  * <ul>
  133.  * <li><code>AdjustmentEvent.TRACK</code> replaces 
  134.  * <code>Event.SCROLL_ABSOLUTE</code>
  135.  * <li><code>AdjustmentEvent.UNIT_INCREMENT</code> replaces 
  136.  * <code>Event.SCROLL_LINE_UP</code>
  137.  * <li><code>AdjustmentEvent.UNIT_DECREMENT</code> replaces 
  138.  * <code>Event.SCROLL_LINE_DOWN</code>
  139.  * <li><code>AdjustmentEvent.BLOCK_INCREMENT</code> replaces 
  140.  * <code>Event.SCROLL_PAGE_UP</code>
  141.  * <li><code>AdjustmentEvent.BLOCK_DECREMENT</code> replaces 
  142.  * <code>Event.SCROLL_PAGE_DOWN</code>
  143.  * </ul>
  144.  * <p>
  145.  *
  146.  * @version    1.50 06/17/97
  147.  * @author     Sami Shaio
  148.  * @see         java.awt.event.AdjustmentEvent
  149.  * @see         java.awt.event.AdjustmentListener
  150.  * @since       JDK1.0
  151.  */
  152. public class Scrollbar extends Component implements Adjustable {
  153.   
  154.     /**
  155.      * A constant that indicates a horizontal scroll bar.
  156.      * @since     JDK1.0
  157.      */
  158.     public static final int    HORIZONTAL = 0;
  159.  
  160.     /**
  161.      * A constant that indicates a vertical scroll bar.
  162.      * @since     JDK1.0
  163.      */
  164.     public static final int    VERTICAL   = 1;
  165.  
  166.     /**
  167.      * The value of the Scrollbar.
  168.      */
  169.     int    value;
  170.  
  171.     /**
  172.      * The maximum value of the Scrollbar.
  173.      */
  174.     int    maximum;
  175.  
  176.     /**
  177.      * The minimum value of the Scrollbar.
  178.      */
  179.     int    minimum;
  180.  
  181.     /**
  182.      * The size of the visible portion of the Scrollbar.
  183.      */
  184.     int    visibleAmount;
  185.  
  186.     /**
  187.      * The Scrollbar's orientation--being either horizontal or vertical.
  188.      */
  189.     int    orientation;
  190.  
  191.     /**
  192.      * The amount by which the scrollbar value will change when going
  193.      * up or down by a line.
  194.      */
  195.     int lineIncrement = 1;
  196.  
  197.     /**
  198.      * The amount by which the scrollbar value will change when going
  199.      * up or down by a page.
  200.      */
  201.     int pageIncrement = 10;
  202.  
  203.     transient AdjustmentListener adjustmentListener;
  204.  
  205.     private static final String base = "scrollbar";
  206.     private static int nameCounter = 0;
  207.     
  208.     /*
  209.      * JDK 1.1 serialVersionUID 
  210.      */
  211.     private static final long serialVersionUID = 8451667562882310543L;
  212.  
  213.     /**
  214.      * Constructs a new vertical scroll bar.
  215.      * @since   JDK1.0
  216.      */
  217.     public Scrollbar() {
  218.     this(VERTICAL, 0, 10, 0, 100);
  219.     }
  220.  
  221.     /**
  222.      * Constructs a new scroll bar with the specified orientation.
  223.      * <p>
  224.      * The <code>orientation</code> argument must take one of the two 
  225.      * values <code>Scrollbar.HORIZONTAL</code>, 
  226.      * or <code>Scrollbar.VERTICAL</code>, 
  227.      * indicating a horizontal or vertical scroll bar, respectively. 
  228.      * @param       orientation   indicates the orientation of the scroll bar.
  229.      * @exception   IllegalArgumentException    when an illegal value for
  230.      *                    the <code>orientation</code> argument is supplied.
  231.      * @since       JDK1.0
  232.      */
  233.     public Scrollbar(int orientation) {
  234.         this(orientation, 0, 10, 0, 100);
  235.     }
  236.  
  237.     /**
  238.      * Constructs a new scroll bar with the specified orientation, 
  239.      * initial value, page size, and minimum and maximum values. 
  240.      * <p>
  241.      * The <code>orientation</code> argument must take one of the two 
  242.      * values <code>Scrollbar.HORIZONTAL</code>, 
  243.      * or <code>Scrollbar.VERTICAL</code>, 
  244.      * indicating a horizontal or vertical scroll bar, respectively. 
  245.      * <p>
  246.      * If the specified maximum value is less than the minimum value, it 
  247.      * is changed to be the same as the minimum value. If the initial 
  248.      * value is lower than the minimum value, it is changed to be the 
  249.      * minimum value; if it is greater than the maximum value, it is 
  250.      * changed to be the maximum value. 
  251.      * @param     orientation   indicates the orientation of the scroll bar.
  252.      * @param     value     the initial value of the scroll bar.
  253.      * @param     visible   the size of the scroll bar's bubble, representing
  254.      *                      the visible portion; the scroll bar uses this 
  255.                             value when paging up or down by a page.
  256.      * @param     minimum   the minimum value of the scroll bar.
  257.      * @param     maximum   the maximum value of the scroll bar.
  258.      * @since     JDK1.0
  259.      */
  260.     public Scrollbar(int orientation, int value, int visible, int minimum, int maximum) {
  261.     this.name = base + nameCounter++;
  262.     switch (orientation) {
  263.       case HORIZONTAL:
  264.       case VERTICAL:
  265.         this.orientation = orientation;
  266.         break;
  267.       default:
  268.         throw new IllegalArgumentException("illegal scrollbar orientation");
  269.     }
  270.     setValues(value, visible, minimum, maximum);
  271.     }
  272.  
  273.     /**
  274.      * Creates the Scrollbar's peer.  The peer allows you to modify
  275.      * the appearance of the Scrollbar without changing any of its
  276.      * functionality.
  277.      */
  278.  
  279.     public void addNotify() {
  280.     peer = getToolkit().createScrollbar(this);
  281.     super.addNotify();
  282.     }
  283.  
  284.     /**
  285.      * Determines the orientation of this scroll bar. 
  286.      * @return    the orientation of this scroll bar, either 
  287.      *               <code>Scrollbar.HORIZONTAL</code> or 
  288.      *               <code>Scrollbar.VERTICAL</code>.
  289.      * @see       java.awt.Scrollbar#setOrientation
  290.      * @since     JDK1.0
  291.      */
  292.     public int getOrientation() {
  293.     return orientation;
  294.     }
  295.  
  296.     /**
  297.      * Sets the orientation for this scroll bar. 
  298.      * @param     the orientation of this scroll bar, either 
  299.      *               <code>Scrollbar.HORIZONTAL</code> or 
  300.      *               <code>Scrollbar.VERTICAL</code>.
  301.      * @see       java.awt.Scrollbar#getOrientation
  302.      * @exception   IllegalArgumentException  if the value supplied
  303.      *                   for <code>orientation</code> is not a
  304.      *                   legal value.
  305.      * @since     JDK1.1
  306.      */
  307.     public synchronized void setOrientation(int orientation) {
  308.     if (orientation == this.orientation) {
  309.         return;
  310.     }
  311.     switch (orientation) {
  312.       case HORIZONTAL:
  313.       case VERTICAL:
  314.         this.orientation = orientation;
  315.         break;
  316.       default:
  317.         throw new IllegalArgumentException("illegal scrollbar orientation");
  318.     }
  319.     /* Create a new peer with the specified orientation. */
  320.     synchronized (Component.LOCK) {
  321.         if (peer != null) {
  322.         removeNotify();
  323.         addNotify();
  324.         invalidate();
  325.         }
  326.     }
  327.     }
  328.  
  329.     /**
  330.      * Gets the current value of this scroll bar.
  331.      * @return      the current value of this scroll bar.
  332.      * @see         java.awt.Scrollbar#getMinimum
  333.      * @see         java.awt.Scrollbar#getMaximum
  334.      * @since       JDK1.0
  335.      */
  336.     public int getValue() {
  337.     return value;
  338.     }
  339.  
  340.     /**
  341.      * Sets the value of this scroll bar to the specified value.
  342.      * <p>
  343.      * If the value supplied is less than the current minimum or 
  344.      * greater than the current maximum, then one of those values
  345.      * is substituted, as appropriate.
  346.      * <p>
  347.      * Normally, a program should change a scroll bar's  
  348.      * value only by calling <code>setValues</code>. 
  349.      * The <code>setValues</code> method simultaneously 
  350.      * and synchronously sets the minimum, maximum, visible amount, 
  351.      * and value properties of a scroll bar, so that they are 
  352.      * mutually consistent.
  353.      * @param       newValue   the new value of the scroll bar. 
  354.      * @see         java.awt.Scrollbar#setValues
  355.      * @see         java.awt.Scrollbar#getValue
  356.      * @see         java.awt.Scrollbar#getMinimum
  357.      * @see         java.awt.Scrollbar#getMaximum
  358.      * @since       JDK1.0
  359.      */
  360.     public synchronized void setValue(int newValue) {
  361.     /* Use setValues so that a consistent policy
  362.          * relating minimum, maximum, and value is enforced.
  363.          */
  364.         setValues(newValue, visibleAmount, minimum, maximum);
  365.     }
  366.  
  367.     /**
  368.      * Gets the minimum value of this scroll bar.
  369.      * @return      the minimum value of this scroll bar.
  370.      * @see         java.awt.Scrollbar#getValue
  371.      * @see         java.awt.Scrollbar#getMaximum
  372.      * @since       JDK1.0
  373.      */
  374.     public int getMinimum() {
  375.     return minimum;
  376.     }
  377.  
  378.     /**
  379.      * Sets the minimum value of this scroll bar.
  380.      * <p>
  381.      * Normally, a program should change a scroll bar's minimum 
  382.      * value only by calling <code>setValues</code>. 
  383.      * The <code>setValues</code> method simultaneously 
  384.      * and synchronously sets the minimum, maximum, visible amount, 
  385.      * and value properties of a scroll bar, so that they are 
  386.      * mutually consistent.
  387.      * @param       newMinimum   the new minimum value  
  388.      *                     for this scroll bar.
  389.      * @see         java.awt.Scrollbar#setValues
  390.      * @see         java.awt.Scrollbar#setMaximum
  391.      * @since       JDK1.1
  392.      */
  393.     public synchronized void setMinimum(int newMinimum) {
  394.     /* Use setValues so that a consistent policy
  395.          * relating minimum, maximum, and value is enforced.
  396.          */
  397.     setValues(value, visibleAmount, newMinimum, maximum);
  398.     }
  399.  
  400.     /**
  401.      * Gets the maximum value of this scroll bar.
  402.      * @return      the maximum value of this scroll bar.
  403.      * @see         java.awt.Scrollbar#getValue
  404.      * @see         java.awt.Scrollbar#getMinimum
  405.      * @since       JDK1.0
  406.      */
  407.     public int getMaximum() {
  408.     return maximum;
  409.     }
  410.  
  411.     /**
  412.      * Sets the maximum value of this scroll bar.
  413.      * <p>
  414.      * Normally, a program should change a scroll bar's maximum 
  415.      * value only by calling <code>setValues</code>. 
  416.      * The <code>setValues</code> method simultaneously 
  417.      * and synchronously sets the minimum, maximum, visible amount, 
  418.      * and value properties of a scroll bar, so that they are 
  419.      * mutually consistent.
  420.      * @param       newMaximum   the new maximum value  
  421.      *                     for this scroll bar.
  422.      * @see         java.awtScrollbar#setValues
  423.      * @see         java.awtScrollbar#setMinimum
  424.      * @since       JDK1.1
  425.      */
  426.     public synchronized void setMaximum(int newMaximum) {
  427.     /* Use setValues so that a consistent policy
  428.          * relating minimum, maximum, and value is enforced.
  429.          */
  430.         setValues(value, visibleAmount, minimum, newMaximum);
  431.     }
  432.  
  433.     /**
  434.      * Gets the visible amount of this scroll bar.
  435.      * <p>
  436.      * The visible amount of a scroll bar is the range of 
  437.      * values represented by the width of the scroll bar's 
  438.      * bubble. It is used to determine the scroll bar's 
  439.      * block increment.
  440.      * @return      the visible amount of this scroll bar.
  441.      * @see         java.awt.Scrollbar#setVisibleAmount
  442.      * @since       JDK1.1
  443.      */
  444.     public int getVisibleAmount() {
  445.     return getVisible();
  446.     }
  447.  
  448.     /**
  449.      * @deprecated As of JDK version 1.1,
  450.      * replaced by <code>getVisibleAmount()</code>.
  451.      */
  452.     public int getVisible() {
  453.     return visibleAmount;
  454.     }
  455.  
  456.     /**
  457.      * Sets the visible amount of this scroll bar.
  458.      * <p>
  459.      * The visible amount of a scroll bar is the range of 
  460.      * values represented by the width of the scroll bar's 
  461.      * bubble. It is used to determine the scroll bar's 
  462.      * block increment.
  463.      * <p>
  464.      * Normally, a program should change a scroll bar's  
  465.      * value only by calling <code>setValues</code>. 
  466.      * The <code>setValues</code> method simultaneously 
  467.      * and synchronously sets the minimum, maximum, visible amount, 
  468.      * and value properties of a scroll bar, so that they are 
  469.      * mutually consistent.
  470.      * @param       newAmount the amount visible per page.
  471.      * @see         java.awt.Scrollbar#getVisibleAmount
  472.      * @see         java.awt.Scrollbar#setValues
  473.      * @since       JDK1.1
  474.      */
  475.     public synchronized void setVisibleAmount(int newAmount) {
  476.         setValues(value, newAmount, minimum, maximum);
  477.     }
  478.  
  479.     /**
  480.      * Sets the unit increment for this scroll bar. 
  481.      * <p>
  482.      * The unit increment is the value that is added (subtracted) 
  483.      * when the user activates the unit increment area of the 
  484.      * scroll bar, generally through a mouse or keyboard gesture
  485.      * that the scroll bar receives as an adjustment event.
  486.      * @param        v  the amount by which to increment or decrement
  487.      *                         the scroll bar's value.
  488.      * @see          java.awt.Scrollbar#getUnitIncrement
  489.      * @since        JDK1.1
  490.      */
  491.     public synchronized void setUnitIncrement(int v) {
  492.     setLineIncrement(v);
  493.     }
  494.  
  495.     /**
  496.      * @deprecated As of JDK version 1.1,
  497.      * replaced by <code>setUnitIncrement(int)</code>.
  498.      */
  499.     public void setLineIncrement(int v) {
  500.     lineIncrement = v;
  501.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  502.     if (peer != null) {
  503.         peer.setLineIncrement(v);
  504.     }
  505.     }
  506.  
  507.     /**
  508.      * Gets the unit increment for this scrollbar.
  509.      * <p>
  510.      * The unit increment is the value that is added (subtracted) 
  511.      * when the user activates the unit increment area of the 
  512.      * scroll bar, generally through a mouse or keyboard gesture
  513.      * that the scroll bar receives as an adjustment event.
  514.      * @return      the unit increment of this scroll bar.
  515.      * @see         java.awt.Scrollbar#setUnitIncrement
  516.      * @since       JDK1.1
  517.      */
  518.     public int getUnitIncrement() {
  519.     return getLineIncrement();
  520.     }
  521.  
  522.     /**
  523.      * @deprecated As of JDK version 1.1,
  524.      * replaced by <code>getUnitIncrement()</code>.
  525.      */
  526.     public int getLineIncrement() {
  527.     return lineIncrement;
  528.     }
  529.  
  530.     /**
  531.      * Sets the block increment for this scroll bar. 
  532.      * <p>
  533.      * The block increment is the value that is added (subtracted) 
  534.      * when the user activates the block increment area of the 
  535.      * scroll bar, generally through a mouse or keyboard gesture
  536.      * that the scroll bar receives as an adjustment event.
  537.      * @param        v  the amount by which to increment or decrement
  538.      *                         the scroll bar's value.
  539.      * @see          java.awt.Scrollbar#getBlockIncrement
  540.      * @since        JDK1.1
  541.      */
  542.     public synchronized void setBlockIncrement(int v) {
  543.     setPageIncrement(v);
  544.     }
  545.  
  546.     /**
  547.      * @deprecated As of JDK version 1.1,
  548.      * replaced by <code>setBlockIncrement()</code>.
  549.      */
  550.     public void setPageIncrement(int v) {
  551.     pageIncrement = v;
  552.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  553.     if (peer != null) {
  554.         peer.setPageIncrement(v);
  555.     }
  556.     }
  557.  
  558.     /**
  559.      * Gets the block increment of this scroll bar.
  560.      * <p>
  561.      * The block increment is the value that is added (subtracted) 
  562.      * when the user activates the block increment area of the 
  563.      * scroll bar, generally through a mouse or keyboard gesture
  564.      * that the scroll bar receives as an adjustment event.
  565.      * @return      the block increment of this scroll bar.
  566.      * @see         java.awt.Scrollbar#setBlockIncrement
  567.      * @since       JDK1.1
  568.      */
  569.     public int getBlockIncrement() {
  570.     return getPageIncrement();
  571.     }
  572.  
  573.     /**
  574.      * @deprecated As of JDK version 1.1,
  575.      * replaced by <code>getBlockIncrement()</code>.
  576.      */
  577.     public int getPageIncrement() {
  578.     return pageIncrement;
  579.     }
  580.  
  581.     /**
  582.      * Sets the values of four properties for this scroll bar.
  583.      * <p>
  584.      * This method simultaneously and synchronously sets the values 
  585.      * of four scroll bar properties, assuring that the values of
  586.      * these properties are mutually consistent. It enforces the 
  587.      * constraints that maximum cannot be less than minimum, and that 
  588.      * value cannot be less than the minimum or greater than the maximum.
  589.      * @param      value is the position in the current window.
  590.      * @param      visible is the amount visible per page.
  591.      * @param      minimum is the minimum value of the scroll bar.
  592.      * @param      maximum is the maximum value of the scroll bar.
  593.      * @since      JDK1.0
  594.      */
  595.     public synchronized void setValues(int value, int visible, int minimum, int maximum) {
  596.     if (maximum <= minimum) {
  597.         maximum = minimum + 1;
  598.     }
  599.     if (visible > maximum - minimum) {
  600.       visible = maximum - minimum;
  601.     }
  602.     if (visible < 1) {
  603.       visible = 1;
  604.     }
  605.     if (value < minimum) {
  606.         value = minimum;
  607.     }
  608.     if (value > maximum - visible) {
  609.         value = maximum - visible;
  610.     }
  611.  
  612.     this.value = value;
  613.     this.visibleAmount = visible;
  614.     this.minimum = minimum;
  615.     this.maximum = maximum;
  616.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  617.     if (peer != null) {
  618.         peer.setValues(value, visibleAmount, minimum, maximum);
  619.     }
  620.     }
  621.  
  622.     /**
  623.      * Adds the specified adjustment listener to receive instances of 
  624.      * <code>AdjustmentEvent</code> from this scroll bar.
  625.      * @param        l the adjustment listener.
  626.      * @see          java.awt.event.AdjustmentEvent
  627.      * @see          java.awt.event.AdjustmentListener
  628.      * @see          java.awt.Scrollbar#removeAdjustmentListener
  629.      * @since        JDK1.1
  630.      */ 
  631.     public synchronized void addAdjustmentListener(AdjustmentListener l) {
  632.     adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l);
  633.         newEventsOnly = true;
  634.     }
  635.  
  636.     /**
  637.      * Removes the specified adjustment listener so that it no longer 
  638.      * receives instances of <code>AdjustmentEvent</code> from this scroll bar.
  639.      * @param        l    the adjustment listener.
  640.      * @see          java.awt.event.AdjustmentEvent
  641.      * @see          java.awt.event.AdjustmentListener
  642.      * @see          java.awt.Scrollbar#addAdjustmentListener
  643.      * @since        JDK1.1
  644.      */ 
  645.     public synchronized void removeAdjustmentListener(AdjustmentListener l) {
  646.     adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l);
  647.     }
  648.  
  649.     // REMIND: remove when filtering is done at lower level
  650.     boolean eventEnabled(AWTEvent e) {
  651.         if (e.id == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) {
  652.             if ((eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0 ||
  653.                 adjustmentListener != null) {
  654.                 return true;
  655.             } 
  656.             return false;
  657.         }
  658.         return super.eventEnabled(e);
  659.     }     
  660.  
  661.     /**
  662.      * Processes events on this scroll bar. If the event is an 
  663.      * instance of <code>AdjustmentEvent</code>, it invokes the 
  664.      * <code>processAdjustmentEvent</code> method. 
  665.      * Otherwise, it invokes its superclass's 
  666.      * <code>processEvent</code> method.
  667.      * @param        e the event.
  668.      * @see          java.awt.event.AdjustmentEvent
  669.      * @see          java.awt.Scrollbar#processAdjustmentEvent
  670.      * @since        JDK1.1
  671.      */
  672.     protected void processEvent(AWTEvent e) {
  673.         if (e instanceof AdjustmentEvent) {
  674.             processAdjustmentEvent((AdjustmentEvent)e);
  675.             return;
  676.         }
  677.     super.processEvent(e);
  678.     }
  679.  
  680.     /** 
  681.      * Processes adjustment events occurring on this 
  682.      * scrollbar by dispatching them to any registered 
  683.      * <code>AdjustmentListener</code> objects.
  684.      * <p>
  685.      * This method is not called unless adjustment events are 
  686.      * enabled for this component. Adjustment events are enabled 
  687.      * when one of the following occurs:
  688.      * <p><ul>
  689.      * <li>An <code>AdjustmentListener</code> object is registered 
  690.      * via <code>addAdjustmentListener</code>.
  691.      * <li>Adjustment events are enabled via <code>enableEvents</code>.
  692.      * </ul><p>
  693.      * @param       e the adjustment event.
  694.      * @see         java.awt.event.AdjustmentEvent
  695.      * @see         java.awt.event.AdjustmentListener
  696.      * @see         java.awt.Scrollbar#addAdjustmentListener
  697.      * @see         java.awt.Component#enableEvents
  698.      * @since       JDK1.1
  699.      */ 
  700.     protected void processAdjustmentEvent(AdjustmentEvent e) {
  701.         if (adjustmentListener != null) {
  702.             adjustmentListener.adjustmentValueChanged(e);
  703.         }
  704.     }
  705.  
  706.     /**
  707.      * Returns the parameter string representing the state of 
  708.      * this scroll bar. This string is useful for debugging.
  709.      * @return      the parameter string of this scroll bar.
  710.      * @since       JDK1.0
  711.      */
  712.     protected String paramString() {
  713.     return super.paramString() +
  714.         ",val=" + value +
  715.         ",vis=" + visibleAmount +
  716.         ",min=" + minimum +
  717.         ",max=" + maximum +
  718.         ((orientation == VERTICAL) ? ",vert" : ",horz");
  719.     }
  720.  
  721.  
  722.     /* Serialization support. 
  723.      */
  724.  
  725.     private int scrollbarSerializedDataVersion = 1;
  726.  
  727.     private void writeObject(ObjectOutputStream s)
  728.       throws IOException 
  729.     {
  730.       s.defaultWriteObject();
  731.  
  732.       AWTEventMulticaster.save(s, adjustmentListenerK, adjustmentListener);
  733.       s.writeObject(null);
  734.     }
  735.  
  736.     private void readObject(ObjectInputStream s)
  737.       throws ClassNotFoundException, IOException 
  738.     {
  739.       s.defaultReadObject();
  740.  
  741.       Object keyOrNull;
  742.       while(null != (keyOrNull = s.readObject())) {
  743.     String key = ((String)keyOrNull).intern();
  744.  
  745.     if (adjustmentListenerK == key) 
  746.       addAdjustmentListener((AdjustmentListener)(s.readObject()));
  747.  
  748.     else // skip value for unrecognized key
  749.       s.readObject();
  750.       }
  751.     }
  752.  
  753. }
  754.