1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.Frame;
|
---|
9 | import java.awt.GridBagConstraints;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Insets;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.util.Collections;
|
---|
17 | import java.util.HashSet;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Set;
|
---|
20 |
|
---|
21 | import javax.swing.AbstractAction;
|
---|
22 | import javax.swing.Action;
|
---|
23 | import javax.swing.Icon;
|
---|
24 | import javax.swing.JButton;
|
---|
25 | import javax.swing.JDialog;
|
---|
26 | import javax.swing.JLabel;
|
---|
27 | import javax.swing.JOptionPane;
|
---|
28 | import javax.swing.JPanel;
|
---|
29 | import javax.swing.JScrollBar;
|
---|
30 | import javax.swing.JScrollPane;
|
---|
31 | import javax.swing.KeyStroke;
|
---|
32 | import javax.swing.UIManager;
|
---|
33 | import javax.swing.WindowConstants;
|
---|
34 |
|
---|
35 | import org.openstreetmap.josm.gui.help.HelpBrowser;
|
---|
36 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
37 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
38 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
---|
39 | import org.openstreetmap.josm.gui.util.WindowOnTopListener;
|
---|
40 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
41 | import org.openstreetmap.josm.io.NetworkManager;
|
---|
42 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
43 | import org.openstreetmap.josm.tools.GBC;
|
---|
44 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
45 | import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
---|
46 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
47 | import org.openstreetmap.josm.tools.Logging;
|
---|
48 | import org.openstreetmap.josm.tools.Utils;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * General configurable dialog window.
|
---|
52 | * <p>
|
---|
53 | * If dialog is modal, you can use {@link #getValue()} to retrieve the
|
---|
54 | * button index. Note that the user can close the dialog
|
---|
55 | * by other means. This is usually equivalent to cancel action.
|
---|
56 | * <p>
|
---|
57 | * For non-modal dialogs, {@link #buttonAction(int, ActionEvent)} can be overridden.
|
---|
58 | * <p>
|
---|
59 | * There are various options, see below.
|
---|
60 | * <p>
|
---|
61 | * Note: The button indices are counted from 1 and upwards.
|
---|
62 | * So for {@link #getValue()}, {@link #setDefaultButton(int)} and
|
---|
63 | * {@link #setCancelButton} the first button has index 1.
|
---|
64 | * <p>
|
---|
65 | * Simple example:
|
---|
66 | * <pre>
|
---|
67 | * ExtendedDialog ed = new ExtendedDialog(
|
---|
68 | * MainApplication.getMainFrame(), tr("Dialog Title"),
|
---|
69 | * new String[] {tr("Ok"), tr("Cancel")});
|
---|
70 | * ed.setButtonIcons(new String[] {"ok", "cancel"}); // optional
|
---|
71 | * ed.setIcon(JOptionPane.WARNING_MESSAGE); // optional
|
---|
72 | * ed.setContent(tr("Really proceed? Interesting things may happen..."));
|
---|
73 | * ed.showDialog();
|
---|
74 | * if (ed.getValue() == 1) { // user clicked first button "Ok"
|
---|
75 | * // proceed...
|
---|
76 | * }
|
---|
77 | * </pre>
|
---|
78 | */
|
---|
79 | public class ExtendedDialog extends JDialog implements IExtendedDialog {
|
---|
80 | private final boolean disposeOnClose;
|
---|
81 | private volatile int result;
|
---|
82 | public static final int DialogClosedOtherwise = 0;
|
---|
83 | private boolean toggleable;
|
---|
84 | private String rememberSizePref = "";
|
---|
85 | private transient WindowGeometry defaultWindowGeometry;
|
---|
86 | private String togglePref = "";
|
---|
87 | private int toggleValue = -1;
|
---|
88 | private ConditionalOptionPaneUtil.MessagePanel togglePanel;
|
---|
89 | private final Component parentComponent;
|
---|
90 | private Component content;
|
---|
91 | private final String[] bTexts;
|
---|
92 | private String[] bToolTipTexts;
|
---|
93 | private transient Icon[] bIcons;
|
---|
94 | private Set<Integer> cancelButtonIdx = Collections.emptySet();
|
---|
95 | private int defaultButtonIdx = 1;
|
---|
96 | protected JButton defaultButton;
|
---|
97 | private transient Icon icon;
|
---|
98 | private final boolean isModal;
|
---|
99 | private boolean focusOnDefaultButton;
|
---|
100 |
|
---|
101 | /** true, if the dialog should include a help button */
|
---|
102 | private boolean showHelpButton;
|
---|
103 | /** the help topic */
|
---|
104 | private String helpTopic;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * set to true if the content of the extended dialog should
|
---|
108 | * be placed in a {@link JScrollPane}
|
---|
109 | */
|
---|
110 | private boolean placeContentInScrollPane;
|
---|
111 |
|
---|
112 | // For easy access when inherited
|
---|
113 | protected transient Insets contentInsets = new Insets(10, 5, 0, 5);
|
---|
114 | protected transient List<JButton> buttons = new ArrayList<>();
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * This method sets up the most basic options for the dialog. Add more
|
---|
118 | * advanced features with dedicated methods.
|
---|
119 | * Possible features:
|
---|
120 | * <ul>
|
---|
121 | * <li><code>setButtonIcons</code></li>
|
---|
122 | * <li><code>setContent</code></li>
|
---|
123 | * <li><code>toggleEnable</code></li>
|
---|
124 | * <li><code>toggleDisable</code></li>
|
---|
125 | * <li><code>setToggleCheckboxText</code></li>
|
---|
126 | * <li><code>setRememberWindowGeometry</code></li>
|
---|
127 | * </ul>
|
---|
128 | *
|
---|
129 | * When done, call <code>showDialog</code> to display it. You can receive
|
---|
130 | * the user's choice using <code>getValue</code>. Have a look at this function
|
---|
131 | * for possible return values.
|
---|
132 | *
|
---|
133 | * @param parent The parent element that will be used for position and maximum size
|
---|
134 | * @param title The text that will be shown in the window titlebar
|
---|
135 | * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
|
---|
136 | */
|
---|
137 | public ExtendedDialog(Component parent, String title, String... buttonTexts) {
|
---|
138 | this(parent, title, buttonTexts, true, true);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Same as above but lets you define if the dialog should be modal.
|
---|
143 | * @param parent The parent element that will be used for position and maximum size
|
---|
144 | * @param title The text that will be shown in the window titlebar
|
---|
145 | * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
|
---|
146 | * @param modal Set it to {@code true} if you want the dialog to be modal
|
---|
147 | */
|
---|
148 | public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal) {
|
---|
149 | this(parent, title, buttonTexts, modal, true);
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Same as above but lets you define if the dialog should be disposed on close.
|
---|
154 | * @param parent The parent element that will be used for position and maximum size
|
---|
155 | * @param title The text that will be shown in the window titlebar
|
---|
156 | * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
|
---|
157 | * @param modal Set it to {@code true} if you want the dialog to be modal
|
---|
158 | * @param disposeOnClose whether to call {@link #dispose} when closing the dialog
|
---|
159 | */
|
---|
160 | public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal, boolean disposeOnClose) {
|
---|
161 | super(searchRealParent(parent), title, modal ? ModalityType.DOCUMENT_MODAL : ModalityType.MODELESS);
|
---|
162 | this.parentComponent = parent;
|
---|
163 | this.isModal = modal;
|
---|
164 | bTexts = Utils.copyArray(buttonTexts);
|
---|
165 | if (disposeOnClose) {
|
---|
166 | setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
---|
167 | }
|
---|
168 | this.disposeOnClose = disposeOnClose;
|
---|
169 | }
|
---|
170 |
|
---|
171 | private static Frame searchRealParent(Component parent) {
|
---|
172 | if (parent == null) {
|
---|
173 | return null;
|
---|
174 | } else {
|
---|
175 | return GuiHelper.getFrameForComponent(parent);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | @Override
|
---|
180 | public ExtendedDialog setButtonIcons(Icon... buttonIcons) {
|
---|
181 | this.bIcons = Utils.copyArray(buttonIcons);
|
---|
182 | return this;
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Override
|
---|
186 | public ExtendedDialog setButtonIcons(String... buttonIcons) {
|
---|
187 | bIcons = new Icon[buttonIcons.length];
|
---|
188 | for (int i = 0; i < buttonIcons.length; ++i) {
|
---|
189 | bIcons[i] = ImageProvider.get(buttonIcons[i], ImageSizes.LARGEICON);
|
---|
190 | }
|
---|
191 | return this;
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Override
|
---|
195 | public ExtendedDialog setToolTipTexts(String... toolTipTexts) {
|
---|
196 | this.bToolTipTexts = Utils.copyArray(toolTipTexts);
|
---|
197 | return this;
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public ExtendedDialog setContent(Component content) {
|
---|
202 | return setContent(content, true);
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public ExtendedDialog setContent(Component content, boolean placeContentInScrollPane) {
|
---|
207 | this.content = content;
|
---|
208 | this.placeContentInScrollPane = placeContentInScrollPane;
|
---|
209 | return this;
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | public ExtendedDialog setContent(String message) {
|
---|
214 | return setContent(string2label(message), false);
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Override
|
---|
218 | public ExtendedDialog setIcon(Icon icon) {
|
---|
219 | this.icon = icon;
|
---|
220 | return this;
|
---|
221 | }
|
---|
222 |
|
---|
223 | @Override
|
---|
224 | public ExtendedDialog setIcon(int messageType) {
|
---|
225 | switch (messageType) {
|
---|
226 | case JOptionPane.ERROR_MESSAGE:
|
---|
227 | return setIcon(UIManager.getIcon("OptionPane.errorIcon"));
|
---|
228 | case JOptionPane.INFORMATION_MESSAGE:
|
---|
229 | return setIcon(UIManager.getIcon("OptionPane.informationIcon"));
|
---|
230 | case JOptionPane.WARNING_MESSAGE:
|
---|
231 | return setIcon(UIManager.getIcon("OptionPane.warningIcon"));
|
---|
232 | case JOptionPane.QUESTION_MESSAGE:
|
---|
233 | return setIcon(UIManager.getIcon("OptionPane.questionIcon"));
|
---|
234 | case JOptionPane.PLAIN_MESSAGE:
|
---|
235 | return setIcon(null);
|
---|
236 | default:
|
---|
237 | throw new IllegalArgumentException("Unknown message type!");
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | @Override
|
---|
242 | public ExtendedDialog showDialog() {
|
---|
243 | // Check if the user has set the dialog to not be shown again
|
---|
244 | if (toggleCheckState()) {
|
---|
245 | result = toggleValue;
|
---|
246 | return this;
|
---|
247 | }
|
---|
248 |
|
---|
249 | setupDialog();
|
---|
250 | if (defaultButton != null) {
|
---|
251 | getRootPane().setDefaultButton(defaultButton);
|
---|
252 | }
|
---|
253 | // Don't focus the "do not show this again" check box, but the default button.
|
---|
254 | if (toggleable || focusOnDefaultButton) {
|
---|
255 | requestFocusToDefaultButton();
|
---|
256 | }
|
---|
257 | if (MainApplication.getMainFrame() != null) {
|
---|
258 | applyComponentOrientation(MainApplication.getMainFrame().getComponentOrientation());
|
---|
259 | }
|
---|
260 | setVisible(true);
|
---|
261 | toggleSaveState();
|
---|
262 | return this;
|
---|
263 | }
|
---|
264 |
|
---|
265 | @Override
|
---|
266 | public int getValue() {
|
---|
267 | return result;
|
---|
268 | }
|
---|
269 |
|
---|
270 | private boolean setupDone;
|
---|
271 |
|
---|
272 | @Override
|
---|
273 | public void setupDialog() {
|
---|
274 | if (setupDone)
|
---|
275 | return;
|
---|
276 | setupDone = true;
|
---|
277 |
|
---|
278 | setupEscListener();
|
---|
279 |
|
---|
280 | JButton button;
|
---|
281 | JPanel buttonsPanel = new JPanel(new GridBagLayout());
|
---|
282 |
|
---|
283 | for (int i = 0; i < bTexts.length; i++) {
|
---|
284 | button = new JButton(createButtonAction(i));
|
---|
285 | if (i == defaultButtonIdx-1) {
|
---|
286 | defaultButton = button;
|
---|
287 | }
|
---|
288 | if (bIcons != null && bIcons[i] != null) {
|
---|
289 | button.setIcon(bIcons[i]);
|
---|
290 | }
|
---|
291 | if (bToolTipTexts != null && i < bToolTipTexts.length && bToolTipTexts[i] != null) {
|
---|
292 | button.setToolTipText(bToolTipTexts[i]);
|
---|
293 | }
|
---|
294 |
|
---|
295 | buttonsPanel.add(button, GBC.std().insets(2, 2, 2, 2));
|
---|
296 | buttons.add(button);
|
---|
297 | }
|
---|
298 | if (showHelpButton) {
|
---|
299 | buttonsPanel.add(new JButton(new HelpAction()), GBC.std().insets(2, 2, 2, 2));
|
---|
300 | HelpUtil.setHelpContext(getRootPane(), helpTopic);
|
---|
301 | }
|
---|
302 |
|
---|
303 | JPanel cp = new JPanel(new GridBagLayout());
|
---|
304 |
|
---|
305 | GridBagConstraints gc = new GridBagConstraints();
|
---|
306 | gc.gridx = 0;
|
---|
307 | int y = 0;
|
---|
308 | gc.gridy = y++;
|
---|
309 | gc.weightx = 0.0;
|
---|
310 | gc.weighty = 0.0;
|
---|
311 |
|
---|
312 | if (icon != null) {
|
---|
313 | JLabel iconLbl = new JLabel(icon);
|
---|
314 | gc.insets = new Insets(10, 10, 10, 10);
|
---|
315 | gc.anchor = GridBagConstraints.NORTH;
|
---|
316 | gc.weighty = 1.0;
|
---|
317 | cp.add(iconLbl, gc);
|
---|
318 | gc.anchor = GridBagConstraints.CENTER;
|
---|
319 | gc.gridx = 1;
|
---|
320 | }
|
---|
321 |
|
---|
322 | gc.fill = GridBagConstraints.BOTH;
|
---|
323 | gc.insets = contentInsets;
|
---|
324 | gc.weightx = 1.0;
|
---|
325 | gc.weighty = 1.0;
|
---|
326 | cp.add(content, gc);
|
---|
327 |
|
---|
328 | gc.fill = GridBagConstraints.NONE;
|
---|
329 | gc.gridwidth = GridBagConstraints.REMAINDER;
|
---|
330 | gc.weightx = 0.0;
|
---|
331 | gc.weighty = 0.0;
|
---|
332 |
|
---|
333 | if (toggleable) {
|
---|
334 | togglePanel = new ConditionalOptionPaneUtil.MessagePanel(null, ConditionalOptionPaneUtil.isInBulkOperation(togglePref));
|
---|
335 | gc.gridx = icon != null ? 1 : 0;
|
---|
336 | gc.gridy = y++;
|
---|
337 | gc.anchor = GridBagConstraints.LINE_START;
|
---|
338 | gc.insets = new Insets(5, contentInsets.left, 5, contentInsets.right);
|
---|
339 | cp.add(togglePanel, gc);
|
---|
340 | }
|
---|
341 |
|
---|
342 | gc.gridy = y;
|
---|
343 | gc.anchor = GridBagConstraints.CENTER;
|
---|
344 | gc.insets = new Insets(5, 5, 5, 5);
|
---|
345 | cp.add(buttonsPanel, gc);
|
---|
346 | if (placeContentInScrollPane) {
|
---|
347 | JScrollPane pane = new JScrollPane(cp);
|
---|
348 | GuiHelper.setDefaultIncrement(pane);
|
---|
349 | pane.setBorder(null);
|
---|
350 | setContentPane(pane);
|
---|
351 | } else {
|
---|
352 | setContentPane(cp);
|
---|
353 | }
|
---|
354 | pack();
|
---|
355 |
|
---|
356 | // Try to make it not larger than the parent window or at least not larger than 2/3 of the screen
|
---|
357 | Dimension d = getSize();
|
---|
358 | Dimension x = findMaxDialogSize();
|
---|
359 |
|
---|
360 | boolean limitedInWidth = d.width > x.width;
|
---|
361 | boolean limitedInHeight = d.height > x.height;
|
---|
362 |
|
---|
363 | if (x.width > 0 && d.width > x.width) {
|
---|
364 | d.width = x.width;
|
---|
365 | }
|
---|
366 | if (x.height > 0 && d.height > x.height) {
|
---|
367 | d.height = x.height;
|
---|
368 | }
|
---|
369 |
|
---|
370 | // We have a vertical scrollbar and enough space to prevent a horizontal one
|
---|
371 | if (!limitedInWidth && limitedInHeight) {
|
---|
372 | d.width += new JScrollBar().getPreferredSize().width;
|
---|
373 | }
|
---|
374 |
|
---|
375 | setSize(d);
|
---|
376 | setLocationRelativeTo(parentComponent);
|
---|
377 | }
|
---|
378 |
|
---|
379 | protected Action createButtonAction(final int i) {
|
---|
380 | return new AbstractAction(bTexts[i]) {
|
---|
381 | @Override
|
---|
382 | public void actionPerformed(ActionEvent evt) {
|
---|
383 | buttonAction(i, evt);
|
---|
384 | }
|
---|
385 | };
|
---|
386 | }
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * This gets performed whenever a button is clicked or activated
|
---|
390 | * @param buttonIndex the button index (first index is 0)
|
---|
391 | * @param evt the button event
|
---|
392 | */
|
---|
393 | protected void buttonAction(int buttonIndex, ActionEvent evt) {
|
---|
394 | result = buttonIndex+1;
|
---|
395 | setVisible(false);
|
---|
396 | }
|
---|
397 |
|
---|
398 | /**
|
---|
399 | * Tries to find a good value of how large the dialog should be
|
---|
400 | * @return Dimension Size of the parent component if visible or 2/3 of screen size if not available or hidden
|
---|
401 | */
|
---|
402 | protected Dimension findMaxDialogSize() {
|
---|
403 | Dimension screenSize = GuiHelper.getScreenSize();
|
---|
404 | Dimension x = new Dimension(screenSize.width*2/3, screenSize.height*2/3);
|
---|
405 | if (parentComponent != null && parentComponent.isVisible()) {
|
---|
406 | x = GuiHelper.getFrameForComponent(parentComponent).getSize();
|
---|
407 | }
|
---|
408 | return x;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Makes the dialog listen to ESC keypressed
|
---|
413 | */
|
---|
414 | private void setupEscListener() {
|
---|
415 | Action actionListener = new AbstractAction() {
|
---|
416 | @Override
|
---|
417 | public void actionPerformed(ActionEvent actionEvent) {
|
---|
418 | // 0 means that the dialog has been closed otherwise.
|
---|
419 | // We need to set it to zero again, in case the dialog has been re-used
|
---|
420 | // and the result differs from its default value
|
---|
421 | result = ExtendedDialog.DialogClosedOtherwise;
|
---|
422 | if (Logging.isDebugEnabled()) {
|
---|
423 | Logging.debug("{0} ESC action performed ({1}) from {2}",
|
---|
424 | getClass().getName(), actionEvent, new Exception().getStackTrace()[1]);
|
---|
425 | }
|
---|
426 | setVisible(false);
|
---|
427 | }
|
---|
428 | };
|
---|
429 |
|
---|
430 | InputMapUtils.addEscapeAction(getRootPane(), actionListener);
|
---|
431 | }
|
---|
432 |
|
---|
433 | protected final void rememberWindowGeometry(WindowGeometry geometry) {
|
---|
434 | if (geometry != null) {
|
---|
435 | geometry.remember(rememberSizePref);
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | protected final WindowGeometry initWindowGeometry() {
|
---|
440 | return new WindowGeometry(rememberSizePref, defaultWindowGeometry);
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Override setVisible to be able to save the window geometry if required
|
---|
445 | */
|
---|
446 | @Override
|
---|
447 | public void setVisible(boolean visible) {
|
---|
448 | if (visible) {
|
---|
449 | repaint();
|
---|
450 | }
|
---|
451 |
|
---|
452 | if (Logging.isDebugEnabled()) {
|
---|
453 | Logging.debug(getClass().getName()+".setVisible("+visible+") from "+new Exception().getStackTrace()[1]);
|
---|
454 | }
|
---|
455 |
|
---|
456 | // Ensure all required variables are available
|
---|
457 | if (!rememberSizePref.isEmpty() && defaultWindowGeometry != null) {
|
---|
458 | if (visible) {
|
---|
459 | initWindowGeometry().applySafe(this);
|
---|
460 | } else if (isShowing()) { // should fix #6438, #6981, #8295
|
---|
461 | rememberWindowGeometry(new WindowGeometry(this));
|
---|
462 | }
|
---|
463 | }
|
---|
464 | if (visible && isModal()) {
|
---|
465 | this.setAlwaysOnTop(true);
|
---|
466 | this.addWindowFocusListener(new WindowOnTopListener());
|
---|
467 | }
|
---|
468 | super.setVisible(visible);
|
---|
469 |
|
---|
470 | if (!visible && disposeOnClose) {
|
---|
471 | dispose();
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | @Override
|
---|
476 | public ExtendedDialog setRememberWindowGeometry(String pref, WindowGeometry wg) {
|
---|
477 | rememberSizePref = pref == null ? "" : pref;
|
---|
478 | defaultWindowGeometry = wg;
|
---|
479 | return this;
|
---|
480 | }
|
---|
481 |
|
---|
482 | @Override
|
---|
483 | public ExtendedDialog toggleEnable(String togglePref) {
|
---|
484 | if (!isModal) {
|
---|
485 | throw new IllegalStateException();
|
---|
486 | }
|
---|
487 | this.toggleable = true;
|
---|
488 | this.togglePref = togglePref;
|
---|
489 | return this;
|
---|
490 | }
|
---|
491 |
|
---|
492 | @Override
|
---|
493 | public ExtendedDialog setDefaultButton(int defaultButtonIdx) {
|
---|
494 | this.defaultButtonIdx = defaultButtonIdx;
|
---|
495 | return this;
|
---|
496 | }
|
---|
497 |
|
---|
498 | @Override
|
---|
499 | public ExtendedDialog setCancelButton(Integer... cancelButtonIdx) {
|
---|
500 | this.cancelButtonIdx = new HashSet<>(Arrays.asList(cancelButtonIdx));
|
---|
501 | return this;
|
---|
502 | }
|
---|
503 |
|
---|
504 | @Override
|
---|
505 | public void setFocusOnDefaultButton(boolean focus) {
|
---|
506 | focusOnDefaultButton = focus;
|
---|
507 | }
|
---|
508 |
|
---|
509 | private void requestFocusToDefaultButton() {
|
---|
510 | if (defaultButton != null) {
|
---|
511 | GuiHelper.runInEDT(defaultButton::requestFocusInWindow);
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | @Override
|
---|
516 | public final boolean toggleCheckState() {
|
---|
517 | toggleable = !Utils.isEmpty(togglePref);
|
---|
518 | toggleValue = ConditionalOptionPaneUtil.getDialogReturnValue(togglePref);
|
---|
519 | return toggleable && toggleValue != -1;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /**
|
---|
523 | * This function checks the state of the "Do not show again" checkbox and
|
---|
524 | * writes the corresponding pref.
|
---|
525 | */
|
---|
526 | protected void toggleSaveState() {
|
---|
527 | if (!toggleable ||
|
---|
528 | togglePanel == null ||
|
---|
529 | cancelButtonIdx.contains(result) ||
|
---|
530 | result == ExtendedDialog.DialogClosedOtherwise)
|
---|
531 | return;
|
---|
532 | togglePanel.getNotShowAgain().store(togglePref, result);
|
---|
533 | }
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * Convenience function that converts a given string into a JMultilineLabel
|
---|
537 | * @param msg the message to display
|
---|
538 | * @return JMultilineLabel displaying {@code msg}
|
---|
539 | */
|
---|
540 | private static JMultilineLabel string2label(String msg) {
|
---|
541 | JMultilineLabel lbl = new JMultilineLabel(msg);
|
---|
542 | // Make it not wider than 1/2 of the screen
|
---|
543 | Dimension screenSize = GuiHelper.getScreenSize();
|
---|
544 | lbl.setMaxWidth(screenSize.width/2);
|
---|
545 | // Disable default Enter key binding to allow dialog's one (then enables to hit default button from here)
|
---|
546 | lbl.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), new Object());
|
---|
547 | return lbl;
|
---|
548 | }
|
---|
549 |
|
---|
550 | @Override
|
---|
551 | public ExtendedDialog configureContextsensitiveHelp(String helpTopic, boolean showHelpButton) {
|
---|
552 | this.helpTopic = helpTopic;
|
---|
553 | this.showHelpButton = showHelpButton;
|
---|
554 | return this;
|
---|
555 | }
|
---|
556 |
|
---|
557 | class HelpAction extends AbstractAction {
|
---|
558 | /**
|
---|
559 | * Constructs a new {@code HelpAction}.
|
---|
560 | */
|
---|
561 | HelpAction() {
|
---|
562 | putValue(SHORT_DESCRIPTION, tr("Show help information"));
|
---|
563 | putValue(NAME, tr("Help"));
|
---|
564 | new ImageProvider("help").getResource().attachImageIcon(this, true);
|
---|
565 | setEnabled(!NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
566 | }
|
---|
567 |
|
---|
568 | @Override
|
---|
569 | public void actionPerformed(ActionEvent e) {
|
---|
570 | HelpBrowser.setUrlForHelpTopic(helpTopic);
|
---|
571 | }
|
---|
572 | }
|
---|
573 | }
|
---|