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 javax.swing.SwingUtilities; 011 012import org.openstreetmap.josm.gui.MainApplication; 013import org.openstreetmap.josm.gui.preferences.PreferenceDialog; 014import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting; 015import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting; 016import org.openstreetmap.josm.tools.CheckParameterUtil; 017import org.openstreetmap.josm.tools.Shortcut; 018import org.openstreetmap.josm.tools.Utils; 019 020/** 021 * Open the Preferences dialog. 022 * 023 * @author imi 024 */ 025public class PreferencesAction extends JosmAction implements Runnable { 026 027 private final Class<? extends TabPreferenceSetting> tab; 028 private final Class<? extends SubPreferenceSetting> subTab; 029 030 private PreferencesAction(String name, String icon, String tooltip, 031 Class<? extends TabPreferenceSetting> tab, Class<? extends SubPreferenceSetting> subTab) { 032 super(name, icon, tooltip, null, false, "preference_" + Utils.<Class<?>>firstNonNull(tab, subTab).getName(), false); 033 this.tab = tab; 034 this.subTab = subTab; 035 } 036 037 /** 038 * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given tab, with default icon. 039 * @param name The action name 040 * @param tooltip The action tooltip 041 * @param tab The preferences tab to select 042 * @return The created action 043 */ 044 public static PreferencesAction forPreferenceTab(String name, String tooltip, Class<? extends TabPreferenceSetting> tab) { 045 return forPreferenceTab(name, tooltip, tab, "preference"); 046 } 047 048 /** 049 * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given tab, with custom icon. 050 * @param name The action name 051 * @param tooltip The action tooltip 052 * @param tab The preferences tab to select 053 * @param icon The action icon 054 * @return The created action 055 * @since 6969 056 */ 057 public static PreferencesAction forPreferenceTab(String name, String tooltip, Class<? extends TabPreferenceSetting> tab, String icon) { 058 CheckParameterUtil.ensureParameterNotNull(tab); 059 return new PreferencesAction(name, icon, tooltip, tab, null); 060 } 061 062 /** 063 * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given subtab, with default icon. 064 * @param name The action name 065 * @param tooltip The action tooltip 066 * @param subTab The preferences subtab to select 067 * @return The created action 068 */ 069 public static PreferencesAction forPreferenceSubTab(String name, String tooltip, Class<? extends SubPreferenceSetting> subTab) { 070 return forPreferenceSubTab(name, tooltip, subTab, "preference"); 071 } 072 073 /** 074 * Returns a new {@code PreferenceAction} opening preferences dialog directly to the given subtab, with custom icon. 075 * @param name The action name 076 * @param tooltip The action tooltip 077 * @param subTab The preferences subtab to select 078 * @param icon The action icon 079 * @return The created action 080 * @since 6969 081 */ 082 public static PreferencesAction forPreferenceSubTab(String name, String tooltip, Class<? extends SubPreferenceSetting> subTab, String icon) { 083 CheckParameterUtil.ensureParameterNotNull(subTab); 084 return new PreferencesAction(name, icon, tooltip, null, subTab); 085 } 086 087 /** 088 * Create the preference action with "Preferences" as label. 089 */ 090 public PreferencesAction() { 091 super(tr("Preferences..."), "preference", tr("Open a preferences dialog for global settings."), 092 Shortcut.registerShortcut("system:preferences", tr("Edit: {0}", tr("Preferences")), KeyEvent.VK_F12, Shortcut.DIRECT), 093 true, false); 094 setHelpId(ht("/Action/Preferences")); 095 this.tab = null; 096 this.subTab = null; 097 } 098 099 /** 100 * Launch the preferences dialog. 101 */ 102 @Override 103 public void actionPerformed(ActionEvent e) { 104 run(); 105 } 106 107 @Override 108 public void run() { 109 final PreferenceDialog p = new PreferenceDialog(MainApplication.getMainFrame()); 110 SwingUtilities.invokeLater(() -> { 111 if (tab != null) { 112 p.selectPreferencesTabByClass(tab); 113 } else if (subTab != null) { 114 p.selectSubPreferencesTabByClass(subTab); 115 } else { 116 p.selectPreviouslySelectedPreferences(); 117 } 118 }); 119 p.setVisible(true); 120 } 121}