KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > awt > PopupDialog


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.awt;
32
33 import java.awt.*;
34 import java.awt.event.*;
35 import java.util.Vector JavaDoc;
36 import rcm.util.Win;
37
38 // Note: after creating a PopupDialog (like any other top-level window, it
39
// seems), the JDK 1.1 runtime won't exit by itself, even if the PopupDialog
40
// is properly disposed. Need to force it to exit using System.exit().
41

42 public class PopupDialog extends Dialog {
43
44     public static final int YES = 0;
45     public static final int OK = 0;
46     public static final int NO = 1;
47     public static final int CANCEL = 2;
48
49     Component parent;
50     int answer = -1;
51     String JavaDoc text;
52
53     TextField textfield;
54     Button okButton, noButton, cancelButton;
55
56     public static String JavaDoc ask (Component comp, String JavaDoc topic, String JavaDoc question, String JavaDoc defaultAnswer) {
57         PopupDialog d = new PopupDialog (comp, topic, true,
58                                          question, defaultAnswer,
59                                          "OK", null, "Cancel");
60         d.show ();
61         switch (d.getAnswer ()) {
62             case OK:
63                 return d.getText ();
64             default:
65                 return null;
66         }
67     }
68
69     public static String JavaDoc ask (Component comp, String JavaDoc topic, String JavaDoc question) {
70         return ask (comp, topic, question, "");
71     }
72
73     public static boolean okcancel (Component comp, String JavaDoc topic, String JavaDoc question) {
74         PopupDialog d = new PopupDialog (comp, topic, true,
75                                          question, null,
76                                          "OK", null, "Cancel");
77         d.show ();
78         return (d.getAnswer () == OK);
79     }
80
81     public static boolean yesno (Component comp, String JavaDoc topic, String JavaDoc question) {
82         PopupDialog d = new PopupDialog (comp, topic, true,
83                                          question, null,
84                                          "Yes", "No", null);
85         d.show ();
86         return (d.getAnswer () == YES);
87     }
88
89     public static int yesnocancel (Component comp, String JavaDoc topic, String JavaDoc question) {
90         PopupDialog d = new PopupDialog (comp, topic, true,
91                                          question, null,
92                                          "Yes", "No", "Cancel");
93         d.show ();
94         return d.getAnswer ();
95     }
96
97     public static void warn (Component comp, String JavaDoc topic, String JavaDoc message) {
98         PopupDialog d = new PopupDialog (comp, topic, true,
99                                          message, null,
100                                          "OK", null, null);
101         d.show ();
102     }
103
104     public static String JavaDoc currentDirectory = "";
105
106     public static String JavaDoc askFilename (Component comp, String JavaDoc topic,
107                                   String JavaDoc defaultFilename, boolean loading) {
108         try {
109             FileDialog fd = new FileDialog (Win.findFrame(comp),
110                                             topic,
111                                             loading ? FileDialog.LOAD : FileDialog.SAVE);
112
113             if (currentDirectory != null)
114                 fd.setDirectory (currentDirectory);
115             if (defaultFilename != null)
116                 fd.setFile (defaultFilename);
117
118             fd.show ();
119
120             String JavaDoc dir = fd.getDirectory();
121             String JavaDoc file = fd.getFile ();
122
123             if (dir == null || file == null)
124                 return null;
125
126             currentDirectory = dir;
127             return dir + file;
128         } catch (AWTError e) {
129             return ask (comp, topic, "Filename:", defaultFilename);
130         }
131     }
132
133     public static String JavaDoc askDirectory (Component comp, String JavaDoc topic,
134                                   String JavaDoc defaultFilename, boolean loading) {
135         try {
136             FileDialog fd = new FileDialog (Win.findFrame(comp),
137                                             topic,
138                                             loading ? FileDialog.LOAD : FileDialog.SAVE);
139
140             if (currentDirectory != null)
141                 fd.setDirectory (currentDirectory);
142             if (defaultFilename != null)
143                 fd.setFile (defaultFilename);
144
145             fd.show ();
146
147             String JavaDoc dir = fd.getDirectory();
148
149             if (dir != null)
150                 currentDirectory = dir;
151
152             return dir;
153         } catch (AWTError e) {
154             return ask (comp, topic, "Directory:", defaultFilename);
155         }
156     }
157
158
159     public PopupDialog (Component parent, String JavaDoc title, boolean modal) {
160         super (Win.findFrameOrMakeFrame (parent), title, modal);
161         this.parent = parent;
162     }
163     
164     public PopupDialog (Component parent, String JavaDoc title, boolean modal,
165                          String JavaDoc question, String JavaDoc initialEntry,
166                          String JavaDoc okOrYes, String JavaDoc no, String JavaDoc cancel) {
167         this (parent, title, modal);
168
169         if (parent != null)
170             setFont (parent.getFont ());
171
172         Panel middle = new Panel ();
173         add ("Center", BorderPanel.wrap (middle, 10, 10, 10, 5));
174         middle.setLayout (new BorderLayout ());
175         MultiLineLabel questionLabel = new MultiLineLabel (question, Label.LEFT);
176         middle.add ("Center", questionLabel);
177         if (initialEntry != null) {
178             textfield = new TextField (Math.max (40, initialEntry.length()+1));
179             middle.add ("South", textfield);
180             textfield.setText (initialEntry);
181             textfield.selectAll ();
182             textfield.addActionListener (new ActionListener () {
183                 public void actionPerformed (ActionEvent event) {
184                     answer = OK;
185                     close ();
186                 }
187             });
188         }
189
190         Panel bottom = new Panel ();
191         add ("South", bottom);
192
193         if (okOrYes != null) {
194             okButton = new Button (okOrYes);
195             okButton.addActionListener (new ActionListener () {
196                 public void actionPerformed (ActionEvent event) {
197                     answer = OK;
198                     close ();
199                 }
200             });
201             bottom.add (okButton);
202         }
203
204         if (no != null) {
205             noButton = new Button (no);
206             noButton.addActionListener (new ActionListener () {
207                 public void actionPerformed (ActionEvent event) {
208                     answer = NO;
209                     close ();
210                 }
211             });
212             bottom.add (noButton);
213         }
214
215         if (cancel != null) {
216             cancelButton = new Button (cancel);
217             cancelButton.addActionListener (new ActionListener () {
218                 public void actionPerformed (ActionEvent event) {
219                     answer = CANCEL;
220                     close ();
221                 }
222             });
223             bottom.add (cancelButton);
224         }
225
226         addWindowListener (new WindowAdapter () {
227             public void windowClosing (WindowEvent event) {
228                 if (cancelButton != null) {
229                     answer = CANCEL;
230                     close ();
231                 }
232                 else if (noButton == null && cancelButton == null) {
233                     answer = OK;
234                     close ();
235                 }
236             }
237         });
238
239 // if (System.getProperty ("java.vendor").startsWith ("Netscape")) {
240
// // pack() doesn't work under Netscape!
241
// Dimension d = questionLabel.preferredSize();
242
// resize (Math.max (100, d.width), 100 + d.height);
243
// }
244
// else
245
pack ();
246     }
247
248     public static void centerWindow (Window window, Component ref) {
249         Dimension size = window.getSize();
250         Dimension refSize = (ref != null) ? ref.getSize() : Toolkit.getDefaultToolkit().getScreenSize();
251         Point origin = (ref != null) ? ref.getLocationOnScreen () : new Point (0, 0);
252         
253         if (refSize != null) {
254             int x = Math.max (0, origin.x + (refSize.width - size.width) / 2);
255             int y = Math.max (0, origin.y + (refSize.height - size.height) / 2);
256             window.setLocation (x, y);
257         }
258     }
259     
260     public void show () {
261         centerWindow (this, parent);
262         super.show ();
263         if (textfield != null)
264             textfield.requestFocus ();
265     }
266
267     public int getAnswer () {
268         return answer;
269     }
270
271     public void setAnswer (int answer) {
272         this.answer = answer;
273     }
274
275     public String JavaDoc getText () {
276         return text;
277     }
278
279     Vector JavaDoc listeners = new Vector JavaDoc ();
280
281     public synchronized void addPopupListener (PopupListener listener) {
282         listeners.addElement (listener);
283     }
284
285     public synchronized void removePopupListener (PopupListener listener) {
286         listeners.removeElement (listener);
287     }
288
289     public synchronized void close () {
290         text = (answer == OK && textfield != null)
291                  ? textfield.getText () : null;
292
293         dispose ();
294         if (parent == null)
295             ((Frame)getParent()).dispose ();
296         else
297             parent.requestFocus ();
298
299         if (answer != -1) {
300             PopupEvent e = new PopupEvent (answer, text);
301             for (int i=0; i<listeners.size (); ++i) {
302                 PopupListener p = (PopupListener) (listeners.elementAt (i));
303                 switch (e.getID ()) {
304                     case YES:
305                         p.yes (e);
306                         break;
307                     case NO:
308                         p.no (e);
309                         break;
310                     case CANCEL:
311                         p.cancel (e);
312                         break;
313                 }
314             }
315         }
316
317         try {
318             finalize ();
319         } catch (Throwable JavaDoc t) {
320             throw new RuntimeException JavaDoc (t.toString());
321         }
322     }
323
324
325     /*
326      * Testing
327      *
328      */

329     public static void main (String JavaDoc[] args) {
330         String JavaDoc name = ask (null, "Enter Name", "Enter your full name:");
331
332         if (name != null) {
333             switch (yesnocancel (null, "Confirm",
334                                  "Hello, " + name + ".\nIs this your name?")) {
335                 case PopupDialog.YES:
336                     if (okcancel (null, "Thanks",
337                                   "Great!\nDo you want to play a game?")) {
338                         warn (null, "Sorry", "Too bad, my mommy won't let me out of the house.");
339                     }
340                     break;
341
342                 case PopupDialog.NO:
343                     warn (null, "D'oh", "Oops. My bad.");
344                     break;
345             }
346         }
347
348         System.exit (0);
349     }
350
351 }
352
Popular Tags