jdk/src/share/classes/java/awt/FileDialog.java
author dcherepanov
Fri, 12 Feb 2010 19:58:10 +0300
changeset 4913 9b3caa50afcf
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6705345: Enable multiple file selection in AWT FileDialog Reviewed-by: art, anthony, alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.peer.FileDialogPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FilenameFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectInputStream;
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
    31
import java.io.File;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
    32
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The <code>FileDialog</code> class displays a dialog window
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * from which the user can select a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Since it is a modal dialog, when the application calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * its <code>show</code> method to display the dialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * it blocks the rest of the application until the user has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * chosen a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see Window#show
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class FileDialog extends Dialog {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * This constant value indicates that the purpose of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * dialog window is to locate a file from which to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final int LOAD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * This constant value indicates that the purpose of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * dialog window is to locate a file to which to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final int SAVE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * There are two <code>FileDialog</code> modes: <code>LOAD</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <code>SAVE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * This integer will represent one or the other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * If the mode is not specified it will default to <code>LOAD</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see getMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @see setMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @see java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @see java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The string specifying the directory to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * in the file dialog.  This variable may be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @see getDirectory()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @see setDirectory()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    String dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The string specifying the initial value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * filename text field in the file dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * This variable may be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @see getFile()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see setFile()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    String file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
    98
    /**
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
    99
     * Contains the File instances for all the files that the user selects.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   100
     *
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   101
     * @serial
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   102
     * @see getFiles
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   103
     * @since 1.7
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   104
     */
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   105
    private File[] files;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   106
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   107
    /**
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   108
     * Represents whether the file dialog allows the multiple file selection.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   109
     *
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   110
     * @serial
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   111
     * @see #setMultipleMode
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   112
     * @see #isMultipleMode
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   113
     * @since 1.7
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   114
     */
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   115
    private boolean multipleMode = false;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   116
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The filter used as the file dialog's filename filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The file dialog will only be displaying files whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * names are accepted by this filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * This variable may be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see #getFilenameFilter()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see #setFilenameFilter()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see FileNameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    FilenameFilter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final String base = "filedlg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     private static final long serialVersionUID = 5035145889651310422L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   147
    static {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   148
        AWTAccessor.setFileDialogAccessor(
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   149
            new AWTAccessor.FileDialogAccessor() {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   150
                public void setFiles(FileDialog fileDialog, String directory, String files[]) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   151
                    fileDialog.setFiles(directory, files);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   152
                }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   153
                public void setFile(FileDialog fileDialog, String file) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   154
                    fileDialog.file = ("".equals(file)) ? null : file;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   155
                }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   156
                public void setDirectory(FileDialog fileDialog, String directory) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   157
                    fileDialog.dir = ("".equals(directory)) ? null : directory;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   158
                }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   159
                public boolean isMultipleMode(FileDialog fileDialog) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   160
                    synchronized (fileDialog.getObjectLock()) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   161
                        return fileDialog.multipleMode;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   162
                    }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   163
                }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   164
            });
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   165
    }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   166
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Initialize JNI field and method IDs for fields that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
       accessed from C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Creates a file dialog for loading a file.  The title of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * file dialog is initially empty.  This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>FileDialog(parent, "", LOAD)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param parent the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public FileDialog(Frame parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        this(parent, "", LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Creates a file dialog window with the specified title for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * a file. The files shown are those in the current directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <code>FileDialog(parent, title, LOAD)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param     title    the title of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public FileDialog(Frame parent, String title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this(parent, title, LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Creates a file dialog window with the specified title for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * or saving a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * If the value of <code>mode</code> is <code>LOAD</code>, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * file dialog is finding a file to read, and the files shown are those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * in the current directory.   If the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * a place to write a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param     title   the title of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param     mode   the mode of the dialog; either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *          <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception  IllegalArgumentException if an illegal file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *                 dialog mode is supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see       java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see       java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public FileDialog(Frame parent, String title, int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        super(parent, title, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.setMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Creates a file dialog for loading a file.  The title of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * file dialog is initially empty.  This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <code>FileDialog(parent, "", LOAD)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *            <code>GraphicsConfiguration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *            is not from a screen device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @exception java.lang.IllegalArgumentException if <code>parent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *            is <code>null</code>; this exception is always thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *            <code>GraphicsEnvironment.isHeadless</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public FileDialog(Dialog parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        this(parent, "", LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Creates a file dialog window with the specified title for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * a file. The files shown are those in the current directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <code>FileDialog(parent, title, LOAD)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param     title    the title of the dialog; a <code>null</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *                     will be accepted without causing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *                     <code>NullPointerException</code> to be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *            <code>GraphicsConfiguration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *            is not from a screen device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @exception java.lang.IllegalArgumentException if <code>parent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *            is <code>null</code>; this exception is always thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *            <code>GraphicsEnvironment.isHeadless</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @since     1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public FileDialog(Dialog parent, String title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        this(parent, title, LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Creates a file dialog window with the specified title for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * or saving a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * If the value of <code>mode</code> is <code>LOAD</code>, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * file dialog is finding a file to read, and the files shown are those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * in the current directory.   If the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * a place to write a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param     title    the title of the dialog; a <code>null</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *                     will be accepted without causing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *                     <code>NullPointerException</code> to be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param     mode     the mode of the dialog; either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *                     <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @exception java.lang.IllegalArgumentException if an illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *            file dialog mode is supplied;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *            <code>GraphicsConfiguration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *            is not from a screen device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @exception java.lang.IllegalArgumentException if <code>parent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *            is <code>null</code>; this exception is always thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *            <code>GraphicsEnvironment.isHeadless</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @see       java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @see       java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see       java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @since     1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public FileDialog(Dialog parent, String title, int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        super(parent, title, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        this.setMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Constructs a name for this component. Called by <code>getName()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * when the name is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        synchronized (FileDialog.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return base + nameCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Creates the file dialog's peer.  The peer allows us to change the look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * of the file dialog without changing its functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        synchronized(getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (parent != null && parent.getPeer() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                parent.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (peer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                peer = getToolkit().createFileDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            super.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Indicates whether this file dialog box is for loading from a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * or for saving to a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return   the mode of this file dialog window, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *               <code>FileDialog.LOAD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *               <code>FileDialog.SAVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @see      java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see      java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @see      java.awt.FileDialog#setMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public int getMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Sets the mode of the file dialog.  If <code>mode</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * a legal value, an exception will be thrown and <code>mode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * will not be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param      mode  the mode for this file dialog, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *                 <code>FileDialog.LOAD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *                 <code>FileDialog.SAVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @see        java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see        java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see        java.awt.FileDialog#getMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @exception  IllegalArgumentException if an illegal file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *                 dialog mode is supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public void setMode(int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
          case LOAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
          case SAVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            this.mode = mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw new IllegalArgumentException("illegal file dialog mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Gets the directory of this file dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @return  the (potentially <code>null</code> or invalid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *          directory of this <code>FileDialog</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @see       java.awt.FileDialog#setDirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public String getDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Sets the directory of this file dialog window to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * specified directory. Specifying a <code>null</code> or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * invalid directory implies an implementation-defined default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * This default will not be realized, however, until the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * has selected a file. Until this point, <code>getDirectory()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * will return the value passed into this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Specifying "" as the directory is exactly equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * specifying <code>null</code> as the directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param     dir   the specified directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @see       java.awt.FileDialog#getDirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public void setDirectory(String dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        this.dir = (dir != null && dir.equals("")) ? null : dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        FileDialogPeer peer = (FileDialogPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            peer.setDirectory(this.dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Gets the selected file of this file dialog.  If the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * selected <code>CANCEL</code>, the returned file is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @return    the currently selected file of this file dialog window,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *                or <code>null</code> if none is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see       java.awt.FileDialog#setFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public String getFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   415
     * Returns files that the user selects.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   416
     * <p>
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   417
     * If the user cancels the file dialog,
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   418
     * then the method returns an empty array.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   419
     *
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   420
     * @return    files that the user selects or an empty array
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   421
     *            if the user cancels the file dialog.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   422
     * @see       #setFile(String)
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   423
     * @see       #getFile
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   424
     * @since 1.7
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   425
     */
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   426
    public File[] getFiles() {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   427
        synchronized (getObjectLock()) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   428
            if (files != null) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   429
                return files.clone();
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   430
            } else {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   431
                return new File[0];
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   432
            }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   433
        }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   434
    }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   435
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   436
    /**
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   437
     * Stores the names of all the files that the user selects.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   438
     *
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   439
     * Note that the method is private and it's intended to be used
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   440
     * by the peers through the AWTAccessor API.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   441
     *
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   442
     * @param directory the current directory
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   443
     * @param files     the array that contains the short names of
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   444
     *                  all the files that the user selects.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   445
     *
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   446
     * @see #getFiles
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   447
     * @since 1.7
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   448
     */
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   449
    private void setFiles(String directory, String files[]) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   450
        synchronized (getObjectLock()) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   451
            int filesNumber = (files != null) ? files.length : 0;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   452
            this.files = new File[filesNumber];
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   453
            for (int i = 0; i < filesNumber; i++) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   454
                this.files[i] = new File(directory, files[i]);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   455
            }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   456
        }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   457
    }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   458
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   459
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Sets the selected file for this file dialog window to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * specified file. This file becomes the default file if it is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * before the file dialog window is first shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Specifying "" as the file is exactly equivalent to specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * as the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param    file   the file being set
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   469
     * @see      #getFile
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   470
     * @see      #getFiles
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public void setFile(String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        this.file = (file != null && file.equals("")) ? null : file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        FileDialogPeer peer = (FileDialogPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            peer.setFile(this.file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   481
     * Enables or disables multiple file selection for the file dialog.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   482
     *
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   483
     * @param enable    if {@code true}, multiple file selection is enabled;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   484
     *                  {@code false} - disabled.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   485
     * @see #isMultipleMode
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   486
     * @since 1.7
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   487
     */
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   488
    public void setMultipleMode(boolean enable) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   489
        synchronized (getObjectLock()) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   490
            this.multipleMode = enable;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   491
        }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   492
    }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   493
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   494
    /**
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   495
     * Returns whether the file dialog allows the multiple file selection.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   496
     *
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   497
     * @return          {@code true} if the file dialog allows the multiple
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   498
     *                  file selection; {@code false} otherwise.
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   499
     * @see #setMultipleMode
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   500
     * @since 1.7
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   501
     */
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   502
    public boolean isMultipleMode() {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   503
        synchronized (getObjectLock()) {
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   504
            return multipleMode;
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   505
        }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   506
    }
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   507
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 2
diff changeset
   508
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Determines this file dialog's filename filter. A filename filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * allows the user to specify which files appear in the file dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * window.  Filename filters do not function in Sun's reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * implementation for Microsoft Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @return    this file dialog's filename filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @see       java.io.FilenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @see       java.awt.FileDialog#setFilenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public FilenameFilter getFilenameFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Sets the filename filter for this file dialog window to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * specified filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Filename filters do not function in Sun's reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * implementation for Microsoft Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param   filter   the specified filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @see     java.io.FilenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see     java.awt.FileDialog#getFilenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public synchronized void setFilenameFilter(FilenameFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        FileDialogPeer peer = (FileDialogPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            peer.setFilenameFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Reads the <code>ObjectInputStream</code> and performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * a backwards compatibility check by converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * either a <code>dir</code> or a <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * equal to an empty string to <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param s the <code>ObjectInputStream</code> to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // 1.1 Compatibility: "" is not converted to null in 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (dir != null && dir.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            dir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (file != null && file.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            file = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Returns a string representing the state of this <code>FileDialog</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * window. This method is intended to be used only for debugging purposes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * and the content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @return  the parameter string of this file dialog window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        String str = super.paramString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        str += ",dir= " + dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        str += ",file= " + file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return str + ((mode == LOAD) ? ",load" : ",save");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    boolean postsOldMouseEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
}