| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.KeyEvent;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.Main;
|
|---|
| 11 | import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
|
|---|
| 12 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
|---|
| 13 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
|---|
| 14 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 15 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 16 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Open the Preferences dialog.
|
|---|
| 20 | *
|
|---|
| 21 | * @author imi
|
|---|
| 22 | */
|
|---|
| 23 | public class PreferencesAction extends JosmAction implements Runnable {
|
|---|
| 24 |
|
|---|
| 25 | private final Class<? extends TabPreferenceSetting> tab;
|
|---|
| 26 | private final Class<? extends SubPreferenceSetting> subTab;
|
|---|
| 27 |
|
|---|
| 28 | private PreferencesAction(String name, String icon, String tooltip,
|
|---|
| 29 | Class<? extends TabPreferenceSetting> tab, Class<? extends SubPreferenceSetting> subTab) {
|
|---|
| 30 | super(name, icon, tooltip, null, false, "preference_" + Utils.<Class<?>>firstNonNull(tab, subTab).getName(), false);
|
|---|
| 31 | this.tab = tab;
|
|---|
| 32 | this.subTab = subTab;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given tab, with default icon.
|
|---|
| 37 | * @param name The action name
|
|---|
| 38 | * @param tooltip The action tooltip
|
|---|
| 39 | * @param tab The preferences tab to select
|
|---|
| 40 | * @return The created action
|
|---|
| 41 | */
|
|---|
| 42 | public static PreferencesAction forPreferenceTab(String name, String tooltip, Class<? extends TabPreferenceSetting> tab) {
|
|---|
| 43 | return forPreferenceTab(name, tooltip, tab, "preference");
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given tab, with custom icon.
|
|---|
| 48 | * @param name The action name
|
|---|
| 49 | * @param tooltip The action tooltip
|
|---|
| 50 | * @param tab The preferences tab to select
|
|---|
| 51 | * @param icon The action icon
|
|---|
| 52 | * @return The created action
|
|---|
| 53 | * @since 6969
|
|---|
| 54 | */
|
|---|
| 55 | public static PreferencesAction forPreferenceTab(String name, String tooltip, Class<? extends TabPreferenceSetting> tab, String icon) {
|
|---|
| 56 | CheckParameterUtil.ensureParameterNotNull(tab);
|
|---|
| 57 | return new PreferencesAction(name, icon, tooltip, tab, null);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given subtab, with default icon.
|
|---|
| 62 | * @param name The action name
|
|---|
| 63 | * @param tooltip The action tooltip
|
|---|
| 64 | * @param subTab The preferences subtab to select
|
|---|
| 65 | * @return The created action
|
|---|
| 66 | */
|
|---|
| 67 | public static PreferencesAction forPreferenceSubTab(String name, String tooltip, Class<? extends SubPreferenceSetting> subTab) {
|
|---|
| 68 | return forPreferenceSubTab(name, tooltip, subTab, "preference");
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given subtab, with custom icon.
|
|---|
| 73 | * @param name The action name
|
|---|
| 74 | * @param tooltip The action tooltip
|
|---|
| 75 | * @param subTab The preferences subtab to select
|
|---|
| 76 | * @param icon The action icon
|
|---|
| 77 | * @return The created action
|
|---|
| 78 | * @since 6969
|
|---|
| 79 | */
|
|---|
| 80 | public static PreferencesAction forPreferenceSubTab(String name, String tooltip, Class<? extends SubPreferenceSetting> subTab, String icon) {
|
|---|
| 81 | CheckParameterUtil.ensureParameterNotNull(subTab);
|
|---|
| 82 | return new PreferencesAction(name, icon, tooltip, null, subTab);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Create the preference action with "Preferences" as label.
|
|---|
| 87 | */
|
|---|
| 88 | public PreferencesAction() {
|
|---|
| 89 | super(tr("Preferences..."), "preference", tr("Open a preferences dialog for global settings."),
|
|---|
| 90 | Shortcut.registerShortcut("system:preferences", tr("Preferences"), KeyEvent.VK_F12, Shortcut.DIRECT), true);
|
|---|
| 91 | putValue("help", ht("/Action/Preferences"));
|
|---|
| 92 | this.tab = null;
|
|---|
| 93 | this.subTab = null;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * Launch the preferences dialog.
|
|---|
| 98 | */
|
|---|
| 99 | @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 | }
|
|---|