jdk/src/java.desktop/share/classes/javax/swing/text/DateFormatter.java
author serb
Mon, 06 Mar 2017 22:52:45 +0300
changeset 44155 1bf93336ea45
parent 25859 3317bb8137f4
permissions -rw-r--r--
8158209: Editing in TableView breaks the layout, when the document is I18n Reviewed-by: serb, alexsch Contributed-by: Abossolo Foh Guy <guy.abossolo.foh@scientificware.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * DateFormatter is an <code>InternationalFormatter</code> that does its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * formatting by way of an instance of <code>java.text.DateFormat</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    42
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see java.text.DateFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    50
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class DateFormatter extends InternationalFormatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * This is shorthand for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * <code>new DateFormatter(DateFormat.getDateInstance())</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public DateFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this(DateFormat.getDateInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Returns a DateFormatter configured with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <code>Format</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param format Format used to dictate legal values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public DateFormatter(DateFormat format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        setFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Sets the format that dictates the legal values that can be edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * and displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * If you have used the nullary constructor the value of this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * will be determined for the current locale by way of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <code>Dateformat.getDateInstance()</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param format DateFormat instance used for converting from/to Strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public void setFormat(DateFormat format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super.setFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns the Calendar that <code>DateFormat</code> is associated with,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * or if the <code>Format</code> is not a <code>DateFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <code>Calendar.getInstance</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Calendar getCalendar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Format f = getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (f instanceof DateFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return ((DateFormat)f).getCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return Calendar.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Returns true, as DateFormatterFilter will support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * incrementing/decrementing of the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    boolean getSupportsIncrement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns the field that will be adjusted by adjustValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   111
    Object getAdjustField(int start, Map<?, ?> attributes) {
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   112
        Iterator<?> attrs = attributes.keySet().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        while (attrs.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            Object key = attrs.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if ((key instanceof DateFormat.Field) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                (key == DateFormat.Field.HOUR1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                 ((DateFormat.Field)key).getCalendarField() != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Adjusts the Date if FieldPosition identifies a known calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   130
    Object adjustValue(Object value, Map<?, ?> attributes, Object key,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                           int direction) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                      BadLocationException, ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // HOUR1 has no corresponding calendar field, thus, map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // it to HOUR0 which will give the correct behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (key == DateFormat.Field.HOUR1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                key = DateFormat.Field.HOUR0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            field = ((DateFormat.Field)key).getCalendarField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            Calendar calendar = getCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (calendar != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                calendar.setTime((Date)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                int fieldValue = calendar.get(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    calendar.add(field, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    value = calendar.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                } catch (Throwable th) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}