home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / Scrollbar.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  25.1 KB  |  744 lines

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