001: /*
002: * @(#)MonthNameConverter.java 5/8/2006
003: *
004: * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.converter;
008:
009: import java.text.DateFormat;
010: import java.text.ParseException;
011: import java.text.SimpleDateFormat;
012: import java.util.Calendar;
013: import java.util.Date;
014:
015: /**
016: * Converter which converts int to month string and converts it back.
017: */
018: public class MonthNameConverter implements ObjectConverter {
019:
020: /**
021: * Default ConverterContext for MonthConverter.
022: */
023: public static ConverterContext CONTEXT = new ConverterContext(
024: "MonthName");
025:
026: /**
027: * 0 -> "1", 1 -> "2", ..., 11 -> "12"
028: */
029: public static final DateFormat CONCISE_FORMAT = new SimpleDateFormat(
030: "M");
031:
032: /**
033: * 0 -> "01", 1 -> "02", ..., 11 -> "12"
034: */
035: public static final DateFormat SHORT_FORMAT = new SimpleDateFormat(
036: "MM");
037:
038: /**
039: * 0 -> "Jan", 1 -> "Feb", ..., 11 -> "Dec"
040: */
041: public static final DateFormat MEDIUM_FORMAT = new SimpleDateFormat(
042: "MMM");
043:
044: /**
045: * 0 -> "January", 1 -> "February", ..., 11 -> "December"
046: */
047: public static final DateFormat LONG_FORMAT = new SimpleDateFormat(
048: "MMMMM");
049:
050: private DateFormat _defaultFormat = MEDIUM_FORMAT;
051:
052: /**
053: * Creates a new CalendarConverter.
054: */
055: public MonthNameConverter() {
056: }
057:
058: public String toString(Object object, ConverterContext context) {
059: if (object == null || !(object instanceof Integer)) {
060: return "";
061: } else {
062: return _defaultFormat
063: .format((getCalendarByMonth((Integer) object)
064: .getTime()));
065: }
066: }
067:
068: final static Calendar CAL = Calendar.getInstance();
069:
070: static {
071: CAL.set(Calendar.DAY_OF_MONTH, 1);
072: }
073:
074: protected Calendar getCalendarByMonth(int month) {
075: CAL.set(Calendar.MONTH, month);
076: return CAL;
077: }
078:
079: public boolean supportToString(Object object,
080: ConverterContext context) {
081: return true;
082: }
083:
084: public Object fromString(String string, ConverterContext context) {
085: Calendar calendar = Calendar.getInstance();
086: try {
087: Date time = _defaultFormat.parse(string);
088: calendar.setTime(time);
089: } catch (ParseException e1) { // if current formatter doesn't work try those default ones.
090: try {
091: Date time = SHORT_FORMAT.parse(string);
092: calendar.setTime(time);
093: } catch (ParseException e2) {
094: try {
095: Date time = MEDIUM_FORMAT.parse(string);
096: calendar.setTime(time);
097: } catch (ParseException e3) {
098: try {
099: Date time = LONG_FORMAT.parse(string);
100: calendar.setTime(time);
101: } catch (ParseException e4) {
102: try {
103: Date time = CONCISE_FORMAT.parse(string);
104: calendar.setTime(time);
105: } catch (ParseException e5) {
106: return null; // nothing works just return null so that old value will be kept.
107: }
108: }
109: }
110: }
111: }
112: return calendar.get(Calendar.MONTH);
113: }
114:
115: public boolean supportFromString(String string,
116: ConverterContext context) {
117: return true;
118: }
119:
120: /**
121: * Gets default format to format a month.
122: *
123: * @return DefaultFormat
124: */
125: public DateFormat getDefaultFormat() {
126: return _defaultFormat;
127: }
128:
129: /**
130: * Sets default format to format a month. Default is {@link #MEDIUM_FORMAT}.
131: *
132: * @param defaultFormat the default format to format the month.
133: */
134: public void setDefaultFormat(DateFormat defaultFormat) {
135: _defaultFormat = defaultFormat;
136: }
137:
138: public static void main(String[] args) {
139: MonthNameConverter converter = new MonthNameConverter();
140: converter.setDefaultFormat(MonthNameConverter.LONG_FORMAT);
141: for (int i = 0; i < 12; i++) {
142: String str = converter.toString(new Integer(i), null);
143: System.out.println(str);
144: System.out.println(converter.fromString(str, null));
145: }
146: }
147: }
|