1. /*
  2. * @(#)Scrollbar.java 1.108 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.peer.ScrollbarPeer;
  9. import java.awt.event.*;
  10. import java.util.EventListener;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.IOException;
  14. import javax.accessibility.*;
  15. /**
  16. * The <code>Scrollbar</code> class embodies a scroll bar, a
  17. * familiar user-interface object. A scroll bar provides a
  18. * convenient means for allowing a user to select from a
  19. * range of values. The following three vertical
  20. * scroll bars could be used as slider controls to pick
  21. * the red, green, and blue components of a color:
  22. * <p>
  23. * <img src="doc-files/Scrollbar-1.gif" alt="Image shows 3 vertical sliders, side-by-side."
  24. * ALIGN=center HSPACE=10 VSPACE=7>
  25. * <p>
  26. * Each scroll bar in this example could be created with
  27. * code similar to the following:
  28. * <p>
  29. * <hr><blockquote><pre>
  30. * redSlider=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
  31. * add(redSlider);
  32. * </pre></blockquote><hr>
  33. * <p>
  34. * Alternatively, a scroll bar can represent a range of values. For
  35. * example, if a scroll bar is used for scrolling through text, the
  36. * width of the "bubble" (also called the "thumb" or "scroll box")
  37. * can be used to represent the amount of text that is visible.
  38. * Here is an example of a scroll bar that represents a range:
  39. * <p>
  40. * <img src="doc-files/Scrollbar-2.gif"
  41. * alt="Image shows horizontal slider with starting range of 0 and ending range of 300. The slider thumb is labeled 60."
  42. * ALIGN=center HSPACE=10 VSPACE=7>
  43. * <p>
  44. * The value range represented by the bubble in this example
  45. * is the <em>visible amount</em>. The horizontal scroll bar
  46. * in this example could be created with code like the following:
  47. * <p>
  48. * <hr><blockquote><pre>
  49. * ranger = new Scrollbar(Scrollbar.HORIZONTAL, 0, 60, 0, 300);
  50. * add(ranger);
  51. * </pre></blockquote><hr>
  52. * <p>
  53. * Note that the actual maximum value of the scroll bar is the
  54. * <code>maximum</code> minus the <code>visible amount</code>.
  55. * In the previous example, because the <code>maximum</code> is
  56. * 300 and the <code>visible amount</code> is 60, the actual maximum
  57. * value is 240. The range of the scrollbar track is 0 - 300.
  58. * The left side of the bubble indicates the value of the
  59. * scroll bar.
  60. * <p>
  61. * Normally, the user changes the value of the scroll bar by
  62. * making a gesture with the mouse. For example, the user can
  63. * drag the scroll bar's bubble up and down, or click in the
  64. * scroll bar's unit increment or block increment areas. Keyboard
  65. * gestures can also be mapped to the scroll bar. By convention,
  66. * the <b>Page Up</b> and <b>Page Down</b>
  67. * keys are equivalent to clicking in the scroll bar's block
  68. * increment and block decrement areas.
  69. * <p>
  70. * When the user changes the value of the scroll bar, the scroll bar
  71. * receives an instance of <code>AdjustmentEvent</code>.
  72. * The scroll bar processes this event, passing it along to
  73. * any registered listeners.
  74. * <p>
  75. * Any object that wishes to be notified of changes to the
  76. * scroll bar's value should implement
  77. * <code>AdjustmentListener</code>, an interface defined in
  78. * the package <code>java.awt.event</code>.
  79. * Listeners can be added and removed dynamically by calling
  80. * the methods <code>addAdjustmentListener</code> and
  81. * <code>removeAdjustmentListener</code>.
  82. * <p>
  83. * The <code>AdjustmentEvent</code> class defines five types
  84. * of adjustment event, listed here:
  85. * <p>
  86. * <ul>
  87. * <li><code>AdjustmentEvent.TRACK</code> is sent out when the
  88. * user drags the scroll bar's bubble.
  89. * <li><code>AdjustmentEvent.UNIT_INCREMENT</code> is sent out
  90. * when the user clicks in the left arrow of a horizontal scroll
  91. * bar, or the top arrow of a vertical scroll bar, or makes the
  92. * equivalent gesture from the keyboard.
  93. * <li><code>AdjustmentEvent.UNIT_DECREMENT</code> is sent out
  94. * when the user clicks in the right arrow of a horizontal scroll
  95. * bar, or the bottom arrow of a vertical scroll bar, or makes the
  96. * equivalent gesture from the keyboard.
  97. * <li><code>AdjustmentEvent.BLOCK_INCREMENT</code> is sent out
  98. * when the user clicks in the track, to the left of the bubble
  99. * on a horizontal scroll bar, or above the bubble on a vertical
  100. * scroll bar. By convention, the <b>Page Up</b>
  101. * key is equivalent, if the user is using a keyboard that
  102. * defines a <b>Page Up</b> key.
  103. * <li><code>AdjustmentEvent.BLOCK_DECREMENT</code> is sent out
  104. * when the user clicks in the track, to the right of the bubble
  105. * on a horizontal scroll bar, or below the bubble on a vertical
  106. * scroll bar. By convention, the <b>Page Down</b>
  107. * key is equivalent, if the user is using a keyboard that
  108. * defines a <b>Page Down</b> key.
  109. * </ul>
  110. * <p>
  111. * The JDK 1.0 event system is supported for backwards
  112. * compatibility, but its use with newer versions of the platform is
  113. * discouraged. The five types of adjustment events introduced
  114. * with JDK 1.1 correspond to the five event types
  115. * that are associated with scroll bars in previous platform versions.
  116. * The following list gives the adjustment event type,
  117. * and the corresponding JDK 1.0 event type it replaces.
  118. * <p>
  119. * <ul>
  120. * <li><code>AdjustmentEvent.TRACK</code> replaces
  121. * <code>Event.SCROLL_ABSOLUTE</code>
  122. * <li><code>AdjustmentEvent.UNIT_INCREMENT</code> replaces
  123. * <code>Event.SCROLL_LINE_UP</code>
  124. * <li><code>AdjustmentEvent.UNIT_DECREMENT</code> replaces
  125. * <code>Event.SCROLL_LINE_DOWN</code>
  126. * <li><code>AdjustmentEvent.BLOCK_INCREMENT</code> replaces
  127. * <code>Event.SCROLL_PAGE_UP</code>
  128. * <li><code>AdjustmentEvent.BLOCK_DECREMENT</code> replaces
  129. * <code>Event.SCROLL_PAGE_DOWN</code>
  130. * </ul>
  131. * <p>
  132. * <b>Note</b>: We recommend using a <code>Scrollbar</code>
  133. * for value selection only. If you want to implement
  134. * a scrollable component inside a container, we recommend you use
  135. * a {@link ScrollPane ScrollPane}. If you use a
  136. * <code>Scrollbar</code> for this purpose, you are likely to
  137. * encounter issues with painting, key handling, sizing and
  138. * positioning.
  139. *
  140. * @version 1.108, 05/18/04
  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, Accessible {
  147. /**
  148. * A constant that indicates a horizontal scroll bar.
  149. */
  150. public static final int HORIZONTAL = 0;
  151. /**
  152. * A constant that indicates a vertical scroll bar.
  153. */
  154. public static final int VERTICAL = 1;
  155. /**
  156. * The value of the <code>Scrollbar</code>.
  157. * This property must be greater than or equal to <code>minimum</code>
  158. * and less than or equal to
  159. * <code>maximum - visibleAmount</code>
  160. *
  161. * @serial
  162. * @see #getValue
  163. * @see #setValue
  164. */
  165. int value;
  166. /**
  167. * The maximum value of the <code>Scrollbar</code>.
  168. * This value must be greater than the <code>minimum</code>
  169. * value.<br>
  170. *
  171. * @serial
  172. * @see #getMaximum
  173. * @see #setMaximum
  174. */
  175. int maximum;
  176. /**
  177. * The minimum value of the <code>Scrollbar</code>.
  178. * This value must be less than the <code>maximum</code>
  179. * value.<br>
  180. *
  181. * @serial
  182. * @see #getMinimum
  183. * @see #setMinimum
  184. */
  185. int minimum;
  186. /**
  187. * The size of the <code>Scrollbar</code>'s bubble.
  188. * When a scroll bar is used to select a range of values,
  189. * the visibleAmount represents the size of this range.
  190. * This is visually indicated by the size of the bubble.
  191. *
  192. * @serial
  193. * @see #getVisibleAmount
  194. * @see #setVisibleAmount
  195. */
  196. int visibleAmount;
  197. /**
  198. * The <code>Scrollbar</code>'s orientation--being either horizontal
  199. * or vertical.
  200. * This value should be specified when the scrollbar is created.<BR>
  201. * orientation can be either : <code>VERTICAL</code> or
  202. * <code>HORIZONTAL</code> only.
  203. *
  204. * @serial
  205. * @see #getOrientation
  206. * @see #setOrientation
  207. */
  208. int orientation;
  209. /**
  210. * The amount by which the scrollbar value will change when going
  211. * up or down by a line.
  212. * This value must be greater than zero.
  213. *
  214. * @serial
  215. * @see #getLineIncrement
  216. * @see #setLineIncrement
  217. */
  218. int lineIncrement = 1;
  219. /**
  220. * The amount by which the scrollbar value will change when going
  221. * up or down by a page.
  222. * This value must be greater than zero.
  223. *
  224. * @serial
  225. * @see #getPageIncrement
  226. * @see #setPageIncrement
  227. */
  228. int pageIncrement = 10;
  229. /**
  230. * The adjusting status of the <code>Scrollbar</code>.
  231. * True if the value is in the process of changing as a result of
  232. * actions being taken by the user.
  233. *
  234. * @see #getValueIsAdjusting
  235. * @see #setValueIsAdjusting
  236. * @since 1.4
  237. */
  238. transient boolean isAdjusting;
  239. transient AdjustmentListener adjustmentListener;
  240. private static final String base = "scrollbar";
  241. private static int nameCounter = 0;
  242. /*
  243. * JDK 1.1 serialVersionUID
  244. */
  245. private static final long serialVersionUID = 8451667562882310543L;
  246. /**
  247. * Initialize JNI field and method IDs.
  248. */
  249. private static native void initIDs();
  250. static {
  251. /* ensure that the necessary native libraries are loaded */
  252. Toolkit.loadLibraries();
  253. if (!GraphicsEnvironment.isHeadless()) {
  254. initIDs();
  255. }
  256. }
  257. /**
  258. * Constructs a new vertical scroll bar.
  259. * The default properties of the scroll bar are listed in
  260. * the following table:
  261. * <p> </p>
  262. * <table border=1 summary="Scrollbar default properties">
  263. * <tr>
  264. * <th>Property</th>
  265. * <th>Description</th>
  266. * <th>Default Value</th>
  267. * </tr>
  268. * <tr>
  269. * <td>orientation</td>
  270. * <td>indicates whether the scroll bar is vertical
  271. * <br>or horizontal</td>
  272. * <td><code>Scrollbar.VERTICAL</code></td>
  273. * </tr>
  274. * <tr>
  275. * <td>value</td>
  276. * <td>value which controls the location
  277. * <br>of the scroll bar's bubble</td>
  278. * <td>0</td>
  279. * </tr>
  280. * <tr>
  281. * <td>visible amount</td>
  282. * <td>visible amount of the scroll bar's range,
  283. * <br>typically represented by the size of the
  284. * <br>scroll bar's bubble</td>
  285. * <td>10</td>
  286. * </tr>
  287. * <tr>
  288. * <td>minimum</td>
  289. * <td>minimum value of the scroll bar</td>
  290. * <td>0</td>
  291. * </tr>
  292. * <tr>
  293. * <td>maximum</td>
  294. * <td>maximum value of the scroll bar</td>
  295. * <td>100</td>
  296. * </tr>
  297. * <tr>
  298. * <td>unit increment</td>
  299. * <td>amount the value changes when the
  300. * <br>Line Up or Line Down key is pressed,
  301. * <br>or when the end arrows of the scrollbar
  302. * <br>are clicked </td>
  303. * <td>1</td>
  304. * </tr>
  305. * <tr>
  306. * <td>block increment</td>
  307. * <td>amount the value changes when the
  308. * <br>Page Up or Page Down key is pressed,
  309. * <br>or when the scrollbar track is clicked
  310. * <br>on either side of the bubble </td>
  311. * <td>10</td>
  312. * </tr>
  313. * </table>
  314. *
  315. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  316. * returns true.
  317. * @see java.awt.GraphicsEnvironment#isHeadless
  318. */
  319. public Scrollbar() throws HeadlessException {
  320. this(VERTICAL, 0, 10, 0, 100);
  321. }
  322. /**
  323. * Constructs a new scroll bar with the specified orientation.
  324. * <p>
  325. * The <code>orientation</code> argument must take one of the two
  326. * values <code>Scrollbar.HORIZONTAL</code>,
  327. * or <code>Scrollbar.VERTICAL</code>,
  328. * indicating a horizontal or vertical scroll bar, respectively.
  329. *
  330. * @param orientation indicates the orientation of the scroll bar
  331. * @exception IllegalArgumentException when an illegal value for
  332. * the <code>orientation</code> argument is supplied
  333. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  334. * returns true.
  335. * @see java.awt.GraphicsEnvironment#isHeadless
  336. */
  337. public Scrollbar(int orientation) throws HeadlessException {
  338. this(orientation, 0, 10, 0, 100);
  339. }
  340. /**
  341. * Constructs a new scroll bar with the specified orientation,
  342. * initial value, visible amount, and minimum and maximum values.
  343. * <p>
  344. * The <code>orientation</code> argument must take one of the two
  345. * values <code>Scrollbar.HORIZONTAL</code>,
  346. * or <code>Scrollbar.VERTICAL</code>,
  347. * indicating a horizontal or vertical scroll bar, respectively.
  348. * <p>
  349. * The parameters supplied to this constructor are subject to the
  350. * constraints described in {@link #setValues(int, int, int, int)}.
  351. *
  352. * @param orientation indicates the orientation of the scroll bar.
  353. * @param value the initial value of the scroll bar
  354. * @param visible the visible amount of the scroll bar, typically
  355. * represented by the size of the bubble
  356. * @param minimum the minimum value of the scroll bar
  357. * @param maximum the maximum value of the scroll bar
  358. * @exception IllegalArgumentException when an illegal value for
  359. * the <code>orientation</code> argument is supplied
  360. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  361. * returns true.
  362. * @see #setValues
  363. * @see java.awt.GraphicsEnvironment#isHeadless
  364. */
  365. public Scrollbar(int orientation, int value, int visible, int minimum,
  366. int maximum) throws HeadlessException {
  367. GraphicsEnvironment.checkHeadless();
  368. switch (orientation) {
  369. case HORIZONTAL:
  370. case VERTICAL:
  371. this.orientation = orientation;
  372. break;
  373. default:
  374. throw new IllegalArgumentException("illegal scrollbar orientation");
  375. }
  376. setValues(value, visible, minimum, maximum);
  377. }
  378. /**
  379. * Constructs a name for this component. Called by <code>getName</code>
  380. * when the name is <code>null</code>.
  381. */
  382. String constructComponentName() {
  383. synchronized (getClass()) {
  384. return base + nameCounter++;
  385. }
  386. }
  387. /**
  388. * Creates the <code>Scrollbar</code>'s peer. The peer allows you to modify
  389. * the appearance of the <code>Scrollbar</code> without changing any of its
  390. * functionality.
  391. */
  392. public void addNotify() {
  393. synchronized (getTreeLock()) {
  394. if (peer == null)
  395. peer = getToolkit().createScrollbar(this);
  396. super.addNotify();
  397. }
  398. }
  399. /**
  400. * Returns the orientation of this scroll bar.
  401. *
  402. * @return the orientation of this scroll bar, either
  403. * <code>Scrollbar.HORIZONTAL</code> or
  404. * <code>Scrollbar.VERTICAL</code>
  405. * @see java.awt.Scrollbar#setOrientation
  406. */
  407. public int getOrientation() {
  408. return orientation;
  409. }
  410. /**
  411. * Sets the orientation for this scroll bar.
  412. *
  413. * @param orientation the orientation of this scroll bar, either
  414. * <code>Scrollbar.HORIZONTAL</code> or
  415. * <code>Scrollbar.VERTICAL</code>
  416. * @see java.awt.Scrollbar#getOrientation
  417. * @exception IllegalArgumentException if the value supplied
  418. * for <code>orientation</code> is not a
  419. * legal value
  420. * @since JDK1.1
  421. */
  422. public void setOrientation(int orientation) {
  423. synchronized (getTreeLock()) {
  424. if (orientation == this.orientation) {
  425. return;
  426. }
  427. switch (orientation) {
  428. case HORIZONTAL:
  429. case VERTICAL:
  430. this.orientation = orientation;
  431. break;
  432. default:
  433. throw new IllegalArgumentException("illegal scrollbar orientation");
  434. }
  435. /* Create a new peer with the specified orientation. */
  436. if (peer != null) {
  437. removeNotify();
  438. addNotify();
  439. invalidate();
  440. }
  441. }
  442. if (accessibleContext != null) {
  443. accessibleContext.firePropertyChange(
  444. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  445. ((orientation == VERTICAL)
  446. ? AccessibleState.HORIZONTAL : AccessibleState.VERTICAL),
  447. ((orientation == VERTICAL)
  448. ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
  449. }
  450. }
  451. /**
  452. * Gets the current value of this scroll bar.
  453. *
  454. * @return the current value of this scroll bar
  455. * @see java.awt.Scrollbar#getMinimum
  456. * @see java.awt.Scrollbar#getMaximum
  457. */
  458. public int getValue() {
  459. return value;
  460. }
  461. /**
  462. * Sets the value of this scroll bar to the specified value.
  463. * <p>
  464. * If the value supplied is less than the current <code>minimum</code>
  465. * or greater than the current <code>maximum - visibleAmount</code>,
  466. * then either <code>minimum</code> or <code>maximum - visibleAmount</code>
  467. * is substituted, as appropriate.
  468. * <p>
  469. * Normally, a program should change a scroll bar's
  470. * value only by calling <code>setValues</code>.
  471. * The <code>setValues</code> method simultaneously
  472. * and synchronously sets the minimum, maximum, visible amount,
  473. * and value properties of a scroll bar, so that they are
  474. * mutually consistent.
  475. * <p>
  476. * Calling this method does not fire an
  477. * <code>AdjustmentEvent</code>.
  478. *
  479. * @param newValue the new value of the scroll bar
  480. * @see java.awt.Scrollbar#setValues
  481. * @see java.awt.Scrollbar#getValue
  482. * @see java.awt.Scrollbar#getMinimum
  483. * @see java.awt.Scrollbar#getMaximum
  484. */
  485. public void setValue(int newValue) {
  486. // Use setValues so that a consistent policy relating
  487. // minimum, maximum, visible amount, and value is enforced.
  488. setValues(newValue, visibleAmount, minimum, maximum);
  489. }
  490. /**
  491. * Gets the minimum value of this scroll bar.
  492. *
  493. * @return the minimum value of this scroll bar
  494. * @see java.awt.Scrollbar#getValue
  495. * @see java.awt.Scrollbar#getMaximum
  496. */
  497. public int getMinimum() {
  498. return minimum;
  499. }
  500. /**
  501. * Sets the minimum value of this scroll bar.
  502. * <p>
  503. * When <code>setMinimum</code> is called, the minimum value
  504. * is changed, and other values (including the maximum, the
  505. * visible amount, and the current scroll bar value)
  506. * are changed to be consistent with the new minimum.
  507. * <p>
  508. * Normally, a program should change a scroll bar's minimum
  509. * value only by calling <code>setValues</code>.
  510. * The <code>setValues</code> method simultaneously
  511. * and synchronously sets the minimum, maximum, visible amount,
  512. * and value properties of a scroll bar, so that they are
  513. * mutually consistent.
  514. * <p>
  515. * Note that setting the minimum value to <code>Integer.MAX_VALUE</code>
  516. * will result in the new minimum value being set to
  517. * <code>Integer.MAX_VALUE - 1</code>.
  518. *
  519. * @param newMinimum the new minimum value for this scroll bar
  520. * @see java.awt.Scrollbar#setValues
  521. * @see java.awt.Scrollbar#setMaximum
  522. * @since JDK1.1
  523. */
  524. public void setMinimum(int newMinimum) {
  525. // No checks are necessary in this method since minimum is
  526. // the first variable checked in the setValues function.
  527. // Use setValues so that a consistent policy relating
  528. // minimum, maximum, visible amount, and value is enforced.
  529. setValues(value, visibleAmount, newMinimum, maximum);
  530. }
  531. /**
  532. * Gets the maximum value of this scroll bar.
  533. *
  534. * @return the maximum value of this scroll bar
  535. * @see java.awt.Scrollbar#getValue
  536. * @see java.awt.Scrollbar#getMinimum
  537. */
  538. public int getMaximum() {
  539. return maximum;
  540. }
  541. /**
  542. * Sets the maximum value of this scroll bar.
  543. * <p>
  544. * When <code>setMaximum</code> is called, the maximum value
  545. * is changed, and other values (including the minimum, the
  546. * visible amount, and the current scroll bar value)
  547. * are changed to be consistent with the new maximum.
  548. * <p>
  549. * Normally, a program should change a scroll bar's maximum
  550. * value only by calling <code>setValues</code>.
  551. * The <code>setValues</code> method simultaneously
  552. * and synchronously sets the minimum, maximum, visible amount,
  553. * and value properties of a scroll bar, so that they are
  554. * mutually consistent.
  555. * <p>
  556. * Note that setting the maximum value to <code>Integer.MIN_VALUE</code>
  557. * will result in the new maximum value being set to
  558. * <code>Integer.MIN_VALUE + 1</code>.
  559. *
  560. * @param newMaximum the new maximum value
  561. * for this scroll bar
  562. * @see java.awt.Scrollbar#setValues
  563. * @see java.awt.Scrollbar#setMinimum
  564. * @since JDK1.1
  565. */
  566. public void setMaximum(int newMaximum) {
  567. // minimum is checked first in setValues, so we need to
  568. // enforce minimum and maximum checks here.
  569. if (newMaximum == Integer.MIN_VALUE) {
  570. newMaximum = Integer.MIN_VALUE + 1;
  571. }
  572. if (minimum >= newMaximum) {
  573. minimum = newMaximum - 1;
  574. }
  575. // Use setValues so that a consistent policy relating
  576. // minimum, maximum, visible amount, and value is enforced.
  577. setValues(value, visibleAmount, minimum, newMaximum);
  578. }
  579. /**
  580. * Gets the visible amount of this scroll bar.
  581. * <p>
  582. * When a scroll bar is used to select a range of values,
  583. * the visible amount is used to represent the range of values
  584. * that are currently visible. The size of the scroll bar's
  585. * bubble (also called a thumb or scroll box), usually gives a
  586. * visual representation of the relationship of the visible
  587. * amount to the range of the scroll bar.
  588. * <p>
  589. * The scroll bar's bubble may not be displayed when it is not
  590. * moveable (e.g. when it takes up the entire length of the
  591. * scroll bar's track, or when the scroll bar is disabled).
  592. * Whether the bubble is displayed or not will not affect
  593. * the value returned by <code>getVisibleAmount</code>.
  594. *
  595. * @return the visible amount of this scroll bar
  596. * @see java.awt.Scrollbar#setVisibleAmount
  597. * @since JDK1.1
  598. */
  599. public int getVisibleAmount() {
  600. return getVisible();
  601. }
  602. /**
  603. * @deprecated As of JDK version 1.1,
  604. * replaced by <code>getVisibleAmount()</code>.
  605. */
  606. @Deprecated
  607. public int getVisible() {
  608. return visibleAmount;
  609. }
  610. /**
  611. * Sets the visible amount of this scroll bar.
  612. * <p>
  613. * When a scroll bar is used to select a range of values,
  614. * the visible amount is used to represent the range of values
  615. * that are currently visible. The size of the scroll bar's
  616. * bubble (also called a thumb or scroll box), usually gives a
  617. * visual representation of the relationship of the visible
  618. * amount to the range of the scroll bar.
  619. * <p>
  620. * The scroll bar's bubble may not be displayed when it is not
  621. * moveable (e.g. when it takes up the entire length of the
  622. * scroll bar's track, or when the scroll bar is disabled).
  623. * Whether the bubble is displayed or not will not affect
  624. * the value returned by <code>getVisibleAmount</code>.
  625. * <p>
  626. * If the visible amount supplied is less than <code>one</code>
  627. * or greater than the current <code>maximum - minimum</code>,
  628. * then either <code>one</code> or <code>maximum - minimum</code>
  629. * is substituted, as appropriate.
  630. * <p>
  631. * Normally, a program should change a scroll bar's
  632. * value only by calling <code>setValues</code>.
  633. * The <code>setValues</code> method simultaneously
  634. * and synchronously sets the minimum, maximum, visible amount,
  635. * and value properties of a scroll bar, so that they are
  636. * mutually consistent.
  637. *
  638. * @param newAmount the new visible amount
  639. * @see java.awt.Scrollbar#getVisibleAmount
  640. * @see java.awt.Scrollbar#setValues
  641. * @since JDK1.1
  642. */
  643. public void setVisibleAmount(int newAmount) {
  644. // Use setValues so that a consistent policy relating
  645. // minimum, maximum, visible amount, and value is enforced.
  646. setValues(value, newAmount, minimum, maximum);
  647. }
  648. /**
  649. * Sets the unit increment for this scroll bar.
  650. * <p>
  651. * The unit increment is the value that is added or subtracted
  652. * when the user activates the unit increment area of the
  653. * scroll bar, generally through a mouse or keyboard gesture
  654. * that the scroll bar receives as an adjustment event.
  655. * The unit increment must be greater than zero.
  656. * Attepts to set the unit increment to a value lower than 1
  657. * will result in a value of 1 being set.
  658. *
  659. * @param v the amount by which to increment or decrement
  660. * the scroll bar's value
  661. * @see java.awt.Scrollbar#getUnitIncrement
  662. * @since JDK1.1
  663. */
  664. public void setUnitIncrement(int v) {
  665. setLineIncrement(v);
  666. }
  667. /**
  668. * @deprecated As of JDK version 1.1,
  669. * replaced by <code>setUnitIncrement(int)</code>.
  670. */
  671. @Deprecated
  672. public synchronized void setLineIncrement(int v) {
  673. int tmp = (v < 1) ? 1 : v;
  674. if (lineIncrement == tmp) {
  675. return;
  676. }
  677. lineIncrement = tmp;
  678. ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  679. if (peer != null) {
  680. peer.setLineIncrement(lineIncrement);
  681. }
  682. }
  683. /**
  684. * Gets the unit increment for this scrollbar.
  685. * <p>
  686. * The unit increment is the value that is added or subtracted
  687. * when the user activates the unit increment area of the
  688. * scroll bar, generally through a mouse or keyboard gesture
  689. * that the scroll bar receives as an adjustment event.
  690. * The unit increment must be greater than zero.
  691. *
  692. * @return the unit increment of this scroll bar
  693. * @see java.awt.Scrollbar#setUnitIncrement
  694. * @since JDK1.1
  695. */
  696. public int getUnitIncrement() {
  697. return getLineIncrement();
  698. }
  699. /**
  700. * @deprecated As of JDK version 1.1,
  701. * replaced by <code>getUnitIncrement()</code>.
  702. */
  703. @Deprecated
  704. public int getLineIncrement() {
  705. return lineIncrement;
  706. }
  707. /**
  708. * Sets the block increment for this scroll bar.
  709. * <p>
  710. * The block increment is the value that is added or subtracted
  711. * when the user activates the block increment area of the
  712. * scroll bar, generally through a mouse or keyboard gesture
  713. * that the scroll bar receives as an adjustment event.
  714. * The block increment must be greater than zero.
  715. * Attepts to set the block increment to a value lower than 1
  716. * will result in a value of 1 being set.
  717. *
  718. * @param v the amount by which to increment or decrement
  719. * the scroll bar's value
  720. * @see java.awt.Scrollbar#getBlockIncrement
  721. * @since JDK1.1
  722. */
  723. public void setBlockIncrement(int v) {
  724. setPageIncrement(v);
  725. }
  726. /**
  727. * @deprecated As of JDK version 1.1,
  728. * replaced by <code>setBlockIncrement()</code>.
  729. */
  730. @Deprecated
  731. public synchronized void setPageIncrement(int v) {
  732. int tmp = (v < 1) ? 1 : v;
  733. if (pageIncrement == tmp) {
  734. return;
  735. }
  736. pageIncrement = tmp;
  737. ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  738. if (peer != null) {
  739. peer.setPageIncrement(pageIncrement);
  740. }
  741. }
  742. /**
  743. * Gets the block increment of this scroll bar.
  744. * <p>
  745. * The block increment is the value that is added or subtracted
  746. * when the user activates the block increment area of the
  747. * scroll bar, generally through a mouse or keyboard gesture
  748. * that the scroll bar receives as an adjustment event.
  749. * The block increment must be greater than zero.
  750. *
  751. * @return the block increment of this scroll bar
  752. * @see java.awt.Scrollbar#setBlockIncrement
  753. * @since JDK1.1
  754. */
  755. public int getBlockIncrement() {
  756. return getPageIncrement();
  757. }
  758. /**
  759. * @deprecated As of JDK version 1.1,
  760. * replaced by <code>getBlockIncrement()</code>.
  761. */
  762. @Deprecated
  763. public int getPageIncrement() {
  764. return pageIncrement;
  765. }
  766. /**
  767. * Sets the values of four properties for this scroll bar:
  768. * <code>value</code>, <code>visibleAmount</code>,
  769. * <code>minimum</code>, and <code>maximum</code>.
  770. * If the values supplied for these properties are inconsistent
  771. * or incorrect, they will be changed to ensure consistency.
  772. * <p>
  773. * This method simultaneously and synchronously sets the values
  774. * of four scroll bar properties, assuring that the values of
  775. * these properties are mutually consistent. It enforces the
  776. * following constraints:
  777. * <code>maximum</code> must be greater than <code>minimum</code>,
  778. * <code>maximum - minimum</code> must not be greater
  779. * than <code>Integer.MAX_VALUE</code>,
  780. * <code>visibleAmount</code> must be greater than zero.
  781. * <code>visibleAmount</code> must not be greater than
  782. * <code>maximum - minimum</code>,
  783. * <code>value</code> must not be less than <code>minimum</code>,
  784. * and <code>value</code> must not be greater than
  785. * <code>maximum - visibleAmount</code>
  786. * <p>
  787. * Calling this method does not fire an
  788. * <code>AdjustmentEvent</code>.
  789. *
  790. * @param value is the position in the current window
  791. * @param visible is the visible amount of the scroll bar
  792. * @param minimum is the minimum value of the scroll bar
  793. * @param maximum is the maximum value of the scroll bar
  794. * @see #setMinimum
  795. * @see #setMaximum
  796. * @see #setVisibleAmount
  797. * @see #setValue
  798. */
  799. public void setValues(int value, int visible, int minimum, int maximum) {
  800. int oldValue;
  801. synchronized (this) {
  802. if (minimum == Integer.MAX_VALUE) {
  803. minimum = Integer.MAX_VALUE - 1;
  804. }
  805. if (maximum <= minimum) {
  806. maximum = minimum + 1;
  807. }
  808. long maxMinusMin = (long) maximum - (long) minimum;
  809. if (maxMinusMin > Integer.MAX_VALUE) {
  810. maxMinusMin = Integer.MAX_VALUE;
  811. maximum = minimum + (int) maxMinusMin;
  812. }
  813. if (visible > (int) maxMinusMin) {
  814. visible = (int) maxMinusMin;
  815. }
  816. if (visible < 1) {
  817. visible = 1;
  818. }
  819. if (value < minimum) {
  820. value = minimum;
  821. }
  822. if (value > maximum - visible) {
  823. value = maximum - visible;
  824. }
  825. oldValue = this.value;
  826. this.value = value;
  827. this.visibleAmount = visible;
  828. this.minimum = minimum;
  829. this.maximum = maximum;
  830. ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  831. if (peer != null) {
  832. peer.setValues(value, visibleAmount, minimum, maximum);
  833. }
  834. }
  835. if ((oldValue != value) && (accessibleContext != null)) {
  836. accessibleContext.firePropertyChange(
  837. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  838. new Integer(oldValue),
  839. new Integer(value));
  840. }
  841. }
  842. /**
  843. * Returns true if the value is in the process of changing as a
  844. * result of actions being taken by the user.
  845. *
  846. * @return the value of the <code>valueIsAdjusting</code> property
  847. * @see #setValueIsAdjusting
  848. * @since 1.4
  849. */
  850. public boolean getValueIsAdjusting() {
  851. return isAdjusting;
  852. }
  853. /**
  854. * Sets the <code>valueIsAdjusting</code> property.
  855. *
  856. * @param b new adjustment-in-progress status
  857. * @see #getValueIsAdjusting
  858. * @since 1.4
  859. */
  860. public void setValueIsAdjusting(boolean b) {
  861. boolean oldValue;
  862. synchronized (this) {
  863. oldValue = isAdjusting;
  864. isAdjusting = b;
  865. }
  866. if ((oldValue != b) && (accessibleContext != null)) {
  867. accessibleContext.firePropertyChange(
  868. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  869. ((oldValue) ? AccessibleState.BUSY : null),
  870. ((b) ? AccessibleState.BUSY : null));
  871. }
  872. }
  873. /**
  874. * Adds the specified adjustment listener to receive instances of
  875. * <code>AdjustmentEvent</code> from this scroll bar.
  876. * If l is <code>null</code>, no exception is thrown and no
  877. * action is performed.
  878. *
  879. * @param l the adjustment listener
  880. * @see #removeAdjustmentListener
  881. * @see #getAdjustmentListeners
  882. * @see java.awt.event.AdjustmentEvent
  883. * @see java.awt.event.AdjustmentListener
  884. * @since JDK1.1
  885. */
  886. public synchronized void addAdjustmentListener(AdjustmentListener l) {
  887. if (l == null) {
  888. return;
  889. }
  890. adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l);
  891. newEventsOnly = true;
  892. }
  893. /**
  894. * Removes the specified adjustment listener so that it no longer
  895. * receives instances of <code>AdjustmentEvent</code> from this scroll bar.
  896. * If l is <code>null</code>, no exception is thrown and no action
  897. * is performed.
  898. *
  899. * @param l the adjustment listener
  900. * @see #addAdjustmentListener
  901. * @see #getAdjustmentListeners
  902. * @see java.awt.event.AdjustmentEvent
  903. * @see java.awt.event.AdjustmentListener
  904. * @since JDK1.1
  905. */
  906. public synchronized void removeAdjustmentListener(AdjustmentListener l) {
  907. if (l == null) {
  908. return;
  909. }
  910. adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l);
  911. }
  912. /**
  913. * Returns an array of all the adjustment listeners
  914. * registered on this scrollbar.
  915. *
  916. * @return all of this scrollbar's <code>AdjustmentListener</code>s
  917. * or an empty array if no adjustment
  918. * listeners are currently registered
  919. * @see #addAdjustmentListener
  920. * @see #removeAdjustmentListener
  921. * @see java.awt.event.AdjustmentEvent
  922. * @see java.awt.event.AdjustmentListener
  923. * @since 1.4
  924. */
  925. public synchronized AdjustmentListener[] getAdjustmentListeners() {
  926. return (AdjustmentListener[])(getListeners(AdjustmentListener.class));
  927. }
  928. /**
  929. * Returns an array of all the objects currently registered
  930. * as <code><em>Foo</em>Listener</code>s
  931. * upon this <code>Scrollbar</code>.
  932. * <code><em>Foo</em>Listener</code>s are registered using the
  933. * <code>add<em>Foo</em>Listener</code> method.
  934. * <p>
  935. * You can specify the <code>listenerType</code> argument
  936. * with a class literal, such as
  937. * <code><em>Foo</em>Listener.class</code>.
  938. * For example, you can query a
  939. * <code>Scrollbar</code> <code>c</code>
  940. * for its mouse listeners with the following code:
  941. *
  942. * <pre>MouseListener[] mls = (MouseListener[])(c.getListeners(MouseListener.class));</pre>
  943. *
  944. * If no such listeners exist, this method returns an empty array.
  945. *
  946. * @param listenerType the type of listeners requested; this parameter
  947. * should specify an interface that descends from
  948. * <code>java.util.EventListener</code>
  949. * @return an array of all objects registered as
  950. * <code><em>Foo</em>Listener</code>s on this component,
  951. * or an empty array if no such listeners have been added
  952. * @exception ClassCastException if <code>listenerType</code>
  953. * doesn't specify a class or interface that implements
  954. * <code>java.util.EventListener</code>
  955. *
  956. * @since 1.3
  957. */
  958. public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
  959. EventListener l = null;
  960. if (listenerType == AdjustmentListener.class) {
  961. l = adjustmentListener;
  962. } else {
  963. return super.getListeners(listenerType);
  964. }
  965. return AWTEventMulticaster.getListeners(l, listenerType);
  966. }
  967. // REMIND: remove when filtering is done at lower level
  968. boolean eventEnabled(AWTEvent e) {
  969. if (e.id == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) {
  970. if ((eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0 ||
  971. adjustmentListener != null) {
  972. return true;
  973. }
  974. return false;
  975. }
  976. return super.eventEnabled(e);
  977. }
  978. /**
  979. * Processes events on this scroll bar. If the event is an
  980. * instance of <code>AdjustmentEvent</code>, it invokes the
  981. * <code>processAdjustmentEvent</code> method.
  982. * Otherwise, it invokes its superclass's
  983. * <code>processEvent</code> method.
  984. * <p>Note that if the event parameter is <code>null</code>
  985. * the behavior is unspecified and may result in an
  986. * exception.
  987. *
  988. * @param e the event
  989. * @see java.awt.event.AdjustmentEvent
  990. * @see java.awt.Scrollbar#processAdjustmentEvent
  991. * @since JDK1.1
  992. */
  993. protected void processEvent(AWTEvent e) {
  994. if (e instanceof AdjustmentEvent) {
  995. processAdjustmentEvent((AdjustmentEvent)e);
  996. return;
  997. }
  998. super.processEvent(e);
  999. }
  1000. /**
  1001. * Processes adjustment events occurring on this
  1002. * scrollbar by dispatching them to any registered
  1003. * <code>AdjustmentListener</code> objects.
  1004. * <p>
  1005. * This method is not called unless adjustment events are
  1006. * enabled for this component. Adjustment events are enabled
  1007. * when one of the following occurs:
  1008. * <p><ul>
  1009. * <li>An <code>AdjustmentListener</code> object is registered
  1010. * via <code>addAdjustmentListener</code>.
  1011. * <li>Adjustment events are enabled via <code>enableEvents</code>.
  1012. * </ul><p>
  1013. * <p>Note that if the event parameter is <code>null</code>
  1014. * the behavior is unspecified and may result in an
  1015. * exception.
  1016. *
  1017. * @param e the adjustment event
  1018. * @see java.awt.event.AdjustmentEvent
  1019. * @see java.awt.event.AdjustmentListener
  1020. * @see java.awt.Scrollbar#addAdjustmentListener
  1021. * @see java.awt.Component#enableEvents
  1022. * @since JDK1.1
  1023. */
  1024. protected void processAdjustmentEvent(AdjustmentEvent e) {
  1025. AdjustmentListener listener = adjustmentListener;
  1026. if (listener != null) {
  1027. listener.adjustmentValueChanged(e);
  1028. }
  1029. }
  1030. /**
  1031. * Returns a string representing the state of this <code>Scrollbar</code>.
  1032. * This method is intended to be used only for debugging purposes, and the
  1033. * content and format of the returned string may vary between
  1034. * implementations. The returned string may be empty but may not be
  1035. * <code>null</code>.
  1036. *
  1037. * @return the parameter string of this scroll bar
  1038. */
  1039. protected String paramString() {
  1040. return super.paramString() +
  1041. ",val=" + value +
  1042. ",vis=" + visibleAmount +
  1043. ",min=" + minimum +
  1044. ",max=" + maximum +
  1045. ((orientation == VERTICAL) ? ",vert" : ",horz") +
  1046. ",isAdjusting=" + isAdjusting;
  1047. }
  1048. /* Serialization support.
  1049. */
  1050. /**
  1051. * The scroll bar's serialized Data Version.
  1052. *
  1053. * @serial
  1054. */
  1055. private int scrollbarSerializedDataVersion = 1;
  1056. /**
  1057. * Writes default serializable fields to stream. Writes
  1058. * a list of serializable <code>AdjustmentListeners</code>
  1059. * as optional data. The non-serializable listeners are
  1060. * detected and no attempt is made to serialize them.
  1061. *
  1062. * @param s the <code>ObjectOutputStream</code> to write
  1063. * @serialData <code>null</code> terminated sequence of 0
  1064. * or more pairs; the pair consists of a <code>String</code>
  1065. * and an <code>Object</code> the <code>String</code> indicates
  1066. * the type of object and is one of the following:
  1067. * <code>adjustmentListenerK</code> indicating an
  1068. * <code>AdjustmentListener</code> object
  1069. *
  1070. * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
  1071. * @see java.awt.Component#adjustmentListenerK
  1072. * @see #readObject(ObjectInputStream)
  1073. */
  1074. private void writeObject(ObjectOutputStream s)
  1075. throws IOException
  1076. {
  1077. s.defaultWriteObject();
  1078. AWTEventMulticaster.save(s, adjustmentListenerK, adjustmentListener);
  1079. s.writeObject(null);
  1080. }
  1081. /**
  1082. * Reads the <code>ObjectInputStream</code> and if
  1083. * it isn't <code>null</code> adds a listener to
  1084. * receive adjustment events fired by the
  1085. * <code>Scrollbar</code>.
  1086. * Unrecognized keys or values will be ignored.
  1087. *
  1088. * @param s the <code>ObjectInputStream</code> to read
  1089. * @exception HeadlessException if
  1090. * <code>GraphicsEnvironment.isHeadless</code> returns
  1091. * <code>true</code>
  1092. * @see java.awt.GraphicsEnvironment#isHeadless
  1093. * @see #writeObject(ObjectOutputStream)
  1094. */
  1095. private void readObject(ObjectInputStream s)
  1096. throws ClassNotFoundException, IOException, HeadlessException
  1097. {
  1098. GraphicsEnvironment.checkHeadless();
  1099. s.defaultReadObject();
  1100. Object keyOrNull;
  1101. while(null != (keyOrNull = s.readObject())) {
  1102. String key = ((String)keyOrNull).intern();
  1103. if (adjustmentListenerK == key)
  1104. addAdjustmentListener((AdjustmentListener)(s.readObject()));
  1105. else // skip value for unrecognized key
  1106. s.readObject();
  1107. }
  1108. }
  1109. /////////////////
  1110. // Accessibility support
  1111. ////////////////
  1112. /**
  1113. * Gets the <code>AccessibleContext</code> associated with this
  1114. * <code>Scrollbar</code>. For scrollbars, the
  1115. * <code>AccessibleContext</code> takes the form of an
  1116. * <code>AccessibleAWTScrollBar</code>. A new
  1117. * <code>AccessibleAWTScrollBar</code> instance is created if necessary.
  1118. *
  1119. * @return an <code>AccessibleAWTScrollBar</code> that serves as the
  1120. * <code>AccessibleContext</code> of this <code>ScrollBar</code>
  1121. */
  1122. public AccessibleContext getAccessibleContext() {
  1123. if (accessibleContext == null) {
  1124. accessibleContext = new AccessibleAWTScrollBar();
  1125. }
  1126. return accessibleContext;
  1127. }
  1128. /**
  1129. * This class implements accessibility support for the
  1130. * <code>Scrollbar</code> class. It provides an implementation of
  1131. * the Java Accessibility API appropriate to scrollbar
  1132. * user-interface elements.
  1133. */
  1134. protected class AccessibleAWTScrollBar extends AccessibleAWTComponent
  1135. implements AccessibleValue
  1136. {
  1137. /*
  1138. * JDK 1.3 serialVersionUID
  1139. */
  1140. private static final long serialVersionUID = -344337268523697807L;
  1141. /**
  1142. * Get the state set of this object.
  1143. *
  1144. * @return an instance of <code>AccessibleState</code>
  1145. * containing the current state of the object
  1146. * @see AccessibleState
  1147. */
  1148. public AccessibleStateSet getAccessibleStateSet() {
  1149. AccessibleStateSet states = super.getAccessibleStateSet();
  1150. if (getValueIsAdjusting()) {
  1151. states.add(AccessibleState.BUSY);
  1152. }
  1153. if (getOrientation() == VERTICAL) {
  1154. states.add(AccessibleState.VERTICAL);
  1155. } else {
  1156. states.add(AccessibleState.HORIZONTAL);
  1157. }
  1158. return states;
  1159. }
  1160. /**
  1161. * Get the role of this object.
  1162. *
  1163. * @return an instance of <code>AccessibleRole</code>
  1164. * describing the role of the object
  1165. */
  1166. public AccessibleRole getAccessibleRole() {
  1167. return AccessibleRole.SCROLL_BAR;
  1168. }
  1169. /**
  1170. * Get the <code>AccessibleValue</code> associated with this
  1171. * object. In the implementation of the Java Accessibility
  1172. * API for this class, return this object, which is
  1173. * responsible for implementing the
  1174. * <code>AccessibleValue</code> interface on behalf of itself.
  1175. *
  1176. * @return this object
  1177. */
  1178. public AccessibleValue getAccessibleValue() {
  1179. return this;
  1180. }
  1181. /**
  1182. * Get the accessible value of this object.
  1183. *
  1184. * @return The current value of this object.
  1185. */
  1186. public Number getCurrentAccessibleValue() {
  1187. return new Integer(getValue());
  1188. }
  1189. /**
  1190. * Set the value of this object as a Number.
  1191. *
  1192. * @return True if the value was set.
  1193. */
  1194. public boolean setCurrentAccessibleValue(Number n) {
  1195. if (n instanceof Integer) {
  1196. setValue(n.intValue());
  1197. return true;
  1198. } else {
  1199. return false;
  1200. }
  1201. }
  1202. /**
  1203. * Get the minimum accessible value of this object.
  1204. *
  1205. * @return The minimum value of this object.
  1206. */
  1207. public Number getMinimumAccessibleValue() {
  1208. return new Integer(getMinimum());
  1209. }
  1210. /**
  1211. * Get the maximum accessible value of this object.
  1212. *
  1213. * @return The maximum value of this object.
  1214. */
  1215. public Number getMaximumAccessibleValue() {
  1216. return new Integer(getMaximum());
  1217. }
  1218. } // AccessibleAWTScrollBar
  1219. }