0001: /*
0002: * @(#)Scrollbar.java 1.83 06/10/10
0003: *
0004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
0005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0006: *
0007: * This program is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU General Public License version
0009: * 2 only, as published by the Free Software Foundation.
0010: *
0011: * This program is distributed in the hope that it will be useful, but
0012: * WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * General Public License version 2 for more details (a copy is
0015: * included at /legal/license.txt).
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * version 2 along with this work; if not, write to the Free Software
0019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA
0021: *
0022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0023: * Clara, CA 95054 or visit www.sun.com if you need additional
0024: * information or have any questions.
0025: *
0026: */
0027: package java.awt;
0028:
0029: import sun.awt.peer.ScrollbarPeer;
0030: import sun.awt.PeerBasedToolkit;
0031: import java.awt.event.*;
0032: import java.io.ObjectOutputStream;
0033: import java.io.ObjectInputStream;
0034: import java.io.IOException;
0035: import java.util.EventListener;
0036: import java.awt.AWTEventMulticaster;
0037:
0038: /**
0039: * The <code>Scrollbar</code> class embodies a scroll bar, a
0040: * familiar user-interface object. A scroll bar provides a
0041: * convenient means for allowing a user to select from a
0042: * range of values. The following three vertical
0043: * scroll bars could be used as slider controls to pick
0044: * the red, green, and blue components of a color:
0045: * <p>
0046: * <img src="doc-files/Scrollbar-1.gif"
0047: * ALIGN=center HSPACE=10 VSPACE=7>
0048: * <p>
0049: * Each scroll bar in this example could be created with
0050: * code similar to the following:
0051: * <p>
0052: * <hr><blockquote><pre>
0053: * redSlider=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
0054: * add(redSlider);
0055: * </pre></blockquote><hr>
0056: * <p>
0057: * Alternatively, a scroll bar can represent a range of values. For
0058: * example, if a scroll bar is used for scrolling through text, the
0059: * width of the "bubble" or "thumb" can represent the amount of text
0060: * that is visible. Here is an example of a scroll bar that
0061: * represents a range:
0062: * <p>
0063: * <img src="doc-files/Scrollbar-2.gif"
0064: * ALIGN=center HSPACE=10 VSPACE=7>
0065: * <p>
0066: * The value range represented by the bubble is the <em>visible</em>
0067: * range of the scroll bar. The horizontal scroll bar in this
0068: * example could be created with code like the following:
0069: * <p>
0070: * <hr><blockquote><pre>
0071: * ranger = new Scrollbar(Scrollbar.HORIZONTAL, 0, 60, 0, 300);
0072: * add(ranger);
0073: * </pre></blockquote><hr>
0074: * <p>
0075: * Note that the actual maximum value of the scroll bar is the
0076: * <code>maximum</code> minus the <code>visible</code>.
0077: * In the previous example, because the <code>maximum</code> is
0078: * 300 and the <code>visible</code> is 60, the actual maximum
0079: * value is 240. The range of the scrollbar track is 0 - 300.
0080: * The left side of the bubble indicates the value of the
0081: * scroll bar.
0082: * <p>
0083: * Normally, the user changes the value of the scroll bar by
0084: * making a gesture with the mouse. For example, the user can
0085: * drag the scroll bar's bubble up and down, or click in the
0086: * scroll bar's unit increment or block increment areas. Keyboard
0087: * gestures can also be mapped to the scroll bar. By convention,
0088: * the <b>Page Up</b> and <b>Page Down</b>
0089: * keys are equivalent to clicking in the scroll bar's block
0090: * increment and block decrement areas.
0091: * <p>
0092: * When the user changes the value of the scroll bar, the scroll bar
0093: * receives an instance of <code>AdjustmentEvent</code>.
0094: * The scroll bar processes this event, passing it along to
0095: * any registered listeners.
0096: * <p>
0097: * Any object that wishes to be notified of changes to the
0098: * scroll bar's value should implement
0099: * <code>AdjustmentListener</code>, an interface defined in
0100: * the package <code>java.awt.event</code>.
0101: * Listeners can be added and removed dynamically by calling
0102: * the methods <code>addAdjustmentListener</code> and
0103: * <code>removeAdjustmentListener</code>.
0104: * <p>
0105: * The <code>AdjustmentEvent</code> class defines five types
0106: * of adjustment event, listed here:
0107: * <p>
0108: * <ul>
0109: * <li><code>AdjustmentEvent.TRACK</code> is sent out when the
0110: * user drags the scroll bar's bubble.
0111: * <li><code>AdjustmentEvent.UNIT_INCREMENT</code> is sent out
0112: * when the user clicks in the left arrow of a horizontal scroll
0113: * bar, or the top arrow of a vertical scroll bar, or makes the
0114: * equivalent gesture from the keyboard.
0115: * <li><code>AdjustmentEvent.UNIT_DECREMENT</code> is sent out
0116: * when the user clicks in the right arrow of a horizontal scroll
0117: * bar, or the bottom arrow of a vertical scroll bar, or makes the
0118: * equivalent gesture from the keyboard.
0119: * <li><code>AdjustmentEvent.BLOCK_INCREMENT</code> is sent out
0120: * when the user clicks in the track, to the left of the bubble
0121: * on a horizontal scroll bar, or above the bubble on a vertical
0122: * scroll bar. By convention, the <b>Page Up</b>
0123: * key is equivalent, if the user is using a keyboard that
0124: * defines a <b>Page Up</b> key.
0125: * <li><code>AdjustmentEvent.BLOCK_DECREMENT</code> is sent out
0126: * when the user clicks in the track, to the right of the bubble
0127: * on a horizontal scroll bar, or below the bubble on a vertical
0128: * scroll bar. By convention, the <b>Page Down</b>
0129: * key is equivalent, if the user is using a keyboard that
0130: * defines a <b>Page Down</b> key.
0131: * </ul>
0132: * <p>
0133: * The JDK 1.0 event system is supported for backwards
0134: * compatibility, but its use with newer versions of the platform is
0135: * discouraged. The fives types of adjustment event introduced
0136: * with JDK 1.1 correspond to the five event types
0137: * that are associated with scroll bars in previous platform versions.
0138: * The following list gives the adjustment event type,
0139: * and the corresponding JDK 1.0 event type it replaces.
0140: * <p>
0141: * <ul>
0142: * <li><code>AdjustmentEvent.TRACK</code> replaces
0143: * <code>Event.SCROLL_ABSOLUTE</code>
0144: * <li><code>AdjustmentEvent.UNIT_INCREMENT</code> replaces
0145: * <code>Event.SCROLL_LINE_UP</code>
0146: * <li><code>AdjustmentEvent.UNIT_DECREMENT</code> replaces
0147: * <code>Event.SCROLL_LINE_DOWN</code>
0148: * <li><code>AdjustmentEvent.BLOCK_INCREMENT</code> replaces
0149: * <code>Event.SCROLL_PAGE_UP</code>
0150: * <li><code>AdjustmentEvent.BLOCK_DECREMENT</code> replaces
0151: * <code>Event.SCROLL_PAGE_DOWN</code>
0152: * </ul>
0153: * <p>
0154: *
0155: * @version 1.76 08/19/02
0156: * @author Sami Shaio
0157: * @see java.awt.event.AdjustmentEvent
0158: * @see java.awt.event.AdjustmentListener
0159: * @since JDK1.0
0160: */
0161: public class Scrollbar extends Component implements Adjustable {
0162: /**
0163: * A constant that indicates a horizontal scroll bar.
0164: */
0165: public static final int HORIZONTAL = 0;
0166: /**
0167: * A constant that indicates a vertical scroll bar.
0168: */
0169: public static final int VERTICAL = 1;
0170: /**
0171: * The value of the Scrollbar.
0172: * value should be either greater than <code>minimum</code>
0173: * or less that <code>maximum</code>
0174: *
0175: * @serial
0176: * @see getValue()
0177: * @see setValue()
0178: */
0179: int value;
0180: /**
0181: * The maximum value of the Scrollbar.
0182: * This value must be greater than the <code>minimum</code>
0183: * value.<br>
0184: * This integer can be either positive or negative, and
0185: * it's range can be altered at any time.
0186: *
0187: * @serial
0188: * @see getMaximum()
0189: * @see setMaximum()
0190: */
0191: int maximum;
0192: /**
0193: * The minimum value of the Scrollbar.
0194: * This value must be greater than the <code>minimum</code>
0195: * value.<br>
0196: * This integer can be either positive or negative.
0197: *
0198: * @serial
0199: * @see getMinimum()
0200: * @see setMinimum()
0201: */
0202: int minimum;
0203: /**
0204: * The size of the visible portion of the Scrollbar.
0205: * The size of the scrollbox is normally used to indicate
0206: * the visibleAmount.
0207: *
0208: * @serial
0209: * @see getVisibleAmount()
0210: * @see setVisibleAmount()
0211: */
0212: int visibleAmount;
0213: /**
0214: * The Scrollbar's orientation--being either horizontal or vertical.
0215: * This value should be specified when the scrollbar is being
0216: * created.<BR>
0217: * orientation can be either : <code>VERTICAL</code> or
0218: * <code>HORIZONTAL</code> only.
0219: *
0220: * @serial
0221: * @see getOrientation()
0222: * @see setOrientation()
0223: */
0224: int orientation;
0225: /**
0226: * The amount by which the scrollbar value will change when going
0227: * up or down by a line.
0228: * This value should be a non negative integer.
0229: *
0230: * @serial
0231: * @see setLineIncrement()
0232: * @see getLineIncrement()
0233: */
0234: int lineIncrement = 1;
0235: /**
0236: * The amount by which the scrollbar value will change when going
0237: * up or down by a page.
0238: * This value should be a non negative integer.
0239: *
0240: * @serial
0241: * @see setPageIncrement()
0242: * @see getPageIncrement()
0243: */
0244: int pageIncrement = 10;
0245: transient AdjustmentListener adjustmentListener;
0246: private static final String base = "scrollbar";
0247: private static int nameCounter = 0;
0248: /*
0249: * JDK 1.1 serialVersionUID
0250: */
0251: private static final long serialVersionUID = 8451667562882310543L;
0252:
0253: /**
0254: * Constructs a new vertical scroll bar.
0255: * The default properties of the scroll bar are listed in
0256: * the following table:
0257: * <p> </p>
0258: * <table border>
0259: * <tr>
0260: * <th>Property</th>
0261: * <th>Description</th>
0262: * <th>Default Value</th>
0263: * </tr>
0264: * <tr>
0265: * <td>orientation</td>
0266: * <td>indicates if the scroll bar is vertical or horizontal</td>
0267: * <td><code>Scrollbar.VERTICAL</code></td>
0268: * </tr>
0269: * <tr>
0270: * <td>value</td>
0271: * <td>value which controls the location
0272: * <br>of the scroll bar bubble</td>
0273: * <td>0</td>
0274: * </tr>
0275: * <tr>
0276: * <td>minimum</td>
0277: * <td>minimum value of the scroll bar</td>
0278: * <td>0</td>
0279: * </tr>
0280: * <tr>
0281: * <td>maximum</td>
0282: * <td>maximum value of the scroll bar</td>
0283: * <td>100</td>
0284: * </tr>
0285: * <tr>
0286: * <td>unit increment</td>
0287: * <td>amount the value changes when the
0288: * <br>Line Up or Line Down key is pressed,
0289: * <br>or when the end arrows of the scrollbar
0290: * <br>are clicked </td>
0291: * <td>1</td>
0292: * </tr>
0293: * <tr>
0294: * <td>block increment</td>
0295: * <td>amount the value changes when the
0296: * <br>Page Up or Page Down key is pressed,
0297: * <br>or when the scrollbar track is clicked
0298: * <br>on either side of the bubble </td>
0299: * <td>10</td>
0300: * </tr>
0301: * </table>
0302: *
0303: */
0304: public Scrollbar() {
0305: this (VERTICAL, 0, 10, 0, 100);
0306: }
0307:
0308: /**
0309: * Constructs a new scroll bar with the specified orientation.
0310: * <p>
0311: * The <code>orientation</code> argument must take one of the two
0312: * values <code>Scrollbar.HORIZONTAL</code>,
0313: * or <code>Scrollbar.VERTICAL</code>,
0314: * indicating a horizontal or vertical scroll bar, respectively.
0315: * @param orientation indicates the orientation of the scroll bar.
0316: * @exception IllegalArgumentException when an illegal value for
0317: * the <code>orientation</code> argument is supplied.
0318: */
0319: public Scrollbar(int orientation) {
0320: this (orientation, 0, 10, 0, 100);
0321: }
0322:
0323: /**
0324: * Constructs a new scroll bar with the specified orientation,
0325: * initial value, page size, and minimum and maximum values.
0326: * <p>
0327: * The <code>orientation</code> argument must take one of the two
0328: * values <code>Scrollbar.HORIZONTAL</code>,
0329: * or <code>Scrollbar.VERTICAL</code>,
0330: * indicating a horizontal or vertical scroll bar, respectively.
0331: * <p>
0332: * The parameters supplied to this constructor are subject to the
0333: * constraints described in {@link #setValues(int, int, int, int)}.
0334: *
0335: * @param orientation indicates the orientation of the scroll bar.
0336: * @param value the initial value of the scroll bar
0337: * @param visible the visible amount of the scroll bar, typically
0338: * represented by the size of the bubble
0339: * @param minimum the minimum value of the scroll bar
0340: * @param maximum the maximum value of the scroll bar
0341: * @exception IllegalArgumentException when an illegal value for
0342: * the <code>orientation</code> argument is supplied
0343: * @see #setValues
0344: */
0345: public Scrollbar(int orientation, int value, int visible,
0346: int minimum, int maximum) {
0347: switch (orientation) {
0348: case HORIZONTAL:
0349: case VERTICAL:
0350: this .orientation = orientation;
0351: break;
0352:
0353: default:
0354: throw new IllegalArgumentException(
0355: "illegal scrollbar orientation");
0356: }
0357: setValues(value, visible, minimum, maximum);
0358: }
0359:
0360: /**
0361: * Construct a name for this component. Called by getName() when the
0362: * name is null.
0363: */
0364: String constructComponentName() {
0365: synchronized (getClass()) {
0366: return base + nameCounter++;
0367: }
0368: }
0369:
0370: /**
0371: * Creates the Scrollbar's peer. The peer allows you to modify
0372: * the appearance of the Scrollbar without changing any of its
0373: * functionality.
0374: */
0375:
0376: public void addNotify() {
0377: synchronized (getTreeLock()) {
0378: if (peer == null)
0379: peer = ((PeerBasedToolkit) getToolkit())
0380: .createScrollbar(this );
0381: super .addNotify();
0382: }
0383: }
0384:
0385: /**
0386: * Determines the orientation of this scroll bar.
0387: * @return the orientation of this scroll bar, either
0388: * <code>Scrollbar.HORIZONTAL</code> or
0389: * <code>Scrollbar.VERTICAL</code>.
0390: * @see java.awt.Scrollbar#setOrientation
0391: */
0392: public int getOrientation() {
0393: return orientation;
0394: }
0395:
0396: /**
0397: * Sets the orientation for this scroll bar.
0398: * @param the orientation of this scroll bar, either
0399: * <code>Scrollbar.HORIZONTAL</code> or
0400: * <code>Scrollbar.VERTICAL</code>.
0401: * @see java.awt.Scrollbar#getOrientation
0402: * @exception IllegalArgumentException if the value supplied
0403: * for <code>orientation</code> is not a
0404: * legal value.
0405: * @since JDK1.1
0406: */
0407: public void setOrientation(int orientation) {
0408: synchronized (getTreeLock()) {
0409: if (orientation == this .orientation) {
0410: return;
0411: }
0412: switch (orientation) {
0413: case HORIZONTAL:
0414: case VERTICAL:
0415: this .orientation = orientation;
0416: break;
0417:
0418: default:
0419: throw new IllegalArgumentException(
0420: "illegal scrollbar orientation");
0421: }
0422: /* Create a new peer with the specified orientation. */
0423: if (peer != null) {
0424: removeNotify();
0425: addNotify();
0426: invalidate();
0427: }
0428: }
0429: }
0430:
0431: /**
0432: * Gets the current value of this scroll bar.
0433: * @return the current value of this scroll bar.
0434: * @see java.awt.Scrollbar#getMinimum
0435: * @see java.awt.Scrollbar#getMaximum
0436: */
0437: public int getValue() {
0438: return value;
0439: }
0440:
0441: /**
0442: * Sets the value of this scroll bar to the specified value.
0443: * <p>
0444: * If the value supplied is less than the current <code>minimum</code>
0445: * or greater than the current <code>maximum - visibleAmount</code>,
0446: * then either <code>minimum</code> or <code>maximum - visibleAmount</code>
0447: * is substituted, as appropriate.
0448: * <p>
0449: * Normally, a program should change a scroll bar's
0450: * value only by calling <code>setValues</code>.
0451: * The <code>setValues</code> method simultaneously
0452: * and synchronously sets the minimum, maximum, visible amount,
0453: * and value properties of a scroll bar, so that they are
0454: * mutually consistent.
0455: *
0456: * @param newValue the new value of the scroll bar
0457: * @see java.awt.Scrollbar#setValues
0458: * @see java.awt.Scrollbar#getValue
0459: * @see java.awt.Scrollbar#getMinimum
0460: * @see java.awt.Scrollbar#getMaximum
0461: */
0462: public void setValue(int newValue) {
0463: // Use setValues so that a consistent policy relating
0464: // minimum, maximum, visible amount, and value is enforced.
0465: setValues(newValue, visibleAmount, minimum, maximum);
0466: }
0467:
0468: /**
0469: * Gets the minimum value of this scroll bar.
0470: * @return the minimum value of this scroll bar.
0471: * @see java.awt.Scrollbar#getValue
0472: * @see java.awt.Scrollbar#getMaximum
0473: */
0474: public int getMinimum() {
0475: return minimum;
0476: }
0477:
0478: /**
0479: * Sets the minimum value of this scroll bar.
0480: * <p>
0481: * When <code>setMinimum</code> is called, the minimum value
0482: * is changed, and other values (including the maximum, the
0483: * visible amount, and the current scroll bar value)
0484: * are changed to be consistent with the new minimum.
0485: * <p>
0486: * Normally, a program should change a scroll bar's minimum
0487: * value only by calling <code>setValues</code>.
0488: * The <code>setValues</code> method simultaneously
0489: * and synchronously sets the minimum, maximum, visible amount,
0490: * and value properties of a scroll bar, so that they are
0491: * mutually consistent.
0492: * <p>
0493: * Note that setting the minimum value to <code>Integer.MAX_VALUE</code>
0494: * will result in the new minimum value being set to
0495: * <code>Integer.MAX_VALUE - 1</code>.
0496: *
0497: * @param newMinimum the new minimum value for this scroll bar
0498: * @see java.awt.Scrollbar#setValues
0499: * @see java.awt.Scrollbar#setMaximum
0500: * @since JDK1.1
0501: */
0502: public void setMinimum(int newMinimum) {
0503: // No checks are necessary in this method since minimum is
0504: // the first variable checked in the setValues function.
0505:
0506: // Use setValues so that a consistent policy relating
0507: // minimum, maximum, visible amount, and value is enforced.
0508: setValues(value, visibleAmount, newMinimum, maximum);
0509: }
0510:
0511: /**
0512: * Gets the maximum value of this scroll bar.
0513: * @return the maximum value of this scroll bar.
0514: * @see java.awt.Scrollbar#getValue
0515: * @see java.awt.Scrollbar#getMinimum
0516: */
0517: public int getMaximum() {
0518: return maximum;
0519: }
0520:
0521: /**
0522: * Sets the maximum value of this scroll bar.
0523: * <p>
0524: * When <code>setMaximum</code> is called, the maximum value
0525: * is changed, and other values (including the minimum, the
0526: * visible amount, and the current scroll bar value)
0527: * are changed to be consistent with the new maximum.
0528: * <p>
0529: * Normally, a program should change a scroll bar's maximum
0530: * value only by calling <code>setValues</code>.
0531: * The <code>setValues</code> method simultaneously
0532: * and synchronously sets the minimum, maximum, visible amount,
0533: * and value properties of a scroll bar, so that they are
0534: * mutually consistent.
0535: * <p>
0536: * Note that setting the maximum value to <code>Integer.MIN_VALUE</code>
0537: * will result in the new maximum value being set to
0538: * <code>Integer.MIN_VALUE + 1</code>.
0539: *
0540: * @param newMaximum the new maximum value
0541: * for this scroll bar
0542: * @see java.awt.Scrollbar#setValues
0543: * @see java.awt.Scrollbar#setMinimum
0544: * @since JDK1.1
0545: */
0546: public void setMaximum(int newMaximum) {
0547: // minimum is checked first in setValues, so we need to
0548: // enforce minimum and maximum checks here.
0549: if (newMaximum == Integer.MIN_VALUE) {
0550: newMaximum = Integer.MIN_VALUE + 1;
0551: }
0552:
0553: if (minimum >= newMaximum) {
0554: minimum = newMaximum - 1;
0555: }
0556:
0557: // Use setValues so that a consistent policy relating
0558: // minimum, maximum, visible amount, and value is enforced.
0559: setValues(value, visibleAmount, minimum, newMaximum);
0560: }
0561:
0562: /**
0563: * Gets the visible amount of this scroll bar.
0564: * <p>
0565: * When a scroll bar is used to select a range of values,
0566: * the visible amount is used to represent the range of values
0567: * that are currently visible. The size of the scroll bar's
0568: * bubble (also called a thumb or scroll box), usually gives a
0569: * visual representation of the relationship of the visible
0570: * amount to the range of the scroll bar.
0571: * <p>
0572: * The scroll bar's bubble may not be displayed when it is not
0573: * moveable (e.g. when it takes up the entire length of the
0574: * scroll bar's track, or when the scroll bar is disabled).
0575: * Whether the bubble is displayed or not will not affect
0576: * the value returned by <code>getVisibleAmount</code>.
0577: *
0578: * @return the visible amount of this scroll bar
0579: * @see java.awt.Scrollbar#setVisibleAmount
0580: * @since JDK1.1
0581: */
0582: public int getVisibleAmount() {
0583: return getVisible();
0584: }
0585:
0586: /**
0587: * @deprecated As of JDK version 1.1,
0588: * replaced by <code>getVisibleAmount()</code>.
0589: */
0590: public int getVisible() {
0591: return visibleAmount;
0592: }
0593:
0594: /**
0595: * Sets the visible amount of this scroll bar.
0596: * <p>
0597: * When a scroll bar is used to select a range of values,
0598: * the visible amount is used to represent the range of values
0599: * that are currently visible. The size of the scroll bar's
0600: * bubble (also called a thumb or scroll box), usually gives a
0601: * visual representation of the relationship of the visible
0602: * amount to the range of the scroll bar.
0603: * <p>
0604: * The scroll bar's bubble may not be displayed when it is not
0605: * moveable (e.g. when it takes up the entire length of the
0606: * scroll bar's track, or when the scroll bar is disabled).
0607: * Whether the bubble is displayed or not will not affect
0608: * the value returned by <code>getVisibleAmount</code>.
0609: * <p>
0610: * If the visible amount supplied is less than <code>one</code>
0611: * or greater than the current <code>maximum - minimum</code>,
0612: * then either <code>one</code> or <code>maximum - minimum</code>
0613: * is substituted, as appropriate.
0614: * <p>
0615: * Normally, a program should change a scroll bar's
0616: * value only by calling <code>setValues</code>.
0617: * The <code>setValues</code> method simultaneously
0618: * and synchronously sets the minimum, maximum, visible amount,
0619: * and value properties of a scroll bar, so that they are
0620: * mutually consistent.
0621: *
0622: * @param newAmount the new visible amount
0623: * @see java.awt.Scrollbar#getVisibleAmount
0624: * @see java.awt.Scrollbar#setValues
0625: * @since JDK1.1
0626: */
0627: public void setVisibleAmount(int newAmount) {
0628: // Use setValues so that a consistent policy relating
0629: // minimum, maximum, visible amount, and value is enforced.
0630: setValues(value, newAmount, minimum, maximum);
0631: }
0632:
0633: /**
0634: * Sets the unit increment for this scroll bar.
0635: * <p>
0636: * The unit increment is the value that is added or subtracted
0637: * when the user activates the unit increment area of the
0638: * scroll bar, generally through a mouse or keyboard gesture
0639: * that the scroll bar receives as an adjustment event.
0640: * The unit increment must be greater than zero.
0641: * Attepts to set the unit increment to a value lower than 1
0642: * will result in a value of 1 being set.
0643: *
0644: * @param v the amount by which to increment or decrement
0645: * the scroll bar's value
0646: * @see java.awt.Scrollbar#getUnitIncrement
0647: * @since JDK1.1
0648: */
0649: public void setUnitIncrement(int v) {
0650: setLineIncrement(v);
0651: }
0652:
0653: /**
0654: * @deprecated As of JDK version 1.1,
0655: * replaced by <code>setUnitIncrement(int)</code>.
0656: */
0657:
0658: public synchronized void setLineIncrement(int v) {
0659: int tmp = (v < 1) ? 1 : v;
0660:
0661: if (lineIncrement == tmp) {
0662: return;
0663: }
0664: lineIncrement = tmp;
0665:
0666: ScrollbarPeer peer = (ScrollbarPeer) this .peer;
0667: if (peer != null) {
0668: peer.setLineIncrement(v);
0669: }
0670: }
0671:
0672: /**
0673: * Gets the unit increment for this scrollbar.
0674: * <p>
0675: * The unit increment is the value that is added or subtracted
0676: * when the user activates the unit increment area of the
0677: * scroll bar, generally through a mouse or keyboard gesture
0678: * that the scroll bar receives as an adjustment event.
0679: * The unit increment must be greater than zero.
0680: *
0681: * @return the unit increment of this scroll bar
0682: * @see java.awt.Scrollbar#setUnitIncrement
0683: * @since JDK1.1
0684: */
0685: public int getUnitIncrement() {
0686: return getLineIncrement();
0687: }
0688:
0689: /**
0690: * @deprecated As of JDK version 1.1,
0691: * replaced by <code>getUnitIncrement()</code>.
0692: */
0693: public int getLineIncrement() {
0694: return lineIncrement;
0695: }
0696:
0697: /**
0698: * Sets the block increment for this scroll bar.
0699: * <p>
0700: * The block increment is the value that is added or subtracted
0701: * when the user activates the block increment area of the
0702: * scroll bar, generally through a mouse or keyboard gesture
0703: * that the scroll bar receives as an adjustment event.
0704: * The block increment must be greater than zero.
0705: * Attepts to set the block increment to a value lower than 1
0706: * will result in a value of 1 being set.
0707: *
0708: * @param v the amount by which to increment or decrement
0709: * the scroll bar's value
0710: * @see java.awt.Scrollbar#getBlockIncrement
0711: * @since JDK1.1
0712: */
0713: public void setBlockIncrement(int v) {
0714: setPageIncrement(v);
0715: }
0716:
0717: /**
0718: * @deprecated As of JDK version 1.1,
0719: * replaced by <code>setBlockIncrement()</code>.
0720: */
0721: public synchronized void setPageIncrement(int v) {
0722: int tmp = (v < 1) ? 1 : v;
0723:
0724: if (pageIncrement == tmp) {
0725: return;
0726: }
0727: pageIncrement = tmp;
0728:
0729: ScrollbarPeer peer = (ScrollbarPeer) this .peer;
0730: if (peer != null) {
0731: peer.setPageIncrement(v);
0732: }
0733: }
0734:
0735: /**
0736: * Gets the block increment of this scroll bar.
0737: * <p>
0738: * The block increment is the value that is added or subtracted
0739: * when the user activates the block increment area of the
0740: * scroll bar, generally through a mouse or keyboard gesture
0741: * that the scroll bar receives as an adjustment event.
0742: * The block increment must be greater than zero.
0743: *
0744: * @return the block increment of this scroll bar
0745: * @see java.awt.Scrollbar#setBlockIncrement
0746: * @since JDK1.1
0747: */
0748: public int getBlockIncrement() {
0749: return getPageIncrement();
0750: }
0751:
0752: /**
0753: * @deprecated As of JDK version 1.1,
0754: * replaced by <code>getBlockIncrement()</code>.
0755: */
0756: public int getPageIncrement() {
0757: return pageIncrement;
0758: }
0759:
0760: /**
0761: * Sets the values of four properties for this scroll bar:
0762: * <code>value</code>, <code>visibleAmount</code>,
0763: * <code>minimum</code>, and <code>maximum</code>.
0764: * If the values supplied for these properties are inconsistent
0765: * or incorrect, they will be changed to ensure consistency.
0766: * <p>
0767: * This method simultaneously and synchronously sets the values
0768: * of four scroll bar properties, assuring that the values of
0769: * these properties are mutually consistent. It enforces the
0770: * following constraints:
0771: * <code>maximum</code> must be greater than <code>minimum</code>,
0772: * <code>maximum - minimum</code> must not be greater
0773: * than <code>Integer.MAX_VALUE</code>,
0774: * <code>visibleAmount</code> must be greater than zero.
0775: * <code>visibleAmount</code> must not be greater than
0776: * <code>maximum - minimum</code>,
0777: * <code>value</code> must not be less than <code>minimum</code>,
0778: * and <code>value</code> must not be greater than
0779: * <code>maximum - visibleAmount</code>
0780: *
0781: * @param value is the position in the current window
0782: * @param visible is the visible amount of the scroll bar
0783: * @param minimum is the minimum value of the scroll bar
0784: * @param maximum is the maximum value of the scroll bar
0785: * @see #setMinimum
0786: * @see #setMaximum
0787: * @see #setVisibleAmount
0788: * @see #setValue
0789: */
0790: public void setValues(int value, int visible, int minimum,
0791: int maximum) {
0792: int oldValue;
0793: synchronized (this ) {
0794: if (minimum == Integer.MAX_VALUE) {
0795: minimum = Integer.MAX_VALUE - 1;
0796: }
0797: if (maximum <= minimum) {
0798: maximum = minimum + 1;
0799: }
0800:
0801: long maxMinusMin = (long) maximum - (long) minimum;
0802: if (maxMinusMin > Integer.MAX_VALUE) {
0803: maxMinusMin = Integer.MAX_VALUE;
0804: // J2SE 1.5 spec.
0805: maximum = minimum + (int) maxMinusMin;
0806: }
0807: if (visible > (int) maxMinusMin) {
0808: visible = (int) maxMinusMin;
0809: }
0810: if (visible < 1) {
0811: visible = 1;
0812: }
0813:
0814: if (value < minimum) {
0815: value = minimum;
0816: }
0817: if (value > maximum - visible) {
0818: value = maximum - visible;
0819: }
0820:
0821: oldValue = this .value;
0822: this .value = value;
0823: this .visibleAmount = visible;
0824: this .minimum = minimum;
0825: this .maximum = maximum;
0826: ScrollbarPeer peer = (ScrollbarPeer) this .peer;
0827: if (peer != null) {
0828: peer.setValues(value, visibleAmount, minimum, maximum);
0829: }
0830: }
0831: }
0832:
0833: /**
0834: * Adds the specified adjustment listener to receive instances of
0835: * <code>AdjustmentEvent</code> from this scroll bar.
0836: * If l is null, no exception is thrown and no action is performed.
0837: *
0838: * @param l the adjustment listener.
0839: * @see java.awt.event.AdjustmentEvent
0840: * @see java.awt.event.AdjustmentListener
0841: * @see java.awt.Scrollbar#removeAdjustmentListener
0842: * @since JDK1.1
0843: */
0844: public synchronized void addAdjustmentListener(AdjustmentListener l) {
0845: if (l == null) {
0846: return;
0847: }
0848: adjustmentListener = AWTEventMulticaster.add(
0849: adjustmentListener, l);
0850: newEventsOnly = true;
0851: }
0852:
0853: /**
0854: * Removes the specified adjustment listener so that it no longer
0855: * receives instances of <code>AdjustmentEvent</code> from this scroll bar.
0856: * If l is null, no exception is thrown and no action is performed.
0857: *
0858: * @param l the adjustment listener.
0859: * @see java.awt.event.AdjustmentEvent
0860: * @see java.awt.event.AdjustmentListener
0861: * @see java.awt.Scrollbar#addAdjustmentListener
0862: * @since JDK1.1
0863: */
0864: public synchronized void removeAdjustmentListener(
0865: AdjustmentListener l) {
0866: if (l == null) {
0867: return;
0868: }
0869: adjustmentListener = AWTEventMulticaster.remove(
0870: adjustmentListener, l);
0871: }
0872:
0873: /**
0874: * Returns an array of all the adjustment listeners
0875: * registered on this scrollbar.
0876: *
0877: * @return all of this scrollbar's <code>AdjustmentListener</code>s
0878: * or an empty array if no adjustment
0879: * listeners are currently registered
0880: *
0881: * @see #addAdjustmentListener
0882: * @see #removeAdjustmentListener
0883: * @see java.awt.event.AdjustmentEvent
0884: * @see java.awt.event.AdjustmentListener
0885: * @since 1.4
0886: */
0887: public synchronized AdjustmentListener[] getAdjustmentListeners() {
0888: return (AdjustmentListener[]) AWTEventMulticaster.getListeners(
0889: (EventListener) adjustmentListener,
0890: AdjustmentListener.class);
0891: }
0892:
0893: // NOTE: remove when filtering is done at lower level
0894: boolean eventEnabled(AWTEvent e) {
0895: if (e.id == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) {
0896: if ((eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0
0897: || adjustmentListener != null) {
0898: return true;
0899: }
0900: return false;
0901: }
0902: return super .eventEnabled(e);
0903: }
0904:
0905: /**
0906: * Processes events on this scroll bar. If the event is an
0907: * instance of <code>AdjustmentEvent</code>, it invokes the
0908: * <code>processAdjustmentEvent</code> method.
0909: * Otherwise, it invokes its superclass's
0910: * <code>processEvent</code> method.
0911: * @param e the event.
0912: * @see java.awt.event.AdjustmentEvent
0913: * @see java.awt.Scrollbar#processAdjustmentEvent
0914: * @since JDK1.1
0915: */
0916: protected void processEvent(AWTEvent e) {
0917: if (e instanceof AdjustmentEvent) {
0918: processAdjustmentEvent((AdjustmentEvent) e);
0919: return;
0920: }
0921: super .processEvent(e);
0922: }
0923:
0924: /**
0925: * Processes adjustment events occurring on this
0926: * scrollbar by dispatching them to any registered
0927: * <code>AdjustmentListener</code> objects.
0928: * <p>
0929: * This method is not called unless adjustment events are
0930: * enabled for this component. Adjustment events are enabled
0931: * when one of the following occurs:
0932: * <p><ul>
0933: * <li>An <code>AdjustmentListener</code> object is registered
0934: * via <code>addAdjustmentListener</code>.
0935: * <li>Adjustment events are enabled via <code>enableEvents</code>.
0936: * </ul><p>
0937: * @param e the adjustment event.
0938: * @see java.awt.event.AdjustmentEvent
0939: * @see java.awt.event.AdjustmentListener
0940: * @see java.awt.Scrollbar#addAdjustmentListener
0941: * @see java.awt.Component#enableEvents
0942: * @since JDK1.1
0943: */
0944: protected void processAdjustmentEvent(AdjustmentEvent e) {
0945: if (adjustmentListener != null) {
0946: adjustmentListener.adjustmentValueChanged(e);
0947: }
0948: }
0949:
0950: /**
0951: * Returns the parameter string representing the state of
0952: * this scroll bar. This string is useful for debugging.
0953: * @return the parameter string of this scroll bar.
0954: */
0955: protected String paramString() {
0956: return super .paramString() + ",val=" + value + ",vis="
0957: + visibleAmount + ",min=" + minimum + ",max=" + maximum
0958: + ((orientation == VERTICAL) ? ",vert" : ",horz");
0959: }
0960:
0961: /**
0962: * The scrollbars serialized Data Version.
0963: *
0964: * @serial
0965: */
0966: private int scrollbarSerializedDataVersion = 1;
0967:
0968: /**
0969: * Writes default serializable fields to stream. Writes
0970: * a list of serializable ItemListener(s) as optional data.
0971: * The non-serializable ItemListner(s) are detected and
0972: * no attempt is made to serialize them.
0973: *
0974: * @serialData Null terminated sequence of 0 or more pairs.
0975: * The pair consists of a String and Object.
0976: * The String indicates the type of object and
0977: * is one of the following :
0978: * itemListenerK indicating and ItemListener object.
0979: *
0980: * @see AWTEventMulticaster.save(ObjectOutputStream, String, EventListener)
0981: * @see java.awt.Component.itemListenerK
0982: */
0983: private void writeObject(ObjectOutputStream s) throws IOException {
0984: s.defaultWriteObject();
0985: AWTEventMulticaster.save(s, adjustmentListenerK,
0986: adjustmentListener);
0987: s.writeObject(null);
0988: }
0989:
0990: /**
0991: * Read the ObjectInputStream and if it isnt null
0992: * add a listener to receive item events fired
0993: * by the Scrollbar.
0994: * Unrecognised keys or values will be Ignored.
0995: *
0996: * @see removeActionListener()
0997: * @see addActionListener()
0998: */
0999: private void readObject(ObjectInputStream s)
1000: throws ClassNotFoundException, IOException {
1001: s.defaultReadObject();
1002: Object keyOrNull;
1003: while (null != (keyOrNull = s.readObject())) {
1004: String key = ((String) keyOrNull).intern();
1005: if (adjustmentListenerK == key)
1006: addAdjustmentListener((AdjustmentListener) (s
1007: .readObject()));
1008: else
1009: // skip value for unrecognized key
1010: s.readObject();
1011: }
1012: }
1013: }
|