API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing.plaf.basic. BasicSpinnerUI View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052

/*
 * @(#)BasicSpinnerUI.java	1.28 06/04/10
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.swing.plaf.basic;

import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.text.*;

import java.beans.*;
import java.text.*;
import java.util.*;
import sun.swing.DefaultLookup;


/**
 * The default Spinner UI delegate.
 *
 * @version 1.28 04/10/06
 * @author Hans Muller
 * @since 1.4
 */
public class BasicSpinnerUI extends SpinnerUI
{
    /**
     * The spinner that we're a UI delegate for.  Initialized by 
     * the <code>installUI</code> method, and reset to null
     * by <code>uninstallUI</code>.
     * 
     * @see #installUI
     * @see #uninstallUI
     */
    protected JSpinner spinner;  
    private Handler handler;


    /**
     * The mouse/action listeners that are added to the spinner's 
     * arrow buttons.  These listeners are shared by all 
     * spinner arrow buttons.
     * 
     * @see #createNextButton
     * @see #createPreviousButton
     */
    private static final ArrowButtonHandler nextButtonHandler = new ArrowButtonHandler("increment", true);
    private static final ArrowButtonHandler previousButtonHandler = new ArrowButtonHandler("decrement", false);
    private PropertyChangeListener propertyChangeListener;


    /**
     * Used by the default LayoutManager class - SpinnerLayout for 
     * missing (null) editor/nextButton/previousButton children.
     */
    private static final Dimension zeroSize = new Dimension(0, 0);


    /**
     * Returns a new instance of BasicSpinnerUI.  SpinnerListUI 
     * delegates are allocated one per JSpinner.  
     * 
     * @param c the JSpinner (not used)
     * @see ComponentUI#createUI
     * @return a new BasicSpinnerUI object
     */
    public static ComponentUI createUI(JComponent c) {
        return new BasicSpinnerUI();
    }


    private void maybeAdd(Component c, String s) {
	if (c != null) {
	    spinner.add(c, s);
	}
    }


    /**
     * Calls <code>installDefaults</code>, <code>installListeners</code>,
     * and then adds the components returned by <code>createNextButton</code>,
     * <code>createPreviousButton</code>, and <code>createEditor</code>.
     * 
     * @param c the JSpinner
     * @see #installDefaults
     * @see #installListeners
     * @see #createNextButton
     * @see #createPreviousButton
     * @see #createEditor
     */
    public void installUI(JComponent c) {
	this.spinner = (JSpinner)c;
	installDefaults();
	installListeners();
	maybeAdd(createNextButton(), "Next");
	maybeAdd(createPreviousButton(), "Previous");
	maybeAdd(createEditor(), "Editor");
        updateEnabledState();
        installKeyboardActions();
    }


    /**
     * Calls <code>uninstallDefaults</code>, <code>uninstallListeners</code>,
     * and then removes all of the spinners children.
     * 
     * @param c the JSpinner (not used)
     */
    public void uninstallUI(JComponent c) {
	uninstallDefaults();
	uninstallListeners();
	this.spinner = null;
	c.removeAll();
    }

    
    /**
     * Initializes <code>PropertyChangeListener</code> with 
     * a shared object that delegates interesting PropertyChangeEvents
     * to protected methods.
     * <p>
     * This method is called by <code>installUI</code>.
     * 
     * @see #replaceEditor
     * @see #uninstallListeners
     */
    protected void installListeners() {
        propertyChangeListener = createPropertyChangeListener();
        spinner.addPropertyChangeListener(propertyChangeListener);
	if (DefaultLookup.getBoolean(spinner, this,
	    "Spinner.disableOnBoundaryValues", false)) {
	    spinner.addChangeListener(getHandler());
	}
	JComponent editor = spinner.getEditor();
	if (editor != null && editor instanceof JSpinner.DefaultEditor) {
	    JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField();
	    if (tf != null) {
		tf.addFocusListener(nextButtonHandler);
		tf.addFocusListener(previousButtonHandler);
	    }
	}
    }


    /**
     * Removes the <code>PropertyChangeListener</code> added
     * by installListeners.
     * <p>
     * This method is called by <code>uninstallUI</code>.
     * 
     * @see #installListeners
     */
    protected void uninstallListeners() {
	spinner.removePropertyChangeListener(propertyChangeListener);
	spinner.removeChangeListener(handler);
	JComponent editor = spinner.getEditor();
	removeEditorBorderListener(editor);
	if (editor instanceof JSpinner.DefaultEditor) {
	    JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField();
	    if (tf != null) {
		tf.removeFocusListener(nextButtonHandler);
		tf.removeFocusListener(previousButtonHandler);
	    }
	}
        propertyChangeListener = null;
        handler = null;
    }


    /**
     * Initialize the <code>JSpinner</code> <code>border</code>, 
     * <code>foreground</code>, and <code>background</code>, properties 
     * based on the corresponding "Spinner.*" properties from defaults table.  
     * The <code>JSpinners</code> layout is set to the value returned by
     * <code>createLayout</code>.  This method is called by <code>installUI</code>.
     *
     * @see #uninstallDefaults
     * @see #installUI
     * @see #createLayout
     * @see LookAndFeel#installBorder
     * @see LookAndFeel#installColors
     */
    protected void installDefaults() {
	spinner.setLayout(createLayout());
        LookAndFeel.installBorder(spinner, "Spinner.border");
        LookAndFeel.installColorsAndFont(spinner, "Spinner.background", "Spinner.foreground", "Spinner.font");
        LookAndFeel.installProperty(spinner, "opaque", Boolean.TRUE);
    }


    /**
     * Sets the <code>JSpinner's</code> layout manager to null.  This
     * method is called by <code>uninstallUI</code>.
     * 
     * @see #installDefaults
     * @see #uninstallUI
     */
    protected void uninstallDefaults() {
	spinner.setLayout(null);
    }


    private Handler getHandler() {
        if (handler == null) {
            handler = new Handler();
        }
        return handler;
    }


    /**
     * Installs the necessary listeners on the next button, <code>c</code>,
     * to update the <code>JSpinner</code> in response to a user gesture.
     *
     * @param c Component to install the listeners on
     * @throws NullPointerException if <code>c</code> is null.
     * @see #createNextButton
     * @since 1.5
     */
    protected void installNextButtonListeners(Component c) {
        installButtonListeners(c, nextButtonHandler);
    }

    /**
     * Installs the necessary listeners on the previous button, <code>c</code>,
     * to update the <code>JSpinner</code> in response to a user gesture.
     *
     * @param c Component to install the listeners on.
     * @throws NullPointerException if <code>c</code> is null.
     * @see #createPreviousButton
     * @since 1.5
     */
    protected void installPreviousButtonListeners(Component c) {
        installButtonListeners(c, previousButtonHandler);
    }

    private void installButtonListeners(Component c,
                                        ArrowButtonHandler handler) {
        if (c instanceof JButton) {
            ((JButton)c).addActionListener(handler);
        }
        c.addMouseListener(handler);
    }

    /**
     * Create a <code>LayoutManager</code> that manages the <code>editor</code>, 
     * <code>nextButton</code>, and <code>previousButton</code> 
     * children of the JSpinner.  These three children must be
     * added with a constraint that identifies their role: 
     * "Editor", "Next", and "Previous". The default layout manager 
     * can handle the absence of any of these children.
     * 
     * @return a LayoutManager for the editor, next button, and previous button.
     * @see #createNextButton
     * @see #createPreviousButton
     * @see #createEditor
     */
    protected LayoutManager createLayout() {
        return getHandler();
    }


    /**
     * Create a <code>PropertyChangeListener</code> that can be
     * added to the JSpinner itself.  Typically, this listener
     * will call replaceEditor when the "editor" property changes, 
     * since it's the <code>SpinnerUI's</code> responsibility to 
     * add the editor to the JSpinner (and remove the old one).
     * This method is called by <code>installListeners</code>.
     * 
     * @return A PropertyChangeListener for the JSpinner itself
     * @see #installListeners
     */
    protected PropertyChangeListener createPropertyChangeListener() {
        return getHandler();
    }


    /**
     * Create a component that will replace the spinner models value
     * with the object returned by <code>spinner.getPreviousValue</code>.
     * By default the <code>previousButton</code> is a JButton. This
     * method invokes <code>installPreviousButtonListeners</code> to
     * install the necessary listeners to update the <code>JSpinner</code>'s
     * model in response to a user gesture. If a previousButton isn't needed
     * (in a subclass) then override this method to return null.
     *
     * @return a component that will replace the spinners model with the
     *     next value in the sequence, or null
     * @see #installUI
     * @see #createNextButton
     * @see #installPreviousButtonListeners
     */
    protected Component createPreviousButton() {
	Component c = createArrowButton(SwingConstants.SOUTH);
        c.setName("Spinner.previousButton");
        installPreviousButtonListeners(c);
        return c;
    }


    /**
     * Create a component that will replace the spinner models value
     * with the object returned by <code>spinner.getNextValue</code>.
     * By default the <code>nextButton</code> is a JButton
     * who's <code>ActionListener</code> updates it's <code>JSpinner</code>
     * ancestors model.  If a nextButton isn't needed (in a subclass)
     * then override this method to return null.
     *
     * @return a component that will replace the spinners model with the
     *     next value in the sequence, or null
     * @see #installUI
     * @see #createPreviousButton
     * @see #installNextButtonListeners
     */
    protected Component createNextButton() {
	Component c = createArrowButton(SwingConstants.NORTH);
        c.setName("Spinner.nextButton");
        installNextButtonListeners(c);
        return c;
    }

    private Component createArrowButton(int direction) {
	JButton b = new BasicArrowButton(direction);
	Border buttonBorder = UIManager.getBorder("Spinner.arrowButtonBorder");
	if (buttonBorder instanceof UIResource) {
	    // Wrap the border to avoid having the UIResource be replaced by
	    // the ButtonUI. This is the opposite of using BorderUIResource.
	    b.setBorder(new CompoundBorder(buttonBorder, null));
	} else {
	    b.setBorder(buttonBorder);
	}
        b.setInheritsPopupMenu(true);
	return b;
    }


    /**
     * This method is called by installUI to get the editor component
     * of the <code>JSpinner</code>.  By default it just returns 
     * <code>JSpinner.getEditor()</code>.  Subclasses can override
     * <code>createEditor</code> to return a component that contains 
     * the spinner's editor or null, if they're going to handle adding 
     * the editor to the <code>JSpinner</code> in an 
     * <code>installUI</code> override.
     * <p>
     * Typically this method would be overridden to wrap the editor
     * with a container with a custom border, since one can't assume
     * that the editors border can be set directly.  
     * <p>
     * The <code>replaceEditor</code> method is called when the spinners
     * editor is changed with <code>JSpinner.setEditor</code>.  If you've
     * overriden this method, then you'll probably want to override
     * <code>replaceEditor</code> as well.
     * 
     * @return the JSpinners editor JComponent, spinner.getEditor() by default
     * @see #installUI
     * @see #replaceEditor
     * @see JSpinner#getEditor
     */
    protected JComponent createEditor() {
	JComponent editor = spinner.getEditor();
	maybeRemoveEditorBorder(editor);
	installEditorBorderListener(editor);
        editor.setInheritsPopupMenu(true);
        updateEditorAlignment(editor);
        return editor;
    }


    /**
     * Called by the <code>PropertyChangeListener</code> when the 
     * <code>JSpinner</code> editor property changes.  It's the responsibility 
     * of this method to remove the old editor and add the new one.  By
     * default this operation is just:
     * <pre>
     * spinner.remove(oldEditor);
     * spinner.add(newEditor, "Editor");
     * </pre>
     * The implementation of <code>replaceEditor</code> should be coordinated
     * with the <code>createEditor</code> method.
     * 
     * @see #createEditor
     * @see #createPropertyChangeListener
     */
    protected void replaceEditor(JComponent oldEditor, JComponent newEditor) {
	spinner.remove(oldEditor);
	maybeRemoveEditorBorder(newEditor);
	installEditorBorderListener(newEditor);
        newEditor.setInheritsPopupMenu(true);
        updateEditorAlignment(newEditor);
	spinner.add(newEditor, "Editor");
    }

    private void updateEditorAlignment(JComponent editor) {
        if (editor instanceof JSpinner.DefaultEditor) {
            // if editor alignment isn't set in LAF, we get 0 (CENTER) here
            int alignment = UIManager.getInt("Spinner.editorAlignment");
            JTextField text = ((JSpinner.DefaultEditor)editor).getTextField();
            text.setHorizontalAlignment(alignment);
        }
    }
    
    /**
     * Remove the border around the inner editor component for LaFs
     * that install an outside border around the spinner,
     */
    private void maybeRemoveEditorBorder(JComponent editor) {
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
	    if (editor instanceof JPanel &&
		editor.getBorder() == null &&
		editor.getComponentCount() > 0) {

		editor = (JComponent)editor.getComponent(0);
	    }

	    if (editor != null && editor.getBorder() instanceof UIResource) {
		editor.setBorder(null);
	    }
	}
    }

    /**
     * Remove the border around the inner editor component for LaFs
     * that install an outside border around the spinner,
     */
    private void installEditorBorderListener(JComponent editor) {
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
	    if (editor instanceof JPanel &&
		editor.getBorder() == null &&
		editor.getComponentCount() > 0) {

		editor = (JComponent)editor.getComponent(0);
	    }
	    if (editor != null &&
		(editor.getBorder() == null ||
		 editor.getBorder() instanceof UIResource)) {
		editor.addPropertyChangeListener(getHandler());
	    }
	}
    }

    private void removeEditorBorderListener(JComponent editor) {
        if (!UIManager.getBoolean("Spinner.editorBorderPainted")) {
	    if (editor instanceof JPanel &&
		editor.getComponentCount() > 0) {

		editor = (JComponent)editor.getComponent(0);
	    }
	    if (editor != null) {
		editor.removePropertyChangeListener(getHandler());
	    }
	}
    }


    /**
     * Updates the enabled state of the children Components based on the
     * enabled state of the <code>JSpinner</code>.
     */
    private void updateEnabledState() {
        updateEnabledState(spinner, spinner.isEnabled());
    }


    /**
     * Recursively updates the enabled state of the child
     * <code>Component</code>s of <code>c</code>.
     */
    private void updateEnabledState(Container c, boolean enabled) {
        for (int counter = c.getComponentCount() - 1; counter >= 0;counter--) {
            Component child = c.getComponent(counter);

	    if (DefaultLookup.getBoolean(spinner, this,
		"Spinner.disableOnBoundaryValues", false)) {
 		SpinnerModel model = spinner.getModel();
 		if (child.getName() == "Spinner.nextButton" &&
		    model.getNextValue() == null) {
 		    child.setEnabled(false);
 		}
 		else if (child.getName() == "Spinner.previousButton" &&
			 model.getPreviousValue() == null) {
 		    child.setEnabled(false);
 		}
 		else {
 		    child.setEnabled(enabled);
 		}
 	    }
	    else {
		child.setEnabled(enabled);
	    }
            if (child instanceof Container) {
                updateEnabledState((Container)child, enabled);
            }
        }
    }


    /**
     * Installs the keyboard Actions onto the JSpinner.
     *
     * @since 1.5
     */
    protected void installKeyboardActions() {
        InputMap iMap = getInputMap(JComponent.
                                   WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

	SwingUtilities.replaceUIInputMap(spinner, JComponent.
					 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
					 iMap);

        LazyActionMap.installLazyActionMap(spinner, BasicSpinnerUI.class,
                "Spinner.actionMap");
    }

    /**
     * Returns the InputMap to install for <code>condition</code>.
     */
    private InputMap getInputMap(int condition) {
        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
	    return (InputMap)DefaultLookup.get(spinner, this,
                    "Spinner.ancestorInputMap");
        }
        return null;
    }

    static void loadActionMap(LazyActionMap map) {
        map.put("increment", nextButtonHandler);
        map.put("decrement", previousButtonHandler);
    }

    /**
     * Returns the baseline.
     *
     * @throws NullPointerException {@inheritDoc}
     * @throws IllegalArgumentException {@inheritDoc}
     * @see javax.swing.JComponent#getBaseline(int, int)
     * @since 1.6
     */
    public int getBaseline(JComponent c, int width, int height) {
        super.getBaseline(c, width, height);
        JComponent editor = spinner.getEditor();
        Insets insets = spinner.getInsets();
        width = width - insets.left - insets.right;
        height = height - insets.top - insets.bottom;
        if (width >= 0 && height >= 0) {
            int baseline = editor.getBaseline(width, height);
            if (baseline >= 0) {
                return insets.top + baseline;
            }
        }
        return -1;
    }

    /**
     * Returns an enum indicating how the baseline of the component
     * changes as the size changes.
     *
     * @throws NullPointerException {@inheritDoc}
     * @see javax.swing.JComponent#getBaseline(int, int)
     * @since 1.6
     */
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
            JComponent c) {
        super.getBaselineResizeBehavior(c);
        return spinner.getEditor().getBaselineResizeBehavior();
    }

    /**
     * A handler for spinner arrow button mouse and action events.  When 
     * a left mouse pressed event occurs we look up the (enabled) spinner 
     * that's the source of the event and start the autorepeat timer.  The
     * timer fires action events until any button is released at which 
     * point the timer is stopped and the reference to the spinner cleared.
     * The timer doesn't start until after a 300ms delay, so often the 
     * source of the initial (and final) action event is just the button
     * logic for mouse released - which means that we're relying on the fact
     * that our mouse listener runs after the buttons mouse listener.
     * <p>
     * Note that one instance of this handler is shared by all slider previous 
     * arrow buttons and likewise for all of the next buttons, 
     * so it doesn't have any state that persists beyond the limits
     * of a single button pressed/released gesture.
     */
    private static class ArrowButtonHandler extends AbstractAction
					    implements FocusListener, MouseListener, UIResource {
	final javax.swing.Timer autoRepeatTimer;
	final boolean isNext;
	JSpinner spinner = null;
 	JButton arrowButton = null;

	ArrowButtonHandler(String name, boolean isNext) {
            super(name);
	    this.isNext = isNext;
	    autoRepeatTimer = new javax.swing.Timer(60, this);
	    autoRepeatTimer.setInitialDelay(300);
	}

	private JSpinner eventToSpinner(AWTEvent e) {
	    Object src = e.getSource();
	    while ((src instanceof Component) && !(src instanceof JSpinner)) {
		src = ((Component)src).getParent();
	    }
	    return (src instanceof JSpinner) ? (JSpinner)src : null;
	}

	public void actionPerformed(ActionEvent e) {
            JSpinner spinner = this.spinner;

            if (!(e.getSource() instanceof javax.swing.Timer)) {
                // Most likely resulting from being in ActionMap.
                spinner = eventToSpinner(e);
		if (e.getSource() instanceof JButton) {
		    arrowButton = (JButton)e.getSource();
		}
	    } else {
  		if (arrowButton!=null && !arrowButton.getModel().isPressed()
		    && autoRepeatTimer.isRunning()) {
  		    autoRepeatTimer.stop();
  		    spinner = null;
		    arrowButton = null;
  		}
	    }
            if (spinner != null) {
                try {
                    int calendarField = getCalendarField(spinner);
                    spinner.commitEdit();
                    if (calendarField != -1) {
                        ((SpinnerDateModel)spinner.getModel()).
                                 setCalendarField(calendarField);
                    }
                    Object value = (isNext) ? spinner.getNextValue() :
                               spinner.getPreviousValue();
                    if (value != null) {
                        spinner.setValue(value);
                        select(spinner);
                    }
                } catch (IllegalArgumentException iae) {
                    UIManager.getLookAndFeel().provideErrorFeedback(spinner);
                } catch (ParseException pe) {
                    UIManager.getLookAndFeel().provideErrorFeedback(spinner);
                }
            }
	}

        /**
         * If the spinner's editor is a DateEditor, this selects the field
         * associated with the value that is being incremented.
         */
        private void select(JSpinner spinner) {
            JComponent editor = spinner.getEditor();

            if (editor instanceof JSpinner.DateEditor) {
                JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor;
                JFormattedTextField ftf = dateEditor.getTextField();
                Format format = dateEditor.getFormat();
                Object value;

                if (format != null && (value = spinner.getValue()) != null) {
                    SpinnerDateModel model = dateEditor.getModel();
                    DateFormat.Field field = DateFormat.Field.ofCalendarField(
                        model.getCalendarField());

                    if (field != null) {
                        try {
                            AttributedCharacterIterator iterator = format.
                                formatToCharacterIterator(value);
                            if (!select(ftf, iterator, field) &&
                                       field == DateFormat.Field.HOUR0) {
                                select(ftf, iterator, DateFormat.Field.HOUR1);
                            }
                        }
                        catch (IllegalArgumentException iae) {}
                    }
                }
            }
        }

        /**
         * Selects the passed in field, returning true if it is found,
         * false otherwise.
         */
        private boolean select(JFormattedTextField ftf,
                               AttributedCharacterIterator iterator,
                               DateFormat.Field field) {
            int max = ftf.getDocument().getLength();

            iterator.first();
            do {
                Map attrs = iterator.getAttributes();

                if (attrs != null && attrs.containsKey(field)){
                    int start = iterator.getRunStart(field);
                    int end = iterator.getRunLimit(field);

                    if (start != -1 && end != -1 && start <= max &&
                                       end <= max) {
                        ftf.select(start, end);
                    }
                    return true;
                }
            } while (iterator.next() != CharacterIterator.DONE);
            return false;
        }

        /**
         * Returns the calendarField under the start of the selection, or
         * -1 if there is no valid calendar field under the selection (or
         * the spinner isn't editing dates.
         */
        private int getCalendarField(JSpinner spinner) {
            JComponent editor = spinner.getEditor();

            if (editor instanceof JSpinner.DateEditor) {
                JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor;
                JFormattedTextField ftf = dateEditor.getTextField();
                int start = ftf.getSelectionStart();
                JFormattedTextField.AbstractFormatter formatter =
                                    ftf.getFormatter();

                if (formatter instanceof InternationalFormatter) {
                    Format.Field[] fields = ((InternationalFormatter)
                                             formatter).getFields(start);

                    for (int counter = 0; counter < fields.length; counter++) {
                        if (fields[counter] instanceof DateFormat.Field) {
                            int calendarField;

                            if (fields[counter] == DateFormat.Field.HOUR1) {
                                calendarField = Calendar.HOUR;
                            }
                            else {
                                calendarField = ((DateFormat.Field)
                                        fields[counter]).getCalendarField();
                            }
                            if (calendarField != -1) {
                                return calendarField;
                            }
                        }
                    }
                }
            }
            return -1;
        }

	public void mousePressed(MouseEvent e) {
	    if (SwingUtilities.isLeftMouseButton(e) && e.getComponent().isEnabled()) {
		spinner = eventToSpinner(e);
		autoRepeatTimer.start();

                focusSpinnerIfNecessary();
	    }
	}

	public void mouseReleased(MouseEvent e) {
	    autoRepeatTimer.stop();	    
	    arrowButton = null;
	    spinner = null;
	}

        public void mouseClicked(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
	    if (spinner != null && !autoRepeatTimer.isRunning() && spinner == eventToSpinner(e)) {
		autoRepeatTimer.start();	    
	    }
        }

        public void mouseExited(MouseEvent e) {
	    if (autoRepeatTimer.isRunning()) {
		autoRepeatTimer.stop();	    
	    }
        }

        /**
         * Requests focus on a child of the spinner if the spinner doesn't
         * have focus.
         */
        private void focusSpinnerIfNecessary() {
            Component fo = KeyboardFocusManager.
                              getCurrentKeyboardFocusManager().getFocusOwner();
            if (spinner.isRequestFocusEnabled() && (
                        fo == null ||
                        !SwingUtilities.isDescendingFrom(fo, spinner))) {
                Container root = spinner;

                if (!root.isFocusCycleRoot()) {
                    root = root.getFocusCycleRootAncestor();
                }
                if (root != null) {
                    FocusTraversalPolicy ftp = root.getFocusTraversalPolicy();
                    Component child = ftp.getComponentAfter(root, spinner);

                    if (child != null && SwingUtilities.isDescendingFrom(
                                                        child, spinner)) {
                        child.requestFocus();
                    }
                }
            }
        }

	public void focusGained(FocusEvent e) {
	}

	public void focusLost(FocusEvent e) {
            if (spinner == eventToSpinner(e)) {
                if (autoRepeatTimer.isRunning()) {
                    autoRepeatTimer.stop();	    
                }	    
                spinner = null;
                if (arrowButton != null) {
                    ButtonModel model = arrowButton.getModel();
                    model.setPressed(false);
                    model.setArmed(false);
                    arrowButton = null;
                }
            }
	}
    }


    private static class Handler implements LayoutManager,
            PropertyChangeListener, ChangeListener {
        //
        // LayoutManager
        //
	private Component nextButton = null;
	private Component previousButton = null;
	private Component editor = null;

	public void addLayoutComponent(String name, Component c) {
	    if ("Next".equals(name)) {
		nextButton = c;
	    }
	    else if ("Previous".equals(name)) {
		previousButton = c;
	    }
	    else if ("Editor".equals(name)) {
		editor = c;
	    }
	}

	public void removeLayoutComponent(Component c) {
	    if (c == nextButton) {
		c = null;
	    }
	    else if (c == previousButton) {
		previousButton = null;
	    }
	    else if (c == editor) {
		editor = null;
	    }
	}

	private Dimension preferredSize(Component c) {
	    return (c == null) ? zeroSize : c.getPreferredSize();
	}

	public Dimension preferredLayoutSize(Container parent) {
	    Dimension nextD = preferredSize(nextButton);
	    Dimension previousD = preferredSize(previousButton);
	    Dimension editorD = preferredSize(editor);

	    /* Force the editors height to be a multiple of 2
	     */
	    editorD.height = ((editorD.height + 1) / 2) * 2;     

	    Dimension size = new Dimension(editorD.width, editorD.height);
	    size.width += Math.max(nextD.width, previousD.width);
	    Insets insets = parent.getInsets();
	    size.width += insets.left + insets.right;
	    size.height += insets.top + insets.bottom;
	    return size;
	}

	public Dimension minimumLayoutSize(Container parent) {
	    return preferredLayoutSize(parent);
	}

	private void setBounds(Component c, int x, int y, int width, int height) {
	    if (c != null) {
		c.setBounds(x, y, width, height);
	    }
	}

	public void layoutContainer(Container parent) {
	    int width  = parent.getWidth();
	    int height = parent.getHeight();

	    Insets insets = parent.getInsets();
	    Dimension nextD = preferredSize(nextButton);
	    Dimension previousD = preferredSize(previousButton);	    
	    int buttonsWidth = Math.max(nextD.width, previousD.width);
	    int editorHeight = height - (insets.top + insets.bottom);

	    // The arrowButtonInsets value is used instead of the JSpinner's
	    // insets if not null. Defining this to be (0, 0, 0, 0) causes the
	    // buttons to be aligned with the outer edge of the spinner's
	    // border, and leaving it as "null" places the buttons completely
	    // inside the spinner's border.
	    Insets buttonInsets = UIManager.getInsets("Spinner.arrowButtonInsets");
	    if (buttonInsets == null) {
		buttonInsets = insets;
	    }

	    /* Deal with the spinner's componentOrientation property.
	     */
	    int editorX, editorWidth, buttonsX;
	    if (parent.getComponentOrientation().isLeftToRight()) {
		editorX = insets.left;
		editorWidth = width - insets.left - buttonsWidth - buttonInsets.right;
		buttonsX = width - buttonsWidth - buttonInsets.right;
	    } else {
		buttonsX = buttonInsets.left;
		editorX = buttonsX + buttonsWidth;
		editorWidth = width - buttonInsets.left - buttonsWidth - insets.right;
	    }

	    int nextY = buttonInsets.top;
	    int nextHeight = (height / 2) + (height % 2) - nextY;
	    int previousY = buttonInsets.top + nextHeight;
	    int previousHeight = height - previousY - buttonInsets.bottom;

	    setBounds(editor,         editorX,  insets.top, editorWidth, editorHeight);
	    setBounds(nextButton,     buttonsX, nextY,      buttonsWidth, nextHeight);
	    setBounds(previousButton, buttonsX, previousY,  buttonsWidth, previousHeight);
	}


        //
        // PropertyChangeListener
        //
        public void propertyChange(PropertyChangeEvent e)
        {
            String propertyName = e.getPropertyName();
	    if (e.getSource() instanceof JSpinner) {
		JSpinner spinner = (JSpinner)(e.getSource());
		SpinnerUI spinnerUI = spinner.getUI();

		if (spinnerUI instanceof BasicSpinnerUI) {
		    BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;

		    if ("editor".equals(propertyName)) {
			JComponent oldEditor = (JComponent)e.getOldValue();
			JComponent newEditor = (JComponent)e.getNewValue();
			ui.replaceEditor(oldEditor, newEditor);
			ui.updateEnabledState();
			if (oldEditor instanceof JSpinner.DefaultEditor) {
			    JTextField tf =
				((JSpinner.DefaultEditor)oldEditor).getTextField();
			    if (tf != null) {
				tf.removeFocusListener(nextButtonHandler);
				tf.removeFocusListener(previousButtonHandler);
			    }
			}
			if (newEditor instanceof JSpinner.DefaultEditor) {
			    JTextField tf =
				((JSpinner.DefaultEditor)newEditor).getTextField();
			    if (tf != null) {
				if (tf.getFont() instanceof UIResource) {
				    tf.setFont(spinner.getFont());
				}
				tf.addFocusListener(nextButtonHandler);
				tf.addFocusListener(previousButtonHandler);
			    }
			}
		    if (newEditor instanceof JSpinner.DefaultEditor) {
			JTextField tf =
			    ((JSpinner.DefaultEditor)newEditor).getTextField();
			if (tf != null) {
			    if (tf.getFont() instanceof UIResource) {
				tf.setFont(spinner.getFont());
			    }
			}
		    }
		    }
		    else if ("enabled".equals(propertyName) ||
			     "model".equals(propertyName)) {
			ui.updateEnabledState();
		    }
		else if ("font".equals(propertyName)) {
		    JComponent editor = spinner.getEditor();
		    if (editor!=null && editor instanceof JSpinner.DefaultEditor) {
			JTextField tf =
			    ((JSpinner.DefaultEditor)editor).getTextField();
			if (tf != null) {
			    if (tf.getFont() instanceof UIResource) {
				tf.setFont(spinner.getFont());
			    }
			}
		    }
		}
		else if (JComponent.TOOL_TIP_TEXT_KEY.equals(propertyName)) {
		    updateToolTipTextForChildren(spinner);
		}
		}
	    } else if (e.getSource() instanceof JComponent) {
		JComponent c = (JComponent)e.getSource();
		if ((c.getParent() instanceof JPanel) &&
		    (c.getParent().getParent() instanceof JSpinner) &&
		    "border".equals(propertyName)) {

		    JSpinner spinner = (JSpinner)c.getParent().getParent();
		    SpinnerUI spinnerUI = spinner.getUI();
		    if (spinnerUI instanceof BasicSpinnerUI) {
			BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;
			ui.maybeRemoveEditorBorder(c);
		    }
		}
	    }
    	}

	// Syncronizes the ToolTip text for the components within the spinner
	// to be the same value as the spinner ToolTip text.
	private void updateToolTipTextForChildren(JComponent spinner) {
	    String toolTipText = spinner.getToolTipText();
	    Component[] children = spinner.getComponents();
	    for (int i = 0; i < children.length; i++) {
		if (children[i] instanceof JSpinner.DefaultEditor) {
		    JTextField tf = ((JSpinner.DefaultEditor)children[i]).getTextField();
		    if (tf != null) {
			tf.setToolTipText(toolTipText);
		    }
		} else if (children[i] instanceof JComponent) {
		    ((JComponent)children[i]).setToolTipText( spinner.getToolTipText() );
		}
	    }
	}

	public void stateChanged(ChangeEvent e) {
	    if (e.getSource() instanceof JSpinner) {
		JSpinner spinner = (JSpinner)e.getSource();
		SpinnerUI spinnerUI = spinner.getUI();
		if (DefaultLookup.getBoolean(spinner, spinnerUI,
		    "Spinner.disableOnBoundaryValues", false) &&
		    spinnerUI instanceof BasicSpinnerUI) {
		    BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI;
		    ui.updateEnabledState();
		}
	    }
	}
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar