001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.io.Serializable;
023: import java.util.Calendar;
024: import java.util.Date;
025:
026: import org.apache.harmony.awt.text.TextUtils;
027:
028: import org.apache.harmony.x.swing.internal.nls.Messages;
029:
030: public class SpinnerDateModel extends AbstractSpinnerModel implements
031: Serializable {
032:
033: private static final int DEFAULT_CALENDAR_FIELD = Calendar.DAY_OF_MONTH;
034: private Date value;
035: private Comparable start;
036: private Comparable end;
037: private int calendarField;
038:
039: public SpinnerDateModel() {
040: this (new Date(), null, null, DEFAULT_CALENDAR_FIELD);
041: }
042:
043: public SpinnerDateModel(final Date value, final Comparable start,
044: final Comparable end, final int calendarField) {
045: if (value == null) {
046: throw new IllegalArgumentException(Messages.getString(
047: "swing.03", "value")); //$NON-NLS-1$ //$NON-NLS-2$
048: }
049: if (start != null && start.compareTo(value) > 0) {
050: throw new IllegalArgumentException(Messages
051: .getString("swing.59")); //$NON-NLS-1$
052: }
053: if (end != null && end.compareTo(value) < 0) {
054: throw new IllegalArgumentException(Messages
055: .getString("swing.59")); //$NON-NLS-1$
056: }
057: if (!isValidCalendarField(calendarField)) {
058: throw new IllegalArgumentException(Messages
059: .getString("swing.5A")); //$NON-NLS-1$
060: }
061:
062: this .value = value;
063: this .start = start;
064: this .end = end;
065: this .calendarField = calendarField;
066: }
067:
068: public void setStart(final Comparable start) {
069: if (this .start != start) {
070: this .start = start;
071: fireStateChanged();
072: }
073: }
074:
075: public Comparable getStart() {
076: return start;
077: }
078:
079: public void setEnd(final Comparable end) {
080: if (this .end != end) {
081: this .end = end;
082: fireStateChanged();
083: }
084: }
085:
086: public Comparable getEnd() {
087: return end;
088: }
089:
090: public void setCalendarField(final int calendarField) {
091: if (!isValidCalendarField(calendarField)) {
092: throw new IllegalArgumentException(Messages
093: .getString("swing.5A")); //$NON-NLS-1$
094: }
095: if (this .calendarField != calendarField) {
096: this .calendarField = calendarField;
097: fireStateChanged();
098: }
099: }
100:
101: public int getCalendarField() {
102: return calendarField;
103: }
104:
105: public Object getNextValue() {
106: return TextUtils.getNextValue(value, calendarField, end);
107: }
108:
109: public Object getPreviousValue() {
110: return TextUtils.getPreviousValue(value, calendarField, start);
111: }
112:
113: public Date getDate() {
114: return (Date) value.clone();
115: }
116:
117: public Object getValue() {
118: return getDate();
119: }
120:
121: public void setValue(final Object value) {
122: if (!(value instanceof Date)) {
123: throw new IllegalArgumentException(Messages
124: .getString("swing.5B")); //$NON-NLS-1$
125: }
126: if (this .value != value) {
127: this .value = (Date) value;
128: fireStateChanged();
129: }
130: }
131:
132: private boolean isValidCalendarField(final int calendarField) {
133: switch (calendarField) {
134: case Calendar.ERA:
135: case Calendar.YEAR:
136: case Calendar.MONTH:
137: case Calendar.WEEK_OF_YEAR:
138: case Calendar.WEEK_OF_MONTH:
139: case Calendar.DAY_OF_MONTH:
140: case Calendar.DAY_OF_YEAR:
141: case Calendar.DAY_OF_WEEK:
142: case Calendar.DAY_OF_WEEK_IN_MONTH:
143: case Calendar.AM_PM:
144: case Calendar.HOUR:
145: case Calendar.HOUR_OF_DAY:
146: case Calendar.MINUTE:
147: case Calendar.SECOND:
148: case Calendar.MILLISECOND:
149: return true;
150: default:
151: return false;
152: }
153: }
154: }
|