001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.swing.datefield;
020:
021: import java.util.Calendar;
022: import java.util.Date;
023:
024: /**
025: * Adaptor for the {@link java.util.Calendar} class. This fixes issues with the
026: * way in which months are handled (January=0) and also provides a way of
027: * setting and retrieving values by String.
028: *
029: * @author Matthew Large
030: * @version $Revision: 1.1 $
031: *
032: */
033: public class CalendarAdaptor {
034:
035: /**
036: * Calendar that this adaptor is wrapping.
037: */
038: private Calendar m_calendar = null;
039:
040: /**
041: *
042: */
043: public CalendarAdaptor() {
044: super ();
045: this .m_calendar = Calendar.getInstance();
046: }
047:
048: /**
049: * Gets the value for a given time field.
050: *
051: * @param nField The given time field
052: * @return The value for the given time field
053: * @see Calendar#get(int)
054: */
055: public int get(int nField) {
056: if (nField == Calendar.MONTH) {
057: return m_calendar.get(nField) + 1;
058: } else {
059: return m_calendar.get(nField);
060: }
061: }
062:
063: /**
064: * Gets the value for a given time field.
065: *
066: * @param nField The given time field
067: * @return The value for the given time field
068: * @see Calendar#get(int)
069: */
070: public String getStringValue(int nField) {
071: int nValue = this .get(nField);
072: if (nField == Calendar.ERA) {
073: if (nValue == 0) {
074: return "BC";
075: } else {
076: return "AD";
077: }
078: } else if (nField != Calendar.YEAR) {
079: if (nValue < 10) {
080: return "0" + Integer.toString(nValue);
081: } else {
082: return Integer.toString(nValue);
083: }
084: } else if (nField == Calendar.YEAR) {
085: String sVal = Integer.toString(nValue);
086: StringBuffer sBuff = new StringBuffer();
087: if (sVal.length() < 8) {
088: sBuff.append(" ");
089: }
090: if (sVal.length() < 7) {
091: sBuff.append(" ");
092: }
093: if (sVal.length() < 6) {
094: sBuff.append(" ");
095: }
096: if (sVal.length() < 5) {
097: sBuff.append(" ");
098: }
099: if (sVal.length() < 4) {
100: sBuff.append("0");
101: }
102: if (sVal.length() < 3) {
103: sBuff.append("0");
104: }
105: if (sVal.length() < 2) {
106: sBuff.append("0");
107: }
108: sBuff.append(sVal);
109: return sBuff.toString();
110: }
111: return null;
112: }
113:
114: /**
115: * Return the maximum value that this field could have, given the current
116: * date. For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field,
117: * the actual maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for
118: * a Hebrew calendar, for some years the actual maximum for MONTH is 12, and
119: * for others 13. The version of this function on Calendar uses an iterative
120: * algorithm to determine the actual maximum value for the field. There is
121: * almost always a more efficient way to accomplish this (in most cases, you
122: * can simply return getMaximum()). GregorianCalendar overrides this function
123: * with a more efficient implementation.
124: *
125: * @param nField The field to determine the maximum of
126: * @return The maximum of the given field for the current date of this Calendar
127: * @see Calendar#getActualMaximum(int)
128: */
129: public int getActualMaximum(int nField) {
130: return m_calendar.getActualMaximum(nField);
131: }
132:
133: /**
134: * Return the minimum value that this field could have, given the current
135: * date. For the Gregorian calendar, this is the same as getMinimum() and
136: * getGreatestMinimum(). The version of this function on Calendar uses an
137: * iterative algorithm to determine the actual minimum value for the field.
138: * There is almost always a more efficient way to accomplish this (in most
139: * cases, you can simply return getMinimum()). GregorianCalendar overrides
140: * this function with a more efficient implementation.
141: *
142: * @param nField The field to determine the minimum of
143: * @return The minimum of the given field for the current date of this Calendar
144: * @see Calendar#getActualMinimum(int)
145: */
146: public int getActualMinimum(int nField) {
147: return m_calendar.getActualMinimum(nField);
148: }
149:
150: /**
151: * Gets the maximum value for the given time field. e.g. for Gregorian
152: * DAY_OF_MONTH, 31.
153: *
154: * @param nField The given time field
155: * @return The maximum value for the given time field
156: * @see Calendar#getMaximum(int)
157: */
158: public int getMaximum(int nField) {
159: return m_calendar.getMaximum(nField);
160: }
161:
162: /**
163: * Gets the minimum value for the given time field. e.g., for Gregorian
164: * DAY_OF_MONTH, 1.
165: *
166: * @param nField The given time field
167: * @return The minimum value for the given time field
168: * @see Calendar#getMinimum(int)
169: */
170: public int getMinimum(int nField) {
171: return m_calendar.getMinimum(nField);
172: }
173:
174: /**
175: * Sets the time field with the given value. Fixes the problem
176: * with month values, this method assumes January=1.
177: *
178: * @param nField The given time field
179: * @param nValue The value to be set for the given time field
180: * @see Calendar#set(int, int)
181: */
182: public void set(int nField, int nValue) {
183: if (nField == Calendar.MONTH) {
184: m_calendar.set(nField, nValue - 1);
185: } else {
186: m_calendar.set(nField, nValue);
187: }
188: }
189:
190: /**
191: * Sets the time field with the given value. Fixes the problem
192: * with month values, this method assumes January="1". Also uses
193: * "BC" and "AD" for the Era field instead of "0" and "1".
194: *
195: * @param nField The given time field
196: * @param sValue The value to be set for the given time field
197: * @see Calendar#set(int, int)
198: */
199: public void setStringValue(int nField, String sValue) {
200: if (nField == Calendar.ERA) {
201: if (sValue.equalsIgnoreCase("bc")) {
202: this .set(nField, 0);
203: } else {
204: this .set(nField, 1);
205: }
206: } else {
207: this .set(nField, Integer.parseInt(sValue.trim()));
208: }
209: }
210:
211: /**
212: * Gets this Calendar's current time.
213: *
214: * @return The current time
215: * @see Calendar#getTime()
216: */
217: public Date getTime() {
218: return m_calendar.getTime();
219: }
220:
221: /**
222: * Sets this Calendar's current time with the given Date.
223: *
224: * Note: Calling setTime() with Date(Long.MAX_VALUE) or Date(Long.MIN_VALUE) may yield incorrect field values from get().
225: *
226: * @param arg0 The given Date
227: * @see Calendar#setTime(java.util.Date)
228: */
229: public void setTime(Date arg0) {
230: m_calendar.setTime(arg0);
231: }
232:
233: /**
234: * Date Arithmetic function. Adds the specified (signed) amount of time
235: * to the given time field, based on the calendar's rules. For example,
236: * to subtract 5 days from the current time of the calendar, you can achieve
237: * it by calling:
238: *
239: * add(Calendar.DATE, -5).
240: *
241: * @param arg0 The time field
242: * @param arg1 The amount of date or time to be added to the field
243: * @see Calendar#add(int, int)
244: */
245: public void add(int arg0, int arg1) {
246: m_calendar.add(arg0, arg1);
247: }
248:
249: }
|