SpinnerDateModel.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* SpinnerDateModel.java --
  2. Copyright (C) 2002, 2004, 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 java.io.Serializable;
  33. import java.util.Calendar;
  34. import java.util.Date;
  35. import javax.swing.event.ChangeEvent;
  36. /**
  37. * A date model used by the {@link JSpinner} component. This implements a
  38. * spinner model for dates, rotating a calendar field such as month, year,
  39. * day, week, hour, minute.
  40. *
  41. * @author Sven de Marothy
  42. * @since 1.4
  43. */
  44. public class SpinnerDateModel extends AbstractSpinnerModel
  45. implements Serializable
  46. {
  47. /** The current date. */
  48. private Calendar date;
  49. /**
  50. * A constraint on the start or earliest permitted date (<code>null</code>
  51. * for no minimum).
  52. */
  53. private Comparable start;
  54. /**
  55. * A constraint on the end or latest permitted date (<code>null</code> for no
  56. * maximum).
  57. */
  58. private Comparable end;
  59. /**
  60. * The calendar field used to calculate the previous or next date.
  61. */
  62. private int calendarField;
  63. /**
  64. * For compatability with Sun's JDK
  65. */
  66. private static final long serialVersionUID = -4802518107105940612L;
  67. /**
  68. * Constructs a <code>SpinnerDateModel</code> using the current date,
  69. * no start or end limit, and {@link Calendar#DAY_OF_MONTH} as the calendar
  70. * field.
  71. */
  72. public SpinnerDateModel()
  73. {
  74. this(new Date(), null, null, Calendar.DAY_OF_MONTH);
  75. }
  76. /**
  77. * Constructs a <code>SpinnerDateModel</code> with the specified value, lower
  78. * and upper bounds, and which spins the specified calendar field.
  79. * <p>
  80. * The <code>start</code> and <code>end</code> limits must have a
  81. * <code>compareTo</code> method that supports instances of {@link Date}, but
  82. * do not themselves need to be instances of {@link Date} (although typically
  83. * they are).
  84. *
  85. * @param value the initial value/date (<code>null</code> not permitted).
  86. * @param start a constraint that specifies the earliest permitted date
  87. * value, or <code>null</code> for no lower limit.
  88. * @param end a constraint that specifies the latest permitted date value,
  89. * or <code>null</code> for no upper limit.
  90. * @param calendarField the <code>Calendar</code> field to spin,
  91. * (Calendar.ZONE_OFFSET and Calendar.DST_OFFSET are invalid)
  92. */
  93. public SpinnerDateModel(Date value, Comparable start, Comparable end,
  94. int calendarField)
  95. {
  96. if (value == null)
  97. throw new IllegalArgumentException("Null 'value' argument.");
  98. if (start != null && start.compareTo(value) > 0)
  99. throw new IllegalArgumentException("Require value on or after start.");
  100. if (end != null && end.compareTo(value) < 0)
  101. throw new IllegalArgumentException("Require value on or before end.");
  102. date = Calendar.getInstance();
  103. date.setTime(value);
  104. this.start = start;
  105. this.end = end;
  106. setCalendarField(calendarField);
  107. }
  108. /**
  109. * Returns the {@link Calendar} field used to calculate the previous and
  110. * next dates in the sequence.
  111. *
  112. * @return The date field code.
  113. */
  114. public int getCalendarField()
  115. {
  116. return calendarField;
  117. }
  118. /**
  119. * Returns the current date/time.
  120. *
  121. * @return The current date/time (never <code>null</code>).
  122. *
  123. * @see #getValue()
  124. */
  125. public Date getDate()
  126. {
  127. return date.getTime();
  128. }
  129. /**
  130. * Returns the lower limit on the date/time value, or <code>null</code> if
  131. * there is no minimum date/time.
  132. *
  133. * @return The lower limit.
  134. *
  135. * @see #setStart(Comparable)
  136. */
  137. public Comparable getStart()
  138. {
  139. return start;
  140. }
  141. /**
  142. * Returns the upper limit on the date/time value, or <code>null</code> if
  143. * there is no maximum date/time.
  144. *
  145. * @return The upper limit.
  146. *
  147. * @see #setEnd(Comparable)
  148. */
  149. public Comparable getEnd()
  150. {
  151. return end;
  152. }
  153. /**
  154. * Returns the current date in the sequence (this method returns the same as
  155. * {@link #getDate()}).
  156. *
  157. * @return The current date (never <code>null</code>).
  158. */
  159. public Object getValue()
  160. {
  161. return date.getTime();
  162. }
  163. /**
  164. * Returns the next date in the sequence, or <code>null</code> if the
  165. * next date is past the upper limit (if one is specified). The current date
  166. * is not changed.
  167. *
  168. * @return The next date, or <code>null</code> if the current value is
  169. * the latest date represented by the model.
  170. *
  171. * @see #getEnd()
  172. */
  173. public Object getNextValue()
  174. {
  175. Calendar nextCal = Calendar.getInstance();
  176. nextCal.setTime(date.getTime());
  177. nextCal.roll(calendarField, true);
  178. Date nextDate = nextCal.getTime();
  179. if (end != null)
  180. if (end.compareTo(nextDate) < 0)
  181. return null;
  182. return nextDate;
  183. }
  184. /**
  185. * Returns the previous date in the sequence, or <code>null</code> if the
  186. * previous date is prior to the lower limit (if one is specified). The
  187. * current date is not changed.
  188. *
  189. * @return The previous date, or <code>null</code> if the current value is
  190. * the earliest date represented by the model.
  191. *
  192. * @see #getStart()
  193. */
  194. public Object getPreviousValue()
  195. {
  196. Calendar prevCal = Calendar.getInstance();
  197. prevCal.setTime(date.getTime());
  198. prevCal.roll(calendarField, false);
  199. Date prevDate = prevCal.getTime();
  200. if (start != null)
  201. if (start.compareTo(prevDate) > 0)
  202. return null;
  203. return prevDate;
  204. }
  205. /**
  206. * Sets the date field to change when calculating the next and previous
  207. * values. It must be a valid {@link Calendar} field, excluding
  208. * {@link Calendar#ZONE_OFFSET} and {@link Calendar#DST_OFFSET}.
  209. *
  210. * @param calendarField the calendar field to set.
  211. *
  212. * @throws IllegalArgumentException if <code>calendarField</code> is not
  213. * a valid code.
  214. */
  215. public void setCalendarField(int calendarField)
  216. {
  217. if (calendarField < 0 || calendarField >= Calendar.FIELD_COUNT
  218. || calendarField == Calendar.ZONE_OFFSET
  219. || calendarField == Calendar.DST_OFFSET)
  220. throw new IllegalArgumentException("Illegal calendarField");
  221. if (this.calendarField != calendarField)
  222. {
  223. this.calendarField = calendarField;
  224. fireStateChanged();
  225. }
  226. }
  227. /**
  228. * Sets the lower limit for the date/time value and, if the new limit is
  229. * different to the old limit, sends a {@link ChangeEvent} to all registered
  230. * listeners. A <code>null</code> value is interpreted as "no lower limit".
  231. * No check is made to ensure that the current date/time is on or after the
  232. * new lower limit - the caller is responsible for ensuring that this
  233. * relationship holds. In addition, the caller should ensure that
  234. * <code>start</code> is {@link Serializable}.
  235. *
  236. * @param start the new lower limit for the date/time value
  237. * (<code>null</code> permitted).
  238. */
  239. public void setStart(Comparable start)
  240. {
  241. if (this.start != start)
  242. {
  243. this.start = start;
  244. fireStateChanged();
  245. }
  246. }
  247. /**
  248. * Sets the upper limit for the date/time value and, if the new limit is
  249. * different to the old limit, sends a {@link ChangeEvent} to all registered
  250. * listeners. A <code>null</code> value is interpreted as "no upper limit".
  251. * No check is made to ensure that the current date/time is on or before the
  252. * new upper limit - the caller is responsible for ensuring that this
  253. * relationship holds. In addition, the caller should ensure that
  254. * <code>end</code> is {@link Serializable}.
  255. *
  256. * @param end the new upper limit for the date/time value (<code>null</code>
  257. * permitted).
  258. */
  259. public void setEnd(Comparable end)
  260. {
  261. if (this.end != end)
  262. {
  263. this.end = end;
  264. fireStateChanged();
  265. }
  266. }
  267. /**
  268. * Sets the current date and, if the new value is different to the old
  269. * value, sends a {@link ChangeEvent} to all registered listeners.
  270. *
  271. * @param value the new date (<code>null</code> not permitted, must be an
  272. * instance of <code>Date</code>).
  273. *
  274. * @throws IllegalArgumentException if <code>value</code> is not an instance
  275. * of <code>Date</code>.
  276. */
  277. public void setValue(Object value)
  278. {
  279. if (! (value instanceof Date) || value == null)
  280. throw new IllegalArgumentException("Value not a date.");
  281. if (!date.getTime().equals(value))
  282. {
  283. date.setTime((Date) value);
  284. fireStateChanged();
  285. }
  286. }
  287. }