jdk/src/share/classes/javax/swing/plaf/basic/BasicSpinnerUI.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4394 92a8ec883f5d
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.plaf.basic;
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
import java.text.ParseException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The default Spinner UI delegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class BasicSpinnerUI extends SpinnerUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * The spinner that we're a UI delegate for.  Initialized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * the <code>installUI</code> method, and reset to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * by <code>uninstallUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @see #uninstallUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected JSpinner spinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The mouse/action listeners that are added to the spinner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * arrow buttons.  These listeners are shared by all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * spinner arrow buttons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @see #createNextButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see #createPreviousButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final ArrowButtonHandler nextButtonHandler = new ArrowButtonHandler("increment", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final ArrowButtonHandler previousButtonHandler = new ArrowButtonHandler("decrement", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Used by the default LayoutManager class - SpinnerLayout for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * missing (null) editor/nextButton/previousButton children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final Dimension zeroSize = new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Returns a new instance of BasicSpinnerUI.  SpinnerListUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * delegates are allocated one per JSpinner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param c the JSpinner (not used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @see ComponentUI#createUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @return a new BasicSpinnerUI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return new BasicSpinnerUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private void maybeAdd(Component c, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            spinner.add(c, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Calls <code>installDefaults</code>, <code>installListeners</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * and then adds the components returned by <code>createNextButton</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <code>createPreviousButton</code>, and <code>createEditor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param c the JSpinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see #installDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see #installListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see #createNextButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see #createPreviousButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @see #createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.spinner = (JSpinner)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        maybeAdd(createNextButton(), "Next");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        maybeAdd(createPreviousButton(), "Previous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        maybeAdd(createEditor(), "Editor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        updateEnabledState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Calls <code>uninstallDefaults</code>, <code>uninstallListeners</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * and then removes all of the spinners children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param c the JSpinner (not used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        c.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Initializes <code>PropertyChangeListener</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * a shared object that delegates interesting PropertyChangeEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * to protected methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * This method is called by <code>installUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see #replaceEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see #uninstallListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        propertyChangeListener = createPropertyChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        spinner.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (DefaultLookup.getBoolean(spinner, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            "Spinner.disableOnBoundaryValues", false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            spinner.addChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (editor != null && editor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                tf.addFocusListener(nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                tf.addFocusListener(previousButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Removes the <code>PropertyChangeListener</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * by installListeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * This method is called by <code>uninstallUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see #installListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        spinner.removePropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        spinner.removeChangeListener(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        removeEditorBorderListener(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (editor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                tf.removeFocusListener(nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                tf.removeFocusListener(previousButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Initialize the <code>JSpinner</code> <code>border</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <code>foreground</code>, and <code>background</code>, properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * based on the corresponding "Spinner.*" properties from defaults table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * The <code>JSpinners</code> layout is set to the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <code>createLayout</code>.  This method is called by <code>installUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @see #uninstallDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @see #createLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @see LookAndFeel#installBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @see LookAndFeel#installColors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        spinner.setLayout(createLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        LookAndFeel.installBorder(spinner, "Spinner.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        LookAndFeel.installColorsAndFont(spinner, "Spinner.background", "Spinner.foreground", "Spinner.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        LookAndFeel.installProperty(spinner, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
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
     * Sets the <code>JSpinner's</code> layout manager to null.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * method is called by <code>uninstallUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see #installDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see #uninstallUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        spinner.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Installs the necessary listeners on the next button, <code>c</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * to update the <code>JSpinner</code> in response to a user gesture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param c Component to install the listeners on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @throws NullPointerException if <code>c</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see #createNextButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    protected void installNextButtonListeners(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        installButtonListeners(c, nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Installs the necessary listeners on the previous button, <code>c</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * to update the <code>JSpinner</code> in response to a user gesture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param c Component to install the listeners on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws NullPointerException if <code>c</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see #createPreviousButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected void installPreviousButtonListeners(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        installButtonListeners(c, previousButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private void installButtonListeners(Component c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                        ArrowButtonHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (c instanceof JButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            ((JButton)c).addActionListener(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        c.addMouseListener(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Create a <code>LayoutManager</code> that manages the <code>editor</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <code>nextButton</code>, and <code>previousButton</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * children of the JSpinner.  These three children must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * added with a constraint that identifies their role:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * "Editor", "Next", and "Previous". The default layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * can handle the absence of any of these children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @return a LayoutManager for the editor, next button, and previous button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @see #createNextButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see #createPreviousButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @see #createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    protected LayoutManager createLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Create a <code>PropertyChangeListener</code> that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * added to the JSpinner itself.  Typically, this listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * will call replaceEditor when the "editor" property changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * since it's the <code>SpinnerUI's</code> responsibility to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * add the editor to the JSpinner (and remove the old one).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * This method is called by <code>installListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return A PropertyChangeListener for the JSpinner itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see #installListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Create a component that will replace the spinner models value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * with the object returned by <code>spinner.getPreviousValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * By default the <code>previousButton</code> is a JButton. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * method invokes <code>installPreviousButtonListeners</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * install the necessary listeners to update the <code>JSpinner</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * model in response to a user gesture. If a previousButton isn't needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * (in a subclass) then override this method to return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return a component that will replace the spinners model with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *     next value in the sequence, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @see #createNextButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @see #installPreviousButtonListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    protected Component createPreviousButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        Component c = createArrowButton(SwingConstants.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        c.setName("Spinner.previousButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        installPreviousButtonListeners(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Create a component that will replace the spinner models value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * with the object returned by <code>spinner.getNextValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * By default the <code>nextButton</code> is a JButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * who's <code>ActionListener</code> updates it's <code>JSpinner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * ancestors model.  If a nextButton isn't needed (in a subclass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * then override this method to return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return a component that will replace the spinners model with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *     next value in the sequence, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @see #createPreviousButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @see #installNextButtonListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    protected Component createNextButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        Component c = createArrowButton(SwingConstants.NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        c.setName("Spinner.nextButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        installNextButtonListeners(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private Component createArrowButton(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        JButton b = new BasicArrowButton(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        Border buttonBorder = UIManager.getBorder("Spinner.arrowButtonBorder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (buttonBorder instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // Wrap the border to avoid having the UIResource be replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            // the ButtonUI. This is the opposite of using BorderUIResource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            b.setBorder(new CompoundBorder(buttonBorder, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            b.setBorder(buttonBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        b.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * This method is called by installUI to get the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * of the <code>JSpinner</code>.  By default it just returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <code>JSpinner.getEditor()</code>.  Subclasses can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <code>createEditor</code> to return a component that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * the spinner's editor or null, if they're going to handle adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * the editor to the <code>JSpinner</code> in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <code>installUI</code> override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Typically this method would be overridden to wrap the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * with a container with a custom border, since one can't assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * that the editors border can be set directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * The <code>replaceEditor</code> method is called when the spinners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * editor is changed with <code>JSpinner.setEditor</code>.  If you've
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * overriden this method, then you'll probably want to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <code>replaceEditor</code> as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @return the JSpinners editor JComponent, spinner.getEditor() by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see #replaceEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see JSpinner#getEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    protected JComponent createEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        maybeRemoveEditorBorder(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        installEditorBorderListener(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        editor.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        updateEditorAlignment(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Called by the <code>PropertyChangeListener</code> when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <code>JSpinner</code> editor property changes.  It's the responsibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * of this method to remove the old editor and add the new one.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * default this operation is just:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * spinner.remove(oldEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * spinner.add(newEditor, "Editor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * The implementation of <code>replaceEditor</code> should be coordinated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * with the <code>createEditor</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see #createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    protected void replaceEditor(JComponent oldEditor, JComponent newEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        spinner.remove(oldEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        maybeRemoveEditorBorder(newEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        installEditorBorderListener(newEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        newEditor.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        spinner.add(newEditor, "Editor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private void updateEditorAlignment(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (editor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            // if editor alignment isn't set in LAF, we get 0 (CENTER) here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            int alignment = UIManager.getInt("Spinner.editorAlignment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            JTextField text = ((JSpinner.DefaultEditor)editor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            text.setHorizontalAlignment(alignment);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Remove the border around the inner editor component for LaFs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * that install an outside border around the spinner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private void maybeRemoveEditorBorder(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (editor instanceof JPanel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                editor.getBorder() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                editor.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                editor = (JComponent)editor.getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (editor != null && editor.getBorder() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                editor.setBorder(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Remove the border around the inner editor component for LaFs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * that install an outside border around the spinner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private void installEditorBorderListener(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            if (editor instanceof JPanel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                editor.getBorder() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                editor.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                editor = (JComponent)editor.getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (editor != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                (editor.getBorder() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                 editor.getBorder() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                editor.addPropertyChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    private void removeEditorBorderListener(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (editor instanceof JPanel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                editor.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                editor = (JComponent)editor.getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                editor.removePropertyChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Updates the enabled state of the children Components based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * enabled state of the <code>JSpinner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private void updateEnabledState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        updateEnabledState(spinner, spinner.isEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Recursively updates the enabled state of the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <code>Component</code>s of <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private void updateEnabledState(Container c, boolean enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        for (int counter = c.getComponentCount() - 1; counter >= 0;counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            Component child = c.getComponent(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (DefaultLookup.getBoolean(spinner, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                "Spinner.disableOnBoundaryValues", false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                SpinnerModel model = spinner.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                if (child.getName() == "Spinner.nextButton" &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    model.getNextValue() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    child.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                else if (child.getName() == "Spinner.previousButton" &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                         model.getPreviousValue() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    child.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    child.setEnabled(enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                child.setEnabled(enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (child instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                updateEnabledState((Container)child, enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Installs the keyboard Actions onto the JSpinner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        InputMap iMap = getInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                   WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        SwingUtilities.replaceUIInputMap(spinner, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                         iMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        LazyActionMap.installLazyActionMap(spinner, BasicSpinnerUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                "Spinner.actionMap");
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
     * Returns the InputMap to install for <code>condition</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            return (InputMap)DefaultLookup.get(spinner, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    "Spinner.ancestorInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        map.put("increment", nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        map.put("decrement", previousButtonHandler);
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
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        Insets insets = spinner.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        width = width - insets.left - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        height = height - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (width >= 0 && height >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            int baseline = editor.getBaseline(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (baseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                return insets.top + baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        return spinner.getEditor().getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * A handler for spinner arrow button mouse and action events.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * a left mouse pressed event occurs we look up the (enabled) spinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * that's the source of the event and start the autorepeat timer.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * timer fires action events until any button is released at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * point the timer is stopped and the reference to the spinner cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * The timer doesn't start until after a 300ms delay, so often the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * source of the initial (and final) action event is just the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * logic for mouse released - which means that we're relying on the fact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * that our mouse listener runs after the buttons mouse listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Note that one instance of this handler is shared by all slider previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * arrow buttons and likewise for all of the next buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * so it doesn't have any state that persists beyond the limits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * of a single button pressed/released gesture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private static class ArrowButtonHandler extends AbstractAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                            implements FocusListener, MouseListener, UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        final javax.swing.Timer autoRepeatTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        final boolean isNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        JSpinner spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        JButton arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        ArrowButtonHandler(String name, boolean isNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            this.isNext = isNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            autoRepeatTimer = new javax.swing.Timer(60, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            autoRepeatTimer.setInitialDelay(300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        private JSpinner eventToSpinner(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            Object src = e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            while ((src instanceof Component) && !(src instanceof JSpinner)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                src = ((Component)src).getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            return (src instanceof JSpinner) ? (JSpinner)src : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            JSpinner spinner = this.spinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if (!(e.getSource() instanceof javax.swing.Timer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                // Most likely resulting from being in ActionMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                spinner = eventToSpinner(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                if (e.getSource() instanceof JButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    arrowButton = (JButton)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                if (arrowButton!=null && !arrowButton.getModel().isPressed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    && autoRepeatTimer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    autoRepeatTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (spinner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    int calendarField = getCalendarField(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    spinner.commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    if (calendarField != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        ((SpinnerDateModel)spinner.getModel()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                 setCalendarField(calendarField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    Object value = (isNext) ? spinner.getNextValue() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                               spinner.getPreviousValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        spinner.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        select(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    UIManager.getLookAndFeel().provideErrorFeedback(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    UIManager.getLookAndFeel().provideErrorFeedback(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         * If the spinner's editor is a DateEditor, this selects the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * associated with the value that is being incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        private void select(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            if (editor instanceof JSpinner.DateEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                JFormattedTextField ftf = dateEditor.getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                Format format = dateEditor.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                if (format != null && (value = spinner.getValue()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    SpinnerDateModel model = dateEditor.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    DateFormat.Field field = DateFormat.Field.ofCalendarField(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        model.getCalendarField());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    if (field != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                            AttributedCharacterIterator iterator = format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                formatToCharacterIterator(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                            if (!select(ftf, iterator, field) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                                       field == DateFormat.Field.HOUR0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                                select(ftf, iterator, DateFormat.Field.HOUR1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        catch (IllegalArgumentException iae) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         * Selects the passed in field, returning true if it is found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        private boolean select(JFormattedTextField ftf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                               AttributedCharacterIterator iterator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                               DateFormat.Field field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            int max = ftf.getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            iterator.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                Map attrs = iterator.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                if (attrs != null && attrs.containsKey(field)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    int start = iterator.getRunStart(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    int end = iterator.getRunLimit(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    if (start != -1 && end != -1 && start <= max &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                                       end <= max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        ftf.select(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            } while (iterator.next() != CharacterIterator.DONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
         * Returns the calendarField under the start of the selection, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
         * -1 if there is no valid calendar field under the selection (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         * the spinner isn't editing dates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        private int getCalendarField(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (editor instanceof JSpinner.DateEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                JFormattedTextField ftf = dateEditor.getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                int start = ftf.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                JFormattedTextField.AbstractFormatter formatter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                                    ftf.getFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                if (formatter instanceof InternationalFormatter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    Format.Field[] fields = ((InternationalFormatter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                                             formatter).getFields(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    for (int counter = 0; counter < fields.length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        if (fields[counter] instanceof DateFormat.Field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                            int calendarField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                            if (fields[counter] == DateFormat.Field.HOUR1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                calendarField = Calendar.HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                calendarField = ((DateFormat.Field)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                        fields[counter]).getCalendarField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                            if (calendarField != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                return calendarField;
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
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            if (SwingUtilities.isLeftMouseButton(e) && e.getComponent().isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                spinner = eventToSpinner(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                autoRepeatTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                focusSpinnerIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            autoRepeatTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (spinner != null && !autoRepeatTimer.isRunning() && spinner == eventToSpinner(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                autoRepeatTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            if (autoRepeatTimer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                autoRepeatTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * Requests focus on a child of the spinner if the spinner doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         * have focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        private void focusSpinnerIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            Component fo = KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                              getCurrentKeyboardFocusManager().getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            if (spinner.isRequestFocusEnabled() && (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                        fo == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                        !SwingUtilities.isDescendingFrom(fo, spinner))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                Container root = spinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                if (!root.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    root = root.getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    FocusTraversalPolicy ftp = root.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    Component child = ftp.getComponentAfter(root, spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    if (child != null && SwingUtilities.isDescendingFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                                                        child, spinner)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                        child.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            if (spinner == eventToSpinner(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                if (autoRepeatTimer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    autoRepeatTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                if (arrowButton != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    ButtonModel model = arrowButton.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    model.setPressed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    model.setArmed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    private static class Handler implements LayoutManager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            PropertyChangeListener, ChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        // LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        private Component nextButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        private Component previousButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        private Component editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        public void addLayoutComponent(String name, Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            if ("Next".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                nextButton = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            else if ("Previous".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                previousButton = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            else if ("Editor".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                editor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        public void removeLayoutComponent(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            if (c == nextButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                nextButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            else if (c == previousButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                previousButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            else if (c == editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        private Dimension preferredSize(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            return (c == null) ? zeroSize : c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            Dimension nextD = preferredSize(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            Dimension previousD = preferredSize(previousButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            Dimension editorD = preferredSize(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            /* Force the editors height to be a multiple of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            editorD.height = ((editorD.height + 1) / 2) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            Dimension size = new Dimension(editorD.width, editorD.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            size.width += Math.max(nextD.width, previousD.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            size.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            size.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            return preferredLayoutSize(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        private void setBounds(Component c, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                c.setBounds(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            int width  = parent.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            int height = parent.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            Dimension nextD = preferredSize(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            Dimension previousD = preferredSize(previousButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            int buttonsWidth = Math.max(nextD.width, previousD.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            int editorHeight = height - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            // The arrowButtonInsets value is used instead of the JSpinner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            // insets if not null. Defining this to be (0, 0, 0, 0) causes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            // buttons to be aligned with the outer edge of the spinner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            // border, and leaving it as "null" places the buttons completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            // inside the spinner's border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            Insets buttonInsets = UIManager.getInsets("Spinner.arrowButtonInsets");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            if (buttonInsets == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                buttonInsets = insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            /* Deal with the spinner's componentOrientation property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            int editorX, editorWidth, buttonsX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            if (parent.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                editorX = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                editorWidth = width - insets.left - buttonsWidth - buttonInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                buttonsX = width - buttonsWidth - buttonInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                buttonsX = buttonInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                editorX = buttonsX + buttonsWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                editorWidth = width - buttonInsets.left - buttonsWidth - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            int nextY = buttonInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            int nextHeight = (height / 2) + (height % 2) - nextY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            int previousY = buttonInsets.top + nextHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            int previousHeight = height - previousY - buttonInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            setBounds(editor,         editorX,  insets.top, editorWidth, editorHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            setBounds(nextButton,     buttonsX, nextY,      buttonsWidth, nextHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            setBounds(previousButton, buttonsX, previousY,  buttonsWidth, previousHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        public void propertyChange(PropertyChangeEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            if (e.getSource() instanceof JSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                JSpinner spinner = (JSpinner)(e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                SpinnerUI spinnerUI = spinner.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                if (spinnerUI instanceof BasicSpinnerUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    if ("editor".equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                        JComponent oldEditor = (JComponent)e.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        JComponent newEditor = (JComponent)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                        ui.replaceEditor(oldEditor, newEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                        ui.updateEnabledState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                        if (oldEditor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                            JTextField tf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                                ((JSpinner.DefaultEditor)oldEditor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                            if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                                tf.removeFocusListener(nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                                tf.removeFocusListener(previousButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                        if (newEditor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                            JTextField tf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                                ((JSpinner.DefaultEditor)newEditor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                            if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                                if (tf.getFont() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                                    tf.setFont(spinner.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                                tf.addFocusListener(nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                                tf.addFocusListener(previousButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    else if ("enabled".equals(propertyName) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                             "model".equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                        ui.updateEnabledState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                else if ("font".equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                    JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    if (editor!=null && editor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                        JTextField tf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                            ((JSpinner.DefaultEditor)editor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                        if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                            if (tf.getFont() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                                tf.setFont(spinner.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                else if (JComponent.TOOL_TIP_TEXT_KEY.equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    updateToolTipTextForChildren(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            } else if (e.getSource() instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                JComponent c = (JComponent)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                if ((c.getParent() instanceof JPanel) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    (c.getParent().getParent() instanceof JSpinner) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    "border".equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    JSpinner spinner = (JSpinner)c.getParent().getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    SpinnerUI spinnerUI = spinner.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    if (spinnerUI instanceof BasicSpinnerUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                        BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                        ui.maybeRemoveEditorBorder(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        // Syncronizes the ToolTip text for the components within the spinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        // to be the same value as the spinner ToolTip text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        private void updateToolTipTextForChildren(JComponent spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            String toolTipText = spinner.getToolTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            Component[] children = spinner.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                if (children[i] instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    JTextField tf = ((JSpinner.DefaultEditor)children[i]).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                    if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        tf.setToolTipText(toolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                } else if (children[i] instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    ((JComponent)children[i]).setToolTipText( spinner.getToolTipText() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            if (e.getSource() instanceof JSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                JSpinner spinner = (JSpinner)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                SpinnerUI spinnerUI = spinner.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                if (DefaultLookup.getBoolean(spinner, spinnerUI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                    "Spinner.disableOnBoundaryValues", false) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                    spinnerUI instanceof BasicSpinnerUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    ui.updateEnabledState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
}