OSDN Git Service

2005-04-19 Roman Kennke <roman@kennke.org>
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / JFormattedTextField.java
1 /* JFormattedTextField.java --
2    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package javax.swing;
40
41 import java.awt.event.FocusEvent;
42 import java.io.Serializable;
43 import java.text.Format;
44 import java.text.ParseException;
45
46 import javax.swing.text.Document;
47 import javax.swing.text.DocumentFilter;
48 import javax.swing.text.NavigationFilter;
49
50 /**
51  * @author Michael Koch
52  * @since 1.4
53  */
54 public class JFormattedTextField extends JTextField
55 {
56   private static final long serialVersionUID = 5464657870110180632L;
57
58   public abstract static class AbstractFormatter implements Serializable
59   {
60     private static final long serialVersionUID = -5193212041738979680L;
61     
62     private JFormattedTextField textField;
63     
64     public AbstractFormatter ()
65     {
66       //Do nothing here.
67     }
68
69     protected Object clone ()
70       throws CloneNotSupportedException
71     {
72       throw new InternalError ("not implemented");
73     }
74
75     protected Action[] getActions ()
76     {
77       return textField.getActions();
78     }
79
80     protected DocumentFilter getDocumentFilter ()
81     {
82       throw new InternalError ("not implemented");
83     }
84
85     protected JFormattedTextField getFormattedTextField ()
86     {
87       return textField;
88     }
89
90     protected NavigationFilter getNavigationFilter ()
91     {
92       return textField.getNavigationFilter();
93     }
94
95     public void install(JFormattedTextField textField)
96     {
97       if (this.textField != null)
98         uninstall();
99       
100       this.textField = textField;
101     }
102
103     public void uninstall ()
104     {
105       this.textField = null;
106     }
107
108     protected void invalidEdit ()
109     {
110       textField.invalidEdit();
111     }
112
113     protected void setEditValid (boolean valid)
114     {
115       textField.editValid = valid;
116     }
117
118     public abstract Object stringToValue (String text)
119       throws ParseException;
120
121     public abstract String valueToString (Object value)
122       throws ParseException;
123   }
124   
125   public abstract static class AbstractFormatterFactory
126   {
127     public AbstractFormatterFactory ()
128     {
129       // Do nothing here.
130     }
131
132     public abstract AbstractFormatter getFormatter (JFormattedTextField tf);
133   }
134
135   static class FormatterFactoryWrapper extends AbstractFormatterFactory
136   {
137     AbstractFormatter formatter;
138
139     public FormatterFactoryWrapper(AbstractFormatter formatter)
140     {
141       this.formatter = formatter;
142     }
143
144     public AbstractFormatter getFormatter(JFormattedTextField tf)
145     {
146       return formatter;
147     }
148   }
149
150   public static final int COMMIT = 0;
151   public static final int COMMIT_OR_REVERT = 1;
152   public static final int REVERT = 2;
153   public static final int PERSIST = 3;
154
155   private Object value;
156   private int focusLostBehavior = COMMIT_OR_REVERT;
157   private AbstractFormatterFactory formatterFactory;
158   private boolean editValid = true;
159   
160   public JFormattedTextField ()
161   {
162     this((AbstractFormatterFactory) null, null);
163   }
164
165   public JFormattedTextField (Format format)
166   {
167     throw new InternalError ("not implemented");
168   }
169
170   public JFormattedTextField (AbstractFormatter formatter)
171   {
172     this(new FormatterFactoryWrapper(formatter), null);
173   }
174
175   public JFormattedTextField (AbstractFormatterFactory factory)
176   {
177     this(factory, null);
178   }
179
180   public JFormattedTextField (AbstractFormatterFactory factory, Object value)
181   {
182     this.formatterFactory = factory;
183     this.value = value;
184   }
185
186   public JFormattedTextField (Object value)
187   {
188     this.value = value;
189   }
190
191   public void commitEdit ()
192     throws ParseException
193   {
194     throw new InternalError ("not implemented");
195   }
196
197   public Action[] getActions ()
198   {
199     throw new InternalError ("not implemented");
200   }
201
202   public int getFocusLostBehavior()
203   {
204     return focusLostBehavior;
205   }
206
207   public AbstractFormatter getFormatter ()
208   {
209     if (formatterFactory == null)
210       return null;
211     
212     return formatterFactory.getFormatter(this);
213   }
214
215   public AbstractFormatterFactory getFormatterFactory ()
216   {
217     return formatterFactory;
218   }
219
220   public String getUIClassID ()
221   {
222     return "FormattedTextFieldUI";
223   }
224
225   public Object getValue ()
226   {
227     return value;
228   }
229
230   protected void invalidEdit ()
231   {
232     UIManager.getLookAndFeel().provideErrorFeedback(this);
233   }
234
235   public boolean isEditValid ()
236   {
237     return editValid;
238   }
239
240   protected void processFocusEvent (FocusEvent evt)
241   {
242     throw new InternalError ("not implemented");
243   }
244
245   public void setDocument(Document newDocument)
246   {
247     Document oldDocument = getDocument();
248
249     if (oldDocument == newDocument)
250       return;
251     
252     super.setDocument(newDocument);
253   }
254
255   public void setFocusLostBehavior(int behavior)
256   {
257     if (behavior != COMMIT
258         && behavior != COMMIT_OR_REVERT
259         && behavior != PERSIST
260         && behavior != REVERT)
261       throw new IllegalArgumentException("invalid behavior");
262
263     this.focusLostBehavior = behavior;
264   }
265
266   protected void setFormatter (AbstractFormatter formatter)
267   {
268     AbstractFormatter oldFormatter = null;
269     
270     if (formatterFactory != null)
271       oldFormatter = formatterFactory.getFormatter(this);
272
273     if (oldFormatter == formatter)
274       return;
275
276     setFormatterFactory(new FormatterFactoryWrapper(formatter));
277     firePropertyChange("formatter", oldFormatter, formatter);
278   }
279
280   public void setFormatterFactory (AbstractFormatterFactory factory)
281   {
282     if (formatterFactory == factory)
283       return;
284     
285     AbstractFormatterFactory oldFactory = formatterFactory;
286     formatterFactory = factory;
287     firePropertyChange("formatterFactory", oldFactory, factory);
288   }
289
290   public void setValue (Object newValue)
291   {
292     if (value == newValue)
293       return;
294     
295     Object oldValue = value;
296     value = newValue;
297     firePropertyChange("value", oldValue, newValue);
298   }
299 }