2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
package javax.swing.text;
|
|
26 |
|
|
27 |
import java.awt.event.*;
|
|
28 |
import java.text.*;
|
|
29 |
import java.util.*;
|
|
30 |
import javax.swing.*;
|
|
31 |
import javax.swing.text.*;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* DateFormatter is an <code>InternationalFormatter</code> that does its
|
|
35 |
* formatting by way of an instance of <code>java.text.DateFormat</code>.
|
|
36 |
* <p>
|
|
37 |
* <strong>Warning:</strong>
|
|
38 |
* Serialized objects of this class will not be compatible with
|
|
39 |
* future Swing releases. The current serialization support is
|
|
40 |
* appropriate for short term storage or RMI between applications running
|
|
41 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
42 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
43 |
* has been added to the <code>java.beans</code> package.
|
|
44 |
* Please see {@link java.beans.XMLEncoder}.
|
|
45 |
*
|
|
46 |
* @see java.text.DateFormat
|
|
47 |
*
|
|
48 |
* @since 1.4
|
|
49 |
*/
|
|
50 |
public class DateFormatter extends InternationalFormatter {
|
|
51 |
/**
|
|
52 |
* This is shorthand for
|
|
53 |
* <code>new DateFormatter(DateFormat.getDateInstance())</code>.
|
|
54 |
*/
|
|
55 |
public DateFormatter() {
|
|
56 |
this(DateFormat.getDateInstance());
|
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Returns a DateFormatter configured with the specified
|
|
61 |
* <code>Format</code> instance.
|
|
62 |
*
|
|
63 |
* @param format Format used to dictate legal values
|
|
64 |
*/
|
|
65 |
public DateFormatter(DateFormat format) {
|
|
66 |
super(format);
|
|
67 |
setFormat(format);
|
|
68 |
}
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Sets the format that dictates the legal values that can be edited
|
|
72 |
* and displayed.
|
|
73 |
* <p>
|
|
74 |
* If you have used the nullary constructor the value of this property
|
|
75 |
* will be determined for the current locale by way of the
|
|
76 |
* <code>Dateformat.getDateInstance()</code> method.
|
|
77 |
*
|
|
78 |
* @param format DateFormat instance used for converting from/to Strings
|
|
79 |
*/
|
|
80 |
public void setFormat(DateFormat format) {
|
|
81 |
super.setFormat(format);
|
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Returns the Calendar that <code>DateFormat</code> is associated with,
|
|
86 |
* or if the <code>Format</code> is not a <code>DateFormat</code>
|
|
87 |
* <code>Calendar.getInstance</code> is returned.
|
|
88 |
*/
|
|
89 |
private Calendar getCalendar() {
|
|
90 |
Format f = getFormat();
|
|
91 |
|
|
92 |
if (f instanceof DateFormat) {
|
|
93 |
return ((DateFormat)f).getCalendar();
|
|
94 |
}
|
|
95 |
return Calendar.getInstance();
|
|
96 |
}
|
|
97 |
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Returns true, as DateFormatterFilter will support
|
|
101 |
* incrementing/decrementing of the value.
|
|
102 |
*/
|
|
103 |
boolean getSupportsIncrement() {
|
|
104 |
return true;
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Returns the field that will be adjusted by adjustValue.
|
|
109 |
*/
|
|
110 |
Object getAdjustField(int start, Map attributes) {
|
|
111 |
Iterator attrs = attributes.keySet().iterator();
|
|
112 |
|
|
113 |
while (attrs.hasNext()) {
|
|
114 |
Object key = attrs.next();
|
|
115 |
|
|
116 |
if ((key instanceof DateFormat.Field) &&
|
|
117 |
(key == DateFormat.Field.HOUR1 ||
|
|
118 |
((DateFormat.Field)key).getCalendarField() != -1)) {
|
|
119 |
return key;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
return null;
|
|
123 |
}
|
|
124 |
|
|
125 |
/**
|
|
126 |
* Adjusts the Date if FieldPosition identifies a known calendar
|
|
127 |
* field.
|
|
128 |
*/
|
|
129 |
Object adjustValue(Object value, Map attributes, Object key,
|
|
130 |
int direction) throws
|
|
131 |
BadLocationException, ParseException {
|
|
132 |
if (key != null) {
|
|
133 |
int field;
|
|
134 |
|
|
135 |
// HOUR1 has no corresponding calendar field, thus, map
|
|
136 |
// it to HOUR0 which will give the correct behavior.
|
|
137 |
if (key == DateFormat.Field.HOUR1) {
|
|
138 |
key = DateFormat.Field.HOUR0;
|
|
139 |
}
|
|
140 |
field = ((DateFormat.Field)key).getCalendarField();
|
|
141 |
|
|
142 |
Calendar calendar = getCalendar();
|
|
143 |
|
|
144 |
if (calendar != null) {
|
|
145 |
calendar.setTime((Date)value);
|
|
146 |
|
|
147 |
int fieldValue = calendar.get(field);
|
|
148 |
|
|
149 |
try {
|
|
150 |
calendar.add(field, direction);
|
|
151 |
value = calendar.getTime();
|
|
152 |
} catch (Throwable th) {
|
|
153 |
value = null;
|
|
154 |
}
|
|
155 |
return value;
|
|
156 |
}
|
|
157 |
}
|
|
158 |
return null;
|
|
159 |
}
|
|
160 |
}
|