-
-
Notifications
You must be signed in to change notification settings - Fork 255
/
FlatTabbedPaneUI.java
4125 lines (3484 loc) · 138 KB
/
FlatTabbedPaneUI.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 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import static com.formdev.flatlaf.FlatClientProperties.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.IntConsumer;
import java.util.function.Predicate;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.TabbedPaneUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.icons.FlatTabbedPaneCloseIcon;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI;
import com.formdev.flatlaf.ui.FlatStylingSupport.UnknownStyleException;
import com.formdev.flatlaf.util.Animator;
import com.formdev.flatlaf.util.CubicBezierEasing;
import com.formdev.flatlaf.util.JavaCompatibility;
import com.formdev.flatlaf.util.LoggingFacade;
import com.formdev.flatlaf.util.StringUtils;
import com.formdev.flatlaf.util.UIScale;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JTabbedPane}.
*
* @clientProperty JTabbedPane.showTabSeparators boolean
* @clientProperty JTabbedPane.hasFullBorder boolean
*
* <!-- BasicTabbedPaneUI -->
*
* @uiDefault TabbedPane.font Font
* @uiDefault TabbedPane.background Color
* @uiDefault TabbedPane.foreground Color
* @uiDefault TabbedPane.shadow Color used for cropped line
* @uiDefault TabbedPane.textIconGap int
* @uiDefault TabbedPane.tabInsets Insets
* @uiDefault TabbedPane.selectedTabPadInsets Insets unused
* @uiDefault TabbedPane.tabAreaInsets Insets
* @uiDefault TabbedPane.tabsOverlapBorder boolean
* @uiDefault TabbedPane.tabRunOverlay int
* @uiDefault TabbedPane.tabsOpaque boolean
* @uiDefault TabbedPane.contentOpaque boolean unused
* @uiDefault TabbedPane.opaque boolean
* @uiDefault TabbedPane.selectionFollowsFocus boolean default is true
*
* <!-- FlatTabbedPaneUI -->
*
* @uiDefault TabbedPane.disabledForeground Color
* @uiDefault TabbedPane.selectedBackground Color optional
* @uiDefault TabbedPane.selectedForeground Color optional
* @uiDefault TabbedPane.underlineColor Color
* @uiDefault TabbedPane.inactiveUnderlineColor Color
* @uiDefault TabbedPane.disabledUnderlineColor Color
* @uiDefault TabbedPane.hoverColor Color optional
* @uiDefault TabbedPane.hoverForeground Color optional
* @uiDefault TabbedPane.focusColor Color optional
* @uiDefault TabbedPane.focusForeground Color optional
* @uiDefault TabbedPane.tabSeparatorColor Color optional; defaults to TabbedPane.contentAreaColor
* @uiDefault TabbedPane.contentAreaColor Color
* @uiDefault TabbedPane.minimumTabWidth int optional
* @uiDefault TabbedPane.maximumTabWidth int optional
* @uiDefault TabbedPane.tabHeight int
* @uiDefault TabbedPane.tabSelectionHeight int
* @uiDefault TabbedPane.cardTabSelectionHeight int
* @uiDefault TabbedPane.tabArc int
* @uiDefault TabbedPane.tabSelectionArc int
* @uiDefault TabbedPane.cardTabArc int
* @uiDefault TabbedPane.selectedInsets Insets
* @uiDefault TabbedPane.tabSelectionInsets Insets
* @uiDefault TabbedPane.contentSeparatorHeight int
* @uiDefault TabbedPane.showTabSeparators boolean
* @uiDefault TabbedPane.tabSeparatorsFullHeight boolean
* @uiDefault TabbedPane.hasFullBorder boolean
* @uiDefault TabbedPane.rotateTabRuns boolean
*
* @uiDefault TabbedPane.tabLayoutPolicy String wrap (default) or scroll
* @uiDefault TabbedPane.tabType String underlined (default) or card
* @uiDefault TabbedPane.tabsPopupPolicy String never or asNeeded (default)
* @uiDefault TabbedPane.scrollButtonsPolicy String never, asNeeded or asNeededSingle (default)
* @uiDefault TabbedPane.scrollButtonsPlacement String both (default) or trailing
*
* @uiDefault TabbedPane.tabAreaAlignment String leading (default), center, trailing or fill
* @uiDefault TabbedPane.tabAlignment String leading, center (default) or trailing
* @uiDefault TabbedPane.tabWidthMode String preferred (default), equal or compact
* @uiDefault TabbedPane.tabRotation String none (default), auto, left or right
* @uiDefault ScrollPane.smoothScrolling boolean
* @uiDefault TabbedPane.closeIcon Icon
*
* @uiDefault TabbedPane.arrowType String chevron (default) or triangle
* @uiDefault TabbedPane.buttonInsets Insets
* @uiDefault TabbedPane.buttonArc int
* @uiDefault TabbedPane.buttonHoverBackground Color
* @uiDefault TabbedPane.buttonPressedBackground Color
*
* @uiDefault TabbedPane.moreTabsButtonToolTipText String
* @uiDefault TabbedPane.tabCloseToolTipText String
*
* @author Karl Tauber
*/
public class FlatTabbedPaneUI
extends BasicTabbedPaneUI
implements StyleableUI, FlatTitlePane.TitleBarCaptionHitTest
{
// tab type
/** @since 2 */ protected static final int TAB_TYPE_UNDERLINED = 0;
/** @since 2 */ protected static final int TAB_TYPE_CARD = 1;
// tabs popup policy / scroll arrows policy
protected static final int NEVER = 0;
// protected static final int ALWAYS = 1;
protected static final int AS_NEEDED = 2;
protected static final int AS_NEEDED_SINGLE = 3;
// scroll arrows placement
protected static final int BOTH = 100;
// tab area alignment
protected static final int FILL = 100;
// tab width mode
protected static final int WIDTH_MODE_PREFERRED = 0;
protected static final int WIDTH_MODE_EQUAL = 1;
protected static final int WIDTH_MODE_COMPACT = 2;
// tab rotation
/** @since 3.3 */ protected static final int NONE = -1;
/** @since 3.3 */ protected static final int AUTO = -2;
private static Set<KeyStroke> focusForwardTraversalKeys;
private static Set<KeyStroke> focusBackwardTraversalKeys;
protected Color foreground;
@Styleable protected Color disabledForeground;
@Styleable protected Color selectedBackground;
@Styleable protected Color selectedForeground;
@Styleable protected Color underlineColor;
/** @since 2.2 */ @Styleable protected Color inactiveUnderlineColor;
@Styleable protected Color disabledUnderlineColor;
@Styleable protected Color hoverColor;
/** @since 3.1 */ @Styleable protected Color hoverForeground;
@Styleable protected Color focusColor;
/** @since 3.1 */ @Styleable protected Color focusForeground;
@Styleable protected Color tabSeparatorColor;
@Styleable protected Color contentAreaColor;
private int textIconGapUnscaled;
@Styleable protected int minimumTabWidth;
@Styleable protected int maximumTabWidth;
@Styleable protected int tabHeight;
@Styleable protected int tabSelectionHeight;
/** @since 2 */ @Styleable protected int cardTabSelectionHeight;
/** @since 3.2 */ @Styleable protected int tabArc;
/** @since 3.2 */ @Styleable protected int tabSelectionArc;
/** @since 3.2 */ @Styleable protected int cardTabArc;
/** @since 3.2 */ @Styleable protected Insets selectedInsets;
/** @since 3.2 */ @Styleable protected Insets tabSelectionInsets;
@Styleable protected int contentSeparatorHeight;
@Styleable protected boolean showTabSeparators;
@Styleable protected boolean tabSeparatorsFullHeight;
@Styleable protected boolean hasFullBorder;
@Styleable protected boolean tabsOpaque = true;
/** @since 2.5 */ @Styleable protected boolean rotateTabRuns = true;
@Styleable(type=String.class) private int tabType;
@Styleable(type=String.class) private int tabsPopupPolicy;
@Styleable(type=String.class) private int scrollButtonsPolicy;
@Styleable(type=String.class) private int scrollButtonsPlacement;
@Styleable(type=String.class) private int tabAreaAlignment;
@Styleable(type=String.class) private int tabAlignment;
@Styleable(type=String.class) private int tabWidthMode;
/** @since 3.3 */ @Styleable(type=String.class) private int tabRotation;
protected Icon closeIcon;
@Styleable protected String arrowType;
@Styleable protected Insets buttonInsets;
@Styleable protected int buttonArc;
@Styleable protected Color buttonHoverBackground;
@Styleable protected Color buttonPressedBackground;
@Styleable protected String moreTabsButtonToolTipText;
/** @since 2 */ @Styleable protected String tabCloseToolTipText;
// only used via styling (not in UI defaults, but has likewise client properties)
/** @since 2 */ @Styleable protected boolean showContentSeparator = true;
/** @since 2 */ @Styleable protected boolean hideTabAreaWithOneTab;
/** @since 2 */ @Styleable protected boolean tabClosable;
/** @since 2 */ @Styleable protected int tabIconPlacement = LEADING;
protected JViewport tabViewport;
protected FlatWheelTabScroller wheelTabScroller;
private JButton tabCloseButton;
private JButton moreTabsButton;
private Container leadingComponent;
private Container trailingComponent;
private Dimension scrollBackwardButtonPrefSize;
private Handler handler;
private boolean blockRollover;
private boolean rolloverTabClose;
private boolean pressedTabClose;
private boolean inBasicLayoutContainer;
private Object[] oldRenderingHints;
private Map<String, Object> oldStyleValues;
private boolean closeIconShared = true;
public static ComponentUI createUI( JComponent c ) {
return new FlatTabbedPaneUI();
}
@Override
public void installUI( JComponent c ) {
// initialize tab layout policy (if specified)
String tabLayoutPolicyStr = UIManager.getString( "TabbedPane.tabLayoutPolicy" );
if( tabLayoutPolicyStr != null ) {
int tabLayoutPolicy;
switch( tabLayoutPolicyStr ) {
default:
case "wrap": tabLayoutPolicy = JTabbedPane.WRAP_TAB_LAYOUT; break;
case "scroll": tabLayoutPolicy = JTabbedPane.SCROLL_TAB_LAYOUT; break;
}
((JTabbedPane)c).setTabLayoutPolicy( tabLayoutPolicy );
}
// initialize this defaults here because they are used in constructor
// of FlatTabAreaButton, which is invoked before installDefaults()
arrowType = UIManager.getString( "TabbedPane.arrowType" );
foreground = UIManager.getColor( "TabbedPane.foreground" );
disabledForeground = UIManager.getColor( "TabbedPane.disabledForeground" );
buttonHoverBackground = UIManager.getColor( "TabbedPane.buttonHoverBackground" );
buttonPressedBackground = UIManager.getColor( "TabbedPane.buttonPressedBackground" );
super.installUI( c );
FlatSelectedTabRepainter.install();
installStyle();
}
@Override
protected void installDefaults() {
if( UIManager.getBoolean( "TabbedPane.tabsOverlapBorder" ) ) {
// Force BasicTabbedPaneUI.tabsOverlapBorder to false,
// which is necessary for "more tabs" button to work correctly.
//
// If it would be true, class TabbedPaneScrollLayout would invoke TabbedPaneLayout.padSelectedTab(),
// which would modify rectangle of selected tab in a wrong way (for wrap tab layout policy).
// This would cause tab painting issues when scrolled and
// missing "more tabs" button if last tab is selected.
//
// All methods of BasicTabbedPaneUI that use tabsOverlapBorder (except
// the one method mentioned above) are overridden.
//
// This is normally not invoked because the default value for
// TabbedPane.tabsOverlapBorder is false in all FlatLaf themes.
// Anyway, 3rd party themes may have changed it.
// So make sure that it works anyway to avoid issues.
Object oldValue = UIManager.put( "TabbedPane.tabsOverlapBorder", false );
super.installDefaults();
UIManager.put( "TabbedPane.tabsOverlapBorder", oldValue );
} else
super.installDefaults();
selectedBackground = UIManager.getColor( "TabbedPane.selectedBackground" );
selectedForeground = UIManager.getColor( "TabbedPane.selectedForeground" );
underlineColor = UIManager.getColor( "TabbedPane.underlineColor" );
inactiveUnderlineColor = FlatUIUtils.getUIColor( "TabbedPane.inactiveUnderlineColor", underlineColor );
disabledUnderlineColor = UIManager.getColor( "TabbedPane.disabledUnderlineColor" );
hoverColor = UIManager.getColor( "TabbedPane.hoverColor" );
hoverForeground = UIManager.getColor( "TabbedPane.hoverForeground" );
focusColor = UIManager.getColor( "TabbedPane.focusColor" );
focusForeground = UIManager.getColor( "TabbedPane.focusForeground" );
tabSeparatorColor = UIManager.getColor( "TabbedPane.tabSeparatorColor" );
contentAreaColor = UIManager.getColor( "TabbedPane.contentAreaColor" );
textIconGapUnscaled = UIManager.getInt( "TabbedPane.textIconGap" );
minimumTabWidth = UIManager.getInt( "TabbedPane.minimumTabWidth" );
maximumTabWidth = UIManager.getInt( "TabbedPane.maximumTabWidth" );
tabHeight = UIManager.getInt( "TabbedPane.tabHeight" );
tabSelectionHeight = UIManager.getInt( "TabbedPane.tabSelectionHeight" );
cardTabSelectionHeight = UIManager.getInt( "TabbedPane.cardTabSelectionHeight" );
tabArc = UIManager.getInt( "TabbedPane.tabArc" );
tabSelectionArc = UIManager.getInt( "TabbedPane.tabSelectionArc" );
cardTabArc = UIManager.getInt( "TabbedPane.cardTabArc" );
selectedInsets = UIManager.getInsets( "TabbedPane.selectedInsets" );
tabSelectionInsets = UIManager.getInsets( "TabbedPane.tabSelectionInsets" );
contentSeparatorHeight = UIManager.getInt( "TabbedPane.contentSeparatorHeight" );
showTabSeparators = UIManager.getBoolean( "TabbedPane.showTabSeparators" );
tabSeparatorsFullHeight = UIManager.getBoolean( "TabbedPane.tabSeparatorsFullHeight" );
hasFullBorder = UIManager.getBoolean( "TabbedPane.hasFullBorder" );
tabsOpaque = UIManager.getBoolean( "TabbedPane.tabsOpaque" );
rotateTabRuns = FlatUIUtils.getUIBoolean( "TabbedPane.rotateTabRuns", true );
tabType = parseTabType( UIManager.getString( "TabbedPane.tabType" ) );
tabsPopupPolicy = parseTabsPopupPolicy( UIManager.getString( "TabbedPane.tabsPopupPolicy" ) );
scrollButtonsPolicy = parseScrollButtonsPolicy( UIManager.getString( "TabbedPane.scrollButtonsPolicy" ) );
scrollButtonsPlacement = parseScrollButtonsPlacement( UIManager.getString( "TabbedPane.scrollButtonsPlacement" ) );
tabAreaAlignment = parseAlignment( UIManager.getString( "TabbedPane.tabAreaAlignment" ), LEADING );
tabAlignment = parseAlignment( UIManager.getString( "TabbedPane.tabAlignment" ), CENTER );
tabWidthMode = parseTabWidthMode( UIManager.getString( "TabbedPane.tabWidthMode" ) );
tabRotation = parseTabRotation( UIManager.getString( "TabbedPane.tabRotation" ) );
closeIcon = UIManager.getIcon( "TabbedPane.closeIcon" );
closeIconShared = true;
buttonInsets = UIManager.getInsets( "TabbedPane.buttonInsets" );
buttonArc = UIManager.getInt( "TabbedPane.buttonArc" );
Locale l = tabPane.getLocale();
moreTabsButtonToolTipText = UIManager.getString( "TabbedPane.moreTabsButtonToolTipText", l );
tabCloseToolTipText = UIManager.getString( "TabbedPane.tabCloseToolTipText", l );
// scale
textIconGap = scale( textIconGapUnscaled );
// replace focus forward/backward traversal keys with TAB/Shift+TAB because
// the default also includes Ctrl+TAB/Ctrl+Shift+TAB, which we need to switch tabs
if( focusForwardTraversalKeys == null ) {
focusForwardTraversalKeys = Collections.singleton( KeyStroke.getKeyStroke( KeyEvent.VK_TAB, 0 ) );
focusBackwardTraversalKeys = Collections.singleton( KeyStroke.getKeyStroke( KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK ) );
}
// Ideally we should use `LookAndFeel.installProperty( tabPane, "focusTraversalKeysForward", keys )` here
// instead of `tabPane.setFocusTraversalKeys()`, but WindowsTabbedPaneUI also uses later method
// and switching from Windows LaF to FlatLaf would not replace the keys and Ctrl+TAB would not work.
tabPane.setFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, focusForwardTraversalKeys );
tabPane.setFocusTraversalKeys( KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, focusBackwardTraversalKeys );
MigLayoutVisualPadding.install( tabPane, null );
}
@Override
protected void uninstallDefaults() {
// restore focus forward/backward traversal keys
tabPane.setFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null );
tabPane.setFocusTraversalKeys( KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null );
super.uninstallDefaults();
foreground = null;
disabledForeground = null;
selectedBackground = null;
selectedForeground = null;
underlineColor = null;
inactiveUnderlineColor = null;
disabledUnderlineColor = null;
hoverColor = null;
hoverForeground = null;
focusColor = null;
focusForeground = null;
tabSeparatorColor = null;
contentAreaColor = null;
closeIcon = null;
buttonHoverBackground = null;
buttonPressedBackground = null;
oldStyleValues = null;
MigLayoutVisualPadding.uninstall( tabPane );
}
@Override
protected void installComponents() {
super.installComponents();
// find scrollable tab viewport
tabViewport = null;
if( isScrollTabLayout() ) {
for( Component c : tabPane.getComponents() ) {
if( c instanceof JViewport && c.getClass().getName().equals( "javax.swing.plaf.basic.BasicTabbedPaneUI$ScrollableTabViewport" ) ) {
tabViewport = (JViewport) c;
break;
}
}
}
installHiddenTabsNavigation();
installLeadingComponent();
installTrailingComponent();
}
@Override
protected void uninstallComponents() {
// uninstall hidden tabs navigation before invoking super.uninstallComponents() for
// correct uninstallation of BasicTabbedPaneUI tab scroller support
uninstallHiddenTabsNavigation();
uninstallLeadingComponent();
uninstallTrailingComponent();
super.uninstallComponents();
tabCloseButton = null;
tabViewport = null;
}
protected void installHiddenTabsNavigation() {
if( !isScrollTabLayout() || tabViewport == null )
return;
// At this point, BasicTabbedPaneUI already has installed
// TabbedPaneScrollLayout (in super.createLayoutManager()) and
// ScrollableTabSupport, ScrollableTabViewport, ScrollableTabPanel, etc.
// (in super.installComponents()).
// install own layout manager that delegates to original layout manager
tabPane.setLayout( createScrollLayoutManager( (TabbedPaneLayout) tabPane.getLayout() ) );
// create and add "more tabs" button
moreTabsButton = createMoreTabsButton();
tabPane.add( moreTabsButton );
}
protected void uninstallHiddenTabsNavigation() {
// restore layout manager before invoking super.uninstallComponents() for
// correct uninstallation of BasicTabbedPaneUI tab scroller support
if( tabPane.getLayout() instanceof FlatTabbedPaneScrollLayout )
tabPane.setLayout( ((FlatTabbedPaneScrollLayout)tabPane.getLayout()).delegate );
if( moreTabsButton != null ) {
tabPane.remove( moreTabsButton );
moreTabsButton = null;
}
}
protected void installLeadingComponent() {
Object c = tabPane.getClientProperty( TABBED_PANE_LEADING_COMPONENT );
if( c instanceof Component ) {
leadingComponent = new ContainerUIResource( (Component) c );
tabPane.add( leadingComponent );
}
}
protected void uninstallLeadingComponent() {
if( leadingComponent != null ) {
tabPane.remove( leadingComponent );
leadingComponent = null;
}
}
protected void installTrailingComponent() {
Object c = tabPane.getClientProperty( TABBED_PANE_TRAILING_COMPONENT );
if( c instanceof Component ) {
trailingComponent = new ContainerUIResource( (Component) c );
tabPane.add( trailingComponent );
}
}
protected void uninstallTrailingComponent() {
if( trailingComponent != null ) {
tabPane.remove( trailingComponent );
trailingComponent = null;
}
}
@Override
protected void installListeners() {
super.installListeners();
getHandler().installListeners();
if( tabViewport != null && (wheelTabScroller = createWheelTabScroller()) != null ) {
// ideally we would add the mouse listeners to the viewport, but then the
// mouse listener of the tabbed pane would not receive events while
// the mouse pointer is over the viewport
tabPane.addMouseWheelListener( wheelTabScroller );
tabPane.addMouseMotionListener( wheelTabScroller );
tabPane.addMouseListener( wheelTabScroller );
}
}
@Override
protected void uninstallListeners() {
super.uninstallListeners();
if( handler != null ) {
handler.uninstallListeners();
handler = null;
}
if( wheelTabScroller != null ) {
wheelTabScroller.uninstall();
tabPane.removeMouseWheelListener( wheelTabScroller );
tabPane.removeMouseMotionListener( wheelTabScroller );
tabPane.removeMouseListener( wheelTabScroller );
wheelTabScroller = null;
}
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
// get shared action map, used for all tabbed panes
ActionMap map = SwingUtilities.getUIActionMap( tabPane );
if( map != null ) {
// this is required for the case that those actions are used from outside
// (e.g. wheel tab scroller in NetBeans)
RunWithOriginalLayoutManagerDelegateAction.install( map, "scrollTabsForwardAction" );
RunWithOriginalLayoutManagerDelegateAction.install( map, "scrollTabsBackwardAction" );
}
}
private Handler getHandler() {
if( handler == null )
handler = new Handler();
return handler;
}
protected FlatWheelTabScroller createWheelTabScroller() {
return new FlatWheelTabScroller();
}
@Override
protected MouseListener createMouseListener() {
Handler handler = getHandler();
handler.mouseDelegate = super.createMouseListener();
return handler;
}
@Override
protected PropertyChangeListener createPropertyChangeListener() {
Handler handler = getHandler();
handler.propertyChangeDelegate = super.createPropertyChangeListener();
return handler;
}
@Override
protected ChangeListener createChangeListener() {
Handler handler = getHandler();
handler.changeDelegate = super.createChangeListener();
return handler;
}
@Override
protected FocusListener createFocusListener() {
Handler handler = getHandler();
handler.focusDelegate = super.createFocusListener();
return handler;
}
@Override
protected LayoutManager createLayoutManager() {
if( tabPane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT )
return new FlatTabbedPaneLayout();
return super.createLayoutManager();
}
protected LayoutManager createScrollLayoutManager( TabbedPaneLayout delegate ) {
return new FlatTabbedPaneScrollLayout( delegate );
}
protected JButton createMoreTabsButton() {
return new FlatMoreTabsButton();
}
@Override
protected JButton createScrollButton( int direction ) {
return new FlatScrollableTabButton( direction );
}
/** @since 2 */
protected void installStyle() {
try {
applyStyle( FlatStylingSupport.getResolvedStyle( tabPane, "TabbedPane" ) );
} catch( RuntimeException ex ) {
LoggingFacade.INSTANCE.logSevere( null, ex );
}
}
/** @since 2 */
protected void applyStyle( Object style ) {
oldStyleValues = FlatStylingSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
// update buttons
for( Component c : tabPane.getComponents() ) {
if( c instanceof FlatTabAreaButton )
((FlatTabAreaButton)c).updateStyle();
}
}
/** @since 2 */
protected Object applyStyleProperty( String key, Object value ) {
// close icon
if( key.startsWith( "close" ) ) {
if( !(closeIcon instanceof FlatTabbedPaneCloseIcon) )
return new UnknownStyleException( key );
if( closeIconShared ) {
closeIcon = FlatStylingSupport.cloneIcon( closeIcon );
closeIconShared = false;
}
return ((FlatTabbedPaneCloseIcon)closeIcon).applyStyleProperty( key, value );
}
if( value instanceof String ) {
switch( key ) {
case "tabType": value = parseTabType( (String) value ); break;
case "tabsPopupPolicy": value = parseTabsPopupPolicy( (String) value ); break;
case "scrollButtonsPolicy": value = parseScrollButtonsPolicy( (String) value ); break;
case "scrollButtonsPlacement": value = parseScrollButtonsPlacement( (String) value ); break;
case "tabAreaAlignment": value = parseAlignment( (String) value, LEADING ); break;
case "tabAlignment": value = parseAlignment( (String) value, CENTER ); break;
case "tabWidthMode": value = parseTabWidthMode( (String) value ); break;
case "tabRotation": value = parseTabRotation( (String) value ); break;
case "tabIconPlacement": value = parseTabIconPlacement( (String) value ); break;
}
} else {
Object oldValue;
switch( key ) {
// BasicTabbedPaneUI
case "tabInsets": oldValue = tabInsets; tabInsets = (Insets) value; return oldValue;
case "tabAreaInsets": oldValue = tabAreaInsets; tabAreaInsets = (Insets) value; return oldValue;
case "textIconGap":
oldValue = textIconGapUnscaled;
textIconGapUnscaled = (int) value;
textIconGap = scale( textIconGapUnscaled );
return oldValue;
}
}
return FlatStylingSupport.applyToAnnotatedObjectOrComponent( this, tabPane, key, value );
}
/** @since 2 */
@Override
public Map<String, Class<?>> getStyleableInfos( JComponent c ) {
Map<String, Class<?>> infos = new FlatStylingSupport.StyleableInfosMap<>();
infos.put( "tabInsets", Insets.class );
infos.put( "tabAreaInsets", Insets.class );
infos.put( "textIconGap", int.class );
FlatStylingSupport.collectAnnotatedStyleableInfos( this, infos );
if( closeIcon instanceof FlatTabbedPaneCloseIcon )
infos.putAll( ((FlatTabbedPaneCloseIcon)closeIcon).getStyleableInfos() );
return infos;
}
/** @since 2.5 */
@Override
public Object getStyleableValue( JComponent c, String key ) {
// close icon
if( key.startsWith( "close" ) ) {
return (closeIcon instanceof FlatTabbedPaneCloseIcon)
? ((FlatTabbedPaneCloseIcon)closeIcon).getStyleableValue( key )
: null;
}
switch( key ) {
// BasicTabbedPaneUI
case "tabInsets": return tabInsets;
case "tabAreaInsets": return tabAreaInsets;
case "textIconGap": return textIconGapUnscaled;
// FlatTabbedPaneUI
case "tabType":
switch( tabType ) {
default:
case TAB_TYPE_UNDERLINED: return TABBED_PANE_TAB_TYPE_UNDERLINED;
case TAB_TYPE_CARD: return TABBED_PANE_TAB_TYPE_CARD;
}
case "tabsPopupPolicy":
switch( tabsPopupPolicy ) {
default:
case AS_NEEDED: return TABBED_PANE_POLICY_AS_NEEDED;
case NEVER: return TABBED_PANE_POLICY_NEVER;
}
case "scrollButtonsPolicy":
switch( scrollButtonsPolicy ) {
default:
case AS_NEEDED_SINGLE: return TABBED_PANE_POLICY_AS_NEEDED_SINGLE;
case AS_NEEDED: return TABBED_PANE_POLICY_AS_NEEDED;
case NEVER: return TABBED_PANE_POLICY_NEVER;
}
case "scrollButtonsPlacement":
switch( scrollButtonsPlacement ) {
default:
case BOTH: return TABBED_PANE_PLACEMENT_BOTH;
case TRAILING: return TABBED_PANE_PLACEMENT_TRAILING;
}
case "tabAreaAlignment": return alignmentToString( tabAreaAlignment, TABBED_PANE_ALIGN_LEADING );
case "tabAlignment": return alignmentToString( tabAlignment, TABBED_PANE_ALIGN_CENTER );
case "tabWidthMode":
switch( tabWidthMode ) {
default:
case WIDTH_MODE_PREFERRED: return TABBED_PANE_TAB_WIDTH_MODE_PREFERRED;
case WIDTH_MODE_EQUAL: return TABBED_PANE_TAB_WIDTH_MODE_EQUAL;
case WIDTH_MODE_COMPACT: return TABBED_PANE_TAB_WIDTH_MODE_COMPACT;
}
case "tabRotation":
switch( tabRotation ) {
default:
case NONE: return TABBED_PANE_TAB_ROTATION_NONE;
case AUTO: return TABBED_PANE_TAB_ROTATION_AUTO;
case LEFT: return TABBED_PANE_TAB_ROTATION_LEFT;
case RIGHT: return TABBED_PANE_TAB_ROTATION_RIGHT;
}
case "tabIconPlacement":
switch( tabIconPlacement ) {
default:
case LEADING: return "leading";
case TRAILING: return "trailing";
case TOP: return "top";
case BOTTOM: return "bottom";
}
}
return FlatStylingSupport.getAnnotatedStyleableValue( this, key );
}
protected void setRolloverTab( int x, int y ) {
setRolloverTab( tabForCoordinate( tabPane, x, y ) );
}
@Override
protected void setRolloverTab( int index ) {
if( blockRollover )
return;
int oldIndex = getRolloverTab();
super.setRolloverTab( index );
if( index != oldIndex )
repaintRolloverLaterOnce( oldIndex );
}
private boolean repaintRolloverPending;
/**
* Repaint rollover tab, but deferred and only once.
* This is to avoid unnecessary repaints in case of temporary changes to rollover tab.
*
* E.g. when moving mouse over a single tab, a re-layout may occur and
* set rollover tab to -1, in BasicTabbedPaneUI.TabbedPaneLayout.layoutContainer() and
* BasicTabbedPaneUI.TabbedPaneScrollLayout.layoutContainer(), and subsequently
* change rollover tab back to previous value.
*/
private void repaintRolloverLaterOnce( int oldIndex ) {
if( repaintRolloverPending )
return;
repaintRolloverPending = true;
EventQueue.invokeLater( () -> {
repaintRolloverPending = false;
if( tabPane == null )
return;
int index = getRolloverTab();
if( index != oldIndex ) {
// repaint old and new hover tabs
repaintTab( oldIndex );
repaintTab( index );
}
} );
}
protected boolean isRolloverTabClose() {
return rolloverTabClose;
}
protected void setRolloverTabClose( boolean rollover ) {
if( rolloverTabClose == rollover )
return;
rolloverTabClose = rollover;
repaintTab( getRolloverTab() );
}
protected boolean isPressedTabClose() {
return pressedTabClose;
}
protected void setPressedTabClose( boolean pressed ) {
if( pressedTabClose == pressed )
return;
pressedTabClose = pressed;
repaintTab( getRolloverTab() );
}
private void repaintTab( int tabIndex ) {
if( tabIndex < 0 || tabIndex >= tabPane.getTabCount() )
return;
Rectangle r = getTabBounds( tabPane, tabIndex );
if( r == null )
return;
// increase size of repaint region to include part of content border
if( contentSeparatorHeight > 0 &&
clientPropertyBoolean( tabPane, TABBED_PANE_SHOW_CONTENT_SEPARATOR, true ) )
{
int sh = scale( contentSeparatorHeight );
switch( tabPane.getTabPlacement() ) {
default:
case TOP: r.height += sh; break;
case BOTTOM: r.height += sh; r.y -= sh; break;
case LEFT: r.width += sh; break;
case RIGHT: r.width += sh; r.x -= sh; break;
}
}
tabPane.repaint( r );
}
private boolean inCalculateEqual;
@Override
protected int calculateTabWidth( int tabPlacement, int tabIndex, FontMetrics metrics ) {
return (getRealTabRotation( tabPlacement ) == NONE)
? calculateTabWidthImpl( tabPlacement, tabIndex, metrics, false )
: calculateTabHeightImpl( tabPlacement, tabIndex, metrics.getHeight(), true );
}
private int calculateTabWidthImpl( int tabPlacement, int tabIndex, FontMetrics metrics, boolean rotated ) {
int tabWidthMode = getTabWidthMode();
if( tabWidthMode == WIDTH_MODE_EQUAL && isHorizontalOrRotated( tabPlacement ) && !inCalculateEqual ) {
inCalculateEqual = true;
try {
return isHorizontalTabPlacement( tabPlacement )
? calculateMaxTabWidth( tabPlacement )
: calculateMaxTabHeight( tabPlacement );
} finally {
inCalculateEqual = false;
}
}
// update textIconGap before used in super class
textIconGap = scale( textIconGapUnscaled );
int tabWidth;
Icon icon;
if( tabWidthMode == WIDTH_MODE_COMPACT &&
tabIndex != tabPane.getSelectedIndex() &&
isHorizontalOrRotated( tabPlacement ) &&
tabPane.getTabComponentAt( tabIndex ) == null &&
(icon = getIconForTab( tabIndex )) != null )
{
Insets tabInsets = getTabInsets( tabPlacement, tabIndex );
tabWidth = icon.getIconWidth() + tabInsets.left + tabInsets.right;
} else {
int iconPlacement = clientPropertyInt( tabPane, TABBED_PANE_TAB_ICON_PLACEMENT, tabIconPlacement );
if( (iconPlacement == TOP || iconPlacement == BOTTOM) &&
tabPane.getTabComponentAt( tabIndex ) == null &&
(icon = getIconForTab( tabIndex )) != null )
{
// TOP and BOTTOM icon placement
tabWidth = icon.getIconWidth();
View view = getTextViewForTab( tabIndex );
if( view != null )
tabWidth = Math.max( tabWidth, (int) view.getPreferredSpan( View.X_AXIS ) );
else {
String title = tabPane.getTitleAt( tabIndex );
if( title != null )
tabWidth = Math.max( tabWidth, metrics.stringWidth( title ) );
}
Insets tabInsets = getTabInsets( tabPlacement, tabIndex );
tabWidth += tabInsets.left + tabInsets.right;
} else {
tabWidth = super.calculateTabWidth( tabPlacement, tabIndex, metrics ) - 3 /* was added by superclass */;
// tab components are not rotated
Component tabComponent;
if( rotated && (tabComponent = tabPane.getTabComponentAt( tabIndex )) != null ) {
Dimension prefSize = tabComponent.getPreferredSize();
tabWidth = tabWidth - prefSize.width + prefSize.height;
}
}
}
// make tab wider if closable
if( isTabClosable( tabIndex ) )
tabWidth += closeIcon.getIconWidth();
// apply minimum and maximum tab width
int min = getTabClientPropertyInt( tabIndex, TABBED_PANE_MINIMUM_TAB_WIDTH, minimumTabWidth );
int max = getTabClientPropertyInt( tabIndex, TABBED_PANE_MAXIMUM_TAB_WIDTH, maximumTabWidth );
if( min > 0 )
tabWidth = Math.max( tabWidth, scale( min ) );
if( max > 0 && tabPane.getTabComponentAt( tabIndex ) == null )
tabWidth = Math.min( tabWidth, scale( max ) );
return tabWidth;
}
@Override
protected int calculateTabHeight( int tabPlacement, int tabIndex, int fontHeight ) {
return (getRealTabRotation( tabPlacement ) == NONE)
? calculateTabHeightImpl( tabPlacement, tabIndex, fontHeight, false )
: calculateTabWidthImpl( tabPlacement, tabIndex, getFontMetrics(), true );
}
private int calculateTabHeightImpl( int tabPlacement, int tabIndex, int fontHeight, boolean rotated ) {
int tabHeight;
Icon icon;
int iconPlacement = clientPropertyInt( tabPane, TABBED_PANE_TAB_ICON_PLACEMENT, tabIconPlacement );
if( (iconPlacement == TOP || iconPlacement == BOTTOM) &&
tabPane.getTabComponentAt( tabIndex ) == null &&
(icon = getIconForTab( tabIndex )) != null )
{
// TOP and BOTTOM icon placement
tabHeight = icon.getIconHeight();