GNU Classpath (0.19) | ||
Frames | No Frames |
1: /* JOptionPane.java 2: Copyright (C) 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.swing; 40: 41: import java.awt.Component; 42: import java.awt.Frame; 43: 44: import javax.accessibility.Accessible; 45: import javax.accessibility.AccessibleContext; 46: import javax.accessibility.AccessibleRole; 47: import javax.swing.event.InternalFrameAdapter; 48: import javax.swing.event.InternalFrameEvent; 49: import javax.swing.plaf.OptionPaneUI; 50: 51: /** 52: * This class creates different types of JDialogs and JInternalFrames that can 53: * ask users for input or pass on information. JOptionPane can be used by 54: * calling one of the show static methods or by creating an instance of 55: * JOptionPane and calling createDialog or createInternalFrame. 56: */ 57: public class JOptionPane extends JComponent implements Accessible 58: { 59: /** 60: * DOCUMENT ME! 61: */ 62: // FIXME: This inner class is a complete stub and needs to be implemented 63: // properly. 64: protected class AccessibleJOptionPane extends JComponent.AccessibleJComponent 65: { 66: /** DOCUMENT ME! */ 67: private static final long serialVersionUID = 686071432213084821L; 68: 69: /** 70: * Creates a new AccessibleJOptionPane object. 71: */ 72: protected AccessibleJOptionPane() 73: { 74: // Nothing to do here. 75: } 76: 77: /** 78: * DOCUMENT ME! 79: * 80: * @return DOCUMENT ME! 81: */ 82: public AccessibleRole getAccessibleRole() 83: { 84: return null; 85: } 86: } 87: 88: /** DOCUMENT ME! */ 89: private static final long serialVersionUID = 5231143276678566796L; 90: 91: /** The value returned when cancel option is selected. */ 92: public static final int CANCEL_OPTION = 2; 93: 94: /** The value returned when the dialog is closed without a selection. */ 95: public static final int CLOSED_OPTION = -1; 96: 97: /** An option used in confirmation dialog methods. */ 98: public static final int DEFAULT_OPTION = -1; 99: 100: /** The value returned when the no option is selected. */ 101: public static final int NO_OPTION = 1; 102: 103: /** An option used in confirmation dialog methods. */ 104: public static final int OK_CANCEL_OPTION = 2; 105: 106: /** The value returned when the ok option is selected. */ 107: public static final int OK_OPTION = 0; 108: 109: /** An option used in confirmation dialog methods. */ 110: public static final int YES_NO_CANCEL_OPTION = 1; 111: 112: /** An option used in confirmation dialog methods. */ 113: public static final int YES_NO_OPTION = 0; 114: 115: /** The value returned when the yes option is selected. */ 116: public static final int YES_OPTION = 0; 117: 118: /** Identifier for the error message type. */ 119: public static final int ERROR_MESSAGE = 0; 120: 121: /** Identifier for the information message type. */ 122: public static final int INFORMATION_MESSAGE = 1; 123: 124: /** Identifier for the plain message type. */ 125: public static final int PLAIN_MESSAGE = -1; 126: 127: /** Identifier for the question message type. */ 128: public static final int QUESTION_MESSAGE = 3; 129: 130: /** Identifier for the warning message type. */ 131: public static final int WARNING_MESSAGE = 2; 132: 133: /** 134: * The identifier for the propertyChangeEvent when the icon property 135: * changes. 136: */ 137: public static final String ICON_PROPERTY = "icon"; 138: 139: /** 140: * The identifier for the propertyChangeEvent when the initialSelectionValue 141: * property changes. 142: */ 143: public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue"; 144: 145: /** 146: * The identifier for the propertyChangeEvent when the initialValue property 147: * changes. 148: */ 149: public static final String INITIAL_VALUE_PROPERTY = "initialValue"; 150: 151: /** 152: * The identifier for the propertyChangeEvent when the inputValue property 153: * changes. 154: */ 155: public static final String INPUT_VALUE_PROPERTY = "inputValue"; 156: 157: /** 158: * The identifier for the propertyChangeEvent when the message property 159: * changes. 160: */ 161: public static final String MESSAGE_PROPERTY = "message"; 162: 163: /** 164: * The identifier for the propertyChangeEvent when the messageType property 165: * changes. 166: */ 167: public static final String MESSAGE_TYPE_PROPERTY = "messageType"; 168: 169: /** 170: * The identifier for the propertyChangeEvent when the optionType property 171: * changes. 172: */ 173: public static final String OPTION_TYPE_PROPERTY = "optionType"; 174: 175: /** 176: * The identifier for the propertyChangeEvent when the options property 177: * changes. 178: */ 179: public static final String OPTIONS_PROPERTY = "options"; 180: 181: /** 182: * The identifier for the propertyChangeEvent when the selectionValues 183: * property changes. 184: */ 185: public static final String SELECTION_VALUES_PROPERTY = "selectionValues"; 186: 187: /** 188: * The identifier for the propertyChangeEvent when the value property 189: * changes. 190: */ 191: public static final String VALUE_PROPERTY = "value"; 192: 193: /** 194: * The identifier for the propertyChangeEvent when the wantsInput property 195: * changes. 196: */ 197: public static final String WANTS_INPUT_PROPERTY = "wantsInput"; 198: 199: /** The value returned when the inputValue is uninitialized. */ 200: public static Object UNINITIALIZED_VALUE = "uninitializedValue"; 201: 202: /** The icon displayed in the dialog/internal frame. */ 203: protected Icon icon; 204: 205: /** The initial selected value in the input component. */ 206: protected Object initialSelectionValue; 207: 208: /** The object that is initially selected for options. */ 209: protected Object initialValue; 210: 211: /** The value the user inputs. */ 212: protected Object inputValue = UNINITIALIZED_VALUE; 213: 214: /** The message displayed in the dialog/internal frame. */ 215: protected Object message; 216: 217: /** The type of message displayed. */ 218: protected int messageType = PLAIN_MESSAGE; 219: 220: /** 221: * The options (usually buttons) aligned at the bottom for the user to 222: * select. 223: */ 224: protected Object[] options; 225: 226: /** The type of options to display. */ 227: protected int optionType = DEFAULT_OPTION; 228: 229: /** The input values the user can select. */ 230: protected Object[] selectionValues; 231: 232: /** The value returned by selecting an option. */ 233: protected Object value = UNINITIALIZED_VALUE; 234: 235: /** Whether the Dialog/InternalFrame needs input. */ 236: protected boolean wantsInput; 237: 238: /** The common frame used when no parent is provided. */ 239: private static Frame privFrame = SwingUtilities.getOwnerFrame(); 240: 241: /** 242: * Creates a new JOptionPane object using a message of "JOptionPane 243: * message", using the PLAIN_MESSAGE type and DEFAULT_OPTION. 244: */ 245: public JOptionPane() 246: { 247: this("JOptionPane message", PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null); 248: } 249: 250: /** 251: * Creates a new JOptionPane object using the given message using the 252: * PLAIN_MESSAGE type and DEFAULT_OPTION. 253: * 254: * @param message The message to display. 255: */ 256: public JOptionPane(Object message) 257: { 258: this(message, PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null); 259: } 260: 261: /** 262: * Creates a new JOptionPane object using the given message and messageType 263: * and DEFAULT_OPTION. 264: * 265: * @param message The message to display. 266: * @param messageType The type of message. 267: */ 268: public JOptionPane(Object message, int messageType) 269: { 270: this(message, messageType, DEFAULT_OPTION, null, null, null); 271: } 272: 273: /** 274: * Creates a new JOptionPane object using the given message, messageType and 275: * optionType. 276: * 277: * @param message The message to display. 278: * @param messageType The type of message. 279: * @param optionType The type of options. 280: */ 281: public JOptionPane(Object message, int messageType, int optionType) 282: { 283: this(message, messageType, optionType, null, null, null); 284: } 285: 286: /** 287: * Creates a new JOptionPane object using the given message, messageType, 288: * optionType and icon. 289: * 290: * @param message The message to display. 291: * @param messageType The type of message. 292: * @param optionType The type of options. 293: * @param icon The icon to display. 294: */ 295: public JOptionPane(Object message, int messageType, int optionType, Icon icon) 296: { 297: this(message, messageType, optionType, icon, null, null); 298: } 299: 300: /** 301: * Creates a new JOptionPane object using the given message, messageType, 302: * optionType, icon and options. 303: * 304: * @param message The message to display. 305: * @param messageType The type of message. 306: * @param optionType The type of options. 307: * @param icon The icon to display. 308: * @param options The options given. 309: */ 310: public JOptionPane(Object message, int messageType, int optionType, 311: Icon icon, Object[] options) 312: { 313: this(message, messageType, optionType, icon, options, null); 314: } 315: 316: /** 317: * Creates a new JOptionPane object using the given message, messageType, 318: * optionType, icon, options and initialValue. The initialValue will be 319: * focused initially. 320: * 321: * @param message The message to display. 322: * @param messageType The type of message. 323: * @param optionType The type of options. 324: * @param icon The icon to display. 325: * @param options The options given. 326: * @param initialValue The component to focus on initially. 327: * 328: * @throws IllegalArgumentException If the messageType or optionType are not 329: * legal values. 330: */ 331: public JOptionPane(Object message, int messageType, int optionType, 332: Icon icon, Object[] options, Object initialValue) 333: { 334: this.message = message; 335: if (! validMessageType(messageType)) 336: throw new IllegalArgumentException("Message Type not legal value."); 337: this.messageType = messageType; 338: if (! validOptionType(optionType)) 339: throw new IllegalArgumentException("Option Type not legal value."); 340: this.optionType = optionType; 341: this.icon = icon; 342: this.options = options; 343: this.initialValue = initialValue; 344: 345: setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 346: 347: updateUI(); 348: } 349: 350: /** 351: * This method creates a new JDialog that is either centered around the 352: * parent's frame or centered on the screen (if the parent is null). The 353: * JDialog will not be resizable and will be modal. Once the JDialog is 354: * disposed, the inputValue and value properties will be set by the 355: * optionPane. 356: * 357: * @param parentComponent The parent of the Dialog. 358: * @param title The title in the bar of the JDialog. 359: * 360: * @return A new JDialog based on the JOptionPane configuration. 361: */ 362: public JDialog createDialog(Component parentComponent, String title) 363: { 364: Frame toUse = getFrameForComponent(parentComponent); 365: if (toUse == null) 366: toUse = getRootFrame(); 367: 368: JDialog dialog = new JDialog(toUse, title); 369: inputValue = UNINITIALIZED_VALUE; 370: value = UNINITIALIZED_VALUE; 371: 372: // FIXME: This dialog should be centered on the parent 373: // or at the center of the screen (if the parent is null) 374: // Need getGraphicsConfiguration to return non-null in 375: // order for that to work so we know how large the 376: // screen is. 377: dialog.getContentPane().add(this); 378: dialog.setModal(true); 379: dialog.setResizable(false); 380: 381: return dialog; 382: } 383: 384: /** 385: * This method creates a new JInternalFrame that is in the JLayeredPane 386: * which contains the parentComponent given. If no suitable JLayeredPane 387: * can be found from the parentComponent given, a RuntimeException will be 388: * thrown. 389: * 390: * @param parentComponent The parent to find a JDesktopPane from. 391: * @param title The title of the JInternalFrame. 392: * 393: * @return A new JInternalFrame based on the JOptionPane configuration. 394: * 395: * @throws RuntimeException If no suitable JDesktopPane is found. 396: * 397: * @specnote The specification says that the internal frame is placed 398: * in the nearest <code>JDesktopPane</code> that is found in 399: * <code>parent</code>'s ancestors. The behaviour of the JDK 400: * is that it actually looks up the nearest 401: * <code>JLayeredPane</code> in <code>parent</code>'s ancestors. 402: * So do we. 403: */ 404: public JInternalFrame createInternalFrame(Component parentComponent, 405: String title) 406: throws RuntimeException 407: { 408: // Try to find a JDesktopPane. 409: JLayeredPane toUse = getDesktopPaneForComponent(parentComponent); 410: // If we don't have a JDesktopPane, we try to find a JLayeredPane. 411: if (toUse == null) 412: toUse = JLayeredPane.getLayeredPaneAbove(parentComponent); 413: // If this still fails, we throw a RuntimeException. 414: if (toUse == null) 415: throw new RuntimeException 416: ("parentComponent does not have a valid parent"); 417: 418: JInternalFrame frame = new JInternalFrame(title); 419: 420: inputValue = UNINITIALIZED_VALUE; 421: value = UNINITIALIZED_VALUE; 422: 423: frame.setContentPane(this); 424: frame.setClosable(true); 425: 426: toUse.add(frame); 427: frame.setLayer(JLayeredPane.MODAL_LAYER); 428: 429: frame.pack(); 430: frame.setVisible(true); 431: 432: return frame; 433: } 434: 435: /** 436: * DOCUMENT ME! 437: * 438: * @return DOCUMENT ME! 439: */ 440: public AccessibleContext getAccessibleContext() 441: { 442: if (accessibleContext == null) 443: accessibleContext = new AccessibleJOptionPane(); 444: return accessibleContext; 445: } 446: 447: /** 448: * This method returns the JDesktopPane for the given parentComponent or 449: * null if none can be found. 450: * 451: * @param parentComponent The component to look in. 452: * 453: * @return The JDesktopPane for the given component or null if none can be 454: * found. 455: */ 456: public static JDesktopPane getDesktopPaneForComponent(Component parentComponent) 457: { 458: return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class, 459: parentComponent); 460: } 461: 462: /** 463: * This method returns the Frame for the given parentComponent or null if 464: * none can be found. 465: * 466: * @param parentComponent The component to look in. 467: * 468: * @return The Frame for the given component or null if none can be found. 469: */ 470: public static Frame getFrameForComponent(Component parentComponent) 471: { 472: return (Frame) SwingUtilities.getAncestorOfClass(Frame.class, 473: parentComponent); 474: } 475: 476: /** 477: * This method returns the icon displayed. 478: * 479: * @return The icon displayed. 480: */ 481: public Icon getIcon() 482: { 483: return icon; 484: } 485: 486: /** 487: * This method returns the value initially selected from the list of values 488: * the user can input. 489: * 490: * @return The initial selection value. 491: */ 492: public Object getInitialSelectionValue() 493: { 494: return initialSelectionValue; 495: } 496: 497: /** 498: * This method returns the value that is focused from the list of options. 499: * 500: * @return The initial value from options. 501: */ 502: public Object getInitialValue() 503: { 504: return initialValue; 505: } 506: 507: /** 508: * This method returns the value that the user input. 509: * 510: * @return The user's input value. 511: */ 512: public Object getInputValue() 513: { 514: if (getValue().equals(new Integer(CANCEL_OPTION))) 515: setInputValue(null); 516: return inputValue; 517: } 518: 519: /** 520: * This method returns the maximum characters per line. By default, this is 521: * Integer.MAX_VALUE. 522: * 523: * @return The maximum characters per line. 524: */ 525: public int getMaxCharactersPerLineCount() 526: { 527: return Integer.MAX_VALUE; 528: } 529: 530: /** 531: * This method returns the message displayed. 532: * 533: * @return The message displayed. 534: */ 535: public Object getMessage() 536: { 537: return message; 538: } 539: 540: /** 541: * This method returns the message type. 542: * 543: * @return The message type. 544: */ 545: public int getMessageType() 546: { 547: return messageType; 548: } 549: 550: /** 551: * This method returns the options. 552: * 553: * @return The options. 554: */ 555: public Object[] getOptions() 556: { 557: return options; 558: } 559: 560: /** 561: * This method returns the option type. 562: * 563: * @return The option type. 564: */ 565: public int getOptionType() 566: { 567: return optionType; 568: } 569: 570: /** 571: * This method returns the Frame used by JOptionPane dialog's that have no 572: * parent. 573: * 574: * @return The Frame used by dialogs that have no parent. 575: */ 576: public static Frame getRootFrame() 577: { 578: return privFrame; 579: } 580: 581: /** 582: * This method returns the selection values. 583: * 584: * @return The selection values. 585: */ 586: public Object[] getSelectionValues() 587: { 588: return selectionValues; 589: } 590: 591: /** 592: * This method returns the UI used by the JOptionPane. 593: * 594: * @return The UI used by the JOptionPane. 595: */ 596: public OptionPaneUI getUI() 597: { 598: return (OptionPaneUI) ui; 599: } 600: 601: /** 602: * This method returns an identifier to determine which UI class will act as 603: * the UI. 604: * 605: * @return The UI identifier. 606: */ 607: public String getUIClassID() 608: { 609: return "OptionPaneUI"; 610: } 611: 612: /** 613: * This method returns the value that the user selected out of options. 614: * 615: * @return The value that the user selected out of options. 616: */ 617: public Object getValue() 618: { 619: return value; 620: } 621: 622: /** 623: * This method returns whether this JOptionPane wants input. 624: * 625: * @return Whether this JOptionPane wants input. 626: */ 627: public boolean getWantsInput() 628: { 629: return wantsInput; 630: } 631: 632: /** 633: * This method returns a String that describes this JOptionPane. 634: * 635: * @return A String that describes this JOptionPane. 636: */ 637: protected String paramString() 638: { 639: return "JOptionPane"; 640: } 641: 642: /** 643: * This method requests focus for the initial value. 644: */ 645: public void selectInitialValue() 646: { 647: if (ui != null) 648: ((OptionPaneUI) ui).selectInitialValue(this); 649: } 650: 651: /** 652: * This method changes the icon property. 653: * 654: * @param newIcon The new icon to use. 655: */ 656: public void setIcon(Icon newIcon) 657: { 658: if (icon != newIcon) 659: { 660: Icon old = icon; 661: icon = newIcon; 662: firePropertyChange(ICON_PROPERTY, old, icon); 663: } 664: } 665: 666: /** 667: * This method changes the initial selection property. 668: * 669: * @param newValue The new initial selection. 670: */ 671: public void setInitialSelectionValue(Object newValue) 672: { 673: if (initialSelectionValue != newValue) 674: { 675: Object old = initialSelectionValue; 676: initialSelectionValue = newValue; 677: firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old, 678: initialSelectionValue); 679: } 680: } 681: 682: /** 683: * This method changes the initial value property. 684: * 685: * @param newValue The new initial value. 686: */ 687: public void setInitialValue(Object newValue) 688: { 689: if (initialValue != newValue) 690: { 691: Object old = initialValue; 692: initialValue = newValue; 693: firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue); 694: } 695: } 696: 697: /** 698: * This method changes the inputValue property. 699: * 700: * @param newValue The new inputValue. 701: */ 702: public void setInputValue(Object newValue) 703: { 704: if (inputValue != newValue) 705: { 706: Object old = inputValue; 707: inputValue = newValue; 708: firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue); 709: } 710: } 711: 712: /** 713: * This method changes the message property. 714: * 715: * @param newMessage The new message. 716: */ 717: public void setMessage(Object newMessage) 718: { 719: if (message != newMessage) 720: { 721: Object old = message; 722: message = newMessage; 723: firePropertyChange(MESSAGE_PROPERTY, old, message); 724: } 725: } 726: 727: /** 728: * This method changes the messageType property. 729: * 730: * @param newType The new messageType. 731: * 732: * @throws IllegalArgumentException If the messageType is not valid. 733: */ 734: public void setMessageType(int newType) 735: { 736: if (! validMessageType(newType)) 737: throw new IllegalArgumentException("Message Type not legal value."); 738: if (newType != messageType) 739: { 740: int old = messageType; 741: messageType = newType; 742: firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType); 743: } 744: } 745: 746: /** 747: * This method changes the options property. 748: * 749: * @param newOptions The new options. 750: */ 751: public void setOptions(Object[] newOptions) 752: { 753: if (options != newOptions) 754: { 755: Object[] old = options; 756: options = newOptions; 757: firePropertyChange(OPTIONS_PROPERTY, old, options); 758: } 759: } 760: 761: /** 762: * This method changes the optionType property. 763: * 764: * @param newType The new optionType. 765: * 766: * @throws IllegalArgumentException If the optionType is not valid. 767: */ 768: public void setOptionType(int newType) 769: { 770: if (! validOptionType(newType)) 771: throw new IllegalArgumentException("Option Type not legal value."); 772: if (newType != optionType) 773: { 774: int old = optionType; 775: optionType = newType; 776: firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType); 777: } 778: } 779: 780: /** 781: * This method changes the Frame used for JOptionPane dialogs that have no 782: * parent. 783: * 784: * @param newRootFrame The Frame to use for dialogs that have no parent. 785: */ 786: public static void setRootFrame(Frame newRootFrame) 787: { 788: privFrame = newRootFrame; 789: } 790: 791: /** 792: * This method changes the selectionValues property. 793: * 794: * @param newValues The new selectionValues. 795: */ 796: public void setSelectionValues(Object[] newValues) 797: { 798: if (newValues != selectionValues) 799: { 800: if (newValues != null) 801: wantsInput = true; 802: Object[] old = selectionValues; 803: selectionValues = newValues; 804: firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues); 805: } 806: } 807: 808: /** 809: * This method sets the UI used with the JOptionPane. 810: * 811: * @param ui The UI used with the JOptionPane. 812: */ 813: public void setUI(OptionPaneUI ui) 814: { 815: super.setUI(ui); 816: } 817: 818: /** 819: * This method sets the value has been selected out of options. 820: * 821: * @param newValue The value that has been selected out of options. 822: */ 823: public void setValue(Object newValue) 824: { 825: if (value != newValue) 826: { 827: Object old = value; 828: value = newValue; 829: firePropertyChange(VALUE_PROPERTY, old, value); 830: } 831: } 832: 833: /** 834: * This method changes the wantsInput property. 835: * 836: * @param newValue Whether this JOptionPane requires input. 837: */ 838: public void setWantsInput(boolean newValue) 839: { 840: if (wantsInput != newValue) 841: { 842: boolean old = wantsInput; 843: wantsInput = newValue; 844: firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput); 845: } 846: } 847: 848: /** 849: * This method shows a confirmation dialog with the title "Select an Option" 850: * and displays the given message. The parent frame will be the same as the 851: * parent frame of the given parentComponent. This method returns the 852: * option chosen by the user. 853: * 854: * @param parentComponent The parentComponent to find a frame in. 855: * @param message The message to display. 856: * 857: * @return The option that was selected. 858: */ 859: public static int showConfirmDialog(Component parentComponent, Object message) 860: { 861: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 862: JDialog dialog = pane.createDialog(parentComponent, "Select an Option"); 863: 864: dialog.pack(); 865: dialog.show(); 866: 867: if (pane.getValue() instanceof Integer) 868: return ((Integer) pane.getValue()).intValue(); 869: return -1; 870: } 871: 872: /** 873: * This method shows a confirmation dialog with the given message, 874: * optionType and title. The frame that owns the dialog will be the same 875: * frame that holds the given parentComponent. This method returns the 876: * option that was chosen. 877: * 878: * @param parentComponent The component to find a frame in. 879: * @param message The message displayed. 880: * @param title The title of the dialog. 881: * @param optionType The optionType. 882: * 883: * @return The option that was chosen. 884: */ 885: public static int showConfirmDialog(Component parentComponent, 886: Object message, String title, 887: int optionType) 888: { 889: JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType); 890: JDialog dialog = pane.createDialog(parentComponent, title); 891: dialog.pack(); 892: dialog.show(); 893: 894: if (pane.getValue() instanceof Integer) 895: return ((Integer) pane.getValue()).intValue(); 896: return -1; 897: } 898: 899: /** 900: * This method shows a confirmation dialog with the given message, title, 901: * messageType and optionType. The frame owner will be the same frame as 902: * the one that holds the given parentComponent. This method returns the 903: * option selected by the user. 904: * 905: * @param parentComponent The component to find a frame in. 906: * @param message The message displayed. 907: * @param title The title of the dialog. 908: * @param optionType The optionType. 909: * @param messageType The messageType. 910: * 911: * @return The selected option. 912: */ 913: public static int showConfirmDialog(Component parentComponent, 914: Object message, String title, 915: int optionType, int messageType) 916: { 917: JOptionPane pane = new JOptionPane(message, messageType, optionType); 918: JDialog dialog = pane.createDialog(parentComponent, title); 919: dialog.pack(); 920: dialog.show(); 921: 922: if (pane.getValue() instanceof Integer) 923: return ((Integer) pane.getValue()).intValue(); 924: return -1; 925: } 926: 927: /** 928: * This method shows a confirmation dialog with the given message, title, 929: * optionType, messageType and icon. The frame owner will be the same as 930: * the one that holds the given parentComponent. This method returns the 931: * option selected by the user. 932: * 933: * @param parentComponent The component to find a frame in. 934: * @param message The message displayed. 935: * @param title The title of the dialog. 936: * @param optionType The optionType. 937: * @param messageType The messsageType. 938: * @param icon The icon displayed. 939: * 940: * @return The selected option. 941: */ 942: public static int showConfirmDialog(Component parentComponent, 943: Object message, String title, 944: int optionType, int messageType, 945: Icon icon) 946: { 947: JOptionPane pane = new JOptionPane(message, messageType, optionType, icon); 948: JDialog dialog = pane.createDialog(parentComponent, title); 949: dialog.pack(); 950: dialog.show(); 951: 952: if (pane.getValue() instanceof Integer) 953: return ((Integer) pane.getValue()).intValue(); 954: return -1; 955: } 956: 957: /** 958: * This method will show a QUESTION_MESSAGE input dialog with the given 959: * message. No selectionValues is set so the Look and Feel will usually 960: * give the user a TextField to fill out. The frame owner will be the same 961: * frame that holds the given parentComponent. This method will return the 962: * value entered by the user. 963: * 964: * @param parentComponent The component to find a frame in. 965: * @param message The message displayed. 966: * 967: * @return The value entered by the user. 968: */ 969: public static String showInputDialog(Component parentComponent, 970: Object message) 971: { 972: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 973: pane.setWantsInput(true); 974: JDialog dialog = pane.createDialog(parentComponent, null); 975: dialog.pack(); 976: dialog.show(); 977: 978: return (String) pane.getInputValue(); 979: } 980: 981: /** 982: * This method will show a QUESTION_MESSAGE type input dialog with the given 983: * message and initialSelectionValue. Since there is no selectionValues 984: * set, the Look and Feel will usually give a TextField to fill out. The 985: * frame owner will be the same as the one that holds the given 986: * parentComponent. This method will return the value entered by the user. 987: * 988: * @param parentComponent The component to find a frame in. 989: * @param message The message to display. 990: * @param initialSelectionValue The initially selected value. 991: * 992: * @return The value the user input. 993: */ 994: public static String showInputDialog(Component parentComponent, 995: Object message, 996: Object initialSelectionValue) 997: { 998: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 999: pane.setInitialSelectionValue(initialSelectionValue); 1000: pane.setWantsInput(true); 1001: JDialog dialog = pane.createDialog(parentComponent, null); 1002: dialog.pack(); 1003: dialog.show(); 1004: 1005: return (String) pane.getInputValue(); 1006: } 1007: 1008: /** 1009: * This method displays a new input dialog with the given message, title and 1010: * messageType. Since no selectionValues value is given, the Look and Feel 1011: * will usually give the user a TextField to input data to. This method 1012: * returns the value the user inputs. 1013: * 1014: * @param parentComponent The component to find a frame in. 1015: * @param message The message to display. 1016: * @param title The title of the dialog. 1017: * @param messageType The messageType. 1018: * 1019: * @return The value the user input. 1020: */ 1021: public static String showInputDialog(Component parentComponent, 1022: Object message, String title, 1023: int messageType) 1024: { 1025: JOptionPane pane = new JOptionPane(message, messageType); 1026: pane.setWantsInput(true); 1027: JDialog dialog = pane.createDialog(parentComponent, title); 1028: dialog.pack(); 1029: dialog.show(); 1030: 1031: return (String) pane.getInputValue(); 1032: } 1033: 1034: /** 1035: * This method shows an input dialog with the given message, title, 1036: * messageType, icon, selectionValues, and initialSelectionValue. This 1037: * method returns the value that the user selects. 1038: * 1039: * @param parentComponent The component to find a frame in. 1040: * @param message The message displayed. 1041: * @param title The title of the dialog. 1042: * @param messageType The messageType. 1043: * @param icon The icon displayed. 1044: * @param selectionValues The list of values to select from. 1045: * @param initialSelectionValue The initially selected value. 1046: * 1047: * @return The user selected value. 1048: */ 1049: public static Object showInputDialog(Component parentComponent, 1050: Object message, String title, 1051: int messageType, Icon icon, 1052: Object[] selectionValues, 1053: Object initialSelectionValue) 1054: { 1055: JOptionPane pane = new JOptionPane(message, messageType); 1056: pane.setWantsInput(true); 1057: pane.setIcon(icon); 1058: pane.setSelectionValues(selectionValues); 1059: pane.setInitialSelectionValue(initialSelectionValue); 1060: JDialog dialog = pane.createDialog(parentComponent, title); 1061: dialog.pack(); 1062: dialog.show(); 1063: 1064: return pane.getInputValue(); 1065: } 1066: 1067: /** 1068: * This method shows a QUESTION_MESSAGE type input dialog. Since no 1069: * selectionValues is set, the Look and Feel will usually give the user a 1070: * TextField to input data to. This method returns the value the user 1071: * inputs. 1072: * 1073: * @param message The message to display. 1074: * 1075: * @return The user selected value. 1076: */ 1077: public static String showInputDialog(Object message) 1078: { 1079: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 1080: pane.setWantsInput(true); 1081: JDialog dialog = pane.createDialog(null, null); 1082: dialog.pack(); 1083: dialog.show(); 1084: 1085: return (String) pane.getInputValue(); 1086: } 1087: 1088: /** 1089: * This method shows a QUESTION_MESSAGE type input dialog. Since no 1090: * selectionValues is set, the Look and Feel will usually give the user a 1091: * TextField to input data to. The input component will be initialized with 1092: * the initialSelectionValue. This method returns the value the user 1093: * inputs. 1094: * 1095: * @param message The message to display. 1096: * @param initialSelectionValue The initialSelectionValue. 1097: * 1098: * @return The user selected value. 1099: */ 1100: public static String showInputDialog(Object message, 1101: Object initialSelectionValue) 1102: { 1103: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 1104: pane.setWantsInput(true); 1105: pane.setInitialSelectionValue(initialSelectionValue); 1106: JDialog dialog = pane.createDialog(null, null); 1107: dialog.pack(); 1108: dialog.show(); 1109: 1110: return (String) pane.getInputValue(); 1111: } 1112: 1113: /** 1114: * This method shows an internal confirmation dialog with the given message. 1115: * The internal frame dialog will be placed in the first JDesktopPane 1116: * ancestor of the given parentComponent. This method will return the value 1117: * selected. 1118: * 1119: * @param parentComponent The parent to find a JDesktopPane in. 1120: * @param message The message to display. 1121: * 1122: * @return The value selected. 1123: */ 1124: public static int showInternalConfirmDialog(Component parentComponent, 1125: Object message) 1126: { 1127: JOptionPane pane = new JOptionPane(message); 1128: JInternalFrame frame = pane.createInternalFrame(parentComponent, null); 1129: 1130: startModal(frame); 1131: 1132: if (pane.getValue() instanceof Integer) 1133: return ((Integer) pane.getValue()).intValue(); 1134: return -1; 1135: } 1136: 1137: /** 1138: * This method shows an internal confirmation dialog with the given message, 1139: * optionType and title. The internal frame dialog will be placed in the 1140: * first JDesktopPane ancestor of the given parentComponent. This method 1141: * will return the selected value. 1142: * 1143: * @param parentComponent The parent to find a JDesktopPane in. 1144: * @param message The message to display. 1145: * @param title The title to display. 1146: * @param optionType The option type. 1147: * 1148: * @return The selected value. 1149: */ 1150: public static int showInternalConfirmDialog(Component parentComponent, 1151: Object message, String title, 1152: int optionType) 1153: { 1154: JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType); 1155: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1156: 1157: startModal(frame); 1158: 1159: if (pane.getValue() instanceof Integer) 1160: return ((Integer) pane.getValue()).intValue(); 1161: return -1; 1162: } 1163: 1164: /** 1165: * This method shows an internal confirmation dialog with the given message, 1166: * title, optionTypes and icon for the given message type. The internal 1167: * confirmation dialog will be placed in the first instance of 1168: * JDesktopPane ancestor of the given parentComponent. 1169: * 1170: * @param parentComponent The component to find a JDesktopPane in. 1171: * @param message The message to display. 1172: * @param title The title of the dialog. 1173: * @param optionType The option type. 1174: * @param messageType The message type. 1175: * 1176: * @return The selected value. 1177: */ 1178: public static int showInternalConfirmDialog(Component parentComponent, 1179: Object message, String title, 1180: int optionType, int messageType) 1181: { 1182: JOptionPane pane = new JOptionPane(message, messageType, optionType); 1183: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1184: 1185: startModal(frame); 1186: 1187: if (pane.getValue() instanceof Integer) 1188: return ((Integer) pane.getValue()).intValue(); 1189: return -1; 1190: } 1191: 1192: /** 1193: * This method shows an internal confirmation dialog with the given message, 1194: * title, option type, message type, and icon. The internal frame dialog 1195: * will be placed in the first JDesktopPane ancestor that is found in the 1196: * given parentComponent. This method returns the selected value. 1197: * 1198: * @param parentComponent The parent to find a JDesktopPane in. 1199: * @param message The message to display. 1200: * @param title The title to display. 1201: * @param optionType The option type. 1202: * @param messageType The message type. 1203: * @param icon The icon to display. 1204: * 1205: * @return The selected value. 1206: */ 1207: public static int showInternalConfirmDialog(Component parentComponent, 1208: Object message, String title, 1209: int optionType, int messageType, 1210: Icon icon) 1211: { 1212: JOptionPane pane = new JOptionPane(message, messageType, optionType, icon); 1213: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1214: 1215: startModal(frame); 1216: 1217: if (pane.getValue() instanceof Integer) 1218: return ((Integer) pane.getValue()).intValue(); 1219: return -1; 1220: } 1221: 1222: /** 1223: * This method shows an internal input dialog with the given message. The 1224: * internal frame dialog will be placed in the first JDesktopPane ancestor 1225: * of the given parent component. This method returns the value input by 1226: * the user. 1227: * 1228: * @param parentComponent The parent to find a JDesktopPane in. 1229: * @param message The message to display. 1230: * 1231: * @return The user selected value. 1232: */ 1233: public static String showInternalInputDialog(Component parentComponent, 1234: Object message) 1235: { 1236: JOptionPane pane = new JOptionPane(message); 1237: pane.setWantsInput(true); 1238: JInternalFrame frame = pane.createInternalFrame(parentComponent, null); 1239: 1240: startModal(frame); 1241: 1242: return (String) pane.getInputValue(); 1243: } 1244: 1245: /** 1246: * This method shows an internal input dialog with the given message, title 1247: * and message type. The internal input dialog will be placed in the first 1248: * JDesktopPane ancestor found in the given parent component. This method 1249: * will return the input value given by the user. 1250: * 1251: * @param parentComponent The component to find a JDesktopPane in. 1252: * @param message The message to display. 1253: * @param title The title to display. 1254: * @param messageType The message type. 1255: * 1256: * @return The user input value. 1257: */ 1258: public static String showInternalInputDialog(Component parentComponent, 1259: Object message, String title, 1260: int messageType) 1261: { 1262: JOptionPane pane = new JOptionPane(message, messageType); 1263: pane.setWantsInput(true); 1264: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1265: 1266: startModal(frame); 1267: 1268: return (String) pane.getInputValue(); 1269: } 1270: 1271: /** 1272: * This method shows an internal input dialog with the given message, title 1273: * message type, icon, selection value list and initial selection value. 1274: * The internal frame dialog will be placed in the first JDesktopPane 1275: * ancestor found in the given parent component. This method returns the 1276: * input value from the user. 1277: * 1278: * @param parentComponent The parent to find a JDesktopPane in. 1279: * @param message The message to display. 1280: * @param title The title to display. 1281: * @param messageType The message type. 1282: * @param icon The icon to display. 1283: * @param selectionValues The selection value list. 1284: * @param initialSelectionValue The initial selection value. 1285: * 1286: * @return The user input value. 1287: */ 1288: public static Object showInternalInputDialog(Component parentComponent, 1289: Object message, String title, 1290: int messageType, Icon icon, 1291: Object[] selectionValues, 1292: Object initialSelectionValue) 1293: { 1294: JOptionPane pane = new JOptionPane(message, messageType); 1295: pane.setWantsInput(true); 1296: pane.setIcon(icon); 1297: pane.setSelectionValues(selectionValues); 1298: pane.setInitialSelectionValue(initialSelectionValue); 1299: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1300: 1301: startModal(frame); 1302: 1303: return pane.getInputValue(); 1304: } 1305: 1306: /** 1307: * This method shows an internal message dialog with the given message. The 1308: * internal frame dialog will be placed in the first JDesktopPane ancestor 1309: * found in the given parent component. 1310: * 1311: * @param parentComponent The component to find a JDesktopPane in. 1312: * @param message The message to display. 1313: */ 1314: public static void showInternalMessageDialog(Component parentComponent, 1315: Object message) 1316: { 1317: JOptionPane pane = new JOptionPane(message); 1318: JInternalFrame frame = pane.createInternalFrame(parentComponent, null); 1319: 1320: startModal(frame); 1321: } 1322: 1323: /** 1324: * This method shows an internal message dialog with the given message, 1325: * title and message type. The internal message dialog is placed in the 1326: * first JDesktopPane ancestor found in the given parent component. 1327: * 1328: * @param parentComponent The parent component to find a JDesktopPane in. 1329: * @param message The message to display. 1330: * @param title The title to display. 1331: * @param messageType The message type. 1332: */ 1333: public static void showInternalMessageDialog(Component parentComponent, 1334: Object message, String title, 1335: int messageType) 1336: { 1337: JOptionPane pane = new JOptionPane(message, messageType); 1338: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1339: 1340: startModal(frame); 1341: } 1342: 1343: /** 1344: * This method shows an internal message dialog with the given message, 1345: * title, message type and icon. The internal message dialog is placed in 1346: * the first JDesktopPane ancestor found in the given parent component. 1347: * 1348: * @param parentComponent The component to find a JDesktopPane in. 1349: * @param message The message to display. 1350: * @param title The title to display. 1351: * @param messageType The message type. 1352: * @param icon The icon to display. 1353: */ 1354: public static void showInternalMessageDialog(Component parentComponent, 1355: Object message, String title, 1356: int messageType, Icon icon) 1357: { 1358: JOptionPane pane = new JOptionPane(message, messageType); 1359: pane.setIcon(icon); 1360: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1361: 1362: startModal(frame); 1363: } 1364: 1365: /** 1366: * This method displays an internal option dialog with the given message, 1367: * title, option type, message type, icon, option list, and initial option 1368: * value. The internal option dialog is placed in the first JDesktopPane 1369: * ancestor found in the parent component. This method returns the option 1370: * selected. 1371: * 1372: * @param parentComponent The parent to find a JDesktopPane in. 1373: * @param message The message displayed. 1374: * @param title The title displayed. 1375: * @param optionType The option type. 1376: * @param messageType The message type. 1377: * @param icon The icon to display. 1378: * @param options The array of options. 1379: * @param initialValue The initial value selected. 1380: * 1381: * @return The option that was selected. 1382: */ 1383: public static int showInternalOptionDialog(Component parentComponent, 1384: Object message, String title, 1385: int optionType, int messageType, 1386: Icon icon, Object[] options, 1387: Object initialValue) 1388: { 1389: JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, 1390: options, initialValue); 1391: 1392: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1393: 1394: startModal(frame); 1395: 1396: if (pane.getValue() instanceof Integer) 1397: return ((Integer) pane.getValue()).intValue(); 1398: return -1; 1399: } 1400: 1401: /** 1402: * This method shows an INFORMATION_MESSAGE type message dialog. 1403: * 1404: * @param parentComponent The component to find a frame in. 1405: * @param message The message displayed. 1406: */ 1407: public static void showMessageDialog(Component parentComponent, 1408: Object message) 1409: { 1410: JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE); 1411: JDialog dialog = pane.createDialog(parentComponent, null); 1412: dialog.pack(); 1413: dialog.show(); 1414: } 1415: 1416: /** 1417: * This method shows a message dialog with the given message, title and 1418: * messageType. 1419: * 1420: * @param parentComponent The component to find a frame in. 1421: * @param message The message displayed. 1422: * @param title The title of the dialog. 1423: * @param messageType The messageType. 1424: */ 1425: public static void showMessageDialog(Component parentComponent, 1426: Object message, String title, 1427: int messageType) 1428: { 1429: JOptionPane pane = new JOptionPane(message, messageType); 1430: JDialog dialog = pane.createDialog(parentComponent, title); 1431: dialog.pack(); 1432: dialog.show(); 1433: } 1434: 1435: /** 1436: * This method shows a message dialog with the given message, title, 1437: * messageType and icon. 1438: * 1439: * @param parentComponent The component to find a frame in. 1440: * @param message The message displayed. 1441: * @param title The title of the dialog. 1442: * @param messageType The messageType. 1443: * @param icon The icon displayed. 1444: */ 1445: public static void showMessageDialog(Component parentComponent, 1446: Object message, String title, 1447: int messageType, Icon icon) 1448: { 1449: JOptionPane pane = new JOptionPane(message, messageType); 1450: pane.setIcon(icon); 1451: JDialog dialog = pane.createDialog(parentComponent, title); 1452: dialog.pack(); 1453: dialog.show(); 1454: } 1455: 1456: /** 1457: * This method shows an option dialog with the given message, title, 1458: * optionType, messageType, icon, options and initialValue. This method 1459: * returns the option that was selected. 1460: * 1461: * @param parentComponent The component to find a frame in. 1462: * @param message The message displayed. 1463: * @param title The title of the dialog. 1464: * @param optionType The optionType. 1465: * @param messageType The messageType. 1466: * @param icon The icon displayed. 1467: * @param options The options to choose from. 1468: * @param initialValue The initial value. 1469: * 1470: * @return The selected option. 1471: */ 1472: public static int showOptionDialog(Component parentComponent, 1473: Object message, String title, 1474: int optionType, int messageType, 1475: Icon icon, Object[] options, 1476: Object initialValue) 1477: { 1478: JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, 1479: options, initialValue); 1480: 1481: JDialog dialog = pane.createDialog(parentComponent, title); 1482: dialog.pack(); 1483: dialog.show(); 1484: 1485: if (pane.getValue() instanceof Integer) 1486: return ((Integer) pane.getValue()).intValue(); 1487: return -1; 1488: } 1489: 1490: /** 1491: * This method resets the UI to the Look and Feel default. 1492: */ 1493: public void updateUI() 1494: { 1495: setUI((OptionPaneUI) UIManager.getUI(this)); 1496: invalidate(); 1497: } 1498: 1499: /** 1500: * This method returns true if the key is a valid messageType. 1501: * 1502: * @param key The key to check. 1503: * 1504: * @return True if key is valid. 1505: */ 1506: private boolean validMessageType(int key) 1507: { 1508: switch (key) 1509: { 1510: case ERROR_MESSAGE: 1511: case INFORMATION_MESSAGE: 1512: case PLAIN_MESSAGE: 1513: case QUESTION_MESSAGE: 1514: case WARNING_MESSAGE: 1515: return true; 1516: } 1517: return false; 1518: } 1519: 1520: /** 1521: * This method returns true if the key is a valid optionType. 1522: * 1523: * @param key The key to check. 1524: * 1525: * @return True if key is valid. 1526: */ 1527: private boolean validOptionType(int key) 1528: { 1529: switch (key) 1530: { 1531: case DEFAULT_OPTION: 1532: case OK_CANCEL_OPTION: 1533: case YES_NO_CANCEL_OPTION: 1534: case YES_NO_OPTION: 1535: return true; 1536: } 1537: return false; 1538: } 1539: 1540: /** 1541: * This helper method makes the JInternalFrame wait until it is notified by 1542: * an InternalFrameClosing event. This method also adds the given 1543: * JOptionPane to the JInternalFrame and sizes it according to the 1544: * JInternalFrame's preferred size. 1545: * 1546: * @param f The JInternalFrame to make modal. 1547: */ 1548: private static void startModal(JInternalFrame f) 1549: { 1550: synchronized (f) 1551: { 1552: final JInternalFrame tmp = f; 1553: tmp.toFront(); 1554: 1555: f.addInternalFrameListener(new InternalFrameAdapter() 1556: { 1557: public void internalFrameClosed(InternalFrameEvent e) 1558: { 1559: synchronized (tmp) 1560: { 1561: tmp.removeInternalFrameListener(this); 1562: tmp.notifyAll(); 1563: } 1564: } 1565: }); 1566: try 1567: { 1568: while (! f.isClosed()) 1569: f.wait(); 1570: } 1571: catch (InterruptedException ignored) 1572: { 1573: // Ignore this Exception. 1574: } 1575: } 1576: } 1577: }
GNU Classpath (0.19) |