/* MyJOptionPane.java
2: Copyright (C) 2004, 2005, 2006, 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. */
// package javax.swing;
import java.awt.AWTEvent;
import javax.swing.JOptionPane;
import java.awt.ActiveEvent;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.*;
import javax.swing.*;
import java.awt.Frame;
import java.awt.MenuComponent;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import javax.swing.plaf.OptionPaneUI;
/**
60: * This class creates different types of JDialogs and JInternalFrames that can
61: * ask users for input or pass on information. MyJOptionPane can be used by
62: * calling one of the show static methods or by creating an instance of
63: * MyJOptionPane and calling createDialog or createInternalFrame.
64: */
public class MyJOptionPane extends JOptionPane implements Accessible
{
/**
68: * Provides the accessibility features for the <code>MyJOptionPane</code>
69: * component.
70: */
protected class MyAccessibleJOptionPane extends JComponent.AccessibleJComponent
{
private static final long serialVersionUID = 686071432213084821L;
/**
76: * Creates a new <code>MyAccessibleJOptionPane</code> instance.
77: */
protected MyAccessibleJOptionPane()
{
// Nothing to do here.
}
/**
84: * Returns the accessible role of this object, which is always
85: * {@link AccessibleRole#OPTION_PANE}.
86: *
87: * @return the accessible role of this object
88: */
public AccessibleRole getAccessibleRole()
{
return AccessibleRole.OPTION_PANE;
}
}
private static final long serialVersionUID = 5231143276678566796L;
/** The value returned when cancel option is selected. */
public static final int CANCEL_OPTION = 2;
/** The value returned when the dialog is closed without a selection. */
public static final int CLOSED_OPTION = -1;
/** An option used in confirmation dialog methods. */
public static final int DEFAULT_OPTION = -1;
/** The value returned when the no option is selected. */
public static final int NO_OPTION = 1;
/** An option used in confirmation dialog methods. */
public static final int OK_CANCEL_OPTION = 2;
/** The value returned when the ok option is selected. */
public static final int OK_OPTION = 0;
/** An option used in confirmation dialog methods. */
public static final int YES_NO_CANCEL_OPTION = 1;
/** An option used in confirmation dialog methods. */
public static final int YES_NO_OPTION = 0;
/** The value returned when the yes option is selected. */
public static final int YES_OPTION = 0;
/** Identifier for the error message type. */
public static final int ERROR_MESSAGE = 0;
/** Identifier for the information message type. */
public static final int INFORMATION_MESSAGE = 1;
/** Identifier for the plain message type. */
public static final int PLAIN_MESSAGE = -1;
/** Identifier for the question message type. */
public static final int QUESTION_MESSAGE = 3;
/** Identifier for the warning message type. */
public static final int WARNING_MESSAGE = 2;
/**
140: * The identifier for the propertyChangeEvent when the icon property
141: * changes.
142: */
public static final String ICON_PROPERTY = "icon";
/**
146: * The identifier for the propertyChangeEvent when the initialSelectionValue
147: * property changes.
148: */
public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
/**
152: * The identifier for the propertyChangeEvent when the initialValue property
153: * changes.
154: */
public static final String INITIAL_VALUE_PROPERTY = "initialValue";
/**
158: * The identifier for the propertyChangeEvent when the inputValue property
159: * changes.
160: */
public static final String INPUT_VALUE_PROPERTY = "inputValue";
/**
164: * The identifier for the propertyChangeEvent when the message property
165: * changes.
166: */
public static final String MESSAGE_PROPERTY = "message";
/**
170: * The identifier for the propertyChangeEvent when the messageType property
171: * changes.
172: */
public static final String MESSAGE_TYPE_PROPERTY = "messageType";
/**
176: * The identifier for the propertyChangeEvent when the optionType property
177: * changes.
178: */
public static final String OPTION_TYPE_PROPERTY = "optionType";
/**
182: * The identifier for the propertyChangeEvent when the options property
183: * changes.
184: */
public static final String OPTIONS_PROPERTY = "options";
/**
188: * The identifier for the propertyChangeEvent when the selectionValues
189: * property changes.
190: */
public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
/**
194: * The identifier for the propertyChangeEvent when the value property
195: * changes.
196: */
public static final String VALUE_PROPERTY = "value";
/**
200: * The identifier for the propertyChangeEvent when the wantsInput property
201: * changes.
202: */
public static final String WANTS_INPUT_PROPERTY = "wantsInput";
/** The value returned when the inputValue is uninitialized. */
public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
/** The icon displayed in the dialog/internal frame. */
protected Icon icon;
/** The initial selected value in the input component. */
protected Object initialSelectionValue;
/** The object that is initially selected for options. */
protected Object initialValue;
/** The value the user inputs. */
protected Object inputValue = UNINITIALIZED_VALUE;
/** The message displayed in the dialog/internal frame. */
protected Object message;
/** The type of message displayed. */
protected int messageType = PLAIN_MESSAGE;
/**
227: * The options (usually buttons) aligned at the bottom for the user to
228: * select.
229: */
protected Object[] options;
/** The type of options to display. */
protected int optionType = DEFAULT_OPTION;
/** The input values the user can select. */
protected Object[] selectionValues;
/** The value returned by selecting an option. */
protected Object value = UNINITIALIZED_VALUE;
/** Whether the Dialog/InternalFrame needs input. */
protected boolean wantsInput;
/** The common frame used when no parent is provided. */
// private static Frame privFrame = (Frame) SwingUtilities.getOwnerFrame(null);
/**
248: * Creates a new MyJOptionPane object using a message of "MyJOptionPane
249: * message", using the PLAIN_MESSAGE type and DEFAULT_OPTION.
250: */
public MyJOptionPane()
{
this("MyJOptionPane message", PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
}
/**
257: * Creates a new MyJOptionPane object using the given message using the
258: * PLAIN_MESSAGE type and DEFAULT_OPTION.
259: *
260: * @param message The message to display.
261: */
public MyJOptionPane(Object message)
{
this(message, PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
}
/**
268: * Creates a new MyJOptionPane object using the given message and messageType
269: * and DEFAULT_OPTION.
270: *
271: * @param message The message to display.
272: * @param messageType The type of message.
273: */
public MyJOptionPane(Object message, int messageType)
{
this(message, messageType, DEFAULT_OPTION, null, null, null);
}
/**
280: * Creates a new MyJOptionPane object using the given message, messageType and
281: * optionType.
282: *
283: * @param message The message to display.
284: * @param messageType The type of message.
285: * @param optionType The type of options.
286: */
public MyJOptionPane(Object message, int messageType, int optionType)
{
this(message, messageType, optionType, null, null, null);
}
public MyJOptionPane( Object message, int messageType, JScrollPane jsp)
{
this(message, messageType, DEFAULT_OPTION, null, null, null);
add(jsp);
}
/**
293: * Creates a new MyJOptionPane object using the given message, messageType,
294: * optionType and icon.
295: *
296: * @param message The message to display.
297: * @param messageType The type of message.
298: * @param optionType The type of options.
299: * @param icon The icon to display.
300: */
public MyJOptionPane(Object message, int messageType, int optionType, Icon icon)
{
this(message, messageType, optionType, icon, null, null);
}
/**
307: * Creates a new MyJOptionPane object using the given message, messageType,
308: * optionType, icon and options.
309: *
310: * @param message The message to display.
311: * @param messageType The type of message.
312: * @param optionType The type of options.
313: * @param icon The icon to display.
314: * @param options The options given.
315: */
public MyJOptionPane(Object message, int messageType, int optionType,
Icon icon, Object[] options)
{
this(message, messageType, optionType, icon, options, null);
}
/**
323: * Creates a new MyJOptionPane object using the given message, messageType,
324: * optionType, icon, options and initialValue. The initialValue will be
325: * focused initially.
326: *
327: * @param message The message to display.
328: * @param messageType The type of message.
329: * @param optionType The type of options.
330: * @param icon The icon to display.
331: * @param options The options given.
332: * @param initialValue The component to focus on initially.
333: *
334: * @throws IllegalArgumentException If the messageType or optionType are not
335: * legal values.
336: */
public MyJOptionPane(Object message, int messageType, int optionType,
Icon icon, Object[] options, Object initialValue)
{
this.message = message;
if (! validMessageType(messageType))
throw new IllegalArgumentException("Message Type not legal value.");
this.messageType = messageType;
if (! validOptionType(optionType))
throw new IllegalArgumentException("Option Type not legal value.");
this.optionType = optionType;
this.icon = icon;
this.options = options;
this.initialValue = initialValue;
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
updateUI();
}
/**
357: * This method creates a new JDialog that is either centered around the
358: * parent's frame or centered on the screen (if the parent is null). The
359: * JDialog will not be resizable and will be modal. Once the JDialog is
360: * disposed, the inputValue and value properties will be set by the
361: * optionPane.
362: *
363: * @param parentComponent The parent of the Dialog.
364: * @param title The title in the bar of the JDialog.
365: *
366: * @return A new JDialog based on the MyJOptionPane configuration.
367: */
public JDialog createDialog(Component parentComponent, String title)
{
Frame toUse = getFrameForComponent(parentComponent);
if (toUse == null)
toUse = getRootFrame();
JDialog dialog = new JDialog(toUse, title);
inputValue = UNINITIALIZED_VALUE;
value = UNINITIALIZED_VALUE;
dialog.getContentPane().add(this);
dialog.setModal(true);
dialog.setResizable(false);
dialog.pack();
dialog.setLocationRelativeTo(parentComponent);
addPropertyChangeListener(new ValuePropertyHandler(dialog));
return dialog;
}
/**
389: * Handles changes of the value property. Whenever this property changes,
390: * the MyJOptionPane dialog should be closed.
391: */
private static class ValuePropertyHandler
implements PropertyChangeListener
{
/**
396: * The dialog to close.
397: */
JDialog dialog;
/**
401: * Creates a new instance.
402: *
403: * @param d the dialog to be closed
404: */
ValuePropertyHandler(JDialog d)
{
dialog = d;
}
/**
411: * Receives notification when any of the properties change.
412: */
public void propertyChange(PropertyChangeEvent p)
{
String prop = p.getPropertyName();
Object val = p.getNewValue();
if (prop.equals(VALUE_PROPERTY) && val != null
&& val != UNINITIALIZED_VALUE)
{
dialog.setVisible(false);
}
}
}
/**
426: * This method creates a new JInternalFrame that is in the JLayeredPane
427: * which contains the parentComponent given. If no suitable JLayeredPane
428: * can be found from the parentComponent given, a RuntimeException will be
429: * thrown.
430: *
431: * @param parentComponent The parent to find a JDesktopPane from.
432: * @param title The title of the JInternalFrame.
433: *
434: * @return A new JInternalFrame based on the MyJOptionPane configuration.
435: *
436: * @throws RuntimeException If no suitable JDesktopPane is found.
437: *
438: * @specnote The specification says that the internal frame is placed
439: * in the nearest <code>JDesktopPane</code> that is found in
440: * <code>parent</code>'s ancestors. The behaviour of the JDK
441: * is that it actually looks up the nearest
442: * <code>JLayeredPane</code> in <code>parent</code>'s ancestors.
443: * So do we.
444: */
public JInternalFrame createInternalFrame(Component parentComponent,
String title)
throws RuntimeException
{
// Try to find a JDesktopPane.
JLayeredPane toUse = getDesktopPaneForComponent(parentComponent);
// If we don't have a JDesktopPane, we try to find a JLayeredPane.
if (toUse == null)
toUse = JLayeredPane.getLayeredPaneAbove(parentComponent);
// If this still fails, we throw a RuntimeException.
if (toUse == null)
throw new RuntimeException
("parentComponent does not have a valid parent");
JInternalFrame frame = new JInternalFrame(title);
inputValue = UNINITIALIZED_VALUE;
value = UNINITIALIZED_VALUE;
frame.setContentPane(this);
frame.setClosable(true);
toUse.add(frame);
frame.setLayer(JLayeredPane.MODAL_LAYER);
frame.pack();
frame.setVisible(true);
return frame;
}
/**
477: * Returns the object that provides accessibility features for this
478: * <code>MyJOptionPane</code> component.
479: *
480: * @return The accessible context (an instance of
481: * {@link MyAccessibleJOptionPane}).
482: */
public AccessibleContext getAccessibleContext()
{
if (accessibleContext == null)
accessibleContext = new MyAccessibleJOptionPane();
return accessibleContext;
}
/**
491: * This method returns the JDesktopPane for the given parentComponent or
492: * null if none can be found.
493: *
494: * @param parentComponent The component to look in.
495: *
496: * @return The JDesktopPane for the given component or null if none can be
497: * found.
498: */
public static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
{
return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class,
parentComponent);
}
/**
506: * This method returns the Frame for the given parentComponent or null if
507: * none can be found.
508: *
509: * @param parentComponent The component to look in.
510: *
511: * @return The Frame for the given component or null if none can be found.
512: */
public static Frame getFrameForComponent(Component parentComponent)
{
return (Frame) SwingUtilities.getAncestorOfClass(Frame.class,
parentComponent);
}
/**
520: * This method returns the icon displayed.
521: *
522: * @return The icon displayed.
523: */
public Icon getIcon()
{
return icon;
}
/**
530: * This method returns the value initially selected from the list of values
531: * the user can input.
532: *
533: * @return The initial selection value.
534: */
public Object getInitialSelectionValue()
{
return initialSelectionValue;
}
/**
541: * This method returns the value that is focused from the list of options.
*
* @return The initial value from options.
*/
public Object getInitialValue()
{
return initialValue;
}
/**
551: * This method returns the value that the user input.
552: *
553: * @return The user's input value.
554: */
public Object getInputValue()
{
if (getValue().equals(new Integer(CANCEL_OPTION)))
setInputValue(null);
return inputValue;
}
/**
563: * This method returns the maximum characters per line. By default, this is
564: * Integer.MAX_VALUE.
565: *
566: * @return The maximum characters per line.
567: */
public int getMaxCharactersPerLineCount()
{
return Integer.MAX_VALUE;
}
/**
574: * This method returns the message displayed.
575: *
576: * @return The message displayed.
577: */
public Object getMessage()
{
return message;
}
/**
584: * This method returns the message type.
585: *
586: * @return The message type.
587: */
public int getMessageType()
{
return messageType;
}
/**
594: * This method returns the options.
595: *
596: * @return The options.
597: */
public Object[] getOptions()
{
return options;
}
/**
604: * This method returns the option type.
605: *
606: * @return The option type.
607: */
public int getOptionType()
{
return optionType;
}
/**
614: * This method returns the Frame used by MyJOptionPane dialog's that have no
615: * parent.
616: *
617: * @return The Frame used by dialogs that have no parent.
618: */
/*
public static Frame getRootFrame()
{
return privFrame;
}
*/
/**
625: * This method returns the selection values.
626: *
627: * @return The selection values.
628: */
public Object[] getSelectionValues()
{
return selectionValues;
}
/**
635: * This method returns the UI used by the MyJOptionPane.
636: *
637: * @return The UI used by the MyJOptionPane.
638: */
public OptionPaneUI getUI()
{
return (OptionPaneUI) ui;
}
/**
645: * This method returns an identifier to determine which UI class will act as
646: * the UI.
647: *
648: * @return The UI identifier.
649: */
public String getUIClassID()
{
return "OptionPaneUI";
}
/**
656: * This method returns the value that the user selected out of options.
657: *
658: * @return The value that the user selected out of options.
659: */
public Object getValue()
{
return value;
}
/**
666: * This method returns whether this MyJOptionPane wants input.
667: *
668: * @return Whether this MyJOptionPane wants input.
669: */
public boolean getWantsInput()
{
return wantsInput;
}
/**
676: * This method returns a String that describes this MyJOptionPane.
677: *
678: * @return A String that describes this MyJOptionPane.
679: */
protected String paramString()
{
return "MyJOptionPane";
}
/**
686: * This method requests focus for the initial value.
687: */
public void selectInitialValue()
{
if (ui != null)
((OptionPaneUI) ui).selectInitialValue(this);
}
/**
695: * This method changes the icon property.
696: *
697: * @param newIcon The new icon to use.
698: */
public void setIcon(Icon newIcon)
{
if (icon != newIcon)
{
Icon old = icon;
icon = newIcon;
firePropertyChange(ICON_PROPERTY, old, icon);
}
}
/**
710: * This method changes the initial selection property.
711: *
712: * @param newValue The new initial selection.
713: */
public void setInitialSelectionValue(Object newValue)
{
if (initialSelectionValue != newValue)
{
Object old = initialSelectionValue;
initialSelectionValue = newValue;
firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old,
initialSelectionValue);
}
}
/**
726: * This method changes the initial value property.
727: *
728: * @param newValue The new initial value.
729: */
public void setInitialValue(Object newValue)
{
if (initialValue != newValue)
{
Object old = initialValue;
initialValue = newValue;
firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue);
}
}
/**
741: * This method changes the inputValue property.
742: *
743: * @param newValue The new inputValue.
744: */
public void setInputValue(Object newValue)
{
if (inputValue != newValue)
{
Object old = inputValue;
inputValue = newValue;
firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue);
}
}
/**
756: * This method changes the message property.
757: *
758: * @param newMessage The new message.
759: */
public void setMessage(Object newMessage)
{
if (message != newMessage)
{
Object old = message;
message = newMessage;
firePropertyChange(MESSAGE_PROPERTY, old, message);
}
}
/**
771: * This method changes the messageType property.
772: *
773: * @param newType The new messageType.
774: *
775: * @throws IllegalArgumentException If the messageType is not valid.
776: */
public void setMessageType(int newType)
{
if (! validMessageType(newType))
throw new IllegalArgumentException("Message Type not legal value.");
if (newType != messageType)
{
int old = messageType;
messageType = newType;
firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType);
}
}
/**
790: * This method changes the options property.
791: *
792: * @param newOptions The new options.
793: */
public void setOptions(Object[] newOptions)
{
if (options != newOptions)
{
Object[] old = options;
options = newOptions;
firePropertyChange(OPTIONS_PROPERTY, old, options);
}
}
/**
805: * This method changes the optionType property.
806: *
807: * @param newType The new optionType.
808: *
809: * @throws IllegalArgumentException If the optionType is not valid.
810: */
public void setOptionType(int newType)
{
if (! validOptionType(newType))
throw new IllegalArgumentException("Option Type not legal value.");
if (newType != optionType)
{
int old = optionType;
optionType = newType;
firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType);
}
}
/**
824: * This method changes the Frame used for MyJOptionPane dialogs that have no
825: * parent.
826: *
827: * @param newRootFrame The Frame to use for dialogs that have no parent.
828: */
/*
public static void setRootFrame(Frame newRootFrame)
{
privFrame = newRootFrame;
}
*/
/**
835: * This method changes the selectionValues property.
836: *
837: * @param newValues The new selectionValues.
838: */
public void setSelectionValues(Object[] newValues)
{
if (newValues != selectionValues)
{
if (newValues != null)
wantsInput = true;
Object[] old = selectionValues;
selectionValues = newValues;
firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues);
}
}
/**
852: * This method sets the UI used with the MyJOptionPane.
853: *
854: * @param ui The UI used with the MyJOptionPane.
855: */
public void setUI(OptionPaneUI ui)
{
super.setUI(ui);
}
/**
862: * This method sets the value has been selected out of options.
863: *
864: * @param newValue The value that has been selected out of options.
865: */
public void setValue(Object newValue)
{
if (value != newValue)
{
Object old = value;
value = newValue;
firePropertyChange(VALUE_PROPERTY, old, value);
}
}
/**
877: * This method changes the wantsInput property.
878: *
879: * @param newValue Whether this MyJOptionPane requires input.
880: */
public void setWantsInput(boolean newValue)
{
if (wantsInput != newValue)
{
boolean old = wantsInput;
wantsInput = newValue;
firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput);
}
}
/**
892: * This method shows a confirmation dialog with the title "Select an Option"
893: * and displays the given message. The parent frame will be the same as the
894: * parent frame of the given parentComponent. This method returns the
895: * option chosen by the user.
896: *
897: * @param parentComponent The parentComponent to find a frame in.
898: * @param message The message to display.
899: *
900: * @return The option that was selected.
901: */
public static int showConfirmDialog(Component parentComponent, Object message)
{
MyJOptionPane pane = new MyJOptionPane(message, QUESTION_MESSAGE);
JDialog dialog = pane.createDialog(parentComponent, "Select an Option");
dialog.setVisible(true);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
914: * This method shows a confirmation dialog with the given message,
915: * optionType and title. The frame that owns the dialog will be the same
916: * frame that holds the given parentComponent. This method returns the
917: * option that was chosen.
918: *
919: * @param parentComponent The component to find a frame in.
920: * @param message The message displayed.
921: * @param title The title of the dialog.
922: * @param optionType The optionType.
923: *
924: * @return The option that was chosen.
925: */
public static int showConfirmDialog(Component parentComponent,
Object message, String title,
int optionType)
{
MyJOptionPane pane = new MyJOptionPane(message, PLAIN_MESSAGE, optionType);
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(true);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
940: * This method shows a confirmation dialog with the given message, title,
941: * messageType and optionType. The frame owner will be the same frame as
942: * the one that holds the given parentComponent. This method returns the
943: * option selected by the user.
944: *
945: * @param parentComponent The component to find a frame in.
946: * @param message The message displayed.
947: * @param title The title of the dialog.
948: * @param optionType The optionType.
949: * @param messageType The messageType.
950: *
951: * @return The selected option.
952: */
public static int showConfirmDialog(Component parentComponent,
Object message, String title,
int optionType, int messageType)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType, optionType);
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(true);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
967: * This method shows a confirmation dialog with the given message, title,
968: * optionType, messageType and icon. The frame owner will be the same as
969: * the one that holds the given parentComponent. This method returns the
970: * option selected by the user.
971: *
972: * @param parentComponent The component to find a frame in.
973: * @param message The message displayed.
974: * @param title The title of the dialog.
975: * @param optionType The optionType.
976: * @param messageType The messsageType.
977: * @param icon The icon displayed.
978: *
979: * @return The selected option.
980: */
public static int showConfirmDialog(Component parentComponent,
Object message, String title,
int optionType, int messageType,
Icon icon)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType, optionType, icon);
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(true);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
996: * This method will show a QUESTION_MESSAGE input dialog with the given
997: * message. No selectionValues is set so the Look and Feel will usually
998: * give the user a TextField to fill out. The frame owner will be the same
999: * frame that holds the given parentComponent. This method will return the
1000: * value entered by the user.
1001: *
1002: * @param parentComponent The component to find a frame in.
1003: * @param message The message displayed.
1004: *
1005: * @return The value entered by the user.
1006: */
public static String showInputDialog(Component parentComponent,
Object message)
{
MyJOptionPane pane = new MyJOptionPane(message, QUESTION_MESSAGE);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(parentComponent, null);
dialog.setVisible(true);
return (String) pane.getInputValue();
}
/**
1019: * This method will show a QUESTION_MESSAGE type input dialog with the given
1020: * message and initialSelectionValue. Since there is no selectionValues
1021: * set, the Look and Feel will usually give a TextField to fill out. The
1022: * frame owner will be the same as the one that holds the given
1023: * parentComponent. This method will return the value entered by the user.
1024: *
1025: * @param parentComponent The component to find a frame in.
1026: * @param message The message to display.
1027: * @param initialSelectionValue The initially selected value.
1028: *
1029: * @return The value the user input.
1030: */
public static String showInputDialog(Component parentComponent,
Object message,
Object initialSelectionValue)
{
MyJOptionPane pane = new MyJOptionPane(message, QUESTION_MESSAGE);
pane.setInitialSelectionValue(initialSelectionValue);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(parentComponent, null);
dialog.setVisible(true);
return (String) pane.getInputValue();
}
/**
1045: * This method displays a new input dialog with the given message, title and
1046: * messageType. Since no selectionValues value is given, the Look and Feel
1047: * will usually give the user a TextField to input data to. This method
1048: * returns the value the user inputs.
1049: *
1050: * @param parentComponent The component to find a frame in.
1051: * @param message The message to display.
1052: * @param title The title of the dialog.
1053: * @param messageType The messageType.
1054: *
1055: * @return The value the user input.
1056: */
public static String showInputDialog(Component parentComponent,
Object message, String title,
int messageType)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(true);
return (String) pane.getInputValue();
}
/**
1070: * This method shows an input dialog with the given message, title,
1071: * messageType, icon, selectionValues, and initialSelectionValue. This
1072: * method returns the value that the user selects.
1073: *
1074: * @param parentComponent The component to find a frame in.
1075: * @param message The message displayed.
1076: * @param title The title of the dialog.
1077: * @param messageType The messageType.
1078: * @param icon The icon displayed.
1079: * @param selectionValues The list of values to select from.
1080: * @param initialSelectionValue The initially selected value.
1081: *
1082: * @return The user selected value.
1083: */
public static Object showInputDialog(Component parentComponent,
Object message, String title,
int messageType, Icon icon,
Object[] selectionValues,
Object initialSelectionValue)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType);
pane.setWantsInput(true);
pane.setIcon(icon);
pane.setSelectionValues(selectionValues);
pane.setInitialSelectionValue(initialSelectionValue);
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(true);
return pane.getInputValue();
}
/**
1102: * This method shows a QUESTION_MESSAGE type input dialog. Since no
1103: * selectionValues is set, the Look and Feel will usually give the user a
1104: * TextField to input data to. This method returns the value the user
1105: * inputs.
1106: *
1107: * @param message The message to display.
1108: *
1109: * @return The user selected value.
1110: */
public static String showInputDialog(Object message)
{
MyJOptionPane pane = new MyJOptionPane(message, QUESTION_MESSAGE);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(null, null);
dialog.setVisible(true);
return (String) pane.getInputValue();
}
/**
1122: * This method shows a QUESTION_MESSAGE type input dialog. Since no
1123: * selectionValues is set, the Look and Feel will usually give the user a
1124: * TextField to input data to. The input component will be initialized with
1125: * the initialSelectionValue. This method returns the value the user
1126: * inputs.
1127: *
1128: * @param message The message to display.
1129: * @param initialSelectionValue The initialSelectionValue.
1130: *
1131: * @return The user selected value.
1132: */
public static String showInputDialog(Object message,
Object initialSelectionValue)
{
MyJOptionPane pane = new MyJOptionPane(message, QUESTION_MESSAGE);
pane.setWantsInput(true);
pane.setInitialSelectionValue(initialSelectionValue);
JDialog dialog = pane.createDialog(null, null);
dialog.setVisible(true);
return (String) pane.getInputValue();
}
/**
1146: * This method shows an internal confirmation dialog with the given message.
1147: * The internal frame dialog will be placed in the first JDesktopPane
1148: * ancestor of the given parentComponent. This method will return the value
1149: * selected.
1150: *
1151: * @param parentComponent The parent to find a JDesktopPane in.
1152: * @param message The message to display.
1153: *
1154: * @return The value selected.
1155: */
public static int showInternalConfirmDialog(Component parentComponent,
Object message)
{
MyJOptionPane pane = new MyJOptionPane(message);
JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
startModal(frame);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
1170: * This method shows an internal confirmation dialog with the given message,
1171: * optionType and title. The internal frame dialog will be placed in the
1172: * first JDesktopPane ancestor of the given parentComponent. This method
1173: * will return the selected value.
1174: *
1175: * @param parentComponent The parent to find a JDesktopPane in.
1176: * @param message The message to display.
1177: * @param title The title to display.
1178: * @param optionType The option type.
1179: *
1180: * @return The selected value.
1181: */
public static int showInternalConfirmDialog(Component parentComponent,
Object message, String title,
int optionType)
{
MyJOptionPane pane = new MyJOptionPane(message, PLAIN_MESSAGE, optionType);
JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
startModal(frame);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
1197: * This method shows an internal confirmation dialog with the given message,
1198: * title, optionTypes and icon for the given message type. The internal
1199: * confirmation dialog will be placed in the first instance of
1200: * JDesktopPane ancestor of the given parentComponent.
1201: *
1202: * @param parentComponent The component to find a JDesktopPane in.
1203: * @param message The message to display.
1204: * @param title The title of the dialog.
1205: * @param optionType The option type.
1206: * @param messageType The message type.
1207: *
1208: * @return The selected value.
1209: */
public static int showInternalConfirmDialog(Component parentComponent,
Object message, String title,
int optionType, int messageType)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType, optionType);
JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
startModal(frame);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
1225: * This method shows an internal confirmation dialog with the given message,
1226: * title, option type, message type, and icon. The internal frame dialog
1227: * will be placed in the first JDesktopPane ancestor that is found in the
1228: * given parentComponent. This method returns the selected value.
1229: *
1230: * @param parentComponent The parent to find a JDesktopPane in.
1231: * @param message The message to display.
1232: * @param title The title to display.
1233: * @param optionType The option type.
1234: * @param messageType The message type.
1235: * @param icon The icon to display.
1236: *
1237: * @return The selected value.
1238: */
public static int showInternalConfirmDialog(Component parentComponent,
Object message, String title,
int optionType, int messageType,
Icon icon)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType, optionType, icon);
JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
startModal(frame);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
1255: * This method shows an internal input dialog with the given message. The
1256: * internal frame dialog will be placed in the first JDesktopPane ancestor
1257: * of the given parent component. This method returns the value input by
1258: * the user.
1259: *
1260: * @param parentComponent The parent to find a JDesktopPane in.
1261: * @param message The message to display.
1262: *
1263: * @return The user selected value.
*/
public static String showInternalInputDialog(Component parentComponent,
Object message)
{
MyJOptionPane pane = new MyJOptionPane(message);
pane.setWantsInput(true);
JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
startModal(frame);
return (String) pane.getInputValue();
}
/**
1278: * This method shows an internal input dialog with the given message, title
1279: * and message type. The internal input dialog will be placed in the first
1280: * JDesktopPane ancestor found in the given parent component. This method
1281: * will return the input value given by the user.
1282: *
1283: * @param parentComponent The component to find a JDesktopPane in.
1284: * @param message The message to display.
1285: * @param title The title to display.
1286: * @param messageType The message type.
1287: *
1288: * @return The user input value.
1289: */
public static String showInternalInputDialog(Component parentComponent,
Object message, String title,
int messageType)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType);
pane.setWantsInput(true);
JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
startModal(frame);
return (String) pane.getInputValue();
}
/**
1304: * This method shows an internal input dialog with the given message, title
1305: * message type, icon, selection value list and initial selection value.
1306: * The internal frame dialog will be placed in the first JDesktopPane
1307: * ancestor found in the given parent component. This method returns the
1308: * input value from the user.
1309: *
1310: * @param parentComponent The parent to find a JDesktopPane in.
1311: * @param message The message to display.
1312: * @param title The title to display.
1313: * @param messageType The message type.
1314: * @param icon The icon to display.
1315: * @param selectionValues The selection value list.
1316: * @param initialSelectionValue The initial selection value.
1317: *
1318: * @return The user input value.
1319: */
public static Object showInternalInputDialog(Component parentComponent,
Object message, String title,
int messageType, Icon icon,
Object[] selectionValues,
Object initialSelectionValue)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType);
pane.setWantsInput(true);
pane.setIcon(icon);
pane.setSelectionValues(selectionValues);
pane.setInitialSelectionValue(initialSelectionValue);
JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
startModal(frame);
return pane.getInputValue();
}
/**
1339: * This method shows an internal message dialog with the given message. The
1340: * internal frame dialog will be placed in the first JDesktopPane ancestor
1341: * found in the given parent component.
1342: *
1343: * @param parentComponent The component to find a JDesktopPane in.
1344: * @param message The message to display.
1345: */
public static void showInternalMessageDialog(Component parentComponent,
Object message)
{
MyJOptionPane pane = new MyJOptionPane(message);
JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
startModal(frame);
}
/**
1356: * This method shows an internal message dialog with the given message,
1357: * title and message type. The internal message dialog is placed in the
1358: * first JDesktopPane ancestor found in the given parent component.
1359: *
1360: * @param parentComponent The parent component to find a JDesktopPane in.
1361: * @param message The message to display.
1362: * @param title The title to display.
1363: * @param messageType The message type.
1364: */
public static void showInternalMessageDialog(Component parentComponent,
Object message, String title,
int messageType)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType);
JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
startModal(frame);
}
/**
1376: * This method shows an internal message dialog with the given message,
1377: * title, message type and icon. The internal message dialog is placed in
1378: * the first JDesktopPane ancestor found in the given parent component.
1379: *
1380: * @param parentComponent The component to find a JDesktopPane in.
1381: * @param message The message to display.
1382: * @param title The title to display.
1383: * @param messageType The message type.
1384: * @param icon The icon to display.
1385: */
public static void showInternalMessageDialog(Component parentComponent,
Object message, String title,
int messageType, Icon icon)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType);
pane.setIcon(icon);
JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
startModal(frame);
}
/**
1398: * This method displays an internal option dialog with the given message,
1399: * title, option type, message type, icon, option list, and initial option
1400: * value. The internal option dialog is placed in the first JDesktopPane
1401: * ancestor found in the parent component. This method returns the option
1402: * selected.
1403: *
1404: * @param parentComponent The parent to find a JDesktopPane in.
1405: * @param message The message displayed.
1406: * @param title The title displayed.
1407: * @param optionType The option type.
1408: * @param messageType The message type.
1409: * @param icon The icon to display.
1410: * @param options The array of options.
1411: * @param initialValue The initial value selected.
1412: *
1413: * @return The option that was selected.
1414: */
public static int showInternalOptionDialog(Component parentComponent,
Object message, String title,
int optionType, int messageType,
Icon icon, Object[] options,
Object initialValue)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType, optionType, icon,
options, initialValue);
JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
startModal(frame);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
1434: * This method shows an INFORMATION_MESSAGE type message dialog.
1435: *
1436: * @param parentComponent The component to find a frame in.
1437: * @param message The message displayed.
1438: */
public static void showMessageDialog(Component parentComponent,
Object message)
{
MyJOptionPane pane = new MyJOptionPane(message, INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(parentComponent, null);
dialog.setVisible(true);
}
public static void showMessageDialog2(Component parentComponent, Object message, JScrollPane jsp2)
{
MyJOptionPane pane = new MyJOptionPane(message, INFORMATION_MESSAGE, jsp2 );
JDialog dialog = pane.createDialog(parentComponent, null);
dialog.setVisible(true);
}
/**
1448: * This method shows a message dialog with the given message, title and
1449: * messageType.
1450: *
1451: * @param parentComponent The component to find a frame in.
1452: * @param message The message displayed.
1453: * @param title The title of the dialog.
1454: * @param messageType The messageType.
1455: */
public static void showMessageDialog(Component parentComponent,
Object message, String title,
int messageType)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType);
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(true);
}
/**
1466: * This method shows a message dialog with the given message, title,
1467: * messageType and icon.
1468: *
1469: * @param parentComponent The component to find a frame in.
1470: * @param message The message displayed.
1471: * @param title The title of the dialog.
1472: * @param messageType The messageType.
1473: * @param icon The icon displayed.
1474: */
public static void showMessageDialog(Component parentComponent,
Object message, String title,
int messageType, Icon icon)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType);
pane.setIcon(icon);
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(true);
}
/**
1486: * This method shows an option dialog with the given message, title,
1487: * optionType, messageType, icon, options and initialValue. This method
1488: * returns the option that was selected.
1489: *
1490: * @param parentComponent The component to find a frame in.
1491: * @param message The message displayed.
1492: * @param title The title of the dialog.
1493: * @param optionType The optionType.
1494: * @param messageType The messageType.
1495: * @param icon The icon displayed.
1496: * @param options The options to choose from.
1497: * @param initialValue The initial value.
1498: *
1499: * @return The selected option.
1500: */
public static int showOptionDialog(Component parentComponent,
Object message, String title,
int optionType, int messageType,
Icon icon, Object[] options,
Object initialValue)
{
MyJOptionPane pane = new MyJOptionPane(message, messageType, optionType, icon,
options, initialValue);
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(true);
if (pane.getValue() instanceof Integer)
return ((Integer) pane.getValue()).intValue();
return -1;
}
/**
1519: * This method resets the UI to the Look and Feel default.
1520: */
public void updateUI()
{
setUI((OptionPaneUI) UIManager.getUI(this));
}
/**
1527: * This method returns true if the key is a valid messageType.
1528: *
1529: * @param key The key to check.
1530: *
1531: * @return True if key is valid.
1532: */
private boolean validMessageType(int key)
{
switch (key)
{
case ERROR_MESSAGE:
case INFORMATION_MESSAGE:
case PLAIN_MESSAGE:
case QUESTION_MESSAGE:
case WARNING_MESSAGE:
return true;
}
return false;
}
/**
1548: * This method returns true if the key is a valid optionType.
1549: *
1550: * @param key The key to check.
1551: *
1552: * @return True if key is valid.
*/
private boolean validOptionType(int key)
{
switch (key)
{
case DEFAULT_OPTION:
case OK_CANCEL_OPTION:
case YES_NO_CANCEL_OPTION:
case YES_NO_OPTION:
return true;
}
return false;
}
/**
1568: * This helper method makes the JInternalFrame wait until it is notified by
1569: * an InternalFrameClosing event. This method also adds the given
1570: * MyJOptionPane to the JInternalFrame and sizes it according to the
1571: * JInternalFrame's preferred size.
1572: *
1573: * @param f The JInternalFrame to make modal.
1574: */
private static void startModal(JInternalFrame f)
{
// We need to add an additional glasspane-like component directly
// below the frame, which intercepts all mouse events that are not
// directed at the frame itself.
JPanel modalInterceptor = new JPanel();
modalInterceptor.setOpaque(false);
JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
modalInterceptor.addMouseListener(new MouseAdapter(){});
modalInterceptor.addMouseMotionListener(new MouseMotionAdapter(){});
lp.add(modalInterceptor);
f.toFront();
// We need to explicitly dispatch events when we are blocking the event
// dispatch thread.
EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
try
{
while (! f.isClosed())
{
if (EventQueue.isDispatchThread())
{
// The getNextEventMethod() issues wait() when no
// event is available, so we don't need do explicitly wait().
AWTEvent ev = queue.getNextEvent();
// This mimics EventQueue.dispatchEvent(). We can't use
// EventQueue.dispatchEvent() directly, because it is
// protected, unfortunately.
if (ev instanceof ActiveEvent)
((ActiveEvent) ev).dispatch();
else if (ev.getSource() instanceof Component)
((Component) ev.getSource()).dispatchEvent(ev);
else if (ev.getSource() instanceof MenuComponent)
((MenuComponent) ev.getSource()).dispatchEvent(ev);
// Other events are ignored as per spec in
// EventQueue.dispatchEvent
}
else
{
// Give other threads a chance to become active.
Thread.yield();
}
}
}
catch (InterruptedException ex)
{
// If we get interrupted, then leave the modal state.
}
finally
{
// Clean up the modal interceptor.
lp.remove(modalInterceptor);
// Remove the internal frame from its parent, so it is no longer
// lurking around and clogging memory.
Container parent = f.getParent();
if (parent != null)
parent.remove(f);
}
}
}