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.tools.GBC;
013
014public abstract class DefaultTabPreferenceSetting extends DefaultPreferenceSetting implements TabPreferenceSetting {
015
016    private final String iconName;
017    private final String description;
018    private final String title;
019    private final JTabbedPane tabpane;
020    private final Map<SubPreferenceSetting, Component> subSettingMap;
021    
022    public DefaultTabPreferenceSetting() {
023        this(null, null, null);
024    }
025
026    public DefaultTabPreferenceSetting(String iconName, String title, String description) {
027        this(iconName, title, description, false);
028    }
029
030    public DefaultTabPreferenceSetting(String iconName, String title, String description, boolean isExpert) {
031        this(iconName, title, description, isExpert, null);
032    }
033
034    public DefaultTabPreferenceSetting(String iconName, String title, String description, boolean isExpert, JTabbedPane tabpane) {
035        super(isExpert);
036        this.iconName = iconName;
037        this.description = description;
038        this.title = title;
039        this.tabpane = tabpane;
040        this.subSettingMap = tabpane != null ? new HashMap<SubPreferenceSetting, Component>() : null;
041    }
042
043    @Override
044    public String getIconName() {
045        return iconName;
046    }
047
048    @Override
049    public String getTooltip() {
050        if (getDescription() != null) {
051            return "<html>"+getDescription()+"</html>";
052        } else {
053            return null;
054        }
055    }
056
057    @Override
058    public String getDescription() {
059        return description;
060    }
061
062    @Override
063    public String getTitle() {
064        return title;
065    }
066
067    /**
068     * Get the inner tab pane, if any.
069     * @return The JTabbedPane contained in this tab preference settings, or null if none is set.
070     * @since 5631
071     */
072    public final JTabbedPane getTabPane() {
073        return tabpane;
074    }
075    
076    protected final void createPreferenceTabWithScrollPane(PreferenceTabbedPane gui, JPanel panel) {
077        GBC a = GBC.eol().insets(-5,0,0,0);
078        a.anchor = GBC.EAST;
079        
080        JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
081        scrollPane.setBorder(null);
082
083        JPanel tab = gui.createPreferenceTab(this);
084        tab.add(scrollPane, GBC.eol().fill(GBC.BOTH));
085        tab.add(GBC.glue(0,10), a);
086    }
087
088    @Override
089    public boolean selectSubTab(SubPreferenceSetting subPref) {
090        if (tabpane != null && subPref != null) {
091            Component tab = getSubTab(subPref);
092            if (tab != null) {
093                try {
094                    tabpane.setSelectedComponent(tab);
095                    return true;
096                } catch (IllegalArgumentException e) {
097                    // Ignore exception and return false below
098                }
099            }
100        }
101        return false;
102    }
103    
104    @Override
105    public final void addSubTab(SubPreferenceSetting sub, String title, Component component) {
106        addSubTab(sub, title, component, null);
107    }
108    
109    @Override
110    public final void addSubTab(SubPreferenceSetting sub, String title, Component component, String tip) {
111        if (tabpane != null && component != null) {
112            tabpane.addTab(title, null, component, tip);
113            registerSubTab(sub, component);
114        }
115    }
116    
117    @Override
118    public final void registerSubTab(SubPreferenceSetting sub, Component component) {
119        if (subSettingMap != null && sub != null && component != null) {
120            subSettingMap.put(sub, component);
121        }
122    }
123    
124    @Override
125    public final Component getSubTab(SubPreferenceSetting sub) {
126        return subSettingMap != null ? subSettingMap.get(sub) : null;
127    }
128}