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

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

Allow buttons of HelpAwareOptionPane to be dynamically enabled/disabled

  • 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 public void actionPerformed(ActionEvent e) {
116 pane.setValue(value);
117 dialog.setVisible(false);
118 }
119 }
120
121 /**
122 * Creates the list buttons to be displayed in the option pane dialog.
123 *
124 * @param options the option. If null, just creates an OK button and a help button
125 * @param helpTopic the help topic. The context sensitive help of all buttons is equal
126 * to the context sensitive help of the whole dialog
127 * @return the list of buttons
128 */
129 static private List<JButton> createOptionButtons(ButtonSpec[] options, String helpTopic) {
130 List<JButton> buttons = new ArrayList<JButton>();
131 if (options == null) {
132 JButton b = new JButton(tr("OK"));
133 b.setIcon(ImageProvider.get("ok"));
134 b.setToolTipText(tr("Click to close the dialog"));
135 b.setFocusable(true);
136 buttons.add(b);
137 } else {
138 for (final ButtonSpec spec: options) {
139 final JButton b = new JButton(spec.text);
140 b.setIcon(spec.icon);
141 b.setToolTipText(spec.tooltipText == null? "" : spec.tooltipText);
142 if (helpTopic != null) {
143 HelpUtil.setHelpContext(b, helpTopic);
144 }
145 b.setFocusable(true);
146 b.setEnabled(spec.isEnabled());
147 spec.addChangeListener(new ChangeListener() {
148 @Override public void stateChanged(ChangeEvent e) {
149 b.setEnabled(spec.isEnabled());
150 }
151 });
152 buttons.add(b);
153 }
154 }
155 return buttons;
156 }
157
158 /**
159 * Creates the help button
160 *
161 * @param helpTopic the help topic
162 * @return the help button
163 */
164 static private JButton createHelpButton(final String helpTopic) {
165 JButton b = new JButton(tr("Help"));
166 b.setIcon(ImageProvider.get("help"));
167 b.setToolTipText(tr("Show help information"));
168 HelpUtil.setHelpContext(b, helpTopic);
169 Action a = new AbstractAction() {
170 public void actionPerformed(ActionEvent e) {
171 HelpBrowser.setUrlForHelpTopic(helpTopic);
172 }
173 };
174 b.addActionListener(a);
175 InputMapUtils.enableEnter(b);
176 return b;
177 }
178
179 /**
180 * Displays an option dialog which is aware of a help context. If <code>helpTopic</code> isn't null,
181 * the dialog includes a "Help" button and launches the help browser if the user presses F1. If the
182 * user clicks on the "Help" button the option dialog remains open and JOSM launches the help
183 * browser.
184 *
185 * <code>helpTopic</code> is the trailing part of a JOSM online help URL, i.e. the part after the leading
186 * <code>http://josm.openstreetmap.de/wiki/Help</code>. It should start with a leading '/' and it
187 * may include an anchor after a '#'.
188 *
189 * <strong>Examples</strong>
190 * <ul>
191 * <li>/Dialogs/RelationEditor</li>
192 * <li>/Dialogs/RelationEditor#ConflictInData</li>
193 * </ul>
194 *
195 * In addition, the option buttons display JOSM icons, similar to ExtendedDialog.
196 *
197 * @param parentComponent the parent component
198 * @param msg the message
199 * @param title the title
200 * @param messageType the message type (see {@link JOptionPane})
201 * @param icon the icon to display. Can be null.
202 * @param options the list of options to display. Can be null.
203 * @param defaultOption the default option. Can be null.
204 * @param helpTopic the help topic. Can be null.
205 * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
206 */
207 static public int showOptionDialog(Component parentComponent, Object msg, String title, int messageType, Icon icon, final ButtonSpec[] options, final ButtonSpec defaultOption, final String helpTopic) {
208 final List<JButton> buttons = createOptionButtons(options, helpTopic);
209 if (helpTopic != null) {
210 buttons.add(createHelpButton(helpTopic));
211 }
212
213 JButton defaultButton = null;
214 if (options != null && defaultOption != null) {
215 for (int i=0; i< options.length; i++) {
216 if (options[i] == defaultOption) {
217 defaultButton = buttons.get(i);
218 break;
219 }
220 }
221 }
222
223 if (msg instanceof String) {
224 JosmEditorPane pane = new JosmEditorPane("text/html", (String) msg);
225 pane.setEditable(false);
226 pane.setOpaque(false);
227 msg = pane;
228 }
229
230 final JOptionPane pane = new JOptionPane(
231 msg,
232 messageType,
233 JOptionPane.DEFAULT_OPTION,
234 icon,
235 buttons.toArray(),
236 defaultButton
237 );
238
239 pane.getValue();
240 final JDialog dialog = new JDialog(
241 JOptionPane.getFrameForComponent(parentComponent),
242 title,
243 ModalityType.DOCUMENT_MODAL
244 );
245 dialog.setContentPane(pane);
246 dialog.addWindowListener(new WindowAdapter() {
247 @Override
248 public void windowClosing(WindowEvent e) {
249 pane.setValue(JOptionPane.CLOSED_OPTION);
250 super.windowClosed(e);
251 }
252
253 @Override
254 public void windowOpened(WindowEvent e) {
255 if (defaultOption != null && options != null && options.length > 0) {
256 int i;
257 for (i=0; i<options.length;i++) {
258 if (options[i] == defaultOption) {
259 break;
260 }
261 }
262 if (i >= options.length) {
263 buttons.get(0).requestFocusInWindow();
264 }
265 buttons.get(i).requestFocusInWindow();
266 } else {
267 buttons.get(0).requestFocusInWindow();
268 }
269 }
270 });
271 dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0), "close");
272 dialog.getRootPane().getActionMap().put("close", new AbstractAction() {
273 public void actionPerformed(ActionEvent e) {
274 pane.setValue(JOptionPane.CLOSED_OPTION);
275 dialog.setVisible(false);
276 }}
277 );
278
279 if (options != null) {
280 for (int i=0; i < options.length;i++) {
281 final DefaultAction action = new DefaultAction(dialog, pane, i);
282 buttons.get(i).addActionListener(action);
283 buttons.get(i).getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "enter");
284 buttons.get(i).getActionMap().put("enter", action);
285 }
286 } else {
287 final DefaultAction action = new DefaultAction(dialog, pane, 0);
288 buttons.get(0).addActionListener(action);
289 buttons.get(0).getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "enter");
290 buttons.get(0).getActionMap().put("enter", action);
291 }
292
293 dialog.pack();
294 WindowGeometry.centerOnScreen(dialog.getSize()).applySafe(dialog);
295 if (helpTopic != null) {
296 HelpUtil.setHelpContext(dialog.getRootPane(), helpTopic);
297 }
298 dialog.setVisible(true);
299 return (Integer)pane.getValue();
300 }
301
302 /**
303 * Displays an option dialog which is aware of a help context.
304 *
305 * @param parentComponent the parent component
306 * @param msg the message
307 * @param title the title
308 * @param messageType the message type (see {@link JOptionPane})
309 * @param helpTopic the help topic. Can be null.
310 * @return the index of the selected option or {@link JOptionPane#CLOSED_OPTION}
311 * @see #showOptionDialog(Component, Object, String, int, Icon, ButtonSpec[], ButtonSpec, String)
312 */
313 static public int showOptionDialog(Component parentComponent, Object msg, String title, int messageType,final String helpTopic) {
314 return showOptionDialog(parentComponent, msg, title, messageType, null,null,null, helpTopic);
315 }
316
317 /**
318 * Run it in Event Dispatch Thread.
319 * This version does not return anything, so it is more like showMessageDialog.
320 *
321 * It can be used, when you need to show a message dialog from a worker thread,
322 * e.g. from PleaseWaitRunnable
323 */
324 static public void showMessageDialogInEDT(final Component parentComponent, final Object msg, final String title, final int messageType, final String helpTopic) {
325 GuiHelper.runInEDT(new Runnable() {
326 public void run() {
327 showOptionDialog(parentComponent, msg, title, messageType, null, null, null, helpTopic);
328 }
329 });
330 }
331}
Note: See TracBrowser for help on using the repository browser.