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