1. /*
  2. * @(#)JScrollBar.java 1.74 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 javax.swing;
  8. import java.io.Serializable;
  9. import java.awt.Component;
  10. import java.awt.Adjustable;
  11. import java.awt.Dimension;
  12. import java.awt.event.AdjustmentListener;
  13. import java.awt.event.AdjustmentEvent;
  14. import java.awt.Graphics;
  15. import javax.swing.event.*;
  16. import javax.swing.plaf.*;
  17. import javax.accessibility.*;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21. /**
  22. * An implementation of a scrollbar. The user positions the knob in the
  23. * scrollbar to determine the contents of the viewing area. The
  24. * program typically adjusts the display so that the end of the
  25. * scrollbar represents the end of the displayable contents, or 100%
  26. * of the contents. The start of the scrollbar is the beginning of the
  27. * displayable contents, or 0%. The position of the knob within
  28. * those bounds then translates to the corresponding percentage of
  29. * the displayable contents.
  30. * <p>
  31. * Typically, as the position of the knob in the scrollbar changes
  32. * a corresponding change is made to the position of the JViewport on
  33. * the underlying view, changing the contents of the JViewport.
  34. * <p>
  35. * <strong>Warning:</strong>
  36. * Serialized objects of this class will not be compatible with
  37. * future Swing releases. The current serialization support is
  38. * appropriate for short term storage or RMI between applications running
  39. * the same version of Swing. As of 1.4, support for long term storage
  40. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  41. * has been added to the <code>java.beans</code> package.
  42. * Please see {@link java.beans.XMLEncoder}.
  43. *
  44. * @see JScrollPane
  45. * @beaninfo
  46. * attribute: isContainer false
  47. * description: A component that helps determine the visible content range of an area.
  48. *
  49. * @version 1.74 01/23/03
  50. * @author David Kloba
  51. */
  52. public class JScrollBar extends JComponent implements Adjustable, Accessible
  53. {
  54. /**
  55. * @see #getUIClassID
  56. * @see #readObject
  57. */
  58. private static final String uiClassID = "ScrollBarUI";
  59. /**
  60. * All changes from the model are treated as though the user moved
  61. * the scrollbar knob.
  62. */
  63. private ChangeListener fwdAdjustmentEvents = new ModelListener();
  64. /**
  65. * The model that represents the scrollbar's minimum, maximum, extent
  66. * (aka "visibleAmount") and current value.
  67. * @see #setModel
  68. */
  69. protected BoundedRangeModel model;
  70. /**
  71. * @see #setOrientation
  72. */
  73. protected int orientation;
  74. /**
  75. * @see #setUnitIncrement
  76. */
  77. protected int unitIncrement;
  78. /**
  79. * @see #setBlockIncrement
  80. */
  81. protected int blockIncrement;
  82. private void checkOrientation(int orientation) {
  83. switch (orientation) {
  84. case VERTICAL:
  85. case HORIZONTAL:
  86. break;
  87. default:
  88. throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
  89. }
  90. }
  91. /**
  92. * Creates a scrollbar with the specified orientation,
  93. * value, extent, minimum, and maximum.
  94. * The "extent" is the size of the viewable area. It is also known
  95. * as the "visible amount".
  96. * <p>
  97. * Note: Use <code>setBlockIncrement</code> to set the block
  98. * increment to a size slightly smaller than the view's extent.
  99. * That way, when the user jumps the knob to an adjacent position,
  100. * one or two lines of the original contents remain in view.
  101. *
  102. * @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
  103. *
  104. * @see #setOrientation
  105. * @see #setValue
  106. * @see #setVisibleAmount
  107. * @see #setMinimum
  108. * @see #setMaximum
  109. */
  110. public JScrollBar(int orientation, int value, int extent, int min, int max)
  111. {
  112. checkOrientation(orientation);
  113. this.unitIncrement = 1;
  114. this.blockIncrement = (extent == 0) ? 1 : extent;
  115. this.orientation = orientation;
  116. this.model = new DefaultBoundedRangeModel(value, extent, min, max);
  117. this.model.addChangeListener(fwdAdjustmentEvents);
  118. setRequestFocusEnabled(false);
  119. updateUI();
  120. }
  121. /**
  122. * Creates a scrollbar with the specified orientation
  123. * and the following initial values:
  124. * <pre>
  125. * minimum = 0
  126. * maximum = 100
  127. * value = 0
  128. * extent = 10
  129. * </pre>
  130. */
  131. public JScrollBar(int orientation) {
  132. this(orientation, 0, 10, 0, 100);
  133. }
  134. /**
  135. * Creates a vertical scrollbar with the following initial values:
  136. * <pre>
  137. * minimum = 0
  138. * maximum = 100
  139. * value = 0
  140. * extent = 10
  141. * </pre>
  142. */
  143. public JScrollBar() {
  144. this(VERTICAL);
  145. }
  146. /**
  147. * Sets the L&F object that renders this component.
  148. *
  149. * @param ui the <code>ScrollBarUI</code> L&F object
  150. * @see UIDefaults#getUI
  151. * @since 1.4
  152. * @beaninfo
  153. * bound: true
  154. * hidden: true
  155. * attribute: visualUpdate true
  156. * description: The UI object that implements the Component's LookAndFeel
  157. */
  158. public void setUI(ScrollBarUI ui) {
  159. super.setUI(ui);
  160. }
  161. /**
  162. * Returns the delegate that implements the look and feel for
  163. * this component.
  164. *
  165. * @see JComponent#setUI
  166. */
  167. public ScrollBarUI getUI() {
  168. return (ScrollBarUI)ui;
  169. }
  170. /**
  171. * Overrides <code>JComponent.updateUI</code>.
  172. * @see JComponent#updateUI
  173. */
  174. public void updateUI() {
  175. setUI((ScrollBarUI)UIManager.getUI(this));
  176. }
  177. /**
  178. * Returns the name of the LookAndFeel class for this component.
  179. *
  180. * @return "ScrollBarUI"
  181. * @see JComponent#getUIClassID
  182. * @see UIDefaults#getUI
  183. */
  184. public String getUIClassID() {
  185. return uiClassID;
  186. }
  187. /**
  188. * Returns the component's orientation (horizontal or vertical).
  189. *
  190. * @return VERTICAL or HORIZONTAL
  191. * @see #setOrientation
  192. * @see java.awt.Adjustable#getOrientation
  193. */
  194. public int getOrientation() {
  195. return orientation;
  196. }
  197. /**
  198. * Set the scrollbar's orientation to either VERTICAL or
  199. * HORIZONTAL.
  200. *
  201. * @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
  202. * @see #getOrientation
  203. * @beaninfo
  204. * preferred: true
  205. * bound: true
  206. * attribute: visualUpdate true
  207. * description: The scrollbar's orientation.
  208. * enum: VERTICAL JScrollBar.VERTICAL
  209. * HORIZONTAL JScrollBar.HORIZONTAL
  210. */
  211. public void setOrientation(int orientation)
  212. {
  213. checkOrientation(orientation);
  214. int oldValue = this.orientation;
  215. this.orientation = orientation;
  216. firePropertyChange("orientation", oldValue, orientation);
  217. if ((oldValue != orientation) && (accessibleContext != null)) {
  218. accessibleContext.firePropertyChange(
  219. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  220. ((oldValue == VERTICAL)
  221. ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL),
  222. ((orientation == VERTICAL)
  223. ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
  224. }
  225. if (orientation != oldValue) {
  226. revalidate();
  227. }
  228. }
  229. /**
  230. * Returns data model that handles the scrollbar's four
  231. * fundamental properties: minimum, maximum, value, extent.
  232. *
  233. * @see #setModel
  234. */
  235. public BoundedRangeModel getModel() {
  236. return model;
  237. }
  238. /**
  239. * Sets the model that handles the scrollbar's four
  240. * fundamental properties: minimum, maximum, value, extent.
  241. *
  242. * @see #getModel
  243. * @beaninfo
  244. * bound: true
  245. * expert: true
  246. * description: The scrollbar's BoundedRangeModel.
  247. */
  248. public void setModel(BoundedRangeModel newModel) {
  249. Integer oldValue = null;
  250. BoundedRangeModel oldModel = model;
  251. if (model != null) {
  252. model.removeChangeListener(fwdAdjustmentEvents);
  253. oldValue = new Integer(model.getValue());
  254. }
  255. model = newModel;
  256. if (model != null) {
  257. model.addChangeListener(fwdAdjustmentEvents);
  258. }
  259. firePropertyChange("model", oldModel, model);
  260. if (accessibleContext != null) {
  261. accessibleContext.firePropertyChange(
  262. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  263. oldValue, new Integer(model.getValue()));
  264. }
  265. }
  266. /**
  267. * Returns the amount to change the scrollbar's value by,
  268. * given a unit up/down request. A ScrollBarUI implementation
  269. * typically calls this method when the user clicks on a scrollbar
  270. * up/down arrow and uses the result to update the scrollbar's
  271. * value. Subclasses my override this method to compute
  272. * a value, e.g. the change required to scroll up or down one
  273. * (variable height) line text or one row in a table.
  274. * <p>
  275. * The JScrollPane component creates scrollbars (by default)
  276. * that override this method and delegate to the viewports
  277. * Scrollable view, if it has one. The Scrollable interface
  278. * provides a more specialized version of this method.
  279. *
  280. * @param direction is -1 or 1 for up/down respectively
  281. * @return the value of the unitIncrement property
  282. * @see #setUnitIncrement
  283. * @see #setValue
  284. * @see Scrollable#getScrollableUnitIncrement
  285. */
  286. public int getUnitIncrement(int direction) {
  287. return unitIncrement;
  288. }
  289. /**
  290. * Sets the unitIncrement property.
  291. * @see #getUnitIncrement
  292. * @beaninfo
  293. * preferred: true
  294. * bound: true
  295. * description: The scrollbar's unit increment.
  296. */
  297. public void setUnitIncrement(int unitIncrement) {
  298. int oldValue = this.unitIncrement;
  299. this.unitIncrement = unitIncrement;
  300. firePropertyChange("unitIncrement", oldValue, unitIncrement);
  301. }
  302. /**
  303. * Returns the amount to change the scrollbar's value by,
  304. * given a block (usually "page") up/down request. A ScrollBarUI
  305. * implementation typically calls this method when the user clicks
  306. * above or below the scrollbar "knob" to change the value
  307. * up or down by large amount. Subclasses my override this
  308. * method to compute a value, e.g. the change required to scroll
  309. * up or down one paragraph in a text document.
  310. * <p>
  311. * The JScrollPane component creates scrollbars (by default)
  312. * that override this method and delegate to the viewports
  313. * Scrollable view, if it has one. The Scrollable interface
  314. * provides a more specialized version of this method.
  315. *
  316. * @param direction is -1 or 1 for up/down respectively
  317. * @return the value of the blockIncrement property
  318. * @see #setBlockIncrement
  319. * @see #setValue
  320. * @see Scrollable#getScrollableBlockIncrement
  321. */
  322. public int getBlockIncrement(int direction) {
  323. return blockIncrement;
  324. }
  325. /**
  326. * Sets the blockIncrement property.
  327. * @see #getBlockIncrement()
  328. * @beaninfo
  329. * preferred: true
  330. * bound: true
  331. * description: The scrollbar's block increment.
  332. */
  333. public void setBlockIncrement(int blockIncrement) {
  334. int oldValue = this.blockIncrement;
  335. this.blockIncrement = blockIncrement;
  336. firePropertyChange("blockIncrement", oldValue, blockIncrement);
  337. }
  338. /**
  339. * For backwards compatibility with java.awt.Scrollbar.
  340. * @see Adjustable#getUnitIncrement
  341. * @see #getUnitIncrement(int)
  342. */
  343. public int getUnitIncrement() {
  344. return unitIncrement;
  345. }
  346. /**
  347. * For backwards compatibility with java.awt.Scrollbar.
  348. * @see Adjustable#getBlockIncrement
  349. * @see #getBlockIncrement(int)
  350. */
  351. public int getBlockIncrement() {
  352. return blockIncrement;
  353. }
  354. /**
  355. * Returns the scrollbar's value.
  356. * @return the model's value property
  357. * @see #setValue
  358. */
  359. public int getValue() {
  360. return getModel().getValue();
  361. }
  362. /**
  363. * Sets the scrollbar's value. This method just forwards the value
  364. * to the model.
  365. *
  366. * @see #getValue
  367. * @see BoundedRangeModel#setValue
  368. * @beaninfo
  369. * preferred: true
  370. * description: The scrollbar's current value.
  371. */
  372. public void setValue(int value) {
  373. BoundedRangeModel m = getModel();
  374. int oldValue = m.getValue();
  375. m.setValue(value);
  376. if (accessibleContext != null) {
  377. accessibleContext.firePropertyChange(
  378. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  379. new Integer(oldValue),
  380. new Integer(m.getValue()));
  381. }
  382. }
  383. /**
  384. * Returns the scrollbar's extent, aka its "visibleAmount". In many
  385. * scrollbar look and feel implementations the size of the
  386. * scrollbar "knob" or "thumb" is proportional to the extent.
  387. *
  388. * @return the value of the model's extent property
  389. * @see #setVisibleAmount
  390. */
  391. public int getVisibleAmount() {
  392. return getModel().getExtent();
  393. }
  394. /**
  395. * Set the model's extent property.
  396. *
  397. * @see #getVisibleAmount
  398. * @see BoundedRangeModel#setExtent
  399. * @beaninfo
  400. * preferred: true
  401. * description: The amount of the view that is currently visible.
  402. */
  403. public void setVisibleAmount(int extent) {
  404. getModel().setExtent(extent);
  405. }
  406. /**
  407. * Returns the minimum value supported by the scrollbar
  408. * (usually zero).
  409. *
  410. * @return the value of the model's minimum property
  411. * @see #setMinimum
  412. */
  413. public int getMinimum() {
  414. return getModel().getMinimum();
  415. }
  416. /**
  417. * Sets the model's minimum property.
  418. *
  419. * @see #getMinimum
  420. * @see BoundedRangeModel#setMinimum
  421. * @beaninfo
  422. * preferred: true
  423. * description: The scrollbar's minimum value.
  424. */
  425. public void setMinimum(int minimum) {
  426. getModel().setMinimum(minimum);
  427. }
  428. /**
  429. * The maximum value of the scrollbar is maximum - extent.
  430. *
  431. * @return the value of the model's maximum property
  432. * @see #setMaximum
  433. */
  434. public int getMaximum() {
  435. return getModel().getMaximum();
  436. }
  437. /**
  438. * Sets the model's maximum property. Note that the scrollbar's value
  439. * can only be set to maximum - extent.
  440. *
  441. * @see #getMaximum
  442. * @see BoundedRangeModel#setMaximum
  443. * @beaninfo
  444. * preferred: true
  445. * description: The scrollbar's maximum value.
  446. */
  447. public void setMaximum(int maximum) {
  448. getModel().setMaximum(maximum);
  449. }
  450. /**
  451. * True if the scrollbar knob is being dragged.
  452. *
  453. * @return the value of the model's valueIsAdjusting property
  454. * @see #setValueIsAdjusting
  455. */
  456. public boolean getValueIsAdjusting() {
  457. return getModel().getValueIsAdjusting();
  458. }
  459. /**
  460. * Sets the model's valueIsAdjusting property. Scrollbar look and
  461. * feel implementations should set this property to true when
  462. * a knob drag begins, and to false when the drag ends. The
  463. * scrollbar model will not generate ChangeEvents while
  464. * valueIsAdjusting is true.
  465. *
  466. * @see #getValueIsAdjusting
  467. * @see BoundedRangeModel#setValueIsAdjusting
  468. * @beaninfo
  469. * expert: true
  470. * description: True if the scrollbar thumb is being dragged.
  471. */
  472. public void setValueIsAdjusting(boolean b) {
  473. BoundedRangeModel m = getModel();
  474. boolean oldValue = m.getValueIsAdjusting();
  475. m.setValueIsAdjusting(b);
  476. if ((oldValue != b) && (accessibleContext != null)) {
  477. accessibleContext.firePropertyChange(
  478. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  479. ((oldValue) ? AccessibleState.BUSY : null),
  480. ((b) ? AccessibleState.BUSY : null));
  481. }
  482. }
  483. /**
  484. * Sets the four BoundedRangeModel properties after forcing
  485. * the arguments to obey the usual constraints:
  486. * <pre>
  487. * minimum <= value <= value+extent <= maximum
  488. * </pre>
  489. * <p>
  490. *
  491. * @see BoundedRangeModel#setRangeProperties
  492. * @see #setValue
  493. * @see #setVisibleAmount
  494. * @see #setMinimum
  495. * @see #setMaximum
  496. */
  497. public void setValues(int newValue, int newExtent, int newMin, int newMax)
  498. {
  499. BoundedRangeModel m = getModel();
  500. int oldValue = m.getValue();
  501. m.setRangeProperties(newValue, newExtent, newMin, newMax, m.getValueIsAdjusting());
  502. if (accessibleContext != null) {
  503. accessibleContext.firePropertyChange(
  504. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  505. new Integer(oldValue),
  506. new Integer(m.getValue()));
  507. }
  508. }
  509. /**
  510. * Adds an AdjustmentListener. Adjustment listeners are notified
  511. * each time the scrollbar's model changes. Adjustment events are
  512. * provided for backwards compatibility with java.awt.Scrollbar.
  513. * <p>
  514. * Note that the AdjustmentEvents type property will always have a
  515. * placeholder value of AdjustmentEvent.TRACK because all changes
  516. * to a BoundedRangeModels value are considered equivalent. To change
  517. * the value of a BoundedRangeModel one just sets its value property,
  518. * i.e. model.setValue(123). No information about the origin of the
  519. * change, e.g. it's a block decrement, is provided. We don't try
  520. * fabricate the origin of the change here.
  521. *
  522. * @param l the AdjustmentLister to add
  523. * @see #removeAdjustmentListener
  524. * @see BoundedRangeModel#addChangeListener
  525. */
  526. public void addAdjustmentListener(AdjustmentListener l) {
  527. listenerList.add(AdjustmentListener.class, l);
  528. }
  529. /**
  530. * Removes an AdjustmentEvent listener.
  531. *
  532. * @param l the AdjustmentLister to remove
  533. * @see #addAdjustmentListener
  534. */
  535. public void removeAdjustmentListener(AdjustmentListener l) {
  536. listenerList.remove(AdjustmentListener.class, l);
  537. }
  538. /**
  539. * Returns an array of all the <code>AdjustmentListener</code>s added
  540. * to this JScrollBar with addAdjustmentListener().
  541. *
  542. * @return all of the <code>AdjustmentListener</code>s added or an empty
  543. * array if no listeners have been added
  544. * @since 1.4
  545. */
  546. public AdjustmentListener[] getAdjustmentListeners() {
  547. return (AdjustmentListener[])listenerList.getListeners(
  548. AdjustmentListener.class);
  549. }
  550. /*
  551. * Notify listeners that the scrollbar's model has changed.
  552. *
  553. * @see #addAdjustmentListener
  554. * @see EventListenerList
  555. */
  556. protected void fireAdjustmentValueChanged(int id, int type, int value) {
  557. Object[] listeners = listenerList.getListenerList();
  558. AdjustmentEvent e = null;
  559. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  560. if (listeners[i]==AdjustmentListener.class) {
  561. if (e == null) {
  562. e = new AdjustmentEvent(this, id, type, value);
  563. }
  564. ((AdjustmentListener)listeners[i+1]).adjustmentValueChanged(e);
  565. }
  566. }
  567. }
  568. /**
  569. * This class listens to ChangeEvents on the model and forwards
  570. * AdjustmentEvents for the sake of backwards compatibility.
  571. * Unfortunately there's no way to determine the proper
  572. * type of the AdjustmentEvent as all updates to the model's
  573. * value are considered equivalent.
  574. */
  575. private class ModelListener implements ChangeListener, Serializable {
  576. public void stateChanged(ChangeEvent e) {
  577. int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED;
  578. int type = AdjustmentEvent.TRACK;
  579. fireAdjustmentValueChanged(id, type, getValue());
  580. }
  581. }
  582. // PENDING(hmuller) - the next three methods should be removed
  583. /**
  584. * The scrollbar is flexible along it's scrolling axis and
  585. * rigid along the other axis.
  586. */
  587. public Dimension getMinimumSize() {
  588. Dimension pref = getPreferredSize();
  589. if (orientation == VERTICAL) {
  590. return new Dimension(pref.width, 5);
  591. } else {
  592. return new Dimension(5, pref.height);
  593. }
  594. }
  595. /**
  596. * The scrollbar is flexible along it's scrolling axis and
  597. * rigid along the other axis.
  598. */
  599. public Dimension getMaximumSize() {
  600. Dimension pref = getPreferredSize();
  601. if (getOrientation() == VERTICAL) {
  602. return new Dimension(pref.width, Short.MAX_VALUE);
  603. } else {
  604. return new Dimension(Short.MAX_VALUE, pref.height);
  605. }
  606. }
  607. /**
  608. * Enables the component so that the knob position can be changed.
  609. * When the disabled, the knob position cannot be changed.
  610. *
  611. * @param x a boolean value, where true enables the component and
  612. * false disables it
  613. */
  614. public void setEnabled(boolean x) {
  615. super.setEnabled(x);
  616. Component[] children = getComponents();
  617. for(int i = 0; i < children.length; i++) {
  618. children[i].setEnabled(x);
  619. }
  620. }
  621. /**
  622. * See readObject() and writeObject() in JComponent for more
  623. * information about serialization in Swing.
  624. */
  625. private void writeObject(ObjectOutputStream s) throws IOException {
  626. s.defaultWriteObject();
  627. if (getUIClassID().equals(uiClassID)) {
  628. byte count = JComponent.getWriteObjCounter(this);
  629. JComponent.setWriteObjCounter(this, --count);
  630. if (count == 0 && ui != null) {
  631. ui.installUI(this);
  632. }
  633. }
  634. }
  635. /**
  636. * Returns a string representation of this JScrollBar. This method
  637. * is intended to be used only for debugging purposes, and the
  638. * content and format of the returned string may vary between
  639. * implementations. The returned string may be empty but may not
  640. * be <code>null</code>.
  641. *
  642. * @return a string representation of this JScrollBar.
  643. */
  644. protected String paramString() {
  645. String orientationString = (orientation == HORIZONTAL ?
  646. "HORIZONTAL" : "VERTICAL");
  647. return super.paramString() +
  648. ",blockIncrement=" + blockIncrement +
  649. ",orientation=" + orientationString +
  650. ",unitIncrement=" + unitIncrement;
  651. }
  652. /////////////////
  653. // Accessibility support
  654. ////////////////
  655. /**
  656. * Gets the AccessibleContext associated with this JScrollBar.
  657. * For JScrollBar, the AccessibleContext takes the form of an
  658. * AccessibleJScrollBar.
  659. * A new AccessibleJScrollBar instance is created if necessary.
  660. *
  661. * @return an AccessibleJScrollBar that serves as the
  662. * AccessibleContext of this JScrollBar
  663. */
  664. public AccessibleContext getAccessibleContext() {
  665. if (accessibleContext == null) {
  666. accessibleContext = new AccessibleJScrollBar();
  667. }
  668. return accessibleContext;
  669. }
  670. /**
  671. * This class implements accessibility support for the
  672. * <code>JScrollBar</code> class. It provides an implementation of the
  673. * Java Accessibility API appropriate to scroll bar user-interface
  674. * elements.
  675. * <p>
  676. * <strong>Warning:</strong>
  677. * Serialized objects of this class will not be compatible with
  678. * future Swing releases. The current serialization support is
  679. * appropriate for short term storage or RMI between applications running
  680. * the same version of Swing. As of 1.4, support for long term storage
  681. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  682. * has been added to the <code>java.beans</code> package.
  683. * Please see {@link java.beans.XMLEncoder}.
  684. */
  685. protected class AccessibleJScrollBar extends AccessibleJComponent
  686. implements AccessibleValue {
  687. /**
  688. * Get the state set of this object.
  689. *
  690. * @return an instance of AccessibleState containing the current state
  691. * of the object
  692. * @see AccessibleState
  693. */
  694. public AccessibleStateSet getAccessibleStateSet() {
  695. AccessibleStateSet states = super.getAccessibleStateSet();
  696. if (getValueIsAdjusting()) {
  697. states.add(AccessibleState.BUSY);
  698. }
  699. if (getOrientation() == VERTICAL) {
  700. states.add(AccessibleState.VERTICAL);
  701. } else {
  702. states.add(AccessibleState.HORIZONTAL);
  703. }
  704. return states;
  705. }
  706. /**
  707. * Get the role of this object.
  708. *
  709. * @return an instance of AccessibleRole describing the role of the
  710. * object
  711. */
  712. public AccessibleRole getAccessibleRole() {
  713. return AccessibleRole.SCROLL_BAR;
  714. }
  715. /**
  716. * Get the AccessibleValue associated with this object. In the
  717. * implementation of the Java Accessibility API for this class,
  718. * return this object, which is responsible for implementing the
  719. * AccessibleValue interface on behalf of itself.
  720. *
  721. * @return this object
  722. */
  723. public AccessibleValue getAccessibleValue() {
  724. return this;
  725. }
  726. /**
  727. * Get the accessible value of this object.
  728. *
  729. * @return The current value of this object.
  730. */
  731. public Number getCurrentAccessibleValue() {
  732. return new Integer(getValue());
  733. }
  734. /**
  735. * Set the value of this object as a Number.
  736. *
  737. * @return True if the value was set.
  738. */
  739. public boolean setCurrentAccessibleValue(Number n) {
  740. if (n instanceof Integer) {
  741. setValue(n.intValue());
  742. return true;
  743. } else {
  744. return false;
  745. }
  746. }
  747. /**
  748. * Get the minimum accessible value of this object.
  749. *
  750. * @return The minimum value of this object.
  751. */
  752. public Number getMinimumAccessibleValue() {
  753. return new Integer(getMinimum());
  754. }
  755. /**
  756. * Get the maximum accessible value of this object.
  757. *
  758. * @return The maximum value of this object.
  759. */
  760. public Number getMaximumAccessibleValue() {
  761. return new Integer(getMaximum());
  762. }
  763. } // AccessibleJScrollBar
  764. }