jdk/src/java.desktop/share/classes/javax/swing/JSpinner.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 44655 06871a50a4b5
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44655
06871a50a4b5 8177461: Wrong references are used in the javadoc in the java.desktop module
serb
parents: 33253
diff changeset
     2
 * Copyright (c) 2000, 2017, 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: 2658
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: 2658
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: 2658
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.plaf.SpinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
    39
import java.text.spi.DateFormatProvider;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
    40
import java.text.spi.NumberFormatProvider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.accessibility.*;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
    43
import sun.util.locale.provider.LocaleProviderAdapter;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
    44
import sun.util.locale.provider.LocaleResources;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A single line input field that lets the user select a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * number or an object value from an ordered sequence. Spinners typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * provide a pair of tiny arrow buttons for stepping through the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * of the sequence. The keyboard up/down arrow keys also cycle through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * elements. The user may also be allowed to type a (legal) value directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * into the spinner. Although combo boxes provide similar functionality,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * spinners are sometimes preferred because they don't require a drop down list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * that can obscure important data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * A <code>JSpinner</code>'s sequence value is defined by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>SpinnerModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * The <code>model</code> can be specified as a constructor argument and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * changed with the <code>model</code> property.  <code>SpinnerModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * classes for some common types are provided: <code>SpinnerListModel</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <code>SpinnerNumberModel</code>, and <code>SpinnerDateModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * A <code>JSpinner</code> has a single child component that's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * responsible for displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * and potentially changing the current element or <i>value</i> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * the model, which is called the <code>editor</code>.  The editor is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * by the <code>JSpinner</code>'s constructor and can be changed with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <code>editor</code> property.  The <code>JSpinner</code>'s editor stays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * in sync with the model by listening for <code>ChangeEvent</code>s. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * user has changed the value displayed by the <code>editor</code> it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * possible for the <code>model</code>'s value to differ from that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * the <code>editor</code>. To make sure the <code>model</code> has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * value as the editor use the <code>commitEdit</code> method, eg:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *       spinner.commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   }
30922
562d748c9187 8013820: JavaDoc for JSpinner contains errors
serb
parents: 28059
diff changeset
    78
 *   catch (ParseException pe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *       // Edited value is invalid, spinner.getValue() will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *       // the last valid value, you could revert the spinner to show that:
30922
562d748c9187 8013820: JavaDoc for JSpinner contains errors
serb
parents: 28059
diff changeset
    81
 *       JComponent editor = spinner.getEditor();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *       if (editor instanceof DefaultEditor) {
30922
562d748c9187 8013820: JavaDoc for JSpinner contains errors
serb
parents: 28059
diff changeset
    83
 *           ((DefaultEditor)editor).getTextField().setValue(spinner.getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *       // reset the value to some known value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *       spinner.setValue(fallbackValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *       // or treat the last valid value as the current, in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *       // case you don't need to do anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   return spinner.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * For information and examples of using spinner see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20157
diff changeset
    94
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html">How to Use Spinners</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * 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: 20455
diff changeset
   107
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @see SpinnerModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @see AbstractSpinnerModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @see SpinnerListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @see SpinnerNumberModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @see SpinnerDateModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @see JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @author Lynn Monsanto (accessibility)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   122
@JavaBean(defaultProperty = "UI", description = "A single line input field that lets the user select a number or an object value from an ordered set.")
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   123
@SwingContainer(false)
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
   124
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
public class JSpinner extends JComponent implements Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static final String uiClassID = "SpinnerUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static final Action DISABLED_ACTION = new DisabledAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private SpinnerModel model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private JComponent editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private ChangeListener modelListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private transient ChangeEvent changeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private boolean editorExplicitlySet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Constructs a spinner for the given model. The spinner has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * a set of previous/next buttons, and an editor appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * for the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
25760
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25568
diff changeset
   147
     * @param model  a model for the new spinner
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws NullPointerException if the model is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public JSpinner(SpinnerModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (model == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new NullPointerException("model cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.model = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.editor = createEditor(model);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   156
        setUIProperty("opaque",true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Constructs a spinner with an <code>Integer SpinnerNumberModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * with initial value 0 and no minimum or maximum limits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public JSpinner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this(new SpinnerNumberModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 13583
diff changeset
   171
     * Returns the look and feel (L&amp;F) object that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return the <code>SpinnerUI</code> object that renders this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public SpinnerUI getUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return (SpinnerUI)ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 13583
diff changeset
   181
     * Sets the look and feel (L&amp;F) object that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 13583
diff changeset
   183
     * @param ui  the <code>SpinnerUI</code> L&amp;F object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public void setUI(SpinnerUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns the suffix used to construct the name of the look and feel
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 13583
diff changeset
   193
     * (L&amp;F) class used to render this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return the string "SpinnerUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   199
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Resets the UI property with the value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see UIManager#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        setUI((SpinnerUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * This method is called by the constructors to create the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <code>JComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * that displays the current value of the sequence.  The editor may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * also allow the user to enter an element of the sequence directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * An editor must listen for <code>ChangeEvents</code> on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <code>model</code> and keep the value it displays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * in sync with the value of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Subclasses may override this method to add support for new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <code>SpinnerModel</code> classes.  Alternatively one can just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * replace the editor created here with the <code>setEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * method.  The default mapping from model type to editor is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <li> <code>SpinnerNumberModel =&gt; JSpinner.NumberEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <li> <code>SpinnerDateModel =&gt; JSpinner.DateEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <li> <code>SpinnerListModel =&gt; JSpinner.ListEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <li> <i>all others</i> =&gt; <code>JSpinner.DefaultEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return a component that displays the current value of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param model the value of getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @see #setEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    protected JComponent createEditor(SpinnerModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (model instanceof SpinnerDateModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return new DateEditor(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        else if (model instanceof SpinnerListModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return new ListEditor(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        else if (model instanceof SpinnerNumberModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return new NumberEditor(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return new DefaultEditor(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Changes the model that represents the value of this spinner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * If the editor property has not been explicitly set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * the editor property is (implicitly) set after the <code>"model"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <code>PropertyChangeEvent</code> has been fired.  The editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * property is set to the value returned by <code>createEditor</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * as in:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * setEditor(createEditor(model));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param model the new <code>SpinnerModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see #getEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see #setEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @throws IllegalArgumentException if model is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   275
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   276
            = "Model that represents the value of this spinner.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public void setModel(SpinnerModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (model == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new IllegalArgumentException("null model");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (!model.equals(this.model)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            SpinnerModel oldModel = this.model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            this.model = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (modelListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                oldModel.removeChangeListener(modelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                this.model.addChangeListener(modelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            firePropertyChange("model", oldModel, model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (!editorExplicitlySet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                setEditor(createEditor(model)); // sets editorExplicitlySet true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                editorExplicitlySet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Returns the <code>SpinnerModel</code> that defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * this spinners sequence of values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return the value of the model property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see #setModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public SpinnerModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns the current value of the model, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * this value is displayed by the <code>editor</code>. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * user has changed the value displayed by the <code>editor</code> it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * possible for the <code>model</code>'s value to differ from that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * the <code>editor</code>, refer to the class level javadoc for examples
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * of how to deal with this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * This method simply delegates to the <code>model</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * It is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * getModel().getValue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
25760
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25568
diff changeset
   325
     * @return the current value of the model
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see SpinnerModel#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return getModel().getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Changes current value of the model, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * this value is displayed by the <code>editor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * If the <code>SpinnerModel</code> implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * doesn't support the specified value then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * This method simply delegates to the <code>model</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * It is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * getModel().setValue(value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
25760
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25568
diff changeset
   347
     * @param value  new value for the spinner
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @throws IllegalArgumentException if <code>value</code> isn't allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @see SpinnerModel#setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        getModel().setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Returns the object in the sequence that comes after the object returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * by <code>getValue()</code>. If the end of the sequence has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * then return <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Calling this method does not effect <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * This method simply delegates to the <code>model</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * It is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * getModel().getNextValue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return the next legal value or <code>null</code> if one doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see #getPreviousValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see SpinnerModel#getNextValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   374
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public Object getNextValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return getModel().getNextValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * We pass <code>Change</code> events along to the listeners with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * the slider (instead of the model itself) as the event source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    private class ModelListener implements ChangeListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Adds a listener to the list that is notified each time a change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * to the model occurs.  The source of <code>ChangeEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * delivered to <code>ChangeListeners</code> will be this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <code>JSpinner</code>.  Note also that replacing the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * will not affect listeners added directly to JSpinner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Applications can add listeners to  the model directly.  In that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * case is that the source of the event would be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <code>SpinnerModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param listener the <code>ChangeListener</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public void addChangeListener(ChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (modelListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            modelListener = new ModelListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            getModel().addChangeListener(modelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        listenerList.add(ChangeListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Removes a <code>ChangeListener</code> from this spinner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param listener the <code>ChangeListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @see #fireStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public void removeChangeListener(ChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        listenerList.remove(ChangeListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Returns an array of all the <code>ChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * to this JSpinner with addChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @return all of the <code>ChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   435
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public ChangeListener[] getChangeListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   437
        return listenerList.getListeners(ChangeListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Sends a <code>ChangeEvent</code>, whose source is this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <code>JSpinner</code>, to each <code>ChangeListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * When a <code>ChangeListener</code> has been added
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   445
     * to the spinner, this method is called each time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * a <code>ChangeEvent</code> is received from the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    protected void fireStateChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (listeners[i] == ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                if (changeEvent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Returns the object in the sequence that comes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * before the object returned by <code>getValue()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * If the end of the sequence has been reached then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * return <code>null</code>. Calling this method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * not effect <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * This method simply delegates to the <code>model</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * It is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * getModel().getPreviousValue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @return the previous legal value or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *   if one doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @see #getNextValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @see SpinnerModel#getPreviousValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   484
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public Object getPreviousValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return getModel().getPreviousValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Changes the <code>JComponent</code> that displays the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * of the <code>SpinnerModel</code>.  It is the responsibility of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * method to <i>disconnect</i> the old editor from the model and to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * connect the new editor.  This may mean removing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * old editors <code>ChangeListener</code> from the model or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * spinner itself and adding one for the new editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param editor the new editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @see #getEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @see #createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @throws IllegalArgumentException if editor is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   504
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
   505
            = "JComponent that displays the current value of the model")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public void setEditor(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (editor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            throw new IllegalArgumentException("null editor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (!editor.equals(this.editor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            JComponent oldEditor = this.editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            this.editor = editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (oldEditor instanceof DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                ((DefaultEditor)oldEditor).dismiss(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            editorExplicitlySet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            firePropertyChange("editor", oldEditor, editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Returns the component that displays and potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * changes the model's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return the component that displays and potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *    changes the model's value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see #setEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @see #createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public JComponent getEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Commits the currently edited value to the <code>SpinnerModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * If the editor is an instance of <code>DefaultEditor</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * call if forwarded to the editor, otherwise this does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @throws ParseException if the currently edited value couldn't
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   545
     *         be committed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public void commitEdit() throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        JComponent editor = getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (editor instanceof DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            ((DefaultEditor)editor).commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * See readObject and writeObject in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param s Stream to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * A simple base class for more specialized editors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * that displays a read-only view of the model's current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * value with a <code>JFormattedTextField</code>.  Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * can configure the <code>JFormattedTextField</code> to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * an editor that's appropriate for the type of model they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * support and they may want to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * the <code>stateChanged</code> and <code>propertyChanged</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * methods, which keep the model and the text field in sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * This class defines a <code>dismiss</code> method that removes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * editors <code>ChangeListener</code> from the <code>JSpinner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * that it's part of.   The <code>setEditor</code> method knows about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <code>DefaultEditor.dismiss</code>, so if the developer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * replaces an editor that's derived from <code>JSpinner.DefaultEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * its <code>ChangeListener</code> connection back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <code>JSpinner</code> will be removed.  However after that,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * it's up to the developer to manage their editor listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Similarly, if a subclass overrides <code>createEditor</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * it's up to the subclasser to deal with their editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * subsequently being replaced (with <code>setEditor</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * We expect that in most cases, and in editor installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * with <code>setEditor</code> or created by a <code>createEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * override, will not be replaced anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * This class is the <code>LayoutManager</code> for it's single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <code>JFormattedTextField</code> child.   By default the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * child is just centered with the parents insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public static class DefaultEditor extends JPanel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        implements ChangeListener, PropertyChangeListener, LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * Constructs an editor component for the specified <code>JSpinner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * This <code>DefaultEditor</code> is it's own layout manager and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * it is added to the spinner's <code>ChangeListener</code> list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * The constructor creates a single <code>JFormattedTextField</code> child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * initializes it's value to be the spinner model's current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         * and adds it to <code>this</code> <code>DefaultEditor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         * @param spinner the spinner whose model <code>this</code> editor will monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * @see JSpinner#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        public DefaultEditor(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            JFormattedTextField ftf = new JFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            ftf.setName("Spinner.formattedTextField");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            ftf.setValue(spinner.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            ftf.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            ftf.setEditable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            ftf.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            String toolTipText = spinner.getToolTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            if (toolTipText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                ftf.setToolTipText(toolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            add(ftf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            setLayout(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            spinner.addChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            // We want the spinner's increment/decrement actions to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            // active vs those of the JFormattedTextField. As such we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // put disabled actions in the JFormattedTextField's actionmap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            // A binding to a disabled action is treated as a nonexistant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            // binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            ActionMap ftfMap = ftf.getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (ftfMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                ftfMap.put("increment", DISABLED_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                ftfMap.put("decrement", DISABLED_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         * Disconnect <code>this</code> editor from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         * <code>JSpinner</code>.  By default, this method removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         * itself from the spinners <code>ChangeListener</code> list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * @param spinner the <code>JSpinner</code> to disconnect this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         *    editor from; the same spinner as was passed to the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        public void dismiss(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            spinner.removeChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         * Returns the <code>JSpinner</code> ancestor of this editor or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         * <code>null</code> if none of the ancestors are a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         * <code>JSpinner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
         * Typically the editor's parent is a <code>JSpinner</code> however
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         * subclasses of <code>JSpinner</code> may override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         * the <code>createEditor</code> method and insert one or more containers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * between the <code>JSpinner</code> and it's editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * @return <code>JSpinner</code> ancestor; <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         *         if none of the ancestors are a <code>JSpinner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * @see JSpinner#createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        public JSpinner getSpinner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            for (Component c = this; c != null; c = c.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                if (c instanceof JSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    return (JSpinner)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * Returns the <code>JFormattedTextField</code> child of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * editor.  By default the text field is the first and only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * child of editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         * @return the <code>JFormattedTextField</code> that gives the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
         *     access to the <code>SpinnerDateModel's</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         * @see #getSpinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        public JFormattedTextField getTextField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            return (JFormattedTextField)getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * This method is called when the spinner's model's state changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         * It sets the <code>value</code> of the text field to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         * value of the spinners model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         * @param e the <code>ChangeEvent</code> whose source is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         * <code>JSpinner</code> whose model has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         * @see JSpinner#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            JSpinner spinner = (JSpinner)(e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            getTextField().setValue(spinner.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         * Called by the <code>JFormattedTextField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         * <code>PropertyChangeListener</code>.  When the <code>"value"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         * property changes, which implies that the user has typed a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         * number, we set the value of the spinners model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         * This class ignores <code>PropertyChangeEvents</code> whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
         * source is not the <code>JFormattedTextField</code>, so subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
         * may safely make <code>this</code> <code>DefaultEditor</code> a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
         * <code>PropertyChangeListener</code> on other objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
         * @param e the <code>PropertyChangeEvent</code> whose source is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
         *    the <code>JFormattedTextField</code> created by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        public void propertyChange(PropertyChangeEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            JSpinner spinner = getSpinner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if (spinner == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                // Indicates we aren't installed anywhere.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            Object source = e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            String name = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if ((source instanceof JFormattedTextField) && "value".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                Object lastValue = spinner.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                // Try to set the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    spinner.setValue(getTextField().getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    // SpinnerModel didn't like new value, reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                        ((JFormattedTextField)source).setValue(lastValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    } catch (IllegalArgumentException iae2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                        // Still bogus, nothing else we can do, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                        // SpinnerModel and JFormattedTextField are now out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                        // of sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * This <code>LayoutManager</code> method does nothing.  We're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         * only managing a single child and there's no support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
         * for layout constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
         * @param name ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
         * @param child ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        public void addLayoutComponent(String name, Component child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * This <code>LayoutManager</code> method does nothing.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         * isn't any per-child state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         * @param child ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        public void removeLayoutComponent(Component child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * Returns the size of the parents insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        private Dimension insetSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            int w = insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            int h = insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            return new Dimension(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         * Returns the preferred size of first (and only) child plus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * size of the parents insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         * @param parent the Container that's managing the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         * @return the preferred dimensions to lay out the subcomponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         *          of the specified container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            Dimension preferredSize = insetSize(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            if (parent.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                Dimension childSize = getComponent(0).getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                preferredSize.width += childSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                preferredSize.height += childSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            return preferredSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         * Returns the minimum size of first (and only) child plus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         * size of the parents insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
         * @param parent the Container that's managing the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         * @return  the minimum dimensions needed to lay out the subcomponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         *          of the specified container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            Dimension minimumSize = insetSize(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            if (parent.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                Dimension childSize = getComponent(0).getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                minimumSize.width += childSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                minimumSize.height += childSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            return minimumSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * Resize the one (and only) child to completely fill the area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * within the parents insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (parent.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                int w = parent.getWidth() - (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                int h = parent.getHeight() - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                getComponent(0).setBounds(insets.left, insets.top, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         * Pushes the currently edited value to the <code>SpinnerModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         * The default implementation invokes <code>commitEdit</code> on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         * @throws ParseException if the edited value is not legal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        public void commitEdit()  throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            // If the value in the JFormattedTextField is legal, this will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            // the result of pushing the value to the SpinnerModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            // by way of the <code>propertyChange</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            JFormattedTextField ftf = getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            ftf.commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
         * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         * @see javax.swing.JComponent#getBaseline(int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         * @see javax.swing.JComponent#getBaselineResizeBehavior()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        public int getBaseline(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            // check size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            super.getBaseline(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            width = width - insets.left - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            height = height - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            int baseline = getComponent(0).getBaseline(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            if (baseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                return baseline + insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        public BaselineResizeBehavior getBaselineResizeBehavior() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return getComponent(0).getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * This subclass of javax.swing.DateFormatter maps the minimum/maximum
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   906
     * properties to the start/end properties of a SpinnerDateModel.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    private static class DateEditorFormatter extends DateFormatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        private final SpinnerDateModel model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        DateEditorFormatter(SpinnerDateModel model, DateFormat format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            super(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            this.model = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   916
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   917
        @SuppressWarnings("unchecked")
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   918
        public void setMinimum(Comparable<?> min) {
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   919
            model.setStart((Comparable<Date>)min);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   922
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   923
        public Comparable<Date> getMinimum() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            return  model.getStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   927
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   928
        @SuppressWarnings("unchecked")
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   929
        public void setMaximum(Comparable<?> max) {
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   930
            model.setEnd((Comparable<Date>)max);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   933
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
   934
        public Comparable<Date> getMaximum() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            return model.getEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * An editor for a <code>JSpinner</code> whose model is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * <code>SpinnerDateModel</code>.  The value of the editor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * displayed with a <code>JFormattedTextField</code> whose format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * is defined by a <code>DateFormatter</code> instance whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * <code>minimum</code> and <code>maximum</code> properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * are mapped to the <code>SpinnerDateModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    // PENDING(hmuller): more example javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    public static class DateEditor extends DefaultEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        // This is here until SimpleDateFormat gets a constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        // takes a Locale: 4923525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        private static String getDefaultPattern(Locale loc) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
   955
            LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, loc);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
   956
            LocaleResources lr = adapter.getLocaleResources(loc);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
   957
            if (lr == null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
   958
                lr = LocaleProviderAdapter.forJRE().getLocaleResources(loc);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
   959
            }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
   960
            return lr.getDateTimePattern(DateFormat.SHORT, DateFormat.SHORT, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         * Construct a <code>JSpinner</code> editor that supports displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         * and editing the value of a <code>SpinnerDateModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * with a <code>JFormattedTextField</code>.  <code>This</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * <code>DateEditor</code> becomes both a <code>ChangeListener</code>
30931
dad3f622d47a 5109918: Wrong documentation for JSpinner.DateEditor constructor
serb
parents: 30922
diff changeset
   968
         * on the spinner and a <code>PropertyChangeListener</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         * on the new <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
         * @param spinner the spinner whose model <code>this</code> editor will monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
         * @exception IllegalArgumentException if the spinners model is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
         *     an instance of <code>SpinnerDateModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
         * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         * @see #getFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         * @see SpinnerDateModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        public DateEditor(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            this(spinner, getDefaultPattern(spinner.getLocale()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         * Construct a <code>JSpinner</code> editor that supports displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         * and editing the value of a <code>SpinnerDateModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         * with a <code>JFormattedTextField</code>.  <code>This</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         * <code>DateEditor</code> becomes both a <code>ChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         * on the spinner and a <code>PropertyChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         * on the new <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         * @param spinner the spinner whose model <code>this</code> editor will monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         * @param dateFormatPattern the initial pattern for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
         *     <code>SimpleDateFormat</code> object that's used to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
         *     and parse the value of the text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
         * @exception IllegalArgumentException if the spinners model is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
         *     an instance of <code>SpinnerDateModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         * @see #getFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         * @see SpinnerDateModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         * @see java.text.SimpleDateFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        public DateEditor(JSpinner spinner, String dateFormatPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            this(spinner, new SimpleDateFormat(dateFormatPattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                                               spinner.getLocale()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
         * Construct a <code>JSpinner</code> editor that supports displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
         * and editing the value of a <code>SpinnerDateModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
         * with a <code>JFormattedTextField</code>.  <code>This</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
         * <code>DateEditor</code> becomes both a <code>ChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
         * on the spinner and a <code>PropertyChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         * on the new <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
         * @param spinner the spinner whose model <code>this</code> editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
         *        will monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
         * @param format <code>DateFormat</code> object that's used to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
         *     and parse the value of the text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
         * @exception IllegalArgumentException if the spinners model is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
         *     an instance of <code>SpinnerDateModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         * @see #getFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
         * @see SpinnerDateModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
         * @see java.text.SimpleDateFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        private DateEditor(JSpinner spinner, DateFormat format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            super(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            if (!(spinner.getModel() instanceof SpinnerDateModel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                                 "model not a SpinnerDateModel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            SpinnerDateModel model = (SpinnerDateModel)spinner.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            DateFormatter formatter = new DateEditorFormatter(model, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            DefaultFormatterFactory factory = new DefaultFormatterFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                                                  formatter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            JFormattedTextField ftf = getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            ftf.setEditable(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            ftf.setFormatterFactory(factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            /* TBD - initializing the column width of the text field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
             * is imprecise and doing it here is tricky because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
             * the developer may configure the formatter later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                String maxString = formatter.valueToString(model.getStart());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                String minString = formatter.valueToString(model.getEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                ftf.setColumns(Math.max(maxString.length(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                                        minString.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            catch (ParseException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                // PENDING: hmuller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
         * Returns the <code>java.text.SimpleDateFormat</code> object the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
         * <code>JFormattedTextField</code> uses to parse and format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
         * numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
         * @return the value of <code>getTextField().getFormatter().getFormat()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
         * @see java.text.SimpleDateFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        public SimpleDateFormat getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            return (SimpleDateFormat)((DateFormatter)(getTextField().getFormatter())).getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
         * Return our spinner ancestor's <code>SpinnerDateModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
         * @return <code>getSpinner().getModel()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
         * @see #getSpinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        public SpinnerDateModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            return (SpinnerDateModel)(getSpinner().getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * This subclass of javax.swing.NumberFormatter maps the minimum/maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * properties to a SpinnerNumberModel and initializes the valueClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * of the NumberFormatter to match the type of the initial models value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    private static class NumberEditorFormatter extends NumberFormatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        private final SpinnerNumberModel model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        NumberEditorFormatter(SpinnerNumberModel model, NumberFormat format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            super(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            this.model = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            setValueClass(model.getValue().getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
  1100
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
  1101
        public void setMinimum(Comparable<?> min) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            model.setMinimum(min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
  1105
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
  1106
        public Comparable<?> getMinimum() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return  model.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
  1110
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
  1111
        public void setMaximum(Comparable<?> max) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            model.setMaximum(max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
  1115
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23315
diff changeset
  1116
        public Comparable<?> getMaximum() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            return model.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * An editor for a <code>JSpinner</code> whose model is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * <code>SpinnerNumberModel</code>.  The value of the editor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * displayed with a <code>JFormattedTextField</code> whose format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * is defined by a <code>NumberFormatter</code> instance whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * <code>minimum</code> and <code>maximum</code> properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * are mapped to the <code>SpinnerNumberModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    // PENDING(hmuller): more example javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    public static class NumberEditor extends DefaultEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        // This is here until DecimalFormat gets a constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        // takes a Locale: 4923525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        private static String getDefaultPattern(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            // Get the pattern for the default locale.
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
  1139
            LocaleProviderAdapter adapter;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
  1140
            adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class,
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
  1141
                                                       locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
  1142
            LocaleResources lr = adapter.getLocaleResources(locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
  1143
            if (lr == null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
  1144
                lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
  1145
            }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
  1146
            String[] all = lr.getNumberPatterns();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            return all[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
         * Construct a <code>JSpinner</code> editor that supports displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
         * and editing the value of a <code>SpinnerNumberModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
         * with a <code>JFormattedTextField</code>.  <code>This</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
         * <code>NumberEditor</code> becomes both a <code>ChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         * on the spinner and a <code>PropertyChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         * on the new <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
         * @param spinner the spinner whose model <code>this</code> editor will monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
         * @exception IllegalArgumentException if the spinners model is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
         *     an instance of <code>SpinnerNumberModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
         * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
         * @see #getFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
         * @see SpinnerNumberModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        public NumberEditor(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            this(spinner, getDefaultPattern(spinner.getLocale()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
         * Construct a <code>JSpinner</code> editor that supports displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
         * and editing the value of a <code>SpinnerNumberModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
         * with a <code>JFormattedTextField</code>.  <code>This</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         * <code>NumberEditor</code> becomes both a <code>ChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
         * on the spinner and a <code>PropertyChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
         * on the new <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
         * @param spinner the spinner whose model <code>this</code> editor will monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
         * @param decimalFormatPattern the initial pattern for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
         *     <code>DecimalFormat</code> object that's used to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
         *     and parse the value of the text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         * @exception IllegalArgumentException if the spinners model is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
         *     an instance of <code>SpinnerNumberModel</code> or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
         *     <code>decimalFormatPattern</code> is not a legal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
         *     argument to <code>DecimalFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
         * @see SpinnerNumberModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
         * @see java.text.DecimalFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        public NumberEditor(JSpinner spinner, String decimalFormatPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            this(spinner, new DecimalFormat(decimalFormatPattern));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
         * Construct a <code>JSpinner</code> editor that supports displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
         * and editing the value of a <code>SpinnerNumberModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
         * with a <code>JFormattedTextField</code>.  <code>This</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
         * <code>NumberEditor</code> becomes both a <code>ChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
         * on the spinner and a <code>PropertyChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
         * on the new <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         * @param spinner the spinner whose model <code>this</code> editor will monitor
44655
06871a50a4b5 8177461: Wrong references are used in the javadoc in the java.desktop module
serb
parents: 33253
diff changeset
  1205
         * @param format the initial pattern for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
         *     <code>DecimalFormat</code> object that's used to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
         *     and parse the value of the text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
         * @exception IllegalArgumentException if the spinners model is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
         *     an instance of <code>SpinnerNumberModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
         * @see SpinnerNumberModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
         * @see java.text.DecimalFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        private NumberEditor(JSpinner spinner, DecimalFormat format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            super(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            if (!(spinner.getModel() instanceof SpinnerNumberModel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                          "model not a SpinnerNumberModel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            SpinnerNumberModel model = (SpinnerNumberModel)spinner.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            NumberFormatter formatter = new NumberEditorFormatter(model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                                                                  format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            DefaultFormatterFactory factory = new DefaultFormatterFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                                                  formatter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            JFormattedTextField ftf = getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            ftf.setEditable(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            ftf.setFormatterFactory(factory);
23315
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1230
            // Change the text orientation for the NumberEditor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            ftf.setHorizontalAlignment(JTextField.RIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            /* TBD - initializing the column width of the text field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
             * is imprecise and doing it here is tricky because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
             * the developer may configure the formatter later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                String maxString = formatter.valueToString(model.getMinimum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                String minString = formatter.valueToString(model.getMaximum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                ftf.setColumns(Math.max(maxString.length(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                                        minString.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            catch (ParseException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                // TBD should throw a chained error here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
         * Returns the <code>java.text.DecimalFormat</code> object the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
         * <code>JFormattedTextField</code> uses to parse and format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
         * numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
         * @return the value of <code>getTextField().getFormatter().getFormat()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
         * @see java.text.DecimalFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        public DecimalFormat getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            return (DecimalFormat)((NumberFormatter)(getTextField().getFormatter())).getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
         * Return our spinner ancestor's <code>SpinnerNumberModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
         * @return <code>getSpinner().getModel()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
         * @see #getSpinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        public SpinnerNumberModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            return (SpinnerNumberModel)(getSpinner().getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        }
23315
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1274
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1275
        /**
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1276
         * {@inheritDoc}
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1277
         */
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1278
        @Override
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1279
        public void setComponentOrientation(ComponentOrientation o) {
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1280
            super.setComponentOrientation(o);
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1281
            getTextField().setHorizontalAlignment(
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1282
                    o.isLeftToRight() ? JTextField.RIGHT : JTextField.LEFT);
02c932be7d63 8008657: JSpinner setComponentOrientation doesn't affect on text orientation
alexsch
parents: 22574
diff changeset
  1283
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * An editor for a <code>JSpinner</code> whose model is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * <code>SpinnerListModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    public static class ListEditor extends DefaultEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
         * Construct a <code>JSpinner</code> editor that supports displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
         * and editing the value of a <code>SpinnerListModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         * with a <code>JFormattedTextField</code>.  <code>This</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         * <code>ListEditor</code> becomes both a <code>ChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         * on the spinner and a <code>PropertyChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
         * on the new <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
         * @param spinner the spinner whose model <code>this</code> editor will monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
         * @exception IllegalArgumentException if the spinners model is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
         *     an instance of <code>SpinnerListModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
         * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
         * @see SpinnerListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        public ListEditor(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            super(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            if (!(spinner.getModel() instanceof SpinnerListModel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                throw new IllegalArgumentException("model not a SpinnerListModel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            getTextField().setEditable(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            getTextField().setFormatterFactory(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                              DefaultFormatterFactory(new ListFormatter()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
         * Return our spinner ancestor's <code>SpinnerNumberModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
         * @return <code>getSpinner().getModel()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
         * @see #getSpinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
         * @see #getTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        public SpinnerListModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            return (SpinnerListModel)(getSpinner().getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
         * ListFormatter provides completion while text is being input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
         * into the JFormattedTextField. Completion is only done if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
         * user is inserting text at the end of the document. Completion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
         * is done by way of the SpinnerListModel method findNextMatch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        private class ListFormatter extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                          JFormattedTextField.AbstractFormatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            private DocumentFilter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            public String valueToString(Object value) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                    return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                return value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            public Object stringToValue(String string) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            protected DocumentFilter getDocumentFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                if (filter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                    filter = new Filter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                return filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            private class Filter extends DocumentFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                public void replace(FilterBypass fb, int offset, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                                    String string, AttributeSet attrs) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                                           BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    if (string != null && (offset + length) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                                          fb.getDocument().getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                        Object next = getModel().findNextMatch(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                                         fb.getDocument().getText(0, offset) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                                         string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        String value = (next != null) ? next.toString() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                            fb.remove(0, offset + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                            fb.insertString(0, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                            getFormattedTextField().select(offset +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                                                           string.length(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                                                           value.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    super.replace(fb, offset, length, string, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                public void insertString(FilterBypass fb, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                                     String string, AttributeSet attr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                       throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    replace(fb, offset, 0, string, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * An Action implementation that is always disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    private static class DisabledAction implements Action {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        public Object getValue(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        public void putValue(String key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        public void setEnabled(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        public void addPropertyChangeListener(PropertyChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        public void removePropertyChangeListener(PropertyChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    /////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    // Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    ////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * Gets the <code>AccessibleContext</code> for the <code>JSpinner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * @return the <code>AccessibleContext</code> for the <code>JSpinner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30931
diff changeset
  1425
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            accessibleContext = new AccessibleJSpinner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * <code>AccessibleJSpinner</code> implements accessibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * support for the <code>JSpinner</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    protected class AccessibleJSpinner extends AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        implements AccessibleValue, AccessibleAction, AccessibleText,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                   AccessibleEditableText, ChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        private Object oldModelValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
         * AccessibleJSpinner constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        protected AccessibleJSpinner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            // model is guaranteed to be non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            oldModelValue = model.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            JSpinner.this.addChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
         * Invoked when the target of the listener has changed its state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
         * @param e  a <code>ChangeEvent</code> object. Must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         * @throws NullPointerException if the parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            Object newModelValue = model.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            firePropertyChange(ACCESSIBLE_VALUE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                               oldModelValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                               newModelValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            firePropertyChange(ACCESSIBLE_TEXT_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                               null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                               0); // entire text may have changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            oldModelValue = newModelValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        /* ===== Begin AccessibleContext methods ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
         * Gets the role of this object.  The role of the object is the generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
         * purpose or use of the class of this object.  For example, the role
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
         * of a push button is AccessibleRole.PUSH_BUTTON.  The roles in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
         * AccessibleRole are provided so component developers can pick from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         * a set of predefined roles.  This enables assistive technologies to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
         * provide a consistent interface to various tweaked subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
         * components (e.g., use AccessibleRole.PUSH_BUTTON for all components
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1484
         * that act like a push button) as well as distinguish between subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
         * that behave differently (e.g., AccessibleRole.CHECK_BOX for check boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
         * and AccessibleRole.RADIO_BUTTON for radio buttons).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
         * <p>Note that the AccessibleRole class is also extensible, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
         * custom component developers can define their own AccessibleRole's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
         * if the set of predefined roles is inadequate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
         * @return an instance of AccessibleRole describing the role of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            return AccessibleRole.SPIN_BOX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
         * Returns the number of accessible children of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
         * @return the number of accessible children of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            // the JSpinner has one child, the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            if (editor.getAccessibleContext() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
         * Returns the specified Accessible child of the object.  The Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
         * children of an Accessible object are zero-based, so the first child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
         * of an Accessible child is at index 0, the second child is at index 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
         * and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
         * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
         * @return the Accessible child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
         * @see #getAccessibleChildrenCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            // the JSpinner has one child, the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            if (editor.getAccessibleContext() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                return (Accessible)editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        /* ===== End AccessibleContext methods ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
         * Gets the AccessibleAction associated with this object that supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
         * one or more actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
         * @return AccessibleAction if supported by object; else return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
         * @see AccessibleAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        public AccessibleAction getAccessibleAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
         * Gets the AccessibleText associated with this object presenting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
         * text on the display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
         * @return AccessibleText if supported by object; else return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
         * @see AccessibleText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
         * Returns the AccessibleContext for the JSpinner editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        private AccessibleContext getEditorAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            if (editor instanceof DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                JTextField textField = ((DefaultEditor)editor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                if (textField != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                    return textField.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            } else if (editor instanceof Accessible) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1566
                return editor.getAccessibleContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
         * Returns the AccessibleText for the JSpinner editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        private AccessibleText getEditorAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            AccessibleContext ac = getEditorAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                return ac.getAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
         * Returns the AccessibleEditableText for the JSpinner editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        private AccessibleEditableText getEditorAccessibleEditableText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            if (at instanceof AccessibleEditableText) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                return (AccessibleEditableText)at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
         * Gets the AccessibleValue associated with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
         * @return AccessibleValue if supported by object; else return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
         * @see AccessibleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        public AccessibleValue getAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        /* ===== Begin AccessibleValue impl ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
         * Get the value of this object as a Number.  If the value has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
         * set, the return value will be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
         * @return value of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
         * @see #setCurrentAccessibleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        public Number getCurrentAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            Object o = model.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            if (o instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                return (Number)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
         * Set the value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
         * @param n the value to set for this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
         * @return true if the value was set; else False
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
         * @see #getCurrentAccessibleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        public boolean setCurrentAccessibleValue(Number n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            // try to set the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                model.setValue(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                // SpinnerModel didn't like new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
         * Get the minimum value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
         * @return Minimum value of the object; null if this object does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
         * have a minimum value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
         * @see #getMaximumAccessibleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        public Number getMinimumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            if (model instanceof SpinnerNumberModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                SpinnerNumberModel numberModel = (SpinnerNumberModel)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                Object o = numberModel.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                if (o instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                    return (Number)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
         * Get the maximum value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         * @return Maximum value of the object; null if this object does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         * have a maximum value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         * @see #getMinimumAccessibleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        public Number getMaximumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            if (model instanceof SpinnerNumberModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                SpinnerNumberModel numberModel = (SpinnerNumberModel)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                Object o = numberModel.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                if (o instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                    return (Number)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        /* ===== End AccessibleValue impl ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        /* ===== Begin AccessibleAction impl ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
         * Returns the number of accessible actions available in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
         * If there are more than one, the first one is considered the "default"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
         * action of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
         * Two actions are supported: AccessibleAction.INCREMENT which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
         * increments the spinner value and AccessibleAction.DECREMENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
         * which decrements the spinner value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
         * @return the zero-based number of Actions in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        public int getAccessibleActionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
         * Returns a description of the specified action of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
         * @param i zero-based index of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
         * @return a String description of the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
         * @see #getAccessibleActionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        public String getAccessibleActionDescription(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                return AccessibleAction.INCREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            } else if (i == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                return AccessibleAction.DECREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
         * Performs the specified Action on the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
         * @param i zero-based index of actions. The first action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
         * (index 0) is AccessibleAction.INCREMENT and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
         * action (index 1) is AccessibleAction.DECREMENT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
         * @return true if the action was performed; otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
         * @see #getAccessibleActionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        public boolean doAccessibleAction(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            if (i < 0 || i > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            }
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1723
            Object o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                o = getNextValue(); // AccessibleAction.INCREMENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                o = getPreviousValue(); // AccessibleAction.DECREMENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            // try to set the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                model.setValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                // SpinnerModel didn't like new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        /* ===== End AccessibleAction impl ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        /* ===== Begin AccessibleText impl ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
         * Returns whether source and destination components have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
         * same window ancestor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        private boolean sameWindowAncestor(Component src, Component dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            if (src == null || dest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            return SwingUtilities.getWindowAncestor(src) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                SwingUtilities.getWindowAncestor(dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
         * Given a point in local coordinates, return the zero-based index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
         * of the character under that Point.  If the point is invalid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
         * this method returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
         * @param p the Point in local coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
         * @return the zero-based index of the character under Point p; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
         * Point is invalid return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        public int getIndexAtPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            if (at != null && sameWindowAncestor(JSpinner.this, editor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                // convert point from the JSpinner bounds (source) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                // editor bounds (destination)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                Point editorPoint = SwingUtilities.convertPoint(JSpinner.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                                                                p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                                                                editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                if (editorPoint != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                    return at.getIndexAtPoint(editorPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
         * Determines the bounding box of the character at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
         * index into the string.  The bounds are returned in local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
         * coordinates.  If the index is invalid an empty rectangle is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
         * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
         * @param i the index into the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
         * @return the screen coordinates of the character's bounding box,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
         * if index is invalid return an empty rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        public Rectangle getCharacterBounds(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            if (at != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                Rectangle editorRect = at.getCharacterBounds(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                if (editorRect != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                    sameWindowAncestor(JSpinner.this, editor)) {
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
  1795
                    // return rectangle in the JSpinner bounds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                    return SwingUtilities.convertRectangle(editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                                                           editorRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                                                           JSpinner.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
         * Returns the number of characters (valid indicies)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
         * @return the number of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        public int getCharCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                return at.getCharCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
         * Returns the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
         * Note: That to the right of the caret will have the same index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
         * value as the offset (the caret is between two characters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
         * @return the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        public int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                return at.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
         * Returns the String at a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
         * @param part the CHARACTER, WORD, or SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
         * @return the letter, word, or sentence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        public String getAtIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                return at.getAtIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
         * Returns the String after a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
         * @param part the CHARACTER, WORD, or SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
         * @return the letter, word, or sentence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        public String getAfterIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                return at.getAfterIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
         * Returns the String before a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
         * @param part the CHARACTER, WORD, or SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
         * @return the letter, word, or sentence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        public String getBeforeIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                return at.getBeforeIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
         * Returns the AttributeSet for a given character at a given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
         * @param i the zero-based index into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         * @return the AttributeSet of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        public AttributeSet getCharacterAttribute(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                return at.getCharacterAttribute(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
         * Returns the start offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
         * @return the index into the text of the start of the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        public int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                return at.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
         * Returns the end offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
         *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1911
         * @return the index into the text of the end of the selection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        public int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                return at.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         * Returns the portion of the text that is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
         * @return the String portion of the text that is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        public String getSelectedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            AccessibleText at = getEditorAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                return at.getSelectedText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        /* ===== End AccessibleText impl ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        /* ===== Begin AccessibleEditableText impl ===== */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
         * Sets the text contents to the specified string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
         * @param s the string to set the text contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        public void setTextContents(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                at.setTextContents(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         * Inserts the specified string at the given index/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         * @param index the index in the text where the string will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         * be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         * @param s the string to insert in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        public void insertTextAtIndex(int index, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                at.insertTextAtIndex(index, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
         * Returns the text string between two indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
         * @return the text string between the indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        public String getTextRange(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                return at.getTextRange(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
         * Deletes the text between two indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        public void delete(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                at.delete(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
         * Cuts the text between two indices into the system clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        public void cut(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                at.cut(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
         * Pastes the text from the system clipboard into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
         * starting at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        public void paste(int startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                at.paste(startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
         * Replaces the text between two indices with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
         * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
         * @param s the string to replace the text between two indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        public void replaceText(int startIndex, int endIndex, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                at.replaceText(startIndex, endIndex, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
         * Selects the text between two indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        public void selectText(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                at.selectText(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
         * Sets attributes for the text between two indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
         * @param as the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
         * @see AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        public void setAttributes(int startIndex, int endIndex, AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
            AccessibleEditableText at = getEditorAccessibleEditableText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            if (at != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                at.setAttributes(startIndex, endIndex, as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
    }  /* End AccessibleJSpinner */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
}