[7937] | 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.JTabbedPane;
|
---|
| 10 |
|
---|
[13431] | 11 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
[7937] | 12 | import org.openstreetmap.josm.tools.GBC;
|
---|
[12620] | 13 | import org.openstreetmap.josm.tools.Logging;
|
---|
[7937] | 14 |
|
---|
[12460] | 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 | */
|
---|
[7937] | 20 | public 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;
|
---|
[8510] | 27 |
|
---|
[7937] | 28 | /**
|
---|
| 29 | * Constructs a new {@code DefaultTabPreferenceSetting}.
|
---|
| 30 | */
|
---|
[16553] | 31 | protected DefaultTabPreferenceSetting() {
|
---|
[7937] | 32 | this(null, null, null);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[16553] | 35 | protected DefaultTabPreferenceSetting(String iconName, String title, String description) {
|
---|
[7937] | 36 | this(iconName, title, description, false);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[16553] | 39 | protected DefaultTabPreferenceSetting(String iconName, String title, String description, boolean isExpert) {
|
---|
[7937] | 40 | this(iconName, title, description, isExpert, null);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[16553] | 43 | protected DefaultTabPreferenceSetting(String iconName, String title, String description, boolean isExpert, JTabbedPane tabpane) {
|
---|
[7937] | 44 | super(isExpert);
|
---|
| 45 | this.iconName = iconName;
|
---|
| 46 | this.description = description;
|
---|
| 47 | this.title = title;
|
---|
| 48 | this.tabpane = tabpane;
|
---|
[10593] | 49 | this.subSettingMap = tabpane != null ? new HashMap<>() : null;
|
---|
[15845] | 50 | if (tabpane != null) {
|
---|
| 51 | tabpane.addMouseWheelListener(new PreferenceTabbedPane.WheelListener(tabpane));
|
---|
| 52 | }
|
---|
[7937] | 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 | }
|
---|
[8510] | 87 |
|
---|
[7937] | 88 | protected final void createPreferenceTabWithScrollPane(PreferenceTabbedPane gui, JPanel panel) {
|
---|
[8510] | 89 | GBC a = GBC.eol().insets(-5, 0, 0, 0);
|
---|
[7937] | 90 | a.anchor = GBC.EAST;
|
---|
[8510] | 91 |
|
---|
[17159] | 92 | JPanel tab = gui.createPreferenceTab(this, true);
|
---|
| 93 | tab.add(panel, GBC.eol().fill(GBC.BOTH));
|
---|
[8510] | 94 | tab.add(GBC.glue(0, 10), a);
|
---|
[7937] | 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
|
---|
[12620] | 107 | Logging.debug(Logging.getErrorMessage(e));
|
---|
[7937] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | return false;
|
---|
| 112 | }
|
---|
[8510] | 113 |
|
---|
[7937] | 114 | @Override
|
---|
| 115 | public final void addSubTab(SubPreferenceSetting sub, String title, Component component) {
|
---|
| 116 | addSubTab(sub, title, component, null);
|
---|
| 117 | }
|
---|
[8510] | 118 |
|
---|
[7937] | 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 | }
|
---|
[8510] | 126 |
|
---|
[7937] | 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 | }
|
---|
[8510] | 133 |
|
---|
[7937] | 134 | @Override
|
---|
| 135 | public final Component getSubTab(SubPreferenceSetting sub) {
|
---|
| 136 | return subSettingMap != null ? subSettingMap.get(sub) : null;
|
---|
| 137 | }
|
---|
[13431] | 138 |
|
---|
| 139 | @Override
|
---|
[16966] | 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 |
|
---|
[17229] | 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 |
|
---|
[16966] | 159 | @Override
|
---|
[13431] | 160 | public String getHelpContext() {
|
---|
| 161 | return HelpUtil.ht("/Action/Preferences");
|
---|
| 162 | }
|
---|
[7937] | 163 | }
|
---|