001/* FileDialog.java -- A filename selection dialog box
002   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.awt;
040
041import java.awt.peer.FileDialogPeer;
042import java.io.FilenameFilter;
043import java.io.Serializable;
044
045/**
046  * This class implements a file selection dialog box widget.
047  *
048  * @author Aaron M. Renn (arenn@urbanophile.com)
049  * @author Tom Tromey (tromey@redhat.com)
050  */
051public class FileDialog extends Dialog implements Serializable
052{
053
054/*
055 * Static Variables
056 */
057
058/**
059  * Indicates that the purpose of the dialog is for opening a file.
060  */
061public static final int LOAD = 0;
062
063/**
064  * Indicates that the purpose of the dialog is for saving a file.
065  */
066public static final int SAVE = 1;
067
068// Serialization constant
069private static final long serialVersionUID = 5035145889651310422L;
070
071/*************************************************************************/
072
073/*
074 * Instance Variables
075 */
076
077/**
078  * @serial The directory for this file dialog.
079  */
080private String dir;
081
082/**
083  * @serial The filename for this file dialog
084  */
085private String file;
086
087/**
088  * @serial The filter for selecting filenames to display
089  */
090private FilenameFilter filter;
091
092/**
093  * @serial The mode of this dialog, either <code>LOAD</code> or 
094  * <code>SAVE</code>.
095  */
096private int mode;
097
098/**
099 * The number used to generate the name returned by getName.
100 */
101private 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   */
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   */
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   */
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  */
168public
169FileDialog(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  */
183public
184FileDialog(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  */
203public
204FileDialog(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  */
227public int
228getMode()
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  */
245public void
246setMode(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  */
261public String
262getDirectory()
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  */
274public synchronized void
275setDirectory(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  */
292public String
293getFile()
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  */
305public synchronized void
306setFile(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  */
327public FilenameFilter
328getFilenameFilter()
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  */
340public synchronized void
341setFilenameFilter(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  */
356public void
357addNotify()
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  */
371protected String
372paramString()
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 */
383String 
384generateName()
385{
386  return "filedlg" + getUniqueLong();
387}
388
389private static synchronized long 
390getUniqueLong()
391{
392  return next_file_dialog_number++;
393}
394
395} // class FileDialog 
396