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