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

Last change on this file since 2646 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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