KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > DateFormatter


1 /*
2  * @(#)DateFormatter.java 1.10 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.awt.event.*;
10 import java.text.*;
11 import java.util.*;
12 import javax.swing.*;
13 import javax.swing.text.*;
14
15 /**
16  * DateFormatter is an <code>InternationalFormatter</code> that does its
17  * formatting by way of an instance of <code>java.text.DateFormat</code>.
18  * <p>
19  * <strong>Warning:</strong>
20  * Serialized objects of this class will not be compatible with
21  * future Swing releases. The current serialization support is
22  * appropriate for short term storage or RMI between applications running
23  * the same version of Swing. As of 1.4, support for long term storage
24  * of all JavaBeans<sup><font size="-2">TM</font></sup>
25  * has been added to the <code>java.beans</code> package.
26  * Please see {@link java.beans.XMLEncoder}.
27  *
28  * @see java.text.DateFormat
29  *
30  * @version 1.5 04/09/01
31  * @since 1.4
32  */

33 public class DateFormatter extends InternationalFormatter JavaDoc {
34     /**
35      * This is shorthand for
36      * <code>new DateFormatter(DateFormat.getDateInstance())</code>.
37      */

38     public DateFormatter() {
39         this(DateFormat.getDateInstance());
40     }
41
42     /**
43      * Returns a DateFormatter configured with the specified
44      * <code>Format</code> instance.
45      *
46      * @param format Format used to dictate legal values
47      */

48     public DateFormatter(DateFormat format) {
49         super(format);
50         setFormat(format);
51     }
52
53     /**
54      * Sets the format that dictates the legal values that can be edited
55      * and displayed.
56      * <p>
57      * If you have used the nullary constructor the value of this property
58      * will be determined for the current locale by way of the
59      * <code>Dateformat.getDateInstance()</code> method.
60      *
61      * @param format DateFormat instance used for converting from/to Strings
62      */

63     public void setFormat(DateFormat format) {
64         super.setFormat(format);
65     }
66
67     /**
68      * Returns the Calendar that <code>DateFormat</code> is associated with,
69      * or if the <code>Format</code> is not a <code>DateFormat</code>
70      * <code>Calendar.getInstance</code> is returned.
71      */

72     private Calendar getCalendar() {
73         Format f = getFormat();
74
75         if (f instanceof DateFormat) {
76             return ((DateFormat)f).getCalendar();
77         }
78         return Calendar.getInstance();
79     }
80
81
82     /**
83      * Returns true, as DateFormatterFilter will support
84      * incrementing/decrementing of the value.
85      */

86     boolean getSupportsIncrement() {
87         return true;
88     }
89
90     /**
91      * Returns the field that will be adjusted by adjustValue.
92      */

93     Object JavaDoc getAdjustField(int start, Map attributes) {
94         Iterator attrs = attributes.keySet().iterator();
95
96         while (attrs.hasNext()) {
97             Object JavaDoc key = attrs.next();
98
99             if ((key instanceof DateFormat.Field) &&
100                 (key == DateFormat.Field.HOUR1 ||
101                  ((DateFormat.Field)key).getCalendarField() != -1)) {
102                 return key;
103             }
104         }
105         return null;
106     }
107
108     /**
109      * Adjusts the Date if FieldPosition identifies a known calendar
110      * field.
111      */

112     Object JavaDoc adjustValue(Object JavaDoc value, Map attributes, Object JavaDoc key,
113                            int direction) throws
114                       BadLocationException JavaDoc, ParseException {
115         if (key != null) {
116             int field;
117
118             // HOUR1 has no corresponding calendar field, thus, map
119
// it to HOUR0 which will give the correct behavior.
120
if (key == DateFormat.Field.HOUR1) {
121                 key = DateFormat.Field.HOUR0;
122             }
123             field = ((DateFormat.Field)key).getCalendarField();
124
125             Calendar calendar = getCalendar();
126
127             if (calendar != null) {
128                 calendar.setTime((Date)value);
129
130                 int fieldValue = calendar.get(field);
131
132                 try {
133                     calendar.add(field, direction);
134                     value = calendar.getTime();
135                 } catch (Throwable JavaDoc th) {
136                     value = null;
137                 }
138                 return value;
139             }
140         }
141         return null;
142     }
143 }
144
Popular Tags