1. /*
  2. * @(#)JOptionPane.java 1.61 01/02/09
  3. *
  4. * Copyright 1997-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.awt.BorderLayout;
  12. import java.awt.Component;
  13. import java.awt.Container;
  14. import java.awt.Dialog;
  15. import java.awt.Dimension;
  16. import java.awt.Frame;
  17. import java.awt.Toolkit;
  18. import java.awt.Window;
  19. import java.beans.PropertyChangeEvent;
  20. import java.beans.PropertyChangeListener;
  21. import java.awt.event.WindowAdapter;
  22. import java.awt.event.WindowEvent;
  23. import java.io.IOException;
  24. import java.io.ObjectInputStream;
  25. import java.io.ObjectOutputStream;
  26. import java.io.Serializable;
  27. import java.util.Vector;
  28. import javax.swing.plaf.OptionPaneUI;
  29. import javax.accessibility.*;
  30. /**
  31. * <code>JOptionPane</code> makes it easy to pop up a standard dialog box that
  32. * prompts users for a value or informs them of something.
  33. * For information about using <code>JOptionPane</code>, see
  34. * <a
  35. href="http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html">How to Make Dialogs</a>,
  36. * a section in <em>The Java Tutorial</em>.
  37. *
  38. * <p>
  39. *
  40. * While the <code>JOptionPane</code>
  41. * class may appear complex because of the large number of methods, almost
  42. * all uses of this class are one-line calls to one of the static
  43. * <code>showXxxDialog</code> methods shown below:
  44. * <blockquote>
  45. * <table>
  46. * <tr align=top><td>showConfirmDialog<td>Asks a confirming question,
  47. * like yes/no/cancel.
  48. * <tr align=top><td>showInputDialog<td>Prompt for some input.
  49. * <tr align=top><td>showMessageDialog<td>Tell the user about something
  50. * that has happened.
  51. * <tr align=top><td>showOptionDialog<td>The Grand Unification of the above three.
  52. * </table>
  53. * </blockquote>
  54. * Each of these methods also comes in a <code>showInternalXXX</code>
  55. * flavor, which uses an internal frame to hold the dialog box (see
  56. * {@link JInternalFrame}).
  57. * Multiple convenience methods have also been defined -- overloaded
  58. * versions of the basic methods that use different parameter lists.
  59. * <p>
  60. * All dialogs are modal. Each <code>showXxxDialog</code> method blocks
  61. * the current thread until the user's interaction is complete.
  62. * <p>
  63. * <table cellspacing=6 cellpadding=4 border=0 align=right>
  64. * <tr>
  65. * <td bgcolor=#FFe0d0 rowspan=2>
  66. * icon
  67. * <td bgcolor=#FFe0d0>
  68. * message
  69. * <tr>
  70. * <td bgcolor=#FFe0d0>
  71. * input value
  72. * <tr>
  73. * <td bgcolor=#FFe0d0 colspan=2>
  74. * option buttons
  75. * </table>
  76. * The basic appearance of one of these dialog boxes is generally
  77. * similar to the picture at the right, although the various
  78. * look-and-feels are
  79. * ultimatly responsible for the final result.
  80. * <br clear=all>
  81. * <p>
  82. * <b>Parameters:</b><br>
  83. * The parameters to these methods follow consistent patterns:
  84. * <blockquote>
  85. * <dl compact>
  86. * <dt>parentComponent<dd>
  87. * Defines the <code>Component</code> that is to be the parent of this
  88. * dialog box.
  89. * It is used in two ways: the <code>Frame</code> that contains
  90. * it is used as the <code>Frame</code>
  91. * parent for the dialog box, and its screen coordinates are used in
  92. * the placement of the dialog box. In general, the dialog box is placed
  93. * just below the component. This parameter may be <code>null</code>,
  94. * in which case a default <code>Frame</code> is used as the parent,
  95. * and the dialog will be
  96. * centered on the screen (depending on the L&F).
  97. * <dt><a name=message>message</a><dd>
  98. * A descriptive message to be placed in the dialog box.
  99. * In the most common usage, message is just a <code>String</code> or
  100. * <code>String</code> constant.
  101. * However, the type of this parameter is actually <code>Object</code>. Its
  102. * interpretation depends on its type:
  103. * <dl compact>
  104. * <dt>Object[]<dd>An array of objects is interpreted as a series of
  105. * messages (one per object) arranged in a vertical stack.
  106. * The interpretation is recursive -- each object in the
  107. * array is interpreted according to its type.
  108. * <dt>Component<dd>The <code>Component</code> is displayed in the dialog.
  109. * <dt>Icon<dd>The <code>Icon</code> is wrapped in a <code>JLabel</code>
  110. * and displayed in the dialog.
  111. * <dt>others<dd>The object is converted to a <code>String</code> by calling
  112. * its <code>toString</code> method. The result is wrapped in a
  113. * <code>JLabel</code> and displayed.
  114. * </dl>
  115. * <dt>messageType<dd>Defines the style of the message. The Look and Feel
  116. * manager may lay out the dialog differently depending on this value, and
  117. * will often provide a default icon. The possible values are:
  118. * <ul>
  119. * <li>ERROR_MESSAGE
  120. * <li>INFORMATION_MESSAGE
  121. * <li>WARNING_MESSAGE
  122. * <li>QUESTION_MESSAGE
  123. * <li>PLAIN_MESSAGE
  124. * </ul>
  125. * <dt>optionType<dd>Defines the set of option buttons that appear at
  126. * the bottom of the dialog box:
  127. * <ul>
  128. * <li>DEFAULT_OPTION
  129. * <li>YES_NO_OPTION
  130. * <li>YES_NO_CANCEL_OPTION
  131. * <li>OK_CANCEL_OPTION
  132. * </ul>
  133. * You aren't limited to this set of option buttons. You can provide any
  134. * buttons you want using the options parameter.
  135. * <dt>options<dd>A more detailed description of the set of option buttons
  136. * that will appear at the bottom of the dialog box.
  137. * The usual value for the options parameter is an array of
  138. * <code>String</code>s. But
  139. * the parameter type is an array of <code>Objects</code>.
  140. * A button is created for each object depending on its type:
  141. * <dl compact>
  142. * <dt>Component<dd>The component is added to the button row directly.
  143. * <dt>Icon<dd>A <code>JButton</code> is created with this as its label.
  144. * <dt>other<dd>The <code>Object</code> is converted to a string using its
  145. * <code>toString</code> method and the result is used to
  146. * label a <code>JButton</code>.
  147. * </dl>
  148. * <dt>icon<dd>A decorative icon to be placed in the dialog box. A default
  149. * value for this is determined by the <code>messageType</code> parameter.
  150. * <dt>title<dd>The title for the dialog box.
  151. * <dt>initialValue<dd>The default selection (input value).
  152. * </dl>
  153. * </blockquote>
  154. * <p>
  155. * When the selection is changed, <code>setValue</code> is invoked,
  156. * which generates a <code>PropertyChangeEvent</code>.
  157. * <p>
  158. * If a <code>JOptionPane</code> has configured to all input
  159. * <code>setWantsInput</code>
  160. * the bound property <code>JOptionPane.INPUT_VALUE_PROPERTY</code>
  161. * can also be listened
  162. * to, to determine when the user has input or selected a value.
  163. * <p>
  164. * When one of the <code>showXxxDialog</code> methods returns an integer,
  165. * the possible values are:<pre>
  166. * YES_OPTION,
  167. * NO_OPTION,
  168. * CANCEL_OPTION,
  169. * OK_OPTION, or
  170. * CLOSED_OPTION.
  171. * </pre>
  172. * <b>Examples:</b>
  173. * <dl>
  174. * <dt>Show an error dialog that displays the message, 'alert':
  175. * <dd><code>
  176. * JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
  177. * </code><p>
  178. * <dt>Show an internal information dialog with the message, 'information':
  179. * <dd><code>
  180. * JOptionPane.showInternalMessageDialog(frame, "information",<br>
  181. * <ul><ul>"information", JOptionPane.INFORMATION_MESSAGE);</ul></ul>
  182. * </code><p>
  183. * <dt>Show an information panel with the options yes/no and message 'choose one':
  184. * <dd><code>JOptionPane.showConfirmDialog(null,
  185. * <ul><ul>"choose one", "choose one", JOptionPane.YES_NO_OPTION);</ul></ul>
  186. * </code><p>
  187. * <dt>Show an internal information dialog with the options yes/no/cancel and
  188. * message 'please choose one' and title information:
  189. * <dd><code>JOptionPane.showInternalConfirmDialog(frame,
  190. * <ul><ul>"please choose one", "information",</ul></ul>
  191. * <ul><ul>JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);</ul></ul>
  192. * </code><p>
  193. * <dt>Show a warning dialog with the options OK, CANCEL, title 'Warning', and
  194. * message 'Click OK to continue':
  195. * <dd><code>
  196. * Object[] options = { "OK", "CANCEL" };<br>
  197. * JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
  198. * <ul><ul>JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,</ul></ul>
  199. * <ul><ul>null, options, options[0]);</ul></ul>
  200. * </code><p>
  201. * <dt>Show a dialog asking the user to type in a String:
  202. * <dd><code>
  203. * String inputValue = JOptionPane.showInputDialog("Please input a value");
  204. * </code><p>
  205. * <dt>Show a dialog asking the user to select a String:
  206. * <dd><code>
  207. * Object[] possibleValues = { "First", "Second", "Third" };<br>
  208. * Object selectedValue = JOptionPane.showInputDialog(null,
  209. * <ul><ul>"Choose one", "Input",</ul></ul>
  210. * <ul><ul>JOptionPane.INFORMATION_MESSAGE, null,</ul></ul>
  211. * <ul><ul>possibleValues, possibleValues[0]);</ul></ul>
  212. * </code><p>
  213. * </dl>
  214. * <b>Direct Use:</b><br>
  215. * To create and use an <code>JOptionPane</code> directly, the
  216. * standard pattern is roughly as follows:
  217. * <pre>
  218. * JOptionPane pane = new JOptionPane(<i>arguments</i>);
  219. * pane.set<i>.Xxxx(...); // Configure</i>
  220. * JDialog dialog = pane.createDialog(<i>parentComponent, title</i>);
  221. * dialog.show();
  222. * Object selectedValue = pane.getValue();
  223. * if(selectedValue == null)
  224. * return CLOSED_OPTION;
  225. * <i>//If there is <b>not</b> an array of option buttons:</i>
  226. * if(options == null) {
  227. * if(selectedValue instanceof Integer)
  228. * return ((Integer)selectedValue).intValue();
  229. * return CLOSED_OPTION;
  230. * }
  231. * <i>//If there is an array of option buttons:</i>
  232. * for(int counter = 0, maxCounter = options.length;
  233. * counter < maxCounter; counter++) {
  234. * if(options[counter].equals(selectedValue))
  235. * return counter;
  236. * }
  237. * return CLOSED_OPTION;
  238. * </pre>
  239. * <p>
  240. * For the keyboard keys used by this component in the standard Look and
  241. * Feel (L&F) renditions, see the
  242. * <a href="doc-files/Key-Index.html#JOptionPane">JOptionPane</a> key assignments.
  243. * <p>
  244. * <strong>Warning:</strong>
  245. * Serialized objects of this class will not be compatible with
  246. * future Swing releases. The current serialization support is appropriate
  247. * for short term storage or RMI between applications running the same
  248. * version of Swing. A future release of Swing will provide support for
  249. * long term persistence.
  250. *
  251. * @see JInternalFrame
  252. *
  253. * @beaninfo
  254. * attribute: isContainer true
  255. * description: A component which implements standard dialog box controls.
  256. *
  257. * @version 1.61 02/09/01
  258. * @author James Gosling
  259. * @author Scott Violet
  260. */
  261. public class JOptionPane extends JComponent implements Accessible
  262. {
  263. /**
  264. * @see #getUIClassID
  265. * @see #readObject
  266. */
  267. private static final String uiClassID = "OptionPaneUI";
  268. /**
  269. * Indicates that the user has not yet selected a value.
  270. */
  271. public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
  272. //
  273. // Option types
  274. //
  275. /**
  276. * Type meaning Look and Feel should not supply any options -- only
  277. * use the options from the JOptionPane.
  278. */
  279. public static final int DEFAULT_OPTION = -1;
  280. /** Type used for showConfirmDialog. */
  281. public static final int YES_NO_OPTION = 0;
  282. /** Type used for showConfirmDialog. */
  283. public static final int YES_NO_CANCEL_OPTION = 1;
  284. /** Type used for showConfirmDialog. */
  285. public static final int OK_CANCEL_OPTION = 2;
  286. //
  287. // Return values.
  288. //
  289. /** Return value from class method if YES is chosen. */
  290. public static final int YES_OPTION = 0;
  291. /** Return value from class method if NO is chosen. */
  292. public static final int NO_OPTION = 1;
  293. /** Return value from class method if CANCEL is chosen. */
  294. public static final int CANCEL_OPTION = 2;
  295. /** Return value form class method if OK is chosen. */
  296. public static final int OK_OPTION = 0;
  297. /** Return value from class method if user closes window without selecting
  298. * anything, more than likely this should be treated as either a
  299. * CANCEL_OPTION or NO_OPTION. */
  300. public static final int CLOSED_OPTION = -1;
  301. //
  302. // Message types. Used by the UI to determine what icon to display,
  303. // and possibly what behavior to give based on the type.
  304. //
  305. /** Used for error messages. */
  306. public static final int ERROR_MESSAGE = 0;
  307. /** Used for information messages. */
  308. public static final int INFORMATION_MESSAGE = 1;
  309. /** Used for warning messages. */
  310. public static final int WARNING_MESSAGE = 2;
  311. /** Used for questions. */
  312. public static final int QUESTION_MESSAGE = 3;
  313. /** No icon is used. */
  314. public static final int PLAIN_MESSAGE = -1;
  315. /** Bound property name for icon. */
  316. public static final String ICON_PROPERTY = "icon";
  317. /** Bound property name for message. */
  318. public static final String MESSAGE_PROPERTY = "message";
  319. /** Bounds property name for value. */
  320. public static final String VALUE_PROPERTY = "value";
  321. /** Bounds property namer for option. */
  322. public static final String OPTIONS_PROPERTY = "options";
  323. /** Bounds property name for initialValue. */
  324. public static final String INITIAL_VALUE_PROPERTY = "initialValue";
  325. /** Bounds property name for type. */
  326. public static final String MESSAGE_TYPE_PROPERTY = "messageType";
  327. /** Bound property name for optionType. */
  328. public static final String OPTION_TYPE_PROPERTY = "optionType";
  329. /** Bound property name for selectionValues. */
  330. public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
  331. /** Bound property name for initialSelectionValue. */
  332. public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
  333. /** Bound property name for inputValue. */
  334. public static final String INPUT_VALUE_PROPERTY = "inputValue";
  335. /** Bound property name for wantsInput. */
  336. public static final String WANTS_INPUT_PROPERTY = "wantsInput";
  337. /** Icon used in pane. */
  338. transient protected Icon icon;
  339. /** Message to display. */
  340. transient protected Object message;
  341. /** Options to display to the user. */
  342. transient protected Object[] options;
  343. /** Value that should be initialy selected in options. */
  344. transient protected Object initialValue;
  345. /** Message type. */
  346. protected int messageType;
  347. /** Option type, one of DEFAULT_OPTION, YES_NO_OPTION,
  348. * YES_NO_CANCEL_OPTION or OK_CANCEL_OPTION. */
  349. protected int optionType;
  350. /** Currently selected value, will be a valid option, or
  351. * UNINITIALIZED_VALUE or <code>null</code>. */
  352. transient protected Object value;
  353. /** Array of values the user can choose from. Look and feel will
  354. * provide the UI component to choose this from. */
  355. protected transient Object[] selectionValues;
  356. /** Value the user has input. */
  357. protected transient Object inputValue;
  358. /** Initial value to select in selectionValues. */
  359. protected transient Object initialSelectionValue;
  360. /** If true, a UI widget will be provided to the user to get input. */
  361. protected boolean wantsInput;
  362. /**
  363. * Shows a question-message dialog requesting input from the user. The
  364. * dialog uses the default frame, which usually means it is centered on
  365. * the screen.
  366. *
  367. * @param message the <code>Object</code> to display
  368. */
  369. public static String showInputDialog(Object message) {
  370. return showInputDialog(null, message);
  371. }
  372. /**
  373. * Shows a question-message dialog requesting input from the user
  374. * parented to <code>parentComponent</code>.
  375. * The dialog is displayed in the Component's
  376. * frame, and is usually positioned below the <code>Component</code>.
  377. *
  378. * @param parentComponent the parent <code>Component</code> for the
  379. * dialog
  380. * @param message the <code>Object</code> to display
  381. */
  382. public static String showInputDialog(Component parentComponent, Object message){
  383. return showInputDialog(parentComponent, message, "Input", QUESTION_MESSAGE);
  384. }
  385. /**
  386. * Shows a dialog requesting input from the user parented to
  387. * <code>parentComponent</code> with the dialog having the title
  388. * <code>title</code> and message type <code>messageType</code>.
  389. *
  390. * @param parentComponent the parent <code>Component</code> for the
  391. * dialog
  392. * @param message the <code>Object</code> to display
  393. * @param title the <code>String</code> to display in the dialog
  394. * title bar
  395. * @param messageType the type of message that is to be displayed:
  396. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  397. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  398. */
  399. public static String showInputDialog(Component parentComponent, Object message,
  400. String title, int messageType) {
  401. return (String)showInputDialog(parentComponent, message, title,
  402. messageType, null, null, null);
  403. }
  404. /**
  405. * Prompts the user for input in a blocking dialog where the
  406. * initial selection, possible selections, and all other options can
  407. * be specified. The user will able to choose from
  408. * <code>selectionValues</code>, where <code>null</code> implies the
  409. * user can input
  410. * whatever they wish, usually by means of a <code>JTextField</code>.
  411. * <code>initialSelectionValue</code> is the initial value to prompt
  412. * the user with. It is up to the UI to decide how best to represent
  413. * the <code>selectionValues</code>, but usually a
  414. * <code>JComboBox</code>, <code>JList</code>, or
  415. * <code>JTextField</code> will be used.
  416. *
  417. * @param parentComponent the parent <code>Component</code> for the
  418. * dialog
  419. * @param message the <code>Object</code> to display
  420. * @param title the <code>String</code> to display in the
  421. * dialog title bar
  422. * @param messageType the type of message to be displayed:
  423. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  424. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  425. * @param icon the Icon image to display
  426. * @param selectionValues an array of <code>Object</code>s that
  427. * gives the possible selections
  428. * @param initialSelectionValue the value used to initialize the input
  429. * field
  430. * @return users input, or <code>null</code> meaning the user
  431. * cancelled the input
  432. */
  433. public static Object showInputDialog(Component parentComponent, Object message,
  434. String title, int messageType, Icon icon,
  435. Object[] selectionValues, Object initialSelectionValue) {
  436. JOptionPane pane = new JOptionPane(message, messageType,
  437. OK_CANCEL_OPTION, icon,
  438. null, null);
  439. pane.setWantsInput(true);
  440. pane.setSelectionValues(selectionValues);
  441. pane.setInitialSelectionValue(initialSelectionValue);
  442. JDialog dialog = pane.createDialog(parentComponent, title);
  443. pane.selectInitialValue();
  444. dialog.show();
  445. Object value = pane.getInputValue();
  446. if(value == UNINITIALIZED_VALUE)
  447. return null;
  448. return value;
  449. }
  450. /**
  451. * Brings up a modal information-message dialog titled "Message".
  452. *
  453. * @param parentComponent determines the <code>Frame</code> in
  454. * which the dialog is displayed; if <code>null</code>,
  455. * or if the <code>parentComponent</code> has no
  456. * <code>Frame</code>, a default <code>Frame</code> is used
  457. * @param message the <code>Object</code> to display
  458. */
  459. public static void showMessageDialog(Component parentComponent, Object message) {
  460. showMessageDialog(parentComponent, message, "Message", INFORMATION_MESSAGE);
  461. }
  462. /**
  463. * Brings up a dialog that displays a message using a default
  464. * icon determined by the <code>messageType</code> parameter.
  465. *
  466. * @param parentComponent determines the <code>Frame</code>
  467. * in which the dialog is displayed; if <code>null</code>,
  468. * or if the <code>parentComponent</code> has no
  469. * <code>Frame</code>, a default <code>Frame</code> is used
  470. * @param message the <code>Object</code> to display
  471. * @param title the title string for the dialog
  472. * @param messageType the type of message to be displayed:
  473. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  474. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  475. */
  476. public static void showMessageDialog(Component parentComponent, Object message,
  477. String title, int messageType) {
  478. showMessageDialog(parentComponent, message, title, messageType, null);
  479. }
  480. /**
  481. * Brings up a dialog displaying a message, specifying all parameters.
  482. *
  483. * @param parentComponent determines the <code>Frame</code> in which the
  484. * dialog is displayed; if <code>null</code>,
  485. * or if the <code>parentComponent</code> has no
  486. * <code>Frame</code>, a
  487. * default <code>Frame</code> is used
  488. * @param message the <code>Object</code> to display
  489. * @param title the title string for the dialog
  490. * @param messageType the type of message to be displayed:
  491. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  492. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  493. * @param icon an icon to display in the dialog that helps the user
  494. * identify the kind of message that is being displayed
  495. */
  496. public static void showMessageDialog(Component parentComponent, Object message,
  497. String title, int messageType,
  498. Icon icon){
  499. showOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
  500. messageType, icon, null, null);
  501. }
  502. /**
  503. * Brings up a modal dialog with the options Yes, No and Cancel; with the
  504. * title, "Select an Option".
  505. *
  506. * @param parentComponent determines the <code>Frame</code> in which the
  507. * dialog is displayed; if <code>null</code>,
  508. * or if the <code>parentComponent</code> has no
  509. * <code>Frame</code>, a
  510. * default <code>Frame</code> is used
  511. * @param message the <code>Object</code> to display
  512. * @return an integer indicating the option selected by the user
  513. */
  514. public static int showConfirmDialog(Component parentComponent, Object message) {
  515. return showConfirmDialog(parentComponent, message,
  516. UIManager.getString("OptionPane.titleText"), YES_NO_CANCEL_OPTION);
  517. }
  518. /**
  519. * Brings up a modal dialog where the number of choices is determined
  520. * by the <code>optionType</code> parameter.
  521. *
  522. * @param parentComponent determines the <code>Frame</code> in which the
  523. * dialog is displayed; if <code>null</code>,
  524. * or if the <code>parentComponent</code> has no
  525. * <code>Frame</code>, a
  526. * default <code>Frame</code> is used
  527. * @param message the <code>Object</code> to display
  528. * @param title the title string for the dialog
  529. * @param optionType an int designating the options available on the dialog:
  530. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  531. * @return an int indicating the option selected by the user
  532. */
  533. public static int showConfirmDialog(Component parentComponent, Object message,
  534. String title, int optionType) {
  535. return showConfirmDialog(parentComponent, message, title, optionType,
  536. QUESTION_MESSAGE);
  537. }
  538. /**
  539. * Brings up a modal dialog where the number of choices is determined
  540. * by the <code>optionType</code> parameter, where the
  541. * <code>messageType</code>
  542. * parameter determines the icon to display.
  543. * The <code>messageType</code> parameter is primarily used to supply
  544. * a default icon from the Look and Feel.
  545. *
  546. * @param parentComponent determines the <code>Frame</code> in
  547. * which the dialog is displayed; if <code>null</code>,
  548. * or if the <code>parentComponent</code> has no
  549. * <code>Frame</code>, a
  550. * default <code>Frame</code> is used.
  551. * @param message the <code>Object</code> to display
  552. * @param title the title string for the dialog
  553. * @param optionType an integer designating the options available
  554. * on the dialog:
  555. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  556. * @param messageType an integer designating the kind of message this is,
  557. * primarily used to determine the icon from the
  558. * pluggable
  559. * Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  560. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
  561. * @return an integer indicating the option selected by the user
  562. */
  563. public static int showConfirmDialog(Component parentComponent, Object message,
  564. String title, int optionType,
  565. int messageType) {
  566. return showConfirmDialog(parentComponent, message, title, optionType,
  567. messageType, null);
  568. }
  569. /**
  570. * Brings up a modal dialog with a specified icon, where the number of
  571. * choices is determined by the <code>optionType</code> parameter.
  572. * The <code>messageType</code> parameter is primarily used to supply
  573. * a default icon from the Look and Feel.
  574. *
  575. * @param parentComponent determines the <code>Frame</code> in which the
  576. * dialog is displayed; if <code>null</code>,
  577. * or if the <code>parentComponent</code> has no
  578. * <code>Frame</code>, a
  579. * default <code>Frame</code> is used
  580. * @param message The Object to display
  581. * @param title the title string for the dialog
  582. * @param optionType an int designating the options available on the dialog:
  583. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  584. * @param messageType an int designating the kind of message this is,
  585. * primarily used to determine the icon from the pluggable
  586. * Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  587. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
  588. * @param icon the icon to display in the dialog
  589. * @return an int indicating the option selected by the user
  590. */
  591. public static int showConfirmDialog(Component parentComponent, Object message,
  592. String title, int optionType,
  593. int messageType, Icon icon) {
  594. return showOptionDialog(parentComponent, message, title, optionType,
  595. messageType, icon, null, null);
  596. }
  597. /**
  598. * Brings up a modal dialog with a specified icon, where the initial
  599. * choice is dermined by the <code>initialValue</code> parameter and
  600. * the number of choices is determined by the <code>optionType</code>
  601. * parameter.
  602. * <p>
  603. * If <code>optionType</code> is YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  604. * and the <code>options</code> parameter is <code>null</code>,
  605. * then the options are
  606. * supplied by the Look and Feel.
  607. * <p>
  608. * The <code>messageType</code> parameter is primarily used to supply
  609. * a default icon from the Look and Feel.
  610. *
  611. * @param parentComponent determines the <code>Frame</code>
  612. * in which the dialog is displayed; if
  613. * <code>null</code>, or if the
  614. * <code>parentComponent</code> has no
  615. * <code>Frame</code>, a
  616. * default <code>Frame</code> is used
  617. * @param message the <code>Object</code> to display
  618. * @param title the title string for the dialog
  619. * @param optionType an integer designating the options available on the
  620. * dialog: YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  621. * @param messageType an integer designating the kind of message this is,
  622. * primarily used to determine the icon from the
  623. * pluggable
  624. * Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  625. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
  626. * @param icon the icon to display in the dialog
  627. * @param options an array of objects indicating the possible choices
  628. * the user can make; if the objects are components, they
  629. * are rendered properly; non-<code>String</code>
  630. * objects are
  631. * rendered using their <code>toString</code> methods;
  632. * if this parameter is <code>null</code>,
  633. * the options are determined by the Look and Feel.
  634. * @param initialValue the object that represents the default selection
  635. * for the dialog
  636. * @return an integer indicating the option chosen by the user,
  637. * or CLOSED_OPTION if the user closed the Dialog
  638. */
  639. public static int showOptionDialog(Component parentComponent, Object message,
  640. String title, int optionType,
  641. int messageType, Icon icon,
  642. Object[] options, Object initialValue) {
  643. JOptionPane pane = new JOptionPane(message, messageType,
  644. optionType, icon,
  645. options, initialValue);
  646. pane.setInitialValue(initialValue);
  647. JDialog dialog = pane.createDialog(parentComponent, title);
  648. pane.selectInitialValue();
  649. dialog.show();
  650. Object selectedValue = pane.getValue();
  651. if(selectedValue == null)
  652. return CLOSED_OPTION;
  653. if(options == null) {
  654. if(selectedValue instanceof Integer)
  655. return ((Integer)selectedValue).intValue();
  656. return CLOSED_OPTION;
  657. }
  658. for(int counter = 0, maxCounter = options.length;
  659. counter < maxCounter; counter++) {
  660. if(options[counter].equals(selectedValue))
  661. return counter;
  662. }
  663. return CLOSED_OPTION;
  664. }
  665. /**
  666. * Creates and returns a new <code>JDialog</code> wrapping
  667. * <code>this</code> centered on the <code>parentComponent</code>
  668. * in the <code>parentComponent</code>'s frame.
  669. * <code>title</code> is the title of the returned dialog.
  670. * The returned <code>JDialog</code> will be set up such that
  671. * once it is closed, or the user clicks on the OK button,
  672. * the dialog will be disposed and closed.
  673. *
  674. * @param parentComponent determines the frame in which the dialog
  675. * is displayed; if the <code>parentComponent</code> has
  676. * no <code>Frame</code>, a default <code>Frame</code> is used
  677. * @param title the title string for the dialog
  678. * @return a new <code>JDialog</code> containing this instance
  679. */
  680. public JDialog createDialog(Component parentComponent, String title) {
  681. final JDialog dialog;
  682. Window window = JOptionPane.getWindowForComponent(parentComponent);
  683. if (window instanceof Frame) {
  684. dialog = new JDialog((Frame)window, title, true);
  685. } else {
  686. dialog = new JDialog((Dialog)window, title, true);
  687. }
  688. Container contentPane = dialog.getContentPane();
  689. contentPane.setLayout(new BorderLayout());
  690. contentPane.add(this, BorderLayout.CENTER);
  691. dialog.pack();
  692. dialog.setLocationRelativeTo(parentComponent);
  693. dialog.addWindowListener(new WindowAdapter() {
  694. boolean gotFocus = false;
  695. public void windowClosing(WindowEvent we) {
  696. setValue(null);
  697. }
  698. public void windowActivated(WindowEvent we) {
  699. // Once window gets focus, set initial focus
  700. if (!gotFocus) {
  701. selectInitialValue();
  702. gotFocus = true;
  703. }
  704. }
  705. });
  706. addPropertyChangeListener(new PropertyChangeListener() {
  707. public void propertyChange(PropertyChangeEvent event) {
  708. if(dialog.isVisible() && event.getSource() == JOptionPane.this &&
  709. (event.getPropertyName().equals(VALUE_PROPERTY) ||
  710. event.getPropertyName().equals(INPUT_VALUE_PROPERTY))) {
  711. dialog.setVisible(false);
  712. dialog.dispose();
  713. }
  714. }
  715. });
  716. return dialog;
  717. }
  718. /**
  719. * Brings up an internal confirmation dialog panel. The dialog
  720. * is a modal information-message dialog titled "Message".
  721. *
  722. * @param parentComponent determines the <code>Frame</code>
  723. * in which the dialog is displayed; if <code>null</code>,
  724. * or if the <code>parentComponent</code> has no
  725. * <code>Frame</code>, a default <code>Frame</code> is used
  726. * @param message the object to display
  727. */
  728. public static void showInternalMessageDialog(Component parentComponent,
  729. Object message) {
  730. showInternalMessageDialog(parentComponent, message, "Message",
  731. INFORMATION_MESSAGE);
  732. }
  733. /**
  734. * Brings up an internal dialog panel that displays a message
  735. * using a default icon determined by the <code>messageType</code>
  736. * parameter.
  737. *
  738. * @param parentComponent determines the <code>Frame</code>
  739. * in which the dialog is displayed; if <code>null</code>,
  740. * or if the <code>parentComponent</code> has no
  741. * <code>Frame</code>, a default <code>Frame</code> is used
  742. * @param message the <code>Object</code> to display
  743. * @param title the title string for the dialog
  744. * @param messageType the type of message to be displayed:
  745. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  746. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  747. */
  748. public static void showInternalMessageDialog(Component parentComponent,
  749. Object message, String title,
  750. int messageType) {
  751. showInternalMessageDialog(parentComponent, message, title, messageType,null);
  752. }
  753. /**
  754. * Brings up an internal dialog panel displaying a message,
  755. * specifying all parameters.
  756. *
  757. * @param parentComponent determines the <code>Frame</code>
  758. * in which the dialog is displayed; if <code>null</code>,
  759. * or if the <code>parentComponent</code> has no
  760. * <code>Frame</code>, a default <code>Frame</code> is used
  761. * @param message the <code>Object</code> to display
  762. * @param title the title string for the dialog
  763. * @param messageType the type of message to be displayed:
  764. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  765. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  766. * @param icon an icon to display in the dialog that helps the user
  767. * identify the kind of message that is being displayed
  768. */
  769. public static void showInternalMessageDialog(Component parentComponent,
  770. Object message,
  771. String title, int messageType,
  772. Icon icon){
  773. showInternalOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
  774. messageType, icon, null, null);
  775. }
  776. /**
  777. * Brings up an internal dialog panel with the options Yes, No
  778. * and Cancel; with the title, "Select an Option".
  779. *
  780. * @param parentComponent determines the <code>Frame</code> in
  781. * which the dialog is displayed; if <code>null</code>,
  782. * or if the <code>parentComponent</code> has no
  783. * <code>Frame</code>, a default <code>Frame</code> is used
  784. * @param message the <code>Object</code> to display
  785. * @return an integer indicating the option selected by the user
  786. */
  787. public static int showInternalConfirmDialog(Component parentComponent,
  788. Object message) {
  789. return showInternalConfirmDialog(parentComponent, message,
  790. UIManager.getString("OptionPane.titleText"), YES_NO_CANCEL_OPTION);
  791. }
  792. /**
  793. * Brings up a internal dialog panel where the number of choices
  794. * is determined by the <code>optionType</code> parameter.
  795. *
  796. * @param parentComponent determines the <code>Frame</code>
  797. * in which the dialog is displayed; if <code>null</code>,
  798. * or if the <code>parentComponent</code> has no
  799. * <code>Frame</code>, a default <code>Frame</code> is used
  800. * @param message the object to display in the dialog; a
  801. * <code>Component</code> object is rendered as a
  802. * <code>Component</code> a <code>String</code>
  803. * object is rendered as a string; other objects
  804. * are converted to a <code>String</code> using the
  805. * <code>toString</code> method
  806. * @param title the title string for the dialog
  807. * @param optionType an integer designating the options
  808. * available on the dialog: YES_NO_OPTION,
  809. * or YES_NO_CANCEL_OPTION
  810. * @return an integer indicating the option selected by the user
  811. */
  812. public static int showInternalConfirmDialog(Component parentComponent,
  813. Object message, String title,
  814. int optionType) {
  815. return showInternalConfirmDialog(parentComponent, message, title, optionType,
  816. QUESTION_MESSAGE);
  817. }
  818. /**
  819. * Brings up an internal dialog panel where the number of choices
  820. * is determined by the <code>optionType</code> parameter, where
  821. * the <code>messageType</code> parameter determines the icon to display.
  822. * The <code>messageType</code> parameter is primarily used to supply
  823. * a default icon from the Look and Feel.
  824. *
  825. * @param parentComponent determines the <code>Frame</code> in
  826. * which the dialog is displayed; if <code>null</code>,
  827. * or if the <code>parentComponent</code> has no
  828. * <code>Frame</code>, a default <code>Frame</code> is used
  829. * @param message the object to display in the dialog; a
  830. * <code>Component</code> object is rendered as a
  831. * <code>Component</code> a <code>String</code>
  832. * object is rendered as a string; other objects are
  833. * converted to a <code>String</code> using the
  834. * <code>toString</code> method
  835. * @param title the title string for the dialog
  836. * @param optionType an integer designating the options
  837. * available on the dialog:
  838. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  839. * @param messageType an integer designating the kind of message this is,
  840. * primarily used to determine the icon from the
  841. * pluggable Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  842. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
  843. * @return an integer indicating the option selected by the user
  844. */
  845. public static int showInternalConfirmDialog(Component parentComponent,
  846. Object message,
  847. String title, int optionType,
  848. int messageType) {
  849. return showInternalConfirmDialog(parentComponent, message, title, optionType,
  850. messageType, null);
  851. }
  852. /**
  853. * Brings up an internal dialog panel with a specified icon, where
  854. * the number of choices is determined by the <code>optionType</code>
  855. * parameter.
  856. * The <code>messageType</code> parameter is primarily used to supply
  857. * a default icon from the Look and Feel.
  858. *
  859. * @param parentComponent determines the <code>Frame</code>
  860. * in which the dialog is displayed; if <code>null</code>,
  861. * or if the parentComponent has no Frame, a
  862. * default <code>Frame</code> is used
  863. * @param message the object to display in the dialog; a
  864. * <code>Component</code> object is rendered as a
  865. * <code>Component</code> a <code>String</code>
  866. * object is rendered as a string; other objects are
  867. * converted to a <code>String</code> using the
  868. * <code>toString</code> method
  869. * @param title the title string for the dialog
  870. * @param optionType an integer designating the options available
  871. * on the dialog:
  872. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  873. * @param messageType an integer designating the kind of message this is,
  874. * primarily used to determine the icon from the pluggable
  875. * Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  876. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
  877. * @param icon the icon to display in the dialog
  878. * @return an integer indicating the option selected by the user
  879. */
  880. public static int showInternalConfirmDialog(Component parentComponent,
  881. Object message,
  882. String title, int optionType,
  883. int messageType, Icon icon) {
  884. return showInternalOptionDialog(parentComponent, message, title, optionType,
  885. messageType, icon, null, null);
  886. }
  887. /**
  888. * Brings up an internal dialog panel with a specified icon, where
  889. * the initial choice is dermined by the <code>initialValue</code>
  890. * parameter and the number of choices is determined by the
  891. * <code>optionType</code> parameter.
  892. * <p>
  893. * If <code>optionType</code> is YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  894. * and the <code>options</code> parameter is <code>null</code>,
  895. * then the options are supplied by the Look and Feel.
  896. * <p>
  897. * The <code>messageType</code> parameter is primarily used to supply
  898. * a default icon from the Look and Feel.
  899. *
  900. * @param parentComponent determines the <code>Frame</code>
  901. * in which the dialog is displayed; if <code>null</code>,
  902. * or if the <code>parentComponent</code> has no
  903. * <code>Frame</code>, a default <code>Frame</code> is used
  904. * @param message the object to display in the dialog; a
  905. * <code>Component</code> object is rendered as a
  906. * <code>Component</code> a <code>String</code>
  907. * object is rendered as a string. Other objects are
  908. * converted to a <code>String</code> using the
  909. * <code>toString</code> method
  910. * @param title the title string for the dialog
  911. * @param optionType an integer designating the options available
  912. * on the dialog:
  913. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  914. * @param messageType an integer designating the kind of message this is,
  915. * primarily used to determine the icon from the
  916. * pluggable Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  917. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
  918. * @param icon the icon to display in the dialog
  919. * @param options an array of objects indicating the possible choices
  920. * the user can make; if the objects are components, they
  921. * are rendered properly; non-<code>String</code>
  922. * objects are rendered using their <code>toString</code>
  923. * methods; if this parameter is <code>null</code>,
  924. * the options are determined by the Look and Feel
  925. * @param initialValue the object that represents the default selection
  926. * for the dialog
  927. * @return an integer indicating the option chosen by the user,
  928. * or CLOSED_OPTION if the user closed the Dialog
  929. */
  930. public static int showInternalOptionDialog(Component parentComponent,
  931. Object message,
  932. String title, int optionType,
  933. int messageType, Icon icon,
  934. Object[] options, Object initialValue) {
  935. JOptionPane pane = new JOptionPane(message, messageType,
  936. optionType, icon,
  937. options, initialValue);
  938. pane.setInitialValue(initialValue);
  939. JInternalFrame dialog = pane.createInternalFrame(parentComponent, title);
  940. pane.selectInitialValue();
  941. dialog.setVisible(true);
  942. dialog.startModal();
  943. Object selectedValue = pane.getValue();
  944. if(selectedValue == null)
  945. return CLOSED_OPTION;
  946. if(options == null) {
  947. if(selectedValue instanceof Integer)
  948. return ((Integer)selectedValue).intValue();
  949. return CLOSED_OPTION;
  950. }
  951. for(int counter = 0, maxCounter = options.length;
  952. counter < maxCounter; counter++) {
  953. if(options[counter].equals(selectedValue))
  954. return counter;
  955. }
  956. return CLOSED_OPTION;
  957. }
  958. /**
  959. * Shows an internal question-message dialog requesting input from
  960. * the user parented to <code>parentComponent</code>. The dialog
  961. * is displayed in the <code>Component</code>'s frame,
  962. * and is usually positioned below the <code>Component</code>.
  963. *
  964. * @param parentComponent the parent <code>Component</code>
  965. * for the dialog
  966. * @param message the <code>Object</code> to display
  967. */
  968. public static String showInternalInputDialog(Component parentComponent,
  969. Object message) {
  970. return showInternalInputDialog(parentComponent, message, "Input",
  971. QUESTION_MESSAGE);
  972. }
  973. /**
  974. * Shows an internal dialog requesting input from the user parented
  975. * to <code>parentComponent</code> with the dialog having the title
  976. * <code>title</code> and message type <code>messageType</code>.
  977. *
  978. * @param parentComponent the parent <code>Component</code> for the dialog
  979. * @param message the <code>Object</code> to display
  980. * @param title the <code>String</code> to display in the
  981. * dialog title bar
  982. * @param messageType the type of message that is to be displayed:
  983. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  984. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  985. */
  986. public static String showInternalInputDialog(Component parentComponent,
  987. Object message, String title, int messageType) {
  988. return (String)showInternalInputDialog(parentComponent, message, title,
  989. messageType, null, null, null);
  990. }
  991. /**
  992. * Prompts the user for input in a blocking internal dialog where
  993. * the initial selection, possible selections, and all other
  994. * options can be specified. The user will able to choose from
  995. * <code>selectionValues</code>, where <code>null</code>
  996. * implies the user can input
  997. * whatever they wish, usually by means of a <code>JTextField</code>.
  998. * <code>initialSelectionValue</code> is the initial value to prompt
  999. * the user with. It is up to the UI to decide how best to represent
  1000. * the <code>selectionValues</code>, but usually a
  1001. * <code>JComboBox</code>, <code>JList</code>, or
  1002. * <code>JTextField</code> will be used.
  1003. *
  1004. * @param parentComponent the parent <code>Component</code> for the dialog
  1005. * @param message the <code>Object</code> to display
  1006. * @param title the <code>String</code> to display in the dialog
  1007. * title bar
  1008. * @param messageType the type of message to be displayed:
  1009. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1010. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  1011. * @param icon the <code>Icon</code> image to display
  1012. * @param selectionValues an array of <code>Objects</code> that
  1013. * gives the possible selections
  1014. * @param initialSelectionValue the value used to initialize the input
  1015. * field
  1016. * @return users input, or <code>null</code> meaning the user
  1017. * cancelled the input
  1018. */
  1019. public static Object showInternalInputDialog(Component parentComponent,
  1020. Object message, String title, int messageType, Icon icon,
  1021. Object[] selectionValues, Object initialSelectionValue) {
  1022. JOptionPane pane = new JOptionPane(message, messageType,
  1023. OK_CANCEL_OPTION, icon,
  1024. null, null);
  1025. pane.setWantsInput(true);
  1026. pane.setSelectionValues(selectionValues);
  1027. pane.setInitialSelectionValue(initialSelectionValue);
  1028. JInternalFrame dialog = pane.createInternalFrame(parentComponent, title);
  1029. pane.selectInitialValue();
  1030. dialog.setVisible(true);
  1031. dialog.startModal();
  1032. Object value = pane.getInputValue();
  1033. if(value == UNINITIALIZED_VALUE)
  1034. return null;
  1035. return (String)value;
  1036. }
  1037. /**
  1038. * Creates and returns an instance of <code>JInternalFrame</code>.
  1039. * The internal frame is created with the specified title,
  1040. * and wrapping the <code>JOptionPane</code>.
  1041. * The returned <code>JInternalFrame</code> is
  1042. * added to the <code>JDesktopPane</code> ancestor of
  1043. * <code>parentComponent</code>, or components
  1044. * parent if one its ancestors isn't a <code>JDesktopPane</code>,
  1045. * or if <code>parentComponent</code>
  1046. * doesn't have a parent then a <code>RuntimeException</code> is thrown.
  1047. *
  1048. * @param parentComponent the parent <code>Component</code> for
  1049. * the internal frame
  1050. * @param title the <code>String</code> to display in the
  1051. * frame's title bar
  1052. * @return a <code>JInternalFrame</code> containing a
  1053. * <code>JOptionPane</code>
  1054. * @exception RuntimeException if <code>parentComponent</code> does
  1055. * not have a valid parent
  1056. */
  1057. public JInternalFrame createInternalFrame(Component parentComponent,
  1058. String title) {
  1059. Container parent = JOptionPane.
  1060. getDesktopPaneForComponent(parentComponent);
  1061. if(parent == null && (parentComponent == null ||
  1062. (parent = parentComponent.getParent()) == null))
  1063. throw new RuntimeException("JOptionPane: parentComponent does not have a valid parent");
  1064. final JInternalFrame iFrame = new JInternalFrame(title, false, false,
  1065. false, false);
  1066. iFrame.putClientProperty( "JInternalFrame.frameType", "optionDialog" ); //jcs
  1067. addPropertyChangeListener(new PropertyChangeListener() {
  1068. public void propertyChange(PropertyChangeEvent event) {
  1069. if(iFrame.isVisible() && event.getSource() == JOptionPane.this &&
  1070. (event.getPropertyName().equals(VALUE_PROPERTY) ||
  1071. event.getPropertyName().equals(INPUT_VALUE_PROPERTY))) {
  1072. try {
  1073. iFrame.setClosed(true);
  1074. } catch (java.beans.PropertyVetoException e) {}
  1075. iFrame.setVisible(false);
  1076. iFrame.stopModal();
  1077. }
  1078. }
  1079. });
  1080. iFrame.getContentPane().add(this, BorderLayout.CENTER);
  1081. if(parent instanceof JDesktopPane) {
  1082. parent.add(iFrame, JLayeredPane.MODAL_LAYER);
  1083. } else {
  1084. parent.add(iFrame, BorderLayout.CENTER);
  1085. }
  1086. Dimension iFrameSize = iFrame.getPreferredSize();
  1087. Dimension rootSize = parent.getSize();
  1088. iFrame.setBounds((rootSize.width - iFrameSize.width) / 2,
  1089. (rootSize.height - iFrameSize.height) / 2,
  1090. iFrameSize.width, iFrameSize.height);
  1091. parent.validate();
  1092. try {
  1093. iFrame.setSelected(true);
  1094. } catch (java.beans.PropertyVetoException e) {}
  1095. return iFrame;
  1096. }
  1097. /**
  1098. * Returns the specified component's <code>Frame</code>.
  1099. *
  1100. * @param parentComponent the <code>Component</code> to check for a
  1101. * <code>Frame</code>
  1102. * @return the <code>Frame</code> that contains the component,
  1103. * or the default frame if the component is <code>null</code>,
  1104. * or does not have a valid <code>Frame</code> parent
  1105. */
  1106. public static Frame getFrameForComponent(Component parentComponent) {
  1107. if (parentComponent == null)
  1108. return getRootFrame();
  1109. if (parentComponent instanceof Frame)
  1110. return (Frame)parentComponent;
  1111. return JOptionPane.getFrameForComponent(parentComponent.getParent());
  1112. }
  1113. /**
  1114. * Returns the specified component's toplevel <code>Frame</code> or
  1115. * <code>Dialog</code>.
  1116. *
  1117. * @param parentComponent the <code>Component</code> to check for a
  1118. * <code>Frame</code> or <code>Dialog</code>
  1119. * @return the <code>Frame</code> or <code>Dialog</code> that
  1120. * contains the component, or the default
  1121. * frame if the component is <code>null</code>,
  1122. * or does not have a valid
  1123. * <code>Frame</code> or <code>Dialog</code> parent
  1124. */
  1125. static Window getWindowForComponent(Component parentComponent) {
  1126. if (parentComponent == null)
  1127. return getRootFrame();
  1128. if (parentComponent instanceof Frame || parentComponent instanceof Dialog)
  1129. return (Window)parentComponent;
  1130. return JOptionPane.getWindowForComponent(parentComponent.getParent());
  1131. }
  1132. /**
  1133. * Returns the specified component's desktop pane.
  1134. *
  1135. * @param parentComponent the <code>Component</code> to check for a
  1136. * desktop
  1137. * @return the <code>JDesktopPane</code> that contains the component,
  1138. * or <code>null</code> if the component is <code>null</code>
  1139. * or does not have an ancestor that is a
  1140. * <code>JInternalFrame</code>
  1141. */
  1142. public static JDesktopPane getDesktopPaneForComponent(Component parentComponent) {
  1143. if(parentComponent == null)
  1144. return null;
  1145. if(parentComponent instanceof JDesktopPane)
  1146. return (JDesktopPane)parentComponent;
  1147. return getDesktopPaneForComponent(parentComponent.getParent());
  1148. }
  1149. private static final Object sharedFrameKey = JOptionPane.class;
  1150. /**
  1151. * Sets the frame to use for class methods in which a frame is
  1152. * not provided.
  1153. *
  1154. * @param newRootFrame the default <code>Frame</code> to use
  1155. */
  1156. public static void setRootFrame(Frame newRootFrame) {
  1157. if (newRootFrame != null) {
  1158. SwingUtilities.appContextPut(sharedFrameKey, newRootFrame);
  1159. } else {
  1160. SwingUtilities.appContextRemove(sharedFrameKey);
  1161. }
  1162. }
  1163. /**
  1164. * Returns the <code>Frame</code> to use for the class methods in
  1165. * which a frame is not provided.
  1166. *
  1167. * @return the default <code>Frame</code> to use
  1168. */
  1169. public static Frame getRootFrame() {
  1170. Frame sharedFrame =
  1171. (Frame)SwingUtilities.appContextGet(sharedFrameKey);
  1172. if (sharedFrame == null) {
  1173. sharedFrame = SwingUtilities.getSharedOwnerFrame();
  1174. SwingUtilities.appContextPut(sharedFrameKey, sharedFrame);
  1175. }
  1176. return sharedFrame;
  1177. }
  1178. /**
  1179. * Creates a <code>JOptionPane</code> with a test message.
  1180. */
  1181. public JOptionPane() {
  1182. this("JOptionPane message");
  1183. }
  1184. /**
  1185. * Creates a instance of <code>JOptionPane</code> to display a
  1186. * message using the
  1187. * plain-message message type and the default options delivered by
  1188. * the UI.
  1189. *
  1190. * @param message the <code>Object</code> to display
  1191. */
  1192. public JOptionPane(Object message) {
  1193. this(message, PLAIN_MESSAGE);
  1194. }
  1195. /**
  1196. * Creates an instance of <code>JOptionPane</code> to display a message
  1197. * with the specified message type and the default options,
  1198. *
  1199. * @param message the <code>Object</code> to display
  1200. * @param messageType the type of message to be displayed:
  1201. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1202. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  1203. */
  1204. public JOptionPane(Object message, int messageType) {
  1205. this(message, messageType, DEFAULT_OPTION);
  1206. }
  1207. /**
  1208. * Creates an instance of <code>JOptionPane</code> to display a message
  1209. * with the specified message type and options.
  1210. *
  1211. * @param message the <code>Object</code> to display
  1212. * @param messageType the type of message to be displayed:
  1213. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1214. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  1215. * @param optionType the options to display in the pane:
  1216. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION
  1217. * OK_CANCEL_OPTION
  1218. */
  1219. public JOptionPane(Object message, int messageType, int optionType) {
  1220. this(message, messageType, optionType, null);
  1221. }
  1222. /**
  1223. * Creates an instance of <code>JOptionPane</code> to display a message
  1224. * with the specified message type, options, and icon.
  1225. *
  1226. * @param message the <code>Object</code> to display
  1227. * @param messageType the type of message to be displayed:
  1228. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1229. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  1230. * @param optionType the options to display in the pane:
  1231. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION
  1232. * OK_CANCEL_OPTION
  1233. * @param icon the <code>Icon</code> image to display
  1234. */
  1235. public JOptionPane(Object message, int messageType, int optionType,
  1236. Icon icon) {
  1237. this(message, messageType, optionType, icon, null);
  1238. }
  1239. /**
  1240. * Creates an instance of JOptionPane to display a message
  1241. * with the specified message type, icon, and options.
  1242. * None of the options is initially selected.
  1243. * <p>
  1244. * The options objects should contain either instances of
  1245. * <code>Component</code>s, (which are added directly) or
  1246. * <code>Strings</code> (which are wrapped in a <code>JButton</code>).
  1247. * If you provide <code>Component</code>s, you must ensure that when the
  1248. * <code>Component</code> is clicked it messages <code>setValue</code>
  1249. * in the created <code>JOptionPane</code>.
  1250. *
  1251. * @param message the <code>Object</code> to display
  1252. * @param messageType the type of message to be displayed:
  1253. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1254. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  1255. * @param optionType the options to display in the pane:
  1256. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION
  1257. * OK_CANCEL_OPTION; only meaningful if the
  1258. * <code>options</code> parameter is <code>null</code>
  1259. * @param icon the <code>Icon</code> image to display
  1260. * @param options the choices the user can select
  1261. */
  1262. public JOptionPane(Object message, int messageType, int optionType,
  1263. Icon icon, Object[] options) {
  1264. this(message, messageType, optionType, icon, options, null);
  1265. }
  1266. /**
  1267. * Creates an instance of <code>JOptionPane</code> to display a message
  1268. * with the specified message type, icon, and options, with the
  1269. * initially-selected option specified.
  1270. *
  1271. * @param message the <code>Object</code> to display
  1272. * @param messageType the type of message to be displayed:
  1273. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1274. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  1275. * @param optionType the options to display in the pane:
  1276. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION
  1277. * OK_CANCEL_OPTION; only meaningful if the
  1278. * <code>options</code> parameter is <code>null</code>
  1279. * @param icon the Icon image to display
  1280. * @param options the choices the user can select
  1281. * @param initialValue the choice that is initially selected
  1282. */
  1283. public JOptionPane(Object message, int messageType, int optionType,
  1284. Icon icon, Object[] options, Object initialValue) {
  1285. this.message = message;
  1286. this.options = options;
  1287. this.initialValue = initialValue;
  1288. this.icon = icon;
  1289. setMessageType(messageType);
  1290. setOptionType(optionType);
  1291. value = UNINITIALIZED_VALUE;
  1292. inputValue = UNINITIALIZED_VALUE;
  1293. updateUI();
  1294. }
  1295. /**
  1296. * Sets the UI object which implements the L&F for this component.
  1297. *
  1298. * @param ui the <code>OptionPaneUI</code> L&F object
  1299. * @see UIDefaults#getUI
  1300. * @beaninfo
  1301. * bound: true
  1302. * hidden: true
  1303. * description: The UI object that implements the optionpane's LookAndFeel
  1304. */
  1305. public void setUI(OptionPaneUI ui) {
  1306. if ((OptionPaneUI)this.ui != ui) {
  1307. super.setUI(ui);
  1308. invalidate();
  1309. }
  1310. }
  1311. /**
  1312. * Returns the UI object which implements the L&F for this component.
  1313. *
  1314. * @return the <code>OptionPaneUI</code> object
  1315. */
  1316. public OptionPaneUI getUI() {
  1317. return (OptionPaneUI)ui;
  1318. }
  1319. /**
  1320. * Notification from the <code>UIManager</code> that the L&F has changed.
  1321. * Replaces the current UI object with the latest version from the
  1322. * <code>UIManager</code>.
  1323. *
  1324. * @see JComponent#updateUI
  1325. */
  1326. public void updateUI() {
  1327. setUI((OptionPaneUI)UIManager.getUI(this));
  1328. }
  1329. /**
  1330. * Returns the name of the UI class that implements the
  1331. * L&F for this component.
  1332. *
  1333. * @return the string "OptionPaneUI"
  1334. * @see JComponent#getUIClassID
  1335. * @see UIDefaults#getUI
  1336. */
  1337. public String getUIClassID() {
  1338. return uiClassID;
  1339. }
  1340. /**
  1341. * Sets the option pane's message-object.
  1342. * @param newMessage the <code>Object</code> to display
  1343. * @see #getMessage
  1344. *
  1345. * @beaninfo
  1346. * preferred: true
  1347. * bound: true
  1348. * description: The optionpane's message object.
  1349. */
  1350. public void setMessage(Object newMessage) {
  1351. Object oldMessage = message;
  1352. message = newMessage;
  1353. firePropertyChange(MESSAGE_PROPERTY, oldMessage, message);
  1354. }
  1355. /**
  1356. * Returns the message-object this pane displays.
  1357. * @see #setMessage
  1358. *
  1359. * @return the <code>Object</code> that is displayed
  1360. */
  1361. public Object getMessage() {
  1362. return message;
  1363. }
  1364. /**
  1365. * Sets the icon to display. If non-<code>null</code>, the Look and Feel
  1366. * does not provide an icon.
  1367. * @param icon the <code>Icon</code> to display
  1368. *
  1369. * @see #getIcon
  1370. * @beaninfo
  1371. * preferred: true
  1372. * bound: true
  1373. * description: The option pane's type icon.
  1374. */
  1375. public void setIcon(Icon newIcon) {
  1376. Object oldIcon = icon;
  1377. icon = newIcon;
  1378. firePropertyChange(ICON_PROPERTY, oldIcon, icon);
  1379. }
  1380. /**
  1381. * Returns the icon this pane displays.
  1382. * @return the <code>Icon</code> that is displayed
  1383. *
  1384. * @see #setIcon
  1385. */
  1386. public Icon getIcon() {
  1387. return icon;
  1388. }
  1389. /**
  1390. * Sets the value the user has chosen.
  1391. * @param newValue the chosen value
  1392. *
  1393. * @see #getValue
  1394. * @beaninfo
  1395. * preferred: true
  1396. * bound: true
  1397. * description: The option pane's value object.
  1398. */
  1399. public void setValue(Object newValue) {
  1400. Object oldValue = value;
  1401. value = newValue;
  1402. firePropertyChange(VALUE_PROPERTY, oldValue, value);
  1403. }
  1404. /**
  1405. * Returns the value the user has selected. UNINITIALIZED_VALUE
  1406. * implies the user has not yet made a choice, <code>null</code> means the
  1407. * user closed the window with out choosing anything. Otherwise
  1408. * the returned value will be one of the options defined in this
  1409. * object.
  1410. *
  1411. * @return the <code>Object</code> chosen by the user, UNINITIALIZED_VALUE
  1412. * if the user has not yet made a choice, or <code>null</code> if
  1413. * the user closed the window without making a choice
  1414. *
  1415. * @see #setValue
  1416. */
  1417. public Object getValue() {
  1418. return value;
  1419. }
  1420. /**
  1421. * Sets the options this pane displays. If an element in
  1422. * <code>newOptions</code> is a <code>Component</code>
  1423. * it is added directly to the pane,
  1424. * otherwise a button is created for the element.
  1425. *
  1426. * @param newOptions an array of <code>Objects</code> that create the
  1427. * buttons the user can click on, or arbitrary
  1428. * <code>Components</code> to add to the pane
  1429. *
  1430. * @see #getOptions
  1431. * @beaninfo
  1432. * bound: true
  1433. * description: The option pane's options objects.
  1434. */
  1435. public void setOptions(Object[] newOptions) {
  1436. Object[] oldOptions = options;
  1437. options = newOptions;
  1438. firePropertyChange(OPTIONS_PROPERTY, oldOptions, options);
  1439. }
  1440. /**
  1441. * Returns the choices the user can make.
  1442. * @return the array of <code>Objects</code> that give the user's choices
  1443. *
  1444. * @see #setOptions
  1445. */
  1446. public Object[] getOptions() {
  1447. if(options != null) {
  1448. int optionCount = options.length;
  1449. Object[] retOptions = new Object[optionCount];
  1450. System.arraycopy(options, 0, retOptions, 0, optionCount);
  1451. return retOptions;
  1452. }
  1453. return options;
  1454. }
  1455. /**
  1456. * Sets the initial value that is to be enabled -- the
  1457. * <code>Component</code>
  1458. * that has the focus when the pane is initially displayed.
  1459. *
  1460. * @param newInitialValue the <code>Object</code> that gets the initial
  1461. * keyboard focus
  1462. *
  1463. * @see #getInitialValue
  1464. * @beaninfo
  1465. * preferred: true
  1466. * bound: true
  1467. * description: The option pane's initial value object.
  1468. */
  1469. public void setInitialValue(Object newInitialValue) {
  1470. Object oldIV = initialValue;
  1471. initialValue = newInitialValue;
  1472. firePropertyChange(INITIAL_VALUE_PROPERTY, oldIV, initialValue);
  1473. }
  1474. /**
  1475. * Returns the initial value.
  1476. *
  1477. * @return the <code>Object</code> that gets the initial keyboard focus
  1478. *
  1479. * @see #setInitialValue
  1480. */
  1481. public Object getInitialValue() {
  1482. return initialValue;
  1483. }
  1484. /**
  1485. * Sets the option pane's message type.
  1486. * The message type is used by the Look and Feel to determine the
  1487. * icon to display (if not supplied) as well as potentially how to
  1488. * lay out the <code>parentComponent</code>.
  1489. * @param newType an integer specifying the kind of message to display:
  1490. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1491. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  1492. * @exception RuntimeException if <code>newType</code> is not one of the
  1493. * legal values listed above
  1494. * @see #getMessageType
  1495. * @beaninfo
  1496. * preferred: true
  1497. * bound: true
  1498. * description: The option pane's message type.
  1499. */
  1500. public void setMessageType(int newType) {
  1501. if(newType != ERROR_MESSAGE && newType != INFORMATION_MESSAGE &&
  1502. newType != WARNING_MESSAGE && newType != QUESTION_MESSAGE &&
  1503. newType != PLAIN_MESSAGE)
  1504. 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");
  1505. int oldType = messageType;
  1506. messageType = newType;
  1507. firePropertyChange(MESSAGE_TYPE_PROPERTY, oldType, messageType);
  1508. }
  1509. /**
  1510. * Returns the message type.
  1511. *
  1512. * @return an integer specifying the message type
  1513. *
  1514. * @see #setMessageType
  1515. */
  1516. public int getMessageType() {
  1517. return messageType;
  1518. }
  1519. /**
  1520. * Sets the options to display.
  1521. * The option type is used by the Look and Feel to
  1522. * determine what buttons to show (unless options are supplied).
  1523. * @param newType an integer specifying the options the L&F is to display:
  1524. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION,
  1525. * or OK_CANCEL_OPTION
  1526. * @exception RuntimeException if <code>newType</code> is not one of
  1527. * the legal values listed above
  1528. *
  1529. * @see #getOptionType
  1530. * @see #setOptions
  1531. * @beaninfo
  1532. * preferred: true
  1533. * bound: true
  1534. * description: The option pane's option type.
  1535. */
  1536. public void setOptionType(int newType) {
  1537. if(newType != DEFAULT_OPTION && newType != YES_NO_OPTION &&
  1538. newType != YES_NO_CANCEL_OPTION && newType != OK_CANCEL_OPTION)
  1539. 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");
  1540. int oldType = optionType;
  1541. optionType = newType;
  1542. firePropertyChange(OPTION_TYPE_PROPERTY, oldType, optionType);
  1543. }
  1544. /**
  1545. * Returns the type of options that are displayed.
  1546. *
  1547. * @return an integer specifying the user-selectable options
  1548. *
  1549. * @see #setOptionType
  1550. */
  1551. public int getOptionType() {
  1552. return optionType;
  1553. }
  1554. /**
  1555. * Sets the selection values for a pane that provides the user
  1556. * with a list of items to choose from. (The UI provides a widget
  1557. * for choosing one of the values.)
  1558. * <p>
  1559. * Sets <code>wantsInput</code> to true. Use
  1560. * <code>setInitialSelectionValue</code> to specify the initially-chosen
  1561. * value. After the pane as been enabled, <code>inputValue</code> is
  1562. * set to the value the user has selected.
  1563. * @param newValues an array of <code>Objects</code> the user to be
  1564. * displayed
  1565. * (usually in a list or combo-box) from which
  1566. * the user can make a selection
  1567. * @see #setWantsInput
  1568. * @see #setInitialSelectionValue
  1569. * @see #getSelectionValues
  1570. * @beaninfo
  1571. * bound: true
  1572. * description: The option pane's selection values.
  1573. */
  1574. public void setSelectionValues(Object[] newValues) {
  1575. Object[] oldValues = selectionValues;
  1576. selectionValues = newValues;
  1577. firePropertyChange(SELECTION_VALUES_PROPERTY, oldValues, newValues);
  1578. if(selectionValues != null)
  1579. setWantsInput(true);
  1580. }
  1581. /**
  1582. * Returns the selection values.
  1583. *
  1584. * @param return the array of <code>Objects</code> the user can select
  1585. * @see #setSelectionValues
  1586. */
  1587. public Object[] getSelectionValues() {
  1588. return selectionValues;
  1589. }
  1590. /**
  1591. * Sets the initial selection value. Only used if <code>wantsInput</code>
  1592. * is true.
  1593. * @param newValue the initially selected value
  1594. * @see #setSelectionValues
  1595. * @see #getInitialSelectionValue
  1596. * @beaninfo
  1597. * bound: true
  1598. * description: The option pane's initial selection value object.
  1599. */
  1600. public void setInitialSelectionValue(Object newValue) {
  1601. Object oldValue = initialSelectionValue;
  1602. initialSelectionValue = newValue;
  1603. firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, oldValue,
  1604. newValue);
  1605. }
  1606. /**
  1607. * Returns the initial-selection value.
  1608. *
  1609. * @return the initially selected value
  1610. * @see #setInitialSelectionValue
  1611. * @see #setSelectionValues
  1612. */
  1613. public Object getInitialSelectionValue() {
  1614. return initialSelectionValue;
  1615. }
  1616. /**
  1617. * Sets the user's input-value.
  1618. *
  1619. * @param newValue the <code>Object</code> used to initialized the
  1620. * value that the user specified (usually in a text field)
  1621. * @see #setSelectionValues
  1622. * @see #setWantsInput
  1623. * @see #getInputValue
  1624. * @beaninfo
  1625. * preferred: true
  1626. * bound: true
  1627. * description: The option pane's input value object.
  1628. */
  1629. public void setInputValue(Object newValue) {
  1630. Object oldValue = inputValue;
  1631. inputValue = newValue;
  1632. firePropertyChange(INPUT_VALUE_PROPERTY, oldValue, newValue);
  1633. }
  1634. /**
  1635. * Returns the value the user has input, if <code>wantsInput</code>
  1636. * is true.
  1637. *
  1638. * @return the <code>Object</code> the user specified,
  1639. * if it was one of the objects, or a
  1640. * <code>String</code> if it was a value typed into a
  1641. * field
  1642. * @see #setSelectionValues
  1643. * @see #setWantsInput
  1644. * @see #setInputValue
  1645. */
  1646. public Object getInputValue() {
  1647. return inputValue;
  1648. }
  1649. /**
  1650. * Returns the maximum number of characters to place on a line in a
  1651. * message. Default is to return <code>Integer.MAX_VALUE</code>.
  1652. * The value can be
  1653. * changed by overriding this method in a subclass.
  1654. *
  1655. * @return an integer giving the maximum number of characters on a line
  1656. */
  1657. public int getMaxCharactersPerLineCount() {
  1658. return Integer.MAX_VALUE;
  1659. }
  1660. /**
  1661. * If <code>newValue</code> is true, a <code>parentComponent</code>
  1662. * is provided to
  1663. * allow the user to input a value. If <code>getSelectionValues</code>
  1664. * returns a non-<code>null</code> the input value is one of the
  1665. * objects in that
  1666. * array. Otherwise the input value is whatever the user inputs.
  1667. * <p>
  1668. * This is a bound property.
  1669. *
  1670. * @see #setSelectionValues
  1671. * @see #setInputValue
  1672. * @beaninfo
  1673. * preferred: true
  1674. * bound: true
  1675. * description: Flag which allows the user to input a value.
  1676. */
  1677. public void setWantsInput(boolean newValue) {
  1678. boolean oldValue = wantsInput;
  1679. wantsInput = newValue;
  1680. firePropertyChange(WANTS_INPUT_PROPERTY, oldValue, newValue);
  1681. }
  1682. /**
  1683. * Returns true if a <code>parentComponent</code> will be provided
  1684. * for the user to input.
  1685. *
  1686. * @return true if a <code>parentComponent</code> will be provided
  1687. * @see #setWantsInput
  1688. */
  1689. public boolean getWantsInput() {
  1690. return wantsInput;
  1691. }
  1692. /**
  1693. * Requests that the initial value be selected, which will set
  1694. * focus to the initial value. This method
  1695. * should be invoked after the window containing the option pane
  1696. * is made visible.
  1697. */
  1698. public void selectInitialValue() {
  1699. OptionPaneUI ui = getUI();
  1700. if (ui != null) {
  1701. ui.selectInitialValue(this);
  1702. }
  1703. }
  1704. // Serialization support.
  1705. private void writeObject(ObjectOutputStream s) throws IOException {
  1706. Vector values = new Vector();
  1707. s.defaultWriteObject();
  1708. // Save the icon, if its Serializable.
  1709. if(icon != null && icon instanceof Serializable) {
  1710. values.addElement("icon");
  1711. values.addElement(icon);
  1712. }
  1713. // Save the message, if its Serializable.
  1714. if(message != null && message instanceof Serializable) {
  1715. values.addElement("message");
  1716. values.addElement(message);
  1717. }
  1718. // Save the treeModel, if its Serializable.
  1719. if(options != null) {
  1720. Vector serOptions = new Vector();
  1721. for(int counter = 0, maxCounter = options.length;
  1722. counter < maxCounter; counter++)
  1723. if(options[counter] instanceof Serializable)
  1724. serOptions.addElement(options[counter]);
  1725. if(serOptions.size() > 0) {
  1726. int optionCount = serOptions.size();
  1727. Object[] arrayOptions = new Object[optionCount];
  1728. serOptions.copyInto(arrayOptions);
  1729. values.addElement("options");
  1730. values.addElement(arrayOptions);
  1731. }
  1732. }
  1733. // Save the initialValue, if its Serializable.
  1734. if(initialValue != null && initialValue instanceof Serializable) {
  1735. values.addElement("initialValue");
  1736. values.addElement(initialValue);
  1737. }
  1738. // Save the value, if its Serializable.
  1739. if(value != null && value instanceof Serializable) {
  1740. values.addElement("value");
  1741. values.addElement(value);
  1742. }
  1743. // Save the selectionValues, if its Serializable.
  1744. if(selectionValues != null) {
  1745. boolean serialize = true;
  1746. for(int counter = 0, maxCounter = selectionValues.length;
  1747. counter < maxCounter; counter++) {
  1748. if(selectionValues[counter] != null &&
  1749. !(selectionValues[counter] instanceof Serializable)) {
  1750. serialize = false;
  1751. break;
  1752. }
  1753. }
  1754. if(serialize) {
  1755. values.addElement("selectionValues");
  1756. values.addElement(selectionValues);
  1757. }
  1758. }
  1759. // Save the inputValue, if its Serializable.
  1760. if(inputValue != null && inputValue instanceof Serializable) {
  1761. values.addElement("inputValue");
  1762. values.addElement(inputValue);
  1763. }
  1764. // Save the initialSelectionValue, if its Serializable.
  1765. if(initialSelectionValue != null &&
  1766. initialSelectionValue instanceof Serializable) {
  1767. values.addElement("initialSelectionValue");
  1768. values.addElement(initialSelectionValue);
  1769. }
  1770. s.writeObject(values);
  1771. }
  1772. private void readObject(ObjectInputStream s)
  1773. throws IOException, ClassNotFoundException {
  1774. s.defaultReadObject();
  1775. Vector values = (Vector)s.readObject();
  1776. int indexCounter = 0;
  1777. int maxCounter = values.size();
  1778. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  1779. equals("icon")) {
  1780. icon = (Icon)values.elementAt(++indexCounter);
  1781. indexCounter++;
  1782. }
  1783. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  1784. equals("message")) {
  1785. message = values.elementAt(++indexCounter);
  1786. indexCounter++;
  1787. }
  1788. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  1789. equals("options")) {
  1790. options = (Object[])values.elementAt(++indexCounter);
  1791. indexCounter++;
  1792. }
  1793. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  1794. equals("initialValue")) {
  1795. initialValue = values.elementAt(++indexCounter);
  1796. indexCounter++;
  1797. }
  1798. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  1799. equals("value")) {
  1800. value = values.elementAt(++indexCounter);
  1801. indexCounter++;
  1802. }
  1803. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  1804. equals("selectionValues")) {
  1805. selectionValues = (Object[])values.elementAt(++indexCounter);
  1806. indexCounter++;
  1807. }
  1808. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  1809. equals("inputValue")) {
  1810. inputValue = values.elementAt(++indexCounter);
  1811. indexCounter++;
  1812. }
  1813. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  1814. equals("initialSelectionValue")) {
  1815. initialSelectionValue = values.elementAt(++indexCounter);
  1816. indexCounter++;
  1817. }
  1818. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  1819. ui.installUI(this);
  1820. }
  1821. }
  1822. /**
  1823. * Returns a string representation of this <code>JOptionPane</code>.
  1824. * This method
  1825. * is intended to be used only for debugging purposes, and the
  1826. * content and format of the returned string may vary between
  1827. * implementations. The returned string may be empty but may not
  1828. * be <code>null</code>.
  1829. *
  1830. * @return a string representation of this <code>JOptionPane</code>
  1831. */
  1832. protected String paramString() {
  1833. String iconString = (icon != null ?
  1834. icon.toString() : "");
  1835. String initialValueString = (initialValue != null ?
  1836. initialValue.toString() : "");
  1837. String messageString = (message != null ?
  1838. message.toString() : "");
  1839. String messageTypeString;
  1840. if (messageType == ERROR_MESSAGE) {
  1841. messageTypeString = "ERROR_MESSAGE";
  1842. } else if (messageType == INFORMATION_MESSAGE) {
  1843. messageTypeString = "INFORMATION_MESSAGE";
  1844. } else if (messageType == WARNING_MESSAGE) {
  1845. messageTypeString = "WARNING_MESSAGE";
  1846. } else if (messageType == QUESTION_MESSAGE) {
  1847. messageTypeString = "QUESTION_MESSAGE";
  1848. } else if (messageType == PLAIN_MESSAGE) {
  1849. messageTypeString = "PLAIN_MESSAGE";
  1850. } else messageTypeString = "";
  1851. String optionTypeString;
  1852. if (optionType == DEFAULT_OPTION) {
  1853. optionTypeString = "DEFAULT_OPTION";
  1854. } else if (optionType == YES_NO_OPTION) {
  1855. optionTypeString = "YES_NO_OPTION";
  1856. } else if (optionType == YES_NO_CANCEL_OPTION) {
  1857. optionTypeString = "YES_NO_CANCEL_OPTION";
  1858. } else if (optionType == OK_CANCEL_OPTION) {
  1859. optionTypeString = "OK_CANCEL_OPTION";
  1860. } else optionTypeString = "";
  1861. String wantsInputString = (wantsInput ?
  1862. "true" : "false");
  1863. return super.paramString() +
  1864. ",icon=" + iconString +
  1865. ",initialValue=" + initialValueString +
  1866. ",message=" + messageString +
  1867. ",messageType=" + messageTypeString +
  1868. ",optionType=" + optionTypeString +
  1869. ",wantsInput=" + wantsInputString;
  1870. }
  1871. ///////////////////
  1872. // Accessibility support
  1873. ///////////////////
  1874. /**
  1875. * Gets the AccessibleContext associated with this JOptionPane.
  1876. * For option panes, the AccessibleContext takes the form of an
  1877. * AccessibleJOptionPane.
  1878. * A new AccessibleJOptionPane instance is created if necessary.
  1879. *
  1880. * @return an AccessibleJOptionPane that serves as the
  1881. * AccessibleContext of this AccessibleJOptionPane
  1882. * @beaninfo
  1883. * expert: true
  1884. * description: The AccessibleContext associated with this option pane
  1885. */
  1886. public AccessibleContext getAccessibleContext() {
  1887. if (accessibleContext == null) {
  1888. accessibleContext = new AccessibleJOptionPane();
  1889. }
  1890. return accessibleContext;
  1891. }
  1892. /**
  1893. * This class implements accessibility support for the
  1894. * <code>JOptionPane</code> class. It provides an implementation of the
  1895. * Java Accessibility API appropriate to option pane user-interface
  1896. * elements.
  1897. * <p>
  1898. * <strong>Warning:</strong>
  1899. * Serialized objects of this class will not be compatible with
  1900. * future Swing releases. The current serialization support is appropriate
  1901. * for short term storage or RMI between applications running the same
  1902. * version of Swing. A future release of Swing will provide support for
  1903. * long term persistence.
  1904. */
  1905. protected class AccessibleJOptionPane extends AccessibleJComponent {
  1906. /**
  1907. * Get the role of this object.
  1908. *
  1909. * @return an instance of AccessibleRole describing the role of the object
  1910. * @see AccessibleRole
  1911. */
  1912. public AccessibleRole getAccessibleRole() {
  1913. return AccessibleRole.OPTION_PANE;
  1914. }
  1915. } // inner class AccessibleJOptionPane
  1916. }