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