GNU Classpath (0.20) | |
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: dialog.getContentPane().add(this); 373: dialog.setModal(true); 374: dialog.setResizable(false); 375: dialog.pack(); 376: dialog.setLocationRelativeTo(parentComponent); 377: 378: return dialog; 379: } 380: 381: /** 382: * This method creates a new JInternalFrame that is in the JLayeredPane 383: * which contains the parentComponent given. If no suitable JLayeredPane 384: * can be found from the parentComponent given, a RuntimeException will be 385: * thrown. 386: * 387: * @param parentComponent The parent to find a JDesktopPane from. 388: * @param title The title of the JInternalFrame. 389: * 390: * @return A new JInternalFrame based on the JOptionPane configuration. 391: * 392: * @throws RuntimeException If no suitable JDesktopPane is found. 393: * 394: * @specnote The specification says that the internal frame is placed 395: * in the nearest <code>JDesktopPane</code> that is found in 396: * <code>parent</code>'s ancestors. The behaviour of the JDK 397: * is that it actually looks up the nearest 398: * <code>JLayeredPane</code> in <code>parent</code>'s ancestors. 399: * So do we. 400: */ 401: public JInternalFrame createInternalFrame(Component parentComponent, 402: String title) 403: throws RuntimeException 404: { 405: // Try to find a JDesktopPane. 406: JLayeredPane toUse = getDesktopPaneForComponent(parentComponent); 407: // If we don't have a JDesktopPane, we try to find a JLayeredPane. 408: if (toUse == null) 409: toUse = JLayeredPane.getLayeredPaneAbove(parentComponent); 410: // If this still fails, we throw a RuntimeException. 411: if (toUse == null) 412: throw new RuntimeException 413: ("parentComponent does not have a valid parent"); 414: 415: JInternalFrame frame = new JInternalFrame(title); 416: 417: inputValue = UNINITIALIZED_VALUE; 418: value = UNINITIALIZED_VALUE; 419: 420: frame.setContentPane(this); 421: frame.setClosable(true); 422: 423: toUse.add(frame); 424: frame.setLayer(JLayeredPane.MODAL_LAYER); 425: 426: frame.pack(); 427: frame.setVisible(true); 428: 429: return frame; 430: } 431: 432: /** 433: * DOCUMENT ME! 434: * 435: * @return DOCUMENT ME! 436: */ 437: public AccessibleContext getAccessibleContext() 438: { 439: if (accessibleContext == null) 440: accessibleContext = new AccessibleJOptionPane(); 441: return accessibleContext; 442: } 443: 444: /** 445: * This method returns the JDesktopPane for the given parentComponent or 446: * null if none can be found. 447: * 448: * @param parentComponent The component to look in. 449: * 450: * @return The JDesktopPane for the given component or null if none can be 451: * found. 452: */ 453: public static JDesktopPane getDesktopPaneForComponent(Component parentComponent) 454: { 455: return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class, 456: parentComponent); 457: } 458: 459: /** 460: * This method returns the Frame for the given parentComponent or null if 461: * none can be found. 462: * 463: * @param parentComponent The component to look in. 464: * 465: * @return The Frame for the given component or null if none can be found. 466: */ 467: public static Frame getFrameForComponent(Component parentComponent) 468: { 469: return (Frame) SwingUtilities.getAncestorOfClass(Frame.class, 470: parentComponent); 471: } 472: 473: /** 474: * This method returns the icon displayed. 475: * 476: * @return The icon displayed. 477: */ 478: public Icon getIcon() 479: { 480: return icon; 481: } 482: 483: /** 484: * This method returns the value initially selected from the list of values 485: * the user can input. 486: * 487: * @return The initial selection value. 488: */ 489: public Object getInitialSelectionValue() 490: { 491: return initialSelectionValue; 492: } 493: 494: /** 495: * This method returns the value that is focused from the list of options. 496: * 497: * @return The initial value from options. 498: */ 499: public Object getInitialValue() 500: { 501: return initialValue; 502: } 503: 504: /** 505: * This method returns the value that the user input. 506: * 507: * @return The user's input value. 508: */ 509: public Object getInputValue() 510: { 511: if (getValue().equals(new Integer(CANCEL_OPTION))) 512: setInputValue(null); 513: return inputValue; 514: } 515: 516: /** 517: * This method returns the maximum characters per line. By default, this is 518: * Integer.MAX_VALUE. 519: * 520: * @return The maximum characters per line. 521: */ 522: public int getMaxCharactersPerLineCount() 523: { 524: return Integer.MAX_VALUE; 525: } 526: 527: /** 528: * This method returns the message displayed. 529: * 530: * @return The message displayed. 531: */ 532: public Object getMessage() 533: { 534: return message; 535: } 536: 537: /** 538: * This method returns the message type. 539: * 540: * @return The message type. 541: */ 542: public int getMessageType() 543: { 544: return messageType; 545: } 546: 547: /** 548: * This method returns the options. 549: * 550: * @return The options. 551: */ 552: public Object[] getOptions() 553: { 554: return options; 555: } 556: 557: /** 558: * This method returns the option type. 559: * 560: * @return The option type. 561: */ 562: public int getOptionType() 563: { 564: return optionType; 565: } 566: 567: /** 568: * This method returns the Frame used by JOptionPane dialog's that have no 569: * parent. 570: * 571: * @return The Frame used by dialogs that have no parent. 572: */ 573: public static Frame getRootFrame() 574: { 575: return privFrame; 576: } 577: 578: /** 579: * This method returns the selection values. 580: * 581: * @return The selection values. 582: */ 583: public Object[] getSelectionValues() 584: { 585: return selectionValues; 586: } 587: 588: /** 589: * This method returns the UI used by the JOptionPane. 590: * 591: * @return The UI used by the JOptionPane. 592: */ 593: public OptionPaneUI getUI() 594: { 595: return (OptionPaneUI) ui; 596: } 597: 598: /** 599: * This method returns an identifier to determine which UI class will act as 600: * the UI. 601: * 602: * @return The UI identifier. 603: */ 604: public String getUIClassID() 605: { 606: return "OptionPaneUI"; 607: } 608: 609: /** 610: * This method returns the value that the user selected out of options. 611: * 612: * @return The value that the user selected out of options. 613: */ 614: public Object getValue() 615: { 616: return value; 617: } 618: 619: /** 620: * This method returns whether this JOptionPane wants input. 621: * 622: * @return Whether this JOptionPane wants input. 623: */ 624: public boolean getWantsInput() 625: { 626: return wantsInput; 627: } 628: 629: /** 630: * This method returns a String that describes this JOptionPane. 631: * 632: * @return A String that describes this JOptionPane. 633: */ 634: protected String paramString() 635: { 636: return "JOptionPane"; 637: } 638: 639: /** 640: * This method requests focus for the initial value. 641: */ 642: public void selectInitialValue() 643: { 644: if (ui != null) 645: ((OptionPaneUI) ui).selectInitialValue(this); 646: } 647: 648: /** 649: * This method changes the icon property. 650: * 651: * @param newIcon The new icon to use. 652: */ 653: public void setIcon(Icon newIcon) 654: { 655: if (icon != newIcon) 656: { 657: Icon old = icon; 658: icon = newIcon; 659: firePropertyChange(ICON_PROPERTY, old, icon); 660: } 661: } 662: 663: /** 664: * This method changes the initial selection property. 665: * 666: * @param newValue The new initial selection. 667: */ 668: public void setInitialSelectionValue(Object newValue) 669: { 670: if (initialSelectionValue != newValue) 671: { 672: Object old = initialSelectionValue; 673: initialSelectionValue = newValue; 674: firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old, 675: initialSelectionValue); 676: } 677: } 678: 679: /** 680: * This method changes the initial value property. 681: * 682: * @param newValue The new initial value. 683: */ 684: public void setInitialValue(Object newValue) 685: { 686: if (initialValue != newValue) 687: { 688: Object old = initialValue; 689: initialValue = newValue; 690: firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue); 691: } 692: } 693: 694: /** 695: * This method changes the inputValue property. 696: * 697: * @param newValue The new inputValue. 698: */ 699: public void setInputValue(Object newValue) 700: { 701: if (inputValue != newValue) 702: { 703: Object old = inputValue; 704: inputValue = newValue; 705: firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue); 706: } 707: } 708: 709: /** 710: * This method changes the message property. 711: * 712: * @param newMessage The new message. 713: */ 714: public void setMessage(Object newMessage) 715: { 716: if (message != newMessage) 717: { 718: Object old = message; 719: message = newMessage; 720: firePropertyChange(MESSAGE_PROPERTY, old, message); 721: } 722: } 723: 724: /** 725: * This method changes the messageType property. 726: * 727: * @param newType The new messageType. 728: * 729: * @throws IllegalArgumentException If the messageType is not valid. 730: */ 731: public void setMessageType(int newType) 732: { 733: if (! validMessageType(newType)) 734: throw new IllegalArgumentException("Message Type not legal value."); 735: if (newType != messageType) 736: { 737: int old = messageType; 738: messageType = newType; 739: firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType); 740: } 741: } 742: 743: /** 744: * This method changes the options property. 745: * 746: * @param newOptions The new options. 747: */ 748: public void setOptions(Object[] newOptions) 749: { 750: if (options != newOptions) 751: { 752: Object[] old = options; 753: options = newOptions; 754: firePropertyChange(OPTIONS_PROPERTY, old, options); 755: } 756: } 757: 758: /** 759: * This method changes the optionType property. 760: * 761: * @param newType The new optionType. 762: * 763: * @throws IllegalArgumentException If the optionType is not valid. 764: */ 765: public void setOptionType(int newType) 766: { 767: if (! validOptionType(newType)) 768: throw new IllegalArgumentException("Option Type not legal value."); 769: if (newType != optionType) 770: { 771: int old = optionType; 772: optionType = newType; 773: firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType); 774: } 775: } 776: 777: /** 778: * This method changes the Frame used for JOptionPane dialogs that have no 779: * parent. 780: * 781: * @param newRootFrame The Frame to use for dialogs that have no parent. 782: */ 783: public static void setRootFrame(Frame newRootFrame) 784: { 785: privFrame = newRootFrame; 786: } 787: 788: /** 789: * This method changes the selectionValues property. 790: * 791: * @param newValues The new selectionValues. 792: */ 793: public void setSelectionValues(Object[] newValues) 794: { 795: if (newValues != selectionValues) 796: { 797: if (newValues != null) 798: wantsInput = true; 799: Object[] old = selectionValues; 800: selectionValues = newValues; 801: firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues); 802: } 803: } 804: 805: /** 806: * This method sets the UI used with the JOptionPane. 807: * 808: * @param ui The UI used with the JOptionPane. 809: */ 810: public void setUI(OptionPaneUI ui) 811: { 812: super.setUI(ui); 813: } 814: 815: /** 816: * This method sets the value has been selected out of options. 817: * 818: * @param newValue The value that has been selected out of options. 819: */ 820: public void setValue(Object newValue) 821: { 822: if (value != newValue) 823: { 824: Object old = value; 825: value = newValue; 826: firePropertyChange(VALUE_PROPERTY, old, value); 827: } 828: } 829: 830: /** 831: * This method changes the wantsInput property. 832: * 833: * @param newValue Whether this JOptionPane requires input. 834: */ 835: public void setWantsInput(boolean newValue) 836: { 837: if (wantsInput != newValue) 838: { 839: boolean old = wantsInput; 840: wantsInput = newValue; 841: firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput); 842: } 843: } 844: 845: /** 846: * This method shows a confirmation dialog with the title "Select an Option" 847: * and displays the given message. The parent frame will be the same as the 848: * parent frame of the given parentComponent. This method returns the 849: * option chosen by the user. 850: * 851: * @param parentComponent The parentComponent to find a frame in. 852: * @param message The message to display. 853: * 854: * @return The option that was selected. 855: */ 856: public static int showConfirmDialog(Component parentComponent, Object message) 857: { 858: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 859: JDialog dialog = pane.createDialog(parentComponent, "Select an Option"); 860: dialog.show(); 861: 862: if (pane.getValue() instanceof Integer) 863: return ((Integer) pane.getValue()).intValue(); 864: return -1; 865: } 866: 867: /** 868: * This method shows a confirmation dialog with the given message, 869: * optionType and title. The frame that owns the dialog will be the same 870: * frame that holds the given parentComponent. This method returns the 871: * option that was chosen. 872: * 873: * @param parentComponent The component to find a frame in. 874: * @param message The message displayed. 875: * @param title The title of the dialog. 876: * @param optionType The optionType. 877: * 878: * @return The option that was chosen. 879: */ 880: public static int showConfirmDialog(Component parentComponent, 881: Object message, String title, 882: int optionType) 883: { 884: JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType); 885: JDialog dialog = pane.createDialog(parentComponent, title); 886: dialog.show(); 887: 888: if (pane.getValue() instanceof Integer) 889: return ((Integer) pane.getValue()).intValue(); 890: return -1; 891: } 892: 893: /** 894: * This method shows a confirmation dialog with the given message, title, 895: * messageType and optionType. The frame owner will be the same frame as 896: * the one that holds the given parentComponent. This method returns the 897: * option selected by the user. 898: * 899: * @param parentComponent The component to find a frame in. 900: * @param message The message displayed. 901: * @param title The title of the dialog. 902: * @param optionType The optionType. 903: * @param messageType The messageType. 904: * 905: * @return The selected option. 906: */ 907: public static int showConfirmDialog(Component parentComponent, 908: Object message, String title, 909: int optionType, int messageType) 910: { 911: JOptionPane pane = new JOptionPane(message, messageType, optionType); 912: JDialog dialog = pane.createDialog(parentComponent, title); 913: dialog.show(); 914: 915: if (pane.getValue() instanceof Integer) 916: return ((Integer) pane.getValue()).intValue(); 917: return -1; 918: } 919: 920: /** 921: * This method shows a confirmation dialog with the given message, title, 922: * optionType, messageType and icon. The frame owner will be the same as 923: * the one that holds the given parentComponent. This method returns the 924: * option selected by the user. 925: * 926: * @param parentComponent The component to find a frame in. 927: * @param message The message displayed. 928: * @param title The title of the dialog. 929: * @param optionType The optionType. 930: * @param messageType The messsageType. 931: * @param icon The icon displayed. 932: * 933: * @return The selected option. 934: */ 935: public static int showConfirmDialog(Component parentComponent, 936: Object message, String title, 937: int optionType, int messageType, 938: Icon icon) 939: { 940: JOptionPane pane = new JOptionPane(message, messageType, optionType, icon); 941: JDialog dialog = pane.createDialog(parentComponent, title); 942: dialog.show(); 943: 944: if (pane.getValue() instanceof Integer) 945: return ((Integer) pane.getValue()).intValue(); 946: return -1; 947: } 948: 949: /** 950: * This method will show a QUESTION_MESSAGE input dialog with the given 951: * message. No selectionValues is set so the Look and Feel will usually 952: * give the user a TextField to fill out. The frame owner will be the same 953: * frame that holds the given parentComponent. This method will return the 954: * value entered by the user. 955: * 956: * @param parentComponent The component to find a frame in. 957: * @param message The message displayed. 958: * 959: * @return The value entered by the user. 960: */ 961: public static String showInputDialog(Component parentComponent, 962: Object message) 963: { 964: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 965: pane.setWantsInput(true); 966: JDialog dialog = pane.createDialog(parentComponent, null); 967: dialog.show(); 968: 969: return (String) pane.getInputValue(); 970: } 971: 972: /** 973: * This method will show a QUESTION_MESSAGE type input dialog with the given 974: * message and initialSelectionValue. Since there is no selectionValues 975: * set, the Look and Feel will usually give a TextField to fill out. The 976: * frame owner will be the same as the one that holds the given 977: * parentComponent. This method will return the value entered by the user. 978: * 979: * @param parentComponent The component to find a frame in. 980: * @param message The message to display. 981: * @param initialSelectionValue The initially selected value. 982: * 983: * @return The value the user input. 984: */ 985: public static String showInputDialog(Component parentComponent, 986: Object message, 987: Object initialSelectionValue) 988: { 989: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 990: pane.setInitialSelectionValue(initialSelectionValue); 991: pane.setWantsInput(true); 992: JDialog dialog = pane.createDialog(parentComponent, null); 993: dialog.show(); 994: 995: return (String) pane.getInputValue(); 996: } 997: 998: /** 999: * This method displays a new input dialog with the given message, title and 1000: * messageType. Since no selectionValues value is given, the Look and Feel 1001: * will usually give the user a TextField to input data to. This method 1002: * returns the value the user inputs. 1003: * 1004: * @param parentComponent The component to find a frame in. 1005: * @param message The message to display. 1006: * @param title The title of the dialog. 1007: * @param messageType The messageType. 1008: * 1009: * @return The value the user input. 1010: */ 1011: public static String showInputDialog(Component parentComponent, 1012: Object message, String title, 1013: int messageType) 1014: { 1015: JOptionPane pane = new JOptionPane(message, messageType); 1016: pane.setWantsInput(true); 1017: JDialog dialog = pane.createDialog(parentComponent, title); 1018: dialog.show(); 1019: 1020: return (String) pane.getInputValue(); 1021: } 1022: 1023: /** 1024: * This method shows an input dialog with the given message, title, 1025: * messageType, icon, selectionValues, and initialSelectionValue. This 1026: * method returns the value that the user selects. 1027: * 1028: * @param parentComponent The component to find a frame in. 1029: * @param message The message displayed. 1030: * @param title The title of the dialog. 1031: * @param messageType The messageType. 1032: * @param icon The icon displayed. 1033: * @param selectionValues The list of values to select from. 1034: * @param initialSelectionValue The initially selected value. 1035: * 1036: * @return The user selected value. 1037: */ 1038: public static Object showInputDialog(Component parentComponent, 1039: Object message, String title, 1040: int messageType, Icon icon, 1041: Object[] selectionValues, 1042: Object initialSelectionValue) 1043: { 1044: JOptionPane pane = new JOptionPane(message, messageType); 1045: pane.setWantsInput(true); 1046: pane.setIcon(icon); 1047: pane.setSelectionValues(selectionValues); 1048: pane.setInitialSelectionValue(initialSelectionValue); 1049: JDialog dialog = pane.createDialog(parentComponent, title); 1050: dialog.show(); 1051: 1052: return pane.getInputValue(); 1053: } 1054: 1055: /** 1056: * This method shows a QUESTION_MESSAGE type input dialog. Since no 1057: * selectionValues is set, the Look and Feel will usually give the user a 1058: * TextField to input data to. This method returns the value the user 1059: * inputs. 1060: * 1061: * @param message The message to display. 1062: * 1063: * @return The user selected value. 1064: */ 1065: public static String showInputDialog(Object message) 1066: { 1067: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 1068: pane.setWantsInput(true); 1069: JDialog dialog = pane.createDialog(null, null); 1070: dialog.show(); 1071: 1072: return (String) pane.getInputValue(); 1073: } 1074: 1075: /** 1076: * This method shows a QUESTION_MESSAGE type input dialog. Since no 1077: * selectionValues is set, the Look and Feel will usually give the user a 1078: * TextField to input data to. The input component will be initialized with 1079: * the initialSelectionValue. This method returns the value the user 1080: * inputs. 1081: * 1082: * @param message The message to display. 1083: * @param initialSelectionValue The initialSelectionValue. 1084: * 1085: * @return The user selected value. 1086: */ 1087: public static String showInputDialog(Object message, 1088: Object initialSelectionValue) 1089: { 1090: JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE); 1091: pane.setWantsInput(true); 1092: pane.setInitialSelectionValue(initialSelectionValue); 1093: JDialog dialog = pane.createDialog(null, null); 1094: dialog.show(); 1095: 1096: return (String) pane.getInputValue(); 1097: } 1098: 1099: /** 1100: * This method shows an internal confirmation dialog with the given message. 1101: * The internal frame dialog will be placed in the first JDesktopPane 1102: * ancestor of the given parentComponent. This method will return the value 1103: * selected. 1104: * 1105: * @param parentComponent The parent to find a JDesktopPane in. 1106: * @param message The message to display. 1107: * 1108: * @return The value selected. 1109: */ 1110: public static int showInternalConfirmDialog(Component parentComponent, 1111: Object message) 1112: { 1113: JOptionPane pane = new JOptionPane(message); 1114: JInternalFrame frame = pane.createInternalFrame(parentComponent, null); 1115: 1116: startModal(frame); 1117: 1118: if (pane.getValue() instanceof Integer) 1119: return ((Integer) pane.getValue()).intValue(); 1120: return -1; 1121: } 1122: 1123: /** 1124: * This method shows an internal confirmation dialog with the given message, 1125: * optionType and title. The internal frame dialog will be placed in the 1126: * first JDesktopPane ancestor of the given parentComponent. This method 1127: * will return the selected value. 1128: * 1129: * @param parentComponent The parent to find a JDesktopPane in. 1130: * @param message The message to display. 1131: * @param title The title to display. 1132: * @param optionType The option type. 1133: * 1134: * @return The selected value. 1135: */ 1136: public static int showInternalConfirmDialog(Component parentComponent, 1137: Object message, String title, 1138: int optionType) 1139: { 1140: JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType); 1141: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1142: 1143: startModal(frame); 1144: 1145: if (pane.getValue() instanceof Integer) 1146: return ((Integer) pane.getValue()).intValue(); 1147: return -1; 1148: } 1149: 1150: /** 1151: * This method shows an internal confirmation dialog with the given message, 1152: * title, optionTypes and icon for the given message type. The internal 1153: * confirmation dialog will be placed in the first instance of 1154: * JDesktopPane ancestor of the given parentComponent. 1155: * 1156: * @param parentComponent The component to find a JDesktopPane in. 1157: * @param message The message to display. 1158: * @param title The title of the dialog. 1159: * @param optionType The option type. 1160: * @param messageType The message type. 1161: * 1162: * @return The selected value. 1163: */ 1164: public static int showInternalConfirmDialog(Component parentComponent, 1165: Object message, String title, 1166: int optionType, int messageType) 1167: { 1168: JOptionPane pane = new JOptionPane(message, messageType, optionType); 1169: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1170: 1171: startModal(frame); 1172: 1173: if (pane.getValue() instanceof Integer) 1174: return ((Integer) pane.getValue()).intValue(); 1175: return -1; 1176: } 1177: 1178: /** 1179: * This method shows an internal confirmation dialog with the given message, 1180: * title, option type, message type, and icon. The internal frame dialog 1181: * will be placed in the first JDesktopPane ancestor that is found in the 1182: * given parentComponent. This method returns the selected value. 1183: * 1184: * @param parentComponent The parent to find a JDesktopPane in. 1185: * @param message The message to display. 1186: * @param title The title to display. 1187: * @param optionType The option type. 1188: * @param messageType The message type. 1189: * @param icon The icon to display. 1190: * 1191: * @return The selected value. 1192: */ 1193: public static int showInternalConfirmDialog(Component parentComponent, 1194: Object message, String title, 1195: int optionType, int messageType, 1196: Icon icon) 1197: { 1198: JOptionPane pane = new JOptionPane(message, messageType, optionType, icon); 1199: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1200: 1201: startModal(frame); 1202: 1203: if (pane.getValue() instanceof Integer) 1204: return ((Integer) pane.getValue()).intValue(); 1205: return -1; 1206: } 1207: 1208: /** 1209: * This method shows an internal input dialog with the given message. The 1210: * internal frame dialog will be placed in the first JDesktopPane ancestor 1211: * of the given parent component. This method returns the value input by 1212: * the user. 1213: * 1214: * @param parentComponent The parent to find a JDesktopPane in. 1215: * @param message The message to display. 1216: * 1217: * @return The user selected value. 1218: */ 1219: public static String showInternalInputDialog(Component parentComponent, 1220: Object message) 1221: { 1222: JOptionPane pane = new JOptionPane(message); 1223: pane.setWantsInput(true); 1224: JInternalFrame frame = pane.createInternalFrame(parentComponent, null); 1225: 1226: startModal(frame); 1227: 1228: return (String) pane.getInputValue(); 1229: } 1230: 1231: /** 1232: * This method shows an internal input dialog with the given message, title 1233: * and message type. The internal input dialog will be placed in the first 1234: * JDesktopPane ancestor found in the given parent component. This method 1235: * will return the input value given by the user. 1236: * 1237: * @param parentComponent The component to find a JDesktopPane in. 1238: * @param message The message to display. 1239: * @param title The title to display. 1240: * @param messageType The message type. 1241: * 1242: * @return The user input value. 1243: */ 1244: public static String showInternalInputDialog(Component parentComponent, 1245: Object message, String title, 1246: int messageType) 1247: { 1248: JOptionPane pane = new JOptionPane(message, messageType); 1249: pane.setWantsInput(true); 1250: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1251: 1252: startModal(frame); 1253: 1254: return (String) pane.getInputValue(); 1255: } 1256: 1257: /** 1258: * This method shows an internal input dialog with the given message, title 1259: * message type, icon, selection value list and initial selection value. 1260: * The internal frame dialog will be placed in the first JDesktopPane 1261: * ancestor found in the given parent component. This method returns the 1262: * input value from the user. 1263: * 1264: * @param parentComponent The parent to find a JDesktopPane in. 1265: * @param message The message to display. 1266: * @param title The title to display. 1267: * @param messageType The message type. 1268: * @param icon The icon to display. 1269: * @param selectionValues The selection value list. 1270: * @param initialSelectionValue The initial selection value. 1271: * 1272: * @return The user input value. 1273: */ 1274: public static Object showInternalInputDialog(Component parentComponent, 1275: Object message, String title, 1276: int messageType, Icon icon, 1277: Object[] selectionValues, 1278: Object initialSelectionValue) 1279: { 1280: JOptionPane pane = new JOptionPane(message, messageType); 1281: pane.setWantsInput(true); 1282: pane.setIcon(icon); 1283: pane.setSelectionValues(selectionValues); 1284: pane.setInitialSelectionValue(initialSelectionValue); 1285: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1286: 1287: startModal(frame); 1288: 1289: return pane.getInputValue(); 1290: } 1291: 1292: /** 1293: * This method shows an internal message dialog with the given message. The 1294: * internal frame dialog will be placed in the first JDesktopPane ancestor 1295: * found in the given parent component. 1296: * 1297: * @param parentComponent The component to find a JDesktopPane in. 1298: * @param message The message to display. 1299: */ 1300: public static void showInternalMessageDialog(Component parentComponent, 1301: Object message) 1302: { 1303: JOptionPane pane = new JOptionPane(message); 1304: JInternalFrame frame = pane.createInternalFrame(parentComponent, null); 1305: 1306: startModal(frame); 1307: } 1308: 1309: /** 1310: * This method shows an internal message dialog with the given message, 1311: * title and message type. The internal message dialog is placed in the 1312: * first JDesktopPane ancestor found in the given parent component. 1313: * 1314: * @param parentComponent The parent component to find a JDesktopPane in. 1315: * @param message The message to display. 1316: * @param title The title to display. 1317: * @param messageType The message type. 1318: */ 1319: public static void showInternalMessageDialog(Component parentComponent, 1320: Object message, String title, 1321: int messageType) 1322: { 1323: JOptionPane pane = new JOptionPane(message, messageType); 1324: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1325: 1326: startModal(frame); 1327: } 1328: 1329: /** 1330: * This method shows an internal message dialog with the given message, 1331: * title, message type and icon. The internal message dialog is placed in 1332: * the first JDesktopPane ancestor found in the given parent component. 1333: * 1334: * @param parentComponent The component to find a JDesktopPane in. 1335: * @param message The message to display. 1336: * @param title The title to display. 1337: * @param messageType The message type. 1338: * @param icon The icon to display. 1339: */ 1340: public static void showInternalMessageDialog(Component parentComponent, 1341: Object message, String title, 1342: int messageType, Icon icon) 1343: { 1344: JOptionPane pane = new JOptionPane(message, messageType); 1345: pane.setIcon(icon); 1346: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1347: 1348: startModal(frame); 1349: } 1350: 1351: /** 1352: * This method displays an internal option dialog with the given message, 1353: * title, option type, message type, icon, option list, and initial option 1354: * value. The internal option dialog is placed in the first JDesktopPane 1355: * ancestor found in the parent component. This method returns the option 1356: * selected. 1357: * 1358: * @param parentComponent The parent to find a JDesktopPane in. 1359: * @param message The message displayed. 1360: * @param title The title displayed. 1361: * @param optionType The option type. 1362: * @param messageType The message type. 1363: * @param icon The icon to display. 1364: * @param options The array of options. 1365: * @param initialValue The initial value selected. 1366: * 1367: * @return The option that was selected. 1368: */ 1369: public static int showInternalOptionDialog(Component parentComponent, 1370: Object message, String title, 1371: int optionType, int messageType, 1372: Icon icon, Object[] options, 1373: Object initialValue) 1374: { 1375: JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, 1376: options, initialValue); 1377: 1378: JInternalFrame frame = pane.createInternalFrame(parentComponent, title); 1379: 1380: startModal(frame); 1381: 1382: if (pane.getValue() instanceof Integer) 1383: return ((Integer) pane.getValue()).intValue(); 1384: return -1; 1385: } 1386: 1387: /** 1388: * This method shows an INFORMATION_MESSAGE type message dialog. 1389: * 1390: * @param parentComponent The component to find a frame in. 1391: * @param message The message displayed. 1392: */ 1393: public static void showMessageDialog(Component parentComponent, 1394: Object message) 1395: { 1396: JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE); 1397: JDialog dialog = pane.createDialog(parentComponent, null); 1398: dialog.show(); 1399: } 1400: 1401: /** 1402: * This method shows a message dialog with the given message, title and 1403: * messageType. 1404: * 1405: * @param parentComponent The component to find a frame in. 1406: * @param message The message displayed. 1407: * @param title The title of the dialog. 1408: * @param messageType The messageType. 1409: */ 1410: public static void showMessageDialog(Component parentComponent, 1411: Object message, String title, 1412: int messageType) 1413: { 1414: JOptionPane pane = new JOptionPane(message, messageType); 1415: JDialog dialog = pane.createDialog(parentComponent, title); 1416: dialog.show(); 1417: } 1418: 1419: /** 1420: * This method shows a message dialog with the given message, title, 1421: * messageType and icon. 1422: * 1423: * @param parentComponent The component to find a frame in. 1424: * @param message The message displayed. 1425: * @param title The title of the dialog. 1426: * @param messageType The messageType. 1427: * @param icon The icon displayed. 1428: */ 1429: public static void showMessageDialog(Component parentComponent, 1430: Object message, String title, 1431: int messageType, Icon icon) 1432: { 1433: JOptionPane pane = new JOptionPane(message, messageType); 1434: pane.setIcon(icon); 1435: JDialog dialog = pane.createDialog(parentComponent, title); 1436: dialog.show(); 1437: } 1438: 1439: /** 1440: * This method shows an option dialog with the given message, title, 1441: * optionType, messageType, icon, options and initialValue. This method 1442: * returns the option that was selected. 1443: * 1444: * @param parentComponent The component to find a frame in. 1445: * @param message The message displayed. 1446: * @param title The title of the dialog. 1447: * @param optionType The optionType. 1448: * @param messageType The messageType. 1449: * @param icon The icon displayed. 1450: * @param options The options to choose from. 1451: * @param initialValue The initial value. 1452: * 1453: * @return The selected option. 1454: */ 1455: public static int showOptionDialog(Component parentComponent, 1456: Object message, String title, 1457: int optionType, int messageType, 1458: Icon icon, Object[] options, 1459: Object initialValue) 1460: { 1461: JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, 1462: options, initialValue); 1463: 1464: JDialog dialog = pane.createDialog(parentComponent, title); 1465: dialog.show(); 1466: 1467: if (pane.getValue() instanceof Integer) 1468: return ((Integer) pane.getValue()).intValue(); 1469: return -1; 1470: } 1471: 1472: /** 1473: * This method resets the UI to the Look and Feel default. 1474: */ 1475: public void updateUI() 1476: { 1477: setUI((OptionPaneUI) UIManager.getUI(this)); 1478: invalidate(); 1479: } 1480: 1481: /** 1482: * This method returns true if the key is a valid messageType. 1483: * 1484: * @param key The key to check. 1485: * 1486: * @return True if key is valid. 1487: */ 1488: private boolean validMessageType(int key) 1489: { 1490: switch (key) 1491: { 1492: case ERROR_MESSAGE: 1493: case INFORMATION_MESSAGE: 1494: case PLAIN_MESSAGE: 1495: case QUESTION_MESSAGE: 1496: case WARNING_MESSAGE: 1497: return true; 1498: } 1499: return false; 1500: } 1501: 1502: /** 1503: * This method returns true if the key is a valid optionType. 1504: * 1505: * @param key The key to check. 1506: * 1507: * @return True if key is valid. 1508: */ 1509: private boolean validOptionType(int key) 1510: { 1511: switch (key) 1512: { 1513: case DEFAULT_OPTION: 1514: case OK_CANCEL_OPTION: 1515: case YES_NO_CANCEL_OPTION: 1516: case YES_NO_OPTION: 1517: return true; 1518: } 1519: return false; 1520: } 1521: 1522: /** 1523: * This helper method makes the JInternalFrame wait until it is notified by 1524: * an InternalFrameClosing event. This method also adds the given 1525: * JOptionPane to the JInternalFrame and sizes it according to the 1526: * JInternalFrame's preferred size. 1527: * 1528: * @param f The JInternalFrame to make modal. 1529: */ 1530: private static void startModal(JInternalFrame f) 1531: { 1532: synchronized (f) 1533: { 1534: final JInternalFrame tmp = f; 1535: tmp.toFront(); 1536: 1537: f.addInternalFrameListener(new InternalFrameAdapter() 1538: { 1539: public void internalFrameClosed(InternalFrameEvent e) 1540: { 1541: synchronized (tmp) 1542: { 1543: tmp.removeInternalFrameListener(this); 1544: tmp.notifyAll(); 1545: } 1546: } 1547: }); 1548: try 1549: { 1550: while (! f.isClosed()) 1551: f.wait(); 1552: } 1553: catch (InterruptedException ignored) 1554: { 1555: // Ignore this Exception. 1556: } 1557: } 1558: } 1559: }
GNU Classpath (0.20) |