author | rupashka |
Tue, 26 Aug 2008 15:12:54 +0400 | |
changeset 1301 | 15e81207e1f2 |
parent 715 | f16baef3a20e |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.swing; |
|
27 |
||
28 |
import java.awt.BorderLayout; |
|
29 |
import java.awt.Component; |
|
30 |
import java.awt.Container; |
|
31 |
import java.awt.Dialog; |
|
32 |
import java.awt.Dimension; |
|
33 |
import java.awt.KeyboardFocusManager; |
|
34 |
import java.awt.Frame; |
|
35 |
import java.awt.Point; |
|
36 |
import java.awt.HeadlessException; |
|
37 |
import java.awt.Toolkit; |
|
38 |
import java.awt.Window; |
|
39 |
import java.beans.PropertyChangeEvent; |
|
40 |
import java.beans.PropertyChangeListener; |
|
41 |
import java.awt.event.WindowListener; |
|
42 |
import java.awt.event.WindowAdapter; |
|
43 |
import java.awt.event.WindowEvent; |
|
44 |
import java.awt.event.ComponentAdapter; |
|
45 |
import java.awt.event.ComponentEvent; |
|
46 |
import java.io.IOException; |
|
47 |
import java.io.ObjectInputStream; |
|
48 |
import java.io.ObjectOutputStream; |
|
49 |
import java.io.Serializable; |
|
50 |
import java.lang.reflect.Method; |
|
51 |
import java.lang.reflect.InvocationTargetException; |
|
52 |
import java.security.AccessController; |
|
53 |
import java.security.PrivilegedAction; |
|
54 |
import java.util.Vector; |
|
55 |
import javax.swing.plaf.OptionPaneUI; |
|
56 |
import javax.swing.event.InternalFrameEvent; |
|
57 |
import javax.swing.event.InternalFrameAdapter; |
|
58 |
import javax.accessibility.*; |
|
59 |
import static javax.swing.ClientPropertyKey.PopupFactory_FORCE_HEAVYWEIGHT_POPUP; |
|
60 |
||
61 |
/** |
|
62 |
* <code>JOptionPane</code> makes it easy to pop up a standard dialog box that |
|
63 |
* prompts users for a value or informs them of something. |
|
64 |
* For information about using <code>JOptionPane</code>, see |
|
65 |
* <a |
|
66 |
href="http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html">How to Make Dialogs</a>, |
|
67 |
* a section in <em>The Java Tutorial</em>. |
|
68 |
* |
|
69 |
* <p> |
|
70 |
* |
|
71 |
* While the <code>JOptionPane</code> |
|
72 |
* class may appear complex because of the large number of methods, almost |
|
73 |
* all uses of this class are one-line calls to one of the static |
|
74 |
* <code>showXxxDialog</code> methods shown below: |
|
75 |
* <blockquote> |
|
76 |
* |
|
77 |
* |
|
78 |
* <table border=1 summary="Common JOptionPane method names and their descriptions"> |
|
79 |
* <tr> |
|
80 |
* <th>Method Name</th> |
|
81 |
* <th>Description</th> |
|
82 |
* </tr> |
|
83 |
* <tr> |
|
84 |
* <td>showConfirmDialog</td> |
|
85 |
* <td>Asks a confirming question, like yes/no/cancel.</td> |
|
86 |
* </tr> |
|
87 |
* <tr> |
|
88 |
* <td>showInputDialog</td> |
|
89 |
* <td>Prompt for some input.</td> |
|
90 |
* </tr> |
|
91 |
* <tr> |
|
92 |
* <td>showMessageDialog</td> |
|
93 |
* <td>Tell the user about something that has happened.</td> |
|
94 |
* </tr> |
|
95 |
* <tr> |
|
96 |
* <td>showOptionDialog</td> |
|
97 |
* <td>The Grand Unification of the above three.</td> |
|
98 |
* </tr> |
|
99 |
* </table> |
|
100 |
* |
|
101 |
* </blockquote> |
|
102 |
* Each of these methods also comes in a <code>showInternalXXX</code> |
|
103 |
* flavor, which uses an internal frame to hold the dialog box (see |
|
104 |
* {@link JInternalFrame}). |
|
105 |
* Multiple convenience methods have also been defined -- overloaded |
|
106 |
* versions of the basic methods that use different parameter lists. |
|
107 |
* <p> |
|
108 |
* All dialogs are modal. Each <code>showXxxDialog</code> method blocks |
|
109 |
* the caller until the user's interaction is complete. |
|
110 |
* <p> |
|
111 |
* |
|
112 |
* <table cellspacing=6 cellpadding=4 border=0 align=right summary="layout"> |
|
113 |
* <tr> |
|
114 |
* <td bgcolor=#FFe0d0 rowspan=2>icon</td> |
|
115 |
* <td bgcolor=#FFe0d0>message</td> |
|
116 |
* </tr> |
|
117 |
* <tr> |
|
118 |
* <td bgcolor=#FFe0d0>input value</td> |
|
119 |
* </tr> |
|
120 |
* <tr> |
|
121 |
* <td bgcolor=#FFe0d0 colspan=2>option buttons</td> |
|
122 |
* </tr> |
|
123 |
* </table> |
|
124 |
* |
|
125 |
* The basic appearance of one of these dialog boxes is generally |
|
126 |
* similar to the picture at the right, although the various |
|
127 |
* look-and-feels are |
|
128 |
* ultimately responsible for the final result. In particular, the |
|
129 |
* look-and-feels will adjust the layout to accommodate the option pane's |
|
130 |
* <code>ComponentOrientation</code> property. |
|
131 |
* <br clear=all> |
|
132 |
* <p> |
|
133 |
* <b>Parameters:</b><br> |
|
134 |
* The parameters to these methods follow consistent patterns: |
|
135 |
* <blockquote> |
|
136 |
* <dl compact> |
|
137 |
* <dt>parentComponent<dd> |
|
138 |
* Defines the <code>Component</code> that is to be the parent of this |
|
139 |
* dialog box. |
|
140 |
* It is used in two ways: the <code>Frame</code> that contains |
|
141 |
* it is used as the <code>Frame</code> |
|
142 |
* parent for the dialog box, and its screen coordinates are used in |
|
143 |
* the placement of the dialog box. In general, the dialog box is placed |
|
144 |
* just below the component. This parameter may be <code>null</code>, |
|
145 |
* in which case a default <code>Frame</code> is used as the parent, |
|
146 |
* and the dialog will be |
|
147 |
* centered on the screen (depending on the L&F). |
|
148 |
* <dt><a name=message>message</a><dd> |
|
149 |
* A descriptive message to be placed in the dialog box. |
|
150 |
* In the most common usage, message is just a <code>String</code> or |
|
151 |
* <code>String</code> constant. |
|
152 |
* However, the type of this parameter is actually <code>Object</code>. Its |
|
153 |
* interpretation depends on its type: |
|
154 |
* <dl compact> |
|
155 |
* <dt>Object[]<dd>An array of objects is interpreted as a series of |
|
156 |
* messages (one per object) arranged in a vertical stack. |
|
157 |
* The interpretation is recursive -- each object in the |
|
158 |
* array is interpreted according to its type. |
|
159 |
* <dt>Component<dd>The <code>Component</code> is displayed in the dialog. |
|
160 |
* <dt>Icon<dd>The <code>Icon</code> is wrapped in a <code>JLabel</code> |
|
161 |
* and displayed in the dialog. |
|
162 |
* <dt>others<dd>The object is converted to a <code>String</code> by calling |
|
163 |
* its <code>toString</code> method. The result is wrapped in a |
|
164 |
* <code>JLabel</code> and displayed. |
|
165 |
* </dl> |
|
166 |
* <dt>messageType<dd>Defines the style of the message. The Look and Feel |
|
167 |
* manager may lay out the dialog differently depending on this value, and |
|
168 |
* will often provide a default icon. The possible values are: |
|
169 |
* <ul> |
|
170 |
* <li><code>ERROR_MESSAGE</code> |
|
171 |
* <li><code>INFORMATION_MESSAGE</code> |
|
172 |
* <li><code>WARNING_MESSAGE</code> |
|
173 |
* <li><code>QUESTION_MESSAGE</code> |
|
174 |
* <li><code>PLAIN_MESSAGE</code> |
|
175 |
* </ul> |
|
176 |
* <dt>optionType<dd>Defines the set of option buttons that appear at |
|
177 |
* the bottom of the dialog box: |
|
178 |
* <ul> |
|
179 |
* <li><code>DEFAULT_OPTION</code> |
|
180 |
* <li><code>YES_NO_OPTION</code> |
|
181 |
* <li><code>YES_NO_CANCEL_OPTION</code> |
|
182 |
* <li><code>OK_CANCEL_OPTION</code> |
|
183 |
* </ul> |
|
184 |
* You aren't limited to this set of option buttons. You can provide any |
|
185 |
* buttons you want using the options parameter. |
|
186 |
* <dt>options<dd>A more detailed description of the set of option buttons |
|
187 |
* that will appear at the bottom of the dialog box. |
|
188 |
* The usual value for the options parameter is an array of |
|
189 |
* <code>String</code>s. But |
|
190 |
* the parameter type is an array of <code>Objects</code>. |
|
191 |
* A button is created for each object depending on its type: |
|
192 |
* <dl compact> |
|
193 |
* <dt>Component<dd>The component is added to the button row directly. |
|
194 |
* <dt>Icon<dd>A <code>JButton</code> is created with this as its label. |
|
195 |
* <dt>other<dd>The <code>Object</code> is converted to a string using its |
|
196 |
* <code>toString</code> method and the result is used to |
|
197 |
* label a <code>JButton</code>. |
|
198 |
* </dl> |
|
199 |
* <dt>icon<dd>A decorative icon to be placed in the dialog box. A default |
|
200 |
* value for this is determined by the <code>messageType</code> parameter. |
|
201 |
* <dt>title<dd>The title for the dialog box. |
|
202 |
* <dt>initialValue<dd>The default selection (input value). |
|
203 |
* </dl> |
|
204 |
* </blockquote> |
|
205 |
* <p> |
|
206 |
* When the selection is changed, <code>setValue</code> is invoked, |
|
207 |
* which generates a <code>PropertyChangeEvent</code>. |
|
208 |
* <p> |
|
209 |
* If a <code>JOptionPane</code> has configured to all input |
|
210 |
* <code>setWantsInput</code> |
|
211 |
* the bound property <code>JOptionPane.INPUT_VALUE_PROPERTY</code> |
|
212 |
* can also be listened |
|
213 |
* to, to determine when the user has input or selected a value. |
|
214 |
* <p> |
|
215 |
* When one of the <code>showXxxDialog</code> methods returns an integer, |
|
216 |
* the possible values are: |
|
217 |
* <ul> |
|
218 |
* <li><code>YES_OPTION</code> |
|
219 |
* <li><code>NO_OPTION</code> |
|
220 |
* <li><code>CANCEL_OPTION</code> |
|
221 |
* <li><code>OK_OPTION</code> |
|
222 |
* <li><code>CLOSED_OPTION</code> |
|
223 |
* </ul> |
|
224 |
* <b>Examples:</b> |
|
225 |
* <dl> |
|
226 |
* <dt>Show an error dialog that displays the message, 'alert': |
|
227 |
* <dd><code> |
|
228 |
* JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE); |
|
229 |
* </code><p> |
|
230 |
* <dt>Show an internal information dialog with the message, 'information': |
|
231 |
* <dd><code> |
|
232 |
* JOptionPane.showInternalMessageDialog(frame, "information",<br> |
|
233 |
* <ul><ul>"information", JOptionPane.INFORMATION_MESSAGE);</ul></ul> |
|
234 |
* </code><p> |
|
235 |
* <dt>Show an information panel with the options yes/no and message 'choose one': |
|
236 |
* <dd><code>JOptionPane.showConfirmDialog(null, |
|
237 |
* <ul><ul>"choose one", "choose one", JOptionPane.YES_NO_OPTION);</ul></ul> |
|
238 |
* </code><p> |
|
239 |
* <dt>Show an internal information dialog with the options yes/no/cancel and |
|
240 |
* message 'please choose one' and title information: |
|
241 |
* <dd><code>JOptionPane.showInternalConfirmDialog(frame, |
|
242 |
* <ul><ul>"please choose one", "information",</ul></ul> |
|
243 |
* <ul><ul>JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);</ul></ul> |
|
244 |
* </code><p> |
|
245 |
* <dt>Show a warning dialog with the options OK, CANCEL, title 'Warning', and |
|
246 |
* message 'Click OK to continue': |
|
247 |
* <dd><code> |
|
248 |
* Object[] options = { "OK", "CANCEL" };<br> |
|
249 |
* JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning", |
|
250 |
* <ul><ul>JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,</ul></ul> |
|
251 |
* <ul><ul>null, options, options[0]);</ul></ul> |
|
252 |
* </code><p> |
|
253 |
* <dt>Show a dialog asking the user to type in a String: |
|
254 |
* <dd><code> |
|
255 |
* String inputValue = JOptionPane.showInputDialog("Please input a value"); |
|
256 |
* </code><p> |
|
257 |
* <dt>Show a dialog asking the user to select a String: |
|
258 |
* <dd><code> |
|
259 |
* Object[] possibleValues = { "First", "Second", "Third" };<br> |
|
260 |
* Object selectedValue = JOptionPane.showInputDialog(null, |
|
261 |
* <ul><ul>"Choose one", "Input",</ul></ul> |
|
262 |
* <ul><ul>JOptionPane.INFORMATION_MESSAGE, null,</ul></ul> |
|
263 |
* <ul><ul>possibleValues, possibleValues[0]);</ul></ul> |
|
264 |
* </code><p> |
|
265 |
* </dl> |
|
266 |
* <b>Direct Use:</b><br> |
|
267 |
* To create and use an <code>JOptionPane</code> directly, the |
|
268 |
* standard pattern is roughly as follows: |
|
269 |
* <pre> |
|
270 |
* JOptionPane pane = new JOptionPane(<i>arguments</i>); |
|
271 |
* pane.set<i>.Xxxx(...); // Configure</i> |
|
272 |
* JDialog dialog = pane.createDialog(<i>parentComponent, title</i>); |
|
273 |
* dialog.show(); |
|
274 |
* Object selectedValue = pane.getValue(); |
|
275 |
* if(selectedValue == null) |
|
276 |
* return CLOSED_OPTION; |
|
277 |
* <i>//If there is <b>not</b> an array of option buttons:</i> |
|
278 |
* if(options == null) { |
|
279 |
* if(selectedValue instanceof Integer) |
|
280 |
* return ((Integer)selectedValue).intValue(); |
|
281 |
* return CLOSED_OPTION; |
|
282 |
* } |
|
283 |
* <i>//If there is an array of option buttons:</i> |
|
284 |
* for(int counter = 0, maxCounter = options.length; |
|
285 |
* counter < maxCounter; counter++) { |
|
286 |
* if(options[counter].equals(selectedValue)) |
|
287 |
* return counter; |
|
288 |
* } |
|
289 |
* return CLOSED_OPTION; |
|
290 |
* </pre> |
|
291 |
* <p> |
|
292 |
* <strong>Warning:</strong> Swing is not thread safe. For more |
|
293 |
* information see <a |
|
294 |
* href="package-summary.html#threading">Swing's Threading |
|
295 |
* Policy</a>. |
|
296 |
* <p> |
|
297 |
* <strong>Warning:</strong> |
|
298 |
* Serialized objects of this class will not be compatible with |
|
299 |
* future Swing releases. The current serialization support is |
|
300 |
* appropriate for short term storage or RMI between applications running |
|
301 |
* the same version of Swing. As of 1.4, support for long term storage |
|
302 |
* of all JavaBeans<sup><font size="-2">TM</font></sup> |
|
303 |
* has been added to the <code>java.beans</code> package. |
|
304 |
* Please see {@link java.beans.XMLEncoder}. |
|
305 |
* |
|
306 |
* @see JInternalFrame |
|
307 |
* |
|
308 |
* @beaninfo |
|
309 |
* attribute: isContainer true |
|
310 |
* description: A component which implements standard dialog box controls. |
|
311 |
* |
|
312 |
* @author James Gosling |
|
313 |
* @author Scott Violet |
|
314 |
*/ |
|
315 |
public class JOptionPane extends JComponent implements Accessible |
|
316 |
{ |
|
317 |
/** |
|
318 |
* @see #getUIClassID |
|
319 |
* @see #readObject |
|
320 |
*/ |
|
321 |
private static final String uiClassID = "OptionPaneUI"; |
|
322 |
||
323 |
/** |
|
324 |
* Indicates that the user has not yet selected a value. |
|
325 |
*/ |
|
326 |
public static final Object UNINITIALIZED_VALUE = "uninitializedValue"; |
|
327 |
||
328 |
// |
|
329 |
// Option types |
|
330 |
// |
|
331 |
||
332 |
/** |
|
333 |
* Type meaning Look and Feel should not supply any options -- only |
|
334 |
* use the options from the <code>JOptionPane</code>. |
|
335 |
*/ |
|
336 |
public static final int DEFAULT_OPTION = -1; |
|
337 |
/** Type used for <code>showConfirmDialog</code>. */ |
|
338 |
public static final int YES_NO_OPTION = 0; |
|
339 |
/** Type used for <code>showConfirmDialog</code>. */ |
|
340 |
public static final int YES_NO_CANCEL_OPTION = 1; |
|
341 |
/** Type used for <code>showConfirmDialog</code>. */ |
|
342 |
public static final int OK_CANCEL_OPTION = 2; |
|
343 |
||
344 |
// |
|
345 |
// Return values. |
|
346 |
// |
|
347 |
/** Return value from class method if YES is chosen. */ |
|
348 |
public static final int YES_OPTION = 0; |
|
349 |
/** Return value from class method if NO is chosen. */ |
|
350 |
public static final int NO_OPTION = 1; |
|
351 |
/** Return value from class method if CANCEL is chosen. */ |
|
352 |
public static final int CANCEL_OPTION = 2; |
|
353 |
/** Return value form class method if OK is chosen. */ |
|
354 |
public static final int OK_OPTION = 0; |
|
355 |
/** Return value from class method if user closes window without selecting |
|
356 |
* anything, more than likely this should be treated as either a |
|
357 |
* <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */ |
|
358 |
public static final int CLOSED_OPTION = -1; |
|
359 |
||
360 |
// |
|
361 |
// Message types. Used by the UI to determine what icon to display, |
|
362 |
// and possibly what behavior to give based on the type. |
|
363 |
// |
|
364 |
/** Used for error messages. */ |
|
365 |
public static final int ERROR_MESSAGE = 0; |
|
366 |
/** Used for information messages. */ |
|
367 |
public static final int INFORMATION_MESSAGE = 1; |
|
368 |
/** Used for warning messages. */ |
|
369 |
public static final int WARNING_MESSAGE = 2; |
|
370 |
/** Used for questions. */ |
|
371 |
public static final int QUESTION_MESSAGE = 3; |
|
372 |
/** No icon is used. */ |
|
373 |
public static final int PLAIN_MESSAGE = -1; |
|
374 |
||
375 |
/** Bound property name for <code>icon</code>. */ |
|
376 |
public static final String ICON_PROPERTY = "icon"; |
|
377 |
/** Bound property name for <code>message</code>. */ |
|
378 |
public static final String MESSAGE_PROPERTY = "message"; |
|
379 |
/** Bound property name for <code>value</code>. */ |
|
380 |
public static final String VALUE_PROPERTY = "value"; |
|
381 |
/** Bound property name for <code>option</code>. */ |
|
382 |
public static final String OPTIONS_PROPERTY = "options"; |
|
383 |
/** Bound property name for <code>initialValue</code>. */ |
|
384 |
public static final String INITIAL_VALUE_PROPERTY = "initialValue"; |
|
385 |
/** Bound property name for <code>type</code>. */ |
|
386 |
public static final String MESSAGE_TYPE_PROPERTY = "messageType"; |
|
387 |
/** Bound property name for <code>optionType</code>. */ |
|
388 |
public static final String OPTION_TYPE_PROPERTY = "optionType"; |
|
389 |
/** Bound property name for <code>selectionValues</code>. */ |
|
390 |
public static final String SELECTION_VALUES_PROPERTY = "selectionValues"; |
|
391 |
/** Bound property name for <code>initialSelectionValue</code>. */ |
|
392 |
public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue"; |
|
393 |
/** Bound property name for <code>inputValue</code>. */ |
|
394 |
public static final String INPUT_VALUE_PROPERTY = "inputValue"; |
|
395 |
/** Bound property name for <code>wantsInput</code>. */ |
|
396 |
public static final String WANTS_INPUT_PROPERTY = "wantsInput"; |
|
397 |
||
398 |
/** Icon used in pane. */ |
|
399 |
transient protected Icon icon; |
|
400 |
/** Message to display. */ |
|
401 |
transient protected Object message; |
|
402 |
/** Options to display to the user. */ |
|
403 |
transient protected Object[] options; |
|
404 |
/** Value that should be initially selected in <code>options</code>. */ |
|
405 |
transient protected Object initialValue; |
|
406 |
/** Message type. */ |
|
407 |
protected int messageType; |
|
408 |
/** |
|
409 |
* Option type, one of <code>DEFAULT_OPTION</code>, |
|
410 |
* <code>YES_NO_OPTION</code>, |
|
411 |
* <code>YES_NO_CANCEL_OPTION</code> or |
|
412 |
* <code>OK_CANCEL_OPTION</code>. |
|
413 |
*/ |
|
414 |
protected int optionType; |
|
415 |
/** Currently selected value, will be a valid option, or |
|
416 |
* <code>UNINITIALIZED_VALUE</code> or <code>null</code>. */ |
|
417 |
transient protected Object value; |
|
418 |
/** Array of values the user can choose from. Look and feel will |
|
419 |
* provide the UI component to choose this from. */ |
|
420 |
protected transient Object[] selectionValues; |
|
421 |
/** Value the user has input. */ |
|
422 |
protected transient Object inputValue; |
|
423 |
/** Initial value to select in <code>selectionValues</code>. */ |
|
424 |
protected transient Object initialSelectionValue; |
|
425 |
/** If true, a UI widget will be provided to the user to get input. */ |
|
426 |
protected boolean wantsInput; |
|
427 |
||
428 |
||
429 |
/** |
|
430 |
* Shows a question-message dialog requesting input from the user. The |
|
431 |
* dialog uses the default frame, which usually means it is centered on |
|
432 |
* the screen. |
|
433 |
* |
|
434 |
* @param message the <code>Object</code> to display |
|
435 |
* @exception HeadlessException if |
|
436 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
437 |
* <code>true</code> |
|
438 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
439 |
*/ |
|
440 |
public static String showInputDialog(Object message) |
|
441 |
throws HeadlessException { |
|
442 |
return showInputDialog(null, message); |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* Shows a question-message dialog requesting input from the user, with |
|
447 |
* the input value initialized to <code>initialSelectionValue</code>. The |
|
448 |
* dialog uses the default frame, which usually means it is centered on |
|
449 |
* the screen. |
|
450 |
* |
|
451 |
* @param message the <code>Object</code> to display |
|
452 |
* @param initialSelectionValue the value used to initialize the input |
|
453 |
* field |
|
454 |
* @since 1.4 |
|
455 |
*/ |
|
456 |
public static String showInputDialog(Object message, Object initialSelectionValue) { |
|
457 |
return showInputDialog(null, message, initialSelectionValue); |
|
458 |
} |
|
459 |
||
460 |
/** |
|
461 |
* Shows a question-message dialog requesting input from the user |
|
462 |
* parented to <code>parentComponent</code>. |
|
463 |
* The dialog is displayed on top of the <code>Component</code>'s |
|
464 |
* frame, and is usually positioned below the <code>Component</code>. |
|
465 |
* |
|
466 |
* @param parentComponent the parent <code>Component</code> for the |
|
467 |
* dialog |
|
468 |
* @param message the <code>Object</code> to display |
|
469 |
* @exception HeadlessException if |
|
470 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
471 |
* <code>true</code> |
|
472 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
473 |
*/ |
|
474 |
public static String showInputDialog(Component parentComponent, |
|
475 |
Object message) throws HeadlessException { |
|
476 |
return showInputDialog(parentComponent, message, UIManager.getString( |
|
477 |
"OptionPane.inputDialogTitle", parentComponent), QUESTION_MESSAGE); |
|
478 |
} |
|
479 |
||
480 |
/** |
|
481 |
* Shows a question-message dialog requesting input from the user and |
|
482 |
* parented to <code>parentComponent</code>. The input value will be |
|
483 |
* initialized to <code>initialSelectionValue</code>. |
|
484 |
* The dialog is displayed on top of the <code>Component</code>'s |
|
485 |
* frame, and is usually positioned below the <code>Component</code>. |
|
486 |
* |
|
487 |
* @param parentComponent the parent <code>Component</code> for the |
|
488 |
* dialog |
|
489 |
* @param message the <code>Object</code> to display |
|
490 |
* @param initialSelectionValue the value used to initialize the input |
|
491 |
* field |
|
492 |
* @since 1.4 |
|
493 |
*/ |
|
494 |
public static String showInputDialog(Component parentComponent, Object message, |
|
495 |
Object initialSelectionValue) { |
|
496 |
return (String)showInputDialog(parentComponent, message, |
|
497 |
UIManager.getString("OptionPane.inputDialogTitle", |
|
498 |
parentComponent), QUESTION_MESSAGE, null, null, |
|
499 |
initialSelectionValue); |
|
500 |
} |
|
501 |
||
502 |
/** |
|
503 |
* Shows a dialog requesting input from the user parented to |
|
504 |
* <code>parentComponent</code> with the dialog having the title |
|
505 |
* <code>title</code> and message type <code>messageType</code>. |
|
506 |
* |
|
507 |
* @param parentComponent the parent <code>Component</code> for the |
|
508 |
* dialog |
|
509 |
* @param message the <code>Object</code> to display |
|
510 |
* @param title the <code>String</code> to display in the dialog |
|
511 |
* title bar |
|
512 |
* @param messageType the type of message that is to be displayed: |
|
513 |
* <code>ERROR_MESSAGE</code>, |
|
514 |
* <code>INFORMATION_MESSAGE</code>, |
|
515 |
* <code>WARNING_MESSAGE</code>, |
|
516 |
* <code>QUESTION_MESSAGE</code>, |
|
517 |
* or <code>PLAIN_MESSAGE</code> |
|
518 |
* @exception HeadlessException if |
|
519 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
520 |
* <code>true</code> |
|
521 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
522 |
*/ |
|
523 |
public static String showInputDialog(Component parentComponent, |
|
524 |
Object message, String title, int messageType) |
|
525 |
throws HeadlessException { |
|
526 |
return (String)showInputDialog(parentComponent, message, title, |
|
527 |
messageType, null, null, null); |
|
528 |
} |
|
529 |
||
530 |
/** |
|
531 |
* Prompts the user for input in a blocking dialog where the |
|
532 |
* initial selection, possible selections, and all other options can |
|
533 |
* be specified. The user will able to choose from |
|
534 |
* <code>selectionValues</code>, where <code>null</code> implies the |
|
535 |
* user can input |
|
536 |
* whatever they wish, usually by means of a <code>JTextField</code>. |
|
537 |
* <code>initialSelectionValue</code> is the initial value to prompt |
|
538 |
* the user with. It is up to the UI to decide how best to represent |
|
539 |
* the <code>selectionValues</code>, but usually a |
|
540 |
* <code>JComboBox</code>, <code>JList</code>, or |
|
541 |
* <code>JTextField</code> will be used. |
|
542 |
* |
|
543 |
* @param parentComponent the parent <code>Component</code> for the |
|
544 |
* dialog |
|
545 |
* @param message the <code>Object</code> to display |
|
546 |
* @param title the <code>String</code> to display in the |
|
547 |
* dialog title bar |
|
548 |
* @param messageType the type of message to be displayed: |
|
549 |
* <code>ERROR_MESSAGE</code>, |
|
550 |
* <code>INFORMATION_MESSAGE</code>, |
|
551 |
* <code>WARNING_MESSAGE</code>, |
|
552 |
* <code>QUESTION_MESSAGE</code>, |
|
553 |
* or <code>PLAIN_MESSAGE</code> |
|
554 |
* @param icon the <code>Icon</code> image to display |
|
555 |
* @param selectionValues an array of <code>Object</code>s that |
|
556 |
* gives the possible selections |
|
557 |
* @param initialSelectionValue the value used to initialize the input |
|
558 |
* field |
|
559 |
* @return user's input, or <code>null</code> meaning the user |
|
560 |
* canceled the input |
|
561 |
* @exception HeadlessException if |
|
562 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
563 |
* <code>true</code> |
|
564 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
565 |
*/ |
|
566 |
public static Object showInputDialog(Component parentComponent, |
|
567 |
Object message, String title, int messageType, Icon icon, |
|
568 |
Object[] selectionValues, Object initialSelectionValue) |
|
569 |
throws HeadlessException { |
|
570 |
JOptionPane pane = new JOptionPane(message, messageType, |
|
571 |
OK_CANCEL_OPTION, icon, |
|
572 |
null, null); |
|
573 |
||
574 |
pane.setWantsInput(true); |
|
575 |
pane.setSelectionValues(selectionValues); |
|
576 |
pane.setInitialSelectionValue(initialSelectionValue); |
|
577 |
pane.setComponentOrientation(((parentComponent == null) ? |
|
578 |
getRootFrame() : parentComponent).getComponentOrientation()); |
|
579 |
||
580 |
int style = styleFromMessageType(messageType); |
|
581 |
JDialog dialog = pane.createDialog(parentComponent, title, style); |
|
582 |
||
583 |
pane.selectInitialValue(); |
|
584 |
dialog.show(); |
|
585 |
dialog.dispose(); |
|
586 |
||
587 |
Object value = pane.getInputValue(); |
|
588 |
||
589 |
if (value == UNINITIALIZED_VALUE) { |
|
590 |
return null; |
|
591 |
} |
|
592 |
return value; |
|
593 |
} |
|
594 |
||
595 |
/** |
|
596 |
* Brings up an information-message dialog titled "Message". |
|
597 |
* |
|
598 |
* @param parentComponent determines the <code>Frame</code> in |
|
599 |
* which the dialog is displayed; if <code>null</code>, |
|
600 |
* or if the <code>parentComponent</code> has no |
|
601 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
602 |
* @param message the <code>Object</code> to display |
|
603 |
* @exception HeadlessException if |
|
604 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
605 |
* <code>true</code> |
|
606 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
607 |
*/ |
|
608 |
public static void showMessageDialog(Component parentComponent, |
|
609 |
Object message) throws HeadlessException { |
|
610 |
showMessageDialog(parentComponent, message, UIManager.getString( |
|
611 |
"OptionPane.messageDialogTitle", parentComponent), |
|
612 |
INFORMATION_MESSAGE); |
|
613 |
} |
|
614 |
||
615 |
/** |
|
616 |
* Brings up a dialog that displays a message using a default |
|
617 |
* icon determined by the <code>messageType</code> parameter. |
|
618 |
* |
|
619 |
* @param parentComponent determines the <code>Frame</code> |
|
620 |
* in which the dialog is displayed; if <code>null</code>, |
|
621 |
* or if the <code>parentComponent</code> has no |
|
622 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
623 |
* @param message the <code>Object</code> to display |
|
624 |
* @param title the title string for the dialog |
|
625 |
* @param messageType the type of message to be displayed: |
|
626 |
* <code>ERROR_MESSAGE</code>, |
|
627 |
* <code>INFORMATION_MESSAGE</code>, |
|
628 |
* <code>WARNING_MESSAGE</code>, |
|
629 |
* <code>QUESTION_MESSAGE</code>, |
|
630 |
* or <code>PLAIN_MESSAGE</code> |
|
631 |
* @exception HeadlessException if |
|
632 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
633 |
* <code>true</code> |
|
634 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
635 |
*/ |
|
636 |
public static void showMessageDialog(Component parentComponent, |
|
637 |
Object message, String title, int messageType) |
|
638 |
throws HeadlessException { |
|
639 |
showMessageDialog(parentComponent, message, title, messageType, null); |
|
640 |
} |
|
641 |
||
642 |
/** |
|
643 |
* Brings up a dialog displaying a message, specifying all parameters. |
|
644 |
* |
|
645 |
* @param parentComponent determines the <code>Frame</code> in which the |
|
646 |
* dialog is displayed; if <code>null</code>, |
|
647 |
* or if the <code>parentComponent</code> has no |
|
648 |
* <code>Frame</code>, a |
|
649 |
* default <code>Frame</code> is used |
|
650 |
* @param message the <code>Object</code> to display |
|
651 |
* @param title the title string for the dialog |
|
652 |
* @param messageType the type of message to be displayed: |
|
653 |
* <code>ERROR_MESSAGE</code>, |
|
654 |
* <code>INFORMATION_MESSAGE</code>, |
|
655 |
* <code>WARNING_MESSAGE</code>, |
|
656 |
* <code>QUESTION_MESSAGE</code>, |
|
657 |
* or <code>PLAIN_MESSAGE</code> |
|
658 |
* @param icon an icon to display in the dialog that helps the user |
|
659 |
* identify the kind of message that is being displayed |
|
660 |
* @exception HeadlessException if |
|
661 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
662 |
* <code>true</code> |
|
663 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
664 |
*/ |
|
665 |
public static void showMessageDialog(Component parentComponent, |
|
666 |
Object message, String title, int messageType, Icon icon) |
|
667 |
throws HeadlessException { |
|
668 |
showOptionDialog(parentComponent, message, title, DEFAULT_OPTION, |
|
669 |
messageType, icon, null, null); |
|
670 |
} |
|
671 |
||
672 |
/** |
|
673 |
* Brings up a dialog with the options <i>Yes</i>, |
|
674 |
* <i>No</i> and <i>Cancel</i>; with the |
|
675 |
* title, <b>Select an Option</b>. |
|
676 |
* |
|
677 |
* @param parentComponent determines the <code>Frame</code> in which the |
|
678 |
* dialog is displayed; if <code>null</code>, |
|
679 |
* or if the <code>parentComponent</code> has no |
|
680 |
* <code>Frame</code>, a |
|
681 |
* default <code>Frame</code> is used |
|
682 |
* @param message the <code>Object</code> to display |
|
683 |
* @return an integer indicating the option selected by the user |
|
684 |
* @exception HeadlessException if |
|
685 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
686 |
* <code>true</code> |
|
687 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
688 |
*/ |
|
689 |
public static int showConfirmDialog(Component parentComponent, |
|
690 |
Object message) throws HeadlessException { |
|
691 |
return showConfirmDialog(parentComponent, message, |
|
692 |
UIManager.getString("OptionPane.titleText"), |
|
693 |
YES_NO_CANCEL_OPTION); |
|
694 |
} |
|
695 |
||
696 |
/** |
|
697 |
* Brings up a dialog where the number of choices is determined |
|
698 |
* by the <code>optionType</code> parameter. |
|
699 |
* |
|
700 |
* @param parentComponent determines the <code>Frame</code> in which the |
|
701 |
* dialog is displayed; if <code>null</code>, |
|
702 |
* or if the <code>parentComponent</code> has no |
|
703 |
* <code>Frame</code>, a |
|
704 |
* default <code>Frame</code> is used |
|
705 |
* @param message the <code>Object</code> to display |
|
706 |
* @param title the title string for the dialog |
|
707 |
* @param optionType an int designating the options available on the dialog: |
|
708 |
* <code>YES_NO_OPTION</code>, |
|
709 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
710 |
* or <code>OK_CANCEL_OPTION</code> |
|
711 |
* @return an int indicating the option selected by the user |
|
712 |
* @exception HeadlessException if |
|
713 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
714 |
* <code>true</code> |
|
715 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
716 |
*/ |
|
717 |
public static int showConfirmDialog(Component parentComponent, |
|
718 |
Object message, String title, int optionType) |
|
719 |
throws HeadlessException { |
|
720 |
return showConfirmDialog(parentComponent, message, title, optionType, |
|
721 |
QUESTION_MESSAGE); |
|
722 |
} |
|
723 |
||
724 |
/** |
|
725 |
* Brings up a dialog where the number of choices is determined |
|
726 |
* by the <code>optionType</code> parameter, where the |
|
727 |
* <code>messageType</code> |
|
728 |
* parameter determines the icon to display. |
|
729 |
* The <code>messageType</code> parameter is primarily used to supply |
|
730 |
* a default icon from the Look and Feel. |
|
731 |
* |
|
732 |
* @param parentComponent determines the <code>Frame</code> in |
|
733 |
* which the dialog is displayed; if <code>null</code>, |
|
734 |
* or if the <code>parentComponent</code> has no |
|
735 |
* <code>Frame</code>, a |
|
736 |
* default <code>Frame</code> is used. |
|
737 |
* @param message the <code>Object</code> to display |
|
738 |
* @param title the title string for the dialog |
|
739 |
* @param optionType an integer designating the options available |
|
740 |
* on the dialog: <code>YES_NO_OPTION</code>, |
|
741 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
742 |
* or <code>OK_CANCEL_OPTION</code> |
|
743 |
* @param messageType an integer designating the kind of message this is; |
|
744 |
* primarily used to determine the icon from the pluggable |
|
745 |
* Look and Feel: <code>ERROR_MESSAGE</code>, |
|
746 |
* <code>INFORMATION_MESSAGE</code>, |
|
747 |
* <code>WARNING_MESSAGE</code>, |
|
748 |
* <code>QUESTION_MESSAGE</code>, |
|
749 |
* or <code>PLAIN_MESSAGE</code> |
|
750 |
* @return an integer indicating the option selected by the user |
|
751 |
* @exception HeadlessException if |
|
752 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
753 |
* <code>true</code> |
|
754 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
755 |
*/ |
|
756 |
public static int showConfirmDialog(Component parentComponent, |
|
757 |
Object message, String title, int optionType, int messageType) |
|
758 |
throws HeadlessException { |
|
759 |
return showConfirmDialog(parentComponent, message, title, optionType, |
|
760 |
messageType, null); |
|
761 |
} |
|
762 |
||
763 |
/** |
|
764 |
* Brings up a dialog with a specified icon, where the number of |
|
765 |
* choices is determined by the <code>optionType</code> parameter. |
|
766 |
* The <code>messageType</code> parameter is primarily used to supply |
|
767 |
* a default icon from the look and feel. |
|
768 |
* |
|
769 |
* @param parentComponent determines the <code>Frame</code> in which the |
|
770 |
* dialog is displayed; if <code>null</code>, |
|
771 |
* or if the <code>parentComponent</code> has no |
|
772 |
* <code>Frame</code>, a |
|
773 |
* default <code>Frame</code> is used |
|
774 |
* @param message the Object to display |
|
775 |
* @param title the title string for the dialog |
|
776 |
* @param optionType an int designating the options available on the dialog: |
|
777 |
* <code>YES_NO_OPTION</code>, |
|
778 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
779 |
* or <code>OK_CANCEL_OPTION</code> |
|
780 |
* @param messageType an int designating the kind of message this is, |
|
781 |
* primarily used to determine the icon from the pluggable |
|
782 |
* Look and Feel: <code>ERROR_MESSAGE</code>, |
|
783 |
* <code>INFORMATION_MESSAGE</code>, |
|
784 |
* <code>WARNING_MESSAGE</code>, |
|
785 |
* <code>QUESTION_MESSAGE</code>, |
|
786 |
* or <code>PLAIN_MESSAGE</code> |
|
787 |
* @param icon the icon to display in the dialog |
|
788 |
* @return an int indicating the option selected by the user |
|
789 |
* @exception HeadlessException if |
|
790 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
791 |
* <code>true</code> |
|
792 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
793 |
*/ |
|
794 |
public static int showConfirmDialog(Component parentComponent, |
|
795 |
Object message, String title, int optionType, |
|
796 |
int messageType, Icon icon) throws HeadlessException { |
|
797 |
return showOptionDialog(parentComponent, message, title, optionType, |
|
798 |
messageType, icon, null, null); |
|
799 |
} |
|
800 |
||
801 |
/** |
|
802 |
* Brings up a dialog with a specified icon, where the initial |
|
803 |
* choice is determined by the <code>initialValue</code> parameter and |
|
804 |
* the number of choices is determined by the <code>optionType</code> |
|
805 |
* parameter. |
|
806 |
* <p> |
|
807 |
* If <code>optionType</code> is <code>YES_NO_OPTION</code>, |
|
808 |
* or <code>YES_NO_CANCEL_OPTION</code> |
|
809 |
* and the <code>options</code> parameter is <code>null</code>, |
|
810 |
* then the options are |
|
811 |
* supplied by the look and feel. |
|
812 |
* <p> |
|
813 |
* The <code>messageType</code> parameter is primarily used to supply |
|
814 |
* a default icon from the look and feel. |
|
815 |
* |
|
816 |
* @param parentComponent determines the <code>Frame</code> |
|
817 |
* in which the dialog is displayed; if |
|
818 |
* <code>null</code>, or if the |
|
819 |
* <code>parentComponent</code> has no |
|
820 |
* <code>Frame</code>, a |
|
821 |
* default <code>Frame</code> is used |
|
822 |
* @param message the <code>Object</code> to display |
|
823 |
* @param title the title string for the dialog |
|
824 |
* @param optionType an integer designating the options available on the |
|
825 |
* dialog: <code>DEFAULT_OPTION</code>, |
|
826 |
* <code>YES_NO_OPTION</code>, |
|
827 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
828 |
* or <code>OK_CANCEL_OPTION</code> |
|
829 |
* @param messageType an integer designating the kind of message this is, |
|
830 |
* primarily used to determine the icon from the |
|
831 |
* pluggable Look and Feel: <code>ERROR_MESSAGE</code>, |
|
832 |
* <code>INFORMATION_MESSAGE</code>, |
|
833 |
* <code>WARNING_MESSAGE</code>, |
|
834 |
* <code>QUESTION_MESSAGE</code>, |
|
835 |
* or <code>PLAIN_MESSAGE</code> |
|
836 |
* @param icon the icon to display in the dialog |
|
837 |
* @param options an array of objects indicating the possible choices |
|
838 |
* the user can make; if the objects are components, they |
|
839 |
* are rendered properly; non-<code>String</code> |
|
840 |
* objects are |
|
841 |
* rendered using their <code>toString</code> methods; |
|
842 |
* if this parameter is <code>null</code>, |
|
843 |
* the options are determined by the Look and Feel |
|
844 |
* @param initialValue the object that represents the default selection |
|
845 |
* for the dialog; only meaningful if <code>options</code> |
|
846 |
* is used; can be <code>null</code> |
|
847 |
* @return an integer indicating the option chosen by the user, |
|
848 |
* or <code>CLOSED_OPTION</code> if the user closed |
|
849 |
* the dialog |
|
850 |
* @exception HeadlessException if |
|
851 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
852 |
* <code>true</code> |
|
853 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
854 |
*/ |
|
855 |
public static int showOptionDialog(Component parentComponent, |
|
856 |
Object message, String title, int optionType, int messageType, |
|
857 |
Icon icon, Object[] options, Object initialValue) |
|
858 |
throws HeadlessException { |
|
859 |
JOptionPane pane = new JOptionPane(message, messageType, |
|
860 |
optionType, icon, |
|
861 |
options, initialValue); |
|
862 |
||
863 |
pane.setInitialValue(initialValue); |
|
864 |
pane.setComponentOrientation(((parentComponent == null) ? |
|
865 |
getRootFrame() : parentComponent).getComponentOrientation()); |
|
866 |
||
867 |
int style = styleFromMessageType(messageType); |
|
868 |
JDialog dialog = pane.createDialog(parentComponent, title, style); |
|
869 |
||
870 |
pane.selectInitialValue(); |
|
871 |
dialog.show(); |
|
872 |
dialog.dispose(); |
|
873 |
||
874 |
Object selectedValue = pane.getValue(); |
|
875 |
||
876 |
if(selectedValue == null) |
|
877 |
return CLOSED_OPTION; |
|
878 |
if(options == null) { |
|
879 |
if(selectedValue instanceof Integer) |
|
880 |
return ((Integer)selectedValue).intValue(); |
|
881 |
return CLOSED_OPTION; |
|
882 |
} |
|
883 |
for(int counter = 0, maxCounter = options.length; |
|
884 |
counter < maxCounter; counter++) { |
|
885 |
if(options[counter].equals(selectedValue)) |
|
886 |
return counter; |
|
887 |
} |
|
888 |
return CLOSED_OPTION; |
|
889 |
} |
|
890 |
||
891 |
/** |
|
892 |
* Creates and returns a new <code>JDialog</code> wrapping |
|
893 |
* <code>this</code> centered on the <code>parentComponent</code> |
|
894 |
* in the <code>parentComponent</code>'s frame. |
|
895 |
* <code>title</code> is the title of the returned dialog. |
|
896 |
* The returned <code>JDialog</code> will not be resizable by the |
|
897 |
* user, however programs can invoke <code>setResizable</code> on |
|
898 |
* the <code>JDialog</code> instance to change this property. |
|
899 |
* The returned <code>JDialog</code> will be set up such that |
|
900 |
* once it is closed, or the user clicks on one of the buttons, |
|
901 |
* the optionpane's value property will be set accordingly and |
|
902 |
* the dialog will be closed. Each time the dialog is made visible, |
|
903 |
* it will reset the option pane's value property to |
|
904 |
* <code>JOptionPane.UNINITIALIZED_VALUE</code> to ensure the |
|
905 |
* user's subsequent action closes the dialog properly. |
|
906 |
* |
|
907 |
* @param parentComponent determines the frame in which the dialog |
|
908 |
* is displayed; if the <code>parentComponent</code> has |
|
909 |
* no <code>Frame</code>, a default <code>Frame</code> is used |
|
910 |
* @param title the title string for the dialog |
|
911 |
* @return a new <code>JDialog</code> containing this instance |
|
912 |
* @exception HeadlessException if |
|
913 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
914 |
* <code>true</code> |
|
915 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
916 |
*/ |
|
917 |
public JDialog createDialog(Component parentComponent, String title) |
|
918 |
throws HeadlessException { |
|
919 |
int style = styleFromMessageType(getMessageType()); |
|
920 |
return createDialog(parentComponent, title, style); |
|
921 |
} |
|
922 |
||
923 |
/** |
|
924 |
* Creates and returns a new parentless <code>JDialog</code> |
|
925 |
* with the specified title. |
|
926 |
* The returned <code>JDialog</code> will not be resizable by the |
|
927 |
* user, however programs can invoke <code>setResizable</code> on |
|
928 |
* the <code>JDialog</code> instance to change this property. |
|
929 |
* The returned <code>JDialog</code> will be set up such that |
|
930 |
* once it is closed, or the user clicks on one of the buttons, |
|
931 |
* the optionpane's value property will be set accordingly and |
|
932 |
* the dialog will be closed. Each time the dialog is made visible, |
|
933 |
* it will reset the option pane's value property to |
|
934 |
* <code>JOptionPane.UNINITIALIZED_VALUE</code> to ensure the |
|
935 |
* user's subsequent action closes the dialog properly. |
|
936 |
* |
|
937 |
* @param title the title string for the dialog |
|
938 |
* @return a new <code>JDialog</code> containing this instance |
|
939 |
* @exception HeadlessException if |
|
940 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
941 |
* <code>true</code> |
|
942 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
943 |
* @since 1.6 |
|
944 |
*/ |
|
945 |
public JDialog createDialog(String title) throws HeadlessException { |
|
946 |
int style = styleFromMessageType(getMessageType()); |
|
947 |
JDialog dialog = new JDialog((Dialog) null, title, true); |
|
948 |
initDialog(dialog, style, null); |
|
949 |
return dialog; |
|
950 |
} |
|
951 |
||
952 |
private JDialog createDialog(Component parentComponent, String title, |
|
953 |
int style) |
|
954 |
throws HeadlessException { |
|
955 |
||
956 |
final JDialog dialog; |
|
957 |
||
958 |
Window window = JOptionPane.getWindowForComponent(parentComponent); |
|
959 |
if (window instanceof Frame) { |
|
960 |
dialog = new JDialog((Frame)window, title, true); |
|
961 |
} else { |
|
962 |
dialog = new JDialog((Dialog)window, title, true); |
|
963 |
} |
|
964 |
if (window instanceof SwingUtilities.SharedOwnerFrame) { |
|
965 |
WindowListener ownerShutdownListener = |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
966 |
SwingUtilities.getSharedOwnerFrameShutdownListener(); |
2 | 967 |
dialog.addWindowListener(ownerShutdownListener); |
968 |
} |
|
969 |
initDialog(dialog, style, parentComponent); |
|
970 |
return dialog; |
|
971 |
} |
|
972 |
||
973 |
private void initDialog(final JDialog dialog, int style, Component parentComponent) { |
|
974 |
dialog.setComponentOrientation(this.getComponentOrientation()); |
|
975 |
Container contentPane = dialog.getContentPane(); |
|
976 |
||
977 |
contentPane.setLayout(new BorderLayout()); |
|
978 |
contentPane.add(this, BorderLayout.CENTER); |
|
979 |
dialog.setResizable(false); |
|
980 |
if (JDialog.isDefaultLookAndFeelDecorated()) { |
|
981 |
boolean supportsWindowDecorations = |
|
982 |
UIManager.getLookAndFeel().getSupportsWindowDecorations(); |
|
983 |
if (supportsWindowDecorations) { |
|
984 |
dialog.setUndecorated(true); |
|
985 |
getRootPane().setWindowDecorationStyle(style); |
|
986 |
} |
|
987 |
} |
|
988 |
dialog.pack(); |
|
989 |
dialog.setLocationRelativeTo(parentComponent); |
|
990 |
WindowAdapter adapter = new WindowAdapter() { |
|
991 |
private boolean gotFocus = false; |
|
992 |
public void windowClosing(WindowEvent we) { |
|
993 |
setValue(null); |
|
994 |
} |
|
995 |
public void windowGainedFocus(WindowEvent we) { |
|
996 |
// Once window gets focus, set initial focus |
|
997 |
if (!gotFocus) { |
|
998 |
selectInitialValue(); |
|
999 |
gotFocus = true; |
|
1000 |
} |
|
1001 |
} |
|
1002 |
}; |
|
1003 |
dialog.addWindowListener(adapter); |
|
1004 |
dialog.addWindowFocusListener(adapter); |
|
1005 |
dialog.addComponentListener(new ComponentAdapter() { |
|
1006 |
public void componentShown(ComponentEvent ce) { |
|
1007 |
// reset value to ensure closing works properly |
|
1008 |
setValue(JOptionPane.UNINITIALIZED_VALUE); |
|
1009 |
} |
|
1010 |
}); |
|
1011 |
addPropertyChangeListener(new PropertyChangeListener() { |
|
1012 |
public void propertyChange(PropertyChangeEvent event) { |
|
1013 |
// Let the defaultCloseOperation handle the closing |
|
1014 |
// if the user closed the window without selecting a button |
|
1015 |
// (newValue = null in that case). Otherwise, close the dialog. |
|
1016 |
if (dialog.isVisible() && event.getSource() == JOptionPane.this && |
|
1017 |
(event.getPropertyName().equals(VALUE_PROPERTY) || |
|
1018 |
event.getPropertyName().equals(INPUT_VALUE_PROPERTY)) && |
|
1019 |
event.getNewValue() != null && |
|
1020 |
event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { |
|
1021 |
dialog.setVisible(false); |
|
1022 |
} |
|
1023 |
} |
|
1024 |
}); |
|
1025 |
} |
|
1026 |
||
1027 |
||
1028 |
/** |
|
1029 |
* Brings up an internal confirmation dialog panel. The dialog |
|
1030 |
* is a information-message dialog titled "Message". |
|
1031 |
* |
|
1032 |
* @param parentComponent determines the <code>Frame</code> |
|
1033 |
* in which the dialog is displayed; if <code>null</code>, |
|
1034 |
* or if the <code>parentComponent</code> has no |
|
1035 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
1036 |
* @param message the object to display |
|
1037 |
*/ |
|
1038 |
public static void showInternalMessageDialog(Component parentComponent, |
|
1039 |
Object message) { |
|
1040 |
showInternalMessageDialog(parentComponent, message, UIManager. |
|
1041 |
getString("OptionPane.messageDialogTitle", |
|
1042 |
parentComponent), INFORMATION_MESSAGE); |
|
1043 |
} |
|
1044 |
||
1045 |
/** |
|
1046 |
* Brings up an internal dialog panel that displays a message |
|
1047 |
* using a default icon determined by the <code>messageType</code> |
|
1048 |
* parameter. |
|
1049 |
* |
|
1050 |
* @param parentComponent determines the <code>Frame</code> |
|
1051 |
* in which the dialog is displayed; if <code>null</code>, |
|
1052 |
* or if the <code>parentComponent</code> has no |
|
1053 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
1054 |
* @param message the <code>Object</code> to display |
|
1055 |
* @param title the title string for the dialog |
|
1056 |
* @param messageType the type of message to be displayed: |
|
1057 |
* <code>ERROR_MESSAGE</code>, |
|
1058 |
* <code>INFORMATION_MESSAGE</code>, |
|
1059 |
* <code>WARNING_MESSAGE</code>, |
|
1060 |
* <code>QUESTION_MESSAGE</code>, |
|
1061 |
* or <code>PLAIN_MESSAGE</code> |
|
1062 |
*/ |
|
1063 |
public static void showInternalMessageDialog(Component parentComponent, |
|
1064 |
Object message, String title, |
|
1065 |
int messageType) { |
|
1066 |
showInternalMessageDialog(parentComponent, message, title, messageType,null); |
|
1067 |
} |
|
1068 |
||
1069 |
/** |
|
1070 |
* Brings up an internal dialog panel displaying a message, |
|
1071 |
* specifying all parameters. |
|
1072 |
* |
|
1073 |
* @param parentComponent determines the <code>Frame</code> |
|
1074 |
* in which the dialog is displayed; if <code>null</code>, |
|
1075 |
* or if the <code>parentComponent</code> has no |
|
1076 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
1077 |
* @param message the <code>Object</code> to display |
|
1078 |
* @param title the title string for the dialog |
|
1079 |
* @param messageType the type of message to be displayed: |
|
1080 |
* <code>ERROR_MESSAGE</code>, |
|
1081 |
* <code>INFORMATION_MESSAGE</code>, |
|
1082 |
* <code>WARNING_MESSAGE</code>, |
|
1083 |
* <code>QUESTION_MESSAGE</code>, |
|
1084 |
* or <code>PLAIN_MESSAGE</code> |
|
1085 |
* @param icon an icon to display in the dialog that helps the user |
|
1086 |
* identify the kind of message that is being displayed |
|
1087 |
*/ |
|
1088 |
public static void showInternalMessageDialog(Component parentComponent, |
|
1089 |
Object message, |
|
1090 |
String title, int messageType, |
|
1091 |
Icon icon){ |
|
1092 |
showInternalOptionDialog(parentComponent, message, title, DEFAULT_OPTION, |
|
1093 |
messageType, icon, null, null); |
|
1094 |
} |
|
1095 |
||
1096 |
/** |
|
1097 |
* Brings up an internal dialog panel with the options <i>Yes</i>, <i>No</i> |
|
1098 |
* and <i>Cancel</i>; with the title, <b>Select an Option</b>. |
|
1099 |
* |
|
1100 |
* @param parentComponent determines the <code>Frame</code> in |
|
1101 |
* which the dialog is displayed; if <code>null</code>, |
|
1102 |
* or if the <code>parentComponent</code> has no |
|
1103 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
1104 |
* @param message the <code>Object</code> to display |
|
1105 |
* @return an integer indicating the option selected by the user |
|
1106 |
*/ |
|
1107 |
public static int showInternalConfirmDialog(Component parentComponent, |
|
1108 |
Object message) { |
|
1109 |
return showInternalConfirmDialog(parentComponent, message, |
|
1110 |
UIManager.getString("OptionPane.titleText"), |
|
1111 |
YES_NO_CANCEL_OPTION); |
|
1112 |
} |
|
1113 |
||
1114 |
/** |
|
1115 |
* Brings up a internal dialog panel where the number of choices |
|
1116 |
* is determined by the <code>optionType</code> parameter. |
|
1117 |
* |
|
1118 |
* @param parentComponent determines the <code>Frame</code> |
|
1119 |
* in which the dialog is displayed; if <code>null</code>, |
|
1120 |
* or if the <code>parentComponent</code> has no |
|
1121 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
1122 |
* @param message the object to display in the dialog; a |
|
1123 |
* <code>Component</code> object is rendered as a |
|
1124 |
* <code>Component</code>; a <code>String</code> |
|
1125 |
* object is rendered as a string; other objects |
|
1126 |
* are converted to a <code>String</code> using the |
|
1127 |
* <code>toString</code> method |
|
1128 |
* @param title the title string for the dialog |
|
1129 |
* @param optionType an integer designating the options |
|
1130 |
* available on the dialog: <code>YES_NO_OPTION</code>, |
|
1131 |
* or <code>YES_NO_CANCEL_OPTION</code> |
|
1132 |
* @return an integer indicating the option selected by the user |
|
1133 |
*/ |
|
1134 |
public static int showInternalConfirmDialog(Component parentComponent, |
|
1135 |
Object message, String title, |
|
1136 |
int optionType) { |
|
1137 |
return showInternalConfirmDialog(parentComponent, message, title, optionType, |
|
1138 |
QUESTION_MESSAGE); |
|
1139 |
} |
|
1140 |
||
1141 |
/** |
|
1142 |
* Brings up an internal dialog panel where the number of choices |
|
1143 |
* is determined by the <code>optionType</code> parameter, where |
|
1144 |
* the <code>messageType</code> parameter determines the icon to display. |
|
1145 |
* The <code>messageType</code> parameter is primarily used to supply |
|
1146 |
* a default icon from the Look and Feel. |
|
1147 |
* |
|
1148 |
* @param parentComponent determines the <code>Frame</code> in |
|
1149 |
* which the dialog is displayed; if <code>null</code>, |
|
1150 |
* or if the <code>parentComponent</code> has no |
|
1151 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
1152 |
* @param message the object to display in the dialog; a |
|
1153 |
* <code>Component</code> object is rendered as a |
|
1154 |
* <code>Component</code>; a <code>String</code> |
|
1155 |
* object is rendered as a string; other objects are |
|
1156 |
* converted to a <code>String</code> using the |
|
1157 |
* <code>toString</code> method |
|
1158 |
* @param title the title string for the dialog |
|
1159 |
* @param optionType an integer designating the options |
|
1160 |
* available on the dialog: |
|
1161 |
* <code>YES_NO_OPTION</code>, or <code>YES_NO_CANCEL_OPTION</code> |
|
1162 |
* @param messageType an integer designating the kind of message this is, |
|
1163 |
* primarily used to determine the icon from the |
|
1164 |
* pluggable Look and Feel: <code>ERROR_MESSAGE</code>, |
|
1165 |
* <code>INFORMATION_MESSAGE</code>, |
|
1166 |
* <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>, |
|
1167 |
* or <code>PLAIN_MESSAGE</code> |
|
1168 |
* @return an integer indicating the option selected by the user |
|
1169 |
*/ |
|
1170 |
public static int showInternalConfirmDialog(Component parentComponent, |
|
1171 |
Object message, |
|
1172 |
String title, int optionType, |
|
1173 |
int messageType) { |
|
1174 |
return showInternalConfirmDialog(parentComponent, message, title, optionType, |
|
1175 |
messageType, null); |
|
1176 |
} |
|
1177 |
||
1178 |
/** |
|
1179 |
* Brings up an internal dialog panel with a specified icon, where |
|
1180 |
* the number of choices is determined by the <code>optionType</code> |
|
1181 |
* parameter. |
|
1182 |
* The <code>messageType</code> parameter is primarily used to supply |
|
1183 |
* a default icon from the look and feel. |
|
1184 |
* |
|
1185 |
* @param parentComponent determines the <code>Frame</code> |
|
1186 |
* in which the dialog is displayed; if <code>null</code>, |
|
1187 |
* or if the parentComponent has no Frame, a |
|
1188 |
* default <code>Frame</code> is used |
|
1189 |
* @param message the object to display in the dialog; a |
|
1190 |
* <code>Component</code> object is rendered as a |
|
1191 |
* <code>Component</code>; a <code>String</code> |
|
1192 |
* object is rendered as a string; other objects are |
|
1193 |
* converted to a <code>String</code> using the |
|
1194 |
* <code>toString</code> method |
|
1195 |
* @param title the title string for the dialog |
|
1196 |
* @param optionType an integer designating the options available |
|
1197 |
* on the dialog: |
|
1198 |
* <code>YES_NO_OPTION</code>, or |
|
1199 |
* <code>YES_NO_CANCEL_OPTION</code>. |
|
1200 |
* @param messageType an integer designating the kind of message this is, |
|
1201 |
* primarily used to determine the icon from the pluggable |
|
1202 |
* Look and Feel: <code>ERROR_MESSAGE</code>, |
|
1203 |
* <code>INFORMATION_MESSAGE</code>, |
|
1204 |
* <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>, |
|
1205 |
* or <code>PLAIN_MESSAGE</code> |
|
1206 |
* @param icon the icon to display in the dialog |
|
1207 |
* @return an integer indicating the option selected by the user |
|
1208 |
*/ |
|
1209 |
public static int showInternalConfirmDialog(Component parentComponent, |
|
1210 |
Object message, |
|
1211 |
String title, int optionType, |
|
1212 |
int messageType, Icon icon) { |
|
1213 |
return showInternalOptionDialog(parentComponent, message, title, optionType, |
|
1214 |
messageType, icon, null, null); |
|
1215 |
} |
|
1216 |
||
1217 |
/** |
|
1218 |
* Brings up an internal dialog panel with a specified icon, where |
|
1219 |
* the initial choice is determined by the <code>initialValue</code> |
|
1220 |
* parameter and the number of choices is determined by the |
|
1221 |
* <code>optionType</code> parameter. |
|
1222 |
* <p> |
|
1223 |
* If <code>optionType</code> is <code>YES_NO_OPTION</code>, or |
|
1224 |
* <code>YES_NO_CANCEL_OPTION</code> |
|
1225 |
* and the <code>options</code> parameter is <code>null</code>, |
|
1226 |
* then the options are supplied by the Look and Feel. |
|
1227 |
* <p> |
|
1228 |
* The <code>messageType</code> parameter is primarily used to supply |
|
1229 |
* a default icon from the look and feel. |
|
1230 |
* |
|
1231 |
* @param parentComponent determines the <code>Frame</code> |
|
1232 |
* in which the dialog is displayed; if <code>null</code>, |
|
1233 |
* or if the <code>parentComponent</code> has no |
|
1234 |
* <code>Frame</code>, a default <code>Frame</code> is used |
|
1235 |
* @param message the object to display in the dialog; a |
|
1236 |
* <code>Component</code> object is rendered as a |
|
1237 |
* <code>Component</code>; a <code>String</code> |
|
1238 |
* object is rendered as a string. Other objects are |
|
1239 |
* converted to a <code>String</code> using the |
|
1240 |
* <code>toString</code> method |
|
1241 |
* @param title the title string for the dialog |
|
1242 |
* @param optionType an integer designating the options available |
|
1243 |
* on the dialog: <code>YES_NO_OPTION</code>, |
|
1244 |
* or <code>YES_NO_CANCEL_OPTION</code> |
|
1245 |
* @param messageType an integer designating the kind of message this is; |
|
1246 |
* primarily used to determine the icon from the |
|
1247 |
* pluggable Look and Feel: <code>ERROR_MESSAGE</code>, |
|
1248 |
* <code>INFORMATION_MESSAGE</code>, |
|
1249 |
* <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>, |
|
1250 |
* or <code>PLAIN_MESSAGE</code> |
|
1251 |
* @param icon the icon to display in the dialog |
|
1252 |
* @param options an array of objects indicating the possible choices |
|
1253 |
* the user can make; if the objects are components, they |
|
1254 |
* are rendered properly; non-<code>String</code> |
|
1255 |
* objects are rendered using their <code>toString</code> |
|
1256 |
* methods; if this parameter is <code>null</code>, |
|
1257 |
* the options are determined by the Look and Feel |
|
1258 |
* @param initialValue the object that represents the default selection |
|
1259 |
* for the dialog; only meaningful if <code>options</code> |
|
1260 |
* is used; can be <code>null</code> |
|
1261 |
* @return an integer indicating the option chosen by the user, |
|
1262 |
* or <code>CLOSED_OPTION</code> if the user closed the Dialog |
|
1263 |
*/ |
|
1264 |
public static int showInternalOptionDialog(Component parentComponent, |
|
1265 |
Object message, |
|
1266 |
String title, int optionType, |
|
1267 |
int messageType, Icon icon, |
|
1268 |
Object[] options, Object initialValue) { |
|
1269 |
JOptionPane pane = new JOptionPane(message, messageType, |
|
1270 |
optionType, icon, options, initialValue); |
|
1271 |
pane.putClientProperty(PopupFactory_FORCE_HEAVYWEIGHT_POPUP, |
|
1272 |
Boolean.TRUE); |
|
1273 |
Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager(). |
|
1274 |
getFocusOwner(); |
|
1275 |
||
1276 |
pane.setInitialValue(initialValue); |
|
1277 |
||
1278 |
JInternalFrame dialog = |
|
1279 |
pane.createInternalFrame(parentComponent, title); |
|
1280 |
pane.selectInitialValue(); |
|
1281 |
dialog.setVisible(true); |
|
1282 |
||
1283 |
/* Since all input will be blocked until this dialog is dismissed, |
|
1284 |
* make sure its parent containers are visible first (this component |
|
1285 |
* is tested below). This is necessary for JApplets, because |
|
1286 |
* because an applet normally isn't made visible until after its |
|
1287 |
* start() method returns -- if this method is called from start(), |
|
1288 |
* the applet will appear to hang while an invisible modal frame |
|
1289 |
* waits for input. |
|
1290 |
*/ |
|
1291 |
if (dialog.isVisible() && !dialog.isShowing()) { |
|
1292 |
Container parent = dialog.getParent(); |
|
1293 |
while (parent != null) { |
|
1294 |
if (parent.isVisible() == false) { |
|
1295 |
parent.setVisible(true); |
|
1296 |
} |
|
1297 |
parent = parent.getParent(); |
|
1298 |
} |
|
1299 |
} |
|
1300 |
||
1301 |
// Use reflection to get Container.startLWModal. |
|
1302 |
try { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1303 |
Method method = AccessController.doPrivileged(new ModalPrivilegedAction( |
2 | 1304 |
Container.class, "startLWModal")); |
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1305 |
if (method != null) { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1306 |
method.invoke(dialog, (Object[])null); |
2 | 1307 |
} |
1308 |
} catch (IllegalAccessException ex) { |
|
1309 |
} catch (IllegalArgumentException ex) { |
|
1310 |
} catch (InvocationTargetException ex) { |
|
1311 |
} |
|
1312 |
||
1313 |
if (parentComponent instanceof JInternalFrame) { |
|
1314 |
try { |
|
1315 |
((JInternalFrame)parentComponent).setSelected(true); |
|
1316 |
} catch (java.beans.PropertyVetoException e) { |
|
1317 |
} |
|
1318 |
} |
|
1319 |
||
1320 |
Object selectedValue = pane.getValue(); |
|
1321 |
||
1322 |
if (fo != null && fo.isShowing()) { |
|
1323 |
fo.requestFocus(); |
|
1324 |
} |
|
1325 |
if (selectedValue == null) { |
|
1326 |
return CLOSED_OPTION; |
|
1327 |
} |
|
1328 |
if (options == null) { |
|
1329 |
if (selectedValue instanceof Integer) { |
|
1330 |
return ((Integer)selectedValue).intValue(); |
|
1331 |
} |
|
1332 |
return CLOSED_OPTION; |
|
1333 |
} |
|
1334 |
for(int counter = 0, maxCounter = options.length; |
|
1335 |
counter < maxCounter; counter++) { |
|
1336 |
if (options[counter].equals(selectedValue)) { |
|
1337 |
return counter; |
|
1338 |
} |
|
1339 |
} |
|
1340 |
return CLOSED_OPTION; |
|
1341 |
} |
|
1342 |
||
1343 |
/** |
|
1344 |
* Shows an internal question-message dialog requesting input from |
|
1345 |
* the user parented to <code>parentComponent</code>. The dialog |
|
1346 |
* is displayed in the <code>Component</code>'s frame, |
|
1347 |
* and is usually positioned below the <code>Component</code>. |
|
1348 |
* |
|
1349 |
* @param parentComponent the parent <code>Component</code> |
|
1350 |
* for the dialog |
|
1351 |
* @param message the <code>Object</code> to display |
|
1352 |
*/ |
|
1353 |
public static String showInternalInputDialog(Component parentComponent, |
|
1354 |
Object message) { |
|
1355 |
return showInternalInputDialog(parentComponent, message, UIManager. |
|
1356 |
getString("OptionPane.inputDialogTitle", parentComponent), |
|
1357 |
QUESTION_MESSAGE); |
|
1358 |
} |
|
1359 |
||
1360 |
/** |
|
1361 |
* Shows an internal dialog requesting input from the user parented |
|
1362 |
* to <code>parentComponent</code> with the dialog having the title |
|
1363 |
* <code>title</code> and message type <code>messageType</code>. |
|
1364 |
* |
|
1365 |
* @param parentComponent the parent <code>Component</code> for the dialog |
|
1366 |
* @param message the <code>Object</code> to display |
|
1367 |
* @param title the <code>String</code> to display in the |
|
1368 |
* dialog title bar |
|
1369 |
* @param messageType the type of message that is to be displayed: |
|
1370 |
* ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, |
|
1371 |
* QUESTION_MESSAGE, or PLAIN_MESSAGE |
|
1372 |
*/ |
|
1373 |
public static String showInternalInputDialog(Component parentComponent, |
|
1374 |
Object message, String title, int messageType) { |
|
1375 |
return (String)showInternalInputDialog(parentComponent, message, title, |
|
1376 |
messageType, null, null, null); |
|
1377 |
} |
|
1378 |
||
1379 |
/** |
|
1380 |
* Prompts the user for input in a blocking internal dialog where |
|
1381 |
* the initial selection, possible selections, and all other |
|
1382 |
* options can be specified. The user will able to choose from |
|
1383 |
* <code>selectionValues</code>, where <code>null</code> |
|
1384 |
* implies the user can input |
|
1385 |
* whatever they wish, usually by means of a <code>JTextField</code>. |
|
1386 |
* <code>initialSelectionValue</code> is the initial value to prompt |
|
1387 |
* the user with. It is up to the UI to decide how best to represent |
|
1388 |
* the <code>selectionValues</code>, but usually a |
|
1389 |
* <code>JComboBox</code>, <code>JList</code>, or |
|
1390 |
* <code>JTextField</code> will be used. |
|
1391 |
* |
|
1392 |
* @param parentComponent the parent <code>Component</code> for the dialog |
|
1393 |
* @param message the <code>Object</code> to display |
|
1394 |
* @param title the <code>String</code> to display in the dialog |
|
1395 |
* title bar |
|
1396 |
* @param messageType the type of message to be displayed: |
|
1397 |
* <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>, |
|
1398 |
* <code>WARNING_MESSAGE</code>, |
|
1399 |
* <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code> |
|
1400 |
* @param icon the <code>Icon</code> image to display |
|
1401 |
* @param selectionValues an array of <code>Objects</code> that |
|
1402 |
* gives the possible selections |
|
1403 |
* @param initialSelectionValue the value used to initialize the input |
|
1404 |
* field |
|
1405 |
* @return user's input, or <code>null</code> meaning the user |
|
1406 |
* canceled the input |
|
1407 |
*/ |
|
1408 |
public static Object showInternalInputDialog(Component parentComponent, |
|
1409 |
Object message, String title, int messageType, Icon icon, |
|
1410 |
Object[] selectionValues, Object initialSelectionValue) { |
|
1411 |
JOptionPane pane = new JOptionPane(message, messageType, |
|
1412 |
OK_CANCEL_OPTION, icon, null, null); |
|
1413 |
pane.putClientProperty(PopupFactory_FORCE_HEAVYWEIGHT_POPUP, |
|
1414 |
Boolean.TRUE); |
|
1415 |
Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager(). |
|
1416 |
getFocusOwner(); |
|
1417 |
||
1418 |
pane.setWantsInput(true); |
|
1419 |
pane.setSelectionValues(selectionValues); |
|
1420 |
pane.setInitialSelectionValue(initialSelectionValue); |
|
1421 |
||
1422 |
JInternalFrame dialog = |
|
1423 |
pane.createInternalFrame(parentComponent, title); |
|
1424 |
||
1425 |
pane.selectInitialValue(); |
|
1426 |
dialog.setVisible(true); |
|
1427 |
||
1428 |
/* Since all input will be blocked until this dialog is dismissed, |
|
1429 |
* make sure its parent containers are visible first (this component |
|
1430 |
* is tested below). This is necessary for JApplets, because |
|
1431 |
* because an applet normally isn't made visible until after its |
|
1432 |
* start() method returns -- if this method is called from start(), |
|
1433 |
* the applet will appear to hang while an invisible modal frame |
|
1434 |
* waits for input. |
|
1435 |
*/ |
|
1436 |
if (dialog.isVisible() && !dialog.isShowing()) { |
|
1437 |
Container parent = dialog.getParent(); |
|
1438 |
while (parent != null) { |
|
1439 |
if (parent.isVisible() == false) { |
|
1440 |
parent.setVisible(true); |
|
1441 |
} |
|
1442 |
parent = parent.getParent(); |
|
1443 |
} |
|
1444 |
} |
|
1445 |
||
1446 |
// Use reflection to get Container.startLWModal. |
|
1447 |
try { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1448 |
Method method = AccessController.doPrivileged(new ModalPrivilegedAction( |
2 | 1449 |
Container.class, "startLWModal")); |
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1450 |
if (method != null) { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1451 |
method.invoke(dialog, (Object[])null); |
2 | 1452 |
} |
1453 |
} catch (IllegalAccessException ex) { |
|
1454 |
} catch (IllegalArgumentException ex) { |
|
1455 |
} catch (InvocationTargetException ex) { |
|
1456 |
} |
|
1457 |
||
1458 |
if (parentComponent instanceof JInternalFrame) { |
|
1459 |
try { |
|
1460 |
((JInternalFrame)parentComponent).setSelected(true); |
|
1461 |
} catch (java.beans.PropertyVetoException e) { |
|
1462 |
} |
|
1463 |
} |
|
1464 |
||
1465 |
if (fo != null && fo.isShowing()) { |
|
1466 |
fo.requestFocus(); |
|
1467 |
} |
|
1468 |
Object value = pane.getInputValue(); |
|
1469 |
||
1470 |
if (value == UNINITIALIZED_VALUE) { |
|
1471 |
return null; |
|
1472 |
} |
|
1473 |
return value; |
|
1474 |
} |
|
1475 |
||
1476 |
/** |
|
1477 |
* Creates and returns an instance of <code>JInternalFrame</code>. |
|
1478 |
* The internal frame is created with the specified title, |
|
1479 |
* and wrapping the <code>JOptionPane</code>. |
|
1480 |
* The returned <code>JInternalFrame</code> is |
|
1481 |
* added to the <code>JDesktopPane</code> ancestor of |
|
1482 |
* <code>parentComponent</code>, or components |
|
1483 |
* parent if one its ancestors isn't a <code>JDesktopPane</code>, |
|
1484 |
* or if <code>parentComponent</code> |
|
1485 |
* doesn't have a parent then a <code>RuntimeException</code> is thrown. |
|
1486 |
* |
|
1487 |
* @param parentComponent the parent <code>Component</code> for |
|
1488 |
* the internal frame |
|
1489 |
* @param title the <code>String</code> to display in the |
|
1490 |
* frame's title bar |
|
1491 |
* @return a <code>JInternalFrame</code> containing a |
|
1492 |
* <code>JOptionPane</code> |
|
1493 |
* @exception RuntimeException if <code>parentComponent</code> does |
|
1494 |
* not have a valid parent |
|
1495 |
*/ |
|
1496 |
public JInternalFrame createInternalFrame(Component parentComponent, |
|
1497 |
String title) { |
|
1498 |
Container parent = |
|
1499 |
JOptionPane.getDesktopPaneForComponent(parentComponent); |
|
1500 |
||
1501 |
if (parent == null && (parentComponent == null || |
|
1502 |
(parent = parentComponent.getParent()) == null)) { |
|
1503 |
throw new RuntimeException("JOptionPane: parentComponent does " + |
|
1504 |
"not have a valid parent"); |
|
1505 |
} |
|
1506 |
||
1507 |
// Option dialogs should be closable only |
|
1508 |
final JInternalFrame iFrame = new JInternalFrame(title, false, true, |
|
1509 |
false, false); |
|
1510 |
||
1511 |
iFrame.putClientProperty("JInternalFrame.frameType", "optionDialog"); |
|
1512 |
iFrame.putClientProperty("JInternalFrame.messageType", |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
1513 |
Integer.valueOf(getMessageType())); |
2 | 1514 |
|
1515 |
iFrame.addInternalFrameListener(new InternalFrameAdapter() { |
|
1516 |
public void internalFrameClosing(InternalFrameEvent e) { |
|
1517 |
if (getValue() == UNINITIALIZED_VALUE) { |
|
1518 |
setValue(null); |
|
1519 |
} |
|
1520 |
} |
|
1521 |
}); |
|
1522 |
addPropertyChangeListener(new PropertyChangeListener() { |
|
1523 |
public void propertyChange(PropertyChangeEvent event) { |
|
1524 |
// Let the defaultCloseOperation handle the closing |
|
1525 |
// if the user closed the iframe without selecting a button |
|
1526 |
// (newValue = null in that case). Otherwise, close the dialog. |
|
1527 |
if (iFrame.isVisible() && |
|
1528 |
event.getSource() == JOptionPane.this && |
|
1529 |
event.getPropertyName().equals(VALUE_PROPERTY)) { |
|
1530 |
// Use reflection to get Container.stopLWModal(). |
|
1531 |
try { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1532 |
Method method = AccessController.doPrivileged( |
2 | 1533 |
new ModalPrivilegedAction( |
1534 |
Container.class, "stopLWModal")); |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1535 |
if (method != null) { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1536 |
method.invoke(iFrame, (Object[])null); |
2 | 1537 |
} |
1538 |
} catch (IllegalAccessException ex) { |
|
1539 |
} catch (IllegalArgumentException ex) { |
|
1540 |
} catch (InvocationTargetException ex) { |
|
1541 |
} |
|
1542 |
||
1543 |
try { |
|
1544 |
iFrame.setClosed(true); |
|
1545 |
} |
|
1546 |
catch (java.beans.PropertyVetoException e) { |
|
1547 |
} |
|
1548 |
||
1549 |
iFrame.setVisible(false); |
|
1550 |
} |
|
1551 |
} |
|
1552 |
}); |
|
1553 |
iFrame.getContentPane().add(this, BorderLayout.CENTER); |
|
1554 |
if (parent instanceof JDesktopPane) { |
|
1555 |
parent.add(iFrame, JLayeredPane.MODAL_LAYER); |
|
1556 |
} else { |
|
1557 |
parent.add(iFrame, BorderLayout.CENTER); |
|
1558 |
} |
|
1559 |
Dimension iFrameSize = iFrame.getPreferredSize(); |
|
1560 |
Dimension rootSize = parent.getSize(); |
|
1561 |
Dimension parentSize = parentComponent.getSize(); |
|
1562 |
||
1563 |
iFrame.setBounds((rootSize.width - iFrameSize.width) / 2, |
|
1564 |
(rootSize.height - iFrameSize.height) / 2, |
|
1565 |
iFrameSize.width, iFrameSize.height); |
|
1566 |
// We want dialog centered relative to its parent component |
|
1567 |
Point iFrameCoord = |
|
1568 |
SwingUtilities.convertPoint(parentComponent, 0, 0, parent); |
|
1569 |
int x = (parentSize.width - iFrameSize.width) / 2 + iFrameCoord.x; |
|
1570 |
int y = (parentSize.height - iFrameSize.height) / 2 + iFrameCoord.y; |
|
1571 |
||
1572 |
// If possible, dialog should be fully visible |
|
1573 |
int ovrx = x + iFrameSize.width - rootSize.width; |
|
1574 |
int ovry = y + iFrameSize.height - rootSize.height; |
|
1575 |
x = Math.max((ovrx > 0? x - ovrx: x), 0); |
|
1576 |
y = Math.max((ovry > 0? y - ovry: y), 0); |
|
1577 |
iFrame.setBounds(x, y, iFrameSize.width, iFrameSize.height); |
|
1578 |
||
1579 |
parent.validate(); |
|
1580 |
try { |
|
1581 |
iFrame.setSelected(true); |
|
1582 |
} catch (java.beans.PropertyVetoException e) {} |
|
1583 |
||
1584 |
return iFrame; |
|
1585 |
} |
|
1586 |
||
1587 |
/** |
|
1588 |
* Returns the specified component's <code>Frame</code>. |
|
1589 |
* |
|
1590 |
* @param parentComponent the <code>Component</code> to check for a |
|
1591 |
* <code>Frame</code> |
|
1592 |
* @return the <code>Frame</code> that contains the component, |
|
1593 |
* or <code>getRootFrame</code> |
|
1594 |
* if the component is <code>null</code>, |
|
1595 |
* or does not have a valid <code>Frame</code> parent |
|
1596 |
* @exception HeadlessException if |
|
1597 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
1598 |
* <code>true</code> |
|
1599 |
* @see #getRootFrame |
|
1600 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
1601 |
*/ |
|
1602 |
public static Frame getFrameForComponent(Component parentComponent) |
|
1603 |
throws HeadlessException { |
|
1604 |
if (parentComponent == null) |
|
1605 |
return getRootFrame(); |
|
1606 |
if (parentComponent instanceof Frame) |
|
1607 |
return (Frame)parentComponent; |
|
1608 |
return JOptionPane.getFrameForComponent(parentComponent.getParent()); |
|
1609 |
} |
|
1610 |
||
1611 |
/** |
|
1612 |
* Returns the specified component's toplevel <code>Frame</code> or |
|
1613 |
* <code>Dialog</code>. |
|
1614 |
* |
|
1615 |
* @param parentComponent the <code>Component</code> to check for a |
|
1616 |
* <code>Frame</code> or <code>Dialog</code> |
|
1617 |
* @return the <code>Frame</code> or <code>Dialog</code> that |
|
1618 |
* contains the component, or the default |
|
1619 |
* frame if the component is <code>null</code>, |
|
1620 |
* or does not have a valid |
|
1621 |
* <code>Frame</code> or <code>Dialog</code> parent |
|
1622 |
* @exception HeadlessException if |
|
1623 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
1624 |
* <code>true</code> |
|
1625 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
1626 |
*/ |
|
1627 |
static Window getWindowForComponent(Component parentComponent) |
|
1628 |
throws HeadlessException { |
|
1629 |
if (parentComponent == null) |
|
1630 |
return getRootFrame(); |
|
1631 |
if (parentComponent instanceof Frame || parentComponent instanceof Dialog) |
|
1632 |
return (Window)parentComponent; |
|
1633 |
return JOptionPane.getWindowForComponent(parentComponent.getParent()); |
|
1634 |
} |
|
1635 |
||
1636 |
||
1637 |
/** |
|
1638 |
* Returns the specified component's desktop pane. |
|
1639 |
* |
|
1640 |
* @param parentComponent the <code>Component</code> to check for a |
|
1641 |
* desktop |
|
1642 |
* @return the <code>JDesktopPane</code> that contains the component, |
|
1643 |
* or <code>null</code> if the component is <code>null</code> |
|
1644 |
* or does not have an ancestor that is a |
|
1645 |
* <code>JInternalFrame</code> |
|
1646 |
*/ |
|
1647 |
public static JDesktopPane getDesktopPaneForComponent(Component parentComponent) { |
|
1648 |
if(parentComponent == null) |
|
1649 |
return null; |
|
1650 |
if(parentComponent instanceof JDesktopPane) |
|
1651 |
return (JDesktopPane)parentComponent; |
|
1652 |
return getDesktopPaneForComponent(parentComponent.getParent()); |
|
1653 |
} |
|
1654 |
||
1655 |
private static final Object sharedFrameKey = JOptionPane.class; |
|
1656 |
||
1657 |
/** |
|
1658 |
* Sets the frame to use for class methods in which a frame is |
|
1659 |
* not provided. |
|
1660 |
* <p> |
|
1661 |
* <strong>Note:</strong> |
|
1662 |
* It is recommended that rather than using this method you supply a valid parent. |
|
1663 |
* |
|
1664 |
* @param newRootFrame the default <code>Frame</code> to use |
|
1665 |
*/ |
|
1666 |
public static void setRootFrame(Frame newRootFrame) { |
|
1667 |
if (newRootFrame != null) { |
|
1668 |
SwingUtilities.appContextPut(sharedFrameKey, newRootFrame); |
|
1669 |
} else { |
|
1670 |
SwingUtilities.appContextRemove(sharedFrameKey); |
|
1671 |
} |
|
1672 |
} |
|
1673 |
||
1674 |
/** |
|
1675 |
* Returns the <code>Frame</code> to use for the class methods in |
|
1676 |
* which a frame is not provided. |
|
1677 |
* |
|
1678 |
* @return the default <code>Frame</code> to use |
|
1679 |
* @exception HeadlessException if |
|
1680 |
* <code>GraphicsEnvironment.isHeadless</code> returns |
|
1681 |
* <code>true</code> |
|
1682 |
* @see #setRootFrame |
|
1683 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
1684 |
*/ |
|
1685 |
public static Frame getRootFrame() throws HeadlessException { |
|
1686 |
Frame sharedFrame = |
|
1687 |
(Frame)SwingUtilities.appContextGet(sharedFrameKey); |
|
1688 |
if (sharedFrame == null) { |
|
1689 |
sharedFrame = SwingUtilities.getSharedOwnerFrame(); |
|
1690 |
SwingUtilities.appContextPut(sharedFrameKey, sharedFrame); |
|
1691 |
} |
|
1692 |
return sharedFrame; |
|
1693 |
} |
|
1694 |
||
1695 |
/** |
|
1696 |
* Creates a <code>JOptionPane</code> with a test message. |
|
1697 |
*/ |
|
1698 |
public JOptionPane() { |
|
1699 |
this("JOptionPane message"); |
|
1700 |
} |
|
1701 |
||
1702 |
/** |
|
1703 |
* Creates a instance of <code>JOptionPane</code> to display a |
|
1704 |
* message using the |
|
1705 |
* plain-message message type and the default options delivered by |
|
1706 |
* the UI. |
|
1707 |
* |
|
1708 |
* @param message the <code>Object</code> to display |
|
1709 |
*/ |
|
1710 |
public JOptionPane(Object message) { |
|
1711 |
this(message, PLAIN_MESSAGE); |
|
1712 |
} |
|
1713 |
||
1714 |
/** |
|
1715 |
* Creates an instance of <code>JOptionPane</code> to display a message |
|
1716 |
* with the specified message type and the default options, |
|
1717 |
* |
|
1718 |
* @param message the <code>Object</code> to display |
|
1719 |
* @param messageType the type of message to be displayed: |
|
1720 |
* <code>ERROR_MESSAGE</code>, |
|
1721 |
* <code>INFORMATION_MESSAGE</code>, |
|
1722 |
* <code>WARNING_MESSAGE</code>, |
|
1723 |
* <code>QUESTION_MESSAGE</code>, |
|
1724 |
* or <code>PLAIN_MESSAGE</code> |
|
1725 |
*/ |
|
1726 |
public JOptionPane(Object message, int messageType) { |
|
1727 |
this(message, messageType, DEFAULT_OPTION); |
|
1728 |
} |
|
1729 |
||
1730 |
/** |
|
1731 |
* Creates an instance of <code>JOptionPane</code> to display a message |
|
1732 |
* with the specified message type and options. |
|
1733 |
* |
|
1734 |
* @param message the <code>Object</code> to display |
|
1735 |
* @param messageType the type of message to be displayed: |
|
1736 |
* <code>ERROR_MESSAGE</code>, |
|
1737 |
* <code>INFORMATION_MESSAGE</code>, |
|
1738 |
* <code>WARNING_MESSAGE</code>, |
|
1739 |
* <code>QUESTION_MESSAGE</code>, |
|
1740 |
* or <code>PLAIN_MESSAGE</code> |
|
1741 |
* @param optionType the options to display in the pane: |
|
1742 |
* <code>DEFAULT_OPTION</code>, <code>YES_NO_OPTION</code>, |
|
1743 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
1744 |
* <code>OK_CANCEL_OPTION</code> |
|
1745 |
*/ |
|
1746 |
public JOptionPane(Object message, int messageType, int optionType) { |
|
1747 |
this(message, messageType, optionType, null); |
|
1748 |
} |
|
1749 |
||
1750 |
/** |
|
1751 |
* Creates an instance of <code>JOptionPane</code> to display a message |
|
1752 |
* with the specified message type, options, and icon. |
|
1753 |
* |
|
1754 |
* @param message the <code>Object</code> to display |
|
1755 |
* @param messageType the type of message to be displayed: |
|
1756 |
* <code>ERROR_MESSAGE</code>, |
|
1757 |
* <code>INFORMATION_MESSAGE</code>, |
|
1758 |
* <code>WARNING_MESSAGE</code>, |
|
1759 |
* <code>QUESTION_MESSAGE</code>, |
|
1760 |
* or <code>PLAIN_MESSAGE</code> |
|
1761 |
* @param optionType the options to display in the pane: |
|
1762 |
* <code>DEFAULT_OPTION</code>, <code>YES_NO_OPTION</code>, |
|
1763 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
1764 |
* <code>OK_CANCEL_OPTION</code> |
|
1765 |
* @param icon the <code>Icon</code> image to display |
|
1766 |
*/ |
|
1767 |
public JOptionPane(Object message, int messageType, int optionType, |
|
1768 |
Icon icon) { |
|
1769 |
this(message, messageType, optionType, icon, null); |
|
1770 |
} |
|
1771 |
||
1772 |
/** |
|
1773 |
* Creates an instance of <code>JOptionPane</code> to display a message |
|
1774 |
* with the specified message type, icon, and options. |
|
1775 |
* None of the options is initially selected. |
|
1776 |
* <p> |
|
1777 |
* The options objects should contain either instances of |
|
1778 |
* <code>Component</code>s, (which are added directly) or |
|
1779 |
* <code>Strings</code> (which are wrapped in a <code>JButton</code>). |
|
1780 |
* If you provide <code>Component</code>s, you must ensure that when the |
|
1781 |
* <code>Component</code> is clicked it messages <code>setValue</code> |
|
1782 |
* in the created <code>JOptionPane</code>. |
|
1783 |
* |
|
1784 |
* @param message the <code>Object</code> to display |
|
1785 |
* @param messageType the type of message to be displayed: |
|
1786 |
* <code>ERROR_MESSAGE</code>, |
|
1787 |
* <code>INFORMATION_MESSAGE</code>, |
|
1788 |
* <code>WARNING_MESSAGE</code>, |
|
1789 |
* <code>QUESTION_MESSAGE</code>, |
|
1790 |
* or <code>PLAIN_MESSAGE</code> |
|
1791 |
* @param optionType the options to display in the pane: |
|
1792 |
* <code>DEFAULT_OPTION</code>, |
|
1793 |
* <code>YES_NO_OPTION</code>, |
|
1794 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
1795 |
* <code>OK_CANCEL_OPTION</code> |
|
1796 |
* @param icon the <code>Icon</code> image to display |
|
1797 |
* @param options the choices the user can select |
|
1798 |
*/ |
|
1799 |
public JOptionPane(Object message, int messageType, int optionType, |
|
1800 |
Icon icon, Object[] options) { |
|
1801 |
this(message, messageType, optionType, icon, options, null); |
|
1802 |
} |
|
1803 |
||
1804 |
/** |
|
1805 |
* Creates an instance of <code>JOptionPane</code> to display a message |
|
1806 |
* with the specified message type, icon, and options, with the |
|
1807 |
* initially-selected option specified. |
|
1808 |
* |
|
1809 |
* @param message the <code>Object</code> to display |
|
1810 |
* @param messageType the type of message to be displayed: |
|
1811 |
* <code>ERROR_MESSAGE</code>, |
|
1812 |
* <code>INFORMATION_MESSAGE</code>, |
|
1813 |
* <code>WARNING_MESSAGE</code>, |
|
1814 |
* <code>QUESTION_MESSAGE</code>, |
|
1815 |
* or <code>PLAIN_MESSAGE</code> |
|
1816 |
* @param optionType the options to display in the pane: |
|
1817 |
* <code>DEFAULT_OPTION</code>, |
|
1818 |
* <code>YES_NO_OPTION</code>, |
|
1819 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
1820 |
* <code>OK_CANCEL_OPTION</code> |
|
1821 |
* @param icon the Icon image to display |
|
1822 |
* @param options the choices the user can select |
|
1823 |
* @param initialValue the choice that is initially selected; if |
|
1824 |
* <code>null</code>, then nothing will be initially selected; |
|
1825 |
* only meaningful if <code>options</code> is used |
|
1826 |
*/ |
|
1827 |
public JOptionPane(Object message, int messageType, int optionType, |
|
1828 |
Icon icon, Object[] options, Object initialValue) { |
|
1829 |
||
1830 |
this.message = message; |
|
1831 |
this.options = options; |
|
1832 |
this.initialValue = initialValue; |
|
1833 |
this.icon = icon; |
|
1834 |
setMessageType(messageType); |
|
1835 |
setOptionType(optionType); |
|
1836 |
value = UNINITIALIZED_VALUE; |
|
1837 |
inputValue = UNINITIALIZED_VALUE; |
|
1838 |
updateUI(); |
|
1839 |
} |
|
1840 |
||
1841 |
/** |
|
1842 |
* Sets the UI object which implements the L&F for this component. |
|
1843 |
* |
|
1844 |
* @param ui the <code>OptionPaneUI</code> L&F object |
|
1845 |
* @see UIDefaults#getUI |
|
1846 |
* @beaninfo |
|
1847 |
* bound: true |
|
1848 |
* hidden: true |
|
1849 |
* description: The UI object that implements the optionpane's LookAndFeel |
|
1850 |
*/ |
|
1851 |
public void setUI(OptionPaneUI ui) { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1852 |
if (this.ui != ui) { |
2 | 1853 |
super.setUI(ui); |
1854 |
invalidate(); |
|
1855 |
} |
|
1856 |
} |
|
1857 |
||
1858 |
/** |
|
1859 |
* Returns the UI object which implements the L&F for this component. |
|
1860 |
* |
|
1861 |
* @return the <code>OptionPaneUI</code> object |
|
1862 |
*/ |
|
1863 |
public OptionPaneUI getUI() { |
|
1864 |
return (OptionPaneUI)ui; |
|
1865 |
} |
|
1866 |
||
1867 |
/** |
|
1868 |
* Notification from the <code>UIManager</code> that the L&F has changed. |
|
1869 |
* Replaces the current UI object with the latest version from the |
|
1870 |
* <code>UIManager</code>. |
|
1871 |
* |
|
1872 |
* @see JComponent#updateUI |
|
1873 |
*/ |
|
1874 |
public void updateUI() { |
|
1875 |
setUI((OptionPaneUI)UIManager.getUI(this)); |
|
1876 |
} |
|
1877 |
||
1878 |
||
1879 |
/** |
|
1880 |
* Returns the name of the UI class that implements the |
|
1881 |
* L&F for this component. |
|
1882 |
* |
|
1883 |
* @return the string "OptionPaneUI" |
|
1884 |
* @see JComponent#getUIClassID |
|
1885 |
* @see UIDefaults#getUI |
|
1886 |
*/ |
|
1887 |
public String getUIClassID() { |
|
1888 |
return uiClassID; |
|
1889 |
} |
|
1890 |
||
1891 |
||
1892 |
/** |
|
1893 |
* Sets the option pane's message-object. |
|
1894 |
* @param newMessage the <code>Object</code> to display |
|
1895 |
* @see #getMessage |
|
1896 |
* |
|
1897 |
* @beaninfo |
|
1898 |
* preferred: true |
|
1899 |
* bound: true |
|
1900 |
* description: The optionpane's message object. |
|
1901 |
*/ |
|
1902 |
public void setMessage(Object newMessage) { |
|
1903 |
Object oldMessage = message; |
|
1904 |
||
1905 |
message = newMessage; |
|
1906 |
firePropertyChange(MESSAGE_PROPERTY, oldMessage, message); |
|
1907 |
} |
|
1908 |
||
1909 |
/** |
|
1910 |
* Returns the message-object this pane displays. |
|
1911 |
* @see #setMessage |
|
1912 |
* |
|
1913 |
* @return the <code>Object</code> that is displayed |
|
1914 |
*/ |
|
1915 |
public Object getMessage() { |
|
1916 |
return message; |
|
1917 |
} |
|
1918 |
||
1919 |
/** |
|
1920 |
* Sets the icon to display. If non-<code>null</code>, the look and feel |
|
1921 |
* does not provide an icon. |
|
1922 |
* @param newIcon the <code>Icon</code> to display |
|
1923 |
* |
|
1924 |
* @see #getIcon |
|
1925 |
* @beaninfo |
|
1926 |
* preferred: true |
|
1927 |
* bound: true |
|
1928 |
* description: The option pane's type icon. |
|
1929 |
*/ |
|
1930 |
public void setIcon(Icon newIcon) { |
|
1931 |
Object oldIcon = icon; |
|
1932 |
||
1933 |
icon = newIcon; |
|
1934 |
firePropertyChange(ICON_PROPERTY, oldIcon, icon); |
|
1935 |
} |
|
1936 |
||
1937 |
/** |
|
1938 |
* Returns the icon this pane displays. |
|
1939 |
* @return the <code>Icon</code> that is displayed |
|
1940 |
* |
|
1941 |
* @see #setIcon |
|
1942 |
*/ |
|
1943 |
public Icon getIcon() { |
|
1944 |
return icon; |
|
1945 |
} |
|
1946 |
||
1947 |
/** |
|
1948 |
* Sets the value the user has chosen. |
|
1949 |
* @param newValue the chosen value |
|
1950 |
* |
|
1951 |
* @see #getValue |
|
1952 |
* @beaninfo |
|
1953 |
* preferred: true |
|
1954 |
* bound: true |
|
1955 |
* description: The option pane's value object. |
|
1956 |
*/ |
|
1957 |
public void setValue(Object newValue) { |
|
1958 |
Object oldValue = value; |
|
1959 |
||
1960 |
value = newValue; |
|
1961 |
firePropertyChange(VALUE_PROPERTY, oldValue, value); |
|
1962 |
} |
|
1963 |
||
1964 |
/** |
|
1965 |
* Returns the value the user has selected. <code>UNINITIALIZED_VALUE</code> |
|
1966 |
* implies the user has not yet made a choice, <code>null</code> means the |
|
1967 |
* user closed the window with out choosing anything. Otherwise |
|
1968 |
* the returned value will be one of the options defined in this |
|
1969 |
* object. |
|
1970 |
* |
|
1971 |
* @return the <code>Object</code> chosen by the user, |
|
1972 |
* <code>UNINITIALIZED_VALUE</code> |
|
1973 |
* if the user has not yet made a choice, or <code>null</code> if |
|
1974 |
* the user closed the window without making a choice |
|
1975 |
* |
|
1976 |
* @see #setValue |
|
1977 |
*/ |
|
1978 |
public Object getValue() { |
|
1979 |
return value; |
|
1980 |
} |
|
1981 |
||
1982 |
/** |
|
1983 |
* Sets the options this pane displays. If an element in |
|
1984 |
* <code>newOptions</code> is a <code>Component</code> |
|
1985 |
* it is added directly to the pane, |
|
1986 |
* otherwise a button is created for the element. |
|
1987 |
* |
|
1988 |
* @param newOptions an array of <code>Objects</code> that create the |
|
1989 |
* buttons the user can click on, or arbitrary |
|
1990 |
* <code>Components</code> to add to the pane |
|
1991 |
* |
|
1992 |
* @see #getOptions |
|
1993 |
* @beaninfo |
|
1994 |
* bound: true |
|
1995 |
* description: The option pane's options objects. |
|
1996 |
*/ |
|
1997 |
public void setOptions(Object[] newOptions) { |
|
1998 |
Object[] oldOptions = options; |
|
1999 |
||
2000 |
options = newOptions; |
|
2001 |
firePropertyChange(OPTIONS_PROPERTY, oldOptions, options); |
|
2002 |
} |
|
2003 |
||
2004 |
/** |
|
2005 |
* Returns the choices the user can make. |
|
2006 |
* @return the array of <code>Objects</code> that give the user's choices |
|
2007 |
* |
|
2008 |
* @see #setOptions |
|
2009 |
*/ |
|
2010 |
public Object[] getOptions() { |
|
2011 |
if(options != null) { |
|
2012 |
int optionCount = options.length; |
|
2013 |
Object[] retOptions = new Object[optionCount]; |
|
2014 |
||
2015 |
System.arraycopy(options, 0, retOptions, 0, optionCount); |
|
2016 |
return retOptions; |
|
2017 |
} |
|
2018 |
return options; |
|
2019 |
} |
|
2020 |
||
2021 |
/** |
|
2022 |
* Sets the initial value that is to be enabled -- the |
|
2023 |
* <code>Component</code> |
|
2024 |
* that has the focus when the pane is initially displayed. |
|
2025 |
* |
|
2026 |
* @param newInitialValue the <code>Object</code> that gets the initial |
|
2027 |
* keyboard focus |
|
2028 |
* |
|
2029 |
* @see #getInitialValue |
|
2030 |
* @beaninfo |
|
2031 |
* preferred: true |
|
2032 |
* bound: true |
|
2033 |
* description: The option pane's initial value object. |
|
2034 |
*/ |
|
2035 |
public void setInitialValue(Object newInitialValue) { |
|
2036 |
Object oldIV = initialValue; |
|
2037 |
||
2038 |
initialValue = newInitialValue; |
|
2039 |
firePropertyChange(INITIAL_VALUE_PROPERTY, oldIV, initialValue); |
|
2040 |
} |
|
2041 |
||
2042 |
/** |
|
2043 |
* Returns the initial value. |
|
2044 |
* |
|
2045 |
* @return the <code>Object</code> that gets the initial keyboard focus |
|
2046 |
* |
|
2047 |
* @see #setInitialValue |
|
2048 |
*/ |
|
2049 |
public Object getInitialValue() { |
|
2050 |
return initialValue; |
|
2051 |
} |
|
2052 |
||
2053 |
/** |
|
2054 |
* Sets the option pane's message type. |
|
2055 |
* The message type is used by the Look and Feel to determine the |
|
2056 |
* icon to display (if not supplied) as well as potentially how to |
|
2057 |
* lay out the <code>parentComponent</code>. |
|
2058 |
* @param newType an integer specifying the kind of message to display: |
|
2059 |
* <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>, |
|
2060 |
* <code>WARNING_MESSAGE</code>, |
|
2061 |
* <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code> |
|
2062 |
* @exception RuntimeException if <code>newType</code> is not one of the |
|
2063 |
* legal values listed above |
|
2064 |
||
2065 |
* @see #getMessageType |
|
2066 |
* @beaninfo |
|
2067 |
* preferred: true |
|
2068 |
* bound: true |
|
2069 |
* description: The option pane's message type. |
|
2070 |
*/ |
|
2071 |
public void setMessageType(int newType) { |
|
2072 |
if(newType != ERROR_MESSAGE && newType != INFORMATION_MESSAGE && |
|
2073 |
newType != WARNING_MESSAGE && newType != QUESTION_MESSAGE && |
|
2074 |
newType != PLAIN_MESSAGE) |
|
2075 |
throw new RuntimeException("JOptionPane: type must be one of JOptionPane.ERROR_MESSAGE, JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE, JOptionPane.QUESTION_MESSAGE or JOptionPane.PLAIN_MESSAGE"); |
|
2076 |
||
2077 |
int oldType = messageType; |
|
2078 |
||
2079 |
messageType = newType; |
|
2080 |
firePropertyChange(MESSAGE_TYPE_PROPERTY, oldType, messageType); |
|
2081 |
} |
|
2082 |
||
2083 |
/** |
|
2084 |
* Returns the message type. |
|
2085 |
* |
|
2086 |
* @return an integer specifying the message type |
|
2087 |
* |
|
2088 |
* @see #setMessageType |
|
2089 |
*/ |
|
2090 |
public int getMessageType() { |
|
2091 |
return messageType; |
|
2092 |
} |
|
2093 |
||
2094 |
/** |
|
2095 |
* Sets the options to display. |
|
2096 |
* The option type is used by the Look and Feel to |
|
2097 |
* determine what buttons to show (unless options are supplied). |
|
2098 |
* @param newType an integer specifying the options the L&F is to display: |
|
2099 |
* <code>DEFAULT_OPTION</code>, |
|
2100 |
* <code>YES_NO_OPTION</code>, |
|
2101 |
* <code>YES_NO_CANCEL_OPTION</code>, |
|
2102 |
* or <code>OK_CANCEL_OPTION</code> |
|
2103 |
* @exception RuntimeException if <code>newType</code> is not one of |
|
2104 |
* the legal values listed above |
|
2105 |
* |
|
2106 |
* @see #getOptionType |
|
2107 |
* @see #setOptions |
|
2108 |
* @beaninfo |
|
2109 |
* preferred: true |
|
2110 |
* bound: true |
|
2111 |
* description: The option pane's option type. |
|
2112 |
*/ |
|
2113 |
public void setOptionType(int newType) { |
|
2114 |
if(newType != DEFAULT_OPTION && newType != YES_NO_OPTION && |
|
2115 |
newType != YES_NO_CANCEL_OPTION && newType != OK_CANCEL_OPTION) |
|
2116 |
throw new RuntimeException("JOptionPane: option type must be one of JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION, JOptionPane.YES_NO_CANCEL_OPTION or JOptionPane.OK_CANCEL_OPTION"); |
|
2117 |
||
2118 |
int oldType = optionType; |
|
2119 |
||
2120 |
optionType = newType; |
|
2121 |
firePropertyChange(OPTION_TYPE_PROPERTY, oldType, optionType); |
|
2122 |
} |
|
2123 |
||
2124 |
/** |
|
2125 |
* Returns the type of options that are displayed. |
|
2126 |
* |
|
2127 |
* @return an integer specifying the user-selectable options |
|
2128 |
* |
|
2129 |
* @see #setOptionType |
|
2130 |
*/ |
|
2131 |
public int getOptionType() { |
|
2132 |
return optionType; |
|
2133 |
} |
|
2134 |
||
2135 |
/** |
|
2136 |
* Sets the input selection values for a pane that provides the user |
|
2137 |
* with a list of items to choose from. (The UI provides a widget |
|
2138 |
* for choosing one of the values.) A <code>null</code> value |
|
2139 |
* implies the user can input whatever they wish, usually by means |
|
2140 |
* of a <code>JTextField</code>. |
|
2141 |
* <p> |
|
2142 |
* Sets <code>wantsInput</code> to true. Use |
|
2143 |
* <code>setInitialSelectionValue</code> to specify the initially-chosen |
|
2144 |
* value. After the pane as been enabled, <code>inputValue</code> is |
|
2145 |
* set to the value the user has selected. |
|
2146 |
* @param newValues an array of <code>Objects</code> the user to be |
|
2147 |
* displayed |
|
2148 |
* (usually in a list or combo-box) from which |
|
2149 |
* the user can make a selection |
|
2150 |
* @see #setWantsInput |
|
2151 |
* @see #setInitialSelectionValue |
|
2152 |
* @see #getSelectionValues |
|
2153 |
* @beaninfo |
|
2154 |
* bound: true |
|
2155 |
* description: The option pane's selection values. |
|
2156 |
*/ |
|
2157 |
public void setSelectionValues(Object[] newValues) { |
|
2158 |
Object[] oldValues = selectionValues; |
|
2159 |
||
2160 |
selectionValues = newValues; |
|
2161 |
firePropertyChange(SELECTION_VALUES_PROPERTY, oldValues, newValues); |
|
2162 |
if(selectionValues != null) |
|
2163 |
setWantsInput(true); |
|
2164 |
} |
|
2165 |
||
2166 |
/** |
|
2167 |
* Returns the input selection values. |
|
2168 |
* |
|
2169 |
* @return the array of <code>Objects</code> the user can select |
|
2170 |
* @see #setSelectionValues |
|
2171 |
*/ |
|
2172 |
public Object[] getSelectionValues() { |
|
2173 |
return selectionValues; |
|
2174 |
} |
|
2175 |
||
2176 |
/** |
|
2177 |
* Sets the input value that is initially displayed as selected to the user. |
|
2178 |
* Only used if <code>wantsInput</code> is true. |
|
2179 |
* @param newValue the initially selected value |
|
2180 |
* @see #setSelectionValues |
|
2181 |
* @see #getInitialSelectionValue |
|
2182 |
* @beaninfo |
|
2183 |
* bound: true |
|
2184 |
* description: The option pane's initial selection value object. |
|
2185 |
*/ |
|
2186 |
public void setInitialSelectionValue(Object newValue) { |
|
2187 |
Object oldValue = initialSelectionValue; |
|
2188 |
||
2189 |
initialSelectionValue = newValue; |
|
2190 |
firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, oldValue, |
|
2191 |
newValue); |
|
2192 |
} |
|
2193 |
||
2194 |
/** |
|
2195 |
* Returns the input value that is displayed as initially selected to the user. |
|
2196 |
* |
|
2197 |
* @return the initially selected value |
|
2198 |
* @see #setInitialSelectionValue |
|
2199 |
* @see #setSelectionValues |
|
2200 |
*/ |
|
2201 |
public Object getInitialSelectionValue() { |
|
2202 |
return initialSelectionValue; |
|
2203 |
} |
|
2204 |
||
2205 |
/** |
|
2206 |
* Sets the input value that was selected or input by the user. |
|
2207 |
* Only used if <code>wantsInput</code> is true. Note that this method |
|
2208 |
* is invoked internally by the option pane (in response to user action) |
|
2209 |
* and should generally not be called by client programs. To set the |
|
2210 |
* input value initially displayed as selected to the user, use |
|
2211 |
* <code>setInitialSelectionValue</code>. |
|
2212 |
* |
|
2213 |
* @param newValue the <code>Object</code> used to set the |
|
2214 |
* value that the user specified (usually in a text field) |
|
2215 |
* @see #setSelectionValues |
|
2216 |
* @see #setInitialSelectionValue |
|
2217 |
* @see #setWantsInput |
|
2218 |
* @see #getInputValue |
|
2219 |
* @beaninfo |
|
2220 |
* preferred: true |
|
2221 |
* bound: true |
|
2222 |
* description: The option pane's input value object. |
|
2223 |
*/ |
|
2224 |
public void setInputValue(Object newValue) { |
|
2225 |
Object oldValue = inputValue; |
|
2226 |
||
2227 |
inputValue = newValue; |
|
2228 |
firePropertyChange(INPUT_VALUE_PROPERTY, oldValue, newValue); |
|
2229 |
} |
|
2230 |
||
2231 |
/** |
|
2232 |
* Returns the value the user has input, if <code>wantsInput</code> |
|
2233 |
* is true. |
|
2234 |
* |
|
2235 |
* @return the <code>Object</code> the user specified, |
|
2236 |
* if it was one of the objects, or a |
|
2237 |
* <code>String</code> if it was a value typed into a |
|
2238 |
* field |
|
2239 |
* @see #setSelectionValues |
|
2240 |
* @see #setWantsInput |
|
2241 |
* @see #setInputValue |
|
2242 |
*/ |
|
2243 |
public Object getInputValue() { |
|
2244 |
return inputValue; |
|
2245 |
} |
|
2246 |
||
2247 |
/** |
|
2248 |
* Returns the maximum number of characters to place on a line in a |
|
2249 |
* message. Default is to return <code>Integer.MAX_VALUE</code>. |
|
2250 |
* The value can be |
|
2251 |
* changed by overriding this method in a subclass. |
|
2252 |
* |
|
2253 |
* @return an integer giving the maximum number of characters on a line |
|
2254 |
*/ |
|
2255 |
public int getMaxCharactersPerLineCount() { |
|
2256 |
return Integer.MAX_VALUE; |
|
2257 |
} |
|
2258 |
||
2259 |
/** |
|
2260 |
* Sets the <code>wantsInput</code> property. |
|
2261 |
* If <code>newValue</code> is true, an input component |
|
2262 |
* (such as a text field or combo box) whose parent is |
|
2263 |
* <code>parentComponent</code> is provided to |
|
2264 |
* allow the user to input a value. If <code>getSelectionValues</code> |
|
2265 |
* returns a non-<code>null</code> array, the input value is one of the |
|
2266 |
* objects in that array. Otherwise the input value is whatever |
|
2267 |
* the user inputs. |
|
2268 |
* <p> |
|
2269 |
* This is a bound property. |
|
2270 |
* |
|
2271 |
* @see #setSelectionValues |
|
2272 |
* @see #setInputValue |
|
2273 |
* @beaninfo |
|
2274 |
* preferred: true |
|
2275 |
* bound: true |
|
2276 |
* description: Flag which allows the user to input a value. |
|
2277 |
*/ |
|
2278 |
public void setWantsInput(boolean newValue) { |
|
2279 |
boolean oldValue = wantsInput; |
|
2280 |
||
2281 |
wantsInput = newValue; |
|
2282 |
firePropertyChange(WANTS_INPUT_PROPERTY, oldValue, newValue); |
|
2283 |
} |
|
2284 |
||
2285 |
/** |
|
2286 |
* Returns the value of the <code>wantsInput</code> property. |
|
2287 |
* |
|
2288 |
* @return true if an input component will be provided |
|
2289 |
* @see #setWantsInput |
|
2290 |
*/ |
|
2291 |
public boolean getWantsInput() { |
|
2292 |
return wantsInput; |
|
2293 |
} |
|
2294 |
||
2295 |
/** |
|
2296 |
* Requests that the initial value be selected, which will set |
|
2297 |
* focus to the initial value. This method |
|
2298 |
* should be invoked after the window containing the option pane |
|
2299 |
* is made visible. |
|
2300 |
*/ |
|
2301 |
public void selectInitialValue() { |
|
2302 |
OptionPaneUI ui = getUI(); |
|
2303 |
if (ui != null) { |
|
2304 |
ui.selectInitialValue(this); |
|
2305 |
} |
|
2306 |
} |
|
2307 |
||
2308 |
||
2309 |
private static int styleFromMessageType(int messageType) { |
|
2310 |
switch (messageType) { |
|
2311 |
case ERROR_MESSAGE: |
|
2312 |
return JRootPane.ERROR_DIALOG; |
|
2313 |
case QUESTION_MESSAGE: |
|
2314 |
return JRootPane.QUESTION_DIALOG; |
|
2315 |
case WARNING_MESSAGE: |
|
2316 |
return JRootPane.WARNING_DIALOG; |
|
2317 |
case INFORMATION_MESSAGE: |
|
2318 |
return JRootPane.INFORMATION_DIALOG; |
|
2319 |
case PLAIN_MESSAGE: |
|
2320 |
default: |
|
2321 |
return JRootPane.PLAIN_DIALOG; |
|
2322 |
} |
|
2323 |
} |
|
2324 |
||
2325 |
// Serialization support. |
|
2326 |
private void writeObject(ObjectOutputStream s) throws IOException { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
2327 |
Vector<Object> values = new Vector<Object>(); |
2 | 2328 |
|
2329 |
s.defaultWriteObject(); |
|
2330 |
// Save the icon, if its Serializable. |
|
2331 |
if(icon != null && icon instanceof Serializable) { |
|
2332 |
values.addElement("icon"); |
|
2333 |
values.addElement(icon); |
|
2334 |
} |
|
2335 |
// Save the message, if its Serializable. |
|
2336 |
if(message != null && message instanceof Serializable) { |
|
2337 |
values.addElement("message"); |
|
2338 |
values.addElement(message); |
|
2339 |
} |
|
2340 |
// Save the treeModel, if its Serializable. |
|
2341 |
if(options != null) { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
2342 |
Vector<Object> serOptions = new Vector<Object>(); |
2 | 2343 |
|
2344 |
for(int counter = 0, maxCounter = options.length; |
|
2345 |
counter < maxCounter; counter++) |
|
2346 |
if(options[counter] instanceof Serializable) |
|
2347 |
serOptions.addElement(options[counter]); |
|
2348 |
if(serOptions.size() > 0) { |
|
2349 |
int optionCount = serOptions.size(); |
|
2350 |
Object[] arrayOptions = new Object[optionCount]; |
|
2351 |
||
2352 |
serOptions.copyInto(arrayOptions); |
|
2353 |
values.addElement("options"); |
|
2354 |
values.addElement(arrayOptions); |
|
2355 |
} |
|
2356 |
} |
|
2357 |
// Save the initialValue, if its Serializable. |
|
2358 |
if(initialValue != null && initialValue instanceof Serializable) { |
|
2359 |
values.addElement("initialValue"); |
|
2360 |
values.addElement(initialValue); |
|
2361 |
} |
|
2362 |
// Save the value, if its Serializable. |
|
2363 |
if(value != null && value instanceof Serializable) { |
|
2364 |
values.addElement("value"); |
|
2365 |
values.addElement(value); |
|
2366 |
} |
|
2367 |
// Save the selectionValues, if its Serializable. |
|
2368 |
if(selectionValues != null) { |
|
2369 |
boolean serialize = true; |
|
2370 |
||
2371 |
for(int counter = 0, maxCounter = selectionValues.length; |
|
2372 |
counter < maxCounter; counter++) { |
|
2373 |
if(selectionValues[counter] != null && |
|
2374 |
!(selectionValues[counter] instanceof Serializable)) { |
|
2375 |
serialize = false; |
|
2376 |
break; |
|
2377 |
} |
|
2378 |
} |
|
2379 |
if(serialize) { |
|
2380 |
values.addElement("selectionValues"); |
|
2381 |
values.addElement(selectionValues); |
|
2382 |
} |
|
2383 |
} |
|
2384 |
// Save the inputValue, if its Serializable. |
|
2385 |
if(inputValue != null && inputValue instanceof Serializable) { |
|
2386 |
values.addElement("inputValue"); |
|
2387 |
values.addElement(inputValue); |
|
2388 |
} |
|
2389 |
// Save the initialSelectionValue, if its Serializable. |
|
2390 |
if(initialSelectionValue != null && |
|
2391 |
initialSelectionValue instanceof Serializable) { |
|
2392 |
values.addElement("initialSelectionValue"); |
|
2393 |
values.addElement(initialSelectionValue); |
|
2394 |
} |
|
2395 |
s.writeObject(values); |
|
2396 |
} |
|
2397 |
||
2398 |
private void readObject(ObjectInputStream s) |
|
2399 |
throws IOException, ClassNotFoundException { |
|
2400 |
s.defaultReadObject(); |
|
2401 |
||
2402 |
Vector values = (Vector)s.readObject(); |
|
2403 |
int indexCounter = 0; |
|
2404 |
int maxCounter = values.size(); |
|
2405 |
||
2406 |
if(indexCounter < maxCounter && values.elementAt(indexCounter). |
|
2407 |
equals("icon")) { |
|
2408 |
icon = (Icon)values.elementAt(++indexCounter); |
|
2409 |
indexCounter++; |
|
2410 |
} |
|
2411 |
if(indexCounter < maxCounter && values.elementAt(indexCounter). |
|
2412 |
equals("message")) { |
|
2413 |
message = values.elementAt(++indexCounter); |
|
2414 |
indexCounter++; |
|
2415 |
} |
|
2416 |
if(indexCounter < maxCounter && values.elementAt(indexCounter). |
|
2417 |
equals("options")) { |
|
2418 |
options = (Object[])values.elementAt(++indexCounter); |
|
2419 |
indexCounter++; |
|
2420 |
} |
|
2421 |
if(indexCounter < maxCounter && values.elementAt(indexCounter). |
|
2422 |
equals("initialValue")) { |
|
2423 |
initialValue = values.elementAt(++indexCounter); |
|
2424 |
indexCounter++; |
|
2425 |
} |
|
2426 |
if(indexCounter < maxCounter && values.elementAt(indexCounter). |
|
2427 |
equals("value")) { |
|
2428 |
value = values.elementAt(++indexCounter); |
|
2429 |
indexCounter++; |
|
2430 |
} |
|
2431 |
if(indexCounter < maxCounter && values.elementAt(indexCounter). |
|
2432 |
equals("selectionValues")) { |
|
2433 |
selectionValues = (Object[])values.elementAt(++indexCounter); |
|
2434 |
indexCounter++; |
|
2435 |
} |
|
2436 |
if(indexCounter < maxCounter && values.elementAt(indexCounter). |
|
2437 |
equals("inputValue")) { |
|
2438 |
inputValue = values.elementAt(++indexCounter); |
|
2439 |
indexCounter++; |
|
2440 |
} |
|
2441 |
if(indexCounter < maxCounter && values.elementAt(indexCounter). |
|
2442 |
equals("initialSelectionValue")) { |
|
2443 |
initialSelectionValue = values.elementAt(++indexCounter); |
|
2444 |
indexCounter++; |
|
2445 |
} |
|
2446 |
if (getUIClassID().equals(uiClassID)) { |
|
2447 |
byte count = JComponent.getWriteObjCounter(this); |
|
2448 |
JComponent.setWriteObjCounter(this, --count); |
|
2449 |
if (count == 0 && ui != null) { |
|
2450 |
ui.installUI(this); |
|
2451 |
} |
|
2452 |
} |
|
2453 |
} |
|
2454 |
||
2455 |
||
2456 |
/** |
|
2457 |
* Returns a string representation of this <code>JOptionPane</code>. |
|
2458 |
* This method |
|
2459 |
* is intended to be used only for debugging purposes, and the |
|
2460 |
* content and format of the returned string may vary between |
|
2461 |
* implementations. The returned string may be empty but may not |
|
2462 |
* be <code>null</code>. |
|
2463 |
* |
|
2464 |
* @return a string representation of this <code>JOptionPane</code> |
|
2465 |
*/ |
|
2466 |
protected String paramString() { |
|
2467 |
String iconString = (icon != null ? |
|
2468 |
icon.toString() : ""); |
|
2469 |
String initialValueString = (initialValue != null ? |
|
2470 |
initialValue.toString() : ""); |
|
2471 |
String messageString = (message != null ? |
|
2472 |
message.toString() : ""); |
|
2473 |
String messageTypeString; |
|
2474 |
if (messageType == ERROR_MESSAGE) { |
|
2475 |
messageTypeString = "ERROR_MESSAGE"; |
|
2476 |
} else if (messageType == INFORMATION_MESSAGE) { |
|
2477 |
messageTypeString = "INFORMATION_MESSAGE"; |
|
2478 |
} else if (messageType == WARNING_MESSAGE) { |
|
2479 |
messageTypeString = "WARNING_MESSAGE"; |
|
2480 |
} else if (messageType == QUESTION_MESSAGE) { |
|
2481 |
messageTypeString = "QUESTION_MESSAGE"; |
|
2482 |
} else if (messageType == PLAIN_MESSAGE) { |
|
2483 |
messageTypeString = "PLAIN_MESSAGE"; |
|
2484 |
} else messageTypeString = ""; |
|
2485 |
String optionTypeString; |
|
2486 |
if (optionType == DEFAULT_OPTION) { |
|
2487 |
optionTypeString = "DEFAULT_OPTION"; |
|
2488 |
} else if (optionType == YES_NO_OPTION) { |
|
2489 |
optionTypeString = "YES_NO_OPTION"; |
|
2490 |
} else if (optionType == YES_NO_CANCEL_OPTION) { |
|
2491 |
optionTypeString = "YES_NO_CANCEL_OPTION"; |
|
2492 |
} else if (optionType == OK_CANCEL_OPTION) { |
|
2493 |
optionTypeString = "OK_CANCEL_OPTION"; |
|
2494 |
} else optionTypeString = ""; |
|
2495 |
String wantsInputString = (wantsInput ? |
|
2496 |
"true" : "false"); |
|
2497 |
||
2498 |
return super.paramString() + |
|
2499 |
",icon=" + iconString + |
|
2500 |
",initialValue=" + initialValueString + |
|
2501 |
",message=" + messageString + |
|
2502 |
",messageType=" + messageTypeString + |
|
2503 |
",optionType=" + optionTypeString + |
|
2504 |
",wantsInput=" + wantsInputString; |
|
2505 |
} |
|
2506 |
||
2507 |
/** |
|
2508 |
* Retrieves a method from the provided class and makes it accessible. |
|
2509 |
*/ |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
2510 |
private static class ModalPrivilegedAction implements PrivilegedAction<Method> { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
2511 |
private Class<?> clazz; |
2 | 2512 |
private String methodName; |
2513 |
||
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
2514 |
public ModalPrivilegedAction(Class<?> clazz, String methodName) { |
2 | 2515 |
this.clazz = clazz; |
2516 |
this.methodName = methodName; |
|
2517 |
} |
|
2518 |
||
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
2519 |
public Method run() { |
2 | 2520 |
Method method = null; |
2521 |
try { |
|
2522 |
method = clazz.getDeclaredMethod(methodName, (Class[])null); |
|
2523 |
} catch (NoSuchMethodException ex) { |
|
2524 |
} |
|
2525 |
if (method != null) { |
|
2526 |
method.setAccessible(true); |
|
2527 |
} |
|
2528 |
return method; |
|
2529 |
} |
|
2530 |
} |
|
2531 |
||
2532 |
||
2533 |
||
2534 |
/////////////////// |
|
2535 |
// Accessibility support |
|
2536 |
/////////////////// |
|
2537 |
||
2538 |
/** |
|
2539 |
* Returns the <code>AccessibleContext</code> associated with this JOptionPane. |
|
2540 |
* For option panes, the <code>AccessibleContext</code> takes the form of an |
|
2541 |
* <code>AccessibleJOptionPane</code>. |
|
2542 |
* A new <code>AccessibleJOptionPane</code> instance is created if necessary. |
|
2543 |
* |
|
2544 |
* @return an AccessibleJOptionPane that serves as the |
|
2545 |
* AccessibleContext of this AccessibleJOptionPane |
|
2546 |
* @beaninfo |
|
2547 |
* expert: true |
|
2548 |
* description: The AccessibleContext associated with this option pane |
|
2549 |
*/ |
|
2550 |
public AccessibleContext getAccessibleContext() { |
|
2551 |
if (accessibleContext == null) { |
|
2552 |
accessibleContext = new AccessibleJOptionPane(); |
|
2553 |
} |
|
2554 |
return accessibleContext; |
|
2555 |
} |
|
2556 |
||
2557 |
/** |
|
2558 |
* This class implements accessibility support for the |
|
2559 |
* <code>JOptionPane</code> class. It provides an implementation of the |
|
2560 |
* Java Accessibility API appropriate to option pane user-interface |
|
2561 |
* elements. |
|
2562 |
* <p> |
|
2563 |
* <strong>Warning:</strong> |
|
2564 |
* Serialized objects of this class will not be compatible with |
|
2565 |
* future Swing releases. The current serialization support is |
|
2566 |
* appropriate for short term storage or RMI between applications running |
|
2567 |
* the same version of Swing. As of 1.4, support for long term storage |
|
2568 |
* of all JavaBeans<sup><font size="-2">TM</font></sup> |
|
2569 |
* has been added to the <code>java.beans</code> package. |
|
2570 |
* Please see {@link java.beans.XMLEncoder}. |
|
2571 |
*/ |
|
2572 |
protected class AccessibleJOptionPane extends AccessibleJComponent { |
|
2573 |
||
2574 |
/** |
|
2575 |
* Get the role of this object. |
|
2576 |
* |
|
2577 |
* @return an instance of AccessibleRole describing the role of the object |
|
2578 |
* @see AccessibleRole |
|
2579 |
*/ |
|
2580 |
public AccessibleRole getAccessibleRole() { |
|
2581 |
switch (messageType) { |
|
2582 |
case ERROR_MESSAGE: |
|
2583 |
case INFORMATION_MESSAGE: |
|
2584 |
case WARNING_MESSAGE: |
|
2585 |
return AccessibleRole.ALERT; |
|
2586 |
||
2587 |
default: |
|
2588 |
return AccessibleRole.OPTION_PANE; |
|
2589 |
} |
|
2590 |
} |
|
2591 |
||
2592 |
} // inner class AccessibleJOptionPane |
|
2593 |
} |