Dialog.java (constructor): Accept null title as per spec.
[gcc.git] / libjava / java / awt / FileDialog.java
1 /* FileDialog.java -- A filename selection dialog box
2 Copyright (C) 1999, 2000, 2001, 2002 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package java.awt;
40
41 import java.awt.peer.FileDialogPeer;
42 import java.awt.peer.DialogPeer;
43 import java.io.FilenameFilter;
44
45 /**
46 * This class implements a file selection dialog box widget.
47 *
48 * @author Aaron M. Renn (arenn@urbanophile.com)
49 * @author Tom Tromey <tromey@redhat.com>
50 */
51 public class FileDialog extends Dialog implements java.io.Serializable
52 {
53
54 /*
55 * Static Variables
56 */
57
58 /**
59 * Indicates that the purpose of the dialog is for opening a file.
60 */
61 public static final int LOAD = 0;
62
63 /**
64 * Indicates that the purpose of the dialog is for saving a file.
65 */
66 public static final int SAVE = 1;
67
68 // Serialization constant
69 private static final long serialVersionUID = 5035145889651310422L;
70
71 /*************************************************************************/
72
73 /*
74 * Instance Variables
75 */
76
77 /**
78 * @serial The directory for this file dialog.
79 */
80 private String dir;
81
82 /**
83 * @serial The filename for this file dialog
84 */
85 private String file;
86
87 /**
88 * @serial The filter for selecting filenames to display
89 */
90 private FilenameFilter filter;
91
92 /**
93 * @serial The mode of this dialog, either <code>LOAD</code> or
94 * <code>SAVE</code>.
95 */
96 private int mode;
97
98 /*************************************************************************/
99
100 /*
101 * Constructors
102 */
103
104 /**
105 * Initializes a new instance of <code>FileDialog</code> with the
106 * specified parent. This dialog will have no title and will be for
107 * loading a file.
108 *
109 * @param parent The parent frame for this dialog.
110 */
111 public
112 FileDialog(Frame parent)
113 {
114 this(parent, "", LOAD);
115 }
116
117 /*************************************************************************/
118
119 /**
120 * Initialized a new instance of <code>FileDialog</code> with the
121 * specified parent and title. This dialog will be for opening a file.
122 *
123 * @param parent The parent frame for this dialog.
124 * @param title The title for this dialog.
125 */
126 public
127 FileDialog(Frame parent, String title)
128 {
129 this(parent, title, LOAD);
130 }
131
132 /*************************************************************************/
133
134 /**
135 * Initialized a new instance of <code>FileDialog</code> with the
136 * specified parent, title, and mode.
137 *
138 * @param parent The parent frame for this dialog.
139 * @param title The title for this dialog.
140 * @param mode The mode of the dialog, either <code>LOAD</code> or
141 * <code>SAVE</code>.
142 *
143 * @exception IllegalArgumentException If an illegal file dialog mode
144 * is supplied.
145 */
146 public
147 FileDialog(Frame parent, String title, int mode)
148 {
149 super(parent, title, true);
150
151 if ((mode != LOAD) && (mode != SAVE))
152 throw new IllegalArgumentException (
153 "Mode argument must be either LOAD or SAVE");
154
155 setMode (mode);
156 }
157
158 /*************************************************************************/
159
160 /*
161 * Instance Methods
162 */
163
164 /**
165 * Returns the mode of this dialog, either <code>LOAD</code> or
166 * <code>SAVE</code>.
167 *
168 * @return The mode of this dialog.
169 */
170 public int
171 getMode()
172 {
173 return(mode);
174 }
175
176 /*************************************************************************/
177
178 /**
179 * Sets the mode of this dialog to either <code>LOAD</code> or
180 * <code>SAVE</code>. This method is only effective before the native
181 * peer is created.
182 *
183 * @param mode The new mode of this file dialog.
184 *
185 * @exception IllegalArgumentException If an illegal file dialog mode
186 * is supplied.
187 */
188 public void
189 setMode(int mode)
190 {
191 if ((mode != LOAD) && (mode != SAVE))
192 throw new IllegalArgumentException("Bad mode: " + mode);
193
194 this.mode = mode;
195 }
196
197 /*************************************************************************/
198
199 /**
200 * Returns the directory for this file dialog.
201 *
202 * @return The directory for this file dialog.
203 */
204 public String
205 getDirectory()
206 {
207 return(dir);
208 }
209
210 /*************************************************************************/
211
212 /**
213 * Sets the directory for this file dialog.
214 *
215 * @param dir The new directory for this file dialog.
216 */
217 public synchronized void
218 setDirectory(String dir)
219 {
220 this.dir = dir;
221 if (peer != null)
222 {
223 FileDialogPeer f = (FileDialogPeer) peer;
224 f.setDirectory (dir);
225 }
226 }
227
228 /*************************************************************************/
229
230 /**
231 * Returns the file that is selected in this dialog.
232 *
233 * @return The file that is selected in this dialog.
234 */
235 public String
236 getFile()
237 {
238 return(file);
239 }
240
241 /*************************************************************************/
242
243 /**
244 * Sets the selected file for this dialog.
245 *
246 * @param file The selected file for this dialog.
247 */
248 public synchronized void
249 setFile(String file)
250 {
251 this.file = file;
252 if (peer != null)
253 {
254 FileDialogPeer f = (FileDialogPeer) peer;
255 f.setFile (file);
256 }
257 }
258
259 /*************************************************************************/
260
261 /**
262 * Returns the filename filter being used by this dialog.
263 *
264 * @param The filename filter being used by this dialog.
265 */
266 public FilenameFilter
267 getFilenameFilter()
268 {
269 return(filter);
270 }
271
272 /*************************************************************************/
273
274 /**
275 * Sets the filename filter used by this dialog.
276 *
277 * @param filter The new filename filter for this file dialog box.
278 */
279 public synchronized void
280 setFilenameFilter(FilenameFilter filter)
281 {
282 this.filter = filter;
283 if (peer != null)
284 {
285 FileDialogPeer f = (FileDialogPeer) peer;
286 f.setFilenameFilter (filter);
287 }
288 }
289
290 /*************************************************************************/
291
292 /**
293 * Creates the native peer for this file dialog box.
294 */
295 public void
296 addNotify()
297 {
298 if (peer == null)
299 peer = getToolkit ().createFileDialog (this);
300 super.addNotify ();
301 }
302
303 /*************************************************************************/
304
305 /**
306 * Returns a debugging string for this object.
307 *
308 * @return A debugging string for this object.
309 */
310 protected String
311 paramString()
312 {
313 return ("dir=" + dir + ",file=" + file +
314 ",mode=" + mode + "," + super.paramString());
315 }
316
317 } // class FileDialog
318