001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences;
003
004import java.awt.Component;
005import java.util.HashMap;
006import java.util.Map;
007
008import javax.swing.JPanel;
009import javax.swing.JScrollPane;
010import javax.swing.JTabbedPane;
011
012import org.openstreetmap.josm.gui.help.HelpUtil;
013import org.openstreetmap.josm.tools.GBC;
014import org.openstreetmap.josm.tools.Logging;
015
016/**
017 * Abstract base class for {@link TabPreferenceSetting} implementations.
018 *
019 * Support for common functionality, like icon, title and adding a tab ({@link SubPreferenceSetting}).
020 */
021public abstract class DefaultTabPreferenceSetting extends DefaultPreferenceSetting implements TabPreferenceSetting {
022
023    private final String iconName;
024    private final String description;
025    private final String title;
026    private final JTabbedPane tabpane;
027    private final Map<SubPreferenceSetting, Component> subSettingMap;
028
029    /**
030     * Constructs a new {@code DefaultTabPreferenceSetting}.
031     */
032    public DefaultTabPreferenceSetting() {
033        this(null, null, null);
034    }
035
036    public DefaultTabPreferenceSetting(String iconName, String title, String description) {
037        this(iconName, title, description, false);
038    }
039
040    public DefaultTabPreferenceSetting(String iconName, String title, String description, boolean isExpert) {
041        this(iconName, title, description, isExpert, null);
042    }
043
044    public DefaultTabPreferenceSetting(String iconName, String title, String description, boolean isExpert, JTabbedPane tabpane) {
045        super(isExpert);
046        this.iconName = iconName;
047        this.description = description;
048        this.title = title;
049        this.tabpane = tabpane;
050        this.subSettingMap = tabpane != null ? new HashMap<>() : null;
051    }
052
053    @Override
054    public String getIconName() {
055        return iconName;
056    }
057
058    @Override
059    public String getTooltip() {
060        if (getDescription() != null) {
061            return "<html>"+getDescription()+"</html>";
062        } else {
063            return null;
064        }
065    }
066
067    @Override
068    public String getDescription() {
069        return description;
070    }
071
072    @Override
073    public String getTitle() {
074        return title;
075    }
076
077    /**
078     * Get the inner tab pane, if any.
079     * @return The JTabbedPane contained in this tab preference settings, or null if none is set.
080     * @since 5631
081     */
082    public final JTabbedPane getTabPane() {
083        return tabpane;
084    }
085
086    protected final void createPreferenceTabWithScrollPane(PreferenceTabbedPane gui, JPanel panel) {
087        GBC a = GBC.eol().insets(-5, 0, 0, 0);
088        a.anchor = GBC.EAST;
089
090        JScrollPane scrollPane = new JScrollPane(panel);
091        scrollPane.setBorder(null);
092
093        JPanel tab = gui.createPreferenceTab(this);
094        tab.add(scrollPane, GBC.eol().fill(GBC.BOTH));
095        tab.add(GBC.glue(0, 10), a);
096    }
097
098    @Override
099    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}