1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Graphics;
|
---|
5 |
|
---|
6 | import javax.swing.JTabbedPane;
|
---|
7 | import javax.swing.plaf.basic.BasicTabbedPaneUI;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * A {@link JTabbedPane} extension that completely hides the tab area and border if it contains less than 2 tabs.
|
---|
11 | * @since 17314
|
---|
12 | */
|
---|
13 | public class HideableTabbedPane extends JTabbedPane {
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Creates an empty <code>HideableTabbedPane</code> with a default tab placement of <code>JTabbedPane.TOP</code>.
|
---|
17 | * @see #addTab
|
---|
18 | */
|
---|
19 | public HideableTabbedPane() {
|
---|
20 | initUI();
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Creates an empty <code>HideableTabbedPane</code> with the specified tab placement of either:
|
---|
25 | * <code>JTabbedPane.TOP</code>, <code>JTabbedPane.BOTTOM</code>, <code>JTabbedPane.LEFT</code>, or <code>JTabbedPane.RIGHT</code>.
|
---|
26 | *
|
---|
27 | * @param tabPlacement the placement for the tabs relative to the content
|
---|
28 | * @see #addTab
|
---|
29 | */
|
---|
30 | public HideableTabbedPane(int tabPlacement) {
|
---|
31 | super(tabPlacement);
|
---|
32 | initUI();
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Creates an empty <code>TabbedPane</code> with the specified tab placement and tab layout policy. Tab placement may be either:
|
---|
37 | * <code>JTabbedPane.TOP</code>, <code>JTabbedPane.BOTTOM</code>, <code>JTabbedPane.LEFT</code>, or <code>JTabbedPane.RIGHT</code>.
|
---|
38 | * Tab layout policy may be either: <code>JTabbedPane.WRAP_TAB_LAYOUT</code> or <code>JTabbedPane.SCROLL_TAB_LAYOUT</code>.
|
---|
39 | *
|
---|
40 | * @param tabPlacement the placement for the tabs relative to the content
|
---|
41 | * @param tabLayoutPolicy the policy for laying out tabs when all tabs will not fit on one run
|
---|
42 | * @exception IllegalArgumentException if tab placement or tab layout policy are not one of the above supported values
|
---|
43 | * @see #addTab
|
---|
44 | */
|
---|
45 | public HideableTabbedPane(int tabPlacement, int tabLayoutPolicy) {
|
---|
46 | super(tabPlacement, tabLayoutPolicy);
|
---|
47 | initUI();
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void initUI() {
|
---|
51 | // See https://stackoverflow.com/a/8897685/2257172
|
---|
52 | setUI(new BasicTabbedPaneUI() {
|
---|
53 | @Override
|
---|
54 | protected int calculateTabAreaHeight(int tabPlacement, int runCount, int maxTabHeight) {
|
---|
55 | return getTabCount() > 1 ? super.calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight) : 0;
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
|
---|
60 | if (getTabCount() > 1) {
|
---|
61 | super.paintTabBorder(g, tabPlacement, tabIndex, x, y, w, h, isSelected);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
|
---|
67 | if (getTabCount() > 1) {
|
---|
68 | super.paintTabBackground(g, tabPlacement, tabIndex, x, y, w, h, isSelected);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
|
---|
74 | if (getTabCount() > 1) {
|
---|
75 | super.paintContentBorder(g, tabPlacement, selectedIndex);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | });
|
---|
79 | }
|
---|
80 | }
|
---|