home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / FileDialog.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.1 KB  |  241 lines

  1. /*
  2.  * @(#)FileDialog.java    1.29 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.FileDialogPeer;
  17. import java.io.FilenameFilter;
  18.  
  19. /**
  20.  * The <code>FileDialog</code> class displays a dialog window 
  21.  * from which the user can select a file. 
  22.  * <p>
  23.  * Since it is a modal dialog, when the application calls 
  24.  * its <code>show</code> method to display the dialog, 
  25.  * it blocks the rest of the application until the user has 
  26.  * chosen a file. 
  27.  *
  28.  * @see Window#show
  29.  *
  30.  * @version     1.29, 03/18/98
  31.  * @author     Sami Shaio
  32.  * @author     Arthur van Hoff
  33.  * @since       JDK1.0
  34.  */
  35. public class FileDialog extends Dialog {
  36.     
  37.     /**
  38.      * This constant value indicates that the purpose of the file  
  39.      * dialog window is to locate a file from which to read. 
  40.      */
  41.     public static final int LOAD = 0;
  42.  
  43.     /**
  44.      * This constant value indicates that the purpose of the file  
  45.      * dialog window is to locate a file to which to write. 
  46.      */
  47.     public static final int SAVE = 1;
  48.  
  49.     int mode;
  50.     String dir;
  51.     String file;
  52.     FilenameFilter filter;
  53.  
  54.     private static final String base = "filedlg";
  55.     private static int nameCounter = 0;
  56.  
  57.     /*
  58.      * JDK 1.1 serialVersionUID 
  59.      */
  60.      private static final long serialVersionUID = 5035145889651310422L;
  61.  
  62.     /**
  63.      * Creates a file dialog for loading a file.  The title of the
  64.      * file dialog is initially empty.
  65.      * @param parent the owner of the dialog
  66.      * @since JDK1.1
  67.      */
  68.     public FileDialog(Frame parent) {
  69.     this(parent, "", LOAD);
  70.     }
  71.  
  72.     /**
  73.      * Creates a file dialog window with the specified title for loading 
  74.      * a file. The files shown are those in the current directory. 
  75.      * @param     parent   the owner of the dialog.
  76.      * @param     title    the title of the dialog.
  77.      */
  78.     public FileDialog(Frame parent, String title) {
  79.     this(parent, title, LOAD);
  80.     }
  81.  
  82.     /**
  83.      * Creates a file dialog window with the specified title for loading 
  84.      * or saving a file. 
  85.      * <p>
  86.      * If the value of <code>mode</code> is <code>LOAD</code>, then the 
  87.      * file dialog is finding a file to read. If the value of 
  88.      * <code>mode</code> is <code>SAVE</code>, the file dialog is finding 
  89.      * a place to write a file. 
  90.      * @param     parent   the owner of the dialog.
  91.      * @param     title   the title of the dialog.
  92.      * @param     mode   the mode of the dialog.
  93.      * @see       java.awt.FileDialog#LOAD
  94.      * @see       java.awt.FileDialog#SAVE
  95.      */
  96.     public FileDialog(Frame parent, String title, int mode) {
  97.     super(parent, title, true);
  98.     this.name = base + nameCounter++;
  99.     this.mode = mode;
  100.     setLayout(null);
  101.     }
  102.  
  103.     /**
  104.      * Creates the file dialog's peer.  The peer allows us to change the look
  105.      * of the file dialog without changing its functionality.
  106.      */
  107.     public void addNotify() {
  108.     peer = getToolkit().createFileDialog(this);
  109.     super.addNotify();
  110.     }
  111.  
  112.     /**
  113.      * Indicates whether this file dialog box is for loading from a file 
  114.      * or for saving to a file. 
  115.      * @return   the mode of this file dialog window, either 
  116.      *               <code>FileDialog.LOAD</code> or 
  117.      *               <code>FileDialog.SAVE</code>.
  118.      * @see      java.awt.FileDialog#LOAD
  119.      * @see      java.awt.FileDialog#SAVE
  120.      * @see      java.awt.FileDialog#setMode
  121.      */
  122.     public int getMode() {
  123.     return mode;
  124.     }
  125.  
  126.     /**
  127.      * Sets the mode of the file dialog.
  128.      * @param      mode  the mode for this file dialog, either 
  129.      *                 <code>FileDialog.LOAD</code> or 
  130.      *                 <code>FileDialog.SAVE</code>.
  131.      * @see        java.awt.FileDialog#LOAD
  132.      * @see        java.awt.FileDialog#SAVE
  133.      * @see        java.awt.FileDialog#getMode
  134.      * @exception  IllegalArgumentException if an illegal file 
  135.      *                 dialog mode is used.
  136.      * @since      JDK1.1
  137.      */
  138.     public void setMode(int mode) {
  139.     switch (mode) {
  140.       case LOAD:
  141.       case SAVE:
  142.         this.mode = mode;
  143.         break;
  144.       default:
  145.         throw new IllegalArgumentException("illegal file dialog mode");
  146.     }
  147.     }
  148.  
  149.     /**
  150.      * Gets the directory of this file dialog.
  151.      * @return    the directory of this file dialog.
  152.      * @see       java.awt.FileDialog#setDirectory
  153.      */
  154.     public String getDirectory() {
  155.     return dir;
  156.     }
  157.  
  158.     /**
  159.      * Sets the directory of this file dialog window to be the  
  160.      * specified directory. 
  161.      * @param     dir   the specific directory.
  162.      * @see       java.awt.FileDialog#getDirectory
  163.      */
  164.     public synchronized void setDirectory(String dir) {
  165.     this.dir = dir;
  166.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  167.     if (peer != null) {
  168.         peer.setDirectory(dir);
  169.     }
  170.     }
  171.  
  172.     /**
  173.      * Gets the selected file of this file dialog.
  174.      * @return    the currently selected file of this file dialog window, 
  175.      *                or <code>null</code> if none is selected.
  176.      * @see       java.awt.FileDialog#setFile
  177.      */
  178.     public String getFile() {
  179.     return file;
  180.     }
  181.  
  182.     /**
  183.      * Sets the selected file for this file dialog window to be the 
  184.      * specified file. This file becomes the default file if it is set 
  185.      * before the file dialog window is first shown. 
  186.      * @param    file   the file being set.
  187.      * @see      java.awt.FileDialog#getFile
  188.      */
  189.     public synchronized void setFile(String file) {
  190.     this.file = file;
  191.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  192.     if (peer != null) {
  193.         peer.setFile(file);
  194.     }
  195.     }
  196.     
  197.     /**
  198.      * Determines this file dialog's filename filter. A filename filter 
  199.      * allows the user to specify which files appear in the file dialog 
  200.      * window. 
  201.      * @return    this file dialog's filename filter.
  202.      * @see       java.io.FilenameFilter
  203.      * @see       java.awt.FileDialog#setFilenameFilter
  204.      */
  205.     public FilenameFilter getFilenameFilter() {
  206.     return filter;
  207.     }
  208.  
  209.     /**
  210.      * Sets the filename filter for this file dialog window to the 
  211.      * specified filter. 
  212.      * @param   filter   the specified filter.
  213.      * @see     java.io.FilenameFilter
  214.      * @see     java.awt.FileDialog#getFilenameFilter
  215.      */
  216.     public synchronized void setFilenameFilter(FilenameFilter filter) {
  217.     this.filter = filter;
  218.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  219.     if (peer != null) {
  220.         peer.setFilenameFilter(filter);
  221.     }
  222.     }
  223.  
  224.     /**
  225.      * Returns the parameter string representing the state of this file 
  226.      * dialog window. This string is useful for debugging. 
  227.      * @return  the parameter string of this file dialog window.
  228.      */
  229.     protected String paramString() {
  230.     String str = super.paramString();
  231.     if (dir != null) {
  232.         str += ",dir= " + dir;
  233.     }
  234.     return str + ((mode == LOAD) ? ",load" : ",save");
  235.     }
  236.  
  237.     boolean postsOldMouseEvents() {
  238.         return false;
  239.     }
  240. }
  241.