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