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