JSlider.java 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /* JSlider.java --
  2. Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.awt.MenuContainer;
  34. import java.awt.image.ImageObserver;
  35. import java.beans.PropertyChangeEvent;
  36. import java.io.Serializable;
  37. import java.util.Dictionary;
  38. import java.util.Enumeration;
  39. import java.util.Hashtable;
  40. import javax.accessibility.Accessible;
  41. import javax.accessibility.AccessibleContext;
  42. import javax.accessibility.AccessibleRole;
  43. import javax.accessibility.AccessibleState;
  44. import javax.accessibility.AccessibleStateSet;
  45. import javax.accessibility.AccessibleValue;
  46. import javax.swing.event.ChangeEvent;
  47. import javax.swing.event.ChangeListener;
  48. import javax.swing.plaf.SliderUI;
  49. import javax.swing.plaf.UIResource;
  50. /**
  51. * A visual component that allows selection of a value within a
  52. * range by adjusting a thumb in a track. The values for the minimum,
  53. * maximum, extent and value are stored in a {@link
  54. * DefaultBoundedRangeModel}.
  55. * <p>
  56. * A <code>JSlider</code> component has the following properties:
  57. * </p>
  58. *
  59. * <table>
  60. * <tr><th> Property </th><th> Stored in </th><th> Bound? </th></tr>
  61. * <tr><td> extent </td><td> model </td><td> no </td></tr>
  62. * <tr><td> inverted </td><td> slider </td><td> yes </td></tr>
  63. * <tr><td> labelTable </td><td> slider </td><td> yes </td></tr>
  64. * <tr><td> majorTickSpacing </td><td> slider </td><td> yes </td></tr>
  65. * <tr><td> maximum </td><td> model </td><td> yes </td></tr>
  66. * <tr><td> minimum </td><td> model </td><td> yes </td></tr>
  67. * <tr><td> minorTickSpacing </td><td> slider </td><td> yes </td></tr>
  68. * <tr><td> model </td><td> slider </td><td> yes </td></tr>
  69. * <tr><td> orientation </td><td> slider </td><td> yes </td></tr>
  70. * <tr><td> paintLabels </td><td> slider </td><td> yes </td></tr>
  71. * <tr><td> paintTicks </td><td> slider </td><td> yes </td></tr>
  72. * <tr><td> snapToTicks </td><td> slider </td><td> yes </td></tr>
  73. * <tr><td> value </td><td> model </td><td> no </td></tr>
  74. * <tr><td> valueIsAdjusting </td><td> model </td><td> no </td></tr>
  75. * </table>
  76. *
  77. * <p>
  78. * The various behavioural aspects of these properties follows:
  79. * </p>
  80. *
  81. * <ul>
  82. * <li>
  83. * When a non-bound property stored in the slider changes, the slider fires
  84. * a {@link ChangeEvent} to its change listeners.
  85. * </li>
  86. * <li>
  87. * When a bound property stored in the slider changes, the slider fires a
  88. * {@link PropertyChangeEvent} to its property change listeners.
  89. * </li>
  90. * <li>
  91. * If any of the model's properties change, it fires a {@link ChangeEvent} to
  92. * its listeners, which include the slider.
  93. * </li>
  94. * <li>
  95. * If the slider receives a {@link ChangeEvent} from its model, it will
  96. * propagate the event to its own change listeners, with the event's "source"
  97. * property set to refer to the slider, rather than the model.
  98. * </li>
  99. * </ul>
  100. */
  101. public class JSlider extends JComponent implements SwingConstants, Accessible,
  102. ImageObserver,
  103. MenuContainer, Serializable
  104. {
  105. /**
  106. * A little testing shows that the reference implementation creates
  107. * labels from a class named LabelUIResource.
  108. */
  109. private class LabelUIResource
  110. extends JLabel
  111. implements UIResource
  112. {
  113. LabelUIResource(String text, int align)
  114. {
  115. super(text, align);
  116. setName("Slider.label");
  117. }
  118. }
  119. private static final long serialVersionUID = -1441275936141218479L;
  120. /**
  121. * Provides the accessibility features for the <code>JSlider</code>
  122. * component.
  123. */
  124. protected class AccessibleJSlider extends JComponent.AccessibleJComponent
  125. implements AccessibleValue
  126. {
  127. private static final long serialVersionUID = -6301740148041106789L;
  128. /**
  129. * Creates a new <code>AccessibleJSlider</code> instance.
  130. */
  131. protected AccessibleJSlider()
  132. {
  133. // Nothing to do here.
  134. }
  135. /**
  136. * Returns a set containing the current state of the {@link JSlider}
  137. * component.
  138. *
  139. * @return The accessible state set.
  140. */
  141. public AccessibleStateSet getAccessibleStateSet()
  142. {
  143. AccessibleStateSet result = super.getAccessibleStateSet();
  144. if (orientation == JSlider.HORIZONTAL)
  145. result.add(AccessibleState.HORIZONTAL);
  146. else if (orientation == JSlider.VERTICAL)
  147. result.add(AccessibleState.VERTICAL);
  148. return result;
  149. }
  150. /**
  151. * Returns the accessible role for the <code>JSlider</code> component.
  152. *
  153. * @return {@link AccessibleRole#SLIDER}.
  154. */
  155. public AccessibleRole getAccessibleRole()
  156. {
  157. return AccessibleRole.SLIDER;
  158. }
  159. /**
  160. * Returns an object that provides access to the current, minimum and
  161. * maximum values for the {@link JSlider}. Since this class implements
  162. * {@link AccessibleValue}, it returns itself.
  163. *
  164. * @return The accessible value.
  165. */
  166. public AccessibleValue getAccessibleValue()
  167. {
  168. return this;
  169. }
  170. /**
  171. * Returns the current value of the {@link JSlider} component, as an
  172. * {@link Integer}.
  173. *
  174. * @return The current value of the {@link JSlider} component.
  175. */
  176. public Number getCurrentAccessibleValue()
  177. {
  178. return new Integer(getValue());
  179. }
  180. /**
  181. * Sets the current value of the {@link JSlider} component and sends a
  182. * {@link PropertyChangeEvent} (with the property name
  183. * {@link AccessibleContext#ACCESSIBLE_VALUE_PROPERTY}) to all registered
  184. * listeners. If the supplied value is <code>null</code>, this method
  185. * does nothing and returns <code>false</code>.
  186. *
  187. * @param value the new slider value (<code>null</code> permitted).
  188. *
  189. * @return <code>true</code> if the slider value is updated, and
  190. * <code>false</code> otherwise.
  191. */
  192. public boolean setCurrentAccessibleValue(Number value)
  193. {
  194. if (value == null)
  195. return false;
  196. Number oldValue = getCurrentAccessibleValue();
  197. setValue(value.intValue());
  198. firePropertyChange(AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, oldValue,
  199. new Integer(getValue()));
  200. return true;
  201. }
  202. /**
  203. * Returns the minimum value of the {@link JSlider} component, as an
  204. * {@link Integer}.
  205. *
  206. * @return The minimum value of the {@link JSlider} component.
  207. */
  208. public Number getMinimumAccessibleValue()
  209. {
  210. return new Integer(getMinimum());
  211. }
  212. /**
  213. * Returns the maximum value of the {@link JSlider} component, as an
  214. * {@link Integer}.
  215. *
  216. * @return The maximum value of the {@link JSlider} component.
  217. */
  218. public Number getMaximumAccessibleValue()
  219. {
  220. return new Integer(getMaximum());
  221. }
  222. }
  223. /** Whether or not this slider paints its ticks. */
  224. private transient boolean paintTicks;
  225. /** Whether or not this slider paints its track. */
  226. private transient boolean paintTrack = true;
  227. /** Whether or not this slider paints its labels. */
  228. private transient boolean paintLabels;
  229. /**
  230. * A dictionary of (Integer, Component) pairs where each Component is a
  231. * JLabel and the Integer determines where the label will be painted.
  232. */
  233. private transient Dictionary labelTable;
  234. /** The model used to store the slider's range and current value. */
  235. protected BoundedRangeModel sliderModel;
  236. /** The space/distance between major ticks. */
  237. protected int majorTickSpacing;
  238. /** The space/distance between minor ticks. */
  239. protected int minorTickSpacing;
  240. /** Whether the slider snaps its values to ticks. */
  241. protected boolean snapToTicks;
  242. /** The orientation (horizontal or vertical) of the slider. */
  243. protected int orientation = HORIZONTAL;
  244. /** Whether the slider is inverted. */
  245. private transient boolean isInverted;
  246. /**
  247. * The listener that monitors the slider's model and forwards events to the
  248. * slider's listeners (see <code>createChangeListener()</code>).
  249. */
  250. protected ChangeListener changeListener;
  251. /** The change event that is passed to all listeners of this slider. */
  252. protected transient ChangeEvent changeEvent;
  253. /**
  254. * Creates a new horizontal <code>JSlider</code> instance with a minimum of
  255. * 0, a maximum of 100, and a value of 50.
  256. */
  257. public JSlider()
  258. {
  259. this(HORIZONTAL, 0, 100, 50);
  260. }
  261. /**
  262. * Creates a new <code>JSlider</code> instance with the given orientation
  263. * and a minimum of 0, a maximum of 100, and a value of 50.
  264. *
  265. * @param orientation The orientation of the slider ({@link #HORIZONTAL} or
  266. * {@link #VERTICAL}).
  267. *
  268. * @throws IllegalArgumentException if <code>orientation</code> is not one of
  269. * the specified values.
  270. */
  271. public JSlider(int orientation)
  272. {
  273. this(orientation, 0, 100, 50);
  274. }
  275. /**
  276. * Creates a new horizontal <code>JSlider</code> instance with the given
  277. * maximum and minimum and a value that is halfway between the minimum and the
  278. * maximum.
  279. *
  280. * @param minimum The minimum value.
  281. * @param maximum The maximum value.
  282. *
  283. * @throws IllegalArgumentException if <code>minimum</code> is greater than
  284. * <code>maximum</code>.
  285. */
  286. public JSlider(int minimum, int maximum)
  287. {
  288. this(HORIZONTAL, minimum, maximum, (maximum + minimum) / 2);
  289. }
  290. /**
  291. * Creates a new horizontal <code>JSlider</code> instance with the given
  292. * minimum, maximum, and value.
  293. *
  294. * @param minimum The minimum value.
  295. * @param maximum The maximum value.
  296. * @param value The initial value.
  297. *
  298. * @throws IllegalArgumentException if <code>value</code> is not in the
  299. * specified range.
  300. * @throws IllegalArgumentException if <code>minimum</code> is greater than
  301. * <code>maximum</code>.
  302. */
  303. public JSlider(int minimum, int maximum, int value)
  304. {
  305. this(HORIZONTAL, minimum, maximum, value);
  306. }
  307. /**
  308. * Creates a new <code>JSlider</code> instance with the given orientation,
  309. * minimum, maximum, and value.
  310. *
  311. * @param orientation The orientation of the slider ({@link #HORIZONTAL} or
  312. * {@link #VERTICAL}).
  313. * @param minimum The minimum value of the JSlider.
  314. * @param maximum The maximum value of the JSlider.
  315. * @param value The initial value of the JSlider.
  316. *
  317. * @throws IllegalArgumentException if <code>orientation</code> is not one of
  318. * the specified values.
  319. * @throws IllegalArgumentException if <code>value</code> is not in the
  320. * specified range.
  321. * @throws IllegalArgumentException if <code>minimum</code> is greater than
  322. * <code>maximum</code>.
  323. */
  324. public JSlider(int orientation, int minimum, int maximum, int value)
  325. {
  326. sliderModel = new DefaultBoundedRangeModel(value, 0, minimum, maximum);
  327. if (orientation != HORIZONTAL && orientation != VERTICAL)
  328. throw new IllegalArgumentException(orientation
  329. + " is not a legal orientation");
  330. this.orientation = orientation;
  331. changeListener = createChangeListener();
  332. sliderModel.addChangeListener(changeListener);
  333. updateUI();
  334. }
  335. /**
  336. * Creates a new horizontal <code>JSlider</code> instance with the given
  337. * model.
  338. *
  339. * @param model The model (<code>null</code> not permitted).
  340. *
  341. * @throws NullPointerException if <code>model</code> is <code>null</code>.
  342. */
  343. public JSlider(BoundedRangeModel model)
  344. {
  345. sliderModel = model;
  346. changeListener = createChangeListener();
  347. sliderModel.addChangeListener(changeListener);
  348. updateUI();
  349. }
  350. /**
  351. * Returns the slider's value (from the slider's model).
  352. *
  353. * @return The value of the slider.
  354. *
  355. * @see #setValue(int)
  356. */
  357. public int getValue()
  358. {
  359. return sliderModel.getValue();
  360. }
  361. /**
  362. * Sets the slider's value and sends a {@link ChangeEvent} to all
  363. * registered listeners. Note that the model will fire a change event to all
  364. * of its registered listeners first (with the model as the event source) and
  365. * then the slider will fire another change event to all of its registered
  366. * listeners (this time with the slider as the event source).
  367. *
  368. * @param value the new value.
  369. *
  370. * @see #getValue()
  371. */
  372. public void setValue(int value)
  373. {
  374. sliderModel.setValue(value);
  375. }
  376. /**
  377. * Returns the slider's UI delegate.
  378. *
  379. * @return The slider's UI delegate.
  380. */
  381. public SliderUI getUI()
  382. {
  383. return (SliderUI) ui;
  384. }
  385. /**
  386. * Sets the slider's UI delegate.
  387. *
  388. * @param ui the UI delegate.
  389. */
  390. public void setUI(SliderUI ui)
  391. {
  392. super.setUI(ui);
  393. }
  394. /**
  395. * Sets this slider's UI delegate to the default (obtained from the
  396. * {@link UIManager}) for the current look and feel.
  397. */
  398. public void updateUI()
  399. {
  400. updateLabelUIs();
  401. setUI((SliderUI) UIManager.getUI(this));
  402. }
  403. /**
  404. * Returns the suffix (<code>"SliderUI"</code> in this case) used to
  405. * determine the class name for a UI delegate that can provide the look and
  406. * feel for a <code>JSlider</code>.
  407. *
  408. * @return <code>"SliderUI"</code>.
  409. */
  410. public String getUIClassID()
  411. {
  412. return "SliderUI";
  413. }
  414. /**
  415. * Creates a {@link ChangeListener} that is added to the slider's model and
  416. * forwards change events generated by the model to the listeners that are
  417. * registered with the <code>JSlider</code> (by calling the
  418. * {@link #fireStateChanged} method).
  419. *
  420. * @return A new listener.
  421. */
  422. protected ChangeListener createChangeListener()
  423. {
  424. return new ChangeListener()
  425. {
  426. public void stateChanged(ChangeEvent ce)
  427. {
  428. // No need to trigger a repaint since the UI listens to the model
  429. // as well. All we need to do is pass on the stateChanged event
  430. // to our listeners.
  431. fireStateChanged();
  432. }
  433. };
  434. }
  435. /**
  436. * Registers a listener with the slider so that it will receive
  437. * {@link ChangeEvent} notifications. Note that change events generated
  438. * by the slider's model will be forwarded automatically to the slider's
  439. * listeners.
  440. *
  441. * @param listener the listener to register.
  442. *
  443. * @see #removeChangeListener(ChangeListener)
  444. */
  445. public void addChangeListener(ChangeListener listener)
  446. {
  447. listenerList.add(ChangeListener.class, listener);
  448. }
  449. /**
  450. * Removes a listener from this slider so that it will no longer receive
  451. * {@link ChangeEvent} notifications from the slider.
  452. *
  453. * @param listener The listener to remove.
  454. *
  455. * @see #addChangeListener(ChangeListener)
  456. */
  457. public void removeChangeListener(ChangeListener listener)
  458. {
  459. listenerList.remove(ChangeListener.class, listener);
  460. }
  461. /**
  462. * Sends a {@link ChangeEvent} to all registered listeners, with this slider
  463. * as the source.
  464. */
  465. protected void fireStateChanged()
  466. {
  467. Object[] changeListeners = listenerList.getListenerList();
  468. if (changeEvent == null)
  469. changeEvent = new ChangeEvent(this);
  470. for (int i = changeListeners.length - 2; i >= 0; i -= 2)
  471. {
  472. if (changeListeners[i] == ChangeListener.class)
  473. ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
  474. }
  475. }
  476. /**
  477. * Returns an array containing all the {@link ChangeListener} instances
  478. * registered with this slider. If no listeners are registered, this method
  479. * returns an empty array.
  480. *
  481. * @return An array array containing all the {@link ChangeListener} instances
  482. * registered with this slider (possibly empty, but never
  483. * <code>null</code>).
  484. */
  485. public ChangeListener[] getChangeListeners()
  486. {
  487. return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
  488. }
  489. /**
  490. * Returns the slider's model, which stores the minimum, maximum and current
  491. * values.
  492. *
  493. * @return The slider's model.
  494. *
  495. * @see #setModel(BoundedRangeModel)
  496. */
  497. public BoundedRangeModel getModel()
  498. {
  499. return sliderModel;
  500. }
  501. /**
  502. * Sets the slider's model and sends a {@link PropertyChangeEvent} (with the
  503. * property name "model") to all registered listeners. The change listener
  504. * that the slider registered with the original model is removed and added
  505. * to the new model (this ensures that {@link ChangeEvent} notifications
  506. * generated by the model are automatically forwarded to listeners that are
  507. * registered with the slider).
  508. *
  509. * @param model The model to use with the slider.
  510. *
  511. * @see #getModel()
  512. */
  513. public void setModel(BoundedRangeModel model)
  514. {
  515. // I didn't do the null pointer check on purpose.
  516. // If you try it with Sun's, it'll go ahead and set it to null
  517. // and bork the next time it tries to access the model.
  518. if (model != sliderModel)
  519. {
  520. BoundedRangeModel oldModel = sliderModel;
  521. sliderModel = model;
  522. oldModel.removeChangeListener(changeListener);
  523. sliderModel.addChangeListener(changeListener);
  524. firePropertyChange("model", oldModel, sliderModel);
  525. }
  526. }
  527. /**
  528. * Returns the minimum value of the slider (from the slider's model).
  529. *
  530. * @return The minimum value of the slider.
  531. *
  532. * @see #setMinimum(int)
  533. */
  534. public int getMinimum()
  535. {
  536. return sliderModel.getMinimum();
  537. }
  538. /**
  539. * Sets the minimum value of the slider and fires a
  540. * {@link PropertyChangeEvent} (with the property name "minimum") to all
  541. * registered listeners. Note that:
  542. * <p>
  543. * <ul>
  544. * <li>the minimum value is stored in the slider's model (see
  545. * {@link #getModel()});</li>
  546. * <li>in addition to the property change event, the slider also fires a
  547. * {@link ChangeEvent}.</li>
  548. * </ul>
  549. *
  550. * @param minimum The minimum value of the slider.
  551. *
  552. * @see #getMinimum()
  553. */
  554. public void setMinimum(int minimum)
  555. {
  556. int old = sliderModel.getMinimum();
  557. sliderModel.setMinimum(minimum);
  558. if (minimum != old)
  559. firePropertyChange("minimum", old, minimum);
  560. }
  561. /**
  562. * Returns the slider's maximum value (obtained from the slider's model).
  563. *
  564. * @return The maximum value of the slider.
  565. *
  566. * @see #setMaximum(int)
  567. */
  568. public int getMaximum()
  569. {
  570. return sliderModel.getMaximum();
  571. }
  572. /**
  573. * Sets the maximum value of the slider and fires a
  574. * {@link PropertyChangeEvent} (with the property name "maximum") to all
  575. * registered listeners. Note that:
  576. * <p>
  577. * <ul>
  578. * <li>the maximum value is stored in the slider's model (see
  579. * {@link #getModel()});</li>
  580. * <li>in addition to the property change event, the slider also fires a
  581. * {@link ChangeEvent}.</li>
  582. * </ul>
  583. *
  584. * @param maximum The maximum value of the slider.
  585. *
  586. * @see #getMaximum()
  587. */
  588. public void setMaximum(int maximum)
  589. {
  590. int old = sliderModel.getMaximum();
  591. sliderModel.setMaximum(maximum);
  592. if (maximum != old)
  593. firePropertyChange("maximum", old, maximum);
  594. }
  595. /**
  596. * Returns the <code>valueIsAdjusting</code> flag from the slider's model.
  597. *
  598. * @return The <code>valueIsAdjusting</code> flag from the slider's model.
  599. *
  600. * @see #setValueIsAdjusting(boolean)
  601. */
  602. public boolean getValueIsAdjusting()
  603. {
  604. return sliderModel.getValueIsAdjusting();
  605. }
  606. /**
  607. * Sets the <code>valueIsAdjusting</code> flag in the slider's model, and
  608. * sends a {@link ChangeEvent} to all registered listeners.
  609. *
  610. * @param adjusting the new flag value.
  611. *
  612. * @see #getValueIsAdjusting()
  613. */
  614. public void setValueIsAdjusting(boolean adjusting)
  615. {
  616. sliderModel.setValueIsAdjusting(adjusting);
  617. }
  618. /**
  619. * Returns the slider's extent value, obtained from the slider's model.
  620. *
  621. * @return The extent value.
  622. *
  623. * @see #setExtent(int)
  624. */
  625. public int getExtent()
  626. {
  627. return sliderModel.getExtent();
  628. }
  629. /**
  630. * Sets the slider's extent value and sends a {@link ChangeEvent} to all
  631. * registered listeners. Note that the model will fire a change event to all
  632. * of its registered listeners first (with the model as the event source) and
  633. * then the slider will fire another change event to all of its registered
  634. * listeners (this time with the slider as the event source).
  635. *
  636. * @param extent The extent value for this slider.
  637. *
  638. * @see #getExtent()
  639. */
  640. public void setExtent(int extent)
  641. {
  642. sliderModel.setExtent(extent);
  643. }
  644. /**
  645. * Returns the orientation of the slider, either {@link JSlider#HORIZONTAL}
  646. * or {@link JSlider#VERTICAL}.
  647. *
  648. * @return The orientation of the slider.
  649. *
  650. * @see #setOrientation(int)
  651. */
  652. public int getOrientation()
  653. {
  654. return orientation;
  655. }
  656. /**
  657. * Sets the orientation for the slider and sends a
  658. * {@link PropertyChangeEvent} (with the property name "orientation") to all
  659. * registered listeners.
  660. *
  661. * @param orientation the orientation (one of {@link JSlider#HORIZONTAL} or
  662. * {@link JSlider#VERTICAL}).
  663. *
  664. * @throws IllegalArgumentException if <code>orientation</code> is not one of
  665. * the permitted values.
  666. *
  667. * @see #getOrientation()
  668. */
  669. public void setOrientation(int orientation)
  670. {
  671. if (orientation != VERTICAL && orientation != HORIZONTAL)
  672. throw new IllegalArgumentException(
  673. "orientation must be one of: VERTICAL, HORIZONTAL");
  674. if (orientation != this.orientation)
  675. {
  676. int oldOrientation = this.orientation;
  677. this.orientation = orientation;
  678. firePropertyChange("orientation", oldOrientation, this.orientation);
  679. revalidate();
  680. }
  681. }
  682. /**
  683. * Returns the label table for the slider.
  684. *
  685. * @return The label table for the slider (possibly <code>null</code>).
  686. *
  687. * @see #setLabelTable(Dictionary)
  688. */
  689. public Dictionary getLabelTable()
  690. {
  691. return labelTable;
  692. }
  693. /**
  694. * Sets the table of labels for the slider and sends a
  695. * {@link PropertyChangeEvent} (with the property name "labelTable") to all
  696. * registered listeners.
  697. *
  698. * @param table the table of labels (<code>null</code> permitted).
  699. *
  700. * @see #getLabelTable()
  701. */
  702. public void setLabelTable(Dictionary table)
  703. {
  704. if (table != labelTable)
  705. {
  706. Dictionary oldTable = labelTable;
  707. labelTable = table;
  708. updateLabelUIs();
  709. firePropertyChange("labelTable", oldTable, labelTable);
  710. revalidate();
  711. repaint();
  712. }
  713. }
  714. /**
  715. * Resets the UI delegates for the labels in the <code>labelTable</code> to
  716. * the default for the current look and feel.
  717. */
  718. protected void updateLabelUIs()
  719. {
  720. if (labelTable != null)
  721. {
  722. for (Enumeration list = labelTable.elements(); list.hasMoreElements();)
  723. {
  724. Object o = list.nextElement();
  725. if (o instanceof JComponent)
  726. {
  727. JComponent jc = (JComponent) o;
  728. jc.updateUI();
  729. jc.setSize(jc.getPreferredSize());
  730. }
  731. }
  732. }
  733. }
  734. /**
  735. * Creates a hashtable of <code>(Integer, JLabel)</code> pairs that can be
  736. * used as a label table for this slider. The labels will start from the
  737. * slider's minimum and increase by the increment. Each label will have a text
  738. * string indicating its integer value.
  739. *
  740. * @param increment The increment between labels (must be > 0).
  741. *
  742. * @return A hashtable containing the labels.
  743. *
  744. * @throws IllegalArgumentException if <code>increment</code> is not greater
  745. * than zero.
  746. */
  747. public Hashtable createStandardLabels(int increment)
  748. {
  749. return createStandardLabels(increment, sliderModel.getMinimum());
  750. }
  751. /**
  752. * Creates a hashtable of <code>(Integer, JLabel)</code> pairs that can be
  753. * used as a label table for this slider. The labels will start from the
  754. * given start value and increase by the increment. Each label will have a
  755. * text string indicating its integer value.
  756. *
  757. * @param increment The increment between labels (must be > 0).
  758. * @param start The value to start from.
  759. *
  760. * @return A hashtable with the labels and their keys.
  761. *
  762. * @throws IllegalArgumentException if <code>increment</code> is not greater
  763. * than zero, or <code>start</code> is not within the range of the
  764. * model.
  765. */
  766. public Hashtable createStandardLabels(int increment, int start)
  767. {
  768. if (increment <= 0)
  769. throw new IllegalArgumentException("Requires 'increment' > 0.");
  770. if (start < getMinimum() || start > getMaximum())
  771. throw new IllegalArgumentException("The 'start' value is out of range.");
  772. Hashtable table = new Hashtable();
  773. int max = getMaximum();
  774. for (int i = start; i <= max; i += increment)
  775. {
  776. LabelUIResource label = new LabelUIResource(String.valueOf(i),
  777. JLabel.CENTER);
  778. table.put(new Integer(i), label);
  779. }
  780. return table;
  781. }
  782. /**
  783. * Returns the flag that controls whether or not the value scale for the
  784. * slider is inverted (the default value is <code>false</code>).
  785. *
  786. * @return The flag that controls whether or not the value scale for the
  787. * slider is inverted.
  788. *
  789. * @see #setInverted(boolean)
  790. */
  791. public boolean getInverted()
  792. {
  793. return isInverted;
  794. }
  795. /**
  796. * Sets the flag that controls whether or not the value scale for the
  797. * slider is inverted and, if the new flag value is different to the old flag
  798. * value, sends a {@link PropertyChangeEvent} to all registered listeners.
  799. * Typically, a horizontal slider will display a scale that increases from
  800. * left to right, but this is reversed if the 'inverted' flag is set to
  801. * <code>true</code>. Similarly, a vertical slider will display a scale that
  802. * increases from bottom to top, and this is reversed if the 'inverted' flag
  803. * is set to <code>true</code>.
  804. *
  805. * @param inverted the new flag value.
  806. *
  807. * @see #getInverted()
  808. */
  809. public void setInverted(boolean inverted)
  810. {
  811. if (isInverted != inverted)
  812. {
  813. boolean oldInverted = isInverted;
  814. isInverted = inverted;
  815. firePropertyChange("inverted", oldInverted, isInverted);
  816. repaint();
  817. }
  818. }
  819. /**
  820. * Returns the distance between major tick marks along the slider's value
  821. * scale.
  822. *
  823. * @return The amount of units between each major tick mark.
  824. *
  825. * @see #setMajorTickSpacing(int)
  826. */
  827. public int getMajorTickSpacing()
  828. {
  829. return majorTickSpacing;
  830. }
  831. /**
  832. * Sets the distance between major tick marks along the slider's value scale,
  833. * and sends a {@link PropertyChangeEvent} (with the property name
  834. * "majorTickSpacing") to all registered listeners.
  835. *
  836. * @param spacing the distance between major tick marks.
  837. *
  838. * @see #getMajorTickSpacing()
  839. */
  840. public void setMajorTickSpacing(int spacing)
  841. {
  842. if (majorTickSpacing != spacing)
  843. {
  844. int oldSpacing = majorTickSpacing;
  845. majorTickSpacing = spacing;
  846. if (labelTable == null && majorTickSpacing > 0 && getPaintLabels())
  847. setLabelTable(createStandardLabels(majorTickSpacing));
  848. firePropertyChange("majorTickSpacing", oldSpacing, majorTickSpacing);
  849. if (getPaintTicks())
  850. repaint();
  851. }
  852. }
  853. /**
  854. * Returns the distance between minor tick marks along the slider's value
  855. * scale.
  856. *
  857. * @return The distance between minor tick marks along the slider's value
  858. * scale.
  859. *
  860. * @see #setMinorTickSpacing(int)
  861. */
  862. public int getMinorTickSpacing()
  863. {
  864. return minorTickSpacing;
  865. }
  866. /**
  867. * Sets the distance between minor tick marks along the slider's value scale,
  868. * and sends a {@link PropertyChangeEvent} (with the property name
  869. * "minorTickSpacing") to all registered listeners.
  870. *
  871. * @param spacing the distance between minor tick marks.
  872. *
  873. * @see #getMinorTickSpacing()
  874. */
  875. public void setMinorTickSpacing(int spacing)
  876. {
  877. if (minorTickSpacing != spacing)
  878. {
  879. int oldSpacing = minorTickSpacing;
  880. minorTickSpacing = spacing;
  881. firePropertyChange("minorTickSpacing", oldSpacing, minorTickSpacing);
  882. if (getPaintTicks())
  883. repaint();
  884. }
  885. }
  886. /**
  887. * Returns the flag that controls whether the slider thumb will snap to ticks.
  888. * Sliders that snap to ticks will automatically move the thumb to the
  889. * nearest tick mark.
  890. *
  891. * @return <code>true</code> if the slider thumb automatically.
  892. *
  893. * @see #setSnapToTicks(boolean)
  894. */
  895. public boolean getSnapToTicks()
  896. {
  897. return snapToTicks;
  898. }
  899. /**
  900. * Sets the flag that controls whether the slider thumb will snap to ticks
  901. * and sends a {@link PropertyChangeEvent} (with the property name
  902. * 'snapToTicks') to all registered listeners. Sliders that snap to ticks
  903. * will automatically move the thumb to the nearest tick mark.
  904. *
  905. * @param snap the new flag value.
  906. *
  907. * @see #getSnapToTicks()
  908. */
  909. public void setSnapToTicks(boolean snap)
  910. {
  911. if (snap != snapToTicks)
  912. {
  913. snapToTicks = snap;
  914. firePropertyChange("snapToTicks", !snap, snap);
  915. }
  916. }
  917. /**
  918. * Returns the flag that controls whether or not tick marks are painted along
  919. * the slider's value scale.
  920. *
  921. * @return <code>true</code> if tick marks should be painted, and
  922. * <code>false</code> if tick marks should not be painted.
  923. *
  924. * @see #setPaintTicks(boolean)
  925. */
  926. public boolean getPaintTicks()
  927. {
  928. return paintTicks;
  929. }
  930. /**
  931. * Sets the flag that controls whether or not tick marks are painted along
  932. * the slider's value scale, and sends a {@link PropertyChangeEvent} (with
  933. * the property name "paintTicks") to all registered listeners. In
  934. * addition to setting this property to <code>true</code>, one or both of the
  935. * minor tick spacing and major tick spacing attributes must be set to a
  936. * value greater than 0 in order for ticks to be painted.
  937. *
  938. * @param paint Whether ticks will be painted.
  939. *
  940. * @see #getPaintTicks()
  941. */
  942. public void setPaintTicks(boolean paint)
  943. {
  944. if (paint != paintTicks)
  945. {
  946. boolean oldPaintTicks = paintTicks;
  947. paintTicks = paint;
  948. firePropertyChange("paintTicks", oldPaintTicks, paintTicks);
  949. revalidate();
  950. repaint();
  951. }
  952. }
  953. /**
  954. * Returns the flag that controls whether or not the track is painted.
  955. *
  956. * @return Whether the track will be painted.
  957. *
  958. * @see #setPaintTrack(boolean)
  959. */
  960. public boolean getPaintTrack()
  961. {
  962. return paintTrack;
  963. }
  964. /**
  965. * Sets the flag that controls whether or not the track is painted, and
  966. * sends a {@link PropertyChangeEvent} (for the "paintTrack" property) to all
  967. * registered listeners.
  968. *
  969. * @param paint Whether the track will be painted.
  970. *
  971. * @see #getPaintTrack()
  972. */
  973. public void setPaintTrack(boolean paint)
  974. {
  975. if (paintTrack != paint)
  976. {
  977. paintTrack = paint;
  978. firePropertyChange("paintTrack", !paint, paint);
  979. repaint();
  980. }
  981. }
  982. /**
  983. * Returns the flag that controls whether or not labels are painted for the
  984. * tick marks along the slider.
  985. *
  986. * @return Whether labels will be painted.
  987. *
  988. * @see #setPaintLabels(boolean)
  989. */
  990. public boolean getPaintLabels()
  991. {
  992. return paintLabels;
  993. }
  994. /**
  995. * Sets the flag that controls whether or not labels are painted for the
  996. * tick marks along the slider and sends a {@link PropertyChangeEvent} (with
  997. * the property name "paintLabels") to all registered listeners.
  998. *
  999. * @param paint Whether labels will be painted.
  1000. *
  1001. * @see #getPaintLabels()
  1002. */
  1003. public void setPaintLabels(boolean paint)
  1004. {
  1005. if (paint != paintLabels)
  1006. {
  1007. paintLabels = paint;
  1008. if (paint && majorTickSpacing > 0 && labelTable == null)
  1009. setLabelTable(createStandardLabels(majorTickSpacing));
  1010. firePropertyChange("paintLabels", !paint, paint);
  1011. revalidate();
  1012. repaint();
  1013. }
  1014. }
  1015. /**
  1016. * Returns an implementation-dependent string describing the attributes of
  1017. * this <code>JSlider</code>.
  1018. *
  1019. * @return A string describing the attributes of this <code>JSlider</code>
  1020. * (never <code>null</code>).
  1021. */
  1022. protected String paramString()
  1023. {
  1024. String superParamStr = super.paramString();
  1025. CPStringBuilder sb = new CPStringBuilder();
  1026. sb.append(",isInverted=").append(getInverted());
  1027. sb.append(",majorTickSpacing=").append(getMajorTickSpacing());
  1028. sb.append(",minorTickSpacing=").append(getMinorTickSpacing());
  1029. sb.append(",orientation=");
  1030. if (orientation == HORIZONTAL)
  1031. sb.append("HORIZONTAL");
  1032. else
  1033. sb.append("VERTICAL");
  1034. sb.append(",paintLabels=").append(getPaintLabels());
  1035. sb.append(",paintTicks=").append(getPaintTicks());
  1036. sb.append(",paintTrack=").append(getPaintTrack());
  1037. sb.append(",snapToTicks=").append(getSnapToTicks());
  1038. // the following is output by the reference implementation. We don't
  1039. // strictly need to replicate this. Perhaps it has some meaning, but
  1040. // I couldn't determine it yet...
  1041. sb.append(",snapToValue=true");
  1042. return superParamStr + sb.toString();
  1043. }
  1044. /**
  1045. * Returns the object that provides accessibility features for this
  1046. * <code>JSlider</code> component.
  1047. *
  1048. * @return The accessible context (an instance of {@link AccessibleJSlider}).
  1049. */
  1050. public AccessibleContext getAccessibleContext()
  1051. {
  1052. if (accessibleContext == null)
  1053. accessibleContext = new AccessibleJSlider();
  1054. return accessibleContext;
  1055. }
  1056. }