History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/javax/swing/text/DefaultFormatterFactory.java (1 of 18352 files). Show all

Unified · Split

java/pkg/javax/swing/text/DefaultFormatterFactory.java+313−0
@@ -0,0 +1,313 @@
1+/*
2+ * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
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
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
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+ *
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.
24+ */
25+package javax.swing.text;
26+
27+import java.io.Serializable;
28+import java.text.ParseException;
29+import javax.swing.JFormattedTextField;
30+
31+/**
32+ * An implementation of
33+ * <code>JFormattedTextField.AbstractFormatterFactory</code>.
34+ * <code>DefaultFormatterFactory</code> allows specifying a number of
35+ * different <code>JFormattedTextField.AbstractFormatter</code>s that are to
36+ * be used.
37+ * The most important one is the default one
38+ * (<code>setDefaultFormatter</code>). The default formatter will be used
39+ * if a more specific formatter could not be found. The following process
40+ * is used to determine the appropriate formatter to use.
41+ * <ol>
42+ * <li>Is the passed in value null? Use the null formatter.
43+ * <li>Does the <code>JFormattedTextField</code> have focus? Use the edit
44+ * formatter.
45+ * <li>Otherwise, use the display formatter.
46+ * <li>If a non-null <code>AbstractFormatter</code> has not been found, use
47+ * the default formatter.
48+ * </ol>
49+ * <p>
50+ * The following code shows how to configure a
51+ * <code>JFormattedTextField</code> with two
52+ * <code>JFormattedTextField.AbstractFormatter</code>s, one for display and
53+ * one for editing.
54+ * <pre>
55+ * JFormattedTextField.AbstractFormatter editFormatter = ...;
56+ * JFormattedTextField.AbstractFormatter displayFormatter = ...;
57+ * DefaultFormatterFactory factory = new DefaultFormatterFactory(
58+ * displayFormatter, displayFormatter, editFormatter);
59+ * JFormattedTextField tf = new JFormattedTextField(factory);
60+ * </pre>
61+ * <p>
62+ * <strong>Warning:</strong>
63+ * Serialized objects of this class will not be compatible with
64+ * future Swing releases. The current serialization support is
65+ * appropriate for short term storage or RMI between applications running
66+ * the same version of Swing. As of 1.4, support for long term storage
67+ * of all JavaBeans<sup><font size="-2">TM</font></sup>
68+ * has been added to the <code>java.beans</code> package.
69+ * Please see {@link java.beans.XMLEncoder}.
70+ *
71+ * @see javax.swing.JFormattedTextField
72+ *
73+ * @since 1.4
74+ */
75+public class DefaultFormatterFactory extends JFormattedTextField.AbstractFormatterFactory implements Serializable {
76+ /**
77+ * Default <code>AbstractFormatter</code> to use if a more specific one has
78+ * not been specified.
79+ */
80+ private JFormattedTextField.AbstractFormatter defaultFormat;
81+
82+ /**
83+ * <code>JFormattedTextField.AbstractFormatter</code> to use for display.
84+ */
85+ private JFormattedTextField.AbstractFormatter displayFormat;
86+
87+ /**
88+ * <code>JFormattedTextField.AbstractFormatter</code> to use for editing.
89+ */
90+ private JFormattedTextField.AbstractFormatter editFormat;
91+
92+ /**
93+ * <code>JFormattedTextField.AbstractFormatter</code> to use if the value
94+ * is null.
95+ */
96+ private JFormattedTextField.AbstractFormatter nullFormat;
97+
98+
99+ public DefaultFormatterFactory() {
100+ }
101+
102+ /**
103+ * Creates a <code>DefaultFormatterFactory</code> with the specified
104+ * <code>JFormattedTextField.AbstractFormatter</code>.
105+ *
106+ * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
107+ * if a more specific
108+ * JFormattedTextField.AbstractFormatter can not be
109+ * found.
110+ */
111+ public DefaultFormatterFactory(JFormattedTextField.
112+ AbstractFormatter defaultFormat) {
113+ this(defaultFormat, null);
114+ }
115+
116+ /**
117+ * Creates a <code>DefaultFormatterFactory</code> with the specified
118+ * <code>JFormattedTextField.AbstractFormatter</code>s.
119+ *
120+ * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
121+ * if a more specific
122+ * JFormattedTextField.AbstractFormatter can not be
123+ * found.
124+ * @param displayFormat JFormattedTextField.AbstractFormatter to be used
125+ * when the JFormattedTextField does not have focus.
126+ */
127+ public DefaultFormatterFactory(
128+ JFormattedTextField.AbstractFormatter defaultFormat,
129+ JFormattedTextField.AbstractFormatter displayFormat) {
130+ this(defaultFormat, displayFormat, null);
131+ }
132+
133+ /**
134+ * Creates a DefaultFormatterFactory with the specified
135+ * JFormattedTextField.AbstractFormatters.
136+ *
137+ * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
138+ * if a more specific
139+ * JFormattedTextField.AbstractFormatter can not be
140+ * found.
141+ * @param displayFormat JFormattedTextField.AbstractFormatter to be used
142+ * when the JFormattedTextField does not have focus.
143+ * @param editFormat JFormattedTextField.AbstractFormatter to be used
144+ * when the JFormattedTextField has focus.
145+ */
146+ public DefaultFormatterFactory(
147+ JFormattedTextField.AbstractFormatter defaultFormat,
148+ JFormattedTextField.AbstractFormatter displayFormat,
149+ JFormattedTextField.AbstractFormatter editFormat) {
150+ this(defaultFormat, displayFormat, editFormat, null);
151+ }
152+
153+ /**
154+ * Creates a DefaultFormatterFactory with the specified
155+ * JFormattedTextField.AbstractFormatters.
156+ *
157+ * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
158+ * if a more specific
159+ * JFormattedTextField.AbstractFormatter can not be
160+ * found.
161+ * @param displayFormat JFormattedTextField.AbstractFormatter to be used
162+ * when the JFormattedTextField does not have focus.
163+ * @param editFormat JFormattedTextField.AbstractFormatter to be used
164+ * when the JFormattedTextField has focus.
165+ * @param nullFormat JFormattedTextField.AbstractFormatter to be used
166+ * when the JFormattedTextField has a null value.
167+ */
168+ public DefaultFormatterFactory(
169+ JFormattedTextField.AbstractFormatter defaultFormat,
170+ JFormattedTextField.AbstractFormatter displayFormat,
171+ JFormattedTextField.AbstractFormatter editFormat,
172+ JFormattedTextField.AbstractFormatter nullFormat) {
173+ this.defaultFormat = defaultFormat;
174+ this.displayFormat = displayFormat;
175+ this.editFormat = editFormat;
176+ this.nullFormat = nullFormat;
177+ }
178+
179+ /**
180+ * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use as
181+ * a last resort, eg in case a display, edit or null
182+ * <code>JFormattedTextField.AbstractFormatter</code> has not been
183+ * specified.
184+ *
185+ * @param atf JFormattedTextField.AbstractFormatter used if a more
186+ * specific is not specified
187+ */
188+ public void setDefaultFormatter(JFormattedTextField.AbstractFormatter atf){
189+ defaultFormat = atf;
190+ }
191+
192+ /**
193+ * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
194+ * as a last resort, eg in case a display, edit or null
195+ * <code>JFormattedTextField.AbstractFormatter</code>
196+ * has not been specified.
197+ *
198+ * @return JFormattedTextField.AbstractFormatter used if a more specific
199+ * one is not specified.
200+ */
201+ public JFormattedTextField.AbstractFormatter getDefaultFormatter() {
202+ return defaultFormat;
203+ }
204+
205+ /**
206+ * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
207+ * the <code>JFormattedTextField</code> is not being edited and either
208+ * the value is not-null, or the value is null and a null formatter has
209+ * has not been specified.
210+ *
211+ * @param atf JFormattedTextField.AbstractFormatter to use when the
212+ * JFormattedTextField does not have focus
213+ */
214+ public void setDisplayFormatter(JFormattedTextField.AbstractFormatter atf){
215+ displayFormat = atf;
216+ }
217+
218+ /**
219+ * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
220+ * if the <code>JFormattedTextField</code> is not being edited and either
221+ * the value is not-null, or the value is null and a null formatter has
222+ * has not been specified.
223+ *
224+ * @return JFormattedTextField.AbstractFormatter to use when the
225+ * JFormattedTextField does not have focus
226+ */
227+ public JFormattedTextField.AbstractFormatter getDisplayFormatter() {
228+ return displayFormat;
229+ }
230+
231+ /**
232+ * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
233+ * the <code>JFormattedTextField</code> is being edited and either
234+ * the value is not-null, or the value is null and a null formatter has
235+ * has not been specified.
236+ *
237+ * @param atf JFormattedTextField.AbstractFormatter to use when the
238+ * component has focus
239+ */
240+ public void setEditFormatter(JFormattedTextField.AbstractFormatter atf) {
241+ editFormat = atf;
242+ }
243+
244+ /**
245+ * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
246+ * if the <code>JFormattedTextField</code> is being edited and either
247+ * the value is not-null, or the value is null and a null formatter has
248+ * has not been specified.
249+ *
250+ * @return JFormattedTextField.AbstractFormatter to use when the
251+ * component has focus
252+ */
253+ public JFormattedTextField.AbstractFormatter getEditFormatter() {
254+ return editFormat;
255+ }
256+
257+ /**
258+ * Sets the formatter to use if the value of the JFormattedTextField is
259+ * null.
260+ *
261+ * @param atf JFormattedTextField.AbstractFormatter to use when
262+ * the value of the JFormattedTextField is null.
263+ */
264+ public void setNullFormatter(JFormattedTextField.AbstractFormatter atf) {
265+ nullFormat = atf;
266+ }
267+
268+ /**
269+ * Returns the formatter to use if the value is null.
270+ *
271+ * @return JFormattedTextField.AbstractFormatter to use when the value is
272+ * null
273+ */
274+ public JFormattedTextField.AbstractFormatter getNullFormatter() {
275+ return nullFormat;
276+ }
277+
278+ /**
279+ * Returns either the default formatter, display formatter, editor
280+ * formatter or null formatter based on the state of the
281+ * JFormattedTextField.
282+ *
283+ * @param source JFormattedTextField requesting
284+ * JFormattedTextField.AbstractFormatter
285+ * @return JFormattedTextField.AbstractFormatter to handle
286+ * formatting duties.
287+ */
288+ public JFormattedTextField.AbstractFormatter getFormatter(
289+ JFormattedTextField source) {
290+ JFormattedTextField.AbstractFormatter format = null;
291+
292+ if (source == null) {
293+ return null;
294+ }
295+ Object value = source.getValue();
296+
297+ if (value == null) {
298+ format = getNullFormatter();
299+ }
300+ if (format == null) {
301+ if (source.hasFocus()) {
302+ format = getEditFormatter();
303+ }
304+ else {
305+ format = getDisplayFormatter();
306+ }
307+ if (format == null) {
308+ format = getDefaultFormatter();
309+ }
310+ }
311+ return format;
312+ }
313+}