| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.gui.preferences; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | |
|---|
| 6 | import java.awt.BorderLayout; |
|---|
| 7 | import java.awt.Component; |
|---|
| 8 | import java.awt.Container; |
|---|
| 9 | import java.awt.Dimension; |
|---|
| 10 | import java.awt.FlowLayout; |
|---|
| 11 | import java.awt.GridBagLayout; |
|---|
| 12 | import java.awt.Insets; |
|---|
| 13 | import java.awt.Toolkit; |
|---|
| 14 | import java.awt.event.ActionEvent; |
|---|
| 15 | import java.awt.event.ActionListener; |
|---|
| 16 | import java.awt.event.KeyEvent; |
|---|
| 17 | import java.awt.event.WindowAdapter; |
|---|
| 18 | import java.awt.event.WindowEvent; |
|---|
| 19 | |
|---|
| 20 | import javax.swing.AbstractAction; |
|---|
| 21 | import javax.swing.BorderFactory; |
|---|
| 22 | import javax.swing.JCheckBox; |
|---|
| 23 | import javax.swing.JComponent; |
|---|
| 24 | import javax.swing.JDialog; |
|---|
| 25 | import javax.swing.JOptionPane; |
|---|
| 26 | import javax.swing.JPanel; |
|---|
| 27 | import javax.swing.KeyStroke; |
|---|
| 28 | |
|---|
| 29 | import org.openstreetmap.josm.actions.ExpertToggleAction; |
|---|
| 30 | import org.openstreetmap.josm.gui.SideButton; |
|---|
| 31 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction; |
|---|
| 32 | import org.openstreetmap.josm.gui.help.HelpUtil; |
|---|
| 33 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener; |
|---|
| 34 | import org.openstreetmap.josm.gui.preferences.map.MapPreference; |
|---|
| 35 | import org.openstreetmap.josm.tools.GBC; |
|---|
| 36 | import org.openstreetmap.josm.tools.ImageProvider; |
|---|
| 37 | import org.openstreetmap.josm.tools.WindowGeometry; |
|---|
| 38 | |
|---|
| 39 | public class PreferenceDialog extends JDialog { |
|---|
| 40 | |
|---|
| 41 | private PreferenceTabbedPane tpPreferences; |
|---|
| 42 | private boolean canceled; |
|---|
| 43 | |
|---|
| 44 | protected JPanel buildActionPanel() { |
|---|
| 45 | JPanel pnl = new JPanel(new GridBagLayout()); |
|---|
| 46 | |
|---|
| 47 | JCheckBox expert = new JCheckBox(tr("Expert mode")); |
|---|
| 48 | expert.setSelected(ExpertToggleAction.isExpert()); |
|---|
| 49 | expert.addActionListener(new ActionListener() { |
|---|
| 50 | public void actionPerformed(ActionEvent e) { |
|---|
| 51 | ExpertToggleAction.getInstance().actionPerformed(null); |
|---|
| 52 | } |
|---|
| 53 | }); |
|---|
| 54 | |
|---|
| 55 | JPanel btns = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 56 | btns.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); |
|---|
| 57 | btns.add(new SideButton(new OKAction())); |
|---|
| 58 | btns.add(new SideButton(new CancelAction())); |
|---|
| 59 | btns.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Action/Preferences")))); |
|---|
| 60 | pnl.add(expert, GBC.std().insets(5,0,0,0)); |
|---|
| 61 | pnl.add(btns, GBC.std().fill(GBC.HORIZONTAL)); |
|---|
| 62 | return pnl; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | protected void build() { |
|---|
| 66 | Container c = getContentPane(); |
|---|
| 67 | c.setLayout(new BorderLayout()); |
|---|
| 68 | c.add(tpPreferences = new PreferenceTabbedPane(), BorderLayout.CENTER); |
|---|
| 69 | tpPreferences.buildGui(); |
|---|
| 70 | tpPreferences.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); |
|---|
| 71 | c.add(buildActionPanel(), BorderLayout.SOUTH); |
|---|
| 72 | |
|---|
| 73 | addWindowListener(new WindowEventHandler()); |
|---|
| 74 | |
|---|
| 75 | getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); |
|---|
| 76 | getRootPane().getActionMap().put("cancel", new CancelAction()); |
|---|
| 77 | HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Action/Preferences")); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public PreferenceDialog(Component parent) { |
|---|
| 81 | super(JOptionPane.getFrameForComponent(parent), tr("Preferences"), ModalityType.DOCUMENT_MODAL); |
|---|
| 82 | build(); |
|---|
| 83 | this.setMinimumSize(new Dimension(600, 350)); |
|---|
| 84 | // set the maximum width to the current screen. If the dialog is opened on a |
|---|
| 85 | // smaller screen than before, this will reset the stored preference. |
|---|
| 86 | this.setMaximumSize( Toolkit.getDefaultToolkit().getScreenSize()); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public boolean isCanceled() { |
|---|
| 90 | return canceled; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | protected void setCanceled(boolean canceled) { |
|---|
| 94 | this.canceled = canceled; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | @Override |
|---|
| 98 | public void setVisible(boolean visible) { |
|---|
| 99 | if (visible) { |
|---|
| 100 | // Make the pref window at most as large as the parent JOSM window |
|---|
| 101 | // Have to take window decorations into account or the windows will |
|---|
| 102 | // be too large |
|---|
| 103 | Insets i = this.getParent().getInsets(); |
|---|
| 104 | Dimension p = this.getParent().getSize(); |
|---|
| 105 | p = new Dimension(Math.min(p.width-i.left-i.right, 700), |
|---|
| 106 | Math.min(p.height-i.top-i.bottom, 800)); |
|---|
| 107 | new WindowGeometry( |
|---|
| 108 | getClass().getName() + ".geometry", |
|---|
| 109 | WindowGeometry.centerInWindow( |
|---|
| 110 | getParent(), |
|---|
| 111 | p |
|---|
| 112 | ) |
|---|
| 113 | ).applySafe(this); |
|---|
| 114 | } else if (!visible && isShowing()){ |
|---|
| 115 | new WindowGeometry(this).remember(getClass().getName() + ".geometry"); |
|---|
| 116 | } |
|---|
| 117 | super.setVisible(visible); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | public void selectPreferencesTabByName(String name) { |
|---|
| 121 | tpPreferences.selectTabByName(name); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | public void selectPreferencesTabByClass(Class<? extends TabPreferenceSetting> clazz) { |
|---|
| 125 | tpPreferences.selectTabByPref(clazz); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | class CancelAction extends AbstractAction { |
|---|
| 129 | public CancelAction() { |
|---|
| 130 | putValue(NAME, tr("Cancel")); |
|---|
| 131 | putValue(SMALL_ICON, ImageProvider.get("cancel")); |
|---|
| 132 | putValue(SHORT_DESCRIPTION, tr("Close the preferences dialog and discard preference updates")); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | public void cancel() { |
|---|
| 136 | setCanceled(true); |
|---|
| 137 | setVisible(false); |
|---|
| 138 | tpPreferences.validationListeners.clear(); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | public void actionPerformed(ActionEvent evt) { |
|---|
| 142 | cancel(); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | class OKAction extends AbstractAction { |
|---|
| 147 | public OKAction() { |
|---|
| 148 | putValue(NAME, tr("OK")); |
|---|
| 149 | putValue(SMALL_ICON, ImageProvider.get("ok")); |
|---|
| 150 | putValue(SHORT_DESCRIPTION, tr("Save the preferences and close the dialog")); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | public void actionPerformed(ActionEvent evt) { |
|---|
| 154 | for (ValidationListener listener: tpPreferences.validationListeners) { |
|---|
| 155 | if (!listener.validatePreferences()) |
|---|
| 156 | return; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | tpPreferences.savePreferences(); |
|---|
| 160 | tpPreferences.validationListeners.clear(); |
|---|
| 161 | setCanceled(false); |
|---|
| 162 | setVisible(false); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | class WindowEventHandler extends WindowAdapter { |
|---|
| 167 | @Override |
|---|
| 168 | public void windowClosing(WindowEvent arg0) { |
|---|
| 169 | new CancelAction().cancel(); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | public void selectMapPaintPreferenceTab() { |
|---|
| 174 | tpPreferences.selectTabByPref(MapPreference.class); |
|---|
| 175 | tpPreferences.getMapPreference().mapcontent.setSelectedIndex(1); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|