001: /*=============================================================================
002: * Copyright Texas Instruments 2001. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.chimera;
020:
021: import javax.swing.*;
022: import java.awt.Component;
023:
024: /**
025: * Create a modal option-pane dialog that is contained in a <code>Dialog</code>.
026: * Developers should use this rather than <code>JOptionPane</code>. For example:
027: * <pre>
028: * OptionPane optionPane = new OptionPane( main.getWindowManager().getDialog("Input"),
029: * "Enter Some Input",
030: * OptionPane.QUESTION_MESSAGE,
031: * OptionPane.OK_CANCEL_OPTION );
032: *
033: * optionPane.setInitialSelectionValue("Foo");
034: *
035: * String result = (String)(optionPane.showModal());
036: * </pre>
037: *
038: * @author Rob Clark
039: * @version 0.1
040: */
041: public class OptionPane extends JOptionPane {
042: /**
043: */
044: private Dialog dialog;
045:
046: /**
047: * Class Constructor. Create a <code>PLAIN_MESSAGE</code> type dialog.
048: *
049: * @param dialog the dialog to realize the option-pane within
050: * @param message the message to display
051: */
052: public OptionPane(Dialog dialog, Object message) {
053: this (dialog, message, PLAIN_MESSAGE);
054: }
055:
056: /**
057: * Class Constructor. Create a dialog of the specified type with the default
058: * options.
059: *
060: * @param dialog the dialog to realize the option-pane within
061: * @param message the message to display
062: * @param messageType the type of the message to display
063: * @see #ERROR_MESSAGE message type
064: * @see #INFORMATION_MESSAGE message type
065: * @see #WARNING_MESSAGE message type
066: * @see #QUESTION_MESSAGE message type
067: * @see #PLAIN_MESSAGE message type
068: */
069: public OptionPane(Dialog dialog, Object message, int messageType) {
070: this (dialog, message, messageType, DEFAULT_OPTION);
071: }
072:
073: /**
074: * Class Constructor. Create dialog with no icon.
075: *
076: * @param dialog the dialog to realize the option-pane within
077: * @param message the message to display
078: * @param messageType the type of the message to display
079: * @param optionType the options to display
080: * @see #ERROR_MESSAGE message type
081: * @see #INFORMATION_MESSAGE message type
082: * @see #WARNING_MESSAGE message type
083: * @see #QUESTION_MESSAGE message type
084: * @see #PLAIN_MESSAGE message type
085: * @see #DEFAULT_OPTION option type
086: * @see #YES_NO_OPTION option type
087: * @see #YES_NO_CANCEL_OPTION option type
088: * @see #OK_CANCEL_OPTION option type
089: */
090: public OptionPane(Dialog dialog, Object message, int messageType,
091: int optionType) {
092: this (dialog, message, messageType, optionType, null);
093: }
094:
095: /**
096: * Class Constructor. Create dialog with specified icon.
097: *
098: * @param dialog the dialog to realize the option-pane within
099: * @param message the message to display
100: * @param messageType the type of the message to display
101: * @param optionType the options to display
102: * @param icon the icon image to display
103: * @see #ERROR_MESSAGE message type
104: * @see #INFORMATION_MESSAGE message type
105: * @see #WARNING_MESSAGE message type
106: * @see #QUESTION_MESSAGE message type
107: * @see #PLAIN_MESSAGE message type
108: * @see #DEFAULT_OPTION option type
109: * @see #YES_NO_OPTION option type
110: * @see #YES_NO_CANCEL_OPTION option type
111: * @see #OK_CANCEL_OPTION option type
112: */
113: public OptionPane(Dialog dialog, Object message, int messageType,
114: int optionType, Icon icon) {
115: this (dialog, message, messageType, optionType, icon, null);
116: }
117:
118: /**
119: * Class Constructor.
120: *
121: * @param dialog the dialog to realize the option-pane within
122: * @param message the message to display
123: * @param messageType the type of the message to display
124: * @param optionType the options to display
125: * @param icon the icon image to display
126: * @param options the choices the user can select
127: * @see #ERROR_MESSAGE message type
128: * @see #INFORMATION_MESSAGE message type
129: * @see #WARNING_MESSAGE message type
130: * @see #QUESTION_MESSAGE message type
131: * @see #PLAIN_MESSAGE message type
132: * @see #DEFAULT_OPTION option type
133: * @see #YES_NO_OPTION option type
134: * @see #YES_NO_CANCEL_OPTION option type
135: * @see #OK_CANCEL_OPTION option type
136: */
137: public OptionPane(Dialog dialog, Object message, int messageType,
138: int optionType, Icon icon, Object[] options) {
139: this (dialog, message, messageType, optionType, icon, options,
140: null);
141: }
142:
143: /**
144: * Class Constructor.
145: *
146: * @param dialog the dialog to realize the option-pane within
147: * @param message the message to display
148: * @param messageType the type of the message to display
149: * @param optionType the options to display
150: * @param icon the icon image to display
151: * @param options the choices the user can select
152: * @param initialVal the initially selected option
153: * @see #ERROR_MESSAGE message type
154: * @see #INFORMATION_MESSAGE message type
155: * @see #WARNING_MESSAGE message type
156: * @see #QUESTION_MESSAGE message type
157: * @see #PLAIN_MESSAGE message type
158: * @see #DEFAULT_OPTION option type
159: * @see #YES_NO_OPTION option type
160: * @see #YES_NO_CANCEL_OPTION option type
161: * @see #OK_CANCEL_OPTION option type
162: */
163: public OptionPane(Dialog dialog, Object message, int messageType,
164: int optionType, Icon icon, Object[] options,
165: Object initialVal) {
166: super (message, messageType, optionType, icon, options,
167: initialVal);
168:
169: this .dialog = dialog;
170:
171: if (((options != null) && (options.length > 0))
172: || (initialVal != null))
173: setWantsInput(true);
174: else
175: setWantsInput(false);
176:
177: dialog.getContentPane().add(this );
178: dialog.pack();
179: dialog.center();
180:
181: class CloseDialogPropertyChangeListener implements
182: java.beans.PropertyChangeListener {
183: private Dialog dialog;
184:
185: CloseDialogPropertyChangeListener(Dialog dialog) {
186: this .dialog = dialog;
187: }
188:
189: public void propertyChange(
190: java.beans.PropertyChangeEvent event) {
191: if (event.getSource() == OptionPane.this
192: && (event.getPropertyName().equals(
193: VALUE_PROPERTY) || event
194: .getPropertyName().equals(
195: INPUT_VALUE_PROPERTY))) {
196: dialog.setVisible(false);
197: dialog.dispose();
198: }
199: }
200: }
201:
202: addPropertyChangeListener(new CloseDialogPropertyChangeListener(
203: dialog));
204: }
205:
206: /**
207: * Show the option-pane dialog. Do not return until the dialog is closed.
208: *
209: * @return the input value
210: */
211: public Object showModal() {
212: dialog.setVisible(true);
213: dialog.showModal();
214:
215: Object value = getInputValue();
216:
217: dialog.dispose();
218:
219: if (value == UNINITIALIZED_VALUE)
220: return null;
221:
222: return value;
223: }
224: }
225:
226: /*
227: * Local Variables:
228: * tab-width: 2
229: * indent-tabs-mode: nil
230: * mode: java
231: * c-indentation-style: java
232: * eval: (c-set-offset 'substatement-open '0)
233: * c-basic-offset: 2
234: * End:
235: */
|