001: /*
002: * $Id: JXDatePickerFormatterFactory.java,v 1.1 2007/08/15 23:28:24 suricate Exp $
003: *
004: * This code comes from the dormant jdnc project at https://jdnc.dev.java.net/. It is licensed with the LGPL
005: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
006: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
007: */
008: package org.jdesktop.swing.calendar;
009:
010: import java.text.ParseException;
011: import java.text.SimpleDateFormat;
012:
013: import javax.swing.JFormattedTextField;
014: import javax.swing.UIManager;
015: import javax.swing.JFormattedTextField.AbstractFormatter;
016: import javax.swing.JFormattedTextField.AbstractFormatterFactory;
017:
018: /**
019: * Default formatter factory for the JXDatePicker component. This factory
020: * creates and returns a formatter that can handle a variety of date formats.
021: *
022: * @author Joshua Outwater
023: */
024: public class JXDatePickerFormatterFactory extends
025: AbstractFormatterFactory {
026: /** Cached formatter */
027: protected AbstractFormatter formatter = null;
028:
029: /**
030: * {@inheritDoc}
031: */
032: public AbstractFormatter getFormatter(JFormattedTextField ftf) {
033: if (formatter == null) {
034: formatter = new JXDatePickerFormatter();
035: }
036: return formatter;
037: }
038:
039: /**
040: * Default formatter class for the JXDatePicker component. This formatter
041: * supports the following three default formats:
042: * <ul>
043: * <li>EEE MM/dd/yyyy (Fri 04/09/2004)
044: * <li>MM/dd/yyyy (04/09/2004)
045: * <li>dd/yy (04/09)
046: * </ul>
047: * These formats are localizable and fields may be re-arranged, such as
048: * swapping the month and day fields. The keys for localizing these fields
049: * are:
050: * <ul>
051: * <li>JXDatePicker.longFormat
052: * <li>JXDatePicker.mediumFormat
053: * <li>JXDatePicker.shortFormat
054: * </ul>
055: * It is important to order the formats in the order of most complex to
056: * least complex as it is possible for less complex formats to match more
057: * complex strings.
058: */
059: private class JXDatePickerFormatter extends
060: JFormattedTextField.AbstractFormatter {
061: private SimpleDateFormat _formats[] = null;
062: private int _formatIndex = 0;
063:
064: public JXDatePickerFormatter() {
065: _formats = new SimpleDateFormat[3];
066: String format = UIManager
067: .getString("JXDatePicker.longFormat");
068: if (format == null) {
069: format = "EEE MM/dd/yyyy";
070: }
071: _formats[0] = new SimpleDateFormat(format);
072:
073: format = UIManager.getString("JXDatePicker.mediumFormat");
074: if (format == null) {
075: format = "MM/dd/yyyy";
076: }
077: _formats[1] = new SimpleDateFormat(format);
078:
079: format = UIManager.getString("JXDatePicker.shortFormat");
080: if (format == null) {
081: format = "MM/dd";
082: }
083: _formats[2] = new SimpleDateFormat(format);
084: }
085:
086: /**
087: * {@inheritDoc}
088: */
089: public Object stringToValue(String text) throws ParseException {
090: Object result = null;
091: ParseException pex = null;
092:
093: if (text == null) {
094: return null;
095: }
096:
097: // Try the current formatter first.
098: try {
099: result = _formats[_formatIndex].parse(text);
100: } catch (ParseException ex) {
101: pex = ex;
102: }
103:
104: // If the current formatter did not work loop through the other
105: // formatters and see if any of them can parse the string passed
106: // in.
107: if (result == null) {
108: for (int i = 0; i < _formats.length; i++) {
109: if (i == _formatIndex) {
110: continue;
111: }
112:
113: try {
114: result = ((SimpleDateFormat) _formats[i])
115: .parse(text);
116:
117: // We got a successful formatter. Update the current
118: // formatter index.
119: _formatIndex = i;
120: pex = null;
121: break;
122: } catch (ParseException ex) {
123: pex = ex;
124: }
125: }
126: }
127:
128: if (pex != null) {
129: throw pex;
130: }
131:
132: return result;
133: }
134:
135: /**
136: * {@inheritDoc}
137: */
138: public String valueToString(Object value) throws ParseException {
139: if (value != null) {
140: return _formats[_formatIndex].format(value);
141: }
142: return null;
143: }
144: }
145: }
|