1. /*
  2. * @(#)Scrollbar.java 1.98 03/01/23
  3. *
  4. * Copyright 2003 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" or "thumb" can represent the amount of text
  37. * that is visible. Here is an example of a scroll bar that
  38. * 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 is the <em>visible</em>
  45. * range of the scroll bar. The horizontal scroll bar in this
  46. * 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</code>.
  55. * In the previous example, because the <code>maximum</code> is
  56. * 300 and the <code>visible</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 event 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. *
  133. * @version 1.98, 01/23/03
  134. * @author Sami Shaio
  135. * @see java.awt.event.AdjustmentEvent
  136. * @see java.awt.event.AdjustmentListener
  137. * @since JDK1.0
  138. */
  139. public class Scrollbar extends Component implements Adjustable, Accessible {
  140. /**
  141. * A constant that indicates a horizontal scroll bar.
  142. */
  143. public static final int HORIZONTAL = 0;
  144. /**
  145. * A constant that indicates a vertical scroll bar.
  146. */
  147. public static final int VERTICAL = 1;
  148. /**
  149. * The value of the <code>Scrollbar</code>.
  150. * This property must be greater than or equal to <code>minimum</code>
  151. * and less than or equal to
  152. * <code>maximum</code> - <code>visibleAmount</code>
  153. *
  154. * @serial
  155. * @see #getValue
  156. * @see #setValue
  157. */
  158. int value;
  159. /**
  160. * The maximum value of the <code>Scrollbar</code>.
  161. * This value must be greater than the <code>minimum</code>
  162. * value.<br>
  163. * This integer can be either positive or negative, and
  164. * its range can be altered at any time.
  165. *
  166. * @serial
  167. * @see #getMaximum
  168. * @see #setMaximum
  169. */
  170. int maximum;
  171. /**
  172. * The minimum value of the <code>Scrollbar</code>.
  173. * This value must be less than the <code>maximum</code>
  174. * value.<br>
  175. * This integer can be either positive or negative.
  176. *
  177. * @serial
  178. * @see #getMinimum
  179. * @see #setMinimum
  180. */
  181. int minimum;
  182. /**
  183. * The size of the visible portion of the <code>Scrollbar</code>.
  184. * This parameter is normally used to indicate the range of
  185. * values that are currently visible.
  186. *
  187. * @serial
  188. * @see #getVisibleAmount
  189. * @see #setVisibleAmount
  190. */
  191. int visibleAmount;
  192. /**
  193. * The <code>Scrollbar</code>'s orientation--being either horizontal
  194. * or vertical.
  195. * This value should be specified when the scrollbar is created.<BR>
  196. * orientation can be either : <code>VERTICAL</code> or
  197. * <code>HORIZONTAL</code> only.
  198. *
  199. * @serial
  200. * @see #getOrientation
  201. * @see #setOrientation
  202. */
  203. int orientation;
  204. /**
  205. * The amount by which the scrollbar value will change when going
  206. * up or down by a line.
  207. * This value should be a non negative integer.
  208. *
  209. * @serial
  210. * @see #getLineIncrement
  211. * @see #setLineIncrement
  212. */
  213. int lineIncrement = 1;
  214. /**
  215. * The amount by which the scrollbar value will change when going
  216. * up or down by a page.
  217. * This value should be a non negative integer.
  218. *
  219. * @serial
  220. * @see #getPageIncrement
  221. * @see #setPageIncrement
  222. */
  223. int pageIncrement = 10;
  224. /**
  225. * The adjusting status of the <code>Scrollbar</code>.
  226. * True if the value is in the process of changing as a result of
  227. * actions being taken by the user.
  228. *
  229. * @see #getValueIsAdjusting
  230. * @see #setValueIsAdjusting
  231. * @since 1.4
  232. */
  233. transient boolean isAdjusting;
  234. transient AdjustmentListener adjustmentListener;
  235. private static final String base = "scrollbar";
  236. private static int nameCounter = 0;
  237. /*
  238. * JDK 1.1 serialVersionUID
  239. */
  240. private static final long serialVersionUID = 8451667562882310543L;
  241. /**
  242. * Initialize JNI field and method IDs.
  243. */
  244. private static native void initIDs();
  245. static {
  246. /* ensure that the necessary native libraries are loaded */
  247. Toolkit.loadLibraries();
  248. if (!GraphicsEnvironment.isHeadless()) {
  249. initIDs();
  250. }
  251. }
  252. /**
  253. * Constructs a new vertical scroll bar.
  254. * The default properties of the scroll bar are listed in
  255. * the following table:
  256. * <p> </p>
  257. * <table border=1 summary="Scrollbar default properties">
  258. * <tr>
  259. * <th>Property</th>
  260. * <th>Description</th>
  261. * <th>Default Value</th>
  262. * </tr>
  263. * <tr>
  264. * <td>orientation</td>
  265. * <td>indicates if the scroll bar is vertical or horizontal</td>
  266. * <td><code>Scrollbar.VERTICAL</code></td>
  267. * </tr>
  268. * <tr>
  269. * <td>value</td>
  270. * <td>value which controls the location
  271. * <br>of the scroll bar bubble</td>
  272. * <td>0</td>
  273. * </tr>
  274. * <tr>
  275. * <td>minimum</td>
  276. * <td>minimum value of the scroll bar</td>
  277. * <td>0</td>
  278. * </tr>
  279. * <tr>
  280. * <td>maximum</td>
  281. * <td>maximum value of the scroll bar</td>
  282. * <td>100</td>
  283. * </tr>
  284. * <tr>
  285. * <td>unit increment</td>
  286. * <td>amount the value changes when the
  287. * <br>Line Up or Line Down key is pressed,
  288. * <br>or when the end arrows of the scrollbar
  289. * <br>are clicked </td>
  290. * <td>1</td>
  291. * </tr>
  292. * <tr>
  293. * <td>block increment</td>
  294. * <td>amount the value changes when the
  295. * <br>Page Up or Page Down key is pressed,
  296. * <br>or when the scrollbar track is clicked
  297. * <br>on either side of the bubble </td>
  298. * <td>10</td>
  299. * </tr>
  300. * </table>
  301. *
  302. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  303. * returns true.
  304. * @see java.awt.GraphicsEnvironment#isHeadless
  305. */
  306. public Scrollbar() throws HeadlessException {
  307. this(VERTICAL, 0, 10, 0, 100);
  308. }
  309. /**
  310. * Constructs a new scroll bar with the specified orientation.
  311. * <p>
  312. * The <code>orientation</code> argument must take one of the two
  313. * values <code>Scrollbar.HORIZONTAL</code>,
  314. * or <code>Scrollbar.VERTICAL</code>,
  315. * indicating a horizontal or vertical scroll bar, respectively.
  316. * @param orientation indicates the orientation of the scroll bar
  317. * @exception IllegalArgumentException when an illegal value for
  318. * the <code>orientation</code> argument is supplied
  319. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  320. * returns true.
  321. * @see java.awt.GraphicsEnvironment#isHeadless
  322. */
  323. public Scrollbar(int orientation) throws HeadlessException {
  324. this(orientation, 0, 10, 0, 100);
  325. }
  326. /**
  327. * Constructs a new scroll bar with the specified orientation,
  328. * initial value, visible amount, and minimum and maximum values.
  329. * <p>
  330. * The <code>orientation</code> argument must take one of the two
  331. * values <code>Scrollbar.HORIZONTAL</code>,
  332. * or <code>Scrollbar.VERTICAL</code>,
  333. * indicating a horizontal or vertical scroll bar, respectively.
  334. * <p>
  335. * The parameters supplied to this constructor are subject to the
  336. * constraints described in {@link #setValues(int, int, int, int)}.
  337. *
  338. * @param orientation indicates the orientation of the scroll bar.
  339. * @param value the initial value of the scroll bar
  340. * @param visible the size of the scroll bar's bubble, representing
  341. * the visible portion.
  342. * @param minimum the minimum value of the scroll bar
  343. * @param maximum the maximum value of the scroll bar
  344. * @exception IllegalArgumentException when an illegal value for
  345. * the <code>orientation</code> argument is supplied
  346. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  347. * returns true.
  348. * @see #setValues
  349. * @see java.awt.GraphicsEnvironment#isHeadless
  350. */
  351. public Scrollbar(int orientation, int value, int visible, int minimum,
  352. int maximum) throws HeadlessException {
  353. GraphicsEnvironment.checkHeadless();
  354. switch (orientation) {
  355. case HORIZONTAL:
  356. case VERTICAL:
  357. this.orientation = orientation;
  358. break;
  359. default:
  360. throw new IllegalArgumentException("illegal scrollbar orientation");
  361. }
  362. setValues(value, visible, minimum, maximum);
  363. }
  364. /**
  365. * Constructs a name for this component. Called by <code>getName</code>
  366. * when the name is <code>null</code>.
  367. */
  368. String constructComponentName() {
  369. synchronized (getClass()) {
  370. return base + nameCounter++;
  371. }
  372. }
  373. /**
  374. * Creates the <code>Scrollbar</code>'s peer. The peer allows you to modify
  375. * the appearance of the <code>Scrollbar</code> without changing any of its
  376. * functionality.
  377. */
  378. public void addNotify() {
  379. synchronized (getTreeLock()) {
  380. if (peer == null)
  381. peer = getToolkit().createScrollbar(this);
  382. super.addNotify();
  383. }
  384. }
  385. /**
  386. * Returns the orientation of this scroll bar.
  387. * @return the orientation of this scroll bar, either
  388. * <code>Scrollbar.HORIZONTAL</code> or
  389. * <code>Scrollbar.VERTICAL</code>
  390. * @see java.awt.Scrollbar#setOrientation
  391. */
  392. public int getOrientation() {
  393. return orientation;
  394. }
  395. /**
  396. * Sets the orientation for this scroll bar.
  397. * @param orientation the orientation of this scroll bar, either
  398. * <code>Scrollbar.HORIZONTAL</code> or
  399. * <code>Scrollbar.VERTICAL</code>
  400. * @see java.awt.Scrollbar#getOrientation
  401. * @exception IllegalArgumentException if the value supplied
  402. * for <code>orientation</code> is not a
  403. * legal value
  404. * @since JDK1.1
  405. */
  406. public void setOrientation(int orientation) {
  407. synchronized (getTreeLock()) {
  408. if (orientation == this.orientation) {
  409. return;
  410. }
  411. switch (orientation) {
  412. case HORIZONTAL:
  413. case VERTICAL:
  414. this.orientation = orientation;
  415. break;
  416. default:
  417. throw new IllegalArgumentException("illegal scrollbar orientation");
  418. }
  419. /* Create a new peer with the specified orientation. */
  420. if (peer != null) {
  421. removeNotify();
  422. addNotify();
  423. invalidate();
  424. }
  425. }
  426. if (accessibleContext != null) {
  427. accessibleContext.firePropertyChange(
  428. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  429. ((orientation == VERTICAL)
  430. ? AccessibleState.HORIZONTAL : AccessibleState.VERTICAL),
  431. ((orientation == VERTICAL)
  432. ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
  433. }
  434. }
  435. /**
  436. * Gets the current value of this scroll bar.
  437. * @return the current value of this scroll bar
  438. * @see java.awt.Scrollbar#getMinimum
  439. * @see java.awt.Scrollbar#getMaximum
  440. */
  441. public int getValue() {
  442. return value;
  443. }
  444. /**
  445. * Sets the value of this scroll bar to the specified value.
  446. * <p>
  447. * If the value supplied is less than the current <code>minimum</code>
  448. * or greater than the current
  449. * <code>maximum</code> - <code>visibleAmount</code>, then one of
  450. * those values is substituted, as appropriate.
  451. * <p>
  452. * Normally, a program should change a scroll bar's
  453. * value only by calling <code>setValues</code>.
  454. * The <code>setValues</code> method simultaneously
  455. * and synchronously sets the minimum, maximum, visible amount,
  456. * and value properties of a scroll bar, so that they are
  457. * mutually consistent.
  458. * @param newValue the new value of the scroll bar
  459. * @see java.awt.Scrollbar#setValues
  460. * @see java.awt.Scrollbar#getValue
  461. * @see java.awt.Scrollbar#getMinimum
  462. * @see java.awt.Scrollbar#getMaximum
  463. */
  464. public void setValue(int newValue) {
  465. /* Use setValues so that a consistent policy
  466. * relating minimum, maximum, and value is enforced.
  467. */
  468. setValues(newValue, visibleAmount, minimum, maximum);
  469. }
  470. /**
  471. * Gets the minimum value of this scroll bar.
  472. * @return the minimum value of this scroll bar
  473. * @see java.awt.Scrollbar#getValue
  474. * @see java.awt.Scrollbar#getMaximum
  475. */
  476. public int getMinimum() {
  477. return minimum;
  478. }
  479. /**
  480. * Sets the minimum value of this scroll bar.
  481. * <p>
  482. * When <code>setMinimum</code> is called, the limiting argument takes
  483. * precedence over the existing scrollbar value.
  484. * <p>
  485. * Normally, a program should change a scroll bar's minimum
  486. * value only by calling <code>setValues</code>.
  487. * The <code>setValues</code> method simultaneously
  488. * and synchronously sets the minimum, maximum, visible amount,
  489. * and value properties of a scroll bar, so that they are
  490. * mutually consistent.
  491. * <p>Note that setting the minimum value to <code>Integer.MIN_VALUE</code>
  492. * will result in the new minimum value to be set to
  493. * <code>Integer.MIN_VALUE - 1</code>.
  494. *
  495. * @param newMinimum the new minimum value for this scroll bar
  496. * @see java.awt.Scrollbar#setValues
  497. * @see java.awt.Scrollbar#setMaximum
  498. * @since JDK1.1
  499. */
  500. public void setMinimum(int newMinimum) {
  501. /* Use setValues so that a consistent policy
  502. * relating minimum, maximum, and value is enforced.
  503. */
  504. setValues(value, visibleAmount, newMinimum, maximum);
  505. }
  506. /**
  507. * Gets the maximum value of this scroll bar.
  508. * @return the maximum value of this scroll bar
  509. * @see java.awt.Scrollbar#getValue
  510. * @see java.awt.Scrollbar#getMinimum
  511. */
  512. public int getMaximum() {
  513. return maximum;
  514. }
  515. /**
  516. * Sets the maximum value of this scroll bar.
  517. * <p>
  518. * When <code>setMaximum</code> is called, the limiting
  519. * argument takes precedence over the existing scrollbar
  520. * value.
  521. * <p>
  522. * Normally, a program should change a scroll bar's maximum
  523. * value only by calling <code>setValues</code>.
  524. * The <code>setValues</code> method simultaneously
  525. * and synchronously sets the minimum, maximum, visible amount,
  526. * and value properties of a scroll bar, so that they are
  527. * mutually consistent.
  528. * @param newMaximum the new maximum value
  529. * for this scroll bar
  530. * @see java.awt.Scrollbar#setValues
  531. * @see java.awt.Scrollbar#setMinimum
  532. * @since JDK1.1
  533. */
  534. public void setMaximum(int newMaximum) {
  535. /* Use setValues so that a consistent policy
  536. * relating minimum, maximum, and value is enforced.
  537. */
  538. setValues(value, visibleAmount, minimum, newMaximum);
  539. }
  540. /**
  541. * Gets the visible amount of this scroll bar.
  542. * <p>
  543. * The visible amount of a scroll bar is the range of
  544. * values represented by the width of the scroll bar's
  545. * bubble.
  546. * @return the visible amount of this scroll bar
  547. * @see java.awt.Scrollbar#setVisibleAmount
  548. * @since JDK1.1
  549. */
  550. public int getVisibleAmount() {
  551. return getVisible();
  552. }
  553. /**
  554. * @deprecated As of JDK version 1.1,
  555. * replaced by <code>getVisibleAmount()</code>.
  556. */
  557. public int getVisible() {
  558. return visibleAmount;
  559. }
  560. /**
  561. * Sets the visible amount of this scroll bar.
  562. * <p>
  563. * The visible amount of a scroll bar is the range of
  564. * values represented by the width of the scroll bar's
  565. * bubble.
  566. * <p>
  567. * Normally, a program should change a scroll bar's
  568. * value only by calling <code>setValues</code>.
  569. * The <code>setValues</code> method simultaneously
  570. * and synchronously sets the minimum, maximum, visible amount,
  571. * and value properties of a scroll bar, so that they are
  572. * mutually consistent.
  573. * @param newAmount the amount visible per page
  574. * @see java.awt.Scrollbar#getVisibleAmount
  575. * @see java.awt.Scrollbar#setValues
  576. * @since JDK1.1
  577. */
  578. public void setVisibleAmount(int newAmount) {
  579. setValues(value, newAmount, minimum, maximum);
  580. }
  581. /**
  582. * Sets the unit increment for this scroll bar.
  583. * <p>
  584. * The unit increment is the value that is added (subtracted)
  585. * when the user activates the unit increment area of the
  586. * scroll bar, generally through a mouse or keyboard gesture
  587. * that the scroll bar receives as an adjustment event.
  588. * @param v the amount by which to increment or decrement
  589. * the scroll bar's value
  590. * @see java.awt.Scrollbar#getUnitIncrement
  591. * @since JDK1.1
  592. */
  593. public void setUnitIncrement(int v) {
  594. setLineIncrement(v);
  595. }
  596. /**
  597. * @deprecated As of JDK version 1.1,
  598. * replaced by <code>setUnitIncrement(int)</code>.
  599. */
  600. public synchronized void setLineIncrement(int v) {
  601. lineIncrement = v;
  602. ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  603. if (peer != null) {
  604. peer.setLineIncrement(v);
  605. }
  606. }
  607. /**
  608. * Gets the unit increment for this scrollbar.
  609. * <p>
  610. * The unit increment is the value that is added (subtracted)
  611. * when the user activates the unit increment area of the
  612. * scroll bar, generally through a mouse or keyboard gesture
  613. * that the scroll bar receives as an adjustment event.
  614. * @return the unit increment of this scroll bar
  615. * @see java.awt.Scrollbar#setUnitIncrement
  616. * @since JDK1.1
  617. */
  618. public int getUnitIncrement() {
  619. return getLineIncrement();
  620. }
  621. /**
  622. * @deprecated As of JDK version 1.1,
  623. * replaced by <code>getUnitIncrement()</code>.
  624. */
  625. public int getLineIncrement() {
  626. return lineIncrement;
  627. }
  628. /**
  629. * Sets the block increment for this scroll bar.
  630. * <p>
  631. * The block increment is the value that is added (subtracted)
  632. * when the user activates the block increment area of the
  633. * scroll bar, generally through a mouse or keyboard gesture
  634. * that the scroll bar receives as an adjustment event.
  635. * @param v the amount by which to increment or decrement
  636. * the scroll bar's value
  637. * @see java.awt.Scrollbar#getBlockIncrement
  638. * @since JDK1.1
  639. */
  640. public void setBlockIncrement(int v) {
  641. setPageIncrement(v);
  642. }
  643. /**
  644. * @deprecated As of JDK version 1.1,
  645. * replaced by <code>setBlockIncrement()</code>.
  646. */
  647. public synchronized void setPageIncrement(int v) {
  648. pageIncrement = v;
  649. ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  650. if (peer != null) {
  651. peer.setPageIncrement(v);
  652. }
  653. }
  654. /**
  655. * Gets the block increment of this scroll bar.
  656. * <p>
  657. * The block increment is the value that is added (subtracted)
  658. * when the user activates the block increment area of the
  659. * scroll bar, generally through a mouse or keyboard gesture
  660. * that the scroll bar receives as an adjustment event.
  661. * @return the block increment of this scroll bar
  662. * @see java.awt.Scrollbar#setBlockIncrement
  663. * @since JDK1.1
  664. */
  665. public int getBlockIncrement() {
  666. return getPageIncrement();
  667. }
  668. /**
  669. * @deprecated As of JDK version 1.1,
  670. * replaced by <code>getBlockIncrement()</code>.
  671. */
  672. public int getPageIncrement() {
  673. return pageIncrement;
  674. }
  675. /**
  676. * Sets the values of four properties for this scroll bar:
  677. * <code>value</code>, <code>visibleAmount</code>,
  678. * <code>minimum</code>, and <code>maximum</code>.
  679. * If the values supplied for these properties are inconsistent
  680. * or incorrect, they will be changed to ensure consistency.
  681. * <p>
  682. * This method simultaneously and synchronously sets the values
  683. * of four scroll bar properties, assuring that the values of
  684. * these properties are mutually consistent. It enforces the
  685. * following constraints:
  686. * <code>maximum</code> must be greater than <code>minimum</code>,
  687. * <code>visibleAmount</code> must be positive,
  688. * <code>visibleAmount</code> must not be greater than
  689. * <code>maximum</code> - <code>minimum</code>,
  690. * <code>value</code> must not be less than <code>minimum</code>,
  691. * and <code>value</code> must not be greater than
  692. * <code>maximum</code> - <code>visibleAmount</code>
  693. *
  694. * @param value is the position in the current window
  695. * @param visible is the amount visible per page
  696. * @param minimum is the minimum value of the scroll bar
  697. * @param maximum is the maximum value of the scroll bar
  698. * @see #setMinimum
  699. * @see #setMaximum
  700. * @see #setVisibleAmount
  701. * @see #setValue
  702. */
  703. public void setValues(int value, int visible, int minimum, int maximum) {
  704. int oldValue;
  705. synchronized (this) {
  706. if (minimum == Integer.MAX_VALUE) {
  707. minimum = Integer.MAX_VALUE - 1;
  708. }
  709. if (maximum <= minimum) {
  710. maximum = minimum + 1;
  711. }
  712. long maxMinusMin = (long) maximum - (long) minimum;
  713. if (maxMinusMin > Integer.MAX_VALUE) {
  714. maxMinusMin = Integer.MAX_VALUE;
  715. }
  716. if (visible > (int) maxMinusMin) {
  717. visible = (int) maxMinusMin;
  718. }
  719. if (visible < 1) {
  720. visible = 1;
  721. }
  722. if (value < minimum) {
  723. value = minimum;
  724. }
  725. if (value > maximum - visible) {
  726. value = maximum - visible;
  727. }
  728. oldValue = this.value;
  729. this.value = value;
  730. this.visibleAmount = visible;
  731. this.minimum = minimum;
  732. this.maximum = maximum;
  733. ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  734. if (peer != null) {
  735. peer.setValues(value, visibleAmount, minimum, maximum);
  736. }
  737. }
  738. if ((oldValue != value) && (accessibleContext != null)) {
  739. accessibleContext.firePropertyChange(
  740. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  741. new Integer(oldValue),
  742. new Integer(value));
  743. }
  744. }
  745. /**
  746. * Returns true if the value is in the process of changing as a
  747. * result of actions being taken by the user.
  748. *
  749. * @return the value of the <code>valueIsAdjusting</code> property
  750. * @see #setValueIsAdjusting
  751. * @since 1.4
  752. */
  753. public boolean getValueIsAdjusting() {
  754. return isAdjusting;
  755. }
  756. /**
  757. * Sets the <code>valueIsAdjusting</code> property.
  758. *
  759. * @param b new adjustment-in-progress status
  760. * @see #getValueIsAdjusting
  761. * @since 1.4
  762. */
  763. public void setValueIsAdjusting(boolean b) {
  764. boolean oldValue;
  765. synchronized (this) {
  766. oldValue = isAdjusting;
  767. isAdjusting = b;
  768. }
  769. if ((oldValue != b) && (accessibleContext != null)) {
  770. accessibleContext.firePropertyChange(
  771. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  772. ((oldValue) ? AccessibleState.BUSY : null),
  773. ((b) ? AccessibleState.BUSY : null));
  774. }
  775. }
  776. /**
  777. * Adds the specified adjustment listener to receive instances of
  778. * <code>AdjustmentEvent</code> from this scroll bar.
  779. * If l is <code>null</code>, no exception is thrown and no
  780. * action is performed.
  781. *
  782. * @param l the adjustment listener
  783. * @see #removeAdjustmentListener
  784. * @see #getAdjustmentListeners
  785. * @see java.awt.event.AdjustmentEvent
  786. * @see java.awt.event.AdjustmentListener
  787. * @since JDK1.1
  788. */
  789. public synchronized void addAdjustmentListener(AdjustmentListener l) {
  790. if (l == null) {
  791. return;
  792. }
  793. adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l);
  794. newEventsOnly = true;
  795. }
  796. /**
  797. * Removes the specified adjustment listener so that it no longer
  798. * receives instances of <code>AdjustmentEvent</code> from this scroll bar.
  799. * If l is <code>null</code>, no exception is thrown and no action
  800. * is performed.
  801. *
  802. * @param l the adjustment listener
  803. * @see #addAdjustmentListener
  804. * @see #getAdjustmentListeners
  805. * @see java.awt.event.AdjustmentEvent
  806. * @see java.awt.event.AdjustmentListener
  807. * @since JDK1.1
  808. */
  809. public synchronized void removeAdjustmentListener(AdjustmentListener l) {
  810. if (l == null) {
  811. return;
  812. }
  813. adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l);
  814. }
  815. /**
  816. * Returns an array of all the adjustment listeners
  817. * registered on this scrollbar.
  818. *
  819. * @return all of this scrollbar's <code>AdjustmentListener</code>s
  820. * or an empty array if no adjustment
  821. * listeners are currently registered
  822. *
  823. * @see #addAdjustmentListener
  824. * @see #removeAdjustmentListener
  825. * @see java.awt.event.AdjustmentEvent
  826. * @see java.awt.event.AdjustmentListener
  827. * @since 1.4
  828. */
  829. public synchronized AdjustmentListener[] getAdjustmentListeners() {
  830. return (AdjustmentListener[])(getListeners(AdjustmentListener.class));
  831. }
  832. /**
  833. * Returns an array of all the objects currently registered
  834. * as <code><em>Foo</em>Listener</code>s
  835. * upon this <code>Scrollbar</code>.
  836. * <code><em>Foo</em>Listener</code>s are registered using the
  837. * <code>add<em>Foo</em>Listener</code> method.
  838. *
  839. * <p>
  840. * You can specify the <code>listenerType</code> argument
  841. * with a class literal, such as
  842. * <code><em>Foo</em>Listener.class</code>.
  843. * For example, you can query a
  844. * <code>Scrollbar</code> <code>c</code>
  845. * for its mouse listeners with the following code:
  846. *
  847. * <pre>MouseListener[] mls = (MouseListener[])(c.getListeners(MouseListener.class));</pre>
  848. *
  849. * If no such listeners exist, this method returns an empty array.
  850. *
  851. * @param listenerType the type of listeners requested; this parameter
  852. * should specify an interface that descends from
  853. * <code>java.util.EventListener</code>
  854. * @return an array of all objects registered as
  855. * <code><em>Foo</em>Listener</code>s on this component,
  856. * or an empty array if no such listeners have been added
  857. * @exception ClassCastException if <code>listenerType</code>
  858. * doesn't specify a class or interface that implements
  859. * <code>java.util.EventListener</code>
  860. *
  861. * @since 1.3
  862. */
  863. public EventListener[] getListeners(Class listenerType) {
  864. EventListener l = null;
  865. if (listenerType == AdjustmentListener.class) {
  866. l = adjustmentListener;
  867. } else {
  868. return super.getListeners(listenerType);
  869. }
  870. return AWTEventMulticaster.getListeners(l, listenerType);
  871. }
  872. // REMIND: remove when filtering is done at lower level
  873. boolean eventEnabled(AWTEvent e) {
  874. if (e.id == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) {
  875. if ((eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0 ||
  876. adjustmentListener != null) {
  877. return true;
  878. }
  879. return false;
  880. }
  881. return super.eventEnabled(e);
  882. }
  883. /**
  884. * Processes events on this scroll bar. If the event is an
  885. * instance of <code>AdjustmentEvent</code>, it invokes the
  886. * <code>processAdjustmentEvent</code> method.
  887. * Otherwise, it invokes its superclass's
  888. * <code>processEvent</code> method.
  889. * <p>Note that if the event parameter is <code>null</code>
  890. * the behavior is unspecified and may result in an
  891. * exception.
  892. *
  893. * @param e the event
  894. * @see java.awt.event.AdjustmentEvent
  895. * @see java.awt.Scrollbar#processAdjustmentEvent
  896. * @since JDK1.1
  897. */
  898. protected void processEvent(AWTEvent e) {
  899. if (e instanceof AdjustmentEvent) {
  900. processAdjustmentEvent((AdjustmentEvent)e);
  901. return;
  902. }
  903. super.processEvent(e);
  904. }
  905. /**
  906. * Processes adjustment events occurring on this
  907. * scrollbar by dispatching them to any registered
  908. * <code>AdjustmentListener</code> objects.
  909. * <p>
  910. * This method is not called unless adjustment events are
  911. * enabled for this component. Adjustment events are enabled
  912. * when one of the following occurs:
  913. * <p><ul>
  914. * <li>An <code>AdjustmentListener</code> object is registered
  915. * via <code>addAdjustmentListener</code>.
  916. * <li>Adjustment events are enabled via <code>enableEvents</code>.
  917. * </ul><p>
  918. * <p>Note that if the event parameter is <code>null</code>
  919. * the behavior is unspecified and may result in an
  920. * exception.
  921. *
  922. * @param e the adjustment event
  923. * @see java.awt.event.AdjustmentEvent
  924. * @see java.awt.event.AdjustmentListener
  925. * @see java.awt.Scrollbar#addAdjustmentListener
  926. * @see java.awt.Component#enableEvents
  927. * @since JDK1.1
  928. */
  929. protected void processAdjustmentEvent(AdjustmentEvent e) {
  930. AdjustmentListener listener = adjustmentListener;
  931. if (listener != null) {
  932. listener.adjustmentValueChanged(e);
  933. }
  934. }
  935. /**
  936. * Returns a string representing the state of this <code>Scrollbar</code>.
  937. * This method is intended to be used only for debugging purposes, and the
  938. * content and format of the returned string may vary between
  939. * implementations. The returned string may be empty but may not be
  940. * <code>null</code>.
  941. *
  942. * @return the parameter string of this scroll bar
  943. */
  944. protected String paramString() {
  945. return super.paramString() +
  946. ",val=" + value +
  947. ",vis=" + visibleAmount +
  948. ",min=" + minimum +
  949. ",max=" + maximum +
  950. ((orientation == VERTICAL) ? ",vert" : ",horz") +
  951. ",isAdjusting=" + isAdjusting;
  952. }
  953. /* Serialization support.
  954. */
  955. /**
  956. * The scrollbars serialized Data Version.
  957. *
  958. * @serial
  959. */
  960. private int scrollbarSerializedDataVersion = 1;
  961. /**
  962. * Writes default serializable fields to stream. Writes
  963. * a list of serializable <code>AdjustmentListeners</code>
  964. * as optional data. The non-serializable listeners are
  965. * detected and no attempt is made to serialize them.
  966. *
  967. * @param s the <code>ObjectOutputStream</code> to write
  968. * @serialData <code>null</code> terminated sequence of 0
  969. * or more pairs; the pair consists of a <code>String</code>
  970. * and an <code>Object</code> the <code>String</code> indicates
  971. * the type of object and is one of the following:
  972. * <code>adjustmentListenerK</code> indicating an
  973. * <code>AdjustmentListener</code> object
  974. *
  975. * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
  976. * @see java.awt.Component#adjustmentListenerK
  977. * @see #readObject(ObjectInputStream)
  978. */
  979. private void writeObject(ObjectOutputStream s)
  980. throws IOException
  981. {
  982. s.defaultWriteObject();
  983. AWTEventMulticaster.save(s, adjustmentListenerK, adjustmentListener);
  984. s.writeObject(null);
  985. }
  986. /**
  987. * Reads the <code>ObjectInputStream</code> and if
  988. * it isn't <code>null</code> adds a listener to
  989. * receive adjustment events fired by the
  990. * <code>Scrollbar</code>.
  991. * Unrecognized keys or values will be ignored.
  992. *
  993. * @param s the <code>ObjectInputStream</code> to read
  994. * @exception HeadlessException if
  995. * <code>GraphicsEnvironment.isHeadless</code> returns
  996. * <code>true</code>
  997. * @see java.awt.GraphicsEnvironment#isHeadless
  998. * @see #writeObject(ObjectOutputStream)
  999. */
  1000. private void readObject(ObjectInputStream s)
  1001. throws ClassNotFoundException, IOException, HeadlessException
  1002. {
  1003. GraphicsEnvironment.checkHeadless();
  1004. s.defaultReadObject();
  1005. Object keyOrNull;
  1006. while(null != (keyOrNull = s.readObject())) {
  1007. String key = ((String)keyOrNull).intern();
  1008. if (adjustmentListenerK == key)
  1009. addAdjustmentListener((AdjustmentListener)(s.readObject()));
  1010. else // skip value for unrecognized key
  1011. s.readObject();
  1012. }
  1013. }
  1014. /////////////////
  1015. // Accessibility support
  1016. ////////////////
  1017. /**
  1018. * Gets the <code>AccessibleContext</code> associated with this
  1019. * <code>Scrollbar</code>. For scrollbars, the
  1020. * <code>AccessibleContext</code> takes the form of an
  1021. * <code>AccessibleAWTScrollBar</code>. A new
  1022. * <code>AccessibleAWTScrollBar</code> instance is created if necessary.
  1023. *
  1024. * @return an <code>AccessibleAWTScrollBar</code> that serves as the
  1025. * <code>AccessibleContext</code> of this <code>ScrollBar</code>
  1026. */
  1027. public AccessibleContext getAccessibleContext() {
  1028. if (accessibleContext == null) {
  1029. accessibleContext = new AccessibleAWTScrollBar();
  1030. }
  1031. return accessibleContext;
  1032. }
  1033. /**
  1034. * This class implements accessibility support for the
  1035. * <code>Scrollbar</code> class. It provides an implementation of
  1036. * the Java Accessibility API appropriate to scrollbar
  1037. * user-interface elements.
  1038. */
  1039. protected class AccessibleAWTScrollBar extends AccessibleAWTComponent
  1040. implements AccessibleValue
  1041. {
  1042. /**
  1043. * Get the state set of this object.
  1044. *
  1045. * @return an instance of <code>AccessibleState</code>
  1046. * containing the current state of the object
  1047. * @see AccessibleState
  1048. */
  1049. public AccessibleStateSet getAccessibleStateSet() {
  1050. AccessibleStateSet states = super.getAccessibleStateSet();
  1051. if (getValueIsAdjusting()) {
  1052. states.add(AccessibleState.BUSY);
  1053. }
  1054. if (getOrientation() == VERTICAL) {
  1055. states.add(AccessibleState.VERTICAL);
  1056. } else {
  1057. states.add(AccessibleState.HORIZONTAL);
  1058. }
  1059. return states;
  1060. }
  1061. /**
  1062. * Get the role of this object.
  1063. *
  1064. * @return an instance of <code>AccessibleRole</code>
  1065. * describing the role of the object
  1066. */
  1067. public AccessibleRole getAccessibleRole() {
  1068. return AccessibleRole.SCROLL_BAR;
  1069. }
  1070. /**
  1071. * Get the <code>AccessibleValue</code> associated with this
  1072. * object. In the implementation of the Java Accessibility
  1073. * API for this class, return this object, which is
  1074. * responsible for implementing the
  1075. * <code>AccessibleValue</code> interface on behalf of itself.
  1076. *
  1077. * @return this object
  1078. */
  1079. public AccessibleValue getAccessibleValue() {
  1080. return this;
  1081. }
  1082. /**
  1083. * Get the accessible value of this object.
  1084. *
  1085. * @return The current value of this object.
  1086. */
  1087. public Number getCurrentAccessibleValue() {
  1088. return new Integer(getValue());
  1089. }
  1090. /**
  1091. * Set the value of this object as a Number.
  1092. *
  1093. * @return True if the value was set.
  1094. */
  1095. public boolean setCurrentAccessibleValue(Number n) {
  1096. if (n instanceof Integer) {
  1097. setValue(n.intValue());
  1098. return true;
  1099. } else {
  1100. return false;
  1101. }
  1102. }
  1103. /**
  1104. * Get the minimum accessible value of this object.
  1105. *
  1106. * @return The minimum value of this object.
  1107. */
  1108. public Number getMinimumAccessibleValue() {
  1109. return new Integer(getMinimum());
  1110. }
  1111. /**
  1112. * Get the maximum accessible value of this object.
  1113. *
  1114. * @return The maximum value of this object.
  1115. */
  1116. public Number getMaximumAccessibleValue() {
  1117. return new Integer(getMaximum());
  1118. }
  1119. } // AccessibleAWTScrollBar
  1120. }