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