DefaultBoundedRangeModel.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* DefaultBoundedRangeModel.java -- Default implementation
  2. of BoundedRangeModel.
  3. Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package javax.swing;
  33. import java.io.IOException;
  34. import java.io.ObjectInputStream;
  35. import java.io.ObjectOutputStream;
  36. import java.io.Serializable;
  37. import java.util.EventListener;
  38. import javax.swing.event.ChangeEvent;
  39. import javax.swing.event.ChangeListener;
  40. import javax.swing.event.EventListenerList;
  41. /**
  42. * The default implementation of <code>BoundedRangeModel</code>.
  43. *
  44. * @author Andrew Selkirk (aselkirk@sympatico.ca)
  45. * @author Sascha Brawer (brawer@dandelis.ch)
  46. */
  47. public class DefaultBoundedRangeModel
  48. implements BoundedRangeModel, Serializable
  49. {
  50. /**
  51. * The identifier of this class in object serialization. Verified
  52. * using the serialver tool of Sun J2SE 1.4.1_01.
  53. */
  54. private static final long serialVersionUID = 5034068491295259790L;
  55. /**
  56. * An event that is sent to all registered {@link ChangeListener}s
  57. * when the state of this range model has changed.
  58. *
  59. * <p>The event object is created on demand, the first time it
  60. * is actually needed.</p>
  61. *
  62. * @see #fireStateChanged()
  63. */
  64. protected transient ChangeEvent changeEvent;
  65. /**
  66. * The list of the currently registered EventListeners.
  67. */
  68. protected EventListenerList listenerList = new EventListenerList();
  69. /**
  70. * The current value of the range model, which is always between
  71. * {@link #minimum} and ({@link #maximum} - {@link #extent}). In a
  72. * scroll bar visualization of a {@link BoundedRangeModel}, the
  73. * <code>value</code> is displayed as the position of the thumb.
  74. */
  75. private int value;
  76. /**
  77. * The current extent of the range model, which is a number greater
  78. * than or equal to zero. In a scroll bar visualization of a {@link
  79. * BoundedRangeModel}, the <code>extent</code> is displayed as the
  80. * size of the thumb.
  81. */
  82. private int extent;
  83. /**
  84. * The current minimum value of the range model, which is always
  85. * less than or equal to {@link #maximum}.
  86. */
  87. private int minimum;
  88. /**
  89. * The current maximum value of the range model, which is always
  90. * greater than or equal to {@link #minimum}.
  91. */
  92. private int maximum;
  93. /**
  94. * A property that indicates whether the value of this {@link
  95. * BoundedRangeModel} is going to change in the immediate future.
  96. */
  97. private boolean isAdjusting;
  98. /**
  99. * Constructs a <code>DefaultBoundedRangeModel</code> with default
  100. * values for the properties. The properties <code>value</code>,
  101. * <code>extent</code> and <code>minimum</code> will be initialized
  102. * to zero; <code>maximum</code> will be set to 100; the property
  103. * <code>valueIsAdjusting</code> will be <code>false</code>.
  104. */
  105. public DefaultBoundedRangeModel()
  106. {
  107. // The fields value, extent, minimum have the default value 0, and
  108. // isAdjusting is already false. These fields no not need to be
  109. // set explicitly.
  110. maximum = 100;
  111. }
  112. /**
  113. * Constructs a <code>DefaultBoundedRangeModel</code> with the
  114. * specified values for some properties.
  115. *
  116. * @param value the initial value of the range model, which must be
  117. * a number between <code>minimum</code> and <code>(maximum -
  118. * extent)</code>. In a scroll bar visualization of a {@link
  119. * BoundedRangeModel}, the <code>value</code> is displayed as the
  120. * position of the thumb.
  121. * @param extent the initial extent of the range model, which is a
  122. * number greater than or equal to zero. In a scroll bar
  123. * visualization of a {@link BoundedRangeModel}, the
  124. * <code>extent</code> is displayed as the size of the thumb.
  125. * @param minimum the initial minimal value of the range model.
  126. * @param maximum the initial maximal value of the range model.
  127. *
  128. * @throws IllegalArgumentException if the following condition is
  129. * not satisfied: <code>minimum &lt;= value &lt;= value + extent &lt;=
  130. * maximum</code>.
  131. */
  132. public DefaultBoundedRangeModel(int value, int extent, int minimum,
  133. int maximum)
  134. {
  135. if (!(minimum <= value && extent >= 0 && (value + extent) <= maximum))
  136. throw new IllegalArgumentException();
  137. this.value = value;
  138. this.extent = extent;
  139. this.minimum = minimum;
  140. this.maximum = maximum;
  141. // The isAdjusting field already has a false value by default.
  142. }
  143. /**
  144. * Returns a string with all relevant properties of this range
  145. * model.
  146. *
  147. * @return a string representing the object
  148. */
  149. public String toString()
  150. {
  151. return getClass().getName()
  152. + "[value=" + value
  153. + ", extent=" + extent
  154. + ", min=" + minimum
  155. + ", max=" + maximum
  156. + ", adj=" + isAdjusting
  157. + ']';
  158. }
  159. /**
  160. * Returns the current value of this bounded range model. In a
  161. * scroll bar visualization of a {@link BoundedRangeModel}, the
  162. * <code>value</code> is displayed as the position of the thumb.
  163. *
  164. * @return the value
  165. */
  166. public int getValue()
  167. {
  168. return value;
  169. }
  170. /**
  171. * Changes the current value of this bounded range model. In a
  172. * scroll bar visualization of a {@link BoundedRangeModel}, the
  173. * <code>value</code> is displayed as the position of the thumb;
  174. * changing the <code>value</code> of a scroll bar's model
  175. * thus moves the thumb to a different position.
  176. *
  177. * @param value the value
  178. */
  179. public void setValue(int value)
  180. {
  181. value = Math.max(minimum, value);
  182. if (value + extent > maximum)
  183. value = maximum - extent;
  184. if (value != this.value)
  185. {
  186. this.value = value;
  187. fireStateChanged();
  188. }
  189. }
  190. /**
  191. * Returns the current extent of this bounded range model, which is
  192. * a number greater than or equal to zero. In a scroll bar
  193. * visualization of a {@link BoundedRangeModel}, the
  194. * <code>extent</code> is displayed as the size of the thumb.
  195. *
  196. * @return the extent
  197. */
  198. public int getExtent()
  199. {
  200. return extent;
  201. }
  202. /**
  203. * Changes the current extent of this bounded range model. In a
  204. * scroll bar visualization of a {@link BoundedRangeModel}, the
  205. * <code>extent</code> is displayed as the size of the thumb.
  206. *
  207. * @param extent the new extent of the range model, which is a
  208. * number greater than or equal to zero.
  209. */
  210. public void setExtent(int extent)
  211. {
  212. extent = Math.max(extent, 0);
  213. if (value + extent > maximum)
  214. extent = maximum - value;
  215. if (extent != this.extent)
  216. {
  217. this.extent = extent;
  218. fireStateChanged();
  219. }
  220. }
  221. /**
  222. * Returns the current minimal value of this bounded range model.
  223. */
  224. public int getMinimum()
  225. {
  226. return minimum;
  227. }
  228. /**
  229. * Changes the current minimal value of this bounded range model.
  230. *
  231. * @param minimum the new minimal value.
  232. */
  233. public void setMinimum(int minimum)
  234. {
  235. int value, maximum;
  236. maximum = Math.max(minimum, this.maximum);
  237. value = Math.max(minimum, this.value);
  238. setRangeProperties(value, extent, minimum, maximum, isAdjusting);
  239. }
  240. /**
  241. * Returns the current maximal value of this bounded range model.
  242. *
  243. * @return the maximum
  244. */
  245. public int getMaximum()
  246. {
  247. return maximum;
  248. }
  249. /**
  250. * Changes the current maximal value of this bounded range model.
  251. *
  252. * @param maximum the new maximal value.
  253. */
  254. public void setMaximum(int maximum)
  255. {
  256. int value, extent, minimum;
  257. minimum = Math.min(this.minimum, maximum);
  258. extent = Math.min(this.extent, maximum - minimum);
  259. value = Math.min(this.value, maximum - extent);
  260. setRangeProperties(value, extent, minimum, maximum, isAdjusting);
  261. }
  262. /**
  263. * Returns whether or not the value of this bounded range model is
  264. * going to change in the immediate future. Scroll bars set this
  265. * property to <code>true</code> while the thumb is being dragged
  266. * around; when the mouse is relased, they set the property to
  267. * <code>false</code> and post a final {@link ChangeEvent}.
  268. *
  269. * @return <code>true</code> if the value will change soon again;
  270. * <code>false</code> if the value will probably not change soon.
  271. */
  272. public boolean getValueIsAdjusting()
  273. {
  274. return isAdjusting;
  275. }
  276. /**
  277. * Specifies whether or not the value of this bounded range model is
  278. * going to change in the immediate future. Scroll bars set this
  279. * property to <code>true</code> while the thumb is being dragged
  280. * around; when the mouse is relased, they set the property to
  281. * <code>false</code>.
  282. *
  283. * @param isAdjusting <code>true</code> if the value will change
  284. * soon again; <code>false</code> if the value will probably not
  285. * change soon.
  286. */
  287. public void setValueIsAdjusting(boolean isAdjusting)
  288. {
  289. if (isAdjusting == this.isAdjusting)
  290. return;
  291. this.isAdjusting = isAdjusting;
  292. fireStateChanged();
  293. }
  294. /**
  295. * Sets all properties.
  296. *
  297. * @param value the new value of the range model. In a scroll bar
  298. * visualization of a {@link BoundedRangeModel}, the
  299. * <code>value</code> is displayed as the position of the thumb.
  300. * @param extent the new extent of the range model, which is a
  301. * number greater than or equal to zero. In a scroll bar
  302. * visualization of a {@link BoundedRangeModel}, the
  303. * <code>extent</code> is displayed as the size of the thumb.
  304. * @param minimum the new minimal value of the range model.
  305. * @param maximum the new maximal value of the range model.
  306. * @param isAdjusting whether or not the value of this bounded range
  307. * model is going to change in the immediate future. Scroll bars set
  308. * this property to <code>true</code> while the thumb is being
  309. * dragged around; when the mouse is relased, they set the property
  310. * to <code>false</code>.
  311. */
  312. public void setRangeProperties(int value, int extent, int minimum,
  313. int maximum, boolean isAdjusting)
  314. {
  315. minimum = Math.min(Math.min(minimum, maximum), value);
  316. maximum = Math.max(value, maximum);
  317. if (extent + value > maximum)
  318. extent = maximum - value;
  319. extent = Math.max(0, extent);
  320. if ((value == this.value)
  321. && (extent == this.extent)
  322. && (minimum == this.minimum)
  323. && (maximum == this.maximum)
  324. && (isAdjusting == this.isAdjusting))
  325. return;
  326. this.value = value;
  327. this.extent = extent;
  328. this.minimum = minimum;
  329. this.maximum = maximum;
  330. this.isAdjusting = isAdjusting;
  331. fireStateChanged();
  332. }
  333. /**
  334. * Subscribes a ChangeListener to state changes.
  335. *
  336. * @param listener the listener to be subscribed.
  337. */
  338. public void addChangeListener(ChangeListener listener)
  339. {
  340. listenerList.add(ChangeListener.class, listener);
  341. }
  342. /**
  343. * Cancels the subscription of a ChangeListener.
  344. *
  345. * @param listener the listener to be unsubscribed.
  346. */
  347. public void removeChangeListener(ChangeListener listener)
  348. {
  349. listenerList.remove(ChangeListener.class, listener);
  350. }
  351. /**
  352. * Sends a {@link ChangeEvent} to any registered {@link
  353. * ChangeListener}s.
  354. *
  355. * @see #addChangeListener(ChangeListener)
  356. * @see #removeChangeListener(ChangeListener)
  357. */
  358. protected void fireStateChanged()
  359. {
  360. ChangeListener[] listeners = getChangeListeners();
  361. if (changeEvent == null)
  362. changeEvent = new ChangeEvent(this);
  363. for (int i = listeners.length - 1; i >= 0; --i)
  364. listeners[i].stateChanged(changeEvent);
  365. }
  366. /**
  367. * Retrieves the current listeners of the specified class.
  368. *
  369. * @param listenerType the class of listeners; usually {@link
  370. * ChangeListener}<code>.class</code>.
  371. *
  372. * @return an array with the currently subscribed listeners, or
  373. * an empty array if there are currently no listeners.
  374. *
  375. * @since 1.3
  376. */
  377. public <T extends EventListener> T[] getListeners(Class<T> listenerType)
  378. {
  379. return listenerList.getListeners(listenerType);
  380. }
  381. /**
  382. * Returns all <code>ChangeListeners</code> that are currently
  383. * subscribed for changes to this
  384. * <code>DefaultBoundedRangeModel</code>.
  385. *
  386. * @return an array with the currently subscribed listeners, or
  387. * an empty array if there are currently no listeners.
  388. *
  389. * @since 1.4
  390. */
  391. public ChangeListener[] getChangeListeners()
  392. {
  393. return (ChangeListener[]) getListeners(ChangeListener.class);
  394. }
  395. /**
  396. * Provides serialization support.
  397. *
  398. * @param stream the output stream (<code>null</code> not permitted).
  399. *
  400. * @throws IOException if there is an I/O error.
  401. */
  402. private void writeObject(ObjectOutputStream stream)
  403. throws IOException
  404. {
  405. stream.defaultWriteObject();
  406. }
  407. /**
  408. * Provides serialization support.
  409. *
  410. * @param stream the input stream (<code>null</code> not permitted).
  411. *
  412. * @throws IOException if there is an I/O error.
  413. * @throws ClassNotFoundException if there is a classpath problem.
  414. */
  415. private void readObject(ObjectInputStream stream)
  416. throws ClassNotFoundException, IOException
  417. {
  418. stream.defaultReadObject();
  419. listenerList = new EventListenerList();
  420. }
  421. }