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

Last change on this file since 10304 was 10122, checked in by Don-vip, 8 years ago

add more debug messages to debug failing unit test

  • Property svn:eol-style set to native
File size: 13.8 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.KeyEvent;
11import java.awt.event.WindowAdapter;
12import java.awt.event.WindowEvent;
13import java.util.ArrayList;
14import java.util.Collection;
15import java.util.HashSet;
16import java.util.List;
17
18import javax.swing.AbstractAction;
19import javax.swing.Action;
20import javax.swing.Icon;
21import javax.swing.JButton;
22import javax.swing.JComponent;
23import javax.swing.JDialog;
24import javax.swing.JOptionPane;
25import javax.swing.KeyStroke;
26import javax.swing.event.ChangeEvent;
27import javax.swing.event.ChangeListener;
28
29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.gui.help.HelpBrowser;
31import org.openstreetmap.josm.gui.help.HelpUtil;
32import org.openstreetmap.josm.gui.util.GuiHelper;
33import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
34import org.openstreetmap.josm.tools.ImageProvider;
35import org.openstreetmap.josm.tools.InputMapUtils;
36import org.openstreetmap.josm.tools.WindowGeometry;
37
38public 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(new ChangeListener() {
155 @Override public void stateChanged(ChangeEvent e) {
156 b.setEnabled(spec.isEnabled());
157 }
158 });
159 buttons.add(b);
160 }
161 }
162 return buttons;
163 }
164
165 /**
166 * Creates the help button
167 *
168 * @param helpTopic the help topic
169 * @return the help button
170 */
171 private static JButton createHelpButton(final String helpTopic) {
172 JButton b = new JButton(tr("Help"));
173 b.setIcon(ImageProvider.get("help"));
174 b.setToolTipText(tr("Show help information"));
175 HelpUtil.setHelpContext(b, helpTopic);
176 Action a = new AbstractAction() {
177 @Override
178 public void actionPerformed(ActionEvent e) {
179 HelpBrowser.setUrlForHelpTopic(helpTopic);
180 }
181 };
182 b.addActionListener(a);
183 InputMapUtils.enableEnter(b);
184 return b;
185 }
186
187 /**
188 * Displays an option dialog which is aware of a help context. If <code>helpTopic</code> isn't null,
189 * the dialog includes a "Help" button and launches the help browser if the user presses F1. If the
190 * user clicks on the "Help" button the option dialog remains open and JOSM launches the help
191 * browser.
192 *
193 * <code>helpTopic</code> is the trailing part of a JOSM online help URL, i.e. the part after the leading
194 * <code>https://josm.openstreetmap.de/wiki/Help</code>. It should start with a leading '/' and it
195 * may include an anchor after a '#'.
196 *
197 * <strong>Examples</strong>
198 * <ul>
199 * <li>/Dialogs/RelationEditor</li>
200 * <li>/Dialogs/RelationEditor#ConflictInData</li>
201 * </ul>
202 *
203 * In addition, the option buttons display JOSM icons, similar to ExtendedDialog.
204 *
205 * @param parentComponent the parent component
206 * @param msg the message
207 * @param title the title
208 * @param messageType the message type (see {@link JOptionPane})
209 * @param icon the icon to display. Can be null.
210 * @param options the list of options to display. Can be null.
211 * @param defaultOption the default option. Can be null.
212 * @param helpTopic the help topic. Can be null.
213 * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
214 */
215 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType,
216 Icon icon, final ButtonSpec[] options, final ButtonSpec defaultOption, final String helpTopic) {
217 final List<JButton> buttons = createOptionButtons(options, helpTopic);
218 if (helpTopic != null) {
219 buttons.add(createHelpButton(helpTopic));
220 }
221
222 JButton defaultButton = null;
223 if (options != null && defaultOption != null) {
224 for (int i = 0; i < options.length; i++) {
225 if (options[i] == defaultOption) {
226 defaultButton = buttons.get(i);
227 break;
228 }
229 }
230 }
231
232 final JOptionPane pane = new JOptionPane(
233 msg instanceof String ? new JMultilineLabel((String) msg, true) : msg,
234 messageType,
235 JOptionPane.DEFAULT_OPTION,
236 icon,
237 buttons.toArray(),
238 defaultButton
239 );
240
241 // Log message. Useful for bug reports and unit tests
242 switch (messageType) {
243 case JOptionPane.ERROR_MESSAGE:
244 Main.error(title + " - " + msg);
245 break;
246 case JOptionPane.WARNING_MESSAGE:
247 Main.warn(title + " - " + msg);
248 break;
249 default:
250 Main.info(title + " - " + msg);
251 }
252
253 if (!GraphicsEnvironment.isHeadless()) {
254 doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane);
255 }
256 return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION;
257 }
258
259 private static void doShowOptionDialog(Component parentComponent, String title, final ButtonSpec[] options,
260 final ButtonSpec defaultOption, final String helpTopic, final List<JButton> buttons,
261 final JOptionPane pane) {
262 final JDialog dialog = new JDialog(
263 GuiHelper.getFrameForComponent(parentComponent),
264 title,
265 ModalityType.DOCUMENT_MODAL
266 );
267 dialog.setContentPane(pane);
268 dialog.addWindowListener(new WindowAdapter() {
269 @Override
270 public void windowClosing(WindowEvent e) {
271 pane.setValue(JOptionPane.CLOSED_OPTION);
272 super.windowClosed(e);
273 }
274
275 @Override
276 public void windowOpened(WindowEvent e) {
277 if (defaultOption != null && options != null && options.length > 0) {
278 int i;
279 for (i = 0; i < options.length; i++) {
280 if (options[i] == defaultOption) {
281 break;
282 }
283 }
284 if (i >= options.length) {
285 buttons.get(0).requestFocusInWindow();
286 }
287 buttons.get(i).requestFocusInWindow();
288 } else {
289 buttons.get(0).requestFocusInWindow();
290 }
291 }
292 });
293 dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
294 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
295 dialog.getRootPane().getActionMap().put("close", new AbstractAction() {
296 @Override
297 public void actionPerformed(ActionEvent e) {
298 pane.setValue(JOptionPane.CLOSED_OPTION);
299 dialog.setVisible(false);
300 }
301 });
302
303 if (options != null) {
304 for (int i = 0; i < options.length; i++) {
305 final DefaultAction action = new DefaultAction(dialog, pane, i);
306 buttons.get(i).addActionListener(action);
307 buttons.get(i).getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
308 buttons.get(i).getActionMap().put("enter", action);
309 }
310 } else {
311 final DefaultAction action = new DefaultAction(dialog, pane, 0);
312 buttons.get(0).addActionListener(action);
313 buttons.get(0).getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
314 buttons.get(0).getActionMap().put("enter", action);
315 }
316
317 dialog.pack();
318 WindowGeometry.centerOnScreen(dialog.getSize()).applySafe(dialog);
319 if (helpTopic != null) {
320 HelpUtil.setHelpContext(dialog.getRootPane(), helpTopic);
321 }
322 dialog.setVisible(true);
323 }
324
325 /**
326 * Displays an option dialog which is aware of a help context.
327 *
328 * @param parentComponent the parent component
329 * @param msg the message
330 * @param title the title
331 * @param messageType the message type (see {@link JOptionPane})
332 * @param helpTopic the help topic. Can be null.
333 * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
334 * @see #showOptionDialog(Component, Object, String, int, Icon, ButtonSpec[], ButtonSpec, String)
335 */
336 public static int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, String helpTopic) {
337 return showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic);
338 }
339
340 /**
341 * Run it in Event Dispatch Thread.
342 * This version does not return anything, so it is more like {@code showMessageDialog}.
343 *
344 * It can be used, when you need to show a message dialog from a worker thread,
345 * e.g. from {@code PleaseWaitRunnable}.
346 *
347 * @param parentComponent the parent component
348 * @param msg the message
349 * @param title the title
350 * @param messageType the message type (see {@link JOptionPane})
351 * @param helpTopic the help topic. Can be null.
352 */
353 public static void showMessageDialogInEDT(final Component parentComponent, final Object msg, final String title,
354 final int messageType, final String helpTopic) {
355 GuiHelper.runInEDT(new Runnable() {
356 @Override
357 public void run() {
358 showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic);
359 }
360 });
361 }
362}
Note: See TracBrowser for help on using the repository browser.