source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/DefaultTabPreferenceSetting.java@ 17994

Last change on this file since 17994 was 17229, checked in by simon04, 4 years ago

see #7548 - Re-organize the preference dialog (hide empty tabs)

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