1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53:
68: public class JFormattedTextField extends JTextField
69: {
70: private static final long serialVersionUID = 5464657870110180632L;
71:
72:
77: public abstract static class AbstractFormatter implements Serializable
78: {
79: private static final long serialVersionUID = -5193212041738979680L;
80:
81: private JFormattedTextField textField;
82:
83: public AbstractFormatter ()
84: {
85:
86: }
87:
88: protected Object clone ()
89: throws CloneNotSupportedException
90: {
91: throw new InternalError ("not implemented");
92: }
93:
94: protected Action[] getActions ()
95: {
96: return textField.getActions();
97: }
98:
99: protected DocumentFilter getDocumentFilter ()
100: {
101: throw new InternalError ("not implemented");
102: }
103:
104: protected JFormattedTextField getFormattedTextField ()
105: {
106: return textField;
107: }
108:
109: protected NavigationFilter getNavigationFilter ()
110: {
111: return textField.getNavigationFilter();
112: }
113:
114: public void install(JFormattedTextField textField)
115: {
116: if (this.textField != null)
117: uninstall();
118:
119: this.textField = textField;
120: }
121:
122: public void uninstall ()
123: {
124: this.textField = null;
125: }
126:
127: protected void invalidEdit ()
128: {
129: textField.invalidEdit();
130: }
131:
132: protected void setEditValid (boolean valid)
133: {
134: textField.editValid = valid;
135: }
136:
137: public abstract Object stringToValue (String text)
138: throws ParseException;
139:
140: public abstract String valueToString (Object value)
141: throws ParseException;
142: }
143:
144:
148: public abstract static class AbstractFormatterFactory
149: {
150: public AbstractFormatterFactory ()
151: {
152:
153: }
154:
155: public abstract AbstractFormatter getFormatter (JFormattedTextField tf);
156: }
157:
158: static class FormatterFactoryWrapper extends AbstractFormatterFactory
159: {
160: AbstractFormatter formatter;
161:
162: public FormatterFactoryWrapper(AbstractFormatter formatter)
163: {
164: this.formatter = formatter;
165: }
166:
167: public AbstractFormatter getFormatter(JFormattedTextField tf)
168: {
169: return formatter;
170: }
171: }
172:
173: public static final int COMMIT = 0;
174: public static final int COMMIT_OR_REVERT = 1;
175: public static final int REVERT = 2;
176: public static final int PERSIST = 3;
177:
178: private Object value;
179: private int focusLostBehavior = COMMIT_OR_REVERT;
180: private AbstractFormatterFactory formatterFactory;
181:
182: boolean editValid = true;
183:
184: public JFormattedTextField ()
185: {
186: this((AbstractFormatterFactory) null, null);
187: }
188:
189: public JFormattedTextField (Format format)
190: {
191: throw new InternalError ("not implemented");
192: }
193:
194: public JFormattedTextField (AbstractFormatter formatter)
195: {
196: this(new FormatterFactoryWrapper(formatter), null);
197: }
198:
199: public JFormattedTextField (AbstractFormatterFactory factory)
200: {
201: this(factory, null);
202: }
203:
204: public JFormattedTextField (AbstractFormatterFactory factory, Object value)
205: {
206: this.formatterFactory = factory;
207: this.value = value;
208: }
209:
210: public JFormattedTextField (Object value)
211: {
212: this.value = value;
213: }
214:
215: public void commitEdit ()
216: throws ParseException
217: {
218: throw new InternalError ("not implemented");
219: }
220:
221: public Action[] getActions ()
222: {
223:
224: return super.getActions();
225: }
226:
227: public int getFocusLostBehavior()
228: {
229: return focusLostBehavior;
230: }
231:
232: public AbstractFormatter getFormatter ()
233: {
234: if (formatterFactory == null)
235: return null;
236:
237: return formatterFactory.getFormatter(this);
238: }
239:
240: public AbstractFormatterFactory getFormatterFactory ()
241: {
242: return formatterFactory;
243: }
244:
245: public String getUIClassID ()
246: {
247: return "FormattedTextFieldUI";
248: }
249:
250: public Object getValue ()
251: {
252: return value;
253: }
254:
255: protected void invalidEdit ()
256: {
257: UIManager.getLookAndFeel().provideErrorFeedback(this);
258: }
259:
260: public boolean isEditValid ()
261: {
262: return editValid;
263: }
264:
265: protected void processFocusEvent (FocusEvent evt)
266: {
267:
268:
269:
270: super.processFocusEvent(evt);
271: }
272:
273: public void setDocument(Document newDocument)
274: {
275: Document oldDocument = getDocument();
276:
277: if (oldDocument == newDocument)
278: return;
279:
280: super.setDocument(newDocument);
281: }
282:
283: public void setFocusLostBehavior(int behavior)
284: {
285: if (behavior != COMMIT
286: && behavior != COMMIT_OR_REVERT
287: && behavior != PERSIST
288: && behavior != REVERT)
289: throw new IllegalArgumentException("invalid behavior");
290:
291: this.focusLostBehavior = behavior;
292: }
293:
294: protected void setFormatter (AbstractFormatter formatter)
295: {
296: AbstractFormatter oldFormatter = null;
297:
298: if (formatterFactory != null)
299: oldFormatter = formatterFactory.getFormatter(this);
300:
301: if (oldFormatter == formatter)
302: return;
303:
304: setFormatterFactory(new FormatterFactoryWrapper(formatter));
305: firePropertyChange("formatter", oldFormatter, formatter);
306: }
307:
308: public void setFormatterFactory (AbstractFormatterFactory factory)
309: {
310: if (formatterFactory == factory)
311: return;
312:
313: AbstractFormatterFactory oldFactory = formatterFactory;
314: formatterFactory = factory;
315: firePropertyChange("formatterFactory", oldFactory, factory);
316: }
317:
318: public void setValue (Object newValue)
319: {
320: if (value == newValue)
321: return;
322:
323:
324: AbstractFormatter formatter = createFormatter(newValue);
325: try
326: {
327: setText(formatter.valueToString(newValue));
328: }
329: catch (ParseException ex)
330: {
331:
332: }
333:
334: Object oldValue = value;
335: value = newValue;
336: firePropertyChange("value", oldValue, newValue);
337: }
338:
339:
352: AbstractFormatter createFormatter(Object value)
353: {
354: AbstractFormatter formatter = null;
355: if (formatterFactory != null
356: && formatterFactory.getFormatter(this) != null)
357: formatter = formatterFactory.getFormatter(this);
358: else
359: {
360: if (value instanceof Date)
361: formatter = new DateFormatter();
362: else
363: formatter = new DefaultFormatter();
364: }
365: return formatter;
366: }
367: }