- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathAbstractButton.java
3080 lines (2847 loc) · 109 KB
/
AbstractButton.java
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
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing;
importjava.awt.*;
importjava.awt.event.*;
importjava.text.*;
importjava.awt.geom.*;
importjava.beans.JavaBean;
importjava.beans.BeanProperty;
importjava.beans.PropertyChangeEvent;
importjava.beans.PropertyChangeListener;
importjava.beans.Transient;
importjava.util.Enumeration;
importjava.io.Serializable;
importjavax.swing.event.*;
importjavax.swing.plaf.*;
importjavax.accessibility.*;
importjavax.swing.text.*;
/**
* Defines common behaviors for buttons and menu items.
* <p>
* Buttons can be configured, and to some degree controlled, by
* <code><a href="Action.html">Action</a></code>s. Using an
* <code>Action</code> with a button has many benefits beyond directly
* configuring a button. Refer to <a href="Action.html#buttonActions">
* Swing Components Supporting <code>Action</code></a> for more
* details, and you can find more information in <a
* href="https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
* to Use Actions</a>, a section in <em>The Java Tutorial</em>.
* <p>
* For further information see
* <a
href="https://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
* a section in <em>The Java Tutorial</em>.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @author Jeff Dinkins
* @since 1.2
*/
@JavaBean(defaultProperty = "UI")
@SuppressWarnings("serial") // Same-version serialization only
publicabstractclassAbstractButtonextendsJComponentimplementsItemSelectable, SwingConstants {
// *********************************
// ******* Button properties *******
// *********************************
/** Identifies a change in the button model. */
publicstaticfinalStringMODEL_CHANGED_PROPERTY = "model";
/** Identifies a change in the button's text. */
publicstaticfinalStringTEXT_CHANGED_PROPERTY = "text";
/** Identifies a change to the button's mnemonic. */
publicstaticfinalStringMNEMONIC_CHANGED_PROPERTY = "mnemonic";
// Text positioning and alignment
/** Identifies a change in the button's margins. */
publicstaticfinalStringMARGIN_CHANGED_PROPERTY = "margin";
/** Identifies a change in the button's vertical alignment. */
publicstaticfinalStringVERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment";
/** Identifies a change in the button's horizontal alignment. */
publicstaticfinalStringHORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment";
/** Identifies a change in the button's vertical text position. */
publicstaticfinalStringVERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition";
/** Identifies a change in the button's horizontal text position. */
publicstaticfinalStringHORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition";
// Paint options
/**
* Identifies a change to having the border drawn,
* or having it not drawn.
*/
publicstaticfinalStringBORDER_PAINTED_CHANGED_PROPERTY = "borderPainted";
/**
* Identifies a change to having the border highlighted when focused,
* or not.
*/
publicstaticfinalStringFOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";
/**
* Identifies a change from rollover enabled to disabled or back
* to enabled.
*/
publicstaticfinalStringROLLOVER_ENABLED_CHANGED_PROPERTY = "rolloverEnabled";
/**
* Identifies a change to having the button paint the content area.
*/
publicstaticfinalStringCONTENT_AREA_FILLED_CHANGED_PROPERTY = "contentAreaFilled";
// Icons
/** Identifies a change to the icon that represents the button. */
publicstaticfinalStringICON_CHANGED_PROPERTY = "icon";
/**
* Identifies a change to the icon used when the button has been
* pressed.
*/
publicstaticfinalStringPRESSED_ICON_CHANGED_PROPERTY = "pressedIcon";
/**
* Identifies a change to the icon used when the button has
* been selected.
*/
publicstaticfinalStringSELECTED_ICON_CHANGED_PROPERTY = "selectedIcon";
/**
* Identifies a change to the icon used when the cursor is over
* the button.
*/
publicstaticfinalStringROLLOVER_ICON_CHANGED_PROPERTY = "rolloverIcon";
/**
* Identifies a change to the icon used when the cursor is
* over the button and it has been selected.
*/
publicstaticfinalStringROLLOVER_SELECTED_ICON_CHANGED_PROPERTY = "rolloverSelectedIcon";
/**
* Identifies a change to the icon used when the button has
* been disabled.
*/
publicstaticfinalStringDISABLED_ICON_CHANGED_PROPERTY = "disabledIcon";
/**
* Identifies a change to the icon used when the button has been
* disabled and selected.
*/
publicstaticfinalStringDISABLED_SELECTED_ICON_CHANGED_PROPERTY = "disabledSelectedIcon";
/** The data model that determines the button's state. */
protectedButtonModelmodel = null;
privateStringtext = ""; // for BeanBox
privateInsetsmargin = null;
privateInsetsdefaultMargin = null;
// Button icons
// PENDING(jeff) - hold icons in an array
privateIcondefaultIcon = null;
privateIconpressedIcon = null;
privateIcondisabledIcon = null;
privateIconselectedIcon = null;
privateIcondisabledSelectedIcon = null;
privateIconrolloverIcon = null;
privateIconrolloverSelectedIcon = null;
// Display properties
privatebooleanpaintBorder = true;
privatebooleanpaintFocus = true;
privatebooleanrolloverEnabled = false;
privatebooleancontentAreaFilled = true;
// Icon/Label Alignment
privateintverticalAlignment = CENTER;
privateinthorizontalAlignment = CENTER;
privateintverticalTextPosition = CENTER;
privateinthorizontalTextPosition = TRAILING;
privateinticonTextGap = 4;
privateintmnemonic;
privateintmnemonicIndex = -1;
privatelongmultiClickThreshhold = 0;
privatebooleanborderPaintedSet = false;
privatebooleanrolloverEnabledSet = false;
privatebooleaniconTextGapSet = false;
privatebooleancontentAreaFilledSet = false;
// Whether or not we've set the LayoutManager.
privatebooleansetLayout = false;
// This is only used by JButton, promoted to avoid an extra
// boolean field in JButton
booleandefaultCapable = true;
/**
* Combined listeners: ActionListener, ChangeListener, ItemListener.
*/
privateHandlerhandler;
/**
* The button model's <code>changeListener</code>.
*/
protectedChangeListenerchangeListener = null;
/**
* The button model's <code>ActionListener</code>.
*/
protectedActionListeneractionListener = null;
/**
* The button model's <code>ItemListener</code>.
*/
protectedItemListeneritemListener = null;
/**
* Only one <code>ChangeEvent</code> is needed per button
* instance since the
* event's only state is the source property. The source of events
* generated is always "this".
*/
protectedtransientChangeEventchangeEvent;
privatebooleanhideActionText = false;
/**
* Constructor for subclasses to call.
*/
protectedAbstractButton() {}
/**
* Sets the <code>hideActionText</code> property, which determines
* whether the button displays text from the <code>Action</code>.
* This is useful only if an <code>Action</code> has been
* installed on the button.
*
* @param hideActionText <code>true</code> if the button's
* <code>text</code> property should not reflect
* that of the <code>Action</code>; the default is
* <code>false</code>
* @see <a href="Action.html#buttonActions">Swing Components Supporting
* <code>Action</code></a>
* @since 1.6
*/
@BeanProperty(expert = true, description
= "Whether the text of the button should come from the <code>Action</code>.")
publicvoidsetHideActionText(booleanhideActionText) {
if (hideActionText != this.hideActionText) {
this.hideActionText = hideActionText;
if (getAction() != null) {
setTextFromAction(getAction(), false);
}
firePropertyChange("hideActionText", !hideActionText,
hideActionText);
}
}
/**
* Returns the value of the <code>hideActionText</code> property, which
* determines whether the button displays text from the
* <code>Action</code>. This is useful only if an <code>Action</code>
* has been installed on the button.
*
* @return <code>true</code> if the button's <code>text</code>
* property should not reflect that of the
* <code>Action</code>; the default is <code>false</code>
* @since 1.6
*/
publicbooleangetHideActionText() {
returnhideActionText;
}
/**
* Returns the button's text.
* @return the buttons text
* @see #setText
*/
publicStringgetText() {
returntext;
}
/**
* Sets the button's text.
* @param text the string used to set the text
* @see #getText
*/
@BeanProperty(preferred = true, visualUpdate = true, description
= "The button's text.")
publicvoidsetText(Stringtext) {
StringoldValue = this.text;
this.text = text;
firePropertyChange(TEXT_CHANGED_PROPERTY, oldValue, text);
updateDisplayedMnemonicIndex(text, getMnemonic());
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, text);
}
if (text == null || oldValue == null || !text.equals(oldValue)) {
revalidate();
repaint();
}
}
/**
* Returns the state of the button. True if the
* toggle button is selected, false if it's not.
* @return true if the toggle button is selected, otherwise false
*/
publicbooleanisSelected() {
returnmodel.isSelected();
}
/**
* Sets the state of the button. Note that this method does not
* trigger an <code>actionEvent</code>.
* Call <code>doClick</code> to perform a programmatic action change.
*
* @param b true if the button is selected, otherwise false
*/
publicvoidsetSelected(booleanb) {
booleanoldValue = isSelected();
// TIGER - 4840653
// Removed code which fired an AccessibleState.SELECTED
// PropertyChangeEvent since this resulted in two
// identical events being fired since
// AbstractButton.fireItemStateChanged also fires the
// same event. This caused screen readers to speak the
// name of the item twice.
model.setSelected(b);
}
/**
* Programmatically perform a "click". This does the same
* thing as if the user had pressed and released the button.
*/
publicvoiddoClick() {
doClick(68);
}
/**
* Programmatically perform a "click". This does the same
* thing as if the user had pressed and released the button.
* The button stays visually "pressed" for <code>pressTime</code>
* milliseconds.
*
* @param pressTime the time to "hold down" the button, in milliseconds
*/
publicvoiddoClick(intpressTime) {
Dimensionsize = getSize();
model.setArmed(true);
model.setPressed(true);
paintImmediately(newRectangle(0,0, size.width, size.height));
try {
Thread.sleep(pressTime);
} catch(InterruptedExceptionie) {
}
model.setPressed(false);
model.setArmed(false);
}
/**
* Sets space for margin between the button's border and
* the label. Setting to <code>null</code> will cause the button to
* use the default margin. The button's default <code>Border</code>
* object will use this value to create the proper margin.
* However, if a non-default border is set on the button,
* it is that <code>Border</code> object's responsibility to create the
* appropriate margin space (else this property will
* effectively be ignored).
*
* @param m the space between the border and the label
*/
@BeanProperty(visualUpdate = true, description
= "The space between the button's border and the label.")
publicvoidsetMargin(Insetsm) {
// Cache the old margin if it comes from the UI
if(minstanceofUIResource) {
defaultMargin = m;
} elseif(margininstanceofUIResource) {
defaultMargin = margin;
}
// If the client passes in a null insets, restore the margin
// from the UI if possible
if(m == null && defaultMargin != null) {
m = defaultMargin;
}
Insetsold = margin;
margin = m;
firePropertyChange(MARGIN_CHANGED_PROPERTY, old, m);
if (old == null || !old.equals(m)) {
revalidate();
repaint();
}
}
/**
* Returns the margin between the button's border and
* the label.
*
* @return an <code>Insets</code> object specifying the margin
* between the botton's border and the label
* @see #setMargin
*/
publicInsetsgetMargin() {
return (margin == null) ? null : (Insets) margin.clone();
}
/**
* Returns the default icon.
* @return the default <code>Icon</code>
* @see #setIcon
*/
publicIcongetIcon() {
returndefaultIcon;
}
/**
* Sets the button's default icon. This icon is
* also used as the "pressed" and "disabled" icon if
* there is no explicitly set pressed icon.
*
* @param defaultIcon the icon used as the default image
* @see #getIcon
* @see #setPressedIcon
*/
@BeanProperty(visualUpdate = true, description
= "The button's default icon")
publicvoidsetIcon(IcondefaultIcon) {
IconoldValue = this.defaultIcon;
this.defaultIcon = defaultIcon;
/* If the default icon has really changed and we had
* generated the disabled icon for this component,
* (i.e. setDisabledIcon() was never called) then
* clear the disabledIcon field.
*/
if (defaultIcon != oldValue && (disabledIconinstanceofUIResource)) {
disabledIcon = null;
}
firePropertyChange(ICON_CHANGED_PROPERTY, oldValue, defaultIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, defaultIcon);
}
if (defaultIcon != oldValue) {
if (defaultIcon == null || oldValue == null ||
defaultIcon.getIconWidth() != oldValue.getIconWidth() ||
defaultIcon.getIconHeight() != oldValue.getIconHeight()) {
revalidate();
}
repaint();
}
}
/**
* Returns the pressed icon for the button.
* @return the <code>pressedIcon</code> property
* @see #setPressedIcon
*/
publicIcongetPressedIcon() {
returnpressedIcon;
}
/**
* Sets the pressed icon for the button.
* @param pressedIcon the icon used as the "pressed" image
* @see #getPressedIcon
*/
@BeanProperty(visualUpdate = true, description
= "The pressed icon for the button.")
publicvoidsetPressedIcon(IconpressedIcon) {
IconoldValue = this.pressedIcon;
this.pressedIcon = pressedIcon;
firePropertyChange(PRESSED_ICON_CHANGED_PROPERTY, oldValue, pressedIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, pressedIcon);
}
if (pressedIcon != oldValue) {
if (getModel().isPressed()) {
repaint();
}
}
}
/**
* Returns the selected icon for the button.
* @return the <code>selectedIcon</code> property
* @see #setSelectedIcon
*/
publicIcongetSelectedIcon() {
returnselectedIcon;
}
/**
* Sets the selected icon for the button.
* @param selectedIcon the icon used as the "selected" image
* @see #getSelectedIcon
*/
@BeanProperty(visualUpdate = true, description
= "The selected icon for the button.")
publicvoidsetSelectedIcon(IconselectedIcon) {
IconoldValue = this.selectedIcon;
this.selectedIcon = selectedIcon;
/* If the default selected icon has really changed and we had
* generated the disabled selected icon for this component,
* (i.e. setDisabledSelectedIcon() was never called) then
* clear the disabledSelectedIcon field.
*/
if (selectedIcon != oldValue &&
disabledSelectedIconinstanceofUIResource) {
disabledSelectedIcon = null;
}
firePropertyChange(SELECTED_ICON_CHANGED_PROPERTY, oldValue, selectedIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, selectedIcon);
}
if (selectedIcon != oldValue) {
if (isSelected()) {
repaint();
}
}
}
/**
* Returns the rollover icon for the button.
* @return the <code>rolloverIcon</code> property
* @see #setRolloverIcon
*/
publicIcongetRolloverIcon() {
returnrolloverIcon;
}
/**
* Sets the rollover icon for the button.
* @param rolloverIcon the icon used as the "rollover" image
* @see #getRolloverIcon
*/
@BeanProperty(visualUpdate = true, description
= "The rollover icon for the button.")
publicvoidsetRolloverIcon(IconrolloverIcon) {
IconoldValue = this.rolloverIcon;
this.rolloverIcon = rolloverIcon;
firePropertyChange(ROLLOVER_ICON_CHANGED_PROPERTY, oldValue, rolloverIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, rolloverIcon);
}
setRolloverEnabled(true);
if (rolloverIcon != oldValue) {
// No way to determine whether we are currently in
// a rollover state, so repaint regardless
repaint();
}
}
/**
* Returns the rollover selection icon for the button.
* @return the <code>rolloverSelectedIcon</code> property
* @see #setRolloverSelectedIcon
*/
publicIcongetRolloverSelectedIcon() {
returnrolloverSelectedIcon;
}
/**
* Sets the rollover selected icon for the button.
* @param rolloverSelectedIcon the icon used as the
* "selected rollover" image
* @see #getRolloverSelectedIcon
*/
@BeanProperty(visualUpdate = true, description
= "The rollover selected icon for the button.")
publicvoidsetRolloverSelectedIcon(IconrolloverSelectedIcon) {
IconoldValue = this.rolloverSelectedIcon;
this.rolloverSelectedIcon = rolloverSelectedIcon;
firePropertyChange(ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, oldValue, rolloverSelectedIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, rolloverSelectedIcon);
}
setRolloverEnabled(true);
if (rolloverSelectedIcon != oldValue) {
// No way to determine whether we are currently in
// a rollover state, so repaint regardless
if (isSelected()) {
repaint();
}
}
}
/**
* Returns the icon used by the button when it's disabled.
* If no disabled icon has been set this will forward the call to
* the look and feel to construct an appropriate disabled Icon.
* <p>
* Some look and feels might not render the disabled Icon, in which
* case they will ignore this.
*
* @return the <code>disabledIcon</code> property
* @see #getPressedIcon
* @see #setDisabledIcon
* @see javax.swing.LookAndFeel#getDisabledIcon
*/
@Transient
publicIcongetDisabledIcon() {
if (disabledIcon == null) {
disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, getIcon());
if (disabledIcon != null) {
firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, null, disabledIcon);
}
}
returndisabledIcon;
}
/**
* Sets the disabled icon for the button.
* @param disabledIcon the icon used as the disabled image
* @see #getDisabledIcon
*/
@BeanProperty(visualUpdate = true, description
= "The disabled icon for the button.")
publicvoidsetDisabledIcon(IcondisabledIcon) {
IconoldValue = this.disabledIcon;
this.disabledIcon = disabledIcon;
firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldValue, disabledIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, disabledIcon);
}
if (disabledIcon != oldValue) {
if (!isEnabled()) {
repaint();
}
}
}
/**
* Returns the icon used by the button when it's disabled and selected.
* If no disabled selection icon has been set, this will forward
* the call to the LookAndFeel to construct an appropriate disabled
* Icon from the selection icon if it has been set and to
* <code>getDisabledIcon()</code> otherwise.
* <p>
* Some look and feels might not render the disabled selected Icon, in
* which case they will ignore this.
*
* @return the <code>disabledSelectedIcon</code> property
* @see #getDisabledIcon
* @see #setDisabledSelectedIcon
* @see javax.swing.LookAndFeel#getDisabledSelectedIcon
*/
publicIcongetDisabledSelectedIcon() {
if (disabledSelectedIcon == null) {
if (selectedIcon != null) {
disabledSelectedIcon = UIManager.getLookAndFeel().
getDisabledSelectedIcon(this, getSelectedIcon());
} else {
returngetDisabledIcon();
}
}
returndisabledSelectedIcon;
}
/**
* Sets the disabled selection icon for the button.
* @param disabledSelectedIcon the icon used as the disabled
* selection image
* @see #getDisabledSelectedIcon
*/
@BeanProperty(visualUpdate = true, description
= "The disabled selection icon for the button.")
publicvoidsetDisabledSelectedIcon(IcondisabledSelectedIcon) {
IconoldValue = this.disabledSelectedIcon;
this.disabledSelectedIcon = disabledSelectedIcon;
firePropertyChange(DISABLED_SELECTED_ICON_CHANGED_PROPERTY, oldValue, disabledSelectedIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, disabledSelectedIcon);
}
if (disabledSelectedIcon != oldValue) {
if (disabledSelectedIcon == null || oldValue == null ||
disabledSelectedIcon.getIconWidth() != oldValue.getIconWidth() ||
disabledSelectedIcon.getIconHeight() != oldValue.getIconHeight()) {
revalidate();
}
if (!isEnabled() && isSelected()) {
repaint();
}
}
}
/**
* Returns the vertical alignment of the text and icon.
*
* @return the <code>verticalAlignment</code> property, one of the
* following values:
* <ul>
* <li>{@code SwingConstants.CENTER} (the default)
* <li>{@code SwingConstants.TOP}
* <li>{@code SwingConstants.BOTTOM}
* </ul>
*/
publicintgetVerticalAlignment() {
returnverticalAlignment;
}
/**
* Sets the vertical alignment of the icon and text.
* @param alignment one of the following values:
* <ul>
* <li>{@code SwingConstants.CENTER} (the default)
* <li>{@code SwingConstants.TOP}
* <li>{@code SwingConstants.BOTTOM}
* </ul>
* @throws IllegalArgumentException if the alignment is not one of the legal
* values listed above
*/
@BeanProperty(visualUpdate = true, enumerationValues = {
"SwingConstants.TOP",
"SwingConstants.CENTER",
"SwingConstants.BOTTOM"}, description
= "The vertical alignment of the icon and text.")
publicvoidsetVerticalAlignment(intalignment) {
if (alignment == verticalAlignment) return;
intoldValue = verticalAlignment;
verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY, oldValue, verticalAlignment); repaint();
}
/**
* Returns the horizontal alignment of the icon and text.
* {@code AbstractButton}'s default is {@code SwingConstants.CENTER},
* but subclasses such as {@code JCheckBox} may use a different default.
*
* @return the <code>horizontalAlignment</code> property,
* one of the following values:
* <ul>
* <li>{@code SwingConstants.RIGHT}
* <li>{@code SwingConstants.LEFT}
* <li>{@code SwingConstants.CENTER}
* <li>{@code SwingConstants.LEADING}
* <li>{@code SwingConstants.TRAILING}
* </ul>
*/
publicintgetHorizontalAlignment() {
returnhorizontalAlignment;
}
/**
* Sets the horizontal alignment of the icon and text.
* {@code AbstractButton}'s default is {@code SwingConstants.CENTER},
* but subclasses such as {@code JCheckBox} may use a different default.
*
* @param alignment the alignment value, one of the following values:
* <ul>
* <li>{@code SwingConstants.RIGHT}
* <li>{@code SwingConstants.LEFT}
* <li>{@code SwingConstants.CENTER}
* <li>{@code SwingConstants.LEADING}
* <li>{@code SwingConstants.TRAILING}
* </ul>
* @throws IllegalArgumentException if the alignment is not one of the
* valid values
*/
@BeanProperty(visualUpdate = true, enumerationValues = {
"SwingConstants.LEFT",
"SwingConstants.CENTER",
"SwingConstants.RIGHT",
"SwingConstants.LEADING",
"SwingConstants.TRAILING"}, description
= "The horizontal alignment of the icon and text.")
publicvoidsetHorizontalAlignment(intalignment) {
if (alignment == horizontalAlignment) return;
intoldValue = horizontalAlignment;
horizontalAlignment = checkHorizontalKey(alignment,
"horizontalAlignment");
firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY,
oldValue, horizontalAlignment);
repaint();
}
/**
* Returns the vertical position of the text relative to the icon.
* @return the <code>verticalTextPosition</code> property,
* one of the following values:
* <ul>
* <li>{@code SwingConstants.CENTER} (the default)
* <li>{@code SwingConstants.TOP}
* <li>{@code SwingConstants.BOTTOM}
* </ul>
*/
publicintgetVerticalTextPosition() {
returnverticalTextPosition;
}
/**
* Sets the vertical position of the text relative to the icon.
* @param textPosition one of the following values:
* <ul>
* <li>{@code SwingConstants.CENTER} (the default)
* <li>{@code SwingConstants.TOP}
* <li>{@code SwingConstants.BOTTOM}
* </ul>
*/
@BeanProperty(visualUpdate = true, enumerationValues = {
"SwingConstants.TOP",
"SwingConstants.CENTER",
"SwingConstants.BOTTOM"}, description
= "The vertical position of the text relative to the icon.")
publicvoidsetVerticalTextPosition(inttextPosition) {
if (textPosition == verticalTextPosition) return;
intoldValue = verticalTextPosition;
verticalTextPosition = checkVerticalKey(textPosition, "verticalTextPosition");
firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, oldValue, verticalTextPosition);
revalidate();
repaint();
}
/**
* Returns the horizontal position of the text relative to the icon.
* @return the <code>horizontalTextPosition</code> property,
* one of the following values:
* <ul>
* <li>{@code SwingConstants.RIGHT}
* <li>{@code SwingConstants.LEFT}
* <li>{@code SwingConstants.CENTER}
* <li>{@code SwingConstants.LEADING}
* <li>{@code SwingConstants.TRAILING} (the default)
* </ul>
*/
publicintgetHorizontalTextPosition() {
returnhorizontalTextPosition;
}
/**
* Sets the horizontal position of the text relative to the icon.
* @param textPosition one of the following values:
* <ul>
* <li>{@code SwingConstants.RIGHT}
* <li>{@code SwingConstants.LEFT}
* <li>{@code SwingConstants.CENTER}
* <li>{@code SwingConstants.LEADING}
* <li>{@code SwingConstants.TRAILING} (the default)
* </ul>
* @throws IllegalArgumentException if <code>textPosition</code>
* is not one of the legal values listed above
*/
@BeanProperty(visualUpdate = true, enumerationValues = {
"SwingConstants.LEFT",
"SwingConstants.CENTER",
"SwingConstants.RIGHT",
"SwingConstants.LEADING",
"SwingConstants.TRAILING"}, description
= "The horizontal position of the text relative to the icon.")
publicvoidsetHorizontalTextPosition(inttextPosition) {
if (textPosition == horizontalTextPosition) return;
intoldValue = horizontalTextPosition;
horizontalTextPosition = checkHorizontalKey(textPosition,
"horizontalTextPosition");
firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY,
oldValue,
horizontalTextPosition);
revalidate();
repaint();
}
/**
* Returns the amount of space between the text and the icon
* displayed in this button.
*
* @return an int equal to the number of pixels between the text
* and the icon.
* @since 1.4
* @see #setIconTextGap
*/
publicintgetIconTextGap() {
returniconTextGap;
}
/**
* If both the icon and text properties are set, this property
* defines the space between them.
* <p>
* The default value of this property is 4 pixels.
* <p>
* This is a JavaBeans bound property.
*
* @param iconTextGap the space between icon and text if these properties are set.
* @since 1.4
* @see #getIconTextGap
*/
@BeanProperty(visualUpdate = true, description
= "If both the icon and text properties are set, this property defines the space between them.")
publicvoidsetIconTextGap(inticonTextGap) {
intoldValue = this.iconTextGap;
this.iconTextGap = iconTextGap;
iconTextGapSet = true;
firePropertyChange("iconTextGap", oldValue, iconTextGap);
if (iconTextGap != oldValue) {
revalidate();
repaint();
}
}
/**
* Verify that the {@code key} argument is a legal value for the
* {@code horizontalAlignment} and {@code horizontalTextPosition}
* properties. Valid values are:
* <ul>
* <li>{@code SwingConstants.RIGHT}
* <li>{@code SwingConstants.LEFT}
* <li>{@code SwingConstants.CENTER}
* <li>{@code SwingConstants.LEADING}
* <li>{@code SwingConstants.TRAILING}
* </ul>
*
* @param key the property value to check
* @param exception the message to use in the
* {@code IllegalArgumentException} that is thrown for an invalid
* value
* @return the {@code key} argument
* @throws IllegalArgumentException if key is not one of the legal
* values listed above
* @see #setHorizontalTextPosition
* @see #setHorizontalAlignment
*/
protectedintcheckHorizontalKey(intkey, Stringexception) {
if ((key == LEFT) ||
(key == CENTER) ||
(key == RIGHT) ||
(key == LEADING) ||
(key == TRAILING)) {
returnkey;
} else {
thrownewIllegalArgumentException(exception);
}
}
/**
* Verify that the {@code key} argument is a legal value for the
* vertical properties. Valid values are:
* <ul>
* <li>{@code SwingConstants.CENTER}
* <li>{@code SwingConstants.TOP}
* <li>{@code SwingConstants.BOTTOM}
* </ul>
*
* @param key the property value to check
* @param exception the message to use in the
* {@code IllegalArgumentException} that is thrown for an invalid
* value
* @return the {@code key} argument
* @throws IllegalArgumentException if key is not one of the legal
* values listed above
*/
protectedintcheckVerticalKey(intkey, Stringexception) {
if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
returnkey;
} else {
thrownewIllegalArgumentException(exception);
}
}
/**
*{@inheritDoc}
*