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

Last change on this file was 18923, checked in by taylor.smock, 6 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
RevLine 
[2512]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;
[3501]7import java.awt.Dialog.ModalityType;
[9917]8import java.awt.GraphicsEnvironment;
[2512]9import java.awt.event.ActionEvent;
10import java.awt.event.WindowAdapter;
11import java.awt.event.WindowEvent;
12import java.util.ArrayList;
[6051]13import java.util.Collection;
14import java.util.HashSet;
[2512]15import java.util.List;
16
[5926]17import javax.swing.AbstractAction;
18import javax.swing.Icon;
19import javax.swing.JButton;
20import javax.swing.JDialog;
21import javax.swing.JOptionPane;
[6051]22import javax.swing.event.ChangeEvent;
23import javax.swing.event.ChangeListener;
[2512]24
[13840]25import org.openstreetmap.josm.actions.JosmAction;
[2715]26import org.openstreetmap.josm.gui.help.HelpBrowser;
[2512]27import org.openstreetmap.josm.gui.help.HelpUtil;
[6040]28import org.openstreetmap.josm.gui.util.GuiHelper;
[12678]29import org.openstreetmap.josm.gui.util.WindowGeometry;
[18923]30import org.openstreetmap.josm.gui.util.WindowOnTopListener;
[16217]31import org.openstreetmap.josm.gui.widgets.HtmlPanel;
[2512]32import org.openstreetmap.josm.tools.ImageProvider;
[13842]33import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
[5200]34import org.openstreetmap.josm.tools.InputMapUtils;
[12620]35import org.openstreetmap.josm.tools.Logging;
[2512]36
[12391]37/**
38 * Utility methods that display an option dialog with an additional help button that links to the JOSM help
39 */
[6362]40public final class HelpAwareOptionPane {
[2512]41
[6360]42 private HelpAwareOptionPane() {
43 // Hide default constructor for utils classes
44 }
[6920]45
[12391]46 /**
47 * A specification of a button that should be added to the options dialog
48 */
[2512]49 public static class ButtonSpec {
[12391]50 /**
51 * the button text
52 */
[5951]53 public final String text;
[12391]54 /**
55 * the icon to display. Can be <code>null</code>
56 */
[5951]57 public final Icon icon;
[12391]58 /**
59 * The tooltip to display when hovering the button
60 */
[5951]61 public final String tooltipText;
[12391]62 /**
63 * The help topic to link
64 */
[5951]65 public final String helpTopic;
[6051]66 private boolean enabled;
[6070]67
[7005]68 private final Collection<ChangeListener> listeners = new HashSet<>();
[6070]69
[5951]70 /**
71 * Constructs a new {@code ButtonSpec}.
72 * @param text the button text
[13842]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
[5951]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 }
[2512]92
93 /**
[5951]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.
[2512]98 * @param helpTopic the help topic. Can be null.
[5951]99 * @param enabled the enabled status
100 * @since 5951
[2512]101 */
[5951]102 public ButtonSpec(String text, Icon icon, String tooltipText, String helpTopic, boolean enabled) {
[2512]103 this.text = text;
104 this.icon = icon;
105 this.tooltipText = tooltipText;
106 this.helpTopic = helpTopic;
[6051]107 setEnabled(enabled);
[2512]108 }
[6070]109
[6051]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 }
[6070]118
[6051]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 }
[6070]133
[8512]134 private boolean addChangeListener(ChangeListener listener) {
[9074]135 return listener != null && listeners.add(listener);
[6051]136 }
[2512]137 }
138
[6889]139 private static class DefaultAction extends AbstractAction {
[9078]140 private final JDialog dialog;
141 private final JOptionPane pane;
142 private final int value;
[2512]143
[8836]144 DefaultAction(JDialog dialog, JOptionPane pane, int value) {
[2512]145 this.dialog = dialog;
146 this.pane = pane;
147 this.value = value;
148 }
149
[6084]150 @Override
[2512]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 */
[6889]165 private static List<JButton> createOptionButtons(ButtonSpec[] options, String helpTopic) {
[7005]166 List<JButton> buttons = new ArrayList<>();
[2512]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 {
[6051]174 for (final ButtonSpec spec: options) {
175 final JButton b = new JButton(spec.text);
[2512]176 b.setIcon(spec.icon);
[8510]177 b.setToolTipText(spec.tooltipText == null ? "" : spec.tooltipText);
[2512]178 if (helpTopic != null) {
179 HelpUtil.setHelpContext(b, helpTopic);
180 }
181 b.setFocusable(true);
[6051]182 b.setEnabled(spec.isEnabled());
[10611]183 spec.addChangeListener(e -> b.setEnabled(spec.isEnabled()));
[2512]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 */
[13840]196 private static JButton createHelpButton(String helpTopic) {
197 JButton b = new JButton(new HelpAction(helpTopic));
[2512]198 HelpUtil.setHelpContext(b, helpTopic);
[5200]199 InputMapUtils.enableEnter(b);
[2512]200 return b;
201 }
202
[13840]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
[2512]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.
[18849]222 * <p>
[2512]223 * <code>helpTopic</code> is the trailing part of a JOSM online help URL, i.e. the part after the leading
[18849]224 * {@code https://josm.openstreetmap.de/wiki/Help}. It should start with a leading '/' and it
[2512]225 * may include an anchor after a '#'.
[18849]226 * <p>
[2512]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
[5266]238 * @param messageType the message type (see {@link JOptionPane})
[2512]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 */
[7466]245 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType,
[10378]246 Icon icon, final ButtonSpec[] options, final ButtonSpec defaultOption, final String helpTopic) {
[2512]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) {
[8510]254 for (int i = 0; i < options.length; i++) {
[2512]255 if (options[i] == defaultOption) {
256 defaultButton = buttons.get(i);
257 break;
258 }
259 }
260 }
[16217]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 }
[2512]268 final JOptionPane pane = new JOptionPane(
[16217]269 content,
[2512]270 messageType,
271 JOptionPane.DEFAULT_OPTION,
272 icon,
273 buttons.toArray(),
274 defaultButton
275 );
276
[10122]277 // Log message. Useful for bug reports and unit tests
278 switch (messageType) {
279 case JOptionPane.ERROR_MESSAGE:
[12620]280 Logging.error(title + " - " + msg);
[10122]281 break;
282 case JOptionPane.WARNING_MESSAGE:
[12620]283 Logging.warn(title + " - " + msg);
[10122]284 break;
285 default:
[12620]286 Logging.info(title + " - " + msg);
[10122]287 }
288
[9917]289 if (!GraphicsEnvironment.isHeadless()) {
290 doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane);
291 }
[9918]292 return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION;
[9917]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) {
[2512]298 final JDialog dialog = new JDialog(
[10035]299 GuiHelper.getFrameForComponent(parentComponent),
[2512]300 title,
[3501]301 ModalityType.DOCUMENT_MODAL
[2512]302 );
303 dialog.setContentPane(pane);
[3501]304 dialog.addWindowListener(new WindowAdapter() {
305 @Override
306 public void windowClosing(WindowEvent e) {
307 pane.setValue(JOptionPane.CLOSED_OPTION);
308 super.windowClosed(e);
309 }
[2512]310
[3501]311 @Override
312 public void windowOpened(WindowEvent e) {
313 if (defaultOption != null && options != null && options.length > 0) {
314 int i;
[8510]315 for (i = 0; i < options.length; i++) {
[3501]316 if (options[i] == defaultOption) {
317 break;
[2512]318 }
319 }
[3501]320 if (i >= options.length) {
321 buttons.get(0).requestFocusInWindow();
322 }
323 buttons.get(i).requestFocusInWindow();
324 } else {
325 buttons.get(0).requestFocusInWindow();
[2512]326 }
[3501]327 }
328 });
[10791]329 InputMapUtils.addEscapeAction(dialog.getRootPane(), new AbstractAction() {
[6084]330 @Override
[2512]331 public void actionPerformed(ActionEvent e) {
332 pane.setValue(JOptionPane.CLOSED_OPTION);
333 dialog.setVisible(false);
[9059]334 }
335 });
[2512]336
337 if (options != null) {
[8510]338 for (int i = 0; i < options.length; i++) {
[2512]339 final DefaultAction action = new DefaultAction(dialog, pane, i);
340 buttons.get(i).addActionListener(action);
[10790]341 InputMapUtils.addEnterAction(buttons.get(i), action);
[2512]342 }
343 } else {
344 final DefaultAction action = new DefaultAction(dialog, pane, 0);
345 buttons.get(0).addActionListener(action);
[10790]346 InputMapUtils.addEnterAction(buttons.get(0), action);
[2512]347 }
348
349 dialog.pack();
350 WindowGeometry.centerOnScreen(dialog.getSize()).applySafe(dialog);
351 if (helpTopic != null) {
352 HelpUtil.setHelpContext(dialog.getRootPane(), helpTopic);
353 }
[18849]354 if (dialog.isModal()) {
355 dialog.setAlwaysOnTop(true);
[18923]356 dialog.addWindowFocusListener(new WindowOnTopListener());
[18849]357 }
[2512]358 dialog.setVisible(true);
359 }
360
361 /**
[5909]362 * Displays an option dialog which is aware of a help context.
[2512]363 *
[5909]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}
[2512]370 * @see #showOptionDialog(Component, Object, String, int, Icon, ButtonSpec[], ButtonSpec, String)
371 */
[10378]372 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, String helpTopic) {
[7466]373 return showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic);
[2512]374 }
[3501]375
376 /**
377 * Run it in Event Dispatch Thread.
[7466]378 * This version does not return anything, so it is more like {@code showMessageDialog}.
[18849]379 * <p>
[3501]380 * It can be used, when you need to show a message dialog from a worker thread,
[7466]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.
[3501]388 */
[8509]389 public static void showMessageDialogInEDT(final Component parentComponent, final Object msg, final String title,
[10378]390 final int messageType, final String helpTopic) {
[10611]391 GuiHelper.runInEDT(() -> showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic));
[3501]392 }
[2512]393}
Note: See TracBrowser for help on using the repository browser.