KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > calendar > JXDatePickerFormatterFactory


1 /*
2  * $Id: JXDatePickerFormatterFactory.java,v 1.2 2004/09/24 23:54:31 dmouse Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package org.jdesktop.swing.calendar;
8
9 import java.text.ParseException JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
11 import javax.swing.JFormattedTextField JavaDoc;
12 import javax.swing.JFormattedTextField.AbstractFormatter;
13 import javax.swing.JFormattedTextField.AbstractFormatterFactory;
14 import javax.swing.UIManager JavaDoc;
15
16 /**
17  * Default formatter factory for the JXDatePicker component. This factory
18  * creates and returns a formatter that can handle a variety of date formats.
19  *
20  * @author Joshua Outwater
21  */

22 public class JXDatePickerFormatterFactory extends AbstractFormatterFactory {
23     /** Cached formatter */
24     protected AbstractFormatter formatter = null;
25
26     /**
27      * {@inheritDoc}
28      */

29     public AbstractFormatter getFormatter(JFormattedTextField JavaDoc ftf) {
30         if (formatter == null) {
31             formatter = new JXDatePickerFormatter();
32         }
33         return formatter;
34     }
35
36     /**
37      * Default formatter class for the JXDatePicker component. This formatter
38      * supports the following three default formats:
39      * <ul>
40      * <li>EEE MM/dd/yyyy (Fri 04/09/2004)
41      * <li>MM/dd/yyyy (04/09/2004)
42      * <li>dd/yy (04/09)
43      * </ul>
44      * These formats are localizable and fields may be re-arranged, such as
45      * swapping the month and day fields. The keys for localizing these fields
46      * are:
47      * <ul>
48      * <li>JXDatePicker.longFormat
49      * <li>JXDatePicker.mediumFormat
50      * <li>JXDatePicker.shortFormat
51      * </ul>
52      * It is important to order the formats in the order of most complex to
53      * least complex as it is possible for less complex formats to match more
54      * complex strings.
55      */

56     private class JXDatePickerFormatter extends
57             JFormattedTextField.AbstractFormatter JavaDoc {
58         private SimpleDateFormat JavaDoc _formats[] = null;
59         private int _formatIndex = 0;
60         
61         public JXDatePickerFormatter() {
62             _formats = new SimpleDateFormat JavaDoc[3];
63             String JavaDoc format = UIManager.getString("JXDatePicker.longFormat");
64             if (format == null) {
65                 format = "EEE MM/dd/yyyy";
66             }
67             _formats[0] = new SimpleDateFormat JavaDoc(format);
68
69             format = UIManager.getString("JXDatePicker.mediumFormat");
70             if (format == null) {
71                 format = "MM/dd/yyyy";
72             }
73             _formats[1] = new SimpleDateFormat JavaDoc(format);
74
75             format = UIManager.getString("JXDatePicker.shortFormat");
76             if (format == null) {
77                 format = "MM/dd";
78             }
79             _formats[2] = new SimpleDateFormat JavaDoc(format);
80         }
81
82         /**
83          * {@inheritDoc}
84          */

85         public Object JavaDoc stringToValue(String JavaDoc text) throws ParseException JavaDoc {
86             Object JavaDoc result = null;
87             ParseException JavaDoc pex = null;
88
89             if (text == null || text.trim().length() == 0) {
90                 return null;
91             }
92
93             // If the current formatter did not work loop through the other
94
// formatters and see if any of them can parse the string passed
95
// in.
96
if (result == null) {
97                 for (int i = 0; i < _formats.length; i++) {
98                     try {
99                         result = ((SimpleDateFormat JavaDoc)_formats[i]).parse(text);
100
101                         // We got a successful formatter. Update the current
102
// formatter index.
103
_formatIndex = i;
104                         pex = null;
105                         break;
106                     } catch (ParseException JavaDoc ex) {
107                         pex = ex;
108                     }
109                 }
110             }
111
112             if (pex != null) {
113                 throw pex;
114             }
115
116             return result;
117         }
118
119         /**
120          * {@inheritDoc}
121          */

122         public String JavaDoc valueToString(Object JavaDoc value) throws ParseException JavaDoc {
123             if (value != null) {
124                 return _formats[_formatIndex].format(value);
125             }
126             return null;
127         }
128     }
129 }
130
Popular Tags