GNU Classpath (0.20) | |
Frames | No Frames |
1: /* SpinnerDateModel.java -- 2: Copyright (C) 2002, 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.swing; 40: 41: import java.io.Serializable; 42: import java.util.Calendar; 43: import java.util.Date; 44: 45: /** 46: * SpinnerDateModel 47: * 48: * Implements a SpinnerModel for dates, rotating a calendar field such as 49: * month, year, day, week, hour, minute. 50: * 51: * @author Sven de Marothy 52: * @version 0.1 (first implementation) 53: */ 54: public class SpinnerDateModel extends AbstractSpinnerModel 55: implements Serializable 56: { 57: private Calendar date; 58: private Comparable start; 59: private Comparable end; 60: private int calendarField; 61: 62: /** 63: * For compatability with Sun's JDK 64: * FIXME: Which fields should be serialized? 65: */ 66: private static final long serialVersionUID = -4802518107105940612L; 67: 68: /** 69: * Constructs a SpinnerDateModel using the current date, 70: * no start or end limit, and Calendar.DAY_OF_MONTH as the calendar field. 71: */ 72: public SpinnerDateModel() 73: { 74: this(new Date(), null, null, Calendar.DAY_OF_MONTH); 75: } 76: 77: /** 78: * Constructs a SpinnerDateModel which spins a given calendar field, 79: * using a given date and start and end date limits. 80: * @param value - the initial Date value 81: * @param start - start limit, as a Date object, or <code>null</code> 82: * for no lower limit. 83: * @param end - end limit, or <code>null</code> for no upper limit. 84: * @param calendarField - the <code>Calendar</code> field to spin, 85: * (Calendar.ZONE_OFFSET and Calendar.DST_OFFSET are invalid) 86: */ 87: public SpinnerDateModel(Date value, Comparable start, Comparable end, 88: int calendarField) 89: { 90: date = Calendar.getInstance(); 91: date.setTime(value); 92: this.start = start; 93: this.end = end; 94: setCalendarField(calendarField); 95: } 96: 97: /** 98: * Returns the value of the Calendar field to spin. 99: */ 100: public int getCalendarField() 101: { 102: return calendarField; 103: } 104: 105: /** 106: * Returns the current date in the sequence. 107: * @return a <code>Date</code> object. 108: */ 109: public Date getDate() 110: { 111: return date.getTime(); 112: } 113: 114: /** 115: * Returns the starting limit of the SpinnerModel. 116: * @return a Date object, or <code>null</code> if there is no limit. 117: */ 118: public Comparable getStart() 119: { 120: return start; 121: } 122: 123: /** 124: * Returns the end limit of the SpinnerModel. 125: * @return a Date object, or <code>null</code> if there is no limit. 126: */ 127: public Comparable getEnd() 128: { 129: return end; 130: } 131: 132: /** 133: * Returns the current date in the sequence, 134: * this method returns the same as <code>getDate()</code>. 135: * @return a <code>Date</code> object. 136: */ 137: public Object getValue() 138: { 139: return date.getTime(); 140: } 141: 142: /** 143: * Returns the next date in the sequence, or <code>null</code> if the 144: * next date is equal to or past the end limit. 145: * @return a Date object, or <code>null</code>. 146: */ 147: public Object getNextValue() 148: { 149: Calendar nextCal = Calendar.getInstance(); 150: nextCal.setTime(date.getTime()); 151: nextCal.roll(calendarField, true); 152: Date nextDate = nextCal.getTime(); 153: if (end != null) 154: if (end.compareTo(nextDate) < 0) 155: return null; 156: return nextDate; 157: } 158: 159: /** 160: * Returns the previous date in the sequence, or <code>null</code> if the 161: * next date is equal to or past the end limit. 162: * @return a Date object, or <code>null</code>. 163: */ 164: public Object getPreviousValue() 165: { 166: Calendar prevCal = Calendar.getInstance(); 167: prevCal.setTime(date.getTime()); 168: prevCal.roll(calendarField, false); 169: Date prevDate = prevCal.getTime(); 170: if (end != null) 171: if (end.compareTo(prevDate) > 0) 172: return null; 173: return prevDate; 174: } 175: 176: /** 177: * Sets the date field to change. It must be a valid Calendar field, 178: * excluding Calendar.ZONE_OFFSET and Calendar.DST_OFFSET. 179: * @param calendarField - the calendar field to set. 180: */ 181: public void setCalendarField(int calendarField) 182: { 183: if (calendarField < 0 || calendarField >= Calendar.FIELD_COUNT 184: || calendarField == Calendar.ZONE_OFFSET 185: || calendarField == Calendar.DST_OFFSET) 186: throw new IllegalArgumentException("Illegal calendarField"); 187: 188: if (this.calendarField != calendarField) 189: { 190: this.calendarField = calendarField; 191: fireStateChanged(); 192: } 193: } 194: 195: /** 196: * Sets the starting date limit for the sequence. 197: * 198: * @param start - a Date object of the limit date, 199: * or <code>null</code> for no limit. 200: */ 201: public void setStart(Comparable start) 202: { 203: if (this.start != start) 204: { 205: this.start = start; 206: fireStateChanged(); 207: } 208: } 209: 210: /** 211: * Sets the end date limit for the sequence. 212: * 213: * @param end - a Date object of the limit date, 214: * or <code>null</code> for no limit. 215: */ 216: public void setEnd(Comparable end) 217: { 218: if (this.end != end) 219: { 220: this.end = end; 221: fireStateChanged(); 222: } 223: } 224: 225: /** 226: * Sets the current date in the sequence. 227: * 228: * @param value - a Date object. 229: */ 230: public void setValue(Object value) 231: { 232: if (! (value instanceof Date) || value == null) 233: throw new IllegalArgumentException("Value not a date."); 234: date.setTime((Date) value); 235: fireStateChanged(); 236: } 237: }
GNU Classpath (0.20) |