1 7 8 package javax.swing; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Component ; 12 import java.awt.Container ; 13 import java.awt.Dialog ; 14 import java.awt.Dimension ; 15 import java.awt.KeyboardFocusManager ; 16 import java.awt.Frame ; 17 import java.awt.Point ; 18 import java.awt.HeadlessException ; 19 import java.awt.Toolkit ; 20 import java.awt.Window ; 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.awt.event.WindowListener ; 24 import java.awt.event.WindowAdapter ; 25 import java.awt.event.WindowEvent ; 26 import java.awt.event.ComponentAdapter ; 27 import java.awt.event.ComponentEvent ; 28 import java.io.IOException ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.io.Serializable ; 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.InvocationTargetException ; 34 import java.security.AccessController ; 35 import java.security.PrivilegedAction ; 36 import java.util.Vector ; 37 import javax.swing.plaf.OptionPaneUI ; 38 import javax.swing.event.InternalFrameEvent ; 39 import javax.swing.event.InternalFrameAdapter ; 40 import javax.accessibility.*; 41 42 292 public class JOptionPane extends JComponent implements Accessible 293 { 294 298 private static final String uiClassID = "OptionPaneUI"; 299 300 303 public static final Object UNINITIALIZED_VALUE = "uninitializedValue"; 304 305 312 313 public static final int DEFAULT_OPTION = -1; 314 315 public static final int YES_NO_OPTION = 0; 316 317 public static final int YES_NO_CANCEL_OPTION = 1; 318 319 public static final int OK_CANCEL_OPTION = 2; 320 321 325 public static final int YES_OPTION = 0; 326 327 public static final int NO_OPTION = 1; 328 329 public static final int CANCEL_OPTION = 2; 330 331 public static final int OK_OPTION = 0; 332 335 public static final int CLOSED_OPTION = -1; 336 337 342 public static final int ERROR_MESSAGE = 0; 343 344 public static final int INFORMATION_MESSAGE = 1; 345 346 public static final int WARNING_MESSAGE = 2; 347 348 public static final int QUESTION_MESSAGE = 3; 349 350 public static final int PLAIN_MESSAGE = -1; 351 352 353 public static final String ICON_PROPERTY = "icon"; 354 355 public static final String MESSAGE_PROPERTY = "message"; 356 357 public static final String VALUE_PROPERTY = "value"; 358 359 public static final String OPTIONS_PROPERTY = "options"; 360 361 public static final String INITIAL_VALUE_PROPERTY = "initialValue"; 362 363 public static final String MESSAGE_TYPE_PROPERTY = "messageType"; 364 365 public static final String OPTION_TYPE_PROPERTY = "optionType"; 366 367 public static final String SELECTION_VALUES_PROPERTY = "selectionValues"; 368 369 public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue"; 370 371 public static final String INPUT_VALUE_PROPERTY = "inputValue"; 372 373 public static final String WANTS_INPUT_PROPERTY = "wantsInput"; 374 375 376 transient protected Icon icon; 377 378 transient protected Object message; 379 380 transient protected Object [] options; 381 382 transient protected Object initialValue; 383 384 protected int messageType; 385 391 protected int optionType; 392 394 transient protected Object value; 395 397 protected transient Object [] selectionValues; 398 399 protected transient Object inputValue; 400 401 protected transient Object initialSelectionValue; 402 403 protected boolean wantsInput; 404 405 406 417 public static String showInputDialog(Object message) 418 throws HeadlessException { 419 return showInputDialog(null, message); 420 } 421 422 433 public static String showInputDialog(Object message, Object initialSelectionValue) { 434 return showInputDialog(null, message, initialSelectionValue); 435 } 436 437 451 public static String showInputDialog(Component parentComponent, 452 Object message) throws HeadlessException { 453 return showInputDialog(parentComponent, message, UIManager.getString( 454 "OptionPane.inputDialogTitle", parentComponent), QUESTION_MESSAGE); 455 } 456 457 471 public static String showInputDialog(Component parentComponent, Object message, 472 Object initialSelectionValue) { 473 return (String )showInputDialog(parentComponent, message, 474 UIManager.getString("OptionPane.inputDialogTitle", 475 parentComponent), QUESTION_MESSAGE, null, null, 476 initialSelectionValue); 477 } 478 479 500 public static String showInputDialog(Component parentComponent, 501 Object message, String title, int messageType) 502 throws HeadlessException { 503 return (String )showInputDialog(parentComponent, message, title, 504 messageType, null, null, null); 505 } 506 507 543 public static Object showInputDialog(Component parentComponent, 544 Object message, String title, int messageType, Icon icon, 545 Object [] selectionValues, Object initialSelectionValue) 546 throws HeadlessException { 547 JOptionPane pane = new JOptionPane (message, messageType, 548 OK_CANCEL_OPTION, icon, 549 null, null); 550 551 pane.setWantsInput(true); 552 pane.setSelectionValues(selectionValues); 553 pane.setInitialSelectionValue(initialSelectionValue); 554 pane.setComponentOrientation(((parentComponent == null) ? 555 getRootFrame() : parentComponent).getComponentOrientation()); 556 557 int style = styleFromMessageType(messageType); 558 JDialog dialog = pane.createDialog(parentComponent, title, style); 559 560 pane.selectInitialValue(); 561 dialog.show(); 562 dialog.dispose(); 563 564 Object value = pane.getInputValue(); 565 566 if (value == UNINITIALIZED_VALUE) { 567 return null; 568 } 569 return value; 570 } 571 572 585 public static void showMessageDialog(Component parentComponent, 586 Object message) throws HeadlessException { 587 showMessageDialog(parentComponent, message, UIManager.getString( 588 "OptionPane.messageDialogTitle", parentComponent), 589 INFORMATION_MESSAGE); 590 } 591 592 613 public static void showMessageDialog(Component parentComponent, 614 Object message, String title, int messageType) 615 throws HeadlessException { 616 showMessageDialog(parentComponent, message, title, messageType, null); 617 } 618 619 642 public static void showMessageDialog(Component parentComponent, 643 Object message, String title, int messageType, Icon icon) 644 throws HeadlessException { 645 showOptionDialog(parentComponent, message, title, DEFAULT_OPTION, 646 messageType, icon, null, null); 647 } 648 649 666 public static int showConfirmDialog(Component parentComponent, 667 Object message) throws HeadlessException { 668 return showConfirmDialog(parentComponent, message, 669 UIManager.getString("OptionPane.titleText"), 670 YES_NO_CANCEL_OPTION); 671 } 672 673 693 public static int showConfirmDialog(Component parentComponent, 694 Object message, String title, int optionType) 695 throws HeadlessException { 696 return showConfirmDialog(parentComponent, message, title, optionType, 697 QUESTION_MESSAGE); 698 } 699 700 731 public static int showConfirmDialog(Component parentComponent, 732 Object message, String title, int optionType, int messageType) 733 throws HeadlessException { 734 return showConfirmDialog(parentComponent, message, title, optionType, 735 messageType, null); 736 } 737 738 768 public static int showConfirmDialog(Component parentComponent, 769 Object message, String title, int optionType, 770 int messageType, Icon icon) throws HeadlessException { 771 return showOptionDialog(parentComponent, message, title, optionType, 772 messageType, icon, null, null); 773 } 774 775 827 public static int showOptionDialog(Component parentComponent, 828 Object message, String title, int optionType, int messageType, 829 Icon icon, Object [] options, Object initialValue) 830 throws HeadlessException { 831 JOptionPane pane = new JOptionPane (message, messageType, 832 optionType, icon, 833 options, initialValue); 834 835 pane.setInitialValue(initialValue); 836 pane.setComponentOrientation(((parentComponent == null) ? 837 getRootFrame() : parentComponent).getComponentOrientation()); 838 839 int style = styleFromMessageType(messageType); 840 JDialog dialog = pane.createDialog(parentComponent, title, style); 841 842 pane.selectInitialValue(); 843 dialog.show(); 844 dialog.dispose(); 845 846 Object selectedValue = pane.getValue(); 847 848 if(selectedValue == null) 849 return CLOSED_OPTION; 850 if(options == null) { 851 if(selectedValue instanceof Integer ) 852 return ((Integer )selectedValue).intValue(); 853 return CLOSED_OPTION; 854 } 855 for(int counter = 0, maxCounter = options.length; 856 counter < maxCounter; counter++) { 857 if(options[counter].equals(selectedValue)) 858 return counter; 859 } 860 return CLOSED_OPTION; 861 } 862 863 889 public JDialog createDialog(Component parentComponent, String title) 890 throws HeadlessException { 891 int style = styleFromMessageType(getMessageType()); 892 return createDialog(parentComponent, title, style); 893 } 894 895 private JDialog createDialog(Component parentComponent, String title, 896 int style) 897 throws HeadlessException { 898 899 final JDialog dialog; 900 901 Window window = JOptionPane.getWindowForComponent(parentComponent); 902 if (window instanceof Frame ) { 903 dialog = new JDialog ((Frame )window, title, true); 904 } else { 905 dialog = new JDialog ((Dialog )window, title, true); 906 } 907 dialog.setComponentOrientation(this.getComponentOrientation()); 908 if (window instanceof SwingUtilities.SharedOwnerFrame ) { 909 WindowListener ownerShutdownListener = 910 (WindowListener )SwingUtilities.getSharedOwnerFrameShutdownListener(); 911 dialog.addWindowListener(ownerShutdownListener); 912 } 913 Container contentPane = dialog.getContentPane(); 914 915 contentPane.setLayout(new BorderLayout ()); 916 contentPane.add(this, BorderLayout.CENTER); 917 dialog.setResizable(false); 918 if (JDialog.isDefaultLookAndFeelDecorated()) { 919 boolean supportsWindowDecorations = 920 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 921 if (supportsWindowDecorations) { 922 dialog.setUndecorated(true); 923 getRootPane().setWindowDecorationStyle(style); 924 } 925 } 926 dialog.pack(); 927 dialog.setLocationRelativeTo(parentComponent); 928 WindowAdapter adapter = new WindowAdapter () { 929 private boolean gotFocus = false; 930 public void windowClosing(WindowEvent we) { 931 setValue(null); 932 } 933 public void windowGainedFocus(WindowEvent we) { 934 if (!gotFocus) { 936 selectInitialValue(); 937 gotFocus = true; 938 } 939 } 940 }; 941 dialog.addWindowListener(adapter); 942 dialog.addWindowFocusListener(adapter); 943 dialog.addComponentListener(new ComponentAdapter () { 944 public void componentShown(ComponentEvent ce) { 945 setValue(JOptionPane.UNINITIALIZED_VALUE); 947 } 948 }); 949 addPropertyChangeListener(new PropertyChangeListener () { 950 public void propertyChange(PropertyChangeEvent event) { 951 if(dialog.isVisible() && event.getSource() == JOptionPane.this && 955 (event.getPropertyName().equals(VALUE_PROPERTY)) && 956 event.getNewValue() != null && 957 event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { 958 dialog.setVisible(false); 959 } 960 } 961 }); 962 return dialog; 963 } 964 965 966 976 public static void showInternalMessageDialog(Component parentComponent, 977 Object message) { 978 showInternalMessageDialog(parentComponent, message, UIManager. 979 getString("OptionPane.messageDialogTitle", 980 parentComponent), INFORMATION_MESSAGE); 981 } 982 983 1001 public static void showInternalMessageDialog(Component parentComponent, 1002 Object message, String title, 1003 int messageType) { 1004 showInternalMessageDialog(parentComponent, message, title, messageType,null); 1005 } 1006 1007 1026 public static void showInternalMessageDialog(Component parentComponent, 1027 Object message, 1028 String title, int messageType, 1029 Icon icon){ 1030 showInternalOptionDialog(parentComponent, message, title, DEFAULT_OPTION, 1031 messageType, icon, null, null); 1032 } 1033 1034 1045 public static int showInternalConfirmDialog(Component parentComponent, 1046 Object message) { 1047 return showInternalConfirmDialog(parentComponent, message, 1048 UIManager.getString("OptionPane.titleText"), 1049 YES_NO_CANCEL_OPTION); 1050 } 1051 1052 1072 public static int showInternalConfirmDialog(Component parentComponent, 1073 Object message, String title, 1074 int optionType) { 1075 return showInternalConfirmDialog(parentComponent, message, title, optionType, 1076 QUESTION_MESSAGE); 1077 } 1078 1079 1108 public static int showInternalConfirmDialog(Component parentComponent, 1109 Object message, 1110 String title, int optionType, 1111 int messageType) { 1112 return showInternalConfirmDialog(parentComponent, message, title, optionType, 1113 messageType, null); 1114 } 1115 1116 1147 public static int showInternalConfirmDialog(Component parentComponent, 1148 Object message, 1149 String title, int optionType, 1150 int messageType, Icon icon) { 1151 return showInternalOptionDialog(parentComponent, message, title, optionType, 1152 messageType, icon, null, null); 1153 } 1154 1155 1202 public static int showInternalOptionDialog(Component parentComponent, 1203 Object message, 1204 String title, int optionType, 1205 int messageType, Icon icon, 1206 Object [] options, Object initialValue) { 1207 JOptionPane pane = new JOptionPane (message, messageType, 1208 optionType, icon, options, initialValue); 1209 pane.putClientProperty(PopupFactory.forceHeavyWeightPopupKey, 1210 Boolean.TRUE); 1211 Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1212 getFocusOwner(); 1213 1214 pane.setInitialValue(initialValue); 1215 1216 JInternalFrame dialog = 1217 pane.createInternalFrame(parentComponent, title); 1218 pane.selectInitialValue(); 1219 dialog.setVisible(true); 1220 1221 1229 if (dialog.isVisible() && !dialog.isShowing()) { 1230 Container parent = dialog.getParent(); 1231 while (parent != null) { 1232 if (parent.isVisible() == false) { 1233 parent.setVisible(true); 1234 } 1235 parent = parent.getParent(); 1236 } 1237 } 1238 1239 try { 1241 Object obj; 1242 obj = AccessController.doPrivileged(new ModalPrivilegedAction( 1243 Container .class, "startLWModal")); 1244 if (obj != null) { 1245 ((Method )obj).invoke(dialog, null); 1246 } 1247 } catch (IllegalAccessException ex) { 1248 } catch (IllegalArgumentException ex) { 1249 } catch (InvocationTargetException ex) { 1250 } 1251 1252 if (parentComponent instanceof JInternalFrame ) { 1253 try { 1254 ((JInternalFrame )parentComponent).setSelected(true); 1255 } catch (java.beans.PropertyVetoException e) { 1256 } 1257 } 1258 1259 Object selectedValue = pane.getValue(); 1260 1261 if (fo != null && fo.isShowing()) { 1262 fo.requestFocus(); 1263 } 1264 if (selectedValue == null) { 1265 return CLOSED_OPTION; 1266 } 1267 if (options == null) { 1268 if (selectedValue instanceof Integer ) { 1269 return ((Integer )selectedValue).intValue(); 1270 } 1271 return CLOSED_OPTION; 1272 } 1273 for(int counter = 0, maxCounter = options.length; 1274 counter < maxCounter; counter++) { 1275 if (options[counter].equals(selectedValue)) { 1276 return counter; 1277 } 1278 } 1279 return CLOSED_OPTION; 1280 } 1281 1282 1292 public static String showInternalInputDialog(Component parentComponent, 1293 Object message) { 1294 return showInternalInputDialog(parentComponent, message, UIManager. 1295 getString("OptionPane.inputDialogTitle", parentComponent), 1296 QUESTION_MESSAGE); 1297 } 1298 1299 1312 public static String showInternalInputDialog(Component parentComponent, 1313 Object message, String title, int messageType) { 1314 return (String )showInternalInputDialog(parentComponent, message, title, 1315 messageType, null, null, null); 1316 } 1317 1318 1347 public static Object showInternalInputDialog(Component parentComponent, 1348 Object message, String title, int messageType, Icon icon, 1349 Object [] selectionValues, Object initialSelectionValue) { 1350 JOptionPane pane = new JOptionPane (message, messageType, 1351 OK_CANCEL_OPTION, icon, null, null); 1352 pane.putClientProperty(PopupFactory.forceHeavyWeightPopupKey, 1353 Boolean.TRUE); 1354 Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1355 getFocusOwner(); 1356 1357 pane.setWantsInput(true); 1358 pane.setSelectionValues(selectionValues); 1359 pane.setInitialSelectionValue(initialSelectionValue); 1360 1361 JInternalFrame dialog = 1362 pane.createInternalFrame(parentComponent, title); 1363 1364 pane.selectInitialValue(); 1365 dialog.setVisible(true); 1366 1367 1375 if (dialog.isVisible() && !dialog.isShowing()) { 1376 Container parent = dialog.getParent(); 1377 while (parent != null) { 1378 if (parent.isVisible() == false) { 1379 parent.setVisible(true); 1380 } 1381 parent = parent.getParent(); 1382 } 1383 } 1384 1385 try { 1387 Object obj; 1388 obj = AccessController.doPrivileged(new ModalPrivilegedAction( 1389 Container .class, "startLWModal")); 1390 if (obj != null) { 1391 ((Method )obj).invoke(dialog, null); 1392 } 1393 } catch (IllegalAccessException ex) { 1394 } catch (IllegalArgumentException ex) { 1395 } catch (InvocationTargetException ex) { 1396 } 1397 1398 if (parentComponent instanceof JInternalFrame ) { 1399 try { 1400 ((JInternalFrame )parentComponent).setSelected(true); 1401 } catch (java.beans.PropertyVetoException e) { 1402 } 1403 } 1404 1405 if (fo != null && fo.isShowing()) { 1406 fo.requestFocus(); 1407 } 1408 Object value = pane.getInputValue(); 1409 1410 if (value == UNINITIALIZED_VALUE) { 1411 return null; 1412 } 1413 return value; 1414 } 1415 1416 1436 public JInternalFrame createInternalFrame(Component parentComponent, 1437 String title) { 1438 Container parent = 1439 JOptionPane.getDesktopPaneForComponent(parentComponent); 1440 1441 if (parent == null && (parentComponent == null || 1442 (parent = parentComponent.getParent()) == null)) { 1443 throw new RuntimeException ("JOptionPane: parentComponent does " + 1444 "not have a valid parent"); 1445 } 1446 1447 final JInternalFrame iFrame = new JInternalFrame (title, false, true, 1449 false, false); 1450 1451 iFrame.putClientProperty("JInternalFrame.frameType", "optionDialog"); 1452 iFrame.putClientProperty("JInternalFrame.messageType", 1453 new Integer (getMessageType())); 1454 1455 iFrame.addInternalFrameListener(new InternalFrameAdapter () { 1456 public void internalFrameClosing(InternalFrameEvent e) { 1457 if (getValue() == UNINITIALIZED_VALUE) { 1458 setValue(null); 1459 } 1460 } 1461 }); 1462 addPropertyChangeListener(new PropertyChangeListener () { 1463 public void propertyChange(PropertyChangeEvent event) { 1464 if (iFrame.isVisible() && 1468 event.getSource() == JOptionPane.this && 1469 event.getPropertyName().equals(VALUE_PROPERTY)) { 1470 try { 1472 Object obj; 1473 obj = AccessController.doPrivileged( 1474 new ModalPrivilegedAction( 1475 Container .class, "stopLWModal")); 1476 if (obj != null) { 1477 ((Method )obj).invoke(iFrame, null); 1478 } 1479 } catch (IllegalAccessException ex) { 1480 } catch (IllegalArgumentException ex) { 1481 } catch (InvocationTargetException ex) { 1482 } 1483 1484 try { 1485 iFrame.setClosed(true); 1486 } 1487 catch (java.beans.PropertyVetoException e) { 1488 } 1489 1490 iFrame.setVisible(false); 1491 } 1492 } 1493 }); 1494 iFrame.getContentPane().add(this, BorderLayout.CENTER); 1495 if (parent instanceof JDesktopPane ) { 1496 parent.add(iFrame, JLayeredPane.MODAL_LAYER); 1497 } else { 1498 parent.add(iFrame, BorderLayout.CENTER); 1499 } 1500 Dimension iFrameSize = iFrame.getPreferredSize(); 1501 Dimension rootSize = parent.getSize(); 1502 Dimension parentSize = parentComponent.getSize(); 1503 1504 iFrame.setBounds((rootSize.width - iFrameSize.width) / 2, 1505 (rootSize.height - iFrameSize.height) / 2, 1506 iFrameSize.width, iFrameSize.height); 1507 Point iFrameCoord = 1509 SwingUtilities.convertPoint(parentComponent, 0, 0, parent); 1510 int x = (parentSize.width - iFrameSize.width) / 2 + iFrameCoord.x; 1511 int y = (parentSize.height - iFrameSize.height) / 2 + iFrameCoord.y; 1512 1513 int ovrx = x + iFrameSize.width - rootSize.width; 1515 int ovry = y + iFrameSize.height - rootSize.height; 1516 x = Math.max((ovrx > 0? x - ovrx: x), 0); 1517 y = Math.max((ovry > 0? y - ovry: y), 0); 1518 iFrame.setBounds(x, y, iFrameSize.width, iFrameSize.height); 1519 1520 parent.validate(); 1521 try { 1522 iFrame.setSelected(true); 1523 } catch (java.beans.PropertyVetoException e) {} 1524 1525 return iFrame; 1526 } 1527 1528 1541 public static Frame getFrameForComponent(Component parentComponent) 1542 throws HeadlessException { 1543 if (parentComponent == null) 1544 return getRootFrame(); 1545 if (parentComponent instanceof Frame ) 1546 return (Frame )parentComponent; 1547 return JOptionPane.getFrameForComponent(parentComponent.getParent()); 1548 } 1549 1550 1566 static Window getWindowForComponent(Component parentComponent) 1567 throws HeadlessException { 1568 if (parentComponent == null) 1569 return getRootFrame(); 1570 if (parentComponent instanceof Frame || parentComponent instanceof Dialog ) 1571 return (Window )parentComponent; 1572 return JOptionPane.getWindowForComponent(parentComponent.getParent()); 1573 } 1574 1575 1576 1586 public static JDesktopPane getDesktopPaneForComponent(Component parentComponent) { 1587 if(parentComponent == null) 1588 return null; 1589 if(parentComponent instanceof JDesktopPane ) 1590 return (JDesktopPane )parentComponent; 1591 return getDesktopPaneForComponent(parentComponent.getParent()); 1592 } 1593 1594 private static final Object sharedFrameKey = JOptionPane .class; 1595 1596 1602 public static void setRootFrame(Frame newRootFrame) { 1603 if (newRootFrame != null) { 1604 SwingUtilities.appContextPut(sharedFrameKey, newRootFrame); 1605 } else { 1606 SwingUtilities.appContextRemove(sharedFrameKey); 1607 } 1608 } 1609 1610 1620 public static Frame getRootFrame() throws HeadlessException { 1621 Frame sharedFrame = 1622 (Frame )SwingUtilities.appContextGet(sharedFrameKey); 1623 if (sharedFrame == null) { 1624 sharedFrame = SwingUtilities.getSharedOwnerFrame(); 1625 SwingUtilities.appContextPut(sharedFrameKey, sharedFrame); 1626 } 1627 return sharedFrame; 1628 } 1629 1630 1633 public JOptionPane() { 1634 this("JOptionPane message"); 1635 } 1636 1637 1645 public JOptionPane(Object message) { 1646 this(message, PLAIN_MESSAGE); 1647 } 1648 1649 1661 public JOptionPane(Object message, int messageType) { 1662 this(message, messageType, DEFAULT_OPTION); 1663 } 1664 1665 1681 public JOptionPane(Object message, int messageType, int optionType) { 1682 this(message, messageType, optionType, null); 1683 } 1684 1685 1702 public JOptionPane(Object message, int messageType, int optionType, 1703 Icon icon) { 1704 this(message, messageType, optionType, icon, null); 1705 } 1706 1707 1734 public JOptionPane(Object message, int messageType, int optionType, 1735 Icon icon, Object [] options) { 1736 this(message, messageType, optionType, icon, options, null); 1737 } 1738 1739 1762 public JOptionPane(Object message, int messageType, int optionType, 1763 Icon icon, Object [] options, Object initialValue) { 1764 1765 this.message = message; 1766 this.options = options; 1767 this.initialValue = initialValue; 1768 this.icon = icon; 1769 setMessageType(messageType); 1770 setOptionType(optionType); 1771 value = UNINITIALIZED_VALUE; 1772 inputValue = UNINITIALIZED_VALUE; 1773 updateUI(); 1774 } 1775 1776 1786 public void setUI(OptionPaneUI ui) { 1787 if ((OptionPaneUI )this.ui != ui) { 1788 super.setUI(ui); 1789 invalidate(); 1790 } 1791 } 1792 1793 1798 public OptionPaneUI getUI() { 1799 return (OptionPaneUI )ui; 1800 } 1801 1802 1809 public void updateUI() { 1810 setUI((OptionPaneUI )UIManager.getUI(this)); 1811 } 1812 1813 1814 1822 public String getUIClassID() { 1823 return uiClassID; 1824 } 1825 1826 1827 1837 public void setMessage(Object newMessage) { 1838 Object oldMessage = message; 1839 1840 message = newMessage; 1841 firePropertyChange(MESSAGE_PROPERTY, oldMessage, message); 1842 } 1843 1844 1850 public Object getMessage() { 1851 return message; 1852 } 1853 1854 1865 public void setIcon(Icon newIcon) { 1866 Object oldIcon = icon; 1867 1868 icon = newIcon; 1869 firePropertyChange(ICON_PROPERTY, oldIcon, icon); 1870 } 1871 1872 1878 public Icon getIcon() { 1879 return icon; 1880 } 1881 1882 1892 public void setValue(Object newValue) { 1893 Object oldValue = value; 1894 1895 value = newValue; 1896 firePropertyChange(VALUE_PROPERTY, oldValue, value); 1897 } 1898 1899 1913 public Object getValue() { 1914 return value; 1915 } 1916 1917 1932 public void setOptions(Object [] newOptions) { 1933 Object [] oldOptions = options; 1934 1935 options = newOptions; 1936 firePropertyChange(OPTIONS_PROPERTY, oldOptions, options); 1937 } 1938 1939 1945 public Object [] getOptions() { 1946 if(options != null) { 1947 int optionCount = options.length; 1948 Object [] retOptions = new Object [optionCount]; 1949 1950 System.arraycopy(options, 0, retOptions, 0, optionCount); 1951 return retOptions; 1952 } 1953 return options; 1954 } 1955 1956 1970 public void setInitialValue(Object newInitialValue) { 1971 Object oldIV = initialValue; 1972 1973 initialValue = newInitialValue; 1974 firePropertyChange(INITIAL_VALUE_PROPERTY, oldIV, initialValue); 1975 } 1976 1977 1984 public Object getInitialValue() { 1985 return initialValue; 1986 } 1987 1988 2006 public void setMessageType(int newType) { 2007 if(newType != ERROR_MESSAGE && newType != INFORMATION_MESSAGE && 2008 newType != WARNING_MESSAGE && newType != QUESTION_MESSAGE && 2009 newType != PLAIN_MESSAGE) 2010 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"); 2011 2012 int oldType = messageType; 2013 2014 messageType = newType; 2015 firePropertyChange(MESSAGE_TYPE_PROPERTY, oldType, messageType); 2016 } 2017 2018 2025 public int getMessageType() { 2026 return messageType; 2027 } 2028 2029 2048 public void setOptionType(int newType) { 2049 if(newType != DEFAULT_OPTION && newType != YES_NO_OPTION && 2050 newType != YES_NO_CANCEL_OPTION && newType != OK_CANCEL_OPTION) 2051 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"); 2052 2053 int oldType = optionType; 2054 2055 optionType = newType; 2056 firePropertyChange(OPTION_TYPE_PROPERTY, oldType, optionType); 2057 } 2058 2059 2066 public int getOptionType() { 2067 return optionType; 2068 } 2069 2070 2092 public void setSelectionValues(Object [] newValues) { 2093 Object [] oldValues = selectionValues; 2094 2095 selectionValues = newValues; 2096 firePropertyChange(SELECTION_VALUES_PROPERTY, oldValues, newValues); 2097 if(selectionValues != null) 2098 setWantsInput(true); 2099 } 2100 2101 2107 public Object [] getSelectionValues() { 2108 return selectionValues; 2109 } 2110 2111 2121 public void setInitialSelectionValue(Object newValue) { 2122 Object oldValue = initialSelectionValue; 2123 2124 initialSelectionValue = newValue; 2125 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, oldValue, 2126 newValue); 2127 } 2128 2129 2136 public Object getInitialSelectionValue() { 2137 return initialSelectionValue; 2138 } 2139 2140 2159 public void setInputValue(Object newValue) { 2160 Object oldValue = inputValue; 2161 2162 inputValue = newValue; 2163 firePropertyChange(INPUT_VALUE_PROPERTY, oldValue, newValue); 2164 } 2165 2166 2178 public Object getInputValue() { 2179 return inputValue; 2180 } 2181 2182 2190 public int getMaxCharactersPerLineCount() { 2191 return Integer.MAX_VALUE; 2192 } 2193 2194 2213 public void setWantsInput(boolean newValue) { 2214 boolean oldValue = wantsInput; 2215 2216 wantsInput = newValue; 2217 firePropertyChange(WANTS_INPUT_PROPERTY, oldValue, newValue); 2218 } 2219 2220 2226 public boolean getWantsInput() { 2227 return wantsInput; 2228 } 2229 2230 2236 public void selectInitialValue() { 2237 OptionPaneUI ui = getUI(); 2238 if (ui != null) { 2239 ui.selectInitialValue(this); 2240 } 2241 } 2242 2243 2244 private static int styleFromMessageType(int messageType) { 2245 switch (messageType) { 2246 case ERROR_MESSAGE: 2247 return JRootPane.ERROR_DIALOG; 2248 case QUESTION_MESSAGE: 2249 return JRootPane.QUESTION_DIALOG; 2250 case WARNING_MESSAGE: 2251 return JRootPane.WARNING_DIALOG; 2252 case INFORMATION_MESSAGE: 2253 return JRootPane.INFORMATION_DIALOG; 2254 case PLAIN_MESSAGE: 2255 default: 2256 return JRootPane.PLAIN_DIALOG; 2257 } 2258 } 2259 2260 private void writeObject(ObjectOutputStream s) throws IOException { 2262 Vector values = new Vector (); 2263 2264 s.defaultWriteObject(); 2265 if(icon != null && icon instanceof Serializable ) { 2267 values.addElement("icon"); 2268 values.addElement(icon); 2269 } 2270 if(message != null && message instanceof Serializable ) { 2272 values.addElement("message"); 2273 values.addElement(message); 2274 } 2275 if(options != null) { 2277 Vector serOptions = new Vector (); 2278 2279 for(int counter = 0, maxCounter = options.length; 2280 counter < maxCounter; counter++) 2281 if(options[counter] instanceof Serializable ) 2282 serOptions.addElement(options[counter]); 2283 if(serOptions.size() > 0) { 2284 int optionCount = serOptions.size(); 2285 Object [] arrayOptions = new Object [optionCount]; 2286 2287 serOptions.copyInto(arrayOptions); 2288 values.addElement("options"); 2289 values.addElement(arrayOptions); 2290 } 2291 } 2292 if(initialValue != null && initialValue instanceof Serializable ) { 2294 values.addElement("initialValue"); 2295 values.addElement(initialValue); 2296 } 2297 if(value != null && value instanceof Serializable ) { 2299 values.addElement("value"); 2300 values.addElement(value); 2301 } 2302 if(selectionValues != null) { 2304 boolean serialize = true; 2305 2306 for(int counter = 0, maxCounter = selectionValues.length; 2307 counter < maxCounter; counter++) { 2308 if(selectionValues[counter] != null && 2309 !(selectionValues[counter] instanceof Serializable )) { 2310 serialize = false; 2311 break; 2312 } 2313 } 2314 if(serialize) { 2315 values.addElement("selectionValues"); 2316 values.addElement(selectionValues); 2317 } 2318 } 2319 if(inputValue != null && inputValue instanceof Serializable ) { 2321 values.addElement("inputValue"); 2322 values.addElement(inputValue); 2323 } 2324 if(initialSelectionValue != null && 2326 initialSelectionValue instanceof Serializable ) { 2327 values.addElement("initialSelectionValue"); 2328 values.addElement(initialSelectionValue); 2329 } 2330 s.writeObject(values); 2331 } 2332 2333 private void readObject(ObjectInputStream s) 2334 throws IOException , ClassNotFoundException { 2335 s.defaultReadObject(); 2336 2337 Vector values = (Vector )s.readObject(); 2338 int indexCounter = 0; 2339 int maxCounter = values.size(); 2340 2341 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2342 equals("icon")) { 2343 icon = (Icon )values.elementAt(++indexCounter); 2344 indexCounter++; 2345 } 2346 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2347 equals("message")) { 2348 message = values.elementAt(++indexCounter); 2349 indexCounter++; 2350 } 2351 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2352 equals("options")) { 2353 options = (Object [])values.elementAt(++indexCounter); 2354 indexCounter++; 2355 } 2356 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2357 equals("initialValue")) { 2358 initialValue = values.elementAt(++indexCounter); 2359 indexCounter++; 2360 } 2361 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2362 equals("value")) { 2363 value = values.elementAt(++indexCounter); 2364 indexCounter++; 2365 } 2366 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2367 equals("selectionValues")) { 2368 selectionValues = (Object [])values.elementAt(++indexCounter); 2369 indexCounter++; 2370 } 2371 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2372 equals("inputValue")) { 2373 inputValue = values.elementAt(++indexCounter); 2374 indexCounter++; 2375 } 2376 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2377 equals("initialSelectionValue")) { 2378 initialSelectionValue = values.elementAt(++indexCounter); 2379 indexCounter++; 2380 } 2381 if (getUIClassID().equals(uiClassID)) { 2382 byte count = JComponent.getWriteObjCounter(this); 2383 JComponent.setWriteObjCounter(this, --count); 2384 if (count == 0 && ui != null) { 2385 ui.installUI(this); 2386 } 2387 } 2388 } 2389 2390 2391 2401 protected String paramString() { 2402 String iconString = (icon != null ? 2403 icon.toString() : ""); 2404 String initialValueString = (initialValue != null ? 2405 initialValue.toString() : ""); 2406 String messageString = (message != null ? 2407 message.toString() : ""); 2408 String messageTypeString; 2409 if (messageType == ERROR_MESSAGE) { 2410 messageTypeString = "ERROR_MESSAGE"; 2411 } else if (messageType == INFORMATION_MESSAGE) { 2412 messageTypeString = "INFORMATION_MESSAGE"; 2413 } else if (messageType == WARNING_MESSAGE) { 2414 messageTypeString = "WARNING_MESSAGE"; 2415 } else if (messageType == QUESTION_MESSAGE) { 2416 messageTypeString = "QUESTION_MESSAGE"; 2417 } else if (messageType == PLAIN_MESSAGE) { 2418 messageTypeString = "PLAIN_MESSAGE"; 2419 } else messageTypeString = ""; 2420 String optionTypeString; 2421 if (optionType == DEFAULT_OPTION) { 2422 optionTypeString = "DEFAULT_OPTION"; 2423 } else if (optionType == YES_NO_OPTION) { 2424 optionTypeString = "YES_NO_OPTION"; 2425 } else if (optionType == YES_NO_CANCEL_OPTION) { 2426 optionTypeString = "YES_NO_CANCEL_OPTION"; 2427 } else if (optionType == OK_CANCEL_OPTION) { 2428 optionTypeString = "OK_CANCEL_OPTION"; 2429 } else optionTypeString = ""; 2430 String wantsInputString = (wantsInput ? 2431 "true" : "false"); 2432 2433 return super.paramString() + 2434 ",icon=" + iconString + 2435 ",initialValue=" + initialValueString + 2436 ",message=" + messageString + 2437 ",messageType=" + messageTypeString + 2438 ",optionType=" + optionTypeString + 2439 ",wantsInput=" + wantsInputString; 2440 } 2441 2442 2445 private static class ModalPrivilegedAction implements PrivilegedAction { 2446 private Class clazz; 2447 private String methodName; 2448 2449 public ModalPrivilegedAction(Class clazz, String methodName) { 2450 this.clazz = clazz; 2451 this.methodName = methodName; 2452 } 2453 2454 public Object run() { 2455 Method method = null; 2456 try { 2457 method = clazz.getDeclaredMethod(methodName, null); 2458 } catch (NoSuchMethodException ex) { 2459 } 2460 if (method != null) { 2461 method.setAccessible(true); 2462 } 2463 return method; 2464 } 2465 } 2466 2467 2468 2469 2473 2485 public AccessibleContext getAccessibleContext() { 2486 if (accessibleContext == null) { 2487 accessibleContext = new AccessibleJOptionPane(); 2488 } 2489 return accessibleContext; 2490 } 2491 2492 2507 protected class AccessibleJOptionPane extends AccessibleJComponent { 2508 2509 2515 public AccessibleRole getAccessibleRole() { 2516 return AccessibleRole.OPTION_PANE; 2517 } 2518 2519 } } 2521 2522 | Popular Tags |