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