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

Last change on this file since 6340 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

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