001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.ActionEvent;
008import java.awt.event.KeyEvent;
009
010import org.openstreetmap.josm.Main;
011import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
012import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
013import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
014import org.openstreetmap.josm.tools.CheckParameterUtil;
015import org.openstreetmap.josm.tools.Shortcut;
016import org.openstreetmap.josm.tools.Utils;
017
018/**
019 * Open the Preferences dialog.
020 *
021 * @author imi
022 */
023public class PreferencesAction extends JosmAction implements Runnable {
024
025    private final Class<? extends TabPreferenceSetting> tab;
026    private final Class<? extends SubPreferenceSetting> subTab;
027
028    private PreferencesAction(String name, String tooltip,
029                              Class<? extends TabPreferenceSetting> tab, Class<? extends SubPreferenceSetting> subTab) {
030        super(name, "preference", tooltip, null, false, "preference_" + Utils.<Class>firstNonNull(tab, subTab).getName(), false);
031        this.tab = tab;
032        this.subTab = subTab;
033    }
034
035    public static PreferencesAction forPreferenceTab(String name, String tooltip, Class<? extends TabPreferenceSetting> tab) {
036        CheckParameterUtil.ensureParameterNotNull(tab);
037        return new PreferencesAction(name, tooltip, tab, null);
038    }
039
040    public static PreferencesAction forPreferenceSubTab(String name, String tooltip, Class<? extends SubPreferenceSetting> subTab) {
041        CheckParameterUtil.ensureParameterNotNull(subTab);
042        return new PreferencesAction(name, tooltip, null, subTab);
043    }
044
045    /**
046     * Create the preference action with "&Preferences" as label.
047     */
048    public PreferencesAction() {
049        super(tr("Preferences..."), "preference", tr("Open a preferences dialog for global settings."),
050                Shortcut.registerShortcut("system:preferences", tr("Preferences"), KeyEvent.VK_F12, Shortcut.DIRECT), true);
051        putValue("help", ht("/Action/Preferences"));
052        this.tab = null;
053        this.subTab = null;
054    }
055
056    /**
057     * Launch the preferences dialog.
058     */
059    @Override
060    public void actionPerformed(ActionEvent e) {
061        run();
062    }
063
064    @Override
065    public void run() {
066        final PreferenceDialog p = new PreferenceDialog(Main.parent);
067        if (tab != null) {
068            p.selectPreferencesTabByClass(tab);
069        } else if( subTab != null) {
070            p.selectSubPreferencesTabByClass(subTab);
071        }
072        p.setVisible(true);
073    }
074}