|
27 | 27 | */ |
28 | 28 | package javax.swing.text; |
29 | 29 |
|
30 | | -//TODO import java.text.DateFormat; |
31 | | -//import java.text.Format; |
32 | | -//import java.text.ParseException; |
33 | | -//import java.util.Calendar; |
34 | | -//import java.util.Date; |
35 | | -//import java.util.Iterator; |
36 | | -//import java.util.Map; |
| 30 | +import java.text.DateFormat; |
| 31 | +import java.text.Format; |
| 32 | +import java.text.ParseException; |
| 33 | +import java.util.Calendar; |
| 34 | +import java.util.Date; |
| 35 | +import java.util.Iterator; |
| 36 | +import java.util.Map; |
37 | 37 |
|
38 | 38 | /** |
39 | 39 | * DateFormatter is an <code>InternationalFormatter</code> that does its |
|
53 | 53 | * @since 1.4 |
54 | 54 | */ |
55 | 55 | public class DateFormatter extends InternationalFormatter { |
56 | | -// /** |
57 | | -// * This is shorthand for |
58 | | -// * <code>new DateFormatter(DateFormat.getDateInstance())</code>. |
59 | | -// */ |
60 | | -// public DateFormatter() { |
61 | | -// this(DateFormat.getDateInstance()); |
62 | | -// } |
63 | | -// |
64 | | -// /** |
65 | | -// * Returns a DateFormatter configured with the specified |
66 | | -// * <code>Format</code> instance. |
67 | | -// * |
68 | | -// * @param format Format used to dictate legal values |
69 | | -// */ |
70 | | -// public DateFormatter(DateFormat format) { |
71 | | -// super(format); |
72 | | -// setFormat(format); |
73 | | -// } |
74 | | -// |
75 | | -// /** |
76 | | -// * Sets the format that dictates the legal values that can be edited |
77 | | -// * and displayed. |
78 | | -// * <p> |
79 | | -// * If you have used the nullary constructor the value of this property |
80 | | -// * will be determined for the current locale by way of the |
81 | | -// * <code>Dateformat.getDateInstance()</code> method. |
82 | | -// * |
83 | | -// * @param format DateFormat instance used for converting from/to Strings |
84 | | -// */ |
85 | | -// public void setFormat(DateFormat format) { |
86 | | -// super.setFormat(format); |
87 | | -// } |
88 | | -// |
89 | | -// /** |
90 | | -// * Returns the Calendar that <code>DateFormat</code> is associated with, |
91 | | -// * or if the <code>Format</code> is not a <code>DateFormat</code> |
92 | | -// * <code>Calendar.getInstance</code> is returned. |
93 | | -// */ |
94 | | -// private Calendar getCalendar() { |
95 | | -// Format f = getFormat(); |
96 | | -// |
97 | | -// if (f instanceof DateFormat) { |
98 | | -// return ((DateFormat)f).getCalendar(); |
99 | | -// } |
100 | | -// return Calendar.getInstance(); |
101 | | -// } |
102 | | -// |
103 | | -// |
104 | | -// /** |
105 | | -// * Returns true, as DateFormatterFilter will support |
106 | | -// * incrementing/decrementing of the value. |
107 | | -// */ |
108 | | -// boolean getSupportsIncrement() { |
109 | | -// return true; |
110 | | -// } |
111 | | -// |
112 | | -// /** |
113 | | -// * Returns the field that will be adjusted by adjustValue. |
114 | | -// */ |
115 | | -// Object getAdjustField(int start, Map attributes) { |
116 | | -// Iterator attrs = attributes.keySet().iterator(); |
117 | | -// |
118 | | -// while (attrs.hasNext()) { |
119 | | -// Object key = attrs.next(); |
120 | | -// |
121 | | -// if ((key instanceof DateFormat.Field) && |
122 | | -// (key == DateFormat.Field.HOUR1 || |
123 | | -// ((DateFormat.Field)key).getCalendarField() != -1)) { |
124 | | -// return key; |
125 | | -// } |
126 | | -// } |
127 | | -// return null; |
128 | | -// } |
129 | | -// |
130 | | -// /** |
131 | | -// * Adjusts the Date if FieldPosition identifies a known calendar |
132 | | -// * field. |
133 | | -// */ |
134 | | -// Object adjustValue(Object value, Map attributes, Object key, |
135 | | -// int direction) throws |
136 | | -// BadLocationException, ParseException { |
137 | | -// if (key != null) { |
138 | | -// int field; |
139 | | -// |
140 | | -// // HOUR1 has no corresponding calendar field, thus, map |
141 | | -// // it to HOUR0 which will give the correct behavior. |
142 | | -// if (key == DateFormat.Field.HOUR1) { |
143 | | -// key = DateFormat.Field.HOUR0; |
144 | | -// } |
145 | | -// field = ((DateFormat.Field)key).getCalendarField(); |
146 | | -// |
147 | | -// Calendar calendar = getCalendar(); |
148 | | -// |
149 | | -// if (calendar != null) { |
150 | | -// calendar.setTime((Date)value); |
151 | | -// |
152 | | -// int fieldValue = calendar.get(field); |
153 | | -// |
154 | | -// try { |
155 | | -// calendar.add(field, direction); |
156 | | -// value = calendar.getTime(); |
157 | | -// } catch (Throwable th) { |
158 | | -// value = null; |
159 | | -// } |
160 | | -// return value; |
161 | | -// } |
162 | | -// } |
163 | | -// return null; |
164 | | -// } |
| 56 | + /** |
| 57 | + * This is shorthand for |
| 58 | + * <code>new DateFormatter(DateFormat.getDateInstance())</code>. |
| 59 | + */ |
| 60 | + public DateFormatter() { |
| 61 | + this(DateFormat.getDateInstance()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns a DateFormatter configured with the specified |
| 66 | + * <code>Format</code> instance. |
| 67 | + * |
| 68 | + * @param format Format used to dictate legal values |
| 69 | + */ |
| 70 | + public DateFormatter(DateFormat format) { |
| 71 | + super(format); |
| 72 | + setFormat(format); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sets the format that dictates the legal values that can be edited |
| 77 | + * and displayed. |
| 78 | + * <p> |
| 79 | + * If you have used the nullary constructor the value of this property |
| 80 | + * will be determined for the current locale by way of the |
| 81 | + * <code>Dateformat.getDateInstance()</code> method. |
| 82 | + * |
| 83 | + * @param format DateFormat instance used for converting from/to Strings |
| 84 | + */ |
| 85 | + public void setFormat(DateFormat format) { |
| 86 | + super.setFormat(format); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns the Calendar that <code>DateFormat</code> is associated with, |
| 91 | + * or if the <code>Format</code> is not a <code>DateFormat</code> |
| 92 | + * <code>Calendar.getInstance</code> is returned. |
| 93 | + */ |
| 94 | + private Calendar getCalendar() { |
| 95 | + Format f = getFormat(); |
| 96 | + |
| 97 | + if (f instanceof DateFormat) { |
| 98 | + return ((DateFormat)f).getCalendar(); |
| 99 | + } |
| 100 | + return Calendar.getInstance(); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns true, as DateFormatterFilter will support |
| 106 | + * incrementing/decrementing of the value. |
| 107 | + */ |
| 108 | + boolean getSupportsIncrement() { |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns the field that will be adjusted by adjustValue. |
| 114 | + */ |
| 115 | + Object getAdjustField(int start, Map attributes) { |
| 116 | + Iterator attrs = attributes.keySet().iterator(); |
| 117 | + |
| 118 | + while (attrs.hasNext()) { |
| 119 | + Object key = attrs.next(); |
| 120 | + |
| 121 | + if ((key instanceof DateFormat.Field) && |
| 122 | + (key == DateFormat.Field.HOUR1 || |
| 123 | + ((DateFormat.Field)key).getCalendarField() != -1)) { |
| 124 | + return key; |
| 125 | + } |
| 126 | + } |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Adjusts the Date if FieldPosition identifies a known calendar |
| 132 | + * field. |
| 133 | + */ |
| 134 | + Object adjustValue(Object value, Map attributes, Object key, |
| 135 | + int direction) throws |
| 136 | + BadLocationException, ParseException { |
| 137 | + if (key != null) { |
| 138 | + int field; |
| 139 | + |
| 140 | + // HOUR1 has no corresponding calendar field, thus, map |
| 141 | + // it to HOUR0 which will give the correct behavior. |
| 142 | + if (key == DateFormat.Field.HOUR1) { |
| 143 | + key = DateFormat.Field.HOUR0; |
| 144 | + } |
| 145 | + field = ((DateFormat.Field)key).getCalendarField(); |
| 146 | + |
| 147 | + Calendar calendar = getCalendar(); |
| 148 | + |
| 149 | + if (calendar != null) { |
| 150 | + calendar.setTime((Date)value); |
| 151 | + |
| 152 | + int fieldValue = calendar.get(field); |
| 153 | + |
| 154 | + try { |
| 155 | + calendar.add(field, direction); |
| 156 | + value = calendar.getTime(); |
| 157 | + } catch (Throwable th) { |
| 158 | + value = null; |
| 159 | + } |
| 160 | + return value; |
| 161 | + } |
| 162 | + } |
| 163 | + return null; |
| 164 | + } |
165 | 165 | } |
0 commit comments