1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.beans.*;
  31 import java.text.*;
  32 import java.text.AttributedCharacterIterator.Attribute;
  33 import java.text.Format.Field;
  34 import java.util.*;
  35 
  36 import javax.swing.*;
  37 import javax.swing.JSpinner.DefaultEditor;
  38 import javax.swing.plaf.*;
  39 import javax.swing.text.InternationalFormatter;
  40 
  41 import apple.laf.*;
  42 import apple.laf.JRSUIConstants.*;
  43 
  44 import com.apple.laf.AquaUtils.RecyclableSingleton;
  45 import com.apple.laf.AquaUtils.RecyclableSingletonFromDefaultConstructor;
  46 
  47 /**
  48  * This is originally derived from BasicSpinnerUI, but they made everything private
  49  * so we can't subclass!
  50  */
  51 public class AquaSpinnerUI extends SpinnerUI {
  52     private static final RecyclableSingleton<? extends PropertyChangeListener> propertyChangeListener = new RecyclableSingletonFromDefaultConstructor<PropertyChangeHandler>(PropertyChangeHandler.class);
  53     static PropertyChangeListener getPropertyChangeListener() {
  54         return propertyChangeListener.get();
  55     }
  56 
  57     private static final RecyclableSingleton<ArrowButtonHandler> nextButtonHandler = new RecyclableSingleton<ArrowButtonHandler>() {
  58         @Override
  59         protected ArrowButtonHandler getInstance() {
  60             return new ArrowButtonHandler("increment", true);
  61         }
  62     };
  63     static ArrowButtonHandler getNextButtonHandler() {
  64         return nextButtonHandler.get();
  65     }
  66     private static final RecyclableSingleton<ArrowButtonHandler> previousButtonHandler = new RecyclableSingleton<ArrowButtonHandler>() {
  67         @Override
  68         protected ArrowButtonHandler getInstance() {
  69             return new ArrowButtonHandler("decrement", false);
  70         }
  71     };
  72     static ArrowButtonHandler getPreviousButtonHandler() {
  73         return previousButtonHandler.get();
  74     }
  75 
  76     JSpinner spinner;
  77     SpinPainter spinPainter;
  78 
  79     public static ComponentUI createUI(final JComponent c) {
  80         return new AquaSpinnerUI();
  81     }
  82 
  83     private void maybeAdd(final Component c, final String s) {
  84         if (c != null) {
  85             spinner.add(c, s);
  86         }
  87     }
  88 
  89     boolean wasOpaque;
  90     public void installUI(final JComponent c) {
  91         this.spinner = (JSpinner)c;
  92         installDefaults();
  93         installListeners();
  94         final TransparentButton next = createNextButton();
  95         final TransparentButton prev = createPreviousButton();
  96         spinPainter = new SpinPainter(next, prev);
  97 
  98         maybeAdd(next, "Next");
  99         maybeAdd(prev, "Previous");
 100         maybeAdd(createEditor(), "Editor");
 101         maybeAdd(spinPainter, "Painter");
 102 
 103         updateEnabledState();
 104         installKeyboardActions();
 105 
 106         // this doesn't work because JSpinner calls setOpaque(true) directly in it's constructor
 107     //    LookAndFeel.installProperty(spinner, "opaque", Boolean.FALSE);
 108 
 109         // ...so we have to handle the is/was opaque ourselves
 110         wasOpaque = spinner.isOpaque();
 111         spinner.setOpaque(false);
 112     }
 113 
 114     public void uninstallUI(final JComponent c) {
 115         uninstallDefaults();
 116         uninstallListeners();
 117         spinner.setOpaque(wasOpaque);
 118         spinner = null;
 119         c.removeAll();
 120     }
 121 
 122     protected void installListeners() {
 123         spinner.addPropertyChangeListener(getPropertyChangeListener());
 124     }
 125 
 126     protected void uninstallListeners() {
 127         spinner.removePropertyChangeListener(getPropertyChangeListener());
 128     }
 129 
 130     protected void installDefaults() {
 131         spinner.setLayout(createLayout());
 132         LookAndFeel.installBorder(spinner, "Spinner.border");
 133         LookAndFeel.installColorsAndFont(spinner, "Spinner.background", "Spinner.foreground", "Spinner.font");
 134     }
 135 
 136     protected void uninstallDefaults() {
 137         spinner.setLayout(null);
 138     }
 139 
 140     protected LayoutManager createLayout() {
 141         return new SpinnerLayout();
 142     }
 143 
 144     protected PropertyChangeListener createPropertyChangeListener() {
 145         return new PropertyChangeHandler();
 146     }
 147 
 148     protected TransparentButton createPreviousButton() {
 149         final TransparentButton b = new TransparentButton();
 150         b.addActionListener(getPreviousButtonHandler());
 151         b.addMouseListener(getPreviousButtonHandler());
 152         b.setInheritsPopupMenu(true);
 153         return b;
 154     }
 155 
 156     protected TransparentButton createNextButton() {
 157         final TransparentButton b = new TransparentButton();
 158         b.addActionListener(getNextButtonHandler());
 159         b.addMouseListener(getNextButtonHandler());
 160         b.setInheritsPopupMenu(true);
 161         return b;
 162     }
 163 
 164     class TransparentButton extends JButton implements SwingConstants {
 165         boolean interceptRepaints = false;
 166 
 167         public TransparentButton() {
 168             super();
 169             setFocusable(false);
 170             // only intercept repaints if we are after this has been initialized
 171             // otherwise we can't talk to our containing class
 172             interceptRepaints = true;
 173         }
 174 
 175         public void paint(final Graphics g) {}
 176 
 177         public void repaint() {
 178             // only intercept repaints if we are after this has been initialized
 179             // otherwise we can't talk to our containing class
 180             if (interceptRepaints) {
 181                 if (spinPainter == null) return;
 182                 spinPainter.repaint();
 183             }
 184             super.repaint();
 185         }
 186     }
 187 
 188     protected JComponent createEditor() {
 189         final JComponent editor = spinner.getEditor();
 190         fixupEditor(editor);
 191         return editor;
 192     }
 193 
 194     protected void replaceEditor(final JComponent oldEditor, final JComponent newEditor) {
 195         spinner.remove(oldEditor);
 196         fixupEditor(newEditor);
 197         spinner.add(newEditor, "Editor");
 198     }
 199 
 200     protected void fixupEditor(final JComponent editor) {
 201         if (!(editor instanceof DefaultEditor)) return;
 202 
 203         editor.setOpaque(false);
 204         editor.setInheritsPopupMenu(true);
 205 
 206         if (editor.getFont() instanceof UIResource) {
 207             editor.setFont(spinner.getFont());
 208         }
 209 
 210         final JFormattedTextField editorTextField = ((DefaultEditor)editor).getTextField();
 211         if (editorTextField.getFont() instanceof UIResource) {
 212             editorTextField.setFont(spinner.getFont());
 213         }
 214         final InputMap spinnerInputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
 215         final InputMap editorInputMap = editorTextField.getInputMap();
 216         final KeyStroke[] keys = spinnerInputMap.keys();
 217         for (final KeyStroke k : keys) {
 218             editorInputMap.put(k, spinnerInputMap.get(k));
 219         }
 220     }
 221 
 222     void updateEnabledState() {
 223         updateEnabledState(spinner, spinner.isEnabled());
 224     }
 225 
 226     private void updateEnabledState(final Container c, final boolean enabled) {
 227         for (int counter = c.getComponentCount() - 1; counter >= 0; counter--) {
 228             final Component child = c.getComponent(counter);
 229 
 230             child.setEnabled(enabled);
 231             if (child instanceof Container) {
 232                 updateEnabledState((Container)child, enabled);
 233             }
 234         }
 235     }
 236 
 237     private void installKeyboardActions() {
 238         final InputMap iMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
 239         SwingUtilities.replaceUIInputMap(spinner, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, iMap);
 240         SwingUtilities.replaceUIActionMap(spinner, getActionMap());
 241     }
 242 
 243     private InputMap getInputMap(final int condition) {
 244         if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
 245             return (InputMap)UIManager.get("Spinner.ancestorInputMap");
 246         }
 247         return null;
 248     }
 249 
 250     private ActionMap getActionMap() {
 251         ActionMap map = (ActionMap)UIManager.get("Spinner.actionMap");
 252 
 253         if (map == null) {
 254             map = createActionMap();
 255             if (map != null) {
 256                 UIManager.getLookAndFeelDefaults().put("Spinner.actionMap", map);
 257             }
 258         }
 259         return map;
 260     }
 261 
 262     private ActionMap createActionMap() {
 263         final ActionMap map = new ActionMapUIResource();
 264         map.put("increment", getNextButtonHandler());
 265         map.put("decrement", getPreviousButtonHandler());
 266         return map;
 267     }
 268 
 269     private static class ArrowButtonHandler extends AbstractAction implements MouseListener {
 270         final javax.swing.Timer autoRepeatTimer;
 271         final boolean isNext;
 272         JSpinner spinner = null;
 273 
 274         ArrowButtonHandler(final String name, final boolean isNext) {
 275             super(name);
 276             this.isNext = isNext;
 277             autoRepeatTimer = new javax.swing.Timer(60, this);
 278             autoRepeatTimer.setInitialDelay(300);
 279         }
 280 
 281         private JSpinner eventToSpinner(final AWTEvent e) {
 282             Object src = e.getSource();
 283             while ((src instanceof Component) && !(src instanceof JSpinner)) {
 284                 src = ((Component)src).getParent();
 285             }
 286             return (src instanceof JSpinner) ? (JSpinner)src : null;
 287         }
 288 
 289         public void actionPerformed(final ActionEvent e) {
 290             if (!(e.getSource() instanceof javax.swing.Timer)) {
 291                 // Most likely resulting from being in ActionMap.
 292                 spinner = eventToSpinner(e);
 293             }
 294 
 295             if (spinner == null) return;
 296 
 297             try {
 298                 final int calendarField = getCalendarField(spinner);
 299                 spinner.commitEdit();
 300                 if (calendarField != -1) {
 301                     ((SpinnerDateModel)spinner.getModel()).setCalendarField(calendarField);
 302                 }
 303                 final Object value = (isNext) ? spinner.getNextValue() : spinner.getPreviousValue();
 304                 if (value != null) {
 305                     spinner.setValue(value);
 306                     select(spinner);
 307                 }
 308             } catch (final IllegalArgumentException iae) {
 309                 UIManager.getLookAndFeel().provideErrorFeedback(spinner);
 310             } catch (final ParseException pe) {
 311                 UIManager.getLookAndFeel().provideErrorFeedback(spinner);
 312             }
 313         }
 314 
 315         /**
 316          * If the spinner's editor is a DateEditor, this selects the field
 317          * associated with the value that is being incremented.
 318          */
 319         private void select(final JSpinner spinnerComponent) {
 320             final JComponent editor = spinnerComponent.getEditor();
 321             if (!(editor instanceof JSpinner.DateEditor)) return;
 322 
 323             final JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor;
 324             final JFormattedTextField ftf = dateEditor.getTextField();
 325             final Format format = dateEditor.getFormat();
 326             Object value;
 327             if (format == null || (value = spinnerComponent.getValue()) == null) return;
 328 
 329             final SpinnerDateModel model = dateEditor.getModel();
 330             final DateFormat.Field field = DateFormat.Field.ofCalendarField(model.getCalendarField());
 331             if (field == null) return;
 332 
 333             try {
 334                 final AttributedCharacterIterator iterator = format.formatToCharacterIterator(value);
 335                 if (!select(ftf, iterator, field) && field == DateFormat.Field.HOUR0) {
 336                     select(ftf, iterator, DateFormat.Field.HOUR1);
 337                 }
 338             } catch (final IllegalArgumentException iae) {}
 339         }
 340 
 341         /**
 342          * Selects the passed in field, returning true if it is found,
 343          * false otherwise.
 344          */
 345         private boolean select(final JFormattedTextField ftf, final AttributedCharacterIterator iterator, final DateFormat.Field field) {
 346             final int max = ftf.getDocument().getLength();
 347 
 348             iterator.first();
 349             do {
 350                 final Map<Attribute,Object> attrs = iterator.getAttributes();
 351                 if (attrs == null || !attrs.containsKey(field)) continue;
 352 
 353                 final int start = iterator.getRunStart(field);
 354                 final int end = iterator.getRunLimit(field);
 355                 if (start != -1 && end != -1 && start <= max && end <= max) {
 356                     ftf.select(start, end);
 357                 }
 358 
 359                 return true;
 360             } while (iterator.next() != CharacterIterator.DONE);
 361             return false;
 362         }
 363 
 364         /**
 365          * Returns the calendarField under the start of the selection, or
 366          * -1 if there is no valid calendar field under the selection (or
 367          * the spinner isn't editing dates.
 368          */
 369         private int getCalendarField(final JSpinner spinnerComponent) {
 370             final JComponent editor = spinnerComponent.getEditor();
 371             if (!(editor instanceof JSpinner.DateEditor)) return -1;
 372 
 373             final JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor;
 374             final JFormattedTextField ftf = dateEditor.getTextField();
 375             final int start = ftf.getSelectionStart();
 376             final JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter();
 377             if (!(formatter instanceof InternationalFormatter)) return -1;
 378 
 379             final Format.Field[] fields = ((InternationalFormatter)formatter).getFields(start);
 380             for (final Field element : fields) {
 381                 if (!(element instanceof DateFormat.Field)) continue;
 382                 int calendarField;
 383 
 384                 if (element == DateFormat.Field.HOUR1) {
 385                     calendarField = Calendar.HOUR;
 386                 } else {
 387                     calendarField = ((DateFormat.Field)element).getCalendarField();
 388                 }
 389 
 390                 if (calendarField != -1) {
 391                     return calendarField;
 392                 }
 393             }
 394             return -1;
 395         }
 396 
 397         public void mousePressed(final MouseEvent e) {
 398             if (!SwingUtilities.isLeftMouseButton(e) || !e.getComponent().isEnabled()) return;
 399             spinner = eventToSpinner(e);
 400             autoRepeatTimer.start();
 401 
 402             focusSpinnerIfNecessary();
 403         }
 404 
 405         public void mouseReleased(final MouseEvent e) {
 406             autoRepeatTimer.stop();
 407             spinner = null;
 408         }
 409 
 410         public void mouseClicked(final MouseEvent e) {}
 411         public void mouseEntered(final MouseEvent e) {}
 412         public void mouseExited(final MouseEvent e) {}
 413 
 414         /**
 415          * Requests focus on a child of the spinner if the spinner doesn't
 416          * have focus.
 417          */
 418         private void focusSpinnerIfNecessary() {
 419             final Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
 420             if (!spinner.isRequestFocusEnabled() || (fo != null && (SwingUtilities.isDescendingFrom(fo, spinner)))) return;
 421             Container root = spinner;
 422 
 423             if (!root.isFocusCycleRoot()) {
 424                 root = root.getFocusCycleRootAncestor();
 425             }
 426 
 427             if (root == null) return;
 428             final FocusTraversalPolicy ftp = root.getFocusTraversalPolicy();
 429             final Component child = ftp.getComponentAfter(root, spinner);
 430 
 431             if (child != null && SwingUtilities.isDescendingFrom(child, spinner)) {
 432                 child.requestFocus();
 433             }
 434         }
 435     }
 436 
 437     class SpinPainter extends JComponent {
 438         final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIStateFactory.getSpinnerArrows());
 439 
 440         ButtonModel fTopModel;
 441         ButtonModel fBottomModel;
 442 
 443         boolean fPressed = false;
 444         boolean fTopPressed = false;
 445 
 446         Dimension kPreferredSize = new Dimension(15, 24); // 19,27 before trimming
 447 
 448         public SpinPainter(final AbstractButton top, final AbstractButton bottom) {
 449             if (top != null) {
 450                 fTopModel = top.getModel();
 451             }
 452 
 453             if (bottom != null) {
 454                 fBottomModel = bottom.getModel();
 455             }
 456         }
 457 
 458         public void paint(final Graphics g) {
 459             if (spinner.isOpaque()) {
 460                 g.setColor(spinner.getBackground());
 461                 g.fillRect(0, 0, getWidth(), getHeight());
 462             }
 463 
 464             AquaUtilControlSize.applySizeForControl(spinner, painter);
 465 
 466             if (isEnabled()) {
 467                 if (fTopModel != null && fTopModel.isPressed()) {
 468                     painter.state.set(State.PRESSED);
 469                     painter.state.set(BooleanValue.NO);
 470                 } else if (fBottomModel != null && fBottomModel.isPressed()) {
 471                     painter.state.set(State.PRESSED);
 472                     painter.state.set(BooleanValue.YES);
 473                 } else {
 474                     painter.state.set(State.ACTIVE);
 475                 }
 476             } else {
 477                 painter.state.set(State.DISABLED);
 478             }
 479 
 480             final Rectangle bounds = getBounds();
 481             painter.paint(g, spinner, 0, 0, bounds.width, bounds.height);
 482         }
 483 
 484         public Dimension getPreferredSize() {
 485             final Size size = AquaUtilControlSize.getUserSizeFrom(this);
 486 
 487             if (size == Size.MINI) {
 488                 return new Dimension(kPreferredSize.width, kPreferredSize.height - 8);
 489             }
 490 
 491             return kPreferredSize;
 492         }
 493     }
 494 
 495     /**
 496      * A simple layout manager for the editor and the next/previous buttons.
 497      * See the AquaSpinnerUI javadoc for more information about exactly
 498      * how the components are arranged.
 499      */
 500     static class SpinnerLayout implements LayoutManager {
 501         private Component nextButton = null;
 502         private Component previousButton = null;
 503         private Component editor = null;
 504         private Component painter = null;
 505 
 506         public void addLayoutComponent(final String name, final Component c) {
 507             if ("Next".equals(name)) {
 508                 nextButton = c;
 509             } else if ("Previous".equals(name)) {
 510                 previousButton = c;
 511             } else if ("Editor".equals(name)) {
 512                 editor = c;
 513             } else if ("Painter".equals(name)) {
 514                 painter = c;
 515             }
 516         }
 517 
 518         public void removeLayoutComponent(Component c) {
 519             if (c == nextButton) {
 520                 c = null;
 521             } else if (c == previousButton) {
 522                 previousButton = null;
 523             } else if (c == editor) {
 524                 editor = null;
 525             } else if (c == painter) {
 526                 painter = null;
 527             }
 528         }
 529 
 530         private Dimension preferredSize(final Component c) {
 531             return (c == null) ? new Dimension(0, 0) : c.getPreferredSize();
 532         }
 533 
 534         public Dimension preferredLayoutSize(final Container parent) {
 535 //            Dimension nextD = preferredSize(nextButton);
 536 //            Dimension previousD = preferredSize(previousButton);
 537             final Dimension editorD = preferredSize(editor);
 538             final Dimension painterD = preferredSize(painter);
 539 
 540             /* Force the editors height to be a multiple of 2
 541              */
 542             editorD.height = ((editorD.height + 1) / 2) * 2;
 543 
 544             final Dimension size = new Dimension(editorD.width, Math.max(painterD.height, editorD.height));
 545             size.width += painterD.width; //Math.max(nextD.width, previousD.width);
 546             final Insets insets = parent.getInsets();
 547             size.width += insets.left + insets.right;
 548             size.height += insets.top + insets.bottom;
 549             return size;
 550         }
 551 
 552         public Dimension minimumLayoutSize(final Container parent) {
 553             return preferredLayoutSize(parent);
 554         }
 555 
 556         private void setBounds(final Component c, final int x, final int y, final int width, final int height) {
 557             if (c != null) {
 558                 c.setBounds(x, y, width, height);
 559             }
 560         }
 561 
 562         public void layoutContainer(final Container parent) {
 563             final Insets insets = parent.getInsets();
 564             final int availWidth = parent.getWidth() - (insets.left + insets.right);
 565             final int availHeight = parent.getHeight() - (insets.top + insets.bottom);
 566 
 567             final Dimension painterD = preferredSize(painter);
 568 //            Dimension nextD = preferredSize(nextButton);
 569 //            Dimension previousD = preferredSize(previousButton);
 570             final int nextHeight = availHeight / 2;
 571             final int previousHeight = availHeight - nextHeight;
 572             final int buttonsWidth = painterD.width; //Math.max(nextD.width, previousD.width);
 573             final int editorWidth = availWidth - buttonsWidth;
 574 
 575             /* Deal with the spinners componentOrientation property.
 576              */
 577             int editorX, buttonsX;
 578             if (parent.getComponentOrientation().isLeftToRight()) {
 579                 editorX = insets.left;
 580                 buttonsX = editorX + editorWidth;
 581             } else {
 582                 buttonsX = insets.left;
 583                 editorX = buttonsX + buttonsWidth;
 584             }
 585 
 586             final int previousY = insets.top + nextHeight;
 587             final int painterTop = previousY - (painterD.height / 2);
 588             setBounds(editor, editorX, insets.top, editorWidth, availHeight);
 589             setBounds(nextButton, buttonsX, insets.top, buttonsWidth, nextHeight);
 590             setBounds(previousButton, buttonsX, previousY, buttonsWidth, previousHeight);
 591             setBounds(painter, buttonsX, painterTop, buttonsWidth, painterD.height);
 592         }
 593     }
 594 
 595     /**
 596      * Detect JSpinner property changes we're interested in and delegate.  Subclasses
 597      * shouldn't need to replace the default propertyChangeListener (although they
 598      * can by overriding createPropertyChangeListener) since all of the interesting
 599      * property changes are delegated to protected methods.
 600      */
 601     static class PropertyChangeHandler implements PropertyChangeListener {
 602         public void propertyChange(final PropertyChangeEvent e) {
 603             final String propertyName = e.getPropertyName();
 604             final JSpinner spinner = (JSpinner)(e.getSource());
 605             final SpinnerUI spinnerUI = spinner.getUI();
 606 
 607             if (spinnerUI instanceof AquaSpinnerUI) {
 608                 final AquaSpinnerUI ui = (AquaSpinnerUI)spinnerUI;
 609 
 610                 if ("editor".equals(propertyName)) {
 611                     final JComponent oldEditor = (JComponent)e.getOldValue();
 612                     final JComponent newEditor = (JComponent)e.getNewValue();
 613                     ui.replaceEditor(oldEditor, newEditor);
 614                     ui.updateEnabledState();
 615                 } else if ("enabled".equals(propertyName)) {
 616                     ui.updateEnabledState();
 617                 } else if (JComponent.TOOL_TIP_TEXT_KEY.equals(propertyName)) {
 618                     ui.updateToolTipTextForChildren(spinner);
 619                 } else if ("font".equals(propertyName)) {
 620                     JComponent editor = spinner.getEditor();
 621                     if (editor != null && editor instanceof JSpinner.DefaultEditor) {
 622                         JTextField tf =
 623                                 ((JSpinner.DefaultEditor) editor).getTextField();
 624                         if (tf != null) {
 625                             if (tf.getFont() instanceof UIResource) {
 626                                 tf.setFont(spinner.getFont());
 627                             }
 628                         }
 629                     }
 630                 }
 631             }
 632         }
 633     }
 634 
 635     // Syncronizes the ToolTip text for the components within the spinner
 636     // to be the same value as the spinner ToolTip text.
 637     void updateToolTipTextForChildren(final JComponent spinnerComponent) {
 638         final String toolTipText = spinnerComponent.getToolTipText();
 639         final Component[] children = spinnerComponent.getComponents();
 640         for (final Component element : children) {
 641             if (element instanceof JSpinner.DefaultEditor) {
 642                 final JTextField tf = ((JSpinner.DefaultEditor)element).getTextField();
 643                 if (tf != null) {
 644                     tf.setToolTipText(toolTipText);
 645                 }
 646             } else if (element instanceof JComponent) {
 647                 ((JComponent)element).setToolTipText(toolTipText);
 648             }
 649         }
 650     }
 651 }