Source for java.awt.FileDialog

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