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

Last change on this file since 13840 was 13840, checked in by Don-vip, 6 years ago

fix #16319 - scale properly icon of Help button

  • Property svn:eol-style set to native
File size: 13.7 KB
Line 
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;
7import java.awt.Dialog.ModalityType;
8import java.awt.GraphicsEnvironment;
9import java.awt.event.ActionEvent;
10import java.awt.event.WindowAdapter;
11import java.awt.event.WindowEvent;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.HashSet;
15import java.util.List;
16
17import javax.swing.AbstractAction;
18import javax.swing.Icon;
19import javax.swing.JButton;
20import javax.swing.JDialog;
21import javax.swing.JOptionPane;
22import javax.swing.event.ChangeEvent;
23import javax.swing.event.ChangeListener;
24
25import org.openstreetmap.josm.actions.JosmAction;
26import org.openstreetmap.josm.gui.help.HelpBrowser;
27import org.openstreetmap.josm.gui.help.HelpUtil;
28import org.openstreetmap.josm.gui.util.GuiHelper;
29import org.openstreetmap.josm.gui.util.WindowGeometry;
30import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
31import org.openstreetmap.josm.tools.ImageProvider;
32import org.openstreetmap.josm.tools.InputMapUtils;
33import org.openstreetmap.josm.tools.Logging;
34
35/**
36 * Utility methods that display an option dialog with an additional help button that links to the JOSM help
37 */
38public final class HelpAwareOptionPane {
39
40 private HelpAwareOptionPane() {
41 // Hide default constructor for utils classes
42 }
43
44 /**
45 * A specification of a button that should be added to the options dialog
46 */
47 public static class ButtonSpec {
48 /**
49 * the button text
50 */
51 public final String text;
52 /**
53 * the icon to display. Can be <code>null</code>
54 */
55 public final Icon icon;
56 /**
57 * The tooltip to display when hovering the button
58 */
59 public final String tooltipText;
60 /**
61 * The help topic to link
62 */
63 public final String helpTopic;
64 private boolean enabled;
65
66 private final Collection<ChangeListener> listeners = new HashSet<>();
67
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 }
78
79 /**
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.
84 * @param helpTopic the help topic. Can be null.
85 * @param enabled the enabled status
86 * @since 5951
87 */
88 public ButtonSpec(String text, Icon icon, String tooltipText, String helpTopic, boolean enabled) {
89 this.text = text;
90 this.icon = icon;
91 this.tooltipText = tooltipText;
92 this.helpTopic = helpTopic;
93 setEnabled(enabled);
94 }
95
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 }
104
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 }
119
120 private boolean addChangeListener(ChangeListener listener) {
121 return listener != null && listeners.add(listener);
122 }
123 }
124
125 private static class DefaultAction extends AbstractAction {
126 private final JDialog dialog;
127 private final JOptionPane pane;
128 private final int value;
129
130 DefaultAction(JDialog dialog, JOptionPane pane, int value) {
131 this.dialog = dialog;
132 this.pane = pane;
133 this.value = value;
134 }
135
136 @Override
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 */
151 private static List<JButton> createOptionButtons(ButtonSpec[] options, String helpTopic) {
152 List<JButton> buttons = new ArrayList<>();
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 {
160 for (final ButtonSpec spec: options) {
161 final JButton b = new JButton(spec.text);
162 b.setIcon(spec.icon);
163 b.setToolTipText(spec.tooltipText == null ? "" : spec.tooltipText);
164 if (helpTopic != null) {
165 HelpUtil.setHelpContext(b, helpTopic);
166 }
167 b.setFocusable(true);
168 b.setEnabled(spec.isEnabled());
169 spec.addChangeListener(e -> b.setEnabled(spec.isEnabled()));
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 */
182 private static JButton createHelpButton(String helpTopic) {
183 JButton b = new JButton(new HelpAction(helpTopic));
184 HelpUtil.setHelpContext(b, helpTopic);
185 InputMapUtils.enableEnter(b);
186 return b;
187 }
188
189 private static class HelpAction extends JosmAction {
190 private final String helpTopic;
191
192 HelpAction(String helpTopic) {
193 super(tr("Help"), "help", tr("Show help information"), null, false, false);
194 this.helpTopic = helpTopic;
195 }
196
197 @Override
198 public void actionPerformed(ActionEvent e) {
199 HelpBrowser.setUrlForHelpTopic(helpTopic);
200 }
201 }
202
203 /**
204 * Displays an option dialog which is aware of a help context. If <code>helpTopic</code> isn't null,
205 * the dialog includes a "Help" button and launches the help browser if the user presses F1. If the
206 * user clicks on the "Help" button the option dialog remains open and JOSM launches the help
207 * browser.
208 *
209 * <code>helpTopic</code> is the trailing part of a JOSM online help URL, i.e. the part after the leading
210 * <code>https://josm.openstreetmap.de/wiki/Help</code>. It should start with a leading '/' and it
211 * may include an anchor after a '#'.
212 *
213 * <strong>Examples</strong>
214 * <ul>
215 * <li>/Dialogs/RelationEditor</li>
216 * <li>/Dialogs/RelationEditor#ConflictInData</li>
217 * </ul>
218 *
219 * In addition, the option buttons display JOSM icons, similar to ExtendedDialog.
220 *
221 * @param parentComponent the parent component
222 * @param msg the message
223 * @param title the title
224 * @param messageType the message type (see {@link JOptionPane})
225 * @param icon the icon to display. Can be null.
226 * @param options the list of options to display. Can be null.
227 * @param defaultOption the default option. Can be null.
228 * @param helpTopic the help topic. Can be null.
229 * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
230 */
231 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType,
232 Icon icon, final ButtonSpec[] options, final ButtonSpec defaultOption, final String helpTopic) {
233 final List<JButton> buttons = createOptionButtons(options, helpTopic);
234 if (helpTopic != null) {
235 buttons.add(createHelpButton(helpTopic));
236 }
237
238 JButton defaultButton = null;
239 if (options != null && defaultOption != null) {
240 for (int i = 0; i < options.length; i++) {
241 if (options[i] == defaultOption) {
242 defaultButton = buttons.get(i);
243 break;
244 }
245 }
246 }
247
248 final JOptionPane pane = new JOptionPane(
249 msg instanceof String ? new JMultilineLabel((String) msg, true) : msg,
250 messageType,
251 JOptionPane.DEFAULT_OPTION,
252 icon,
253 buttons.toArray(),
254 defaultButton
255 );
256
257 // Log message. Useful for bug reports and unit tests
258 switch (messageType) {
259 case JOptionPane.ERROR_MESSAGE:
260 Logging.error(title + " - " + msg);
261 break;
262 case JOptionPane.WARNING_MESSAGE:
263 Logging.warn(title + " - " + msg);
264 break;
265 default:
266 Logging.info(title + " - " + msg);
267 }
268
269 if (!GraphicsEnvironment.isHeadless()) {
270 doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane);
271 }
272 return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION;
273 }
274
275 private static void doShowOptionDialog(Component parentComponent, String title, final ButtonSpec[] options,
276 final ButtonSpec defaultOption, final String helpTopic, final List<JButton> buttons,
277 final JOptionPane pane) {
278 final JDialog dialog = new JDialog(
279 GuiHelper.getFrameForComponent(parentComponent),
280 title,
281 ModalityType.DOCUMENT_MODAL
282 );
283 dialog.setContentPane(pane);
284 dialog.addWindowListener(new WindowAdapter() {
285 @Override
286 public void windowClosing(WindowEvent e) {
287 pane.setValue(JOptionPane.CLOSED_OPTION);
288 super.windowClosed(e);
289 }
290
291 @Override
292 public void windowOpened(WindowEvent e) {
293 if (defaultOption != null && options != null && options.length > 0) {
294 int i;
295 for (i = 0; i < options.length; i++) {
296 if (options[i] == defaultOption) {
297 break;
298 }
299 }
300 if (i >= options.length) {
301 buttons.get(0).requestFocusInWindow();
302 }
303 buttons.get(i).requestFocusInWindow();
304 } else {
305 buttons.get(0).requestFocusInWindow();
306 }
307 }
308 });
309 InputMapUtils.addEscapeAction(dialog.getRootPane(), new AbstractAction() {
310 @Override
311 public void actionPerformed(ActionEvent e) {
312 pane.setValue(JOptionPane.CLOSED_OPTION);
313 dialog.setVisible(false);
314 }
315 });
316
317 if (options != null) {
318 for (int i = 0; i < options.length; i++) {
319 final DefaultAction action = new DefaultAction(dialog, pane, i);
320 buttons.get(i).addActionListener(action);
321 InputMapUtils.addEnterAction(buttons.get(i), action);
322 }
323 } else {
324 final DefaultAction action = new DefaultAction(dialog, pane, 0);
325 buttons.get(0).addActionListener(action);
326 InputMapUtils.addEnterAction(buttons.get(0), action);
327 }
328
329 dialog.pack();
330 WindowGeometry.centerOnScreen(dialog.getSize()).applySafe(dialog);
331 if (helpTopic != null) {
332 HelpUtil.setHelpContext(dialog.getRootPane(), helpTopic);
333 }
334 dialog.setVisible(true);
335 }
336
337 /**
338 * Displays an option dialog which is aware of a help context.
339 *
340 * @param parentComponent the parent component
341 * @param msg the message
342 * @param title the title
343 * @param messageType the message type (see {@link JOptionPane})
344 * @param helpTopic the help topic. Can be null.
345 * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
346 * @see #showOptionDialog(Component, Object, String, int, Icon, ButtonSpec[], ButtonSpec, String)
347 */
348 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, String helpTopic) {
349 return showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic);
350 }
351
352 /**
353 * Run it in Event Dispatch Thread.
354 * This version does not return anything, so it is more like {@code showMessageDialog}.
355 *
356 * It can be used, when you need to show a message dialog from a worker thread,
357 * e.g. from {@code PleaseWaitRunnable}.
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 */
365 public static void showMessageDialogInEDT(final Component parentComponent, final Object msg, final String title,
366 final int messageType, final String helpTopic) {
367 GuiHelper.runInEDT(() -> showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic));
368 }
369}
Note: See TracBrowser for help on using the repository browser.