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