home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Scrollbar.java < prev    next >
Text File  |  1996-05-03  |  7KB  |  272 lines

  1. /*
  2.  * @(#)Scrollbar.java    1.20 95/12/14 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.ScrollbarPeer;
  22.  
  23. /**
  24.  * A Scrollbar component.
  25.  *
  26.  * @version    1.20 14 Dec 1995
  27.  * @author     Sami Shaio
  28.  */
  29. public class Scrollbar extends Component {
  30.   
  31.     /**
  32.      * The horizontal Scrollbar variable.
  33.      */
  34.     public static final int    HORIZONTAL = 0;
  35.  
  36.     /**
  37.      * The vertical Scrollbar variable.
  38.      */
  39.     public static final int    VERTICAL   = 1;
  40.  
  41.     /**
  42.      * The value of the Scrollbar.
  43.      */
  44.     int    value;
  45.  
  46.     /**
  47.      * The maximum value of the Scrollbar.
  48.      */
  49.     int    maximum;
  50.  
  51.     /**
  52.      * The minimum value of the Scrollbar.
  53.      */
  54.     int    minimum;
  55.  
  56.     /**
  57.      * The size of the visible portion of the Scrollbar.
  58.      */
  59.     int    sVisible;
  60.  
  61.     /**
  62.      * The Scrollbar's orientation--being either horizontal or vertical.
  63.      */
  64.     int    orientation;
  65.  
  66.     /**
  67.      * The amount by which the scrollbar value will change when going
  68.      * up or down by a line.
  69.      */
  70.     int lineIncrement = 1;
  71.  
  72.     /**
  73.      * The amount by which the scrollbar value will change when going
  74.      * up or down by a page.
  75.      */
  76.     int pageIncrement = 10;
  77.  
  78.     
  79.     /**
  80.      * Constructs a new vertical Scrollbar.
  81.      */
  82.     public Scrollbar() {
  83.     this(VERTICAL);
  84.     }
  85.  
  86.     /**
  87.      * Constructs a new Scrollbar with the specified orientation.
  88.      * @param orientation either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
  89.      * @exception IllegalArgumentException When an illegal scrollbar orientation is given.
  90.      */
  91.     public Scrollbar(int orientation) {
  92.     switch (orientation) {
  93.       case Scrollbar.HORIZONTAL:
  94.       case Scrollbar.VERTICAL:
  95.         this.orientation = orientation;
  96.         break;
  97.  
  98.       default:
  99.         throw new IllegalArgumentException("illegal scrollbar orientation");
  100.     }
  101.     }
  102.  
  103.     /**
  104.      * Constructs a new Scrollbar with the specified orientation,
  105.      * value, page size,  and minumum and maximum values.
  106.      * @param orientation either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
  107.      * @param value the scrollbar's value
  108.      * @param visible the size of the visible portion of the
  109.      * scrollable area. The scrollbar will use this value when paging up
  110.      * or down by a page.
  111.      * @param minimum the minimum value of the scrollbar
  112.      * @param maximum the maximum value of the scrollbar
  113.      */
  114.     public Scrollbar(int orientation, int value, int visible, int minimum, int maximum) {
  115.     this(orientation);
  116.     setValues(value, visible, minimum, maximum);
  117.     }
  118.  
  119.     /**
  120.      * Creates the Scrollbar's peer.  The peer allows you to modify
  121.      * the appearance of the Scrollbar without changing any of its
  122.      * functionality.
  123.      */
  124.  
  125.     public synchronized void addNotify() {
  126.     peer = getToolkit().createScrollbar(this);
  127.     super.addNotify();
  128.     }
  129.  
  130.     /**
  131.      * Returns the orientation for this Scrollbar.
  132.      */
  133.     public int getOrientation() {
  134.     return orientation;
  135.     }
  136.  
  137.     /**
  138.      * Returns the current value of this Scrollbar.
  139.      * @see #getMinimum
  140.      * @see #getMaximum
  141.      */
  142.     public int getValue() {
  143.     return value;
  144.     }
  145.  
  146.     /**
  147.      * Sets the value of this Scrollbar to the specified value.
  148.      * @param value the new value of the Scrollbar. If this value is
  149.      * below the current minimum or above the current maximum, it becomes the
  150.      * new one of those values, respectively.
  151.      * @see #getValue
  152.      */
  153.     public void setValue(int value) {
  154.     if (value < minimum) {
  155.         value = minimum;
  156.     }
  157.     if (value > maximum) {
  158.         value = maximum;
  159.     }
  160.     if (value != this.value) {
  161.         this.value = value;
  162.         ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  163.         if (peer != null) {
  164.         peer.setValue(value);
  165.         }
  166.     }
  167.     }
  168.  
  169.     /**
  170.      * Returns the minimum value of this Scrollbar.
  171.      * @see #getMaximum
  172.      * @see #getValue
  173.      */
  174.     public int getMinimum() {
  175.     return minimum;
  176.     }
  177.  
  178.     /**
  179.      * Returns the maximum value of this Scrollbar.
  180.      * @see #getMinimum
  181.      * @see #getValue
  182.      */
  183.     public int getMaximum() {
  184.     return maximum;
  185.     }
  186.  
  187.     /**
  188.      * Returns the visible amount of the Scrollbar.
  189.      */
  190.     public int getVisible() {
  191.     return sVisible;
  192.     }
  193.  
  194.     /**
  195.      * Sets the line increment for this scrollbar. This is the value
  196.      * that will be added (subtracted) when the user hits the line down
  197.      * (up) gadgets.
  198.      */
  199.     public void setLineIncrement(int l) {
  200.     lineIncrement = l;
  201.     if (peer != null) {
  202.         ((ScrollbarPeer)peer).setLineIncrement(l);
  203.     }
  204.     }
  205.  
  206.     /**
  207.      * Gets the line increment for this scrollbar.
  208.      */
  209.     public int getLineIncrement() {
  210.     return lineIncrement;
  211.     }
  212.  
  213.     /**
  214.      * Sets the page increment for this scrollbar. This is the value
  215.      * that will be added (subtracted) when the user hits the page down
  216.      * (up) gadgets.
  217.      */
  218.     public void setPageIncrement(int l) {
  219.     pageIncrement = l;
  220.     if (peer != null) {
  221.         ((ScrollbarPeer)peer).setPageIncrement(l);
  222.     }
  223.     }
  224.  
  225.     /**
  226.      * Gets the page increment for this scrollbar.
  227.      */
  228.     public int getPageIncrement() {
  229.     return pageIncrement;
  230.     }
  231.  
  232.     /**
  233.      * Sets the values for this Scrollbar.
  234.      * @param value is the position in the current window.
  235.      * @param visible is the amount visible per page
  236.      * @param minimum is the minimum value of the scrollbar
  237.      * @param maximum is the maximum value of the scrollbar
  238.      */
  239.     public void setValues(int value, int visible, int minimum, int maximum) {
  240.     if (maximum < minimum) {
  241.         maximum = minimum;
  242.     }
  243.     if (value < minimum) {
  244.         value = minimum;
  245.     }
  246.     if (value > maximum) {
  247.         value = maximum;
  248.     }
  249.  
  250.     this.value = value;
  251.     this.sVisible = visible;
  252.     this.minimum = minimum;
  253.     this.maximum = maximum;
  254.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  255.     if (peer != null) {
  256.         peer.setValues(value, sVisible, minimum, maximum);
  257.     }
  258.     }
  259.  
  260.     /**
  261.      * Returns the String parameters for this Scrollbar.
  262.      */
  263.     protected String paramString() {
  264.     return super.paramString() +
  265.         ",val=" + value +
  266.         ",vis=" + visible +
  267.         ",min=" + minimum +
  268.         ",max=" + maximum +
  269.         ((orientation == VERTICAL) ? ",vert" : ",horz");
  270.     }
  271. }
  272.