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

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

fix #18703 - Scroll through TabPreferenceSetting using mouse wheel

  • Property svn:eol-style set to native
File size: 4.4 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.JScrollPane;
10import javax.swing.JTabbedPane;
11
12import org.openstreetmap.josm.gui.help.HelpUtil;
13import org.openstreetmap.josm.tools.GBC;
14import 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 */
21public 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 if (tabpane != null) {
52 tabpane.addMouseWheelListener(new PreferenceTabbedPane.WheelListener(tabpane));
53 }
54 }
55
56 @Override
57 public String getIconName() {
58 return iconName;
59 }
60
61 @Override
62 public String getTooltip() {
63 if (getDescription() != null) {
64 return "<html>"+getDescription()+"</html>";
65 } else {
66 return null;
67 }
68 }
69
70 @Override
71 public String getDescription() {
72 return description;
73 }
74
75 @Override
76 public String getTitle() {
77 return title;
78 }
79
80 /**
81 * Get the inner tab pane, if any.
82 * @return The JTabbedPane contained in this tab preference settings, or null if none is set.
83 * @since 5631
84 */
85 public final JTabbedPane getTabPane() {
86 return tabpane;
87 }
88
89 protected final void createPreferenceTabWithScrollPane(PreferenceTabbedPane gui, JPanel panel) {
90 GBC a = GBC.eol().insets(-5, 0, 0, 0);
91 a.anchor = GBC.EAST;
92
93 JScrollPane scrollPane = new JScrollPane(panel);
94 scrollPane.setBorder(null);
95
96 JPanel tab = gui.createPreferenceTab(this);
97 tab.add(scrollPane, GBC.eol().fill(GBC.BOTH));
98 tab.add(GBC.glue(0, 10), a);
99 }
100
101 @Override
102 public boolean selectSubTab(SubPreferenceSetting subPref) {
103 if (tabpane != null && subPref != null) {
104 Component tab = getSubTab(subPref);
105 if (tab != null) {
106 try {
107 tabpane.setSelectedComponent(tab);
108 return true;
109 } catch (IllegalArgumentException e) {
110 // Ignore exception and return false below
111 Logging.debug(Logging.getErrorMessage(e));
112 }
113 }
114 }
115 return false;
116 }
117
118 @Override
119 public final void addSubTab(SubPreferenceSetting sub, String title, Component component) {
120 addSubTab(sub, title, component, null);
121 }
122
123 @Override
124 public final void addSubTab(SubPreferenceSetting sub, String title, Component component, String tip) {
125 if (tabpane != null && component != null) {
126 tabpane.addTab(title, null, component, tip);
127 registerSubTab(sub, component);
128 }
129 }
130
131 @Override
132 public final void registerSubTab(SubPreferenceSetting sub, Component component) {
133 if (subSettingMap != null && sub != null && component != null) {
134 subSettingMap.put(sub, component);
135 }
136 }
137
138 @Override
139 public final Component getSubTab(SubPreferenceSetting sub) {
140 return subSettingMap != null ? subSettingMap.get(sub) : null;
141 }
142
143 @Override
144 public String getHelpContext() {
145 return HelpUtil.ht("/Action/Preferences");
146 }
147}
Note: See TracBrowser for help on using the repository browser.