1. /*
  2. * @(#)DateFormatter.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 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.awt.event.*;
  9. import java.text.*;
  10. import java.util.*;
  11. import javax.swing.*;
  12. import javax.swing.text.*;
  13. /**
  14. * DateFormatter is an <code>InternationalFormatter</code> that does its
  15. * formatting by way of an instance of <code>java.text.DateFormat</code>.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is
  20. * appropriate for short term storage or RMI between applications running
  21. * the same version of Swing. As of 1.4, support for long term storage
  22. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  23. * has been added to the <code>java.beans</code> package.
  24. * Please see {@link java.beans.XMLEncoder}.
  25. *
  26. * @see java.text.DateFormat
  27. *
  28. * @version 1.5 04/09/01
  29. * @since 1.4
  30. */
  31. public class DateFormatter extends InternationalFormatter {
  32. /**
  33. * This is shorthand for
  34. * <code>new DateFormatter(DateFormat.getDateInstance())</code>.
  35. */
  36. public DateFormatter() {
  37. this(DateFormat.getDateInstance());
  38. }
  39. /**
  40. * Returns a DateFormatter configured with the specified
  41. * <code>Format</code> instance.
  42. *
  43. * @param format Format used to dictate legal values
  44. */
  45. public DateFormatter(DateFormat format) {
  46. super(format);
  47. setFormat(format);
  48. }
  49. /**
  50. * Sets the format that dictates the legal values that can be edited
  51. * and displayed.
  52. * <p>
  53. * If you have used the nullary constructor the value of this property
  54. * will be determined for the current locale by way of the
  55. * <code>Dateformat.getDateInstance()</code> method.
  56. *
  57. * @param format DateFormat instance used for converting from/to Strings
  58. */
  59. public void setFormat(DateFormat format) {
  60. super.setFormat(format);
  61. }
  62. /**
  63. * Returns the Calendar that <code>DateFormat</code> is associated with,
  64. * or if the <code>Format</code> is not a <code>DateFormat</code>
  65. * <code>Calendar.getInstance</code> is returned.
  66. */
  67. private Calendar getCalendar() {
  68. Format f = getFormat();
  69. if (f instanceof DateFormat) {
  70. return ((DateFormat)f).getCalendar();
  71. }
  72. return Calendar.getInstance();
  73. }
  74. /**
  75. * Returns true, as DateFormatterFilter will support
  76. * incrementing/decrementing of the value.
  77. */
  78. boolean getSupportsIncrement() {
  79. return true;
  80. }
  81. /**
  82. * Returns the field that will be adjusted by adjustValue.
  83. */
  84. Object getAdjustField(int start, Map attributes) {
  85. Iterator attrs = attributes.keySet().iterator();
  86. while (attrs.hasNext()) {
  87. Object key = attrs.next();
  88. if ((key instanceof DateFormat.Field) &&
  89. (key == DateFormat.Field.HOUR1 ||
  90. ((DateFormat.Field)key).getCalendarField() != -1)) {
  91. return key;
  92. }
  93. }
  94. return null;
  95. }
  96. /**
  97. * Adjusts the Date if FieldPosition identifies a known calendar
  98. * field.
  99. */
  100. Object adjustValue(Object value, Map attributes, Object key,
  101. int direction) throws
  102. BadLocationException, ParseException {
  103. if (key != null) {
  104. int field;
  105. // HOUR1 has no corresponding calendar field, thus, map
  106. // it to HOUR0 which will give the correct behavior.
  107. if (key == DateFormat.Field.HOUR1) {
  108. key = DateFormat.Field.HOUR0;
  109. }
  110. field = ((DateFormat.Field)key).getCalendarField();
  111. Calendar calendar = getCalendar();
  112. if (calendar != null) {
  113. calendar.setTime((Date)value);
  114. int fieldValue = calendar.get(field);
  115. try {
  116. calendar.add(field, direction);
  117. value = calendar.getTime();
  118. } catch (Throwable th) {
  119. value = null;
  120. }
  121. return value;
  122. }
  123. }
  124. return null;
  125. }
  126. }