jdk/src/share/classes/java/awt/FileDialog.java
changeset 4913 9b3caa50afcf
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
4837:8c4c2196ce14 4913:9b3caa50afcf
    26 
    26 
    27 import java.awt.peer.FileDialogPeer;
    27 import java.awt.peer.FileDialogPeer;
    28 import java.io.FilenameFilter;
    28 import java.io.FilenameFilter;
    29 import java.io.IOException;
    29 import java.io.IOException;
    30 import java.io.ObjectInputStream;
    30 import java.io.ObjectInputStream;
       
    31 import java.io.File;
       
    32 import sun.awt.AWTAccessor;
    31 
    33 
    32 /**
    34 /**
    33  * The <code>FileDialog</code> class displays a dialog window
    35  * The <code>FileDialog</code> class displays a dialog window
    34  * from which the user can select a file.
    36  * from which the user can select a file.
    35  * <p>
    37  * <p>
    91      * @see getFile()
    93      * @see getFile()
    92      * @see setFile()
    94      * @see setFile()
    93      */
    95      */
    94     String file;
    96     String file;
    95 
    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 
    96     /*
   117     /*
    97      * The filter used as the file dialog's filename filter.
   118      * The filter used as the file dialog's filename filter.
    98      * The file dialog will only be displaying files whose
   119      * The file dialog will only be displaying files whose
    99      * names are accepted by this filter.
   120      * names are accepted by this filter.
   100      * This variable may be <code>null</code>.
   121      * This variable may be <code>null</code>.
   119         /* ensure that the necessary native libraries are loaded */
   140         /* ensure that the necessary native libraries are loaded */
   120         Toolkit.loadLibraries();
   141         Toolkit.loadLibraries();
   121         if (!GraphicsEnvironment.isHeadless()) {
   142         if (!GraphicsEnvironment.isHeadless()) {
   122             initIDs();
   143             initIDs();
   123         }
   144         }
       
   145     }
       
   146 
       
   147     static {
       
   148         AWTAccessor.setFileDialogAccessor(
       
   149             new AWTAccessor.FileDialogAccessor() {
       
   150                 public void setFiles(FileDialog fileDialog, String directory, String files[]) {
       
   151                     fileDialog.setFiles(directory, files);
       
   152                 }
       
   153                 public void setFile(FileDialog fileDialog, String file) {
       
   154                     fileDialog.file = ("".equals(file)) ? null : file;
       
   155                 }
       
   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             });
   124     }
   165     }
   125 
   166 
   126     /**
   167     /**
   127      * Initialize JNI field and method IDs for fields that may be
   168      * Initialize JNI field and method IDs for fields that may be
   128        accessed from C.
   169        accessed from C.
   369     public String getFile() {
   410     public String getFile() {
   370         return file;
   411         return file;
   371     }
   412     }
   372 
   413 
   373     /**
   414     /**
       
   415      * Returns files that the user selects.
       
   416      * <p>
       
   417      * If the user cancels the file dialog,
       
   418      * then the method returns an empty array.
       
   419      *
       
   420      * @return    files that the user selects or an empty array
       
   421      *            if the user cancels the file dialog.
       
   422      * @see       #setFile(String)
       
   423      * @see       #getFile
       
   424      * @since 1.7
       
   425      */
       
   426     public File[] getFiles() {
       
   427         synchronized (getObjectLock()) {
       
   428             if (files != null) {
       
   429                 return files.clone();
       
   430             } else {
       
   431                 return new File[0];
       
   432             }
       
   433         }
       
   434     }
       
   435 
       
   436     /**
       
   437      * Stores the names of all the files that the user selects.
       
   438      *
       
   439      * Note that the method is private and it's intended to be used
       
   440      * by the peers through the AWTAccessor API.
       
   441      *
       
   442      * @param directory the current directory
       
   443      * @param files     the array that contains the short names of
       
   444      *                  all the files that the user selects.
       
   445      *
       
   446      * @see #getFiles
       
   447      * @since 1.7
       
   448      */
       
   449     private void setFiles(String directory, String files[]) {
       
   450         synchronized (getObjectLock()) {
       
   451             int filesNumber = (files != null) ? files.length : 0;
       
   452             this.files = new File[filesNumber];
       
   453             for (int i = 0; i < filesNumber; i++) {
       
   454                 this.files[i] = new File(directory, files[i]);
       
   455             }
       
   456         }
       
   457     }
       
   458 
       
   459     /**
   374      * Sets the selected file for this file dialog window to be the
   460      * Sets the selected file for this file dialog window to be the
   375      * specified file. This file becomes the default file if it is set
   461      * specified file. This file becomes the default file if it is set
   376      * before the file dialog window is first shown.
   462      * before the file dialog window is first shown.
   377      * <p>
   463      * <p>
   378      * Specifying "" as the file is exactly equivalent to specifying
   464      * Specifying "" as the file is exactly equivalent to specifying
   379      * <code>null</code>
   465      * <code>null</code>
   380      * as the file.
   466      * as the file.
   381      *
   467      *
   382      * @param    file   the file being set
   468      * @param    file   the file being set
   383      * @see      java.awt.FileDialog#getFile
   469      * @see      #getFile
       
   470      * @see      #getFiles
   384      */
   471      */
   385     public void setFile(String file) {
   472     public void setFile(String file) {
   386         this.file = (file != null && file.equals("")) ? null : file;
   473         this.file = (file != null && file.equals("")) ? null : file;
   387         FileDialogPeer peer = (FileDialogPeer)this.peer;
   474         FileDialogPeer peer = (FileDialogPeer)this.peer;
   388         if (peer != null) {
   475         if (peer != null) {
   389             peer.setFile(this.file);
   476             peer.setFile(this.file);
   390         }
   477         }
   391     }
   478     }
   392 
   479 
   393     /**
   480     /**
       
   481      * Enables or disables multiple file selection for the file dialog.
       
   482      *
       
   483      * @param enable    if {@code true}, multiple file selection is enabled;
       
   484      *                  {@code false} - disabled.
       
   485      * @see #isMultipleMode
       
   486      * @since 1.7
       
   487      */
       
   488     public void setMultipleMode(boolean enable) {
       
   489         synchronized (getObjectLock()) {
       
   490             this.multipleMode = enable;
       
   491         }
       
   492     }
       
   493 
       
   494     /**
       
   495      * Returns whether the file dialog allows the multiple file selection.
       
   496      *
       
   497      * @return          {@code true} if the file dialog allows the multiple
       
   498      *                  file selection; {@code false} otherwise.
       
   499      * @see #setMultipleMode
       
   500      * @since 1.7
       
   501      */
       
   502     public boolean isMultipleMode() {
       
   503         synchronized (getObjectLock()) {
       
   504             return multipleMode;
       
   505         }
       
   506     }
       
   507 
       
   508     /**
   394      * Determines this file dialog's filename filter. A filename filter
   509      * Determines this file dialog's filename filter. A filename filter
   395      * allows the user to specify which files appear in the file dialog
   510      * allows the user to specify which files appear in the file dialog
   396      * window.  Filename filters do not function in Sun's reference
   511      * window.  Filename filters do not function in Sun's reference
   397      * implementation for Microsoft Windows.
   512      * implementation for Microsoft Windows.
   398      *
   513      *