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

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

see #4429 - Right click menu "undo, cut, copy, paste, delete, select all" for each text component (originally based on patch by NooN)

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