home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / FileDialog.java < prev    next >
Text File  |  1996-05-03  |  4KB  |  157 lines

  1. /*
  2.  * @(#)FileDialog.java    1.15 96/01/18 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1995-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.FileDialogPeer;
  22. import java.io.FilenameFilter;
  23.  
  24. /**
  25.  * The File Dialog class displays a file selection dialog. It is a
  26.  * modal dialog and will block the calling thread when the show method
  27.  * is called to display it, until the user has chosen a file.
  28.  *
  29.  * @see Window#show
  30.  *
  31.  * @version     1.15, 18 Jan 1996
  32.  * @author     Sami Shaio
  33.  * @author     Arthur van Hoff
  34.  */
  35. public class FileDialog extends Dialog {
  36.     
  37.     /**
  38.      * The file load variable.
  39.      */
  40.     public static final int LOAD = 0;
  41.  
  42.     /**
  43.      * The file save variable.
  44.      */
  45.     public static final int SAVE = 1;
  46.  
  47.     int mode;
  48.     String dir;
  49.     String file;
  50.     FilenameFilter filter;
  51.  
  52.     /**
  53.      * Creates a file dialog for loading a file.
  54.      * @param parent the owner of the dialog
  55.      * @param title the title of the Dialog
  56.      */
  57.     public FileDialog(Frame parent, String title) {
  58.     this(parent, title, LOAD);
  59.     }
  60.  
  61.     /**
  62.      * Creates a file dialog with the specified title and mode.
  63.      * @param parent the owner of the dialog
  64.      * @param title the title of the Dialog
  65.      * @param mode the mode of the Dialog
  66.      */
  67.     public FileDialog(Frame parent, String title, int mode) {
  68.     super(parent, title, true);
  69.     this.mode = mode;
  70.     setLayout(null);
  71.     }
  72.  
  73.     /**
  74.      * Creates the frame's peer.  The peer allows us to change the look
  75.      * of the file dialog without changing its functionality.
  76.      */
  77.     public synchronized void addNotify() {
  78.     peer = getToolkit().createFileDialog(this);
  79.     super.addNotify();
  80.     }
  81.  
  82.     /**
  83.      * Gets the mode of the file dialog.
  84.      */
  85.     public int getMode() {
  86.     return mode;
  87.     }
  88.  
  89.     /**
  90.      * Gets the directory of the Dialog.
  91.      */
  92.     public String getDirectory() {
  93.     return dir;
  94.     }
  95.  
  96.     /**
  97.      * Set the directory of the Dialog to the specified directory.
  98.      * @param dir the specific directory
  99.      */
  100.     public void setDirectory(String dir) {
  101.     this.dir = dir;
  102.     if (peer != null) {
  103.         ((FileDialogPeer)peer).setDirectory(dir);
  104.     }
  105.     }
  106.  
  107.     /**
  108.      * Gets the file of the Dialog.
  109.      */
  110.     public String getFile() {
  111.     return file;
  112.     }
  113.  
  114.     /**
  115.      * Sets the file for this dialog to the specified file. This will 
  116.      * become the default file if set before the dialog is shown.
  117.      * @param file the file being set
  118.      */
  119.     public void setFile(String file) {
  120.     this.file = file;
  121.     if (peer != null) {
  122.         ((FileDialogPeer)peer).setFile(file);
  123.     }
  124.     }
  125.     
  126.     /**
  127.      * Gets the filter.
  128.      */
  129.     public FilenameFilter getFilenameFilter() {
  130.     return filter;
  131.     }
  132.  
  133.     /**
  134.      * Sets the filter for this dialog to the specified filter.
  135.      * @param filter the specified filter
  136.      */
  137.     public void setFilenameFilter(FilenameFilter filter) {
  138.     this.filter = filter;
  139.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  140.     if (peer != null) {
  141.         peer.setFilenameFilter(filter);
  142.     }
  143.     }
  144.  
  145.     /**
  146.      * Returns the parameter String of this file dialog.
  147.      * Parameter String.
  148.      */
  149.     protected String paramString() {
  150.     String str = super.paramString();
  151.     if (dir != null) {
  152.         str += ",dir= " + dir;
  153.     }
  154.     return str + ((mode == LOAD) ? ",load" : ",save");
  155.     }
  156. }
  157.