1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.HeadlessException;
|
---|
8 |
|
---|
9 | import javax.swing.Icon;
|
---|
10 | import javax.swing.JDialog;
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 | import javax.swing.UIManager;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * OptionPaneUtil provides static utility methods for displaying modal message dialogs.
|
---|
18 | *
|
---|
19 | * They wrap the methods provided by {@see JOptionPane}. Within JOSM you should use these
|
---|
20 | * methods rather than the bare methods from {@see JOptionPane} because the methods provided
|
---|
21 | * by OptionPaneUtil ensure that a dialog window is always on top and isn't hidden by one of the
|
---|
22 | * JOSM windows for detached dialogs, relation editors, history browser and the like.
|
---|
23 | *
|
---|
24 | * Typical usage for a message dialog:
|
---|
25 | * <pre>
|
---|
26 | * OptionPaneUtil.showMessageDialog(Main.parent, tr("My message"), tr("My title"), JOptionPane.WARNING_MESSAGE);
|
---|
27 | * </pre>
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public class OptionPaneUtil {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * this is static utility class, no instances allowed
|
---|
34 | */
|
---|
35 | private OptionPaneUtil () {}
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * prepares a dialog as message dialog which is always on the top of the windows
|
---|
39 | * stack
|
---|
40 | *
|
---|
41 | * @param dialog the dialog
|
---|
42 | */
|
---|
43 | static protected void prepareDialog(JDialog dialog) {
|
---|
44 |
|
---|
45 | // always on top can be disabled in a configuration option. This is necessary
|
---|
46 | // for some WM on Linux, i.e. fluxbox. There, always-on-top propagates to the
|
---|
47 | // parent window, i.e. the JOSM window itself.
|
---|
48 | //
|
---|
49 | // FIXME: this is a temporary solution. I'm still looking for an approach which
|
---|
50 | // works across all OS and WMs. Can we get rid of "always-on-top" in JOSM
|
---|
51 | // completely?
|
---|
52 | //
|
---|
53 | if (Main.pref.getBoolean("window-handling.option-pane-always-on-top", true)) {
|
---|
54 | try {
|
---|
55 | dialog.setAlwaysOnTop(true);
|
---|
56 | } catch(SecurityException e) {
|
---|
57 | System.out.println(tr("Warning: failed to put option pane dialog always on top. Exception was: {0}", e.toString()));
|
---|
58 | }
|
---|
59 | }
|
---|
60 | dialog.setModal(true);
|
---|
61 | dialog.toFront();
|
---|
62 | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Displays an message in modal dialog with an OK button. Makes sure the dialog
|
---|
67 | * is always on top even if there are other open windows like detached dialogs,
|
---|
68 | * relation editors, history browsers and the like.
|
---|
69 | *
|
---|
70 | * @param parent the parent component
|
---|
71 | * @param message the message
|
---|
72 | * @param title the title
|
---|
73 | * @param messageType the message type
|
---|
74 | *
|
---|
75 | * @see JOptionPane#INFORMATION_MESSAGE
|
---|
76 | * @see JOptionPane#WARNING_MESSAGE
|
---|
77 | * @see JOptionPane#ERROR_MESSAGE
|
---|
78 | */
|
---|
79 | static public void showMessageDialog(Component parent, Object message, String title, int messageType) {
|
---|
80 | JOptionPane op = new JOptionPane(
|
---|
81 | message,
|
---|
82 | messageType
|
---|
83 | );
|
---|
84 | JDialog dialog = op.createDialog(Main.parent, title);
|
---|
85 | prepareDialog(dialog);
|
---|
86 | dialog.setVisible(true);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Displays an confirmation dialog with two or three buttons. Makes sure the dialog
|
---|
92 | * is always on top even if there are other open windows like detached dialogs,
|
---|
93 | * relation editors, history browsers and the like.
|
---|
94 | *
|
---|
95 | * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
|
---|
96 | * a NO button.
|
---|
97 |
|
---|
98 | * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
|
---|
99 | * a NO and a CANCEL button
|
---|
100 | *
|
---|
101 | * @param parent the parent component
|
---|
102 | * @param message the message
|
---|
103 | * @param title the title
|
---|
104 | * @param optionType the option type
|
---|
105 | * @param messageType the message type
|
---|
106 | *
|
---|
107 | * @see JOptionPane#INFORMATION_MESSAGE
|
---|
108 | * @see JOptionPane#WARNING_MESSAGE
|
---|
109 | * @see JOptionPane#ERROR_MESSAGE
|
---|
110 | */
|
---|
111 | static public int showConfirmationDialog(Component parent, Object message, String title, int optionType, int messageType) throws HeadlessException {
|
---|
112 | JOptionPane op = new JOptionPane(
|
---|
113 | message,
|
---|
114 | messageType,
|
---|
115 | optionType
|
---|
116 | );
|
---|
117 | JDialog dialog = op.createDialog(Main.parent, title);
|
---|
118 | prepareDialog(dialog);
|
---|
119 | dialog.setVisible(true);
|
---|
120 | Object value = op.getValue();
|
---|
121 | if (value == null || ! (value instanceof Integer))
|
---|
122 | return JOptionPane.CLOSED_OPTION;
|
---|
123 | return (Integer)value;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Displays an confirmation dialog with some option buttons given by <code>optionType</code>.
|
---|
128 | * It is always on top even if there are other open windows like detached dialogs,
|
---|
129 | * relation editors, history browsers and the like.
|
---|
130 | *
|
---|
131 | * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
|
---|
132 | * a NO button.
|
---|
133 |
|
---|
134 | * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
|
---|
135 | * a NO and a CANCEL button
|
---|
136 | *
|
---|
137 | * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false.
|
---|
138 | *
|
---|
139 | * @param parent the parent component
|
---|
140 | * @param message the message
|
---|
141 | * @param title the title
|
---|
142 | * @param optionType the option type
|
---|
143 | * @param messageType the message type
|
---|
144 | * @param trueOption if this option is selected the method replies true
|
---|
145 | *
|
---|
146 | *
|
---|
147 | * @return true, if the selected option is equal to <code>trueOption</code>, otherwise false.
|
---|
148 | *
|
---|
149 | * @see JOptionPane#INFORMATION_MESSAGE
|
---|
150 | * @see JOptionPane#WARNING_MESSAGE
|
---|
151 | * @see JOptionPane#ERROR_MESSAGE
|
---|
152 | */
|
---|
153 | static public boolean showConfirmationDialog(Component parent, Object message, String title, int optionType, int messageType, int trueOption) throws HeadlessException {
|
---|
154 | int ret = showConfirmationDialog(parent, message, title, optionType, messageType);
|
---|
155 | return ret == trueOption;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Displays an confirmation dialog with three buttons: YES, NO and CANCEL. Makes sure the dialog
|
---|
160 | * is always on top even if there are other open windows like detached dialogs,
|
---|
161 | * relation editors, history browsers and the like. The dialog is of type {@see JOptionPane#QUESTION_MESSAGE}
|
---|
162 | *
|
---|
163 | * @param parent the parent component
|
---|
164 | * @param message the message
|
---|
165 | * @param title the title
|
---|
166 | *
|
---|
167 | */
|
---|
168 | static public int showConfirmationDialog(Component parent, Object message, String title) throws HeadlessException {
|
---|
169 | return showConfirmationDialog(parent, message, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Displays a confirmation dialog with arbitrary confirmation options. Makes sure the dialog
|
---|
174 | * is always on top even if there are other open windows like detached dialogs,
|
---|
175 | * relation editors, history browsers and the like.
|
---|
176 | *
|
---|
177 | * Provide an array of option labels in <code>options</code>, i.e.
|
---|
178 | * <pre>
|
---|
179 | * options = new String[] {"Yes, do it!", "No, not yet", "Abort"};
|
---|
180 | * </pre>
|
---|
181 | *
|
---|
182 | * Replies the index of the selected option or {@see JOptionPane#CLOSED_OPTION} if
|
---|
183 | * the dialog was closed.
|
---|
184 | *
|
---|
185 | * @param parent the parent component
|
---|
186 | * @param message the message
|
---|
187 | * @param title the title
|
---|
188 | * @param optionType
|
---|
189 | * @param messageType
|
---|
190 | * @param options
|
---|
191 | * @param initialValue
|
---|
192 | * @return the index of the selected option or {@see JOptionPane#CLOSED_OPTION}
|
---|
193 | */
|
---|
194 | static public int showOptionDialog(Component parent,
|
---|
195 | Object message,
|
---|
196 | String title,
|
---|
197 | int optionType,
|
---|
198 | int messageType,
|
---|
199 | Object[] options,
|
---|
200 | Object initialValue) {
|
---|
201 |
|
---|
202 | JOptionPane op = new JOptionPane(
|
---|
203 | message,
|
---|
204 | messageType,
|
---|
205 | optionType,
|
---|
206 | null /* no icon */,
|
---|
207 | options,
|
---|
208 | initialValue
|
---|
209 | );
|
---|
210 | JDialog dialog = op.createDialog(Main.parent, title);
|
---|
211 | prepareDialog(dialog);
|
---|
212 | dialog.setVisible(true);
|
---|
213 | Object value = op.getValue();
|
---|
214 | if (value == null)
|
---|
215 | return JOptionPane.CLOSED_OPTION;
|
---|
216 | if (options == null) {
|
---|
217 | if (value instanceof Integer)
|
---|
218 | return (Integer)value;
|
---|
219 | return JOptionPane.CLOSED_OPTION;
|
---|
220 | }
|
---|
221 | for(int i = 0; i < options.length; i++) {
|
---|
222 | if(options[i].equals(value))
|
---|
223 | return i;
|
---|
224 | }
|
---|
225 | return JOptionPane.CLOSED_OPTION;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Shows a dialog requesting input from the user parented to
|
---|
230 | * <code>parentComponent</code> with the dialog having the title
|
---|
231 | * <code>title</code> and message type <code>messageType</code>.
|
---|
232 | *
|
---|
233 | * @param parentComponent the parent <code>Component</code> for the
|
---|
234 | * dialog
|
---|
235 | * @param message the <code>Object</code> to display
|
---|
236 | * @param title the <code>String</code> to display in the dialog
|
---|
237 | * title bar
|
---|
238 | * @param messageType the type of message that is to be displayed:
|
---|
239 | * <code>ERROR_MESSAGE</code>,
|
---|
240 | * <code>INFORMATION_MESSAGE</code>,
|
---|
241 | * <code>WARNING_MESSAGE</code>,
|
---|
242 | * <code>QUESTION_MESSAGE</code>,
|
---|
243 | * or <code>PLAIN_MESSAGE</code>
|
---|
244 | * @exception HeadlessException if
|
---|
245 | * <code>GraphicsEnvironment.isHeadless</code> returns
|
---|
246 | * <code>true</code>
|
---|
247 | * @see java.awt.GraphicsEnvironment#isHeadless
|
---|
248 | */
|
---|
249 | public static String showInputDialog(Component parentComponent,
|
---|
250 | Object message, String title, int messageType)
|
---|
251 | throws HeadlessException {
|
---|
252 | return (String)showInputDialog(parentComponent, message, title,
|
---|
253 | messageType, null, null, null);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Prompts the user for input in a blocking dialog where the
|
---|
258 | * initial selection, possible selections, and all other options can
|
---|
259 | * be specified. The user will able to choose from
|
---|
260 | * <code>selectionValues</code>, where <code>null</code> implies the
|
---|
261 | * user can input
|
---|
262 | * whatever they wish, usually by means of a <code>JTextField</code>.
|
---|
263 | * <code>initialSelectionValue</code> is the initial value to prompt
|
---|
264 | * the user with. It is up to the UI to decide how best to represent
|
---|
265 | * the <code>selectionValues</code>, but usually a
|
---|
266 | * <code>JComboBox</code>, <code>JList</code>, or
|
---|
267 | * <code>JTextField</code> will be used.
|
---|
268 | *
|
---|
269 | * @param parentComponent the parent <code>Component</code> for the
|
---|
270 | * dialog
|
---|
271 | * @param message the <code>Object</code> to display
|
---|
272 | * @param title the <code>String</code> to display in the
|
---|
273 | * dialog title bar
|
---|
274 | * @param messageType the type of message to be displayed:
|
---|
275 | * <code>ERROR_MESSAGE</code>,
|
---|
276 | * <code>INFORMATION_MESSAGE</code>,
|
---|
277 | * <code>WARNING_MESSAGE</code>,
|
---|
278 | * <code>QUESTION_MESSAGE</code>,
|
---|
279 | * or <code>PLAIN_MESSAGE</code>
|
---|
280 | * @param icon the <code>Icon</code> image to display
|
---|
281 | * @param selectionValues an array of <code>Object</code>s that
|
---|
282 | * gives the possible selections
|
---|
283 | * @param initialSelectionValue the value used to initialize the input
|
---|
284 | * field
|
---|
285 | * @return user's input, or <code>null</code> meaning the user
|
---|
286 | * canceled the input
|
---|
287 | * @exception HeadlessException if
|
---|
288 | * <code>GraphicsEnvironment.isHeadless</code> returns
|
---|
289 | * <code>true</code>
|
---|
290 | * @see java.awt.GraphicsEnvironment#isHeadless
|
---|
291 | */
|
---|
292 | public static Object showInputDialog(Component parentComponent,
|
---|
293 | Object message, String title, int messageType, Icon icon,
|
---|
294 | Object[] selectionValues, Object initialSelectionValue)
|
---|
295 | throws HeadlessException {
|
---|
296 | JOptionPane pane = new JOptionPane(message, messageType,
|
---|
297 | JOptionPane.OK_CANCEL_OPTION, icon,
|
---|
298 | null, null);
|
---|
299 |
|
---|
300 | pane.setWantsInput(true);
|
---|
301 | pane.setSelectionValues(selectionValues);
|
---|
302 | pane.setInitialSelectionValue(initialSelectionValue);
|
---|
303 | pane.setComponentOrientation(((parentComponent == null) ?
|
---|
304 | JOptionPane.getRootFrame() : parentComponent).getComponentOrientation());
|
---|
305 |
|
---|
306 | JDialog dialog = pane.createDialog(parentComponent, title);
|
---|
307 | // this shows the dialog always on top and brings it to front
|
---|
308 | //
|
---|
309 | prepareDialog(dialog);
|
---|
310 | pane.selectInitialValue();
|
---|
311 | dialog.setVisible(true);
|
---|
312 | dialog.dispose();
|
---|
313 |
|
---|
314 | Object value = pane.getInputValue();
|
---|
315 | if (value == JOptionPane.UNINITIALIZED_VALUE)
|
---|
316 | return null;
|
---|
317 | return value;
|
---|
318 | }
|
---|
319 | }
|
---|