source: josm/trunk/src/org/openstreetmap/josm/gui/OptionPaneUtil.java@ 1987

Last change on this file since 1987 was 1987, checked in by Gubaer, 15 years ago

removed all occurences of setAlwaysOnTop(). See http://josm.openstreetmap.de/wiki/JosmWinMgtDemoApplication

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