jdk/src/share/classes/javax/swing/plaf/synth/SynthSpinnerUI.java
author peterz
Sat, 25 Apr 2009 21:17:50 +0400
changeset 2658 43e06bc950ec
parent 2 90ce3da70b43
child 4394 92a8ec883f5d
permissions -rw-r--r--
6591875: Nimbus Swing Look and Feel Reviewed-by: jasper, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.basic.BasicSpinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.swing.plaf.synth.SynthUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Synth's SpinnerUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Joshua Outwater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class SynthSpinnerUI extends BasicSpinnerUI implements PropertyChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        SynthUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private SynthStyle style;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    46
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    47
     * A FocusListener implementation which causes the entire spinner to be
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    48
     * repainted whenever the editor component (typically a text field) becomes
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    49
     * focused, or loses focus. This is necessary because since SynthSpinnerUI
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    50
     * is composed of an editor and two buttons, it is necessary that all three
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    51
     * components indicate that they are "focused" so that they can be drawn
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    52
     * appropriately. The repaint is used to ensure that the buttons are drawn
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    53
     * in the new focused or unfocused state, mirroring that of the editor.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    54
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    55
    private EditorFocusHandler editorFocusHandler = new EditorFocusHandler();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Returns a new instance of SynthSpinnerUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param c the JSpinner (not used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @see ComponentUI#createUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @return a new SynthSpinnerUI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return new SynthSpinnerUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    68
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        spinner.addPropertyChangeListener(this);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    72
        JComponent editor = spinner.getEditor();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    73
        if (editor instanceof JSpinner.DefaultEditor) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    74
            JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    75
            if (tf != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    76
                tf.addFocusListener(editorFocusHandler);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    77
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    78
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Removes the <code>propertyChangeListener</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * by installListeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * This method is called by <code>uninstallUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see #installListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    89
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        spinner.removePropertyChangeListener(this);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    93
        JComponent editor = spinner.getEditor();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    94
        if (editor instanceof JSpinner.DefaultEditor) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    95
            JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    96
            if (tf != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    97
                tf.removeFocusListener(editorFocusHandler);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    98
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    99
        }
2
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
     * Initialize the <code>JSpinner</code> <code>border</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>foreground</code>, and <code>background</code>, properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * based on the corresponding "Spinner.*" properties from defaults table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * The <code>JSpinners</code> layout is set to the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <code>createLayout</code>.  This method is called by <code>installUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @see #uninstallDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see #createLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see LookAndFeel#installBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see LookAndFeel#installColors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        LayoutManager layout = spinner.getLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (layout == null || layout instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            spinner.setLayout(createLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        updateStyle(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private void updateStyle(JSpinner c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        SynthContext context = getContext(c, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        SynthStyle oldStyle = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        style = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (style != oldStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                // Only call installKeyboardActions as uninstall is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                // public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Sets the <code>JSpinner's</code> layout manager to null.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * method is called by <code>uninstallUI</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see #installDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see #uninstallUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (spinner.getLayout() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            spinner.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        SynthContext context = getContext(spinner, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        style.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected LayoutManager createLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return new SpinnerLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Create a component that will replace the spinner models value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * with the object returned by <code>spinner.getPreviousValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * By default the <code>previousButton</code> is a JButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * who's <code>ActionListener</code> updates it's <code>JSpinner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * ancestors model.  If a previousButton isn't needed (in a subclass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * then override this method to return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return a component that will replace the spinners model with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *     next value in the sequence, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see #createNextButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected Component createPreviousButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        JButton b = new SynthArrowButton(SwingConstants.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        b.setName("Spinner.previousButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        installPreviousButtonListeners(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Create a component that will replace the spinner models value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * with the object returned by <code>spinner.getNextValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * By default the <code>nextButton</code> is a JButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * who's <code>ActionListener</code> updates it's <code>JSpinner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * ancestors model.  If a nextButton isn't needed (in a subclass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * then override this method to return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return a component that will replace the spinners model with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *     next value in the sequence, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see #createPreviousButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected Component createNextButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        JButton b = new SynthArrowButton(SwingConstants.NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        b.setName("Spinner.nextButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        installNextButtonListeners(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * This method is called by installUI to get the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * of the <code>JSpinner</code>.  By default it just returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <code>JSpinner.getEditor()</code>.  Subclasses can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <code>createEditor</code> to return a component that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * the spinner's editor or null, if they're going to handle adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * the editor to the <code>JSpinner</code> in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <code>installUI</code> override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Typically this method would be overridden to wrap the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * with a container with a custom border, since one can't assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * that the editors border can be set directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * The <code>replaceEditor</code> method is called when the spinners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * editor is changed with <code>JSpinner.setEditor</code>.  If you've
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * overriden this method, then you'll probably want to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <code>replaceEditor</code> as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return the JSpinners editor JComponent, spinner.getEditor() by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see #replaceEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see JSpinner#getEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    protected JComponent createEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        editor.setName("Spinner.editor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        updateEditorAlignment(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Called by the <code>PropertyChangeListener</code> when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <code>JSpinner</code> editor property changes.  It's the responsibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * of this method to remove the old editor and add the new one.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * default this operation is just:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * spinner.remove(oldEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * spinner.add(newEditor, "Editor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * The implementation of <code>replaceEditor</code> should be coordinated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * with the <code>createEditor</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see #createEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    protected void replaceEditor(JComponent oldEditor, JComponent newEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        spinner.remove(oldEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        spinner.add(newEditor, "Editor");
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   256
        if (oldEditor instanceof JSpinner.DefaultEditor) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   257
            JTextField tf = ((JSpinner.DefaultEditor)oldEditor).getTextField();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   258
            if (tf != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   259
                tf.removeFocusListener(editorFocusHandler);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   260
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   261
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   262
        if (newEditor instanceof JSpinner.DefaultEditor) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   263
            JTextField tf = ((JSpinner.DefaultEditor)newEditor).getTextField();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   264
            if (tf != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   265
                tf.addFocusListener(editorFocusHandler);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   266
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   267
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private void updateEditorAlignment(JComponent editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (editor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            SynthContext context = getContext(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            Integer alignment = (Integer)context.getStyle().get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    context, "Spinner.editorAlignment");
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   275
            JTextField text = ((JSpinner.DefaultEditor)editor).getTextField();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (alignment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                text.setHorizontalAlignment(alignment);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   278
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   280
            // copy across the sizeVariant property to the editor
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   281
            text.putClientProperty("JComponent.sizeVariant",
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   282
                    spinner.getClientProperty("JComponent.sizeVariant"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public SynthContext getContext(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return getContext(c, getComponentState(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private SynthContext getContext(JComponent c, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return SynthContext.getContext(SynthContext.class, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    SynthLookAndFeel.getRegion(c), style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    private Region getRegion(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return SynthLookAndFeel.getRegion(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private int getComponentState(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return SynthLookAndFeel.getComponentState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        SynthLookAndFeel.update(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        context.getPainter().paintSpinnerBackground(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                          g, 0, 0, c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        context.getPainter().paintSpinnerBorder(context, g, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * A simple layout manager for the editor and the next/previous buttons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * See the SynthSpinnerUI javadoc for more information about exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * how the components are arranged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private static class SpinnerLayout implements LayoutManager, UIResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        private Component nextButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        private Component previousButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        private Component editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        public void addLayoutComponent(String name, Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if ("Next".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                nextButton = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            else if ("Previous".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                previousButton = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            else if ("Editor".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                editor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        public void removeLayoutComponent(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (c == nextButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                nextButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            else if (c == previousButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                previousButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            else if (c == editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        private Dimension preferredSize(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return (c == null) ? new Dimension(0, 0) : c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            Dimension nextD = preferredSize(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            Dimension previousD = preferredSize(previousButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            Dimension editorD = preferredSize(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            /* Force the editors height to be a multiple of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            editorD.height = ((editorD.height + 1) / 2) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            Dimension size = new Dimension(editorD.width, editorD.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            size.width += Math.max(nextD.width, previousD.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            size.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            size.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            return preferredLayoutSize(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        private void setBounds(Component c, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                c.setBounds(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            int availWidth = parent.getWidth() - (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            int availHeight = parent.getHeight() - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            Dimension nextD = preferredSize(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            Dimension previousD = preferredSize(previousButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            int nextHeight = availHeight / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            int previousHeight = availHeight - nextHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            int buttonsWidth = Math.max(nextD.width, previousD.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            int editorWidth = availWidth - buttonsWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            /* Deal with the spinners componentOrientation property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            int editorX, buttonsX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (parent.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                editorX = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                buttonsX = editorX + editorWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                buttonsX = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                editorX = buttonsX + buttonsWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            int previousY = insets.top + nextHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            setBounds(editor, editorX, insets.top, editorWidth, availHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            setBounds(nextButton, buttonsX, insets.top, buttonsWidth, nextHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            setBounds(previousButton, buttonsX, previousY, buttonsWidth, previousHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        JSpinner spinner = (JSpinner)(e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        SpinnerUI spinnerUI = spinner.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (spinnerUI instanceof SynthSpinnerUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            SynthSpinnerUI ui = (SynthSpinnerUI)spinnerUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (SynthLookAndFeel.shouldUpdateStyle(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                ui.updateStyle(spinner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   443
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   444
    /** Listen to editor text field focus changes and repaint whole spinner */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   445
    private class EditorFocusHandler implements FocusListener{
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   446
        /** Invoked when a editor text field gains the keyboard focus. */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   447
        public void focusGained(FocusEvent e) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   448
            spinner.repaint();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   449
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   450
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   451
        /** Invoked when a editor text field loses the keyboard focus. */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   452
        public void focusLost(FocusEvent e) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   453
            spinner.repaint();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   454
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   455
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   456
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   457
    /** Override the arrowbuttons focus handling to follow the text fields focus */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   458
    private class SpinnerArrowButton extends SynthArrowButton{
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   459
        public SpinnerArrowButton(int direction) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   460
            super(direction);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   461
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   462
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   463
        @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   464
        public boolean isFocusOwner() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   465
            if (spinner == null){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   466
                return super.isFocusOwner();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   467
            } else if (spinner.getEditor() instanceof JSpinner.DefaultEditor){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   468
                return ((JSpinner.DefaultEditor)spinner.getEditor())
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   469
                        .getTextField().isFocusOwner();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   470
            } else if (spinner.getEditor()!= null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   471
                return spinner.getEditor().isFocusOwner();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   472
            } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   473
                return super.isFocusOwner();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   474
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   475
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   476
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
}