< prev index next >

src/java.desktop/share/classes/java/awt/FileDialog.java

Print this page




  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.awt.peer.FileDialogPeer;
  28 import java.io.FilenameFilter;
  29 import java.io.IOException;
  30 import java.io.ObjectInputStream;
  31 import java.io.File;
  32 import sun.awt.AWTAccessor;
  33 
  34 /**
  35  * The <code>FileDialog</code> class displays a dialog window
  36  * from which the user can select a file.
  37  * <p>
  38  * Since it is a modal dialog, when the application calls
  39  * its <code>show</code> method to display the dialog,
  40  * it blocks the rest of the application until the user has
  41  * chosen a file.
  42  *
  43  * @see Window#show
  44  *
  45  * @author      Sami Shaio
  46  * @author      Arthur van Hoff
  47  * @since       1.0
  48  */
  49 public class FileDialog extends Dialog {
  50 
  51     /**
  52      * This constant value indicates that the purpose of the file
  53      * dialog window is to locate a file from which to read.
  54      */
  55     public static final int LOAD = 0;
  56 
  57     /**
  58      * This constant value indicates that the purpose of the file
  59      * dialog window is to locate a file to which to write.
  60      */
  61     public static final int SAVE = 1;
  62 
  63     /*
  64      * There are two <code>FileDialog</code> modes: <code>LOAD</code> and
  65      * <code>SAVE</code>.
  66      * This integer will represent one or the other.
  67      * If the mode is not specified it will default to <code>LOAD</code>.
  68      *
  69      * @serial
  70      * @see getMode()
  71      * @see setMode()
  72      * @see java.awt.FileDialog#LOAD
  73      * @see java.awt.FileDialog#SAVE
  74      */
  75     int mode;
  76 
  77     /*
  78      * The string specifying the directory to display
  79      * in the file dialog.  This variable may be <code>null</code>.
  80      *
  81      * @serial
  82      * @see getDirectory()
  83      * @see setDirectory()
  84      */
  85     String dir;
  86 
  87     /*
  88      * The string specifying the initial value of the
  89      * filename text field in the file dialog.
  90      * This variable may be <code>null</code>.
  91      *
  92      * @serial
  93      * @see getFile()
  94      * @see setFile()
  95      */
  96     String file;
  97 
  98     /**
  99      * Contains the File instances for all the files that the user selects.
 100      *
 101      * @serial
 102      * @see #getFiles
 103      * @since 1.7
 104      */
 105     private File[] files;
 106 
 107     /**
 108      * Represents whether the file dialog allows the multiple file selection.
 109      *
 110      * @serial
 111      * @see #setMultipleMode
 112      * @see #isMultipleMode
 113      * @since 1.7
 114      */
 115     private boolean multipleMode = false;
 116 
 117     /*
 118      * The filter used as the file dialog's filename filter.
 119      * The file dialog will only be displaying files whose
 120      * names are accepted by this filter.
 121      * This variable may be <code>null</code>.
 122      *
 123      * @serial
 124      * @see #getFilenameFilter()
 125      * @see #setFilenameFilter()
 126      * @see FileNameFilter
 127      */
 128     FilenameFilter filter;
 129 
 130     private static final String base = "filedlg";
 131     private static int nameCounter = 0;
 132 
 133     /*
 134      * JDK 1.1 serialVersionUID
 135      */
 136      private static final long serialVersionUID = 5035145889651310422L;
 137 
 138 
 139     static {
 140         /* ensure that the necessary native libraries are loaded */
 141         Toolkit.loadLibraries();


 156                 public void setDirectory(FileDialog fileDialog, String directory) {
 157                     fileDialog.dir = ("".equals(directory)) ? null : directory;
 158                 }
 159                 public boolean isMultipleMode(FileDialog fileDialog) {
 160                     synchronized (fileDialog.getObjectLock()) {
 161                         return fileDialog.multipleMode;
 162                     }
 163                 }
 164             });
 165     }
 166 
 167     /**
 168      * Initialize JNI field and method IDs for fields that may be
 169        accessed from C.
 170      */
 171     private static native void initIDs();
 172 
 173     /**
 174      * Creates a file dialog for loading a file.  The title of the
 175      * file dialog is initially empty.  This is a convenience method for
 176      * <code>FileDialog(parent, "", LOAD)</code>.
 177      * <p>
 178      * <strong>Note:</strong> Some platforms may not support
 179      * showing the user-specified title in a file dialog.
 180      * In this situation, either no title will be displayed in the file dialog's
 181      * title bar or, on some systems, the file dialog's title bar will not be
 182      * displayed.
 183      *
 184      * @param parent the owner of the dialog
 185      * @since 1.1
 186      */
 187     public FileDialog(Frame parent) {
 188         this(parent, "", LOAD);
 189     }
 190 
 191     /**
 192      * Creates a file dialog window with the specified title for loading
 193      * a file. The files shown are those in the current directory.
 194      * This is a convenience method for
 195      * <code>FileDialog(parent, title, LOAD)</code>.
 196      * <p>
 197      * <strong>Note:</strong> Some platforms may not support
 198      * showing the user-specified title in a file dialog.
 199      * In this situation, either no title will be displayed in the file dialog's
 200      * title bar or, on some systems, the file dialog's title bar will not be
 201      * displayed.
 202      *
 203      * @param     parent   the owner of the dialog
 204      * @param     title    the title of the dialog
 205      */
 206     public FileDialog(Frame parent, String title) {
 207         this(parent, title, LOAD);
 208     }
 209 
 210     /**
 211      * Creates a file dialog window with the specified title for loading
 212      * or saving a file.
 213      * <p>
 214      * If the value of <code>mode</code> is <code>LOAD</code>, then the
 215      * file dialog is finding a file to read, and the files shown are those
 216      * in the current directory.   If the value of
 217      * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
 218      * a place to write a file.
 219      * <p>
 220      * <strong>Note:</strong> Some platforms may not support
 221      * showing the user-specified title in a file dialog.
 222      * In this situation, either no title will be displayed in the file dialog's
 223      * title bar or, on some systems, the file dialog's title bar will not be
 224      * displayed.
 225      *
 226      * @param     parent   the owner of the dialog
 227      * @param     title   the title of the dialog
 228      * @param     mode   the mode of the dialog; either
 229      *          <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
 230      * @exception  IllegalArgumentException if an illegal file
 231      *                 dialog mode is supplied
 232      * @see       java.awt.FileDialog#LOAD
 233      * @see       java.awt.FileDialog#SAVE
 234      */
 235     public FileDialog(Frame parent, String title, int mode) {
 236         super(parent, title, true);
 237         this.setMode(mode);
 238         setLayout(null);
 239     }
 240 
 241     /**
 242      * Creates a file dialog for loading a file.  The title of the
 243      * file dialog is initially empty.  This is a convenience method for
 244      * <code>FileDialog(parent, "", LOAD)</code>.
 245      * <p>
 246      * <strong>Note:</strong> Some platforms may not support
 247      * showing the user-specified title in a file dialog.
 248      * In this situation, either no title will be displayed in the file dialog's
 249      * title bar or, on some systems, the file dialog's title bar will not be
 250      * displayed.
 251      *
 252      * @param     parent   the owner of the dialog
 253      * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
 254      *            <code>GraphicsConfiguration</code>
 255      *            is not from a screen device;
 256      * @exception java.lang.IllegalArgumentException if <code>parent</code>
 257      *            is <code>null</code>; this exception is always thrown when
 258      *            <code>GraphicsEnvironment.isHeadless</code>
 259      *            returns <code>true</code>
 260      * @see java.awt.GraphicsEnvironment#isHeadless
 261      * @since 1.5
 262      */
 263     public FileDialog(Dialog parent) {
 264         this(parent, "", LOAD);
 265     }
 266 
 267     /**
 268      * Creates a file dialog window with the specified title for loading
 269      * a file. The files shown are those in the current directory.
 270      * This is a convenience method for
 271      * <code>FileDialog(parent, title, LOAD)</code>.
 272      * <p>
 273      * <strong>Note:</strong> Some platforms may not support
 274      * showing the user-specified title in a file dialog.
 275      * In this situation, either no title will be displayed in the file dialog's
 276      * title bar or, on some systems, the file dialog's title bar will not be
 277      * displayed.
 278      *
 279      * @param     parent   the owner of the dialog
 280      * @param     title    the title of the dialog; a <code>null</code> value
 281      *                     will be accepted without causing a
 282      *                     <code>NullPointerException</code> to be thrown
 283      * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
 284      *            <code>GraphicsConfiguration</code>
 285      *            is not from a screen device;
 286      * @exception java.lang.IllegalArgumentException if <code>parent</code>
 287      *            is <code>null</code>; this exception is always thrown when
 288      *            <code>GraphicsEnvironment.isHeadless</code>
 289      *            returns <code>true</code>
 290      * @see java.awt.GraphicsEnvironment#isHeadless
 291      * @since     1.5
 292      */
 293     public FileDialog(Dialog parent, String title) {
 294         this(parent, title, LOAD);
 295     }
 296 
 297     /**
 298      * Creates a file dialog window with the specified title for loading
 299      * or saving a file.
 300      * <p>
 301      * If the value of <code>mode</code> is <code>LOAD</code>, then the
 302      * file dialog is finding a file to read, and the files shown are those
 303      * in the current directory.   If the value of
 304      * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
 305      * a place to write a file.
 306      * <p>
 307      * <strong>Note:</strong> Some platforms may not support
 308      * showing the user-specified title in a file dialog.
 309      * In this situation, either no title will be displayed in the file dialog's
 310      * title bar or, on some systems, the file dialog's title bar will not be
 311      * displayed.
 312      *
 313      * @param     parent   the owner of the dialog
 314      * @param     title    the title of the dialog; a <code>null</code> value
 315      *                     will be accepted without causing a
 316      *                     <code>NullPointerException</code> to be thrown
 317      * @param     mode     the mode of the dialog; either
 318      *                     <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
 319      * @exception java.lang.IllegalArgumentException if an illegal
 320      *            file dialog mode is supplied;
 321      * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
 322      *            <code>GraphicsConfiguration</code>
 323      *            is not from a screen device;
 324      * @exception java.lang.IllegalArgumentException if <code>parent</code>
 325      *            is <code>null</code>; this exception is always thrown when
 326      *            <code>GraphicsEnvironment.isHeadless</code>
 327      *            returns <code>true</code>
 328      * @see       java.awt.GraphicsEnvironment#isHeadless
 329      * @see       java.awt.FileDialog#LOAD
 330      * @see       java.awt.FileDialog#SAVE
 331      * @since     1.5
 332      */
 333     public FileDialog(Dialog parent, String title, int mode) {
 334         super(parent, title, true);
 335         this.setMode(mode);
 336         setLayout(null);
 337     }
 338 
 339 
 340     /**
 341      * {@inheritDoc}
 342      * <p>
 343      * <strong>Note:</strong> Some platforms may not support
 344      * showing the user-specified title in a file dialog.
 345      * In this situation, either no title will be displayed in the file dialog's
 346      * title bar or, on some systems, the file dialog's title bar will not be
 347      * displayed.
 348      */
 349     @Override
 350     public void setTitle(String title) {
 351         super.setTitle(title);
 352     }
 353 
 354 
 355     /**
 356      * Constructs a name for this component. Called by <code>getName()</code>
 357      * when the name is <code>null</code>.
 358      */
 359     String constructComponentName() {
 360         synchronized (FileDialog.class) {
 361             return base + nameCounter++;
 362         }
 363     }
 364 
 365     /**
 366      * Creates the file dialog's peer.  The peer allows us to change the look
 367      * of the file dialog without changing its functionality.
 368      */
 369     public void addNotify() {
 370         synchronized(getTreeLock()) {
 371             if (parent != null && parent.peer == null) {
 372                 parent.addNotify();
 373             }
 374             if (peer == null)
 375                 peer = getComponentFactory().createFileDialog(this);
 376             super.addNotify();
 377         }
 378     }
 379 
 380     /**
 381      * Indicates whether this file dialog box is for loading from a file
 382      * or for saving to a file.
 383      *
 384      * @return   the mode of this file dialog window, either
 385      *               <code>FileDialog.LOAD</code> or
 386      *               <code>FileDialog.SAVE</code>
 387      * @see      java.awt.FileDialog#LOAD
 388      * @see      java.awt.FileDialog#SAVE
 389      * @see      java.awt.FileDialog#setMode
 390      */
 391     public int getMode() {
 392         return mode;
 393     }
 394 
 395     /**
 396      * Sets the mode of the file dialog.  If <code>mode</code> is not
 397      * a legal value, an exception will be thrown and <code>mode</code>
 398      * will not be set.
 399      *
 400      * @param      mode  the mode for this file dialog, either
 401      *                 <code>FileDialog.LOAD</code> or
 402      *                 <code>FileDialog.SAVE</code>
 403      * @see        java.awt.FileDialog#LOAD
 404      * @see        java.awt.FileDialog#SAVE
 405      * @see        java.awt.FileDialog#getMode
 406      * @exception  IllegalArgumentException if an illegal file
 407      *                 dialog mode is supplied
 408      * @since      1.1
 409      */
 410     public void setMode(int mode) {
 411         switch (mode) {
 412           case LOAD:
 413           case SAVE:
 414             this.mode = mode;
 415             break;
 416           default:
 417             throw new IllegalArgumentException("illegal file dialog mode");
 418         }
 419     }
 420 
 421     /**
 422      * Gets the directory of this file dialog.
 423      *
 424      * @return  the (potentially <code>null</code> or invalid)
 425      *          directory of this <code>FileDialog</code>
 426      * @see       java.awt.FileDialog#setDirectory
 427      */
 428     public String getDirectory() {
 429         return dir;
 430     }
 431 
 432     /**
 433      * Sets the directory of this file dialog window to be the
 434      * specified directory. Specifying a <code>null</code> or an
 435      * invalid directory implies an implementation-defined default.
 436      * This default will not be realized, however, until the user
 437      * has selected a file. Until this point, <code>getDirectory()</code>
 438      * will return the value passed into this method.
 439      * <p>
 440      * Specifying "" as the directory is exactly equivalent to
 441      * specifying <code>null</code> as the directory.
 442      *
 443      * @param     dir   the specified directory
 444      * @see       java.awt.FileDialog#getDirectory
 445      */
 446     public void setDirectory(String dir) {
 447         this.dir = (dir != null && dir.equals("")) ? null : dir;
 448         FileDialogPeer peer = (FileDialogPeer)this.peer;
 449         if (peer != null) {
 450             peer.setDirectory(this.dir);
 451         }
 452     }
 453 
 454     /**
 455      * Gets the selected file of this file dialog.  If the user
 456      * selected <code>CANCEL</code>, the returned file is <code>null</code>.
 457      *
 458      * @return    the currently selected file of this file dialog window,
 459      *                or <code>null</code> if none is selected
 460      * @see       java.awt.FileDialog#setFile
 461      */
 462     public String getFile() {
 463         return file;
 464     }
 465 
 466     /**
 467      * Returns files that the user selects.
 468      * <p>
 469      * If the user cancels the file dialog,
 470      * then the method returns an empty array.
 471      *
 472      * @return    files that the user selects or an empty array
 473      *            if the user cancels the file dialog.
 474      * @see       #setFile(String)
 475      * @see       #getFile
 476      * @since 1.7
 477      */
 478     public File[] getFiles() {
 479         synchronized (getObjectLock()) {


 575 
 576     /**
 577      * Sets the filename filter for this file dialog window to the
 578      * specified filter.
 579      * Filename filters do not function in Sun's reference
 580      * implementation for Microsoft Windows.
 581      *
 582      * @param   filter   the specified filter
 583      * @see     java.io.FilenameFilter
 584      * @see     java.awt.FileDialog#getFilenameFilter
 585      */
 586     public synchronized void setFilenameFilter(FilenameFilter filter) {
 587         this.filter = filter;
 588         FileDialogPeer peer = (FileDialogPeer)this.peer;
 589         if (peer != null) {
 590             peer.setFilenameFilter(filter);
 591         }
 592     }
 593 
 594     /**
 595      * Reads the <code>ObjectInputStream</code> and performs
 596      * a backwards compatibility check by converting
 597      * either a <code>dir</code> or a <code>file</code>
 598      * equal to an empty string to <code>null</code>.
 599      *
 600      * @param s the <code>ObjectInputStream</code> to read
 601      */
 602     private void readObject(ObjectInputStream s)
 603         throws ClassNotFoundException, IOException
 604     {
 605         s.defaultReadObject();
 606 
 607         // 1.1 Compatibility: "" is not converted to null in 1.1
 608         if (dir != null && dir.equals("")) {
 609             dir = null;
 610         }
 611         if (file != null && file.equals("")) {
 612             file = null;
 613         }
 614     }
 615 
 616     /**
 617      * Returns a string representing the state of this <code>FileDialog</code>
 618      * window. This method is intended to be used only for debugging purposes,
 619      * and the content and format of the returned string may vary between
 620      * implementations. The returned string may be empty but may not be
 621      * <code>null</code>.
 622      *
 623      * @return  the parameter string of this file dialog window
 624      */
 625     protected String paramString() {
 626         String str = super.paramString();
 627         str += ",dir= " + dir;
 628         str += ",file= " + file;
 629         return str + ((mode == LOAD) ? ",load" : ",save");
 630     }
 631 
 632     boolean postsOldMouseEvents() {
 633         return false;
 634     }
 635 }


  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.awt.peer.FileDialogPeer;
  28 import java.io.FilenameFilter;
  29 import java.io.IOException;
  30 import java.io.ObjectInputStream;
  31 import java.io.File;
  32 import sun.awt.AWTAccessor;
  33 
  34 /**
  35  * The {@code FileDialog} class displays a dialog window
  36  * from which the user can select a file.
  37  * <p>
  38  * Since it is a modal dialog, when the application calls
  39  * its {@code show} method to display the dialog,
  40  * it blocks the rest of the application until the user has
  41  * chosen a file.
  42  *
  43  * @see Window#show
  44  *
  45  * @author      Sami Shaio
  46  * @author      Arthur van Hoff
  47  * @since       1.0
  48  */
  49 public class FileDialog extends Dialog {
  50 
  51     /**
  52      * This constant value indicates that the purpose of the file
  53      * dialog window is to locate a file from which to read.
  54      */
  55     public static final int LOAD = 0;
  56 
  57     /**
  58      * This constant value indicates that the purpose of the file
  59      * dialog window is to locate a file to which to write.
  60      */
  61     public static final int SAVE = 1;
  62 
  63     /*
  64      * There are two {@code FileDialog} modes: {@code LOAD} and
  65      * {@code SAVE}.
  66      * This integer will represent one or the other.
  67      * If the mode is not specified it will default to {@code LOAD}.
  68      *
  69      * @serial
  70      * @see getMode()
  71      * @see setMode()
  72      * @see java.awt.FileDialog#LOAD
  73      * @see java.awt.FileDialog#SAVE
  74      */
  75     int mode;
  76 
  77     /*
  78      * The string specifying the directory to display
  79      * in the file dialog.  This variable may be {@code null}.
  80      *
  81      * @serial
  82      * @see getDirectory()
  83      * @see setDirectory()
  84      */
  85     String dir;
  86 
  87     /*
  88      * The string specifying the initial value of the
  89      * filename text field in the file dialog.
  90      * This variable may be {@code null}.
  91      *
  92      * @serial
  93      * @see getFile()
  94      * @see setFile()
  95      */
  96     String file;
  97 
  98     /**
  99      * Contains the File instances for all the files that the user selects.
 100      *
 101      * @serial
 102      * @see #getFiles
 103      * @since 1.7
 104      */
 105     private File[] files;
 106 
 107     /**
 108      * Represents whether the file dialog allows the multiple file selection.
 109      *
 110      * @serial
 111      * @see #setMultipleMode
 112      * @see #isMultipleMode
 113      * @since 1.7
 114      */
 115     private boolean multipleMode = false;
 116 
 117     /*
 118      * The filter used as the file dialog's filename filter.
 119      * The file dialog will only be displaying files whose
 120      * names are accepted by this filter.
 121      * This variable may be {@code null}.
 122      *
 123      * @serial
 124      * @see #getFilenameFilter()
 125      * @see #setFilenameFilter()
 126      * @see FileNameFilter
 127      */
 128     FilenameFilter filter;
 129 
 130     private static final String base = "filedlg";
 131     private static int nameCounter = 0;
 132 
 133     /*
 134      * JDK 1.1 serialVersionUID
 135      */
 136      private static final long serialVersionUID = 5035145889651310422L;
 137 
 138 
 139     static {
 140         /* ensure that the necessary native libraries are loaded */
 141         Toolkit.loadLibraries();


 156                 public void setDirectory(FileDialog fileDialog, String directory) {
 157                     fileDialog.dir = ("".equals(directory)) ? null : directory;
 158                 }
 159                 public boolean isMultipleMode(FileDialog fileDialog) {
 160                     synchronized (fileDialog.getObjectLock()) {
 161                         return fileDialog.multipleMode;
 162                     }
 163                 }
 164             });
 165     }
 166 
 167     /**
 168      * Initialize JNI field and method IDs for fields that may be
 169        accessed from C.
 170      */
 171     private static native void initIDs();
 172 
 173     /**
 174      * Creates a file dialog for loading a file.  The title of the
 175      * file dialog is initially empty.  This is a convenience method for
 176      * {@code FileDialog(parent, "", LOAD)}.
 177      * <p>
 178      * <strong>Note:</strong> Some platforms may not support
 179      * showing the user-specified title in a file dialog.
 180      * In this situation, either no title will be displayed in the file dialog's
 181      * title bar or, on some systems, the file dialog's title bar will not be
 182      * displayed.
 183      *
 184      * @param parent the owner of the dialog
 185      * @since 1.1
 186      */
 187     public FileDialog(Frame parent) {
 188         this(parent, "", LOAD);
 189     }
 190 
 191     /**
 192      * Creates a file dialog window with the specified title for loading
 193      * a file. The files shown are those in the current directory.
 194      * This is a convenience method for
 195      * {@code FileDialog(parent, title, LOAD)}.
 196      * <p>
 197      * <strong>Note:</strong> Some platforms may not support
 198      * showing the user-specified title in a file dialog.
 199      * In this situation, either no title will be displayed in the file dialog's
 200      * title bar or, on some systems, the file dialog's title bar will not be
 201      * displayed.
 202      *
 203      * @param     parent   the owner of the dialog
 204      * @param     title    the title of the dialog
 205      */
 206     public FileDialog(Frame parent, String title) {
 207         this(parent, title, LOAD);
 208     }
 209 
 210     /**
 211      * Creates a file dialog window with the specified title for loading
 212      * or saving a file.
 213      * <p>
 214      * If the value of {@code mode} is {@code LOAD}, then the
 215      * file dialog is finding a file to read, and the files shown are those
 216      * in the current directory.   If the value of
 217      * {@code mode} is {@code SAVE}, the file dialog is finding
 218      * a place to write a file.
 219      * <p>
 220      * <strong>Note:</strong> Some platforms may not support
 221      * showing the user-specified title in a file dialog.
 222      * In this situation, either no title will be displayed in the file dialog's
 223      * title bar or, on some systems, the file dialog's title bar will not be
 224      * displayed.
 225      *
 226      * @param     parent   the owner of the dialog
 227      * @param     title   the title of the dialog
 228      * @param     mode   the mode of the dialog; either
 229      *            {@code FileDialog.LOAD} or {@code FileDialog.SAVE}
 230      * @exception  IllegalArgumentException if an illegal file
 231      *                 dialog mode is supplied
 232      * @see       java.awt.FileDialog#LOAD
 233      * @see       java.awt.FileDialog#SAVE
 234      */
 235     public FileDialog(Frame parent, String title, int mode) {
 236         super(parent, title, true);
 237         this.setMode(mode);
 238         setLayout(null);
 239     }
 240 
 241     /**
 242      * Creates a file dialog for loading a file.  The title of the
 243      * file dialog is initially empty.  This is a convenience method for
 244      * {@code FileDialog(parent, "", LOAD)}.
 245      * <p>
 246      * <strong>Note:</strong> Some platforms may not support
 247      * showing the user-specified title in a file dialog.
 248      * In this situation, either no title will be displayed in the file dialog's
 249      * title bar or, on some systems, the file dialog's title bar will not be
 250      * displayed.
 251      *
 252      * @param     parent   the owner of the dialog
 253      * @exception java.lang.IllegalArgumentException if the {@code parent}'s
 254      *            {@code GraphicsConfiguration}
 255      *            is not from a screen device;
 256      * @exception java.lang.IllegalArgumentException if {@code parent}
 257      *            is {@code null}; this exception is always thrown when
 258      *            {@code GraphicsEnvironment.isHeadless}
 259      *            returns {@code true}
 260      * @see java.awt.GraphicsEnvironment#isHeadless
 261      * @since 1.5
 262      */
 263     public FileDialog(Dialog parent) {
 264         this(parent, "", LOAD);
 265     }
 266 
 267     /**
 268      * Creates a file dialog window with the specified title for loading
 269      * a file. The files shown are those in the current directory.
 270      * This is a convenience method for
 271      * {@code FileDialog(parent, title, LOAD)}.
 272      * <p>
 273      * <strong>Note:</strong> Some platforms may not support
 274      * showing the user-specified title in a file dialog.
 275      * In this situation, either no title will be displayed in the file dialog's
 276      * title bar or, on some systems, the file dialog's title bar will not be
 277      * displayed.
 278      *
 279      * @param     parent   the owner of the dialog
 280      * @param     title    the title of the dialog; a {@code null} value
 281      *                     will be accepted without causing a
 282      *                     {@code NullPointerException} to be thrown
 283      * @exception java.lang.IllegalArgumentException if the {@code parent}'s
 284      *            {@code GraphicsConfiguration}
 285      *            is not from a screen device;
 286      * @exception java.lang.IllegalArgumentException if {@code parent}
 287      *            is {@code null}; this exception is always thrown when
 288      *            {@code GraphicsEnvironment.isHeadless}
 289      *            returns {@code true}
 290      * @see java.awt.GraphicsEnvironment#isHeadless
 291      * @since     1.5
 292      */
 293     public FileDialog(Dialog parent, String title) {
 294         this(parent, title, LOAD);
 295     }
 296 
 297     /**
 298      * Creates a file dialog window with the specified title for loading
 299      * or saving a file.
 300      * <p>
 301      * If the value of {@code mode} is {@code LOAD}, then the
 302      * file dialog is finding a file to read, and the files shown are those
 303      * in the current directory.   If the value of
 304      * {@code mode} is {@code SAVE}, the file dialog is finding
 305      * a place to write a file.
 306      * <p>
 307      * <strong>Note:</strong> Some platforms may not support
 308      * showing the user-specified title in a file dialog.
 309      * In this situation, either no title will be displayed in the file dialog's
 310      * title bar or, on some systems, the file dialog's title bar will not be
 311      * displayed.
 312      *
 313      * @param     parent   the owner of the dialog
 314      * @param     title    the title of the dialog; a {@code null} value
 315      *                     will be accepted without causing a
 316      *                     {@code NullPointerException} to be thrown
 317      * @param     mode     the mode of the dialog; either
 318      *                     {@code FileDialog.LOAD} or {@code FileDialog.SAVE}
 319      * @exception java.lang.IllegalArgumentException if an illegal
 320      *            file dialog mode is supplied;
 321      * @exception java.lang.IllegalArgumentException if the {@code parent}'s
 322      *            {@code GraphicsConfiguration}
 323      *            is not from a screen device;
 324      * @exception java.lang.IllegalArgumentException if {@code parent}
 325      *            is {@code null}; this exception is always thrown when
 326      *            {@code GraphicsEnvironment.isHeadless}
 327      *            returns {@code true}
 328      * @see       java.awt.GraphicsEnvironment#isHeadless
 329      * @see       java.awt.FileDialog#LOAD
 330      * @see       java.awt.FileDialog#SAVE
 331      * @since     1.5
 332      */
 333     public FileDialog(Dialog parent, String title, int mode) {
 334         super(parent, title, true);
 335         this.setMode(mode);
 336         setLayout(null);
 337     }
 338 
 339 
 340     /**
 341      * {@inheritDoc}
 342      * <p>
 343      * <strong>Note:</strong> Some platforms may not support
 344      * showing the user-specified title in a file dialog.
 345      * In this situation, either no title will be displayed in the file dialog's
 346      * title bar or, on some systems, the file dialog's title bar will not be
 347      * displayed.
 348      */
 349     @Override
 350     public void setTitle(String title) {
 351         super.setTitle(title);
 352     }
 353 
 354 
 355     /**
 356      * Constructs a name for this component. Called by {@code getName()}
 357      * when the name is {@code null}.
 358      */
 359     String constructComponentName() {
 360         synchronized (FileDialog.class) {
 361             return base + nameCounter++;
 362         }
 363     }
 364 
 365     /**
 366      * Creates the file dialog's peer.  The peer allows us to change the look
 367      * of the file dialog without changing its functionality.
 368      */
 369     public void addNotify() {
 370         synchronized(getTreeLock()) {
 371             if (parent != null && parent.peer == null) {
 372                 parent.addNotify();
 373             }
 374             if (peer == null)
 375                 peer = getComponentFactory().createFileDialog(this);
 376             super.addNotify();
 377         }
 378     }
 379 
 380     /**
 381      * Indicates whether this file dialog box is for loading from a file
 382      * or for saving to a file.
 383      *
 384      * @return   the mode of this file dialog window, either
 385      *               {@code FileDialog.LOAD} or
 386      *               {@code FileDialog.SAVE}
 387      * @see      java.awt.FileDialog#LOAD
 388      * @see      java.awt.FileDialog#SAVE
 389      * @see      java.awt.FileDialog#setMode
 390      */
 391     public int getMode() {
 392         return mode;
 393     }
 394 
 395     /**
 396      * Sets the mode of the file dialog.  If {@code mode} is not
 397      * a legal value, an exception will be thrown and {@code mode}
 398      * will not be set.
 399      *
 400      * @param      mode  the mode for this file dialog, either
 401      *                 {@code FileDialog.LOAD} or
 402      *                 {@code FileDialog.SAVE}
 403      * @see        java.awt.FileDialog#LOAD
 404      * @see        java.awt.FileDialog#SAVE
 405      * @see        java.awt.FileDialog#getMode
 406      * @exception  IllegalArgumentException if an illegal file
 407      *                 dialog mode is supplied
 408      * @since      1.1
 409      */
 410     public void setMode(int mode) {
 411         switch (mode) {
 412           case LOAD:
 413           case SAVE:
 414             this.mode = mode;
 415             break;
 416           default:
 417             throw new IllegalArgumentException("illegal file dialog mode");
 418         }
 419     }
 420 
 421     /**
 422      * Gets the directory of this file dialog.
 423      *
 424      * @return  the (potentially {@code null} or invalid)
 425      *          directory of this {@code FileDialog}
 426      * @see       java.awt.FileDialog#setDirectory
 427      */
 428     public String getDirectory() {
 429         return dir;
 430     }
 431 
 432     /**
 433      * Sets the directory of this file dialog window to be the
 434      * specified directory. Specifying a {@code null} or an
 435      * invalid directory implies an implementation-defined default.
 436      * This default will not be realized, however, until the user
 437      * has selected a file. Until this point, {@code getDirectory()}
 438      * will return the value passed into this method.
 439      * <p>
 440      * Specifying "" as the directory is exactly equivalent to
 441      * specifying {@code null} as the directory.
 442      *
 443      * @param     dir   the specified directory
 444      * @see       java.awt.FileDialog#getDirectory
 445      */
 446     public void setDirectory(String dir) {
 447         this.dir = (dir != null && dir.equals("")) ? null : dir;
 448         FileDialogPeer peer = (FileDialogPeer)this.peer;
 449         if (peer != null) {
 450             peer.setDirectory(this.dir);
 451         }
 452     }
 453 
 454     /**
 455      * Gets the selected file of this file dialog.  If the user
 456      * selected {@code CANCEL}, the returned file is {@code null}.
 457      *
 458      * @return    the currently selected file of this file dialog window,
 459      *                or {@code null} if none is selected
 460      * @see       java.awt.FileDialog#setFile
 461      */
 462     public String getFile() {
 463         return file;
 464     }
 465 
 466     /**
 467      * Returns files that the user selects.
 468      * <p>
 469      * If the user cancels the file dialog,
 470      * then the method returns an empty array.
 471      *
 472      * @return    files that the user selects or an empty array
 473      *            if the user cancels the file dialog.
 474      * @see       #setFile(String)
 475      * @see       #getFile
 476      * @since 1.7
 477      */
 478     public File[] getFiles() {
 479         synchronized (getObjectLock()) {


 575 
 576     /**
 577      * Sets the filename filter for this file dialog window to the
 578      * specified filter.
 579      * Filename filters do not function in Sun's reference
 580      * implementation for Microsoft Windows.
 581      *
 582      * @param   filter   the specified filter
 583      * @see     java.io.FilenameFilter
 584      * @see     java.awt.FileDialog#getFilenameFilter
 585      */
 586     public synchronized void setFilenameFilter(FilenameFilter filter) {
 587         this.filter = filter;
 588         FileDialogPeer peer = (FileDialogPeer)this.peer;
 589         if (peer != null) {
 590             peer.setFilenameFilter(filter);
 591         }
 592     }
 593 
 594     /**
 595      * Reads the {@code ObjectInputStream} and performs
 596      * a backwards compatibility check by converting
 597      * either a {@code dir} or a {@code file}
 598      * equal to an empty string to {@code null}.
 599      *
 600      * @param s the {@code ObjectInputStream} to read
 601      */
 602     private void readObject(ObjectInputStream s)
 603         throws ClassNotFoundException, IOException
 604     {
 605         s.defaultReadObject();
 606 
 607         // 1.1 Compatibility: "" is not converted to null in 1.1
 608         if (dir != null && dir.equals("")) {
 609             dir = null;
 610         }
 611         if (file != null && file.equals("")) {
 612             file = null;
 613         }
 614     }
 615 
 616     /**
 617      * Returns a string representing the state of this {@code FileDialog}
 618      * window. This method is intended to be used only for debugging purposes,
 619      * and the content and format of the returned string may vary between
 620      * implementations. The returned string may be empty but may not be
 621      * {@code null}.
 622      *
 623      * @return  the parameter string of this file dialog window
 624      */
 625     protected String paramString() {
 626         String str = super.paramString();
 627         str += ",dir= " + dir;
 628         str += ",file= " + file;
 629         return str + ((mode == LOAD) ? ",load" : ",save");
 630     }
 631 
 632     boolean postsOldMouseEvents() {
 633         return false;
 634     }
 635 }
< prev index next >