1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import javax.swing.JPanel;
|
---|
9 | import javax.swing.JScrollPane;
|
---|
10 | import javax.swing.JTabbedPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
13 | import org.openstreetmap.josm.tools.GBC;
|
---|
14 | import org.openstreetmap.josm.tools.Logging;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Abstract base class for {@link TabPreferenceSetting} implementations.
|
---|
18 | *
|
---|
19 | * Support for common functionality, like icon, title and adding a tab ({@link SubPreferenceSetting}).
|
---|
20 | */
|
---|
21 | public abstract class DefaultTabPreferenceSetting extends DefaultPreferenceSetting implements TabPreferenceSetting {
|
---|
22 |
|
---|
23 | private final String iconName;
|
---|
24 | private final String description;
|
---|
25 | private final String title;
|
---|
26 | private final JTabbedPane tabpane;
|
---|
27 | private final Map<SubPreferenceSetting, Component> subSettingMap;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code DefaultTabPreferenceSetting}.
|
---|
31 | */
|
---|
32 | public DefaultTabPreferenceSetting() {
|
---|
33 | this(null, null, null);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public DefaultTabPreferenceSetting(String iconName, String title, String description) {
|
---|
37 | this(iconName, title, description, false);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public DefaultTabPreferenceSetting(String iconName, String title, String description, boolean isExpert) {
|
---|
41 | this(iconName, title, description, isExpert, null);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public DefaultTabPreferenceSetting(String iconName, String title, String description, boolean isExpert, JTabbedPane tabpane) {
|
---|
45 | super(isExpert);
|
---|
46 | this.iconName = iconName;
|
---|
47 | this.description = description;
|
---|
48 | this.title = title;
|
---|
49 | this.tabpane = tabpane;
|
---|
50 | this.subSettingMap = tabpane != null ? new HashMap<>() : null;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public String getIconName() {
|
---|
55 | return iconName;
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public String getTooltip() {
|
---|
60 | if (getDescription() != null) {
|
---|
61 | return "<html>"+getDescription()+"</html>";
|
---|
62 | } else {
|
---|
63 | return null;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public String getDescription() {
|
---|
69 | return description;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public String getTitle() {
|
---|
74 | return title;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Get the inner tab pane, if any.
|
---|
79 | * @return The JTabbedPane contained in this tab preference settings, or null if none is set.
|
---|
80 | * @since 5631
|
---|
81 | */
|
---|
82 | public final JTabbedPane getTabPane() {
|
---|
83 | return tabpane;
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected final void createPreferenceTabWithScrollPane(PreferenceTabbedPane gui, JPanel panel) {
|
---|
87 | GBC a = GBC.eol().insets(-5, 0, 0, 0);
|
---|
88 | a.anchor = GBC.EAST;
|
---|
89 |
|
---|
90 | JScrollPane scrollPane = new JScrollPane(panel);
|
---|
91 | scrollPane.setBorder(null);
|
---|
92 |
|
---|
93 | JPanel tab = gui.createPreferenceTab(this);
|
---|
94 | tab.add(scrollPane, GBC.eol().fill(GBC.BOTH));
|
---|
95 | tab.add(GBC.glue(0, 10), a);
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public boolean selectSubTab(SubPreferenceSetting subPref) {
|
---|
100 | if (tabpane != null && subPref != null) {
|
---|
101 | Component tab = getSubTab(subPref);
|
---|
102 | if (tab != null) {
|
---|
103 | try {
|
---|
104 | tabpane.setSelectedComponent(tab);
|
---|
105 | return true;
|
---|
106 | } catch (IllegalArgumentException e) {
|
---|
107 | // Ignore exception and return false below
|
---|
108 | Logging.debug(Logging.getErrorMessage(e));
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public final void addSubTab(SubPreferenceSetting sub, String title, Component component) {
|
---|
117 | addSubTab(sub, title, component, null);
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public final void addSubTab(SubPreferenceSetting sub, String title, Component component, String tip) {
|
---|
122 | if (tabpane != null && component != null) {
|
---|
123 | tabpane.addTab(title, null, component, tip);
|
---|
124 | registerSubTab(sub, component);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public final void registerSubTab(SubPreferenceSetting sub, Component component) {
|
---|
130 | if (subSettingMap != null && sub != null && component != null) {
|
---|
131 | subSettingMap.put(sub, component);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public final Component getSubTab(SubPreferenceSetting sub) {
|
---|
137 | return subSettingMap != null ? subSettingMap.get(sub) : null;
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public String getHelpContext() {
|
---|
142 | return HelpUtil.ht("/Action/Preferences");
|
---|
143 | }
|
---|
144 | }
|
---|