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