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 icon, String tooltip,
029                              Class<? extends TabPreferenceSetting> tab, Class<? extends SubPreferenceSetting> subTab) {
030        super(name, icon, tooltip, null, false, "preference_" + Utils.<Class<?>>firstNonNull(tab, subTab).getName(), false);
031        this.tab = tab;
032        this.subTab = subTab;
033    }
034
035    /**
036     * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given tab, with default icon.
037     * @param name The action name
038     * @param tooltip The action tooltip
039     * @param tab The preferences tab to select
040     * @return The created action
041     */
042    public static PreferencesAction forPreferenceTab(String name, String tooltip, Class<? extends TabPreferenceSetting> tab) {
043        return forPreferenceTab(name, tooltip, tab, "preference");
044    }
045
046    /**
047     * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given tab, with custom icon.
048     * @param name The action name
049     * @param tooltip The action tooltip
050     * @param tab The preferences tab to select
051     * @param icon The action icon
052     * @return The created action
053     * @since 6969
054     */
055    public static PreferencesAction forPreferenceTab(String name, String tooltip, Class<? extends TabPreferenceSetting> tab, String icon) {
056        CheckParameterUtil.ensureParameterNotNull(tab);
057        return new PreferencesAction(name, icon, tooltip, tab, null);
058    }
059
060    /**
061     * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given subtab, with default icon.
062     * @param name The action name
063     * @param tooltip The action tooltip
064     * @param subTab The preferences subtab to select
065     * @return The created action
066     */
067    public static PreferencesAction forPreferenceSubTab(String name, String tooltip, Class<? extends SubPreferenceSetting> subTab) {
068        return forPreferenceSubTab(name, tooltip, subTab, "preference");
069    }
070
071    /**
072     * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given subtab, with custom icon.
073     * @param name The action name
074     * @param tooltip The action tooltip
075     * @param subTab The preferences subtab to select
076     * @param icon The action icon
077     * @return The created action
078     * @since 6969
079     */
080    public static PreferencesAction forPreferenceSubTab(String name, String tooltip, Class<? extends SubPreferenceSetting> subTab, String icon) {
081        CheckParameterUtil.ensureParameterNotNull(subTab);
082        return new PreferencesAction(name, icon, tooltip, null, subTab);
083    }
084
085    /**
086     * Create the preference action with "Preferences" as label.
087     */
088    public PreferencesAction() {
089        super(tr("Preferences..."), "preference", tr("Open a preferences dialog for global settings."),
090                Shortcut.registerShortcut("system:preferences", tr("Preferences"), KeyEvent.VK_F12, Shortcut.DIRECT), true);
091        putValue("help", ht("/Action/Preferences"));
092        this.tab = null;
093        this.subTab = null;
094    }
095
096    /**
097     * Launch the preferences dialog.
098     */
099    @Override
100    public void actionPerformed(ActionEvent e) {
101        run();
102    }
103
104    @Override
105    public void run() {
106        final PreferenceDialog p = new PreferenceDialog(Main.parent);
107        if (tab != null) {
108            p.selectPreferencesTabByClass(tab);
109        } else if (subTab != null) {
110            p.selectSubPreferencesTabByClass(subTab);
111        }
112        p.setVisible(true);
113    }
114}