001: /*
002: * @(#) DateConverter.java
003: *
004: * Copyright 2002 - 2003 JIDE Software. All rights reserved.
005: */
006: package com.jidesoft.converter;
007:
008: import java.text.DateFormat;
009: import java.text.ParseException;
010: import java.text.SimpleDateFormat;
011: import java.util.Calendar;
012: import java.util.Date;
013:
014: /**
015: * Converter which converts Date to String and converts it back.
016: */
017: public class DateConverter implements ObjectConverter {
018:
019: public static final ConverterContext DATETIME_CONTEXT = new ConverterContext(
020: "DateTime");
021: public static final ConverterContext TIME_CONTEXT = new ConverterContext(
022: "Time");
023: public static final ConverterContext DATE_CONTEXT = new ConverterContext(
024: "Date");
025:
026: private DateFormat _shortFormat = SimpleDateFormat
027: .getDateInstance(DateFormat.SHORT);
028: private DateFormat _mediumFormat = SimpleDateFormat
029: .getDateInstance(DateFormat.MEDIUM);
030: private DateFormat _longFormat = SimpleDateFormat
031: .getDateInstance(DateFormat.LONG);
032:
033: private DateFormat _defaultFormat = SimpleDateFormat
034: .getDateInstance(DateFormat.DEFAULT);
035:
036: private DateFormat _shortDatetimeFormat = SimpleDateFormat
037: .getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
038: private DateFormat _mediumDatetimeFormat = SimpleDateFormat
039: .getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
040: private DateFormat _longDatetimeFormat = SimpleDateFormat
041: .getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
042:
043: private DateFormat _defaultDatetimeFormat = SimpleDateFormat
044: .getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT);
045:
046: private DateFormat _shortTimeFormat = SimpleDateFormat
047: .getTimeInstance(DateFormat.SHORT);
048: private DateFormat _mediumTimeFormat = SimpleDateFormat
049: .getTimeInstance(DateFormat.MEDIUM);
050: private DateFormat _longTimeFormat = SimpleDateFormat
051: .getTimeInstance(DateFormat.LONG);
052:
053: private DateFormat _defaultTimeFormat = SimpleDateFormat
054: .getTimeInstance(DateFormat.DEFAULT);
055:
056: /**
057: * Creates a DateConverter.
058: */
059: public DateConverter() {
060: }
061:
062: /**
063: * Converts the object to String. The object can be a Calendar, a Date or a Number. As long as the DateFormat
064: * can format it correctly, it will be converted to a String. If the object is already a String, we will
065: * return it directly as it is.
066: *
067: * @param object
068: * @param context
069: * @return the string
070: */
071: public String toString(Object object, ConverterContext context) {
072: if (object == null) {
073: return "";
074: } else {
075: if (object instanceof Calendar) {
076: object = ((Calendar) object).getTime();
077: }
078:
079: if (object instanceof Date || object instanceof Number) {
080: if (context != null
081: && context.getUserObject() instanceof DateFormat) {
082: return ((DateFormat) context.getUserObject())
083: .format(object);
084: } else if (DATETIME_CONTEXT.equals(context)) {
085: return _defaultDatetimeFormat.format(object);
086: } else if (TIME_CONTEXT.equals(context)) {
087: return _defaultTimeFormat.format(object);
088: } else {
089: return _defaultFormat.format(object);
090: }
091: } else if (object instanceof String) {
092: return (String) object;
093: } else {
094: return null;
095: }
096: }
097: }
098:
099: public boolean supportToString(Object object,
100: ConverterContext context) {
101: return true;
102: }
103:
104: /**
105: * Converts from a String to a Date.
106: *
107: * @param string the string to be converted.
108: * @param context the context. It could be DATETIME_CONTEXT, DATE_CONTEXT or TIME_CONTEXT.
109: * @return the Date. If the string is null or empty, null will be returned. If the string cannot be parsed as a date, the string itself will be returned.
110: */
111: public Object fromString(String string, ConverterContext context) {
112: if (string == null || string.trim().length() == 0) {
113: return null;
114: }
115:
116: try {
117: Object userObject = context != null ? context
118: .getUserObject() : null;
119: if (userObject instanceof DateFormat) {
120: return ((DateFormat) userObject).parse(string);
121: } else if (DATETIME_CONTEXT.equals(context)) {
122: return _defaultDatetimeFormat.parse(string);
123: } else if (TIME_CONTEXT.equals(context)) {
124: return _defaultTimeFormat.parse(string);
125: } else {
126: return _defaultFormat.parse(string);
127: }
128: } catch (ParseException e1) { // if current formatter doesn't work try those default ones.
129: if (DATETIME_CONTEXT.equals(context)) {
130: try {
131: return _shortDatetimeFormat.parse(string);
132: } catch (ParseException e2) {
133: try {
134: return _mediumDatetimeFormat.parse(string);
135: } catch (ParseException e3) {
136: try {
137: return _longDatetimeFormat.parse(string);
138: } catch (ParseException e4) {
139: return string; // nothing works just return null so that old value will be kept.
140: }
141: }
142: }
143: } else if (TIME_CONTEXT.equals(context)) {
144: try {
145: return _shortTimeFormat.parse(string);
146: } catch (ParseException e2) {
147: try {
148: return _mediumTimeFormat.parse(string);
149: } catch (ParseException e3) {
150: try {
151: return _longTimeFormat.parse(string);
152: } catch (ParseException e4) {
153: return string; // nothing works just return null so that old value will be kept.
154: }
155: }
156: }
157: } else {
158: try {
159: return _shortFormat.parse(string);
160: } catch (ParseException e2) {
161: try {
162: return _mediumFormat.parse(string);
163: } catch (ParseException e3) {
164: try {
165: return _longFormat.parse(string);
166: } catch (ParseException e4) {
167: return string; // nothing works just return null so that old value will be kept.
168: }
169: }
170: }
171: }
172: }
173: }
174:
175: public boolean supportFromString(String string,
176: ConverterContext context) {
177: return true;
178: }
179:
180: /**
181: * Gets DefaultFormat to format an calendar.
182: *
183: * @return DefaultFormat
184: */
185: public DateFormat getDefaultFormat() {
186: return _defaultFormat;
187: }
188:
189: /**
190: * Sets DefaultFormat to format an calendar.
191: *
192: * @param defaultFormat
193: */
194: public void setDefaultFormat(DateFormat defaultFormat) {
195: _defaultFormat = defaultFormat;
196: }
197:
198: /**
199: * Gets DefaultTimeFormat to format an calendar. This is used
200: * only when context is {@link #TIME_CONTEXT}.
201: *
202: * @return DefaultTimeFormat
203: */
204: public DateFormat getDefaultTimeFormat() {
205: return _defaultTimeFormat;
206: }
207:
208: /**
209: * Sets DefaultTimeFormat to format an calendar. This is used
210: * only when context is {@link #TIME_CONTEXT}.
211: *
212: * @param defaultTimeFormat
213: */
214: public void setDefaultTimeFormat(DateFormat defaultTimeFormat) {
215: _defaultTimeFormat = defaultTimeFormat;
216: }
217:
218: /**
219: * Gets DefaultDatetimeFormat to format an calendar. This is used
220: * only when context is {@link #DATETIME_CONTEXT}.
221: *
222: * @return DefaultDatetimeFormat
223: */
224: public DateFormat getDefaultDatetimeFormat() {
225: return _defaultDatetimeFormat;
226: }
227:
228: /**
229: * Sets DefaultDatetimeFormat to format an calendar. This is used
230: * only when context is {@link #DATETIME_CONTEXT}.
231: *
232: * @param defaultDatetimeFormat
233: */
234: public void setDefaultDatetimeFormat(
235: DateFormat defaultDatetimeFormat) {
236: _defaultDatetimeFormat = defaultDatetimeFormat;
237: }
238: }
|