home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / Scrollbar.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  29.3 KB  |  889 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Scrollbar.java    1.65 98/08/24
  3.  *
  4.  * Copyright 1995-1998 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="doc-files/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="doc-files/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, 60, 0, 300);
  57.  * add(ranger);
  58.  * </pre></blockquote><hr>
  59.  * <p>
  60.  * Note that the actual maximum value of the scroll bar is the
  61.  * <code>maximum</code> minus the <code>visible</code>.
  62.  * In the previous example, because the <code>maximum</code> is
  63.  * 300 and the <code>visible</code> is 60, the actual maximum
  64.  * value is 240.  The range of the scrollbar track is 0 - 300.
  65.  * The left side of the bubble indicates the value of the
  66.  * scroll bar.
  67.  * <p>
  68.  * Normally, the user changes the value of the scroll bar by
  69.  * making a gesture with the mouse. For example, the user can
  70.  * drag the scroll bar's bubble up and down, or click in the
  71.  * scroll bar's unit increment or block increment areas. Keyboard
  72.  * gestures can also be mapped to the scroll bar. By convention,
  73.  * the <b>Page Up</b> and <b>Page Down</b>
  74.  * keys are equivalent to clicking in the scroll bar's block
  75.  * increment and block decrement areas.
  76.  * <p>
  77.  * When the user changes the value of the scroll bar, the scroll bar
  78.  * receives an instance of <code>AdjustmentEvent</code>.
  79.  * The scroll bar processes this event, passing it along to
  80.  * any registered listeners.
  81.  * <p>
  82.  * Any object that wishes to be notified of changes to the
  83.  * scroll bar's value should implement
  84.  * <code>AdjustmentListener</code>, an interface defined in
  85.  * the package <code>java.awt.event</code>.
  86.  * Listeners can be added and removed dynamically by calling
  87.  * the methods <code>addAdjustmentListener</code> and
  88.  * <code>removeAdjustmentListener</code>.
  89.  * <p>
  90.  * The <code>AdjustmentEvent</code> class defines five types
  91.  * of adjustment event, listed here:
  92.  * <p>
  93.  * <ul>
  94.  * <li><code>AdjustmentEvent.TRACK</code> is sent out when the
  95.  * user drags the scroll bar's bubble.
  96.  * <li><code>AdjustmentEvent.UNIT_INCREMENT</code> is sent out
  97.  * when the user clicks in the left arrow of a horizontal scroll
  98.  * bar, or the top arrow of a vertical scroll bar, or makes the
  99.  * equivalent gesture from the keyboard.
  100.  * <li><code>AdjustmentEvent.UNIT_DECREMENT</code> is sent out
  101.  * when the user clicks in the right arrow of a horizontal scroll
  102.  * bar, or the bottom arrow of a vertical scroll bar, or makes the
  103.  * equivalent gesture from the keyboard.
  104.  * <li><code>AdjustmentEvent.BLOCK_INCREMENT</code> is sent out
  105.  * when the user clicks in the track, to the left of the bubble
  106.  * on a horizontal scroll bar, or above the bubble on a vertical
  107.  * scroll bar. By convention, the <b>Page Up</b>
  108.  * key is equivalent, if the user is using a keyboard that
  109.  * defines a <b>Page Up</b> key.
  110.  * <li><code>AdjustmentEvent.BLOCK_DECREMENT</code> is sent out
  111.  * when the user clicks in the track, to the right of the bubble
  112.  * on a horizontal scroll bar, or below the bubble on a vertical
  113.  * scroll bar. By convention, the <b>Page Down</b>
  114.  * key is equivalent, if the user is using a keyboard that
  115.  * defines a <b>Page Down</b> key.
  116.  * </ul>
  117.  * <p>
  118.  * The JDK 1.0 event system is supported for backwards
  119.  * compatibility, but its use with newer versions of JDK is
  120.  * discouraged. The fives types of adjustment event introduced
  121.  * with JDK 1.1 correspond to the five event types
  122.  * that are associated with scroll bars in previous JDK versions.
  123.  * The following list gives the adjustment event type,
  124.  * and the corresponding JDK 1.0 event type it replaces.
  125.  * <p>
  126.  * <ul>
  127.  * <li><code>AdjustmentEvent.TRACK</code> replaces
  128.  * <code>Event.SCROLL_ABSOLUTE</code>
  129.  * <li><code>AdjustmentEvent.UNIT_INCREMENT</code> replaces
  130.  * <code>Event.SCROLL_LINE_UP</code>
  131.  * <li><code>AdjustmentEvent.UNIT_DECREMENT</code> replaces
  132.  * <code>Event.SCROLL_LINE_DOWN</code>
  133.  * <li><code>AdjustmentEvent.BLOCK_INCREMENT</code> replaces
  134.  * <code>Event.SCROLL_PAGE_UP</code>
  135.  * <li><code>AdjustmentEvent.BLOCK_DECREMENT</code> replaces
  136.  * <code>Event.SCROLL_PAGE_DOWN</code>
  137.  * </ul>
  138.  * <p>
  139.  *
  140.  * @version    1.65 08/24/98
  141.  * @author     Sami Shaio
  142.  * @see         java.awt.event.AdjustmentEvent
  143.  * @see         java.awt.event.AdjustmentListener
  144.  * @since       JDK1.0
  145.  */
  146. public class Scrollbar extends Component implements Adjustable {
  147.  
  148.     /**
  149.      * A constant that indicates a horizontal scroll bar.
  150.      */
  151.     public static final int    HORIZONTAL = 0;
  152.  
  153.     /**
  154.      * A constant that indicates a vertical scroll bar.
  155.      */
  156.     public static final int    VERTICAL   = 1;
  157.  
  158.     /**
  159.      * The value of the Scrollbar.
  160.      * value should be either greater than <code>minimum</code>
  161.      * or less that <code>maximum</code>
  162.      *
  163.      * @serial
  164.      * @see getValue()
  165.      * @see setValue()
  166.      */
  167.     int    value;
  168.  
  169.     /**
  170.      * The maximum value of the Scrollbar.
  171.      * This value must be greater than the <code>minimum</code>
  172.      * value.<br>
  173.      * This integer can be either positive or negative, and
  174.      * it's range can be altered at any time.
  175.      *
  176.      * @serial
  177.      * @see getMaximum()
  178.      * @see setMaximum()
  179.      */
  180.     int    maximum;
  181.  
  182.     /**
  183.      * The minimum value of the Scrollbar.
  184.      * This value must be greater than the <code>minimum</code>
  185.      * value.<br>
  186.      * This integer can be either positive or negative.
  187.      *
  188.      * @serial
  189.      * @see getMinimum()
  190.      * @see setMinimum()
  191.      */
  192.     int    minimum;
  193.  
  194.     /**
  195.      * The size of the visible portion of the Scrollbar.
  196.      * The size of the scrollbox is normally used to indicate
  197.      * the visibleAmount.
  198.      *
  199.      * @serial
  200.      * @see getVisibleAmount()
  201.      * @see setVisibleAmount()
  202.      */
  203.     int    visibleAmount;
  204.  
  205.     /**
  206.      * The Scrollbar's orientation--being either horizontal or vertical.
  207.      * This value should be specified when the scrollbar is being
  208.      * created.<BR>
  209.      * orientation can be either : <code>VERTICAL</code> or
  210.      * <code>HORIZONTAL</code> only.
  211.      *
  212.      * @serial
  213.      * @see getOrientation()
  214.      * @see setOrientation()
  215.      */
  216.     int    orientation;
  217.  
  218.     /**
  219.      * The amount by which the scrollbar value will change when going
  220.      * up or down by a line.
  221.      * This value should be a non negative integer.
  222.      *
  223.      * @serial
  224.      * @see setLineIncrement()
  225.      * @see getLineIncrement()
  226.      */
  227.     int lineIncrement = 1;
  228.  
  229.     /**
  230.      * The amount by which the scrollbar value will change when going
  231.      * up or down by a page.
  232.      * This value should be a non negative integer.
  233.      *
  234.      * @serial
  235.      * @see setPageIncrement()
  236.      * @see getPageIncrement()
  237.      */
  238.     int pageIncrement = 10;
  239.  
  240.     transient AdjustmentListener adjustmentListener;
  241.  
  242.     private static final String base = "scrollbar";
  243.     private static int nameCounter = 0;
  244.  
  245.     /*
  246.      * JDK 1.1 serialVersionUID
  247.      */
  248.     private static final long serialVersionUID = 8451667562882310543L;
  249.  
  250.     /**
  251.      * Initialize JNI field and method IDs
  252.      */
  253.     private static native void initIDs();
  254.  
  255.     static {
  256.         /* ensure that the necessary native libraries are loaded */
  257.     Toolkit.loadLibraries();
  258.     initIDs();
  259.     }
  260.  
  261.     /**
  262.      * Constructs a new vertical scroll bar.
  263.      * The default properties of the scroll bar are listed in
  264.      * the following table:
  265.      * <p> </p>
  266.      * <table border>
  267.      * <tr>
  268.      *   <th>Property</th>
  269.      *   <th>Description</th>
  270.      *   <th>Default Value</th>
  271.      * </tr>
  272.      * <tr>
  273.      *   <td>orientation</td>
  274.      *   <td>indicates if the scroll bar is vertical or horizontal</td>
  275.      *   <td><code>Scrollbar.VERTICAL</code></td>
  276.      * </tr>
  277.      * <tr>
  278.      *   <td>value</td>
  279.      *   <td>value which controls the location
  280.      *   <br>of the scroll bar bubble</td>
  281.      *   <td>0</td>
  282.      * </tr>
  283.      * <tr>
  284.      *   <td>minimum</td>
  285.      *   <td>minimum value of the scroll bar</td>
  286.      *   <td>0</td>
  287.      * </tr>
  288.      * <tr>
  289.      *   <td>maximum</td>
  290.      *   <td>maximum value of the scroll bar</td>
  291.      *   <td>100</td>
  292.      * </tr>
  293.      * <tr>
  294.      *   <td>unit increment</td>
  295.      *   <td>amount the value changes when the
  296.      *   <br>Line Up or Line Down key is pressed,
  297.      *   <br>or when the end arrows of the scrollbar
  298.      *   <br>are clicked </td>
  299.      *   <td>1</td>
  300.      * </tr>
  301.      * <tr>
  302.      *   <td>block increment</td>
  303.      *   <td>amount the value changes when the
  304.      *   <br>Page Up or Page Down key is pressed,
  305.      *   <br>or when the scrollbar track is clicked 
  306.      *   <br>on either side of the bubble </td>
  307.      *   <td>10</td>
  308.      * </tr>
  309.      * </table>
  310.      *
  311.      */
  312.     public Scrollbar() {
  313.     this(VERTICAL, 0, 10, 0, 100);
  314.     }
  315.  
  316.     /**
  317.      * Constructs a new scroll bar with the specified orientation.
  318.      * <p>
  319.      * The <code>orientation</code> argument must take one of the two
  320.      * values <code>Scrollbar.HORIZONTAL</code>,
  321.      * or <code>Scrollbar.VERTICAL</code>,
  322.      * indicating a horizontal or vertical scroll bar, respectively.
  323.      * @param       orientation   indicates the orientation of the scroll bar.
  324.      * @exception   IllegalArgumentException    when an illegal value for
  325.      *                    the <code>orientation</code> argument is supplied.
  326.      */
  327.     public Scrollbar(int orientation) {
  328.         this(orientation, 0, 10, 0, 100);
  329.     }
  330.  
  331.     /**
  332.      * Constructs a new scroll bar with the specified orientation,
  333.      * initial value, page size, and minimum and maximum values.
  334.      * <p>
  335.      * The <code>orientation</code> argument must take one of the two
  336.      * values <code>Scrollbar.HORIZONTAL</code>,
  337.      * or <code>Scrollbar.VERTICAL</code>,
  338.      * indicating a horizontal or vertical scroll bar, respectively.
  339.      * <p>
  340.      * If the specified maximum value is less than the minimum value, it
  341.      * is changed to be the same as the minimum value. If the initial
  342.      * value is lower than the minimum value, it is changed to be the
  343.      * minimum value; if it is greater than the maximum value, it is
  344.      * changed to be the maximum value.
  345.      * @param     orientation   indicates the orientation of the scroll bar.
  346.      * @param     value     the initial value of the scroll bar.
  347.      * @param     visible   the size of the scroll bar's bubble, representing
  348.      *                      the visible portion; the scroll bar uses this
  349.                             value when paging up or down by a page.
  350.      * @param     minimum   the minimum value of the scroll bar.
  351.      * @param     maximum   the maximum value of the scroll bar.
  352.      */
  353.     public Scrollbar(int orientation, int value, int visible, int minimum, int maximum) {
  354.     switch (orientation) {
  355.       case HORIZONTAL:
  356.       case VERTICAL:
  357.         this.orientation = orientation;
  358.         break;
  359.       default:
  360.         throw new IllegalArgumentException("illegal scrollbar orientation");
  361.     }
  362.     setValues(value, visible, minimum, maximum);
  363.     }
  364.  
  365.     /**
  366.      * Construct a name for this component.  Called by getName() when the
  367.      * name is null.
  368.      */
  369.     String constructComponentName() {
  370.         synchronized (getClass()) {
  371.         return base + nameCounter++;
  372.     }
  373.     }
  374.  
  375.     /**
  376.      * Creates the Scrollbar's peer.  The peer allows you to modify
  377.      * the appearance of the Scrollbar without changing any of its
  378.      * functionality.
  379.      */
  380.     public void addNotify() {
  381.         synchronized (getTreeLock()) {
  382.         if (peer == null) 
  383.             peer = getToolkit().createScrollbar(this);
  384.         super.addNotify();
  385.     }
  386.     }
  387.  
  388.     /**
  389.      * Determines the orientation of this scroll bar.
  390.      * @return    the orientation of this scroll bar, either
  391.      *               <code>Scrollbar.HORIZONTAL</code> or
  392.      *               <code>Scrollbar.VERTICAL</code>.
  393.      * @see       java.awt.Scrollbar#setOrientation
  394.      */
  395.     public int getOrientation() {
  396.     return orientation;
  397.     }
  398.  
  399.     /**
  400.      * Sets the orientation for this scroll bar.
  401.      * @param     the orientation of this scroll bar, either
  402.      *               <code>Scrollbar.HORIZONTAL</code> or
  403.      *               <code>Scrollbar.VERTICAL</code>.
  404.      * @see       java.awt.Scrollbar#getOrientation
  405.      * @exception   IllegalArgumentException  if the value supplied
  406.      *                   for <code>orientation</code> is not a
  407.      *                   legal value.
  408.      * @since     JDK1.1
  409.      */
  410.     public void setOrientation(int orientation) {
  411.         synchronized (getTreeLock()) {
  412.         if (orientation == this.orientation) {
  413.             return;
  414.         }
  415.         switch (orientation) {
  416.             case HORIZONTAL:
  417.             case VERTICAL:
  418.             this.orientation = orientation;
  419.             break;
  420.             default:
  421.             throw new IllegalArgumentException("illegal scrollbar orientation");
  422.         }
  423.         /* Create a new peer with the specified orientation. */
  424.         if (peer != null) {
  425.         removeNotify();
  426.         addNotify();
  427.         invalidate();
  428.         }
  429.     }
  430.     }
  431.  
  432.     /**
  433.      * Gets the current value of this scroll bar.
  434.      * @return      the current value of this scroll bar.
  435.      * @see         java.awt.Scrollbar#getMinimum
  436.      * @see         java.awt.Scrollbar#getMaximum
  437.      */
  438.     public int getValue() {
  439.     return value;
  440.     }
  441.  
  442.     /**
  443.      * Sets the value of this scroll bar to the specified value.
  444.      * <p>
  445.      * If the value supplied is less than the current minimum or
  446.      * greater than the current maximum, then one of those values
  447.      * is substituted, as appropriate.
  448.      * <p>
  449.      * Normally, a program should change a scroll bar's
  450.      * value only by calling <code>setValues</code>.
  451.      * The <code>setValues</code> method simultaneously
  452.      * and synchronously sets the minimum, maximum, visible amount,
  453.      * and value properties of a scroll bar, so that they are
  454.      * mutually consistent.
  455.      * @param       newValue   the new value of the scroll bar.
  456.      * @see         java.awt.Scrollbar#setValues
  457.      * @see         java.awt.Scrollbar#getValue
  458.      * @see         java.awt.Scrollbar#getMinimum
  459.      * @see         java.awt.Scrollbar#getMaximum
  460.      */
  461.     public void setValue(int newValue) {
  462.     /* Use setValues so that a consistent policy
  463.          * relating minimum, maximum, and value is enforced.
  464.          */
  465.         setValues(newValue, visibleAmount, minimum, maximum);
  466.     }
  467.  
  468.     /**
  469.      * Gets the minimum value of this scroll bar.
  470.      * @return      the minimum value of this scroll bar.
  471.      * @see         java.awt.Scrollbar#getValue
  472.      * @see         java.awt.Scrollbar#getMaximum
  473.      */
  474.     public int getMinimum() {
  475.     return minimum;
  476.     }
  477.  
  478.     /**
  479.      * Sets the minimum value of this scroll bar.
  480.      * <p>
  481.      * Normally, a program should change a scroll bar's minimum
  482.      * value only by calling <code>setValues</code>.
  483.      * The <code>setValues</code> method simultaneously
  484.      * and synchronously sets the minimum, maximum, visible amount,
  485.      * and value properties of a scroll bar, so that they are
  486.      * mutually consistent.
  487.      * @param       newMinimum   the new minimum value
  488.      *                     for this scroll bar.
  489.      * @see         java.awt.Scrollbar#setValues
  490.      * @see         java.awt.Scrollbar#setMaximum
  491.      * @since       JDK1.1
  492.      */
  493.     public void setMinimum(int newMinimum) {
  494.     /* Use setValues so that a consistent policy
  495.          * relating minimum, maximum, and value is enforced.
  496.          */
  497.     setValues(value, visibleAmount, newMinimum, maximum);
  498.     }
  499.  
  500.     /**
  501.      * Gets the maximum value of this scroll bar.
  502.      * @return      the maximum value of this scroll bar.
  503.      * @see         java.awt.Scrollbar#getValue
  504.      * @see         java.awt.Scrollbar#getMinimum
  505.      */
  506.     public int getMaximum() {
  507.     return maximum;
  508.     }
  509.  
  510.     /**
  511.      * Sets the maximum value of this scroll bar.
  512.      * <p>
  513.      * Normally, a program should change a scroll bar's maximum
  514.      * value only by calling <code>setValues</code>.
  515.      * The <code>setValues</code> method simultaneously
  516.      * and synchronously sets the minimum, maximum, visible amount,
  517.      * and value properties of a scroll bar, so that they are
  518.      * mutually consistent.
  519.      * @param       newMaximum   the new maximum value
  520.      *                     for this scroll bar.
  521.      * @see         java.awtScrollbar#setValues
  522.      * @see         java.awtScrollbar#setMinimum
  523.      * @since       JDK1.1
  524.      */
  525.     public void setMaximum(int newMaximum) {
  526.     /* Use setValues so that a consistent policy
  527.          * relating minimum, maximum, and value is enforced.
  528.          */
  529.         setValues(value, visibleAmount, minimum, newMaximum);
  530.     }
  531.  
  532.     /**
  533.      * Gets the visible amount of this scroll bar.
  534.      * <p>
  535.      * The visible amount of a scroll bar is the range of
  536.      * values represented by the width of the scroll bar's
  537.      * bubble. It is used to determine the scroll bar's
  538.      * block increment.
  539.      * @return      the visible amount of this scroll bar.
  540.      * @see         java.awt.Scrollbar#setVisibleAmount
  541.      * @since       JDK1.1
  542.      */
  543.     public int getVisibleAmount() {
  544.     return getVisible();
  545.     }
  546.  
  547.     /**
  548.      * @deprecated As of JDK version 1.1,
  549.      * replaced by <code>getVisibleAmount()</code>.
  550.      */
  551.     public int getVisible() {
  552.     return visibleAmount;
  553.     }
  554.  
  555.     /**
  556.      * Sets the visible amount of this scroll bar.
  557.      * <p>
  558.      * The visible amount of a scroll bar is the range of
  559.      * values represented by the width of the scroll bar's
  560.      * bubble. It is used to determine the scroll bar's
  561.      * block increment.
  562.      * <p>
  563.      * Normally, a program should change a scroll bar's
  564.      * value only by calling <code>setValues</code>.
  565.      * The <code>setValues</code> method simultaneously
  566.      * and synchronously sets the minimum, maximum, visible amount,
  567.      * and value properties of a scroll bar, so that they are
  568.      * mutually consistent.
  569.      * @param       newAmount the amount visible per page.
  570.      * @see         java.awt.Scrollbar#getVisibleAmount
  571.      * @see         java.awt.Scrollbar#setValues
  572.      * @since       JDK1.1
  573.      */
  574.     public void setVisibleAmount(int newAmount) {
  575.         setValues(value, newAmount, minimum, maximum);
  576.     }
  577.  
  578.     /**
  579.      * Sets the unit increment for this scroll bar.
  580.      * <p>
  581.      * The unit increment is the value that is added (subtracted)
  582.      * when the user activates the unit increment area of the
  583.      * scroll bar, generally through a mouse or keyboard gesture
  584.      * that the scroll bar receives as an adjustment event.
  585.      * @param        v  the amount by which to increment or decrement
  586.      *                         the scroll bar's value.
  587.      * @see          java.awt.Scrollbar#getUnitIncrement
  588.      * @since        JDK1.1
  589.      */
  590.     public void setUnitIncrement(int v) {
  591.     setLineIncrement(v);
  592.     }
  593.  
  594.     /**
  595.      * @deprecated As of JDK version 1.1,
  596.      * replaced by <code>setUnitIncrement(int)</code>.
  597.      */
  598.     public synchronized void setLineIncrement(int v) {
  599.     lineIncrement = v;
  600.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  601.     if (peer != null) {
  602.         peer.setLineIncrement(v);
  603.     }
  604.     }
  605.  
  606.     /**
  607.      * Gets the unit increment for this scrollbar.
  608.      * <p>
  609.      * The unit increment is the value that is added (subtracted)
  610.      * when the user activates the unit increment area of the
  611.      * scroll bar, generally through a mouse or keyboard gesture
  612.      * that the scroll bar receives as an adjustment event.
  613.      * @return      the unit increment of this scroll bar.
  614.      * @see         java.awt.Scrollbar#setUnitIncrement
  615.      * @since       JDK1.1
  616.      */
  617.     public int getUnitIncrement() {
  618.     return getLineIncrement();
  619.     }
  620.  
  621.     /**
  622.      * @deprecated As of JDK version 1.1,
  623.      * replaced by <code>getUnitIncrement()</code>.
  624.      */
  625.     public int getLineIncrement() {
  626.     return lineIncrement;
  627.     }
  628.  
  629.     /**
  630.      * Sets the block increment for this scroll bar.
  631.      * <p>
  632.      * The block increment is the value that is added (subtracted)
  633.      * when the user activates the block increment area of the
  634.      * scroll bar, generally through a mouse or keyboard gesture
  635.      * that the scroll bar receives as an adjustment event.
  636.      * @param        v  the amount by which to increment or decrement
  637.      *                         the scroll bar's value.
  638.      * @see          java.awt.Scrollbar#getBlockIncrement
  639.      * @since        JDK1.1
  640.      */
  641.     public void setBlockIncrement(int v) {
  642.     setPageIncrement(v);
  643.     }
  644.  
  645.     /**
  646.      * @deprecated As of JDK version 1.1,
  647.      * replaced by <code>setBlockIncrement()</code>.
  648.      */
  649.     public synchronized void setPageIncrement(int v) {
  650.     pageIncrement = v;
  651.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  652.     if (peer != null) {
  653.         peer.setPageIncrement(v);
  654.     }
  655.     }
  656.  
  657.     /**
  658.      * Gets the block increment of this scroll bar.
  659.      * <p>
  660.      * The block increment is the value that is added (subtracted)
  661.      * when the user activates the block increment area of the
  662.      * scroll bar, generally through a mouse or keyboard gesture
  663.      * that the scroll bar receives as an adjustment event.
  664.      * @return      the block increment of this scroll bar.
  665.      * @see         java.awt.Scrollbar#setBlockIncrement
  666.      * @since       JDK1.1
  667.      */
  668.     public int getBlockIncrement() {
  669.     return getPageIncrement();
  670.     }
  671.  
  672.     /**
  673.      * @deprecated As of JDK version 1.1,
  674.      * replaced by <code>getBlockIncrement()</code>.
  675.      */
  676.     public int getPageIncrement() {
  677.     return pageIncrement;
  678.     }
  679.  
  680.     /**
  681.      * Sets the values of four properties for this scroll bar.
  682.      * <p>
  683.      * This method simultaneously and synchronously sets the values
  684.      * of four scroll bar properties, assuring that the values of
  685.      * these properties are mutually consistent. It enforces the
  686.      * constraints that maximum cannot be less than minimum, and that
  687.      * value cannot be less than the minimum or greater than the maximum.
  688.      * @param      value is the position in the current window.
  689.      * @param      visible is the amount visible per page.
  690.      * @param      minimum is the minimum value of the scroll bar.
  691.      * @param      maximum is the maximum value of the scroll bar.
  692.      */
  693.     public synchronized void setValues(int value, int visible, int minimum, int maximum) {
  694.     if (maximum <= minimum) {
  695.         maximum = minimum + 1;
  696.     }
  697.     if (visible > maximum - minimum) {
  698.       visible = maximum - minimum;
  699.     }
  700.     if (visible < 1) {
  701.       visible = 1;
  702.     }
  703.     if (value < minimum) {
  704.         value = minimum;
  705.     }
  706.     if (value > maximum - visible) {
  707.         value = maximum - visible;
  708.     }
  709.  
  710.     this.value = value;
  711.     this.visibleAmount = visible;
  712.     this.minimum = minimum;
  713.     this.maximum = maximum;
  714.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  715.     if (peer != null) {
  716.         peer.setValues(value, visibleAmount, minimum, maximum);
  717.     }
  718.     }
  719.  
  720.     /**
  721.      * Adds the specified adjustment listener to receive instances of
  722.      * <code>AdjustmentEvent</code> from this scroll bar.
  723.      * If l is null, no exception is thrown and no action is performed.
  724.      *
  725.      * @param        l the adjustment listener.
  726.      * @see          java.awt.event.AdjustmentEvent
  727.      * @see          java.awt.event.AdjustmentListener
  728.      * @see          java.awt.Scrollbar#removeAdjustmentListener
  729.      * @since        JDK1.1
  730.      */
  731.     public synchronized void addAdjustmentListener(AdjustmentListener l) {
  732.     if (l == null) {
  733.         return;
  734.     }
  735.     adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l);
  736.         newEventsOnly = true;
  737.     }
  738.  
  739.     /**
  740.      * Removes the specified adjustment listener so that it no longer
  741.      * receives instances of <code>AdjustmentEvent</code> from this scroll bar.
  742.      * If l is null, no exception is thrown and no action is performed.
  743.      *
  744.      * @param            l    the adjustment listener.
  745.      * @see              java.awt.event.AdjustmentEvent
  746.      * @see              java.awt.event.AdjustmentListener
  747.      * @see              java.awt.Scrollbar#addAdjustmentListener
  748.      * @since            JDK1.1
  749.      */
  750.     public synchronized void removeAdjustmentListener(AdjustmentListener l) {
  751.     if (l == null) {
  752.         return;
  753.     }
  754.     adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l);
  755.     }
  756.  
  757.     // REMIND: remove when filtering is done at lower level
  758.     boolean eventEnabled(AWTEvent e) {
  759.         if (e.id == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) {
  760.             if ((eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0 ||
  761.                 adjustmentListener != null) {
  762.                 return true;
  763.             }
  764.             return false;
  765.         }
  766.         return super.eventEnabled(e);
  767.     }
  768.  
  769.     /**
  770.      * Processes events on this scroll bar. If the event is an
  771.      * instance of <code>AdjustmentEvent</code>, it invokes the
  772.      * <code>processAdjustmentEvent</code> method.
  773.      * Otherwise, it invokes its superclass's
  774.      * <code>processEvent</code> method.
  775.      * @param        e the event.
  776.      * @see          java.awt.event.AdjustmentEvent
  777.      * @see          java.awt.Scrollbar#processAdjustmentEvent
  778.      * @since        JDK1.1
  779.      */
  780.     protected void processEvent(AWTEvent e) {
  781.         if (e instanceof AdjustmentEvent) {
  782.             processAdjustmentEvent((AdjustmentEvent)e);
  783.             return;
  784.         }
  785.     super.processEvent(e);
  786.     }
  787.  
  788.     /**
  789.      * Processes adjustment events occurring on this
  790.      * scrollbar by dispatching them to any registered
  791.      * <code>AdjustmentListener</code> objects.
  792.      * <p>
  793.      * This method is not called unless adjustment events are
  794.      * enabled for this component. Adjustment events are enabled
  795.      * when one of the following occurs:
  796.      * <p><ul>
  797.      * <li>An <code>AdjustmentListener</code> object is registered
  798.      * via <code>addAdjustmentListener</code>.
  799.      * <li>Adjustment events are enabled via <code>enableEvents</code>.
  800.      * </ul><p>
  801.      * @param       e the adjustment event.
  802.      * @see         java.awt.event.AdjustmentEvent
  803.      * @see         java.awt.event.AdjustmentListener
  804.      * @see         java.awt.Scrollbar#addAdjustmentListener
  805.      * @see         java.awt.Component#enableEvents
  806.      * @since       JDK1.1
  807.      */
  808.     protected void processAdjustmentEvent(AdjustmentEvent e) {
  809.         if (adjustmentListener != null) {
  810.             adjustmentListener.adjustmentValueChanged(e);
  811.         }
  812.     }
  813.  
  814.     /**
  815.      * Returns the parameter string representing the state of
  816.      * this scroll bar. This string is useful for debugging.
  817.      * @return      the parameter string of this scroll bar.
  818.      */
  819.     protected String paramString() {
  820.     return super.paramString() +
  821.         ",val=" + value +
  822.         ",vis=" + visibleAmount +
  823.         ",min=" + minimum +
  824.         ",max=" + maximum +
  825.         ((orientation == VERTICAL) ? ",vert" : ",horz");
  826.     }
  827.  
  828.  
  829.     /* Serialization support.
  830.      */
  831.     /**
  832.      * The scrollbars serialized Data Version.
  833.      *
  834.      * @serial
  835.      */
  836.     private int scrollbarSerializedDataVersion = 1;
  837.  
  838.     /**
  839.      * Writes default serializable fields to stream.  Writes
  840.      * a list of serializable ItemListener(s) as optional data.
  841.      * The non-serializable ItemListner(s) are detected and
  842.      * no attempt is made to serialize them.
  843.      *
  844.      * @serialData Null terminated sequence of 0 or more pairs.
  845.      *             The pair consists of a String and Object.
  846.      *             The String indicates the type of object and
  847.      *             is one of the following :
  848.      *             itemListenerK indicating and ItemListener object.
  849.      *
  850.      * @see AWTEventMulticaster.save(ObjectOutputStream, String, EventListener)
  851.      * @see java.awt.Component.itemListenerK
  852.      */
  853.     private void writeObject(ObjectOutputStream s)
  854.       throws IOException
  855.     {
  856.       s.defaultWriteObject();
  857.  
  858.       AWTEventMulticaster.save(s, adjustmentListenerK, adjustmentListener);
  859.       s.writeObject(null);
  860.     }
  861.  
  862.     /**
  863.      * Read the ObjectInputStream and if it isnt null
  864.      * add a listener to receive item events fired
  865.      * by the Scrollbar.
  866.      * Unrecognised keys or values will be Ignored.
  867.      * 
  868.      * @see removeActionListener()
  869.      * @see addActionListener()
  870.      */
  871.     private void readObject(ObjectInputStream s)
  872.       throws ClassNotFoundException, IOException
  873.     {
  874.       s.defaultReadObject();
  875.  
  876.       Object keyOrNull;
  877.       while(null != (keyOrNull = s.readObject())) {
  878.     String key = ((String)keyOrNull).intern();
  879.  
  880.     if (adjustmentListenerK == key)
  881.       addAdjustmentListener((AdjustmentListener)(s.readObject()));
  882.  
  883.     else // skip value for unrecognized key
  884.       s.readObject();
  885.       }
  886.     }
  887.  
  888. }
  889.