| 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 |