jdk/src/share/classes/javax/swing/plaf/basic/BasicSpinnerUI.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7789 c7ebee06dfd6
child 23315 02c932be7d63
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7789
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.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
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   271
     * Creates a <code>LayoutManager</code> that manages the <code>editor</code>,
2
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
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   289
     * Creates a <code>PropertyChangeListener</code> that can be
2
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
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   305
     * Creates a decrement button, i.e. component that replaces the spinner
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   306
     * value with the object returned by <code>spinner.getPreviousValue</code>.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   307
     * By default the <code>previousButton</code> is a {@code JButton}. If the
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   308
     * decrement button is not needed this method should return {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   310
     * @return a component that will replace the spinner's value with the
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   311
     *     previous value in the sequence, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see #createNextButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @see #installPreviousButtonListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    protected Component createPreviousButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        Component c = createArrowButton(SwingConstants.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        c.setName("Spinner.previousButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        installPreviousButtonListeners(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   325
     * Creates an increment button, i.e. component that replaces the spinner
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   326
     * value with the object returned by <code>spinner.getNextValue</code>.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   327
     * By default the <code>nextButton</code> is a {@code JButton}. If the
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   328
     * increment button is not needed this method should return {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   330
     * @return a component that will replace the spinner's value with the
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   331
     *     next value in the sequence, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see #createPreviousButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @see #installNextButtonListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    protected Component createNextButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Component c = createArrowButton(SwingConstants.NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        c.setName("Spinner.nextButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        installNextButtonListeners(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private Component createArrowButton(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        JButton b = new BasicArrowButton(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        Border buttonBorder = UIManager.getBorder("Spinner.arrowButtonBorder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (buttonBorder instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // Wrap the border to avoid having the UIResource be replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // the ButtonUI. This is the opposite of using BorderUIResource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            b.setBorder(new CompoundBorder(buttonBorder, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            b.setBorder(buttonBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        b.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * This method is called by installUI to get the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * of the <code>JSpinner</code>.  By default it just returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <code>JSpinner.getEditor()</code>.  Subclasses can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <code>createEditor</code> to return a component that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * the spinner's editor or null, if they're going to handle adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * the editor to the <code>JSpinner</code> in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <code>installUI</code> override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Typically this method would be overridden to wrap the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * with a container with a custom border, since one can't assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * that the editors border can be set directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * The <code>replaceEditor</code> method is called when the spinners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * editor is changed with <code>JSpinner.setEditor</code>.  If you've
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * overriden this method, then you'll probably want to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <code>replaceEditor</code> as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return the JSpinners editor JComponent, spinner.getEditor() by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @see #replaceEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @see JSpinner#getEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    protected JComponent createEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        maybeRemoveEditorBorder(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        installEditorBorderListener(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        editor.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        updateEditorAlignment(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Called by the <code>PropertyChangeListener</code> when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <code>JSpinner</code> editor property changes.  It's the responsibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * of this method to remove the old editor and add the new one.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * default this operation is just:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * spinner.remove(oldEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * spinner.add(newEditor, "Editor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * The implementation of <code>replaceEditor</code> should be coordinated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * with the <code>createEditor</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @see #createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    protected void replaceEditor(JComponent oldEditor, JComponent newEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        spinner.remove(oldEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        maybeRemoveEditorBorder(newEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        installEditorBorderListener(newEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        newEditor.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        spinner.add(newEditor, "Editor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private void updateEditorAlignment(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (editor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // if editor alignment isn't set in LAF, we get 0 (CENTER) here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            int alignment = UIManager.getInt("Spinner.editorAlignment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            JTextField text = ((JSpinner.DefaultEditor)editor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            text.setHorizontalAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Remove the border around the inner editor component for LaFs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * that install an outside border around the spinner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private void maybeRemoveEditorBorder(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            if (editor instanceof JPanel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                editor.getBorder() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                editor.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                editor = (JComponent)editor.getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (editor != null && editor.getBorder() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                editor.setBorder(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Remove the border around the inner editor component for LaFs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * that install an outside border around the spinner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private void installEditorBorderListener(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (editor instanceof JPanel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                editor.getBorder() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                editor.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                editor = (JComponent)editor.getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (editor != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                (editor.getBorder() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                 editor.getBorder() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                editor.addPropertyChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    private void removeEditorBorderListener(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (editor instanceof JPanel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                editor.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                editor = (JComponent)editor.getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                editor.removePropertyChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Updates the enabled state of the children Components based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * enabled state of the <code>JSpinner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    private void updateEnabledState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        updateEnabledState(spinner, spinner.isEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Recursively updates the enabled state of the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <code>Component</code>s of <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private void updateEnabledState(Container c, boolean enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        for (int counter = c.getComponentCount() - 1; counter >= 0;counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            Component child = c.getComponent(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (DefaultLookup.getBoolean(spinner, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                "Spinner.disableOnBoundaryValues", false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                SpinnerModel model = spinner.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                if (child.getName() == "Spinner.nextButton" &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    model.getNextValue() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    child.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                else if (child.getName() == "Spinner.previousButton" &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                         model.getPreviousValue() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    child.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    child.setEnabled(enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                child.setEnabled(enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (child instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                updateEnabledState((Container)child, enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Installs the keyboard Actions onto the JSpinner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        InputMap iMap = getInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                   WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        SwingUtilities.replaceUIInputMap(spinner, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                         iMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        LazyActionMap.installLazyActionMap(spinner, BasicSpinnerUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                "Spinner.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Returns the InputMap to install for <code>condition</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    private InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return (InputMap)DefaultLookup.get(spinner, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    "Spinner.ancestorInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        map.put("increment", nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        map.put("decrement", previousButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        Insets insets = spinner.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        width = width - insets.left - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        height = height - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (width >= 0 && height >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            int baseline = editor.getBaseline(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if (baseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                return insets.top + baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        return spinner.getEditor().getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * A handler for spinner arrow button mouse and action events.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * a left mouse pressed event occurs we look up the (enabled) spinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * that's the source of the event and start the autorepeat timer.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * timer fires action events until any button is released at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * point the timer is stopped and the reference to the spinner cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * The timer doesn't start until after a 300ms delay, so often the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * source of the initial (and final) action event is just the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * logic for mouse released - which means that we're relying on the fact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * that our mouse listener runs after the buttons mouse listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Note that one instance of this handler is shared by all slider previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * arrow buttons and likewise for all of the next buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * so it doesn't have any state that persists beyond the limits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * of a single button pressed/released gesture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private static class ArrowButtonHandler extends AbstractAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                            implements FocusListener, MouseListener, UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        final javax.swing.Timer autoRepeatTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        final boolean isNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        JSpinner spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        JButton arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        ArrowButtonHandler(String name, boolean isNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            this.isNext = isNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            autoRepeatTimer = new javax.swing.Timer(60, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            autoRepeatTimer.setInitialDelay(300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        private JSpinner eventToSpinner(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            Object src = e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            while ((src instanceof Component) && !(src instanceof JSpinner)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                src = ((Component)src).getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            return (src instanceof JSpinner) ? (JSpinner)src : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            JSpinner spinner = this.spinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            if (!(e.getSource() instanceof javax.swing.Timer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                // Most likely resulting from being in ActionMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                spinner = eventToSpinner(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                if (e.getSource() instanceof JButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    arrowButton = (JButton)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                if (arrowButton!=null && !arrowButton.getModel().isPressed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    && autoRepeatTimer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    autoRepeatTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (spinner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    int calendarField = getCalendarField(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    spinner.commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    if (calendarField != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        ((SpinnerDateModel)spinner.getModel()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                 setCalendarField(calendarField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    Object value = (isNext) ? spinner.getNextValue() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                               spinner.getPreviousValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        spinner.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        select(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    UIManager.getLookAndFeel().provideErrorFeedback(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    UIManager.getLookAndFeel().provideErrorFeedback(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         * If the spinner's editor is a DateEditor, this selects the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         * associated with the value that is being incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        private void select(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            if (editor instanceof JSpinner.DateEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                JFormattedTextField ftf = dateEditor.getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                Format format = dateEditor.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                if (format != null && (value = spinner.getValue()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    SpinnerDateModel model = dateEditor.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    DateFormat.Field field = DateFormat.Field.ofCalendarField(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        model.getCalendarField());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    if (field != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                            AttributedCharacterIterator iterator = format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                                formatToCharacterIterator(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                            if (!select(ftf, iterator, field) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                                       field == DateFormat.Field.HOUR0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                select(ftf, iterator, DateFormat.Field.HOUR1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        catch (IllegalArgumentException iae) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * Selects the passed in field, returning true if it is found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        private boolean select(JFormattedTextField ftf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                               AttributedCharacterIterator iterator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                               DateFormat.Field field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            int max = ftf.getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            iterator.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                Map attrs = iterator.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                if (attrs != null && attrs.containsKey(field)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    int start = iterator.getRunStart(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    int end = iterator.getRunLimit(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    if (start != -1 && end != -1 && start <= max &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                                       end <= max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        ftf.select(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            } while (iterator.next() != CharacterIterator.DONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         * Returns the calendarField under the start of the selection, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
         * -1 if there is no valid calendar field under the selection (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
         * the spinner isn't editing dates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        private int getCalendarField(JSpinner spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (editor instanceof JSpinner.DateEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                JFormattedTextField ftf = dateEditor.getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                int start = ftf.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                JFormattedTextField.AbstractFormatter formatter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                    ftf.getFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if (formatter instanceof InternationalFormatter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    Format.Field[] fields = ((InternationalFormatter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                             formatter).getFields(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    for (int counter = 0; counter < fields.length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        if (fields[counter] instanceof DateFormat.Field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                            int calendarField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                            if (fields[counter] == DateFormat.Field.HOUR1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                                calendarField = Calendar.HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                                calendarField = ((DateFormat.Field)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                                        fields[counter]).getCalendarField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                            if (calendarField != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                return calendarField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            if (SwingUtilities.isLeftMouseButton(e) && e.getComponent().isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                spinner = eventToSpinner(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                autoRepeatTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                focusSpinnerIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            autoRepeatTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            if (spinner != null && !autoRepeatTimer.isRunning() && spinner == eventToSpinner(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                autoRepeatTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if (autoRepeatTimer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                autoRepeatTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * Requests focus on a child of the spinner if the spinner doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         * have focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        private void focusSpinnerIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            Component fo = KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                              getCurrentKeyboardFocusManager().getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if (spinner.isRequestFocusEnabled() && (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                        fo == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        !SwingUtilities.isDescendingFrom(fo, spinner))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                Container root = spinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                if (!root.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    root = root.getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    FocusTraversalPolicy ftp = root.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    Component child = ftp.getComponentAfter(root, spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    if (child != null && SwingUtilities.isDescendingFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                                                        child, spinner)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        child.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (spinner == eventToSpinner(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                if (autoRepeatTimer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    autoRepeatTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                spinner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                if (arrowButton != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    ButtonModel model = arrowButton.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    model.setPressed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    model.setArmed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    arrowButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    private static class Handler implements LayoutManager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            PropertyChangeListener, ChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        // LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        private Component nextButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        private Component previousButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        private Component editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        public void addLayoutComponent(String name, Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            if ("Next".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                nextButton = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            else if ("Previous".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                previousButton = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            else if ("Editor".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                editor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        public void removeLayoutComponent(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            if (c == nextButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                nextButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            else if (c == previousButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                previousButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            else if (c == editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        private Dimension preferredSize(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            return (c == null) ? zeroSize : c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            Dimension nextD = preferredSize(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            Dimension previousD = preferredSize(previousButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            Dimension editorD = preferredSize(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            /* Force the editors height to be a multiple of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            editorD.height = ((editorD.height + 1) / 2) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            Dimension size = new Dimension(editorD.width, editorD.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            size.width += Math.max(nextD.width, previousD.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            size.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            size.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return preferredLayoutSize(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        private void setBounds(Component c, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                c.setBounds(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            int width  = parent.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            int height = parent.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            Insets insets = parent.getInsets();
7789
c7ebee06dfd6 6532833: PIT: Metal LAF - The right side border is not shown for the Spinner after the removing the buttons
rupashka
parents: 5506
diff changeset
   911
c7ebee06dfd6 6532833: PIT: Metal LAF - The right side border is not shown for the Spinner after the removing the buttons
rupashka
parents: 5506
diff changeset
   912
            if (nextButton == null && previousButton == null) {
c7ebee06dfd6 6532833: PIT: Metal LAF - The right side border is not shown for the Spinner after the removing the buttons
rupashka
parents: 5506
diff changeset
   913
                setBounds(editor, insets.left,  insets.top, width - insets.left - insets.right,
c7ebee06dfd6 6532833: PIT: Metal LAF - The right side border is not shown for the Spinner after the removing the buttons
rupashka
parents: 5506
diff changeset
   914
                        height - insets.top - insets.bottom);
c7ebee06dfd6 6532833: PIT: Metal LAF - The right side border is not shown for the Spinner after the removing the buttons
rupashka
parents: 5506
diff changeset
   915
c7ebee06dfd6 6532833: PIT: Metal LAF - The right side border is not shown for the Spinner after the removing the buttons
rupashka
parents: 5506
diff changeset
   916
                return;
c7ebee06dfd6 6532833: PIT: Metal LAF - The right side border is not shown for the Spinner after the removing the buttons
rupashka
parents: 5506
diff changeset
   917
            }
c7ebee06dfd6 6532833: PIT: Metal LAF - The right side border is not shown for the Spinner after the removing the buttons
rupashka
parents: 5506
diff changeset
   918
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            Dimension nextD = preferredSize(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            Dimension previousD = preferredSize(previousButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            int buttonsWidth = Math.max(nextD.width, previousD.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            int editorHeight = height - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            // The arrowButtonInsets value is used instead of the JSpinner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            // insets if not null. Defining this to be (0, 0, 0, 0) causes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            // buttons to be aligned with the outer edge of the spinner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            // border, and leaving it as "null" places the buttons completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            // inside the spinner's border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            Insets buttonInsets = UIManager.getInsets("Spinner.arrowButtonInsets");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            if (buttonInsets == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                buttonInsets = insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            /* Deal with the spinner's componentOrientation property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            int editorX, editorWidth, buttonsX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            if (parent.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                editorX = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                editorWidth = width - insets.left - buttonsWidth - buttonInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                buttonsX = width - buttonsWidth - buttonInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                buttonsX = buttonInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                editorX = buttonsX + buttonsWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                editorWidth = width - buttonInsets.left - buttonsWidth - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            int nextY = buttonInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            int nextHeight = (height / 2) + (height % 2) - nextY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            int previousY = buttonInsets.top + nextHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            int previousHeight = height - previousY - buttonInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            setBounds(editor,         editorX,  insets.top, editorWidth, editorHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            setBounds(nextButton,     buttonsX, nextY,      buttonsWidth, nextHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            setBounds(previousButton, buttonsX, previousY,  buttonsWidth, previousHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        public void propertyChange(PropertyChangeEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (e.getSource() instanceof JSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                JSpinner spinner = (JSpinner)(e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                SpinnerUI spinnerUI = spinner.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (spinnerUI instanceof BasicSpinnerUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    if ("editor".equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                        JComponent oldEditor = (JComponent)e.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                        JComponent newEditor = (JComponent)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                        ui.replaceEditor(oldEditor, newEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                        ui.updateEnabledState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                        if (oldEditor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                            JTextField tf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                                ((JSpinner.DefaultEditor)oldEditor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                            if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                                tf.removeFocusListener(nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                                tf.removeFocusListener(previousButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                        if (newEditor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                            JTextField tf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                                ((JSpinner.DefaultEditor)newEditor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                            if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                                if (tf.getFont() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                                    tf.setFont(spinner.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                                tf.addFocusListener(nextButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                                tf.addFocusListener(previousButtonHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    else if ("enabled".equals(propertyName) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                             "model".equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                        ui.updateEnabledState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                else if ("font".equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                    if (editor!=null && editor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                        JTextField tf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                            ((JSpinner.DefaultEditor)editor).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                        if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                            if (tf.getFont() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                                tf.setFont(spinner.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                else if (JComponent.TOOL_TIP_TEXT_KEY.equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    updateToolTipTextForChildren(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            } else if (e.getSource() instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                JComponent c = (JComponent)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                if ((c.getParent() instanceof JPanel) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    (c.getParent().getParent() instanceof JSpinner) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    "border".equals(propertyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    JSpinner spinner = (JSpinner)c.getParent().getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    SpinnerUI spinnerUI = spinner.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    if (spinnerUI instanceof BasicSpinnerUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                        BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                        ui.maybeRemoveEditorBorder(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        // Syncronizes the ToolTip text for the components within the spinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        // to be the same value as the spinner ToolTip text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        private void updateToolTipTextForChildren(JComponent spinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            String toolTipText = spinner.getToolTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            Component[] children = spinner.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                if (children[i] instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    JTextField tf = ((JSpinner.DefaultEditor)children[i]).getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    if (tf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                        tf.setToolTipText(toolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                } else if (children[i] instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    ((JComponent)children[i]).setToolTipText( spinner.getToolTipText() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            if (e.getSource() instanceof JSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                JSpinner spinner = (JSpinner)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                SpinnerUI spinnerUI = spinner.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                if (DefaultLookup.getBoolean(spinner, spinnerUI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    "Spinner.disableOnBoundaryValues", false) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    spinnerUI instanceof BasicSpinnerUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    ui.updateEnabledState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
}