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

Last change on this file since 9569 was 9512, checked in by simon04, 8 years ago

see #12131 - Pressing escape in closing relation confirmation should not discard the changes and close the editor

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