src/share/classes/javax/swing/JSpinner.java

Print this page
rev 5615 : 6336885: RFE: Locale Data Deployment Enhancements
4609153: Provide locale data for Indic locales
5104387: Support for gl_ES locale (galician language)
6337471: desktop/system locale preferences support
7056139: (cal) SPI support for locale-dependent Calendar parameters
7058206: Provide CalendarData SPI for week params and display field value names
7073852: Support multiple scripts for digits and decimal symbols per locale
7079560: [Fmt-Da] Context dependent month names support in SimpleDateFormat
7171324: getAvailableLocales() of locale sensitive services should return the actual availability of locales
7151414: (cal) Support calendar type identification
7168528: LocaleServiceProvider needs to be aware of Locale extensions
7171372: (cal) locale's default Calendar should be created if unknown calendar is specified
Summary: JEP 127: Improve Locale Data Packaging and Adopt Unicode CLDR Data (part 1 w/o Jigsaw. by Naoto Sato and Masayoshi Okutsu)
   1 /*
   2  * Copyright (c) 2000, 2008, 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 
  26 package javax.swing;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 
  31 import javax.swing.event.*;
  32 import javax.swing.text.*;
  33 import javax.swing.plaf.SpinnerUI;
  34 
  35 import java.util.*;
  36 import java.beans.*;
  37 import java.text.*;
  38 import java.io.*;
  39 import java.util.HashMap;
  40 import sun.util.resources.LocaleData;
  41 
  42 import javax.accessibility.*;



  43 
  44 
  45 /**
  46  * A single line input field that lets the user select a
  47  * number or an object value from an ordered sequence. Spinners typically
  48  * provide a pair of tiny arrow buttons for stepping through the elements
  49  * of the sequence. The keyboard up/down arrow keys also cycle through the
  50  * elements. The user may also be allowed to type a (legal) value directly
  51  * into the spinner. Although combo boxes provide similar functionality,
  52  * spinners are sometimes preferred because they don't require a drop down list
  53  * that can obscure important data.
  54  * <p>
  55  * A <code>JSpinner</code>'s sequence value is defined by its
  56  * <code>SpinnerModel</code>.
  57  * The <code>model</code> can be specified as a constructor argument and
  58  * changed with the <code>model</code> property.  <code>SpinnerModel</code>
  59  * classes for some common types are provided: <code>SpinnerListModel</code>,
  60  * <code>SpinnerNumberModel</code>, and <code>SpinnerDateModel</code>.
  61  * <p>
  62  * A <code>JSpinner</code> has a single child component that's


 929             return model.getEnd();
 930         }
 931     }
 932 
 933 
 934     /**
 935      * An editor for a <code>JSpinner</code> whose model is a
 936      * <code>SpinnerDateModel</code>.  The value of the editor is
 937      * displayed with a <code>JFormattedTextField</code> whose format
 938      * is defined by a <code>DateFormatter</code> instance whose
 939      * <code>minimum</code> and <code>maximum</code> properties
 940      * are mapped to the <code>SpinnerDateModel</code>.
 941      * @since 1.4
 942      */
 943     // PENDING(hmuller): more example javadoc
 944     public static class DateEditor extends DefaultEditor
 945     {
 946         // This is here until SimpleDateFormat gets a constructor that
 947         // takes a Locale: 4923525
 948         private static String getDefaultPattern(Locale loc) {
 949             ResourceBundle r = LocaleData.getDateFormatData(loc);
 950             String[] dateTimePatterns = r.getStringArray("DateTimePatterns");
 951             Object[] dateTimeArgs = {dateTimePatterns[DateFormat.SHORT],
 952                                      dateTimePatterns[DateFormat.SHORT + 4]};
 953             return MessageFormat.format(dateTimePatterns[8], dateTimeArgs);
 954         }


 955 
 956         /**
 957          * Construct a <code>JSpinner</code> editor that supports displaying
 958          * and editing the value of a <code>SpinnerDateModel</code>
 959          * with a <code>JFormattedTextField</code>.  <code>This</code>
 960          * <code>DateEditor</code> becomes both a <code>ChangeListener</code>
 961          * on the spinners model and a <code>PropertyChangeListener</code>
 962          * on the new <code>JFormattedTextField</code>.
 963          *
 964          * @param spinner the spinner whose model <code>this</code> editor will monitor
 965          * @exception IllegalArgumentException if the spinners model is not
 966          *     an instance of <code>SpinnerDateModel</code>
 967          *
 968          * @see #getModel
 969          * @see #getFormat
 970          * @see SpinnerDateModel
 971          */
 972         public DateEditor(JSpinner spinner) {
 973             this(spinner, getDefaultPattern(spinner.getLocale()));
 974         }


1108     }
1109 
1110 
1111 
1112     /**
1113      * An editor for a <code>JSpinner</code> whose model is a
1114      * <code>SpinnerNumberModel</code>.  The value of the editor is
1115      * displayed with a <code>JFormattedTextField</code> whose format
1116      * is defined by a <code>NumberFormatter</code> instance whose
1117      * <code>minimum</code> and <code>maximum</code> properties
1118      * are mapped to the <code>SpinnerNumberModel</code>.
1119      * @since 1.4
1120      */
1121     // PENDING(hmuller): more example javadoc
1122     public static class NumberEditor extends DefaultEditor
1123     {
1124         // This is here until DecimalFormat gets a constructor that
1125         // takes a Locale: 4923525
1126         private static String getDefaultPattern(Locale locale) {
1127             // Get the pattern for the default locale.
1128             ResourceBundle rb = LocaleData.getNumberFormatData(locale);
1129             String[] all = rb.getStringArray("NumberPatterns");






1130             return all[0];
1131         }
1132 
1133         /**
1134          * Construct a <code>JSpinner</code> editor that supports displaying
1135          * and editing the value of a <code>SpinnerNumberModel</code>
1136          * with a <code>JFormattedTextField</code>.  <code>This</code>
1137          * <code>NumberEditor</code> becomes both a <code>ChangeListener</code>
1138          * on the spinner and a <code>PropertyChangeListener</code>
1139          * on the new <code>JFormattedTextField</code>.
1140          *
1141          * @param spinner the spinner whose model <code>this</code> editor will monitor
1142          * @exception IllegalArgumentException if the spinners model is not
1143          *     an instance of <code>SpinnerNumberModel</code>
1144          *
1145          * @see #getModel
1146          * @see #getFormat
1147          * @see SpinnerNumberModel
1148          */
1149         public NumberEditor(JSpinner spinner) {


   1 /*
   2  * Copyright (c) 2000, 2012, 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 
  26 package javax.swing;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 
  31 import javax.swing.event.*;
  32 import javax.swing.text.*;
  33 import javax.swing.plaf.SpinnerUI;
  34 
  35 import java.util.*;
  36 import java.beans.*;
  37 import java.text.*;
  38 import java.io.*;
  39 import java.text.spi.DateFormatProvider;
  40 import java.text.spi.NumberFormatProvider;
  41 
  42 import javax.accessibility.*;
  43 import sun.util.locale.provider.LocaleProviderAdapter;
  44 import sun.util.locale.provider.LocaleResources;
  45 import sun.util.locale.provider.LocaleServiceProviderPool;
  46 
  47 
  48 /**
  49  * A single line input field that lets the user select a
  50  * number or an object value from an ordered sequence. Spinners typically
  51  * provide a pair of tiny arrow buttons for stepping through the elements
  52  * of the sequence. The keyboard up/down arrow keys also cycle through the
  53  * elements. The user may also be allowed to type a (legal) value directly
  54  * into the spinner. Although combo boxes provide similar functionality,
  55  * spinners are sometimes preferred because they don't require a drop down list
  56  * that can obscure important data.
  57  * <p>
  58  * A <code>JSpinner</code>'s sequence value is defined by its
  59  * <code>SpinnerModel</code>.
  60  * The <code>model</code> can be specified as a constructor argument and
  61  * changed with the <code>model</code> property.  <code>SpinnerModel</code>
  62  * classes for some common types are provided: <code>SpinnerListModel</code>,
  63  * <code>SpinnerNumberModel</code>, and <code>SpinnerDateModel</code>.
  64  * <p>
  65  * A <code>JSpinner</code> has a single child component that's


 932             return model.getEnd();
 933         }
 934     }
 935 
 936 
 937     /**
 938      * An editor for a <code>JSpinner</code> whose model is a
 939      * <code>SpinnerDateModel</code>.  The value of the editor is
 940      * displayed with a <code>JFormattedTextField</code> whose format
 941      * is defined by a <code>DateFormatter</code> instance whose
 942      * <code>minimum</code> and <code>maximum</code> properties
 943      * are mapped to the <code>SpinnerDateModel</code>.
 944      * @since 1.4
 945      */
 946     // PENDING(hmuller): more example javadoc
 947     public static class DateEditor extends DefaultEditor
 948     {
 949         // This is here until SimpleDateFormat gets a constructor that
 950         // takes a Locale: 4923525
 951         private static String getDefaultPattern(Locale loc) {
 952             LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, loc);
 953             LocaleResources lr = adapter.getLocaleResources(loc);
 954             if (lr == null) {
 955                 lr = LocaleProviderAdapter.forJRE().getLocaleResources(loc);

 956             }
 957             return lr.getDateTimePattern(DateFormat.SHORT, DateFormat.SHORT, null);
 958         }
 959 
 960         /**
 961          * Construct a <code>JSpinner</code> editor that supports displaying
 962          * and editing the value of a <code>SpinnerDateModel</code>
 963          * with a <code>JFormattedTextField</code>.  <code>This</code>
 964          * <code>DateEditor</code> becomes both a <code>ChangeListener</code>
 965          * on the spinners model and a <code>PropertyChangeListener</code>
 966          * on the new <code>JFormattedTextField</code>.
 967          *
 968          * @param spinner the spinner whose model <code>this</code> editor will monitor
 969          * @exception IllegalArgumentException if the spinners model is not
 970          *     an instance of <code>SpinnerDateModel</code>
 971          *
 972          * @see #getModel
 973          * @see #getFormat
 974          * @see SpinnerDateModel
 975          */
 976         public DateEditor(JSpinner spinner) {
 977             this(spinner, getDefaultPattern(spinner.getLocale()));
 978         }


1112     }
1113 
1114 
1115 
1116     /**
1117      * An editor for a <code>JSpinner</code> whose model is a
1118      * <code>SpinnerNumberModel</code>.  The value of the editor is
1119      * displayed with a <code>JFormattedTextField</code> whose format
1120      * is defined by a <code>NumberFormatter</code> instance whose
1121      * <code>minimum</code> and <code>maximum</code> properties
1122      * are mapped to the <code>SpinnerNumberModel</code>.
1123      * @since 1.4
1124      */
1125     // PENDING(hmuller): more example javadoc
1126     public static class NumberEditor extends DefaultEditor
1127     {
1128         // This is here until DecimalFormat gets a constructor that
1129         // takes a Locale: 4923525
1130         private static String getDefaultPattern(Locale locale) {
1131             // Get the pattern for the default locale.
1132             LocaleProviderAdapter adapter;
1133             adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class,
1134                                                        locale);
1135             LocaleResources lr = adapter.getLocaleResources(locale);
1136             if (lr == null) {
1137                 lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);
1138             }
1139             String[] all = lr.getNumberPatterns();
1140             return all[0];
1141         }
1142 
1143         /**
1144          * Construct a <code>JSpinner</code> editor that supports displaying
1145          * and editing the value of a <code>SpinnerNumberModel</code>
1146          * with a <code>JFormattedTextField</code>.  <code>This</code>
1147          * <code>NumberEditor</code> becomes both a <code>ChangeListener</code>
1148          * on the spinner and a <code>PropertyChangeListener</code>
1149          * on the new <code>JFormattedTextField</code>.
1150          *
1151          * @param spinner the spinner whose model <code>this</code> editor will monitor
1152          * @exception IllegalArgumentException if the spinners model is not
1153          *     an instance of <code>SpinnerNumberModel</code>
1154          *
1155          * @see #getModel
1156          * @see #getFormat
1157          * @see SpinnerNumberModel
1158          */
1159         public NumberEditor(JSpinner spinner) {