source: josm/trunk/src/org/openstreetmap/josm/gui/HelpAwareOptionPane.java

Last change on this file was 18923, checked in by taylor.smock, 4 months ago

Fix #16485: Ensure windows lose always-on-top status when JOSM loses focus

Also fix some spotbugs/sonarlint issues.

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