| 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.util.WindowOnTopListener;
|
|---|
| 31 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
|---|
| 32 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 33 | import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
|---|
| 34 | import org.openstreetmap.josm.tools.InputMapUtils;
|
|---|
| 35 | import 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 | */
|
|---|
| 40 | public 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 | ((HtmlPanel) content).enableClickableHyperlinks();
|
|---|
| 266 | } else {
|
|---|
| 267 | content = msg;
|
|---|
| 268 | }
|
|---|
| 269 | final JOptionPane pane = new JOptionPane(
|
|---|
| 270 | content,
|
|---|
| 271 | messageType,
|
|---|
| 272 | JOptionPane.DEFAULT_OPTION,
|
|---|
| 273 | icon,
|
|---|
| 274 | buttons.toArray(),
|
|---|
| 275 | defaultButton
|
|---|
| 276 | );
|
|---|
| 277 |
|
|---|
| 278 | // Log message. Useful for bug reports and unit tests
|
|---|
| 279 | switch (messageType) {
|
|---|
| 280 | case JOptionPane.ERROR_MESSAGE:
|
|---|
| 281 | Logging.error(title + " - " + msg);
|
|---|
| 282 | break;
|
|---|
| 283 | case JOptionPane.WARNING_MESSAGE:
|
|---|
| 284 | Logging.warn(title + " - " + msg);
|
|---|
| 285 | break;
|
|---|
| 286 | default:
|
|---|
| 287 | Logging.info(title + " - " + msg);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | if (!GraphicsEnvironment.isHeadless()) {
|
|---|
| 291 | doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane);
|
|---|
| 292 | }
|
|---|
| 293 | return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | private static void doShowOptionDialog(Component parentComponent, String title, final ButtonSpec[] options,
|
|---|
| 297 | final ButtonSpec defaultOption, final String helpTopic, final List<JButton> buttons,
|
|---|
| 298 | final JOptionPane pane) {
|
|---|
| 299 | final JDialog dialog = new JDialog(
|
|---|
| 300 | GuiHelper.getFrameForComponent(parentComponent),
|
|---|
| 301 | title,
|
|---|
| 302 | ModalityType.DOCUMENT_MODAL
|
|---|
| 303 | );
|
|---|
| 304 | dialog.setContentPane(pane);
|
|---|
| 305 | dialog.addWindowListener(new WindowAdapter() {
|
|---|
| 306 | @Override
|
|---|
| 307 | public void windowClosing(WindowEvent e) {
|
|---|
| 308 | pane.setValue(JOptionPane.CLOSED_OPTION);
|
|---|
| 309 | super.windowClosed(e);
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | @Override
|
|---|
| 313 | public void windowOpened(WindowEvent e) {
|
|---|
| 314 | if (defaultOption != null && options != null && options.length > 0) {
|
|---|
| 315 | int i;
|
|---|
| 316 | for (i = 0; i < options.length; i++) {
|
|---|
| 317 | if (options[i] == defaultOption) {
|
|---|
| 318 | break;
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 | if (i >= options.length) {
|
|---|
| 322 | buttons.get(0).requestFocusInWindow();
|
|---|
| 323 | }
|
|---|
| 324 | buttons.get(i).requestFocusInWindow();
|
|---|
| 325 | } else {
|
|---|
| 326 | buttons.get(0).requestFocusInWindow();
|
|---|
| 327 | }
|
|---|
| 328 | }
|
|---|
| 329 | });
|
|---|
| 330 | InputMapUtils.addEscapeAction(dialog.getRootPane(), new AbstractAction() {
|
|---|
| 331 | @Override
|
|---|
| 332 | public void actionPerformed(ActionEvent e) {
|
|---|
| 333 | pane.setValue(JOptionPane.CLOSED_OPTION);
|
|---|
| 334 | dialog.setVisible(false);
|
|---|
| 335 | }
|
|---|
| 336 | });
|
|---|
| 337 |
|
|---|
| 338 | if (options != null) {
|
|---|
| 339 | for (int i = 0; i < options.length; i++) {
|
|---|
| 340 | final DefaultAction action = new DefaultAction(dialog, pane, i);
|
|---|
| 341 | buttons.get(i).addActionListener(action);
|
|---|
| 342 | InputMapUtils.addEnterAction(buttons.get(i), action);
|
|---|
| 343 | }
|
|---|
| 344 | } else {
|
|---|
| 345 | final DefaultAction action = new DefaultAction(dialog, pane, 0);
|
|---|
| 346 | buttons.get(0).addActionListener(action);
|
|---|
| 347 | InputMapUtils.addEnterAction(buttons.get(0), action);
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | dialog.pack();
|
|---|
| 351 | WindowGeometry.centerOnScreen(dialog.getSize()).applySafe(dialog);
|
|---|
| 352 | if (helpTopic != null) {
|
|---|
| 353 | HelpUtil.setHelpContext(dialog.getRootPane(), helpTopic);
|
|---|
| 354 | }
|
|---|
| 355 | if (dialog.isModal()) {
|
|---|
| 356 | dialog.setAlwaysOnTop(true);
|
|---|
| 357 | dialog.addWindowFocusListener(new WindowOnTopListener());
|
|---|
| 358 | }
|
|---|
| 359 | dialog.setVisible(true);
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /**
|
|---|
| 363 | * Displays an option dialog which is aware of a help context.
|
|---|
| 364 | *
|
|---|
| 365 | * @param parentComponent the parent component
|
|---|
| 366 | * @param msg the message
|
|---|
| 367 | * @param title the title
|
|---|
| 368 | * @param messageType the message type (see {@link JOptionPane})
|
|---|
| 369 | * @param helpTopic the help topic. Can be null.
|
|---|
| 370 | * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
|
|---|
| 371 | * @see #showOptionDialog(Component, Object, String, int, Icon, ButtonSpec[], ButtonSpec, String)
|
|---|
| 372 | */
|
|---|
| 373 | public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, String helpTopic) {
|
|---|
| 374 | return showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /**
|
|---|
| 378 | * Run it in Event Dispatch Thread.
|
|---|
| 379 | * This version does not return anything, so it is more like {@code showMessageDialog}.
|
|---|
| 380 | * <p>
|
|---|
| 381 | * It can be used, when you need to show a message dialog from a worker thread,
|
|---|
| 382 | * e.g. from {@code PleaseWaitRunnable}.
|
|---|
| 383 | *
|
|---|
| 384 | * @param parentComponent the parent component
|
|---|
| 385 | * @param msg the message
|
|---|
| 386 | * @param title the title
|
|---|
| 387 | * @param messageType the message type (see {@link JOptionPane})
|
|---|
| 388 | * @param helpTopic the help topic. Can be null.
|
|---|
| 389 | */
|
|---|
| 390 | public static void showMessageDialogInEDT(final Component parentComponent, final Object msg, final String title,
|
|---|
| 391 | final int messageType, final String helpTopic) {
|
|---|
| 392 | GuiHelper.runInEDT(() -> showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic));
|
|---|
| 393 | }
|
|---|
| 394 | }
|
|---|