1 /*
   2  * Copyright (c) 2000, 2014, 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.awt.event.*;
  28 import java.text.*;
  29 import java.util.*;
  30 import javax.swing.*;
  31 import javax.swing.text.*;
  32 
  33 /**
  34  * DateFormatter is an <code>InternationalFormatter</code> that does its
  35  * formatting by way of an instance of <code>java.text.DateFormat</code>.
  36  * <p>
  37  * <strong>Warning:</strong>
  38  * Serialized objects of this class will not be compatible with
  39  * future Swing releases. The current serialization support is
  40  * appropriate for short term storage or RMI between applications running
  41  * the same version of Swing.  As of 1.4, support for long term storage
  42  * of all JavaBeans&trade;
  43  * has been added to the <code>java.beans</code> package.
  44  * Please see {@link java.beans.XMLEncoder}.
  45  *
  46  * @see java.text.DateFormat
  47  *
  48  * @since 1.4
  49  */
  50 @SuppressWarnings("serial") // Same-version serialization only
  51 public class DateFormatter extends InternationalFormatter {
  52     /**
  53      * This is shorthand for
  54      * <code>new DateFormatter(DateFormat.getDateInstance())</code>.
  55      */
  56     public DateFormatter() {
  57         this(DateFormat.getDateInstance());
  58     }
  59 
  60     /**
  61      * Returns a DateFormatter configured with the specified
  62      * <code>Format</code> instance.
  63      *
  64      * @param format Format used to dictate legal values
  65      */
  66     public DateFormatter(DateFormat format) {
  67         super(format);
  68         setFormat(format);
  69     }
  70 
  71     /**
  72      * Sets the format that dictates the legal values that can be edited
  73      * and displayed.
  74      * <p>
  75      * If you have used the nullary constructor the value of this property
  76      * will be determined for the current locale by way of the
  77      * <code>Dateformat.getDateInstance()</code> method.
  78      *
  79      * @param format DateFormat instance used for converting from/to Strings
  80      */
  81     public void setFormat(DateFormat format) {
  82         super.setFormat(format);
  83     }
  84 
  85     /**
  86      * Returns the Calendar that <code>DateFormat</code> is associated with,
  87      * or if the <code>Format</code> is not a <code>DateFormat</code>
  88      * <code>Calendar.getInstance</code> is returned.
  89      */
  90     private Calendar getCalendar() {
  91         Format f = getFormat();
  92 
  93         if (f instanceof DateFormat) {
  94             return ((DateFormat)f).getCalendar();
  95         }
  96         return Calendar.getInstance();
  97     }
  98 
  99 
 100     /**
 101      * Returns true, as DateFormatterFilter will support
 102      * incrementing/decrementing of the value.
 103      */
 104     boolean getSupportsIncrement() {
 105         return true;
 106     }
 107 
 108     /**
 109      * Returns the field that will be adjusted by adjustValue.
 110      */
 111     Object getAdjustField(int start, Map attributes) {
 112         Iterator attrs = attributes.keySet().iterator();
 113 
 114         while (attrs.hasNext()) {
 115             Object key = attrs.next();
 116 
 117             if ((key instanceof DateFormat.Field) &&
 118                 (key == DateFormat.Field.HOUR1 ||
 119                  ((DateFormat.Field)key).getCalendarField() != -1)) {
 120                 return key;
 121             }
 122         }
 123         return null;
 124     }
 125 
 126     /**
 127      * Adjusts the Date if FieldPosition identifies a known calendar
 128      * field.
 129      */
 130     Object adjustValue(Object value, Map attributes, Object key,
 131                            int direction) throws
 132                       BadLocationException, ParseException {
 133         if (key != null) {
 134             int field;
 135 
 136             // HOUR1 has no corresponding calendar field, thus, map
 137             // it to HOUR0 which will give the correct behavior.
 138             if (key == DateFormat.Field.HOUR1) {
 139                 key = DateFormat.Field.HOUR0;
 140             }
 141             field = ((DateFormat.Field)key).getCalendarField();
 142 
 143             Calendar calendar = getCalendar();
 144 
 145             if (calendar != null) {
 146                 calendar.setTime((Date)value);
 147 
 148                 int fieldValue = calendar.get(field);
 149 
 150                 try {
 151                     calendar.add(field, direction);
 152                     value = calendar.getTime();
 153                 } catch (Throwable th) {
 154                     value = null;
 155                 }
 156                 return value;
 157             }
 158         }
 159         return null;
 160     }
 161 }