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