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