1 /* FileDialog.java -- A filename selection dialog box
2    Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005  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. */
37 
38 
39 package java.awt;
40 
41 import java.awt.peer.FileDialogPeer;
42 import java.io.FilenameFilter;
43 import java.io.Serializable;
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 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  * The number used to generate the name returned by getName.
100  */
101 private static transient long next_file_dialog_number;
102 
103 /*************************************************************************/
104 
105 /*
106  * Constructors
107  */
108 
109   /**
110    * Initializes a new instance of <code>FileDialog</code> with the specified
111    * parent. This dialog will have no title and will be for loading a file.
112    *
113    * @param parent The parent dialog for this.
114    *
115    * @since 1.5
116    */
FileDialog(Dialog parent)117   public FileDialog(Dialog parent)
118   {
119     this(parent, "", LOAD);
120   }
121 
122   /**
123    * Initialized a new instance of <code>FileDialog</code> with the
124    * specified parent and title.  This dialog will be for opening a file.
125    *
126    * @param parent The parent dialog for this.
127    * @param title The title for this dialog.
128    *
129    * @since 1.5
130    */
FileDialog(Dialog parent, String title)131   public FileDialog(Dialog parent, String title)
132   {
133     this(parent, title, LOAD);
134   }
135 
136   /**
137    * Initialized a new instance of <code>FileDialog</code> with the specified
138    * parent, title, and mode.
139    *
140    * @param parent The parent dialog for this.
141    * @param title The title for this dialog.
142    * @param mode The mode of the dialog, either <code>LOAD</code> or
143    *          <code>SAVE</code>.
144    * @throws IllegalArgumentException - if illegal mode, if
145    *           GraphicsEnvironment.isHeadless or if parent is null.
146    *
147    * @since 1.5
148    */
FileDialog(Dialog parent, String title, int mode)149   public FileDialog(Dialog parent, String title, int mode)
150   {
151     super(parent, title, true);
152 
153     // Other IllegalArgumentException cases are taken care of in Window.java
154     if (mode != LOAD && mode != SAVE)
155       throw new IllegalArgumentException (
156         "Mode argument must be either LOAD or SAVE");
157 
158     setMode(mode);
159   }
160 
161 /**
162   * Initializes a new instance of <code>FileDialog</code> with the
163   * specified parent.  This dialog will have no title and will be for
164   * loading a file.
165   *
166   * @param parent The parent frame for this dialog.
167   */
168 public
FileDialog(Frame parent)169 FileDialog(Frame parent)
170 {
171   this(parent, "", LOAD);
172 }
173 
174 /*************************************************************************/
175 
176 /**
177   * Initialized a new instance of <code>FileDialog</code> with the
178   * specified parent and title.  This dialog will be for opening a file.
179   *
180   * @param parent The parent frame for this dialog.
181   * @param title The title for this dialog.
182   */
183 public
FileDialog(Frame parent, String title)184 FileDialog(Frame parent, String title)
185 {
186   this(parent, title, LOAD);
187 }
188 
189 /*************************************************************************/
190 
191 /**
192   * Initialized a new instance of <code>FileDialog</code> with the
193   * specified parent, title, and mode.
194   *
195   * @param parent The parent frame for this dialog.
196   * @param title The title for this dialog.
197   * @param mode The mode of the dialog, either <code>LOAD</code> or
198   * <code>SAVE</code>.
199   *
200   * @exception IllegalArgumentException If an illegal file dialog mode
201   * is supplied.
202   */
203 public
FileDialog(Frame parent, String title, int mode)204 FileDialog(Frame parent, String title, int mode)
205 {
206   super(parent, title, true);
207 
208   if ((mode != LOAD) && (mode != SAVE))
209     throw new IllegalArgumentException (
210       "Mode argument must be either LOAD or SAVE");
211 
212   setMode (mode);
213 }
214 
215 /*************************************************************************/
216 
217 /*
218  * Instance Methods
219  */
220 
221 /**
222   * Returns the mode of this dialog, either <code>LOAD</code> or
223   * <code>SAVE</code>.
224   *
225   * @return The mode of this dialog.
226   */
227 public int
getMode()228 getMode()
229 {
230   return(mode);
231 }
232 
233 /*************************************************************************/
234 
235 /**
236   * Sets the mode of this dialog to either <code>LOAD</code> or
237   * <code>SAVE</code>.  This method is only effective before the native
238   * peer is created.
239   *
240   * @param mode The new mode of this file dialog.
241   *
242   * @exception IllegalArgumentException If an illegal file dialog mode
243   * is supplied.
244   */
245 public void
setMode(int mode)246 setMode(int mode)
247 {
248   if ((mode != LOAD) && (mode != SAVE))
249     throw new IllegalArgumentException("Bad mode: " + mode);
250 
251   this.mode = mode;
252 }
253 
254 /*************************************************************************/
255 
256 /**
257   * Returns the directory for this file dialog.
258   *
259   * @return The directory for this file dialog.
260   */
261 public String
getDirectory()262 getDirectory()
263 {
264   return(dir);
265 }
266 
267 /*************************************************************************/
268 
269 /**
270   * Sets the directory for this file dialog.
271   *
272   * @param dir The new directory for this file dialog.
273   */
274 public synchronized void
setDirectory(String dir)275 setDirectory(String dir)
276 {
277   this.dir = dir;
278   if (peer != null)
279     {
280       FileDialogPeer f = (FileDialogPeer) peer;
281       f.setDirectory (dir);
282     }
283 }
284 
285 /*************************************************************************/
286 
287 /**
288   * Returns the file that is selected in this dialog.
289   *
290   * @return The file that is selected in this dialog.
291   */
292 public String
getFile()293 getFile()
294 {
295   return(file);
296 }
297 
298 /*************************************************************************/
299 
300 /**
301   * Sets the selected file for this dialog.
302   *
303   * @param file The selected file for this dialog.
304   */
305 public synchronized void
setFile(String file)306 setFile(String file)
307 {
308   if ("".equals(file))
309     this.file = null;
310   else
311     this.file = file;
312 
313   if (peer != null)
314     {
315       FileDialogPeer f = (FileDialogPeer) peer;
316       f.setFile (file);
317     }
318 }
319 
320 /*************************************************************************/
321 
322 /**
323   * Returns the filename filter being used by this dialog.
324   *
325   * @return The filename filter being used by this dialog.
326   */
327 public FilenameFilter
getFilenameFilter()328 getFilenameFilter()
329 {
330   return(filter);
331 }
332 
333 /*************************************************************************/
334 
335 /**
336   * Sets the filename filter used by this dialog.
337   *
338   * @param filter The new filename filter for this file dialog box.
339   */
340 public synchronized void
setFilenameFilter(FilenameFilter filter)341 setFilenameFilter(FilenameFilter filter)
342 {
343   this.filter = filter;
344   if (peer != null)
345     {
346       FileDialogPeer f = (FileDialogPeer) peer;
347       f.setFilenameFilter (filter);
348     }
349 }
350 
351 /*************************************************************************/
352 
353 /**
354   * Creates the native peer for this file dialog box.
355   */
356 public void
addNotify()357 addNotify()
358 {
359   if (peer == null)
360     peer = getToolkit ().createFileDialog (this);
361   super.addNotify ();
362 }
363 
364 /*************************************************************************/
365 
366 /**
367   * Returns a debugging string for this object.
368   *
369   * @return A debugging string for this object.
370   */
371 protected String
paramString()372 paramString()
373 {
374   return ("dir=" + dir + ",file=" + file +
375           ",mode=" + mode + "," + super.paramString());
376 }
377 
378 /**
379  * Generate a unique name for this <code>FileDialog</code>.
380  *
381  * @return A unique name for this <code>FileDialog</code>.
382  */
383 String
generateName()384 generateName()
385 {
386   return "filedlg" + getUniqueLong();
387 }
388 
389 private static synchronized long
getUniqueLong()390 getUniqueLong()
391 {
392   return next_file_dialog_number++;
393 }
394 
395 } // class FileDialog
396