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

Last change on this file was 19225, checked in by taylor.smock, 10 months ago

Fix #23925: Indicate/link to alternative download methods when the user attempts to download too much data

  • Property svn:eol-style set to native
File size: 14.8 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>");
[19225]265 ((HtmlPanel) content).enableClickableHyperlinks();
[16217]266 } else {
267 content = msg;
268 }
[2512]269 final JOptionPane pane = new JOptionPane(
[16217]270 content,
[2512]271 messageType,
272 JOptionPane.DEFAULT_OPTION,
273 icon,
274 buttons.toArray(),
275 defaultButton
276 );
277
[10122]278 // Log message. Useful for bug reports and unit tests
279 switch (messageType) {
280 case JOptionPane.ERROR_MESSAGE:
[12620]281 Logging.error(title + " - " + msg);
[10122]282 break;
283 case JOptionPane.WARNING_MESSAGE:
[12620]284 Logging.warn(title + " - " + msg);
[10122]285 break;
286 default:
[12620]287 Logging.info(title + " - " + msg);
[10122]288 }
289
[9917]290 if (!GraphicsEnvironment.isHeadless()) {
291 doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane);
292 }
[9918]293 return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION;
[9917]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) {
[2512]299 final JDialog dialog = new JDialog(
[10035]300 GuiHelper.getFrameForComponent(parentComponent),
[2512]301 title,
[3501]302 ModalityType.DOCUMENT_MODAL
[2512]303 );
304 dialog.setContentPane(pane);
[3501]305 dialog.addWindowListener(new WindowAdapter() {
306 @Override
307 public void windowClosing(WindowEvent e) {
308 pane.setValue(JOptionPane.CLOSED_OPTION);
309 super.windowClosed(e);
310 }
[2512]311
[3501]312 @Override
313 public void windowOpened(WindowEvent e) {
314 if (defaultOption != null && options != null && options.length > 0) {
315 int i;
[8510]316 for (i = 0; i < options.length; i++) {
[3501]317 if (options[i] == defaultOption) {
318 break;
[2512]319 }
320 }
[3501]321 if (i >= options.length) {
322 buttons.get(0).requestFocusInWindow();
323 }
324 buttons.get(i).requestFocusInWindow();
325 } else {
326 buttons.get(0).requestFocusInWindow();
[2512]327 }
[3501]328 }
329 });
[10791]330 InputMapUtils.addEscapeAction(dialog.getRootPane(), new AbstractAction() {
[6084]331 @Override
[2512]332 public void actionPerformed(ActionEvent e) {
333 pane.setValue(JOptionPane.CLOSED_OPTION);
334 dialog.setVisible(false);
[9059]335 }
336 });
[2512]337
338 if (options != null) {
[8510]339 for (int i = 0; i < options.length; i++) {
[2512]340 final DefaultAction action = new DefaultAction(dialog, pane, i);
341 buttons.get(i).addActionListener(action);
[10790]342 InputMapUtils.addEnterAction(buttons.get(i), action);
[2512]343 }
344 } else {
345 final DefaultAction action = new DefaultAction(dialog, pane, 0);
346 buttons.get(0).addActionListener(action);
[10790]347 InputMapUtils.addEnterAction(buttons.get(0), action);
[2512]348 }
349
350 dialog.pack();
351 WindowGeometry.centerOnScreen(dialog.getSize()).applySafe(dialog);
352 if (helpTopic != null) {
353 HelpUtil.setHelpContext(dialog.getRootPane(), helpTopic);
354 }
[18849]355 if (dialog.isModal()) {
356 dialog.setAlwaysOnTop(true);
[18923]357 dialog.addWindowFocusListener(new WindowOnTopListener());
[18849]358 }
[2512]359 dialog.setVisible(true);
360 }
361
362 /**
[5909]363 * Displays an option dialog which is aware of a help context.
[2512]364 *
[5909]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}
[2512]371 * @see #showOptionDialog(Component, Object, String, int, Icon, ButtonSpec[], ButtonSpec, String)
372 */
[10378]373 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, String helpTopic) {
[7466]374 return showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic);
[2512]375 }
[3501]376
377 /**
378 * Run it in Event Dispatch Thread.
[7466]379 * This version does not return anything, so it is more like {@code showMessageDialog}.
[18849]380 * <p>
[3501]381 * It can be used, when you need to show a message dialog from a worker thread,
[7466]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.
[3501]389 */
[8509]390 public static void showMessageDialogInEDT(final Component parentComponent, final Object msg, final String title,
[10378]391 final int messageType, final String helpTopic) {
[10611]392 GuiHelper.runInEDT(() -> showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic));
[3501]393 }
[2512]394}
Note: See TracBrowser for help on using the repository browser.