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.Dialog.ModalityType;
|
---|
8 | import java.awt.GraphicsEnvironment;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.WindowAdapter;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import javax.swing.AbstractAction;
|
---|
18 | import javax.swing.Icon;
|
---|
19 | import javax.swing.JButton;
|
---|
20 | import javax.swing.JDialog;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.event.ChangeEvent;
|
---|
23 | import javax.swing.event.ChangeListener;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
26 | import org.openstreetmap.josm.gui.help.HelpBrowser;
|
---|
27 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
28 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
29 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
32 | import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
---|
33 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
34 | import org.openstreetmap.josm.tools.Logging;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Utility methods that display an option dialog with an additional help button that links to the JOSM help
|
---|
38 | */
|
---|
39 | public final class HelpAwareOptionPane {
|
---|
40 |
|
---|
41 | private HelpAwareOptionPane() {
|
---|
42 | // Hide default constructor for utils classes
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * A specification of a button that should be added to the options dialog
|
---|
47 | */
|
---|
48 | public static class ButtonSpec {
|
---|
49 | /**
|
---|
50 | * the button text
|
---|
51 | */
|
---|
52 | public final String text;
|
---|
53 | /**
|
---|
54 | * the icon to display. Can be <code>null</code>
|
---|
55 | */
|
---|
56 | public final Icon icon;
|
---|
57 | /**
|
---|
58 | * The tooltip to display when hovering the button
|
---|
59 | */
|
---|
60 | public final String tooltipText;
|
---|
61 | /**
|
---|
62 | * The help topic to link
|
---|
63 | */
|
---|
64 | public final String helpTopic;
|
---|
65 | private boolean enabled;
|
---|
66 |
|
---|
67 | private final Collection<ChangeListener> listeners = new HashSet<>();
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Constructs a new {@code ButtonSpec}.
|
---|
71 | * @param text the button text
|
---|
72 | * @param imageProvider provides the icon to display. Can be null
|
---|
73 | * @param tooltipText the tooltip text. Can be null.
|
---|
74 | * @param helpTopic the help topic. Can be null.
|
---|
75 | * @since 13842
|
---|
76 | */
|
---|
77 | public ButtonSpec(String text, ImageProvider imageProvider, String tooltipText, String helpTopic) {
|
---|
78 | this(text, imageProvider != null ? imageProvider.setSize(ImageSizes.LARGEICON).get() : null, tooltipText, helpTopic, true);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Constructs a new {@code ButtonSpec}.
|
---|
83 | * @param text the button text
|
---|
84 | * @param icon the icon to display. Can be null
|
---|
85 | * @param tooltipText the tooltip text. Can be null.
|
---|
86 | * @param helpTopic the help topic. Can be null.
|
---|
87 | */
|
---|
88 | public ButtonSpec(String text, Icon icon, String tooltipText, String helpTopic) {
|
---|
89 | this(text, icon, tooltipText, helpTopic, true);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Constructs a new {@code ButtonSpec}.
|
---|
94 | * @param text the button text
|
---|
95 | * @param icon the icon to display. Can be null
|
---|
96 | * @param tooltipText the tooltip text. Can be null.
|
---|
97 | * @param helpTopic the help topic. Can be null.
|
---|
98 | * @param enabled the enabled status
|
---|
99 | * @since 5951
|
---|
100 | */
|
---|
101 | public ButtonSpec(String text, Icon icon, String tooltipText, String helpTopic, boolean enabled) {
|
---|
102 | this.text = text;
|
---|
103 | this.icon = icon;
|
---|
104 | this.tooltipText = tooltipText;
|
---|
105 | this.helpTopic = helpTopic;
|
---|
106 | setEnabled(enabled);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Determines if this button spec is enabled
|
---|
111 | * @return {@code true} if this button spec is enabled, {@code false} otherwise
|
---|
112 | * @since 6051
|
---|
113 | */
|
---|
114 | public final boolean isEnabled() {
|
---|
115 | return enabled;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Enables or disables this button spec, depending on the value of the parameter {@code b}.
|
---|
120 | * @param enabled if {@code true}, this button spec is enabled; otherwise this button spec is disabled
|
---|
121 | * @since 6051
|
---|
122 | */
|
---|
123 | public final void setEnabled(boolean enabled) {
|
---|
124 | if (this.enabled != enabled) {
|
---|
125 | this.enabled = enabled;
|
---|
126 | ChangeEvent event = new ChangeEvent(this);
|
---|
127 | for (ChangeListener listener : listeners) {
|
---|
128 | listener.stateChanged(event);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | private boolean addChangeListener(ChangeListener listener) {
|
---|
134 | return listener != null && listeners.add(listener);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static class DefaultAction extends AbstractAction {
|
---|
139 | private final JDialog dialog;
|
---|
140 | private final JOptionPane pane;
|
---|
141 | private final int value;
|
---|
142 |
|
---|
143 | DefaultAction(JDialog dialog, JOptionPane pane, int value) {
|
---|
144 | this.dialog = dialog;
|
---|
145 | this.pane = pane;
|
---|
146 | this.value = value;
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void actionPerformed(ActionEvent e) {
|
---|
151 | pane.setValue(value);
|
---|
152 | dialog.setVisible(false);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Creates the list buttons to be displayed in the option pane dialog.
|
---|
158 | *
|
---|
159 | * @param options the option. If null, just creates an OK button and a help button
|
---|
160 | * @param helpTopic the help topic. The context sensitive help of all buttons is equal
|
---|
161 | * to the context sensitive help of the whole dialog
|
---|
162 | * @return the list of buttons
|
---|
163 | */
|
---|
164 | private static List<JButton> createOptionButtons(ButtonSpec[] options, String helpTopic) {
|
---|
165 | List<JButton> buttons = new ArrayList<>();
|
---|
166 | if (options == null) {
|
---|
167 | JButton b = new JButton(tr("OK"));
|
---|
168 | b.setIcon(ImageProvider.get("ok"));
|
---|
169 | b.setToolTipText(tr("Click to close the dialog"));
|
---|
170 | b.setFocusable(true);
|
---|
171 | buttons.add(b);
|
---|
172 | } else {
|
---|
173 | for (final ButtonSpec spec: options) {
|
---|
174 | final JButton b = new JButton(spec.text);
|
---|
175 | b.setIcon(spec.icon);
|
---|
176 | b.setToolTipText(spec.tooltipText == null ? "" : spec.tooltipText);
|
---|
177 | if (helpTopic != null) {
|
---|
178 | HelpUtil.setHelpContext(b, helpTopic);
|
---|
179 | }
|
---|
180 | b.setFocusable(true);
|
---|
181 | b.setEnabled(spec.isEnabled());
|
---|
182 | spec.addChangeListener(e -> b.setEnabled(spec.isEnabled()));
|
---|
183 | buttons.add(b);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return buttons;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Creates the help button
|
---|
191 | *
|
---|
192 | * @param helpTopic the help topic
|
---|
193 | * @return the help button
|
---|
194 | */
|
---|
195 | private static JButton createHelpButton(String helpTopic) {
|
---|
196 | JButton b = new JButton(new HelpAction(helpTopic));
|
---|
197 | HelpUtil.setHelpContext(b, helpTopic);
|
---|
198 | InputMapUtils.enableEnter(b);
|
---|
199 | return b;
|
---|
200 | }
|
---|
201 |
|
---|
202 | private static class HelpAction extends JosmAction {
|
---|
203 | private final String helpTopic;
|
---|
204 |
|
---|
205 | HelpAction(String helpTopic) {
|
---|
206 | super(tr("Help"), "help", tr("Show help information"), null, false, false);
|
---|
207 | this.helpTopic = helpTopic;
|
---|
208 | }
|
---|
209 |
|
---|
210 | @Override
|
---|
211 | public void actionPerformed(ActionEvent e) {
|
---|
212 | HelpBrowser.setUrlForHelpTopic(helpTopic);
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Displays an option dialog which is aware of a help context. If <code>helpTopic</code> isn't null,
|
---|
218 | * the dialog includes a "Help" button and launches the help browser if the user presses F1. If the
|
---|
219 | * user clicks on the "Help" button the option dialog remains open and JOSM launches the help
|
---|
220 | * browser.
|
---|
221 | *
|
---|
222 | * <code>helpTopic</code> is the trailing part of a JOSM online help URL, i.e. the part after the leading
|
---|
223 | * <code>https://josm.openstreetmap.de/wiki/Help</code>. It should start with a leading '/' and it
|
---|
224 | * may include an anchor after a '#'.
|
---|
225 | *
|
---|
226 | * <strong>Examples</strong>
|
---|
227 | * <ul>
|
---|
228 | * <li>/Dialogs/RelationEditor</li>
|
---|
229 | * <li>/Dialogs/RelationEditor#ConflictInData</li>
|
---|
230 | * </ul>
|
---|
231 | *
|
---|
232 | * In addition, the option buttons display JOSM icons, similar to ExtendedDialog.
|
---|
233 | *
|
---|
234 | * @param parentComponent the parent component
|
---|
235 | * @param msg the message
|
---|
236 | * @param title the title
|
---|
237 | * @param messageType the message type (see {@link JOptionPane})
|
---|
238 | * @param icon the icon to display. Can be null.
|
---|
239 | * @param options the list of options to display. Can be null.
|
---|
240 | * @param defaultOption the default option. Can be null.
|
---|
241 | * @param helpTopic the help topic. Can be null.
|
---|
242 | * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
|
---|
243 | */
|
---|
244 | public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType,
|
---|
245 | Icon icon, final ButtonSpec[] options, final ButtonSpec defaultOption, final String helpTopic) {
|
---|
246 | final List<JButton> buttons = createOptionButtons(options, helpTopic);
|
---|
247 | if (helpTopic != null) {
|
---|
248 | buttons.add(createHelpButton(helpTopic));
|
---|
249 | }
|
---|
250 |
|
---|
251 | JButton defaultButton = null;
|
---|
252 | if (options != null && defaultOption != null) {
|
---|
253 | for (int i = 0; i < options.length; i++) {
|
---|
254 | if (options[i] == defaultOption) {
|
---|
255 | defaultButton = buttons.get(i);
|
---|
256 | break;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | final Object content;
|
---|
261 | if (msg instanceof String) {
|
---|
262 | String msgStr = (String) msg;
|
---|
263 | content = new HtmlPanel(msgStr.startsWith("<html>") ? msgStr : "<html>" + msgStr + "</html>");
|
---|
264 | } else {
|
---|
265 | content = msg;
|
---|
266 | }
|
---|
267 | final JOptionPane pane = new JOptionPane(
|
---|
268 | content,
|
---|
269 | messageType,
|
---|
270 | JOptionPane.DEFAULT_OPTION,
|
---|
271 | icon,
|
---|
272 | buttons.toArray(),
|
---|
273 | defaultButton
|
---|
274 | );
|
---|
275 |
|
---|
276 | // Log message. Useful for bug reports and unit tests
|
---|
277 | switch (messageType) {
|
---|
278 | case JOptionPane.ERROR_MESSAGE:
|
---|
279 | Logging.error(title + " - " + msg);
|
---|
280 | break;
|
---|
281 | case JOptionPane.WARNING_MESSAGE:
|
---|
282 | Logging.warn(title + " - " + msg);
|
---|
283 | break;
|
---|
284 | default:
|
---|
285 | Logging.info(title + " - " + msg);
|
---|
286 | }
|
---|
287 |
|
---|
288 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
289 | doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane);
|
---|
290 | }
|
---|
291 | return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION;
|
---|
292 | }
|
---|
293 |
|
---|
294 | private static void doShowOptionDialog(Component parentComponent, String title, final ButtonSpec[] options,
|
---|
295 | final ButtonSpec defaultOption, final String helpTopic, final List<JButton> buttons,
|
---|
296 | final JOptionPane pane) {
|
---|
297 | final JDialog dialog = new JDialog(
|
---|
298 | GuiHelper.getFrameForComponent(parentComponent),
|
---|
299 | title,
|
---|
300 | ModalityType.DOCUMENT_MODAL
|
---|
301 | );
|
---|
302 | dialog.setContentPane(pane);
|
---|
303 | dialog.addWindowListener(new WindowAdapter() {
|
---|
304 | @Override
|
---|
305 | public void windowClosing(WindowEvent e) {
|
---|
306 | pane.setValue(JOptionPane.CLOSED_OPTION);
|
---|
307 | super.windowClosed(e);
|
---|
308 | }
|
---|
309 |
|
---|
310 | @Override
|
---|
311 | public void windowOpened(WindowEvent e) {
|
---|
312 | if (defaultOption != null && options != null && options.length > 0) {
|
---|
313 | int i;
|
---|
314 | for (i = 0; i < options.length; i++) {
|
---|
315 | if (options[i] == defaultOption) {
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | if (i >= options.length) {
|
---|
320 | buttons.get(0).requestFocusInWindow();
|
---|
321 | }
|
---|
322 | buttons.get(i).requestFocusInWindow();
|
---|
323 | } else {
|
---|
324 | buttons.get(0).requestFocusInWindow();
|
---|
325 | }
|
---|
326 | }
|
---|
327 | });
|
---|
328 | InputMapUtils.addEscapeAction(dialog.getRootPane(), new AbstractAction() {
|
---|
329 | @Override
|
---|
330 | public void actionPerformed(ActionEvent e) {
|
---|
331 | pane.setValue(JOptionPane.CLOSED_OPTION);
|
---|
332 | dialog.setVisible(false);
|
---|
333 | }
|
---|
334 | });
|
---|
335 |
|
---|
336 | if (options != null) {
|
---|
337 | for (int i = 0; i < options.length; i++) {
|
---|
338 | final DefaultAction action = new DefaultAction(dialog, pane, i);
|
---|
339 | buttons.get(i).addActionListener(action);
|
---|
340 | InputMapUtils.addEnterAction(buttons.get(i), action);
|
---|
341 | }
|
---|
342 | } else {
|
---|
343 | final DefaultAction action = new DefaultAction(dialog, pane, 0);
|
---|
344 | buttons.get(0).addActionListener(action);
|
---|
345 | InputMapUtils.addEnterAction(buttons.get(0), action);
|
---|
346 | }
|
---|
347 |
|
---|
348 | dialog.pack();
|
---|
349 | WindowGeometry.centerOnScreen(dialog.getSize()).applySafe(dialog);
|
---|
350 | if (helpTopic != null) {
|
---|
351 | HelpUtil.setHelpContext(dialog.getRootPane(), helpTopic);
|
---|
352 | }
|
---|
353 | dialog.setVisible(true);
|
---|
354 | }
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * Displays an option dialog which is aware of a help context.
|
---|
358 | *
|
---|
359 | * @param parentComponent the parent component
|
---|
360 | * @param msg the message
|
---|
361 | * @param title the title
|
---|
362 | * @param messageType the message type (see {@link JOptionPane})
|
---|
363 | * @param helpTopic the help topic. Can be null.
|
---|
364 | * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
|
---|
365 | * @see #showOptionDialog(Component, Object, String, int, Icon, ButtonSpec[], ButtonSpec, String)
|
---|
366 | */
|
---|
367 | public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, String helpTopic) {
|
---|
368 | return showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic);
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Run it in Event Dispatch Thread.
|
---|
373 | * This version does not return anything, so it is more like {@code showMessageDialog}.
|
---|
374 | *
|
---|
375 | * It can be used, when you need to show a message dialog from a worker thread,
|
---|
376 | * e.g. from {@code PleaseWaitRunnable}.
|
---|
377 | *
|
---|
378 | * @param parentComponent the parent component
|
---|
379 | * @param msg the message
|
---|
380 | * @param title the title
|
---|
381 | * @param messageType the message type (see {@link JOptionPane})
|
---|
382 | * @param helpTopic the help topic. Can be null.
|
---|
383 | */
|
---|
384 | public static void showMessageDialogInEDT(final Component parentComponent, final Object msg, final String title,
|
---|
385 | final int messageType, final String helpTopic) {
|
---|
386 | GuiHelper.runInEDT(() -> showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic));
|
---|
387 | }
|
---|
388 | }
|
---|