5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.swing;
27
28 import java.awt.BorderLayout;
29 import java.awt.Component;
30 import java.awt.Container;
31 import java.awt.Dialog;
32 import java.awt.Dimension;
33 import java.awt.KeyboardFocusManager;
34 import java.awt.Frame;
35 import java.awt.Point;
36 import java.awt.HeadlessException;
37 import java.awt.Window;
38 import java.beans.PropertyChangeEvent;
39 import java.beans.PropertyChangeListener;
40 import java.awt.event.WindowListener;
41 import java.awt.event.WindowAdapter;
42 import java.awt.event.WindowEvent;
43 import java.awt.event.ComponentAdapter;
44 import java.awt.event.ComponentEvent;
45 import java.io.IOException;
46 import java.io.InvalidObjectException;
47 import java.io.ObjectInputStream;
48 import java.io.ObjectOutputStream;
49 import java.io.Serializable;
50 import java.util.Vector;
51 import javax.swing.plaf.OptionPaneUI;
52 import javax.swing.event.InternalFrameEvent;
53 import javax.swing.event.InternalFrameAdapter;
54 import javax.accessibility.*;
55 import static javax.swing.ClientPropertyKey.PopupFactory_FORCE_HEAVYWEIGHT_POPUP;
56 import sun.awt.AWTAccessor;
57
284 * }
285 * return CLOSED_OPTION;
286 * </pre>
287 * <p>
288 * <strong>Warning:</strong> Swing is not thread safe. For more
289 * information see <a
290 * href="package-summary.html#threading">Swing's Threading
291 * Policy</a>.
292 * <p>
293 * <strong>Warning:</strong>
294 * Serialized objects of this class will not be compatible with
295 * future Swing releases. The current serialization support is
296 * appropriate for short term storage or RMI between applications running
297 * the same version of Swing. As of 1.4, support for long term storage
298 * of all JavaBeans™
299 * has been added to the <code>java.beans</code> package.
300 * Please see {@link java.beans.XMLEncoder}.
301 *
302 * @see JInternalFrame
303 *
304 * @beaninfo
305 * attribute: isContainer true
306 * description: A component which implements standard dialog box controls.
307 *
308 * @author James Gosling
309 * @author Scott Violet
310 * @since 1.2
311 */
312 @SuppressWarnings("serial") // Same-version serialization only
313 public class JOptionPane extends JComponent implements Accessible
314 {
315 /**
316 * @see #getUIClassID
317 * @see #readObject
318 */
319 private static final String uiClassID = "OptionPaneUI";
320
321 /**
322 * Indicates that the user has not yet selected a value.
323 */
324 public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
325
326 //
327 // Option types
328 //
329
330 /**
331 * Type meaning Look and Feel should not supply any options -- only
1809 */
1810 public JOptionPane(Object message, int messageType, int optionType,
1811 Icon icon, Object[] options, Object initialValue) {
1812
1813 this.message = message;
1814 this.options = options;
1815 this.initialValue = initialValue;
1816 this.icon = icon;
1817 setMessageType(messageType);
1818 setOptionType(optionType);
1819 value = UNINITIALIZED_VALUE;
1820 inputValue = UNINITIALIZED_VALUE;
1821 updateUI();
1822 }
1823
1824 /**
1825 * Sets the UI object which implements the {@literal L&F} for this component.
1826 *
1827 * @param ui the <code>OptionPaneUI</code> {@literal L&F} object
1828 * @see UIDefaults#getUI
1829 * @beaninfo
1830 * bound: true
1831 * hidden: true
1832 * description: The UI object that implements the optionpane's LookAndFeel
1833 */
1834 public void setUI(OptionPaneUI ui) {
1835 if (this.ui != ui) {
1836 super.setUI(ui);
1837 invalidate();
1838 }
1839 }
1840
1841 /**
1842 * Returns the UI object which implements the {@literal L&F} for this component.
1843 *
1844 * @return the <code>OptionPaneUI</code> object
1845 */
1846 public OptionPaneUI getUI() {
1847 return (OptionPaneUI)ui;
1848 }
1849
1850 /**
1851 * Notification from the <code>UIManager</code> that the {@literal L&F} has changed.
1852 * Replaces the current UI object with the latest version from the
1853 * <code>UIManager</code>.
1854 *
1855 * @see JComponent#updateUI
1856 */
1857 public void updateUI() {
1858 setUI((OptionPaneUI)UIManager.getUI(this));
1859 }
1860
1861
1862 /**
1863 * Returns the name of the UI class that implements the
1864 * {@literal L&F} for this component.
1865 *
1866 * @return the string "OptionPaneUI"
1867 * @see JComponent#getUIClassID
1868 * @see UIDefaults#getUI
1869 */
1870 public String getUIClassID() {
1871 return uiClassID;
1872 }
1873
1874
1875 /**
1876 * Sets the option pane's message-object.
1877 * @param newMessage the <code>Object</code> to display
1878 * @see #getMessage
1879 *
1880 * @beaninfo
1881 * preferred: true
1882 * bound: true
1883 * description: The optionpane's message object.
1884 */
1885 public void setMessage(Object newMessage) {
1886 Object oldMessage = message;
1887
1888 message = newMessage;
1889 firePropertyChange(MESSAGE_PROPERTY, oldMessage, message);
1890 }
1891
1892 /**
1893 * Returns the message-object this pane displays.
1894 * @see #setMessage
1895 *
1896 * @return the <code>Object</code> that is displayed
1897 */
1898 public Object getMessage() {
1899 return message;
1900 }
1901
1902 /**
1903 * Sets the icon to display. If non-<code>null</code>, the look and feel
1904 * does not provide an icon.
1905 * @param newIcon the <code>Icon</code> to display
1906 *
1907 * @see #getIcon
1908 * @beaninfo
1909 * preferred: true
1910 * bound: true
1911 * description: The option pane's type icon.
1912 */
1913 public void setIcon(Icon newIcon) {
1914 Object oldIcon = icon;
1915
1916 icon = newIcon;
1917 firePropertyChange(ICON_PROPERTY, oldIcon, icon);
1918 }
1919
1920 /**
1921 * Returns the icon this pane displays.
1922 * @return the <code>Icon</code> that is displayed
1923 *
1924 * @see #setIcon
1925 */
1926 public Icon getIcon() {
1927 return icon;
1928 }
1929
1930 /**
1931 * Sets the value the user has chosen.
1932 * @param newValue the chosen value
1933 *
1934 * @see #getValue
1935 * @beaninfo
1936 * preferred: true
1937 * bound: true
1938 * description: The option pane's value object.
1939 */
1940 public void setValue(Object newValue) {
1941 Object oldValue = value;
1942
1943 value = newValue;
1944 firePropertyChange(VALUE_PROPERTY, oldValue, value);
1945 }
1946
1947 /**
1948 * Returns the value the user has selected. <code>UNINITIALIZED_VALUE</code>
1949 * implies the user has not yet made a choice, <code>null</code> means the
1950 * user closed the window with out choosing anything. Otherwise
1951 * the returned value will be one of the options defined in this
1952 * object.
1953 *
1954 * @return the <code>Object</code> chosen by the user,
1955 * <code>UNINITIALIZED_VALUE</code>
1956 * if the user has not yet made a choice, or <code>null</code> if
1957 * the user closed the window without making a choice
1958 *
1959 * @see #setValue
1960 */
1961 public Object getValue() {
1962 return value;
1963 }
1964
1965 /**
1966 * Sets the options this pane displays. If an element in
1967 * <code>newOptions</code> is a <code>Component</code>
1968 * it is added directly to the pane,
1969 * otherwise a button is created for the element.
1970 *
1971 * @param newOptions an array of <code>Objects</code> that create the
1972 * buttons the user can click on, or arbitrary
1973 * <code>Components</code> to add to the pane
1974 *
1975 * @see #getOptions
1976 * @beaninfo
1977 * bound: true
1978 * description: The option pane's options objects.
1979 */
1980 public void setOptions(Object[] newOptions) {
1981 Object[] oldOptions = options;
1982
1983 options = newOptions;
1984 firePropertyChange(OPTIONS_PROPERTY, oldOptions, options);
1985 }
1986
1987 /**
1988 * Returns the choices the user can make.
1989 * @return the array of <code>Objects</code> that give the user's choices
1990 *
1991 * @see #setOptions
1992 */
1993 public Object[] getOptions() {
1994 if(options != null) {
1995 int optionCount = options.length;
1996 Object[] retOptions = new Object[optionCount];
1997
1998 System.arraycopy(options, 0, retOptions, 0, optionCount);
1999 return retOptions;
2000 }
2001 return options;
2002 }
2003
2004 /**
2005 * Sets the initial value that is to be enabled -- the
2006 * <code>Component</code>
2007 * that has the focus when the pane is initially displayed.
2008 *
2009 * @param newInitialValue the <code>Object</code> that gets the initial
2010 * keyboard focus
2011 *
2012 * @see #getInitialValue
2013 * @beaninfo
2014 * preferred: true
2015 * bound: true
2016 * description: The option pane's initial value object.
2017 */
2018 public void setInitialValue(Object newInitialValue) {
2019 Object oldIV = initialValue;
2020
2021 initialValue = newInitialValue;
2022 firePropertyChange(INITIAL_VALUE_PROPERTY, oldIV, initialValue);
2023 }
2024
2025 /**
2026 * Returns the initial value.
2027 *
2028 * @return the <code>Object</code> that gets the initial keyboard focus
2029 *
2030 * @see #setInitialValue
2031 */
2032 public Object getInitialValue() {
2033 return initialValue;
2034 }
2035
2036 /**
2037 * Sets the option pane's message type.
2038 * The message type is used by the Look and Feel to determine the
2039 * icon to display (if not supplied) as well as potentially how to
2040 * lay out the <code>parentComponent</code>.
2041 * @param newType an integer specifying the kind of message to display:
2042 * <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>,
2043 * <code>WARNING_MESSAGE</code>,
2044 * <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code>
2045 * @exception RuntimeException if <code>newType</code> is not one of the
2046 * legal values listed above
2047
2048 * @see #getMessageType
2049 * @beaninfo
2050 * preferred: true
2051 * bound: true
2052 * description: The option pane's message type.
2053 */
2054 public void setMessageType(int newType) {
2055 checkMessageType(newType);
2056 int oldType = messageType;
2057 messageType = newType;
2058 firePropertyChange(MESSAGE_TYPE_PROPERTY, oldType, messageType);
2059 }
2060
2061 private static void checkMessageType(int newType){
2062 if(newType != ERROR_MESSAGE && newType != INFORMATION_MESSAGE &&
2063 newType != WARNING_MESSAGE && newType != QUESTION_MESSAGE &&
2064 newType != PLAIN_MESSAGE)
2065 throw new RuntimeException("JOptionPane: type must be one of"
2066 + " JOptionPane.ERROR_MESSAGE,"
2067 + " JOptionPane.INFORMATION_MESSAGE,"
2068 + " JOptionPane.WARNING_MESSAGE,"
2069 + " JOptionPane.QUESTION_MESSAGE"
2070 + " or JOptionPane.PLAIN_MESSAGE");
2071 }
2072
2073 /**
2078 * @see #setMessageType
2079 */
2080 public int getMessageType() {
2081 return messageType;
2082 }
2083
2084 /**
2085 * Sets the options to display.
2086 * The option type is used by the Look and Feel to
2087 * determine what buttons to show (unless options are supplied).
2088 * @param newType an integer specifying the options the {@literal L&F} is to display:
2089 * <code>DEFAULT_OPTION</code>,
2090 * <code>YES_NO_OPTION</code>,
2091 * <code>YES_NO_CANCEL_OPTION</code>,
2092 * or <code>OK_CANCEL_OPTION</code>
2093 * @exception RuntimeException if <code>newType</code> is not one of
2094 * the legal values listed above
2095 *
2096 * @see #getOptionType
2097 * @see #setOptions
2098 * @beaninfo
2099 * preferred: true
2100 * bound: true
2101 * description: The option pane's option type.
2102 */
2103 public void setOptionType(int newType) {
2104 checkOptionType(newType);
2105 int oldType = optionType;
2106 optionType = newType;
2107 firePropertyChange(OPTION_TYPE_PROPERTY, oldType, optionType);
2108 }
2109
2110 private static void checkOptionType(int newType) {
2111 if (newType != DEFAULT_OPTION && newType != YES_NO_OPTION
2112 && newType != YES_NO_CANCEL_OPTION
2113 && newType != OK_CANCEL_OPTION) {
2114 throw new RuntimeException("JOptionPane: option type must be one of"
2115 + " JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION,"
2116 + " JOptionPane.YES_NO_CANCEL_OPTION"
2117 + " or JOptionPane.OK_CANCEL_OPTION");
2118 }
2119 }
2120
2121 /**
2122 * Returns the type of options that are displayed.
2130 }
2131
2132 /**
2133 * Sets the input selection values for a pane that provides the user
2134 * with a list of items to choose from. (The UI provides a widget
2135 * for choosing one of the values.) A <code>null</code> value
2136 * implies the user can input whatever they wish, usually by means
2137 * of a <code>JTextField</code>.
2138 * <p>
2139 * Sets <code>wantsInput</code> to true. Use
2140 * <code>setInitialSelectionValue</code> to specify the initially-chosen
2141 * value. After the pane as been enabled, <code>inputValue</code> is
2142 * set to the value the user has selected.
2143 * @param newValues an array of <code>Objects</code> the user to be
2144 * displayed
2145 * (usually in a list or combo-box) from which
2146 * the user can make a selection
2147 * @see #setWantsInput
2148 * @see #setInitialSelectionValue
2149 * @see #getSelectionValues
2150 * @beaninfo
2151 * bound: true
2152 * description: The option pane's selection values.
2153 */
2154 public void setSelectionValues(Object[] newValues) {
2155 Object[] oldValues = selectionValues;
2156
2157 selectionValues = newValues;
2158 firePropertyChange(SELECTION_VALUES_PROPERTY, oldValues, newValues);
2159 if(selectionValues != null)
2160 setWantsInput(true);
2161 }
2162
2163 /**
2164 * Returns the input selection values.
2165 *
2166 * @return the array of <code>Objects</code> the user can select
2167 * @see #setSelectionValues
2168 */
2169 public Object[] getSelectionValues() {
2170 return selectionValues;
2171 }
2172
2173 /**
2174 * Sets the input value that is initially displayed as selected to the user.
2175 * Only used if <code>wantsInput</code> is true.
2176 * @param newValue the initially selected value
2177 * @see #setSelectionValues
2178 * @see #getInitialSelectionValue
2179 * @beaninfo
2180 * bound: true
2181 * description: The option pane's initial selection value object.
2182 */
2183 public void setInitialSelectionValue(Object newValue) {
2184 Object oldValue = initialSelectionValue;
2185
2186 initialSelectionValue = newValue;
2187 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, oldValue,
2188 newValue);
2189 }
2190
2191 /**
2192 * Returns the input value that is displayed as initially selected to the user.
2193 *
2194 * @return the initially selected value
2195 * @see #setInitialSelectionValue
2196 * @see #setSelectionValues
2197 */
2198 public Object getInitialSelectionValue() {
2199 return initialSelectionValue;
2200 }
2201
2202 /**
2203 * Sets the input value that was selected or input by the user.
2204 * Only used if <code>wantsInput</code> is true. Note that this method
2205 * is invoked internally by the option pane (in response to user action)
2206 * and should generally not be called by client programs. To set the
2207 * input value initially displayed as selected to the user, use
2208 * <code>setInitialSelectionValue</code>.
2209 *
2210 * @param newValue the <code>Object</code> used to set the
2211 * value that the user specified (usually in a text field)
2212 * @see #setSelectionValues
2213 * @see #setInitialSelectionValue
2214 * @see #setWantsInput
2215 * @see #getInputValue
2216 * @beaninfo
2217 * preferred: true
2218 * bound: true
2219 * description: The option pane's input value object.
2220 */
2221 public void setInputValue(Object newValue) {
2222 Object oldValue = inputValue;
2223
2224 inputValue = newValue;
2225 firePropertyChange(INPUT_VALUE_PROPERTY, oldValue, newValue);
2226 }
2227
2228 /**
2229 * Returns the value the user has input, if <code>wantsInput</code>
2230 * is true.
2231 *
2232 * @return the <code>Object</code> the user specified,
2233 * if it was one of the objects, or a
2234 * <code>String</code> if it was a value typed into a
2235 * field
2236 * @see #setSelectionValues
2237 * @see #setWantsInput
2238 * @see #setInputValue
2239 */
2240 public Object getInputValue() {
2241 return inputValue;
2242 }
2243
2244 /**
2245 * Returns the maximum number of characters to place on a line in a
2246 * message. Default is to return <code>Integer.MAX_VALUE</code>.
2247 * The value can be
2248 * changed by overriding this method in a subclass.
2249 *
2250 * @return an integer giving the maximum number of characters on a line
2251 */
2252 public int getMaxCharactersPerLineCount() {
2253 return Integer.MAX_VALUE;
2254 }
2255
2256 /**
2257 * Sets the <code>wantsInput</code> property.
2258 * If <code>newValue</code> is true, an input component
2259 * (such as a text field or combo box) whose parent is
2260 * <code>parentComponent</code> is provided to
2261 * allow the user to input a value. If <code>getSelectionValues</code>
2262 * returns a non-<code>null</code> array, the input value is one of the
2263 * objects in that array. Otherwise the input value is whatever
2264 * the user inputs.
2265 * <p>
2266 * This is a bound property.
2267 *
2268 * @param newValue if true, an input component whose parent is {@code parentComponent}
2269 * is provided to allow the user to input a value.
2270 * @see #setSelectionValues
2271 * @see #setInputValue
2272 * @beaninfo
2273 * preferred: true
2274 * bound: true
2275 * description: Flag which allows the user to input a value.
2276 */
2277 public void setWantsInput(boolean newValue) {
2278 boolean oldValue = wantsInput;
2279
2280 wantsInput = newValue;
2281 firePropertyChange(WANTS_INPUT_PROPERTY, oldValue, newValue);
2282 }
2283
2284 /**
2285 * Returns the value of the <code>wantsInput</code> property.
2286 *
2287 * @return true if an input component will be provided
2288 * @see #setWantsInput
2289 */
2290 public boolean getWantsInput() {
2291 return wantsInput;
2292 }
2293
2294 /**
2295 * Requests that the initial value be selected, which will set
2296 * focus to the initial value. This method
2506 ",icon=" + iconString +
2507 ",initialValue=" + initialValueString +
2508 ",message=" + messageString +
2509 ",messageType=" + messageTypeString +
2510 ",optionType=" + optionTypeString +
2511 ",wantsInput=" + wantsInputString;
2512 }
2513
2514 ///////////////////
2515 // Accessibility support
2516 ///////////////////
2517
2518 /**
2519 * Returns the <code>AccessibleContext</code> associated with this JOptionPane.
2520 * For option panes, the <code>AccessibleContext</code> takes the form of an
2521 * <code>AccessibleJOptionPane</code>.
2522 * A new <code>AccessibleJOptionPane</code> instance is created if necessary.
2523 *
2524 * @return an AccessibleJOptionPane that serves as the
2525 * AccessibleContext of this AccessibleJOptionPane
2526 * @beaninfo
2527 * expert: true
2528 * description: The AccessibleContext associated with this option pane
2529 */
2530 public AccessibleContext getAccessibleContext() {
2531 if (accessibleContext == null) {
2532 accessibleContext = new AccessibleJOptionPane();
2533 }
2534 return accessibleContext;
2535 }
2536
2537 /**
2538 * This class implements accessibility support for the
2539 * <code>JOptionPane</code> class. It provides an implementation of the
2540 * Java Accessibility API appropriate to option pane user-interface
2541 * elements.
2542 * <p>
2543 * <strong>Warning:</strong>
2544 * Serialized objects of this class will not be compatible with
2545 * future Swing releases. The current serialization support is
2546 * appropriate for short term storage or RMI between applications running
2547 * the same version of Swing. As of 1.4, support for long term storage
2548 * of all JavaBeans™
2549 * has been added to the <code>java.beans</code> package.
|
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package javax.swing;
26
27 import java.awt.BorderLayout;
28 import java.awt.Component;
29 import java.awt.Container;
30 import java.awt.Dialog;
31 import java.awt.Dimension;
32 import java.awt.KeyboardFocusManager;
33 import java.awt.Frame;
34 import java.awt.Point;
35 import java.awt.HeadlessException;
36 import java.awt.Window;
37 import java.beans.JavaBean;
38 import java.beans.BeanProperty;
39 import java.beans.PropertyChangeEvent;
40 import java.beans.PropertyChangeListener;
41 import java.awt.event.WindowListener;
42 import java.awt.event.WindowAdapter;
43 import java.awt.event.WindowEvent;
44 import java.awt.event.ComponentAdapter;
45 import java.awt.event.ComponentEvent;
46 import java.io.IOException;
47 import java.io.InvalidObjectException;
48 import java.io.ObjectInputStream;
49 import java.io.ObjectOutputStream;
50 import java.io.Serializable;
51 import java.util.Vector;
52 import javax.swing.plaf.OptionPaneUI;
53 import javax.swing.event.InternalFrameEvent;
54 import javax.swing.event.InternalFrameAdapter;
55 import javax.accessibility.*;
56 import static javax.swing.ClientPropertyKey.PopupFactory_FORCE_HEAVYWEIGHT_POPUP;
57 import sun.awt.AWTAccessor;
58
285 * }
286 * return CLOSED_OPTION;
287 * </pre>
288 * <p>
289 * <strong>Warning:</strong> Swing is not thread safe. For more
290 * information see <a
291 * href="package-summary.html#threading">Swing's Threading
292 * Policy</a>.
293 * <p>
294 * <strong>Warning:</strong>
295 * Serialized objects of this class will not be compatible with
296 * future Swing releases. The current serialization support is
297 * appropriate for short term storage or RMI between applications running
298 * the same version of Swing. As of 1.4, support for long term storage
299 * of all JavaBeans™
300 * has been added to the <code>java.beans</code> package.
301 * Please see {@link java.beans.XMLEncoder}.
302 *
303 * @see JInternalFrame
304 *
305 * @author James Gosling
306 * @author Scott Violet
307 * @since 1.2
308 */
309 @JavaBean(defaultProperty = "UI", description = "A component which implements standard dialog box controls.")
310 @SwingContainer
311 @SuppressWarnings("serial") // Same-version serialization only
312 public class JOptionPane extends JComponent implements Accessible
313 {
314 /**
315 * @see #getUIClassID
316 * @see #readObject
317 */
318 private static final String uiClassID = "OptionPaneUI";
319
320 /**
321 * Indicates that the user has not yet selected a value.
322 */
323 public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
324
325 //
326 // Option types
327 //
328
329 /**
330 * Type meaning Look and Feel should not supply any options -- only
1808 */
1809 public JOptionPane(Object message, int messageType, int optionType,
1810 Icon icon, Object[] options, Object initialValue) {
1811
1812 this.message = message;
1813 this.options = options;
1814 this.initialValue = initialValue;
1815 this.icon = icon;
1816 setMessageType(messageType);
1817 setOptionType(optionType);
1818 value = UNINITIALIZED_VALUE;
1819 inputValue = UNINITIALIZED_VALUE;
1820 updateUI();
1821 }
1822
1823 /**
1824 * Sets the UI object which implements the {@literal L&F} for this component.
1825 *
1826 * @param ui the <code>OptionPaneUI</code> {@literal L&F} object
1827 * @see UIDefaults#getUI
1828 */
1829 @BeanProperty(hidden = true, description
1830 = "The UI object that implements the optionpane's LookAndFeel")
1831 public void setUI(OptionPaneUI ui) {
1832 if (this.ui != ui) {
1833 super.setUI(ui);
1834 invalidate();
1835 }
1836 }
1837
1838 /**
1839 * Returns the UI object which implements the {@literal L&F} for this component.
1840 *
1841 * @return the <code>OptionPaneUI</code> object
1842 */
1843 public OptionPaneUI getUI() {
1844 return (OptionPaneUI)ui;
1845 }
1846
1847 /**
1848 * Notification from the <code>UIManager</code> that the {@literal L&F} has changed.
1849 * Replaces the current UI object with the latest version from the
1850 * <code>UIManager</code>.
1851 *
1852 * @see JComponent#updateUI
1853 */
1854 public void updateUI() {
1855 setUI((OptionPaneUI)UIManager.getUI(this));
1856 }
1857
1858
1859 /**
1860 * Returns the name of the UI class that implements the
1861 * {@literal L&F} for this component.
1862 *
1863 * @return the string "OptionPaneUI"
1864 * @see JComponent#getUIClassID
1865 * @see UIDefaults#getUI
1866 */
1867 @BeanProperty(bound = false)
1868 public String getUIClassID() {
1869 return uiClassID;
1870 }
1871
1872
1873 /**
1874 * Sets the option pane's message-object.
1875 * @param newMessage the <code>Object</code> to display
1876 * @see #getMessage
1877 */
1878 @BeanProperty(preferred = true, description
1879 = "The optionpane's message object.")
1880 public void setMessage(Object newMessage) {
1881 Object oldMessage = message;
1882
1883 message = newMessage;
1884 firePropertyChange(MESSAGE_PROPERTY, oldMessage, message);
1885 }
1886
1887 /**
1888 * Returns the message-object this pane displays.
1889 * @see #setMessage
1890 *
1891 * @return the <code>Object</code> that is displayed
1892 */
1893 public Object getMessage() {
1894 return message;
1895 }
1896
1897 /**
1898 * Sets the icon to display. If non-<code>null</code>, the look and feel
1899 * does not provide an icon.
1900 * @param newIcon the <code>Icon</code> to display
1901 *
1902 * @see #getIcon
1903 */
1904 @BeanProperty(preferred = true, description
1905 = "The option pane's type icon.")
1906 public void setIcon(Icon newIcon) {
1907 Object oldIcon = icon;
1908
1909 icon = newIcon;
1910 firePropertyChange(ICON_PROPERTY, oldIcon, icon);
1911 }
1912
1913 /**
1914 * Returns the icon this pane displays.
1915 * @return the <code>Icon</code> that is displayed
1916 *
1917 * @see #setIcon
1918 */
1919 public Icon getIcon() {
1920 return icon;
1921 }
1922
1923 /**
1924 * Sets the value the user has chosen.
1925 * @param newValue the chosen value
1926 *
1927 * @see #getValue
1928 */
1929 @BeanProperty(preferred = true, description
1930 = "The option pane's value object.")
1931 public void setValue(Object newValue) {
1932 Object oldValue = value;
1933
1934 value = newValue;
1935 firePropertyChange(VALUE_PROPERTY, oldValue, value);
1936 }
1937
1938 /**
1939 * Returns the value the user has selected. <code>UNINITIALIZED_VALUE</code>
1940 * implies the user has not yet made a choice, <code>null</code> means the
1941 * user closed the window with out choosing anything. Otherwise
1942 * the returned value will be one of the options defined in this
1943 * object.
1944 *
1945 * @return the <code>Object</code> chosen by the user,
1946 * <code>UNINITIALIZED_VALUE</code>
1947 * if the user has not yet made a choice, or <code>null</code> if
1948 * the user closed the window without making a choice
1949 *
1950 * @see #setValue
1951 */
1952 public Object getValue() {
1953 return value;
1954 }
1955
1956 /**
1957 * Sets the options this pane displays. If an element in
1958 * <code>newOptions</code> is a <code>Component</code>
1959 * it is added directly to the pane,
1960 * otherwise a button is created for the element.
1961 *
1962 * @param newOptions an array of <code>Objects</code> that create the
1963 * buttons the user can click on, or arbitrary
1964 * <code>Components</code> to add to the pane
1965 *
1966 * @see #getOptions
1967 */
1968 @BeanProperty(description
1969 = "The option pane's options objects.")
1970 public void setOptions(Object[] newOptions) {
1971 Object[] oldOptions = options;
1972
1973 options = newOptions;
1974 firePropertyChange(OPTIONS_PROPERTY, oldOptions, options);
1975 }
1976
1977 /**
1978 * Returns the choices the user can make.
1979 * @return the array of <code>Objects</code> that give the user's choices
1980 *
1981 * @see #setOptions
1982 */
1983 public Object[] getOptions() {
1984 if(options != null) {
1985 int optionCount = options.length;
1986 Object[] retOptions = new Object[optionCount];
1987
1988 System.arraycopy(options, 0, retOptions, 0, optionCount);
1989 return retOptions;
1990 }
1991 return options;
1992 }
1993
1994 /**
1995 * Sets the initial value that is to be enabled -- the
1996 * <code>Component</code>
1997 * that has the focus when the pane is initially displayed.
1998 *
1999 * @param newInitialValue the <code>Object</code> that gets the initial
2000 * keyboard focus
2001 *
2002 * @see #getInitialValue
2003 */
2004 @BeanProperty(preferred = true, description
2005 = "The option pane's initial value object.")
2006 public void setInitialValue(Object newInitialValue) {
2007 Object oldIV = initialValue;
2008
2009 initialValue = newInitialValue;
2010 firePropertyChange(INITIAL_VALUE_PROPERTY, oldIV, initialValue);
2011 }
2012
2013 /**
2014 * Returns the initial value.
2015 *
2016 * @return the <code>Object</code> that gets the initial keyboard focus
2017 *
2018 * @see #setInitialValue
2019 */
2020 public Object getInitialValue() {
2021 return initialValue;
2022 }
2023
2024 /**
2025 * Sets the option pane's message type.
2026 * The message type is used by the Look and Feel to determine the
2027 * icon to display (if not supplied) as well as potentially how to
2028 * lay out the <code>parentComponent</code>.
2029 * @param newType an integer specifying the kind of message to display:
2030 * <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>,
2031 * <code>WARNING_MESSAGE</code>,
2032 * <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code>
2033 * @exception RuntimeException if <code>newType</code> is not one of the
2034 * legal values listed above
2035
2036 * @see #getMessageType
2037 */
2038 @BeanProperty(preferred = true, description
2039 = "The option pane's message type.")
2040 public void setMessageType(int newType) {
2041 checkMessageType(newType);
2042 int oldType = messageType;
2043 messageType = newType;
2044 firePropertyChange(MESSAGE_TYPE_PROPERTY, oldType, messageType);
2045 }
2046
2047 private static void checkMessageType(int newType){
2048 if(newType != ERROR_MESSAGE && newType != INFORMATION_MESSAGE &&
2049 newType != WARNING_MESSAGE && newType != QUESTION_MESSAGE &&
2050 newType != PLAIN_MESSAGE)
2051 throw new RuntimeException("JOptionPane: type must be one of"
2052 + " JOptionPane.ERROR_MESSAGE,"
2053 + " JOptionPane.INFORMATION_MESSAGE,"
2054 + " JOptionPane.WARNING_MESSAGE,"
2055 + " JOptionPane.QUESTION_MESSAGE"
2056 + " or JOptionPane.PLAIN_MESSAGE");
2057 }
2058
2059 /**
2064 * @see #setMessageType
2065 */
2066 public int getMessageType() {
2067 return messageType;
2068 }
2069
2070 /**
2071 * Sets the options to display.
2072 * The option type is used by the Look and Feel to
2073 * determine what buttons to show (unless options are supplied).
2074 * @param newType an integer specifying the options the {@literal L&F} is to display:
2075 * <code>DEFAULT_OPTION</code>,
2076 * <code>YES_NO_OPTION</code>,
2077 * <code>YES_NO_CANCEL_OPTION</code>,
2078 * or <code>OK_CANCEL_OPTION</code>
2079 * @exception RuntimeException if <code>newType</code> is not one of
2080 * the legal values listed above
2081 *
2082 * @see #getOptionType
2083 * @see #setOptions
2084 */
2085 @BeanProperty(preferred = true, description
2086 = "The option pane's option type.")
2087 public void setOptionType(int newType) {
2088 checkOptionType(newType);
2089 int oldType = optionType;
2090 optionType = newType;
2091 firePropertyChange(OPTION_TYPE_PROPERTY, oldType, optionType);
2092 }
2093
2094 private static void checkOptionType(int newType) {
2095 if (newType != DEFAULT_OPTION && newType != YES_NO_OPTION
2096 && newType != YES_NO_CANCEL_OPTION
2097 && newType != OK_CANCEL_OPTION) {
2098 throw new RuntimeException("JOptionPane: option type must be one of"
2099 + " JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION,"
2100 + " JOptionPane.YES_NO_CANCEL_OPTION"
2101 + " or JOptionPane.OK_CANCEL_OPTION");
2102 }
2103 }
2104
2105 /**
2106 * Returns the type of options that are displayed.
2114 }
2115
2116 /**
2117 * Sets the input selection values for a pane that provides the user
2118 * with a list of items to choose from. (The UI provides a widget
2119 * for choosing one of the values.) A <code>null</code> value
2120 * implies the user can input whatever they wish, usually by means
2121 * of a <code>JTextField</code>.
2122 * <p>
2123 * Sets <code>wantsInput</code> to true. Use
2124 * <code>setInitialSelectionValue</code> to specify the initially-chosen
2125 * value. After the pane as been enabled, <code>inputValue</code> is
2126 * set to the value the user has selected.
2127 * @param newValues an array of <code>Objects</code> the user to be
2128 * displayed
2129 * (usually in a list or combo-box) from which
2130 * the user can make a selection
2131 * @see #setWantsInput
2132 * @see #setInitialSelectionValue
2133 * @see #getSelectionValues
2134 */
2135 @BeanProperty(description
2136 = "The option pane's selection values.")
2137 public void setSelectionValues(Object[] newValues) {
2138 Object[] oldValues = selectionValues;
2139
2140 selectionValues = newValues;
2141 firePropertyChange(SELECTION_VALUES_PROPERTY, oldValues, newValues);
2142 if(selectionValues != null)
2143 setWantsInput(true);
2144 }
2145
2146 /**
2147 * Returns the input selection values.
2148 *
2149 * @return the array of <code>Objects</code> the user can select
2150 * @see #setSelectionValues
2151 */
2152 public Object[] getSelectionValues() {
2153 return selectionValues;
2154 }
2155
2156 /**
2157 * Sets the input value that is initially displayed as selected to the user.
2158 * Only used if <code>wantsInput</code> is true.
2159 * @param newValue the initially selected value
2160 * @see #setSelectionValues
2161 * @see #getInitialSelectionValue
2162 */
2163 @BeanProperty(description
2164 = "The option pane's initial selection value object.")
2165 public void setInitialSelectionValue(Object newValue) {
2166 Object oldValue = initialSelectionValue;
2167
2168 initialSelectionValue = newValue;
2169 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, oldValue,
2170 newValue);
2171 }
2172
2173 /**
2174 * Returns the input value that is displayed as initially selected to the user.
2175 *
2176 * @return the initially selected value
2177 * @see #setInitialSelectionValue
2178 * @see #setSelectionValues
2179 */
2180 public Object getInitialSelectionValue() {
2181 return initialSelectionValue;
2182 }
2183
2184 /**
2185 * Sets the input value that was selected or input by the user.
2186 * Only used if <code>wantsInput</code> is true. Note that this method
2187 * is invoked internally by the option pane (in response to user action)
2188 * and should generally not be called by client programs. To set the
2189 * input value initially displayed as selected to the user, use
2190 * <code>setInitialSelectionValue</code>.
2191 *
2192 * @param newValue the <code>Object</code> used to set the
2193 * value that the user specified (usually in a text field)
2194 * @see #setSelectionValues
2195 * @see #setInitialSelectionValue
2196 * @see #setWantsInput
2197 * @see #getInputValue
2198 */
2199 @BeanProperty(preferred = true, description
2200 = "The option pane's input value object.")
2201 public void setInputValue(Object newValue) {
2202 Object oldValue = inputValue;
2203
2204 inputValue = newValue;
2205 firePropertyChange(INPUT_VALUE_PROPERTY, oldValue, newValue);
2206 }
2207
2208 /**
2209 * Returns the value the user has input, if <code>wantsInput</code>
2210 * is true.
2211 *
2212 * @return the <code>Object</code> the user specified,
2213 * if it was one of the objects, or a
2214 * <code>String</code> if it was a value typed into a
2215 * field
2216 * @see #setSelectionValues
2217 * @see #setWantsInput
2218 * @see #setInputValue
2219 */
2220 public Object getInputValue() {
2221 return inputValue;
2222 }
2223
2224 /**
2225 * Returns the maximum number of characters to place on a line in a
2226 * message. Default is to return <code>Integer.MAX_VALUE</code>.
2227 * The value can be
2228 * changed by overriding this method in a subclass.
2229 *
2230 * @return an integer giving the maximum number of characters on a line
2231 */
2232 @BeanProperty(bound = false)
2233 public int getMaxCharactersPerLineCount() {
2234 return Integer.MAX_VALUE;
2235 }
2236
2237 /**
2238 * Sets the <code>wantsInput</code> property.
2239 * If <code>newValue</code> is true, an input component
2240 * (such as a text field or combo box) whose parent is
2241 * <code>parentComponent</code> is provided to
2242 * allow the user to input a value. If <code>getSelectionValues</code>
2243 * returns a non-<code>null</code> array, the input value is one of the
2244 * objects in that array. Otherwise the input value is whatever
2245 * the user inputs.
2246 * <p>
2247 * This is a bound property.
2248 *
2249 * @param newValue if true, an input component whose parent is {@code parentComponent}
2250 * is provided to allow the user to input a value.
2251 * @see #setSelectionValues
2252 * @see #setInputValue
2253 */
2254 @BeanProperty(preferred = true, description
2255 = "Flag which allows the user to input a value.")
2256 public void setWantsInput(boolean newValue) {
2257 boolean oldValue = wantsInput;
2258
2259 wantsInput = newValue;
2260 firePropertyChange(WANTS_INPUT_PROPERTY, oldValue, newValue);
2261 }
2262
2263 /**
2264 * Returns the value of the <code>wantsInput</code> property.
2265 *
2266 * @return true if an input component will be provided
2267 * @see #setWantsInput
2268 */
2269 public boolean getWantsInput() {
2270 return wantsInput;
2271 }
2272
2273 /**
2274 * Requests that the initial value be selected, which will set
2275 * focus to the initial value. This method
2485 ",icon=" + iconString +
2486 ",initialValue=" + initialValueString +
2487 ",message=" + messageString +
2488 ",messageType=" + messageTypeString +
2489 ",optionType=" + optionTypeString +
2490 ",wantsInput=" + wantsInputString;
2491 }
2492
2493 ///////////////////
2494 // Accessibility support
2495 ///////////////////
2496
2497 /**
2498 * Returns the <code>AccessibleContext</code> associated with this JOptionPane.
2499 * For option panes, the <code>AccessibleContext</code> takes the form of an
2500 * <code>AccessibleJOptionPane</code>.
2501 * A new <code>AccessibleJOptionPane</code> instance is created if necessary.
2502 *
2503 * @return an AccessibleJOptionPane that serves as the
2504 * AccessibleContext of this AccessibleJOptionPane
2505 */
2506 @BeanProperty(bound = false, expert = true, description
2507 = "The AccessibleContext associated with this option pane")
2508 public AccessibleContext getAccessibleContext() {
2509 if (accessibleContext == null) {
2510 accessibleContext = new AccessibleJOptionPane();
2511 }
2512 return accessibleContext;
2513 }
2514
2515 /**
2516 * This class implements accessibility support for the
2517 * <code>JOptionPane</code> class. It provides an implementation of the
2518 * Java Accessibility API appropriate to option pane user-interface
2519 * elements.
2520 * <p>
2521 * <strong>Warning:</strong>
2522 * Serialized objects of this class will not be compatible with
2523 * future Swing releases. The current serialization support is
2524 * appropriate for short term storage or RMI between applications running
2525 * the same version of Swing. As of 1.4, support for long term storage
2526 * of all JavaBeans™
2527 * has been added to the <code>java.beans</code> package.
|