001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Component; 007import java.awt.Dialog.ModalityType; 008import java.awt.GraphicsEnvironment; 009import java.awt.event.ActionEvent; 010import java.awt.event.WindowAdapter; 011import java.awt.event.WindowEvent; 012import java.util.ArrayList; 013import java.util.Collection; 014import java.util.HashSet; 015import java.util.List; 016 017import javax.swing.AbstractAction; 018import javax.swing.Icon; 019import javax.swing.JButton; 020import javax.swing.JDialog; 021import javax.swing.JOptionPane; 022import javax.swing.event.ChangeEvent; 023import javax.swing.event.ChangeListener; 024 025import org.openstreetmap.josm.actions.JosmAction; 026import org.openstreetmap.josm.gui.help.HelpBrowser; 027import org.openstreetmap.josm.gui.help.HelpUtil; 028import org.openstreetmap.josm.gui.util.GuiHelper; 029import org.openstreetmap.josm.gui.util.WindowGeometry; 030import org.openstreetmap.josm.gui.widgets.HtmlPanel; 031import org.openstreetmap.josm.tools.ImageProvider; 032import org.openstreetmap.josm.tools.ImageProvider.ImageSizes; 033import org.openstreetmap.josm.tools.InputMapUtils; 034import org.openstreetmap.josm.tools.Logging; 035 036/** 037 * Utility methods that display an option dialog with an additional help button that links to the JOSM help 038 */ 039public final class HelpAwareOptionPane { 040 041 private HelpAwareOptionPane() { 042 // Hide default constructor for utils classes 043 } 044 045 /** 046 * A specification of a button that should be added to the options dialog 047 */ 048 public static class ButtonSpec { 049 /** 050 * the button text 051 */ 052 public final String text; 053 /** 054 * the icon to display. Can be <code>null</code> 055 */ 056 public final Icon icon; 057 /** 058 * The tooltip to display when hovering the button 059 */ 060 public final String tooltipText; 061 /** 062 * The help topic to link 063 */ 064 public final String helpTopic; 065 private boolean enabled; 066 067 private final Collection<ChangeListener> listeners = new HashSet<>(); 068 069 /** 070 * Constructs a new {@code ButtonSpec}. 071 * @param text the button text 072 * @param imageProvider provides the icon to display. Can be null 073 * @param tooltipText the tooltip text. Can be null. 074 * @param helpTopic the help topic. Can be null. 075 * @since 13842 076 */ 077 public ButtonSpec(String text, ImageProvider imageProvider, String tooltipText, String helpTopic) { 078 this(text, imageProvider != null ? imageProvider.setSize(ImageSizes.LARGEICON).get() : null, tooltipText, helpTopic, true); 079 } 080 081 /** 082 * Constructs a new {@code ButtonSpec}. 083 * @param text the button text 084 * @param icon the icon to display. Can be null 085 * @param tooltipText the tooltip text. Can be null. 086 * @param helpTopic the help topic. Can be null. 087 */ 088 public ButtonSpec(String text, Icon icon, String tooltipText, String helpTopic) { 089 this(text, icon, tooltipText, helpTopic, true); 090 } 091 092 /** 093 * Constructs a new {@code ButtonSpec}. 094 * @param text the button text 095 * @param icon the icon to display. Can be null 096 * @param tooltipText the tooltip text. Can be null. 097 * @param helpTopic the help topic. Can be null. 098 * @param enabled the enabled status 099 * @since 5951 100 */ 101 public ButtonSpec(String text, Icon icon, String tooltipText, String helpTopic, boolean enabled) { 102 this.text = text; 103 this.icon = icon; 104 this.tooltipText = tooltipText; 105 this.helpTopic = helpTopic; 106 setEnabled(enabled); 107 } 108 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 } 117 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 } 132 133 private boolean addChangeListener(ChangeListener listener) { 134 return listener != null && listeners.add(listener); 135 } 136 } 137 138 private static class DefaultAction extends AbstractAction { 139 private final JDialog dialog; 140 private final JOptionPane pane; 141 private final int value; 142 143 DefaultAction(JDialog dialog, JOptionPane pane, int value) { 144 this.dialog = dialog; 145 this.pane = pane; 146 this.value = value; 147 } 148 149 @Override 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 */ 164 private static List<JButton> createOptionButtons(ButtonSpec[] options, String helpTopic) { 165 List<JButton> buttons = new ArrayList<>(); 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 { 173 for (final ButtonSpec spec: options) { 174 final JButton b = new JButton(spec.text); 175 b.setIcon(spec.icon); 176 b.setToolTipText(spec.tooltipText == null ? "" : spec.tooltipText); 177 if (helpTopic != null) { 178 HelpUtil.setHelpContext(b, helpTopic); 179 } 180 b.setFocusable(true); 181 b.setEnabled(spec.isEnabled()); 182 spec.addChangeListener(e -> b.setEnabled(spec.isEnabled())); 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 */ 195 private static JButton createHelpButton(String helpTopic) { 196 JButton b = new JButton(new HelpAction(helpTopic)); 197 HelpUtil.setHelpContext(b, helpTopic); 198 InputMapUtils.enableEnter(b); 199 return b; 200 } 201 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 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 223 * <code>https://josm.openstreetmap.de/wiki/Help</code>. It should start with a leading '/' and it 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 237 * @param messageType the message type (see {@link JOptionPane}) 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 */ 244 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, 245 Icon icon, final ButtonSpec[] options, final ButtonSpec defaultOption, final String helpTopic) { 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) { 253 for (int i = 0; i < options.length; i++) { 254 if (options[i] == defaultOption) { 255 defaultButton = buttons.get(i); 256 break; 257 } 258 } 259 } 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 } 267 final JOptionPane pane = new JOptionPane( 268 content, 269 messageType, 270 JOptionPane.DEFAULT_OPTION, 271 icon, 272 buttons.toArray(), 273 defaultButton 274 ); 275 276 // Log message. Useful for bug reports and unit tests 277 switch (messageType) { 278 case JOptionPane.ERROR_MESSAGE: 279 Logging.error(title + " - " + msg); 280 break; 281 case JOptionPane.WARNING_MESSAGE: 282 Logging.warn(title + " - " + msg); 283 break; 284 default: 285 Logging.info(title + " - " + msg); 286 } 287 288 if (!GraphicsEnvironment.isHeadless()) { 289 doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane); 290 } 291 return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION; 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) { 297 final JDialog dialog = new JDialog( 298 GuiHelper.getFrameForComponent(parentComponent), 299 title, 300 ModalityType.DOCUMENT_MODAL 301 ); 302 dialog.setContentPane(pane); 303 dialog.addWindowListener(new WindowAdapter() { 304 @Override 305 public void windowClosing(WindowEvent e) { 306 pane.setValue(JOptionPane.CLOSED_OPTION); 307 super.windowClosed(e); 308 } 309 310 @Override 311 public void windowOpened(WindowEvent e) { 312 if (defaultOption != null && options != null && options.length > 0) { 313 int i; 314 for (i = 0; i < options.length; i++) { 315 if (options[i] == defaultOption) { 316 break; 317 } 318 } 319 if (i >= options.length) { 320 buttons.get(0).requestFocusInWindow(); 321 } 322 buttons.get(i).requestFocusInWindow(); 323 } else { 324 buttons.get(0).requestFocusInWindow(); 325 } 326 } 327 }); 328 InputMapUtils.addEscapeAction(dialog.getRootPane(), new AbstractAction() { 329 @Override 330 public void actionPerformed(ActionEvent e) { 331 pane.setValue(JOptionPane.CLOSED_OPTION); 332 dialog.setVisible(false); 333 } 334 }); 335 336 if (options != null) { 337 for (int i = 0; i < options.length; i++) { 338 final DefaultAction action = new DefaultAction(dialog, pane, i); 339 buttons.get(i).addActionListener(action); 340 InputMapUtils.addEnterAction(buttons.get(i), action); 341 } 342 } else { 343 final DefaultAction action = new DefaultAction(dialog, pane, 0); 344 buttons.get(0).addActionListener(action); 345 InputMapUtils.addEnterAction(buttons.get(0), action); 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 /** 357 * Displays an option dialog which is aware of a help context. 358 * 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} 365 * @see #showOptionDialog(Component, Object, String, int, Icon, ButtonSpec[], ButtonSpec, String) 366 */ 367 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, String helpTopic) { 368 return showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic); 369 } 370 371 /** 372 * Run it in Event Dispatch Thread. 373 * This version does not return anything, so it is more like {@code showMessageDialog}. 374 * 375 * It can be used, when you need to show a message dialog from a worker thread, 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. 383 */ 384 public static void showMessageDialogInEDT(final Component parentComponent, final Object msg, final String title, 385 final int messageType, final String helpTopic) { 386 GuiHelper.runInEDT(() -> showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic)); 387 } 388}