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