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