home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / Scrollbar.java < prev    next >
Text File  |  1997-10-27  |  14KB  |  492 lines

  1. /*
  2.  * @(#)Scrollbar.java    1.49 97/03/03
  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.  * A Scrollbar component which implements the Adjustable interface.
  33.  *
  34.  * @version    1.49 03/03/97
  35.  * @author     Sami Shaio
  36.  */
  37. public class Scrollbar extends Component implements Adjustable {
  38.   
  39.     /**
  40.      * The horizontal Scrollbar variable.
  41.      */
  42.     public static final int    HORIZONTAL = 0;
  43.  
  44.     /**
  45.      * The vertical Scrollbar variable.
  46.      */
  47.     public static final int    VERTICAL   = 1;
  48.  
  49.     /**
  50.      * The value of the Scrollbar.
  51.      */
  52.     int    value;
  53.  
  54.     /**
  55.      * The maximum value of the Scrollbar.
  56.      */
  57.     int    maximum;
  58.  
  59.     /**
  60.      * The minimum value of the Scrollbar.
  61.      */
  62.     int    minimum;
  63.  
  64.     /**
  65.      * The size of the visible portion of the Scrollbar.
  66.      */
  67.     int    visibleAmount;
  68.  
  69.     /**
  70.      * The Scrollbar's orientation--being either horizontal or vertical.
  71.      */
  72.     int    orientation;
  73.  
  74.     /**
  75.      * The amount by which the scrollbar value will change when going
  76.      * up or down by a line.
  77.      */
  78.     int lineIncrement = 1;
  79.  
  80.     /**
  81.      * The amount by which the scrollbar value will change when going
  82.      * up or down by a page.
  83.      */
  84.     int pageIncrement = 10;
  85.  
  86.     transient AdjustmentListener adjustmentListener;
  87.  
  88.     private static final String base = "scrollbar";
  89.     private static int nameCounter = 0;
  90.     
  91.     /*
  92.      * JDK 1.1 serialVersionUID 
  93.      */
  94.     private static final long serialVersionUID = 8451667562882310543L;
  95.  
  96.     /**
  97.      * Constructs a new vertical Scrollbar.
  98.      */
  99.     public Scrollbar() {
  100.     this(VERTICAL, 0, 10, 0, 100);
  101.     }
  102.  
  103.     /**
  104.      * Constructs a new Scrollbar with the specified orientation.
  105.      * @param orientation either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
  106.      * @exception IllegalArgumentException When an illegal scrollbar orientation is given.
  107.      */
  108.     public Scrollbar(int orientation) {
  109.         this(orientation, 0, 10, 0, 100);
  110.     }
  111.  
  112.     /**
  113.      * Constructs a new Scrollbar with the specified orientation,
  114.      * value, page size,  and minumum and maximum values.
  115.      * @param orientation either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
  116.      * @param value the scrollbar's value
  117.      * @param visible the size of the visible portion of the
  118.      * scrollable area. The scrollbar will use this value when paging up
  119.      * or down by a page.
  120.      * @param minimum the minimum value of the scrollbar
  121.      * @param maximum the maximum value of the scrollbar
  122.      */
  123.     public Scrollbar(int orientation, int value, int visible, int minimum, int maximum) {
  124.     this.name = base + nameCounter++;
  125.     switch (orientation) {
  126.       case HORIZONTAL:
  127.       case VERTICAL:
  128.         this.orientation = orientation;
  129.         break;
  130.       default:
  131.         throw new IllegalArgumentException("illegal scrollbar orientation");
  132.     }
  133.     setValues(value, visible, minimum, maximum);
  134.     }
  135.  
  136.     /**
  137.      * Creates the Scrollbar's peer.  The peer allows you to modify
  138.      * the appearance of the Scrollbar without changing any of its
  139.      * functionality.
  140.      */
  141.  
  142.     public void addNotify() {
  143.     peer = getToolkit().createScrollbar(this);
  144.     super.addNotify();
  145.     }
  146.  
  147.     /**
  148.      * Returns the orientation for this Scrollbar.
  149.      */
  150.     public int getOrientation() {
  151.     return orientation;
  152.     }
  153.  
  154.     /**
  155.      * Sets the orientation for this Scrollbar.
  156.      * @param orientation  the orientation (HORIZONTAL or VERTICAL) of
  157.      * this scrollbar.
  158.      */
  159.     public synchronized void setOrientation(int orientation) {
  160.     if (orientation == this.orientation) {
  161.         return;
  162.     }
  163.     switch (orientation) {
  164.       case HORIZONTAL:
  165.       case VERTICAL:
  166.         this.orientation = orientation;
  167.         break;
  168.       default:
  169.         throw new IllegalArgumentException("illegal scrollbar orientation");
  170.     }
  171.     /* Create a new peer with the specified orientation. */
  172.     synchronized (Component.LOCK) {
  173.         if (peer != null) {
  174.         removeNotify();
  175.         addNotify();
  176.         invalidate();
  177.         }
  178.     }
  179.     }
  180.  
  181.     /**
  182.      * Returns the current value of this Scrollbar.
  183.      * @see #getMinimum
  184.      * @see #getMaximum
  185.      */
  186.     public int getValue() {
  187.     return value;
  188.     }
  189.  
  190.     /**
  191.      * Sets the value of this Scrollbar to the specified value.
  192.      * @param value the new value of the Scrollbar. If this value is
  193.      * below the current minimum or above the current maximum minus 
  194.      * the visible amount, it becomes the new one of those values, 
  195.      * respectively.
  196.      * @see #getValue
  197.      */
  198.     public synchronized void setValue(int newValue) {
  199.     /* Use setValues so that a consistent policy
  200.          * relating minimum, maximum, and value is enforced.
  201.          */
  202.         setValues(newValue, visibleAmount, minimum, maximum);
  203.     }
  204.  
  205.     /**
  206.      * Returns the minimum value of this Scrollbar.
  207.      * @see #getMaximum
  208.      * @see #getValue
  209.      */
  210.     public int getMinimum() {
  211.     return minimum;
  212.     }
  213.  
  214.     /**
  215.      * Sets the minimum value for this Scrollbar.
  216.      * @param minimum the minimum value of the scrollbar
  217.      */
  218.     public synchronized void setMinimum(int newMinimum) {
  219.     /* Use setValues so that a consistent policy
  220.          * relating minimum, maximum, and value is enforced.
  221.          */
  222.     setValues(value, visibleAmount, newMinimum, maximum);
  223.     }
  224.  
  225.     /**
  226.      * Returns the maximum value of this Scrollbar.
  227.      * @see #getMinimum
  228.      * @see #getValue
  229.      */
  230.     public int getMaximum() {
  231.     return maximum;
  232.     }
  233.  
  234.     /**
  235.      * Sets the maximum value for this Scrollbar.
  236.      * @param maximum the maximum value of the scrollbar
  237.      */
  238.     public synchronized void setMaximum(int newMaximum) {
  239.     /* Use setValues so that a consistent policy
  240.          * relating minimum, maximum, and value is enforced.
  241.          */
  242.         setValues(value, visibleAmount, minimum, newMaximum);
  243.     }
  244.  
  245.     /**
  246.      * Returns the visible amount of this Scrollbar.
  247.      */
  248.     public int getVisibleAmount() {
  249.     return getVisible();
  250.     }
  251.  
  252.     /**
  253.      * @deprecated As of JDK version 1.1,
  254.      * replaced by getVisibleAmount().
  255.      */
  256.     public int getVisible() {
  257.     return visibleAmount;
  258.     }
  259.  
  260.     /**
  261.      * Sets the visible amount of this Scrollbar, which is the range
  262.      * of values represented by the width of the scroll bar's bubble.
  263.      * @param visible the amount visible per page
  264.      */
  265.     public synchronized void setVisibleAmount(int newAmount) {
  266.         setValues(value, newAmount, minimum, maximum);
  267.     }
  268.  
  269.     /**
  270.      * Sets the unit increment for this scrollbar. This is the value
  271.      * that will be added (subtracted) when the user hits the unit down
  272.      * (up) gadgets.
  273.      */
  274.     public synchronized void setUnitIncrement(int v) {
  275.     setLineIncrement(v);
  276.     }
  277.  
  278.     /**
  279.      * @deprecated As of JDK version 1.1,
  280.      * replaced by setUnitIncrement(int).
  281.      */
  282.     public void setLineIncrement(int v) {
  283.     lineIncrement = v;
  284.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  285.     if (peer != null) {
  286.         peer.setLineIncrement(v);
  287.     }
  288.     }
  289.  
  290.     /**
  291.      * Gets the unit increment for this scrollbar.
  292.      */
  293.     public int getUnitIncrement() {
  294.     return getLineIncrement();
  295.     }
  296.  
  297.     /**
  298.      * @deprecated As of JDK version 1.1,
  299.      * replaced by getUnitIncrement().
  300.      */
  301.     public int getLineIncrement() {
  302.     return lineIncrement;
  303.     }
  304.  
  305.     /**
  306.      * Sets the block increment for this scrollbar. This is the value
  307.      * that will be added (subtracted) when the user hits the block down
  308.      * (up) gadgets.
  309.      */
  310.     public synchronized void setBlockIncrement(int v) {
  311.     setPageIncrement(v);
  312.     }
  313.  
  314.     /**
  315.      * @deprecated As of JDK version 1.1,
  316.      * replaced by setBlockIncrement().
  317.      */
  318.     public void setPageIncrement(int v) {
  319.     pageIncrement = v;
  320.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  321.     if (peer != null) {
  322.         peer.setPageIncrement(v);
  323.     }
  324.     }
  325.  
  326.     /**
  327.      * Gets the block increment for this scrollbar.
  328.      */
  329.     public int getBlockIncrement() {
  330.     return getPageIncrement();
  331.     }
  332.  
  333.     /**
  334.      * @deprecated As of JDK version 1.1,
  335.      * replaced by getBlockIncrement().
  336.      */
  337.     public int getPageIncrement() {
  338.     return pageIncrement;
  339.     }
  340.  
  341.     /**
  342.      * Sets the values for this Scrollbar.
  343.      * This method enforces the following constraints:
  344.      * <UL>
  345.      * <LI> The maximum must be greater than the minimum </LI>
  346.      * <LI> The value must be greater than or equal to the minumum 
  347.      *      and less than or equal to the maximum minus the 
  348.      *      visible amount </LI>  
  349.      * <LI> The visible amount must be greater than 1 and less than or equal
  350.      *      to the difference between the maximum and minimum values. </LI>
  351.      * </UL>
  352.      * Values which do not meet these criteria are quietly coerced to the
  353.      * appropriate boundary value.
  354.      * @param value is the position in the current window.
  355.      * @param visible is the amount visible per page
  356.      * @param minimum is the minimum value of the scrollbar
  357.      * @param maximum is the maximum value of the scrollbar
  358.      */
  359.     public synchronized void setValues(int value, int visible, int minimum, int maximum) {
  360.     if (maximum <= minimum) {
  361.         maximum = minimum + 1;
  362.     }
  363.     if (visible > maximum - minimum) {
  364.       visible = maximum - minimum;
  365.     }
  366.     if (visible < 1) {
  367.       visible = 1;
  368.     }
  369.     if (value < minimum) {
  370.         value = minimum;
  371.     }
  372.     if (value > maximum - visible) {
  373.         value = maximum - visible;
  374.     }
  375.  
  376.     this.value = value;
  377.     this.visibleAmount = visible;
  378.     this.minimum = minimum;
  379.     this.maximum = maximum;
  380.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  381.     if (peer != null) {
  382.         peer.setValues(value, visibleAmount, minimum, maximum);
  383.     }
  384.     }
  385.  
  386.     /**
  387.      * Adds the specified adjustment listener  to recieve adjustment events 
  388.      * from this scrollbar.
  389.      * @param l the adjustment listener
  390.      */ 
  391.     public synchronized void addAdjustmentListener(AdjustmentListener l) {
  392.     adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l);
  393.         newEventsOnly = true;
  394.     }
  395.  
  396.     /**
  397.      * Removes the specified adjustment listener so that it no longer
  398.      * receives adjustment events from this scrollbar..
  399.      */ 
  400.     public synchronized void removeAdjustmentListener(AdjustmentListener l) {
  401.     adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l);
  402.     }
  403.  
  404.     // REMIND: remove when filtering is done at lower level
  405.     boolean eventEnabled(AWTEvent e) {
  406.         if (e.id == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) {
  407.             if ((eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0 ||
  408.                 adjustmentListener != null) {
  409.                 return true;
  410.             } 
  411.             return false;
  412.         }
  413.         return super.eventEnabled(e);
  414.     }     
  415.  
  416.     /**
  417.      * Processes events on this scrollbar. If the event is an 
  418.      * AdjustmentEvent, it invokes the processAdjustmentEvent method,
  419.      * else it invokes its superclass's processEvent.
  420.      * @param e the event
  421.      */
  422.     protected void processEvent(AWTEvent e) {
  423.         if (e instanceof AdjustmentEvent) {
  424.             processAdjustmentEvent((AdjustmentEvent)e);
  425.             return;
  426.         }
  427.     super.processEvent(e);
  428.     }
  429.  
  430.     /** 
  431.      * Processes adjustment events occurring on this scrollbar by
  432.      * dispatching them to any registered AdjustmentListener objects.
  433.      * NOTE: This method will not be called unless adjustment events
  434.      * are enabled for this component; this happens when one of the
  435.      * following occurs:
  436.      * a) An AdjustmentListener object is registered via addAdjustmentListener()
  437.      * b) Adjustment events are enabled via enableEvents()
  438.      * @see Component#enableEvents
  439.      * @param e the adjustment event
  440.      */ 
  441.     protected void processAdjustmentEvent(AdjustmentEvent e) {
  442.         if (adjustmentListener != null) {
  443.             adjustmentListener.adjustmentValueChanged(e);
  444.         }
  445.     }
  446.  
  447.     /**
  448.      * Returns the String parameters for this Scrollbar.
  449.      */
  450.     protected String paramString() {
  451.     return super.paramString() +
  452.         ",val=" + value +
  453.         ",vis=" + visibleAmount +
  454.         ",min=" + minimum +
  455.         ",max=" + maximum +
  456.         ((orientation == VERTICAL) ? ",vert" : ",horz");
  457.     }
  458.  
  459.  
  460.     /* Serialization support. 
  461.      */
  462.  
  463.     private int scrollbarSerializedDataVersion = 1;
  464.  
  465.     private void writeObject(ObjectOutputStream s)
  466.       throws IOException 
  467.     {
  468.       s.defaultWriteObject();
  469.  
  470.       AWTEventMulticaster.save(s, adjustmentListenerK, adjustmentListener);
  471.       s.writeObject(null);
  472.     }
  473.  
  474.     private void readObject(ObjectInputStream s)
  475.       throws ClassNotFoundException, IOException 
  476.     {
  477.       s.defaultReadObject();
  478.  
  479.       Object keyOrNull;
  480.       while(null != (keyOrNull = s.readObject())) {
  481.     String key = ((String)keyOrNull).intern();
  482.  
  483.     if (adjustmentListenerK == key) 
  484.       addAdjustmentListener((AdjustmentListener)(s.readObject()));
  485.  
  486.     else // skip value for unrecognized key
  487.       s.readObject();
  488.       }
  489.     }
  490.  
  491. }
  492.