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

Last change on this file since 3734 was 3501, checked in by bastiK, 14 years ago

fixed #4632 - Button Help puts help window under main window

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