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

Last change on this file since 4936 was 4816, checked in by simon04, 12 years ago

fix #7257, fix #4093 - Provide an option to automatically download elements after a reference error

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