1. /*
  2. * @(#)JScrollBar.java 1.78 03/12/19
  3. *
  4. * Copyright 2004 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.78 12/19/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. * <p>
  292. * Note, that if the argument is equal to the value of Integer.MIN_VALUE,
  293. * the most look and feels will not provide the scrolling to the right/down.
  294. * @see #getUnitIncrement
  295. * @beaninfo
  296. * preferred: true
  297. * bound: true
  298. * description: The scrollbar's unit increment.
  299. */
  300. public void setUnitIncrement(int unitIncrement) {
  301. int oldValue = this.unitIncrement;
  302. this.unitIncrement = unitIncrement;
  303. firePropertyChange("unitIncrement", oldValue, unitIncrement);
  304. }
  305. /**
  306. * Returns the amount to change the scrollbar's value by,
  307. * given a block (usually "page") up/down request. A ScrollBarUI
  308. * implementation typically calls this method when the user clicks
  309. * above or below the scrollbar "knob" to change the value
  310. * up or down by large amount. Subclasses my override this
  311. * method to compute a value, e.g. the change required to scroll
  312. * up or down one paragraph in a text document.
  313. * <p>
  314. * The JScrollPane component creates scrollbars (by default)
  315. * that override this method and delegate to the viewports
  316. * Scrollable view, if it has one. The Scrollable interface
  317. * provides a more specialized version of this method.
  318. *
  319. * @param direction is -1 or 1 for up/down respectively
  320. * @return the value of the blockIncrement property
  321. * @see #setBlockIncrement
  322. * @see #setValue
  323. * @see Scrollable#getScrollableBlockIncrement
  324. */
  325. public int getBlockIncrement(int direction) {
  326. return blockIncrement;
  327. }
  328. /**
  329. * Sets the blockIncrement property.
  330. * <p>
  331. * Note, that if the argument is equal to the value of Integer.MIN_VALUE,
  332. * the most look and feels will not provide the scrolling to the right/down.
  333. * @see #getBlockIncrement()
  334. * @beaninfo
  335. * preferred: true
  336. * bound: true
  337. * description: The scrollbar's block increment.
  338. */
  339. public void setBlockIncrement(int blockIncrement) {
  340. int oldValue = this.blockIncrement;
  341. this.blockIncrement = blockIncrement;
  342. firePropertyChange("blockIncrement", oldValue, blockIncrement);
  343. }
  344. /**
  345. * For backwards compatibility with java.awt.Scrollbar.
  346. * @see Adjustable#getUnitIncrement
  347. * @see #getUnitIncrement(int)
  348. */
  349. public int getUnitIncrement() {
  350. return unitIncrement;
  351. }
  352. /**
  353. * For backwards compatibility with java.awt.Scrollbar.
  354. * @see Adjustable#getBlockIncrement
  355. * @see #getBlockIncrement(int)
  356. */
  357. public int getBlockIncrement() {
  358. return blockIncrement;
  359. }
  360. /**
  361. * Returns the scrollbar's value.
  362. * @return the model's value property
  363. * @see #setValue
  364. */
  365. public int getValue() {
  366. return getModel().getValue();
  367. }
  368. /**
  369. * Sets the scrollbar's value. This method just forwards the value
  370. * to the model.
  371. *
  372. * @see #getValue
  373. * @see BoundedRangeModel#setValue
  374. * @beaninfo
  375. * preferred: true
  376. * description: The scrollbar's current value.
  377. */
  378. public void setValue(int value) {
  379. BoundedRangeModel m = getModel();
  380. int oldValue = m.getValue();
  381. m.setValue(value);
  382. if (accessibleContext != null) {
  383. accessibleContext.firePropertyChange(
  384. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  385. new Integer(oldValue),
  386. new Integer(m.getValue()));
  387. }
  388. }
  389. /**
  390. * Returns the scrollbar's extent, aka its "visibleAmount". In many
  391. * scrollbar look and feel implementations the size of the
  392. * scrollbar "knob" or "thumb" is proportional to the extent.
  393. *
  394. * @return the value of the model's extent property
  395. * @see #setVisibleAmount
  396. */
  397. public int getVisibleAmount() {
  398. return getModel().getExtent();
  399. }
  400. /**
  401. * Set the model's extent property.
  402. *
  403. * @see #getVisibleAmount
  404. * @see BoundedRangeModel#setExtent
  405. * @beaninfo
  406. * preferred: true
  407. * description: The amount of the view that is currently visible.
  408. */
  409. public void setVisibleAmount(int extent) {
  410. getModel().setExtent(extent);
  411. }
  412. /**
  413. * Returns the minimum value supported by the scrollbar
  414. * (usually zero).
  415. *
  416. * @return the value of the model's minimum property
  417. * @see #setMinimum
  418. */
  419. public int getMinimum() {
  420. return getModel().getMinimum();
  421. }
  422. /**
  423. * Sets the model's minimum property.
  424. *
  425. * @see #getMinimum
  426. * @see BoundedRangeModel#setMinimum
  427. * @beaninfo
  428. * preferred: true
  429. * description: The scrollbar's minimum value.
  430. */
  431. public void setMinimum(int minimum) {
  432. getModel().setMinimum(minimum);
  433. }
  434. /**
  435. * The maximum value of the scrollbar is maximum - extent.
  436. *
  437. * @return the value of the model's maximum property
  438. * @see #setMaximum
  439. */
  440. public int getMaximum() {
  441. return getModel().getMaximum();
  442. }
  443. /**
  444. * Sets the model's maximum property. Note that the scrollbar's value
  445. * can only be set to maximum - extent.
  446. *
  447. * @see #getMaximum
  448. * @see BoundedRangeModel#setMaximum
  449. * @beaninfo
  450. * preferred: true
  451. * description: The scrollbar's maximum value.
  452. */
  453. public void setMaximum(int maximum) {
  454. getModel().setMaximum(maximum);
  455. }
  456. /**
  457. * True if the scrollbar knob is being dragged.
  458. *
  459. * @return the value of the model's valueIsAdjusting property
  460. * @see #setValueIsAdjusting
  461. */
  462. public boolean getValueIsAdjusting() {
  463. return getModel().getValueIsAdjusting();
  464. }
  465. /**
  466. * Sets the model's valueIsAdjusting property. Scrollbar look and
  467. * feel implementations should set this property to true when
  468. * a knob drag begins, and to false when the drag ends. The
  469. * scrollbar model will not generate ChangeEvents while
  470. * valueIsAdjusting is true.
  471. *
  472. * @see #getValueIsAdjusting
  473. * @see BoundedRangeModel#setValueIsAdjusting
  474. * @beaninfo
  475. * expert: true
  476. * description: True if the scrollbar thumb is being dragged.
  477. */
  478. public void setValueIsAdjusting(boolean b) {
  479. BoundedRangeModel m = getModel();
  480. boolean oldValue = m.getValueIsAdjusting();
  481. m.setValueIsAdjusting(b);
  482. if ((oldValue != b) && (accessibleContext != null)) {
  483. accessibleContext.firePropertyChange(
  484. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  485. ((oldValue) ? AccessibleState.BUSY : null),
  486. ((b) ? AccessibleState.BUSY : null));
  487. }
  488. }
  489. /**
  490. * Sets the four BoundedRangeModel properties after forcing
  491. * the arguments to obey the usual constraints:
  492. * <pre>
  493. * minimum <= value <= value+extent <= maximum
  494. * </pre>
  495. * <p>
  496. *
  497. * @see BoundedRangeModel#setRangeProperties
  498. * @see #setValue
  499. * @see #setVisibleAmount
  500. * @see #setMinimum
  501. * @see #setMaximum
  502. */
  503. public void setValues(int newValue, int newExtent, int newMin, int newMax)
  504. {
  505. BoundedRangeModel m = getModel();
  506. int oldValue = m.getValue();
  507. m.setRangeProperties(newValue, newExtent, newMin, newMax, m.getValueIsAdjusting());
  508. if (accessibleContext != null) {
  509. accessibleContext.firePropertyChange(
  510. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  511. new Integer(oldValue),
  512. new Integer(m.getValue()));
  513. }
  514. }
  515. /**
  516. * Adds an AdjustmentListener. Adjustment listeners are notified
  517. * each time the scrollbar's model changes. Adjustment events are
  518. * provided for backwards compatibility with java.awt.Scrollbar.
  519. * <p>
  520. * Note that the AdjustmentEvents type property will always have a
  521. * placeholder value of AdjustmentEvent.TRACK because all changes
  522. * to a BoundedRangeModels value are considered equivalent. To change
  523. * the value of a BoundedRangeModel one just sets its value property,
  524. * i.e. model.setValue(123). No information about the origin of the
  525. * change, e.g. it's a block decrement, is provided. We don't try
  526. * fabricate the origin of the change here.
  527. *
  528. * @param l the AdjustmentLister to add
  529. * @see #removeAdjustmentListener
  530. * @see BoundedRangeModel#addChangeListener
  531. */
  532. public void addAdjustmentListener(AdjustmentListener l) {
  533. listenerList.add(AdjustmentListener.class, l);
  534. }
  535. /**
  536. * Removes an AdjustmentEvent listener.
  537. *
  538. * @param l the AdjustmentLister to remove
  539. * @see #addAdjustmentListener
  540. */
  541. public void removeAdjustmentListener(AdjustmentListener l) {
  542. listenerList.remove(AdjustmentListener.class, l);
  543. }
  544. /**
  545. * Returns an array of all the <code>AdjustmentListener</code>s added
  546. * to this JScrollBar with addAdjustmentListener().
  547. *
  548. * @return all of the <code>AdjustmentListener</code>s added or an empty
  549. * array if no listeners have been added
  550. * @since 1.4
  551. */
  552. public AdjustmentListener[] getAdjustmentListeners() {
  553. return (AdjustmentListener[])listenerList.getListeners(
  554. AdjustmentListener.class);
  555. }
  556. /*
  557. * Notify listeners that the scrollbar's model has changed.
  558. *
  559. * @see #addAdjustmentListener
  560. * @see EventListenerList
  561. */
  562. protected void fireAdjustmentValueChanged(int id, int type, int value) {
  563. fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting());
  564. }
  565. /*
  566. * Notify listeners that the scrollbar's model has changed.
  567. *
  568. * @see #addAdjustmentListener
  569. * @see EventListenerList
  570. */
  571. private void fireAdjustmentValueChanged(int id, int type, int value,
  572. boolean isAdjusting) {
  573. Object[] listeners = listenerList.getListenerList();
  574. AdjustmentEvent e = null;
  575. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  576. if (listeners[i]==AdjustmentListener.class) {
  577. if (e == null) {
  578. e = new AdjustmentEvent(this, id, type, value, isAdjusting);
  579. }
  580. ((AdjustmentListener)listeners[i+1]).adjustmentValueChanged(e);
  581. }
  582. }
  583. }
  584. /**
  585. * This class listens to ChangeEvents on the model and forwards
  586. * AdjustmentEvents for the sake of backwards compatibility.
  587. * Unfortunately there's no way to determine the proper
  588. * type of the AdjustmentEvent as all updates to the model's
  589. * value are considered equivalent.
  590. */
  591. private class ModelListener implements ChangeListener, Serializable {
  592. public void stateChanged(ChangeEvent e) {
  593. Object obj = e.getSource();
  594. if (obj instanceof BoundedRangeModel) {
  595. int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED;
  596. int type = AdjustmentEvent.TRACK;
  597. BoundedRangeModel model = (BoundedRangeModel)obj;
  598. int value = model.getValue();
  599. boolean isAdjusting = model.getValueIsAdjusting();
  600. fireAdjustmentValueChanged(id, type, value, isAdjusting);
  601. }
  602. }
  603. }
  604. // PENDING(hmuller) - the next three methods should be removed
  605. /**
  606. * The scrollbar is flexible along it's scrolling axis and
  607. * rigid along the other axis.
  608. */
  609. public Dimension getMinimumSize() {
  610. Dimension pref = getPreferredSize();
  611. if (orientation == VERTICAL) {
  612. return new Dimension(pref.width, 5);
  613. } else {
  614. return new Dimension(5, pref.height);
  615. }
  616. }
  617. /**
  618. * The scrollbar is flexible along it's scrolling axis and
  619. * rigid along the other axis.
  620. */
  621. public Dimension getMaximumSize() {
  622. Dimension pref = getPreferredSize();
  623. if (getOrientation() == VERTICAL) {
  624. return new Dimension(pref.width, Short.MAX_VALUE);
  625. } else {
  626. return new Dimension(Short.MAX_VALUE, pref.height);
  627. }
  628. }
  629. /**
  630. * Enables the component so that the knob position can be changed.
  631. * When the disabled, the knob position cannot be changed.
  632. *
  633. * @param x a boolean value, where true enables the component and
  634. * false disables it
  635. */
  636. public void setEnabled(boolean x) {
  637. super.setEnabled(x);
  638. Component[] children = getComponents();
  639. for(int i = 0; i < children.length; i++) {
  640. children[i].setEnabled(x);
  641. }
  642. }
  643. /**
  644. * See readObject() and writeObject() in JComponent for more
  645. * information about serialization in Swing.
  646. */
  647. private void writeObject(ObjectOutputStream s) throws IOException {
  648. s.defaultWriteObject();
  649. if (getUIClassID().equals(uiClassID)) {
  650. byte count = JComponent.getWriteObjCounter(this);
  651. JComponent.setWriteObjCounter(this, --count);
  652. if (count == 0 && ui != null) {
  653. ui.installUI(this);
  654. }
  655. }
  656. }
  657. /**
  658. * Returns a string representation of this JScrollBar. This method
  659. * is intended to be used only for debugging purposes, and the
  660. * content and format of the returned string may vary between
  661. * implementations. The returned string may be empty but may not
  662. * be <code>null</code>.
  663. *
  664. * @return a string representation of this JScrollBar.
  665. */
  666. protected String paramString() {
  667. String orientationString = (orientation == HORIZONTAL ?
  668. "HORIZONTAL" : "VERTICAL");
  669. return super.paramString() +
  670. ",blockIncrement=" + blockIncrement +
  671. ",orientation=" + orientationString +
  672. ",unitIncrement=" + unitIncrement;
  673. }
  674. /////////////////
  675. // Accessibility support
  676. ////////////////
  677. /**
  678. * Gets the AccessibleContext associated with this JScrollBar.
  679. * For JScrollBar, the AccessibleContext takes the form of an
  680. * AccessibleJScrollBar.
  681. * A new AccessibleJScrollBar instance is created if necessary.
  682. *
  683. * @return an AccessibleJScrollBar that serves as the
  684. * AccessibleContext of this JScrollBar
  685. */
  686. public AccessibleContext getAccessibleContext() {
  687. if (accessibleContext == null) {
  688. accessibleContext = new AccessibleJScrollBar();
  689. }
  690. return accessibleContext;
  691. }
  692. /**
  693. * This class implements accessibility support for the
  694. * <code>JScrollBar</code> class. It provides an implementation of the
  695. * Java Accessibility API appropriate to scroll bar user-interface
  696. * elements.
  697. * <p>
  698. * <strong>Warning:</strong>
  699. * Serialized objects of this class will not be compatible with
  700. * future Swing releases. The current serialization support is
  701. * appropriate for short term storage or RMI between applications running
  702. * the same version of Swing. As of 1.4, support for long term storage
  703. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  704. * has been added to the <code>java.beans</code> package.
  705. * Please see {@link java.beans.XMLEncoder}.
  706. */
  707. protected class AccessibleJScrollBar extends AccessibleJComponent
  708. implements AccessibleValue {
  709. /**
  710. * Get the state set of this object.
  711. *
  712. * @return an instance of AccessibleState containing the current state
  713. * of the object
  714. * @see AccessibleState
  715. */
  716. public AccessibleStateSet getAccessibleStateSet() {
  717. AccessibleStateSet states = super.getAccessibleStateSet();
  718. if (getValueIsAdjusting()) {
  719. states.add(AccessibleState.BUSY);
  720. }
  721. if (getOrientation() == VERTICAL) {
  722. states.add(AccessibleState.VERTICAL);
  723. } else {
  724. states.add(AccessibleState.HORIZONTAL);
  725. }
  726. return states;
  727. }
  728. /**
  729. * Get the role of this object.
  730. *
  731. * @return an instance of AccessibleRole describing the role of the
  732. * object
  733. */
  734. public AccessibleRole getAccessibleRole() {
  735. return AccessibleRole.SCROLL_BAR;
  736. }
  737. /**
  738. * Get the AccessibleValue associated with this object. In the
  739. * implementation of the Java Accessibility API for this class,
  740. * return this object, which is responsible for implementing the
  741. * AccessibleValue interface on behalf of itself.
  742. *
  743. * @return this object
  744. */
  745. public AccessibleValue getAccessibleValue() {
  746. return this;
  747. }
  748. /**
  749. * Get the accessible value of this object.
  750. *
  751. * @return The current value of this object.
  752. */
  753. public Number getCurrentAccessibleValue() {
  754. return new Integer(getValue());
  755. }
  756. /**
  757. * Set the value of this object as a Number.
  758. *
  759. * @return True if the value was set.
  760. */
  761. public boolean setCurrentAccessibleValue(Number n) {
  762. // TIGER - 4422535
  763. if (n == null) {
  764. return false;
  765. }
  766. setValue(n.intValue());
  767. return true;
  768. }
  769. /**
  770. * Get the minimum accessible value of this object.
  771. *
  772. * @return The minimum value of this object.
  773. */
  774. public Number getMinimumAccessibleValue() {
  775. return new Integer(getMinimum());
  776. }
  777. /**
  778. * Get the maximum accessible value of this object.
  779. *
  780. * @return The maximum value of this object.
  781. */
  782. public Number getMaximumAccessibleValue() {
  783. // TIGER - 4422362
  784. return new Integer(model.getMaximum() - model.getExtent());
  785. }
  786. } // AccessibleJScrollBar
  787. }