home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / FileDialog.java < prev    next >
Text File  |  1997-10-01  |  8KB  |  261 lines

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