jdk/src/share/classes/java/awt/FileDialog.java
author tbell
Wed, 07 Oct 2009 14:15:01 -0700
changeset 3965 63aae8ce7f47
parent 2 90ce3da70b43
child 4913 9b3caa50afcf
permissions -rw-r--r--
Merge
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The <code>FileDialog</code> class displays a dialog window
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * from which the user can select a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Since it is a modal dialog, when the application calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * its <code>show</code> method to display the dialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * it blocks the rest of the application until the user has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * chosen a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see Window#show
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class FileDialog extends Dialog {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * This constant value indicates that the purpose of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * dialog window is to locate a file from which to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final int LOAD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * This constant value indicates that the purpose of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * dialog window is to locate a file to which to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int SAVE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * There are two <code>FileDialog</code> modes: <code>LOAD</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * <code>SAVE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * This integer will represent one or the other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * If the mode is not specified it will default to <code>LOAD</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see getMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @see setMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @see java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The string specifying the directory to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * in the file dialog.  This variable may be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @see getDirectory()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @see setDirectory()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    String dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * The string specifying the initial value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * filename text field in the file dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * This variable may be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @see getFile()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @see setFile()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    String file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The filter used as the file dialog's filename filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The file dialog will only be displaying files whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * names are accepted by this filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * This variable may be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see #getFilenameFilter()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see #setFilenameFilter()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @see FileNameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    FilenameFilter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final String base = "filedlg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     private static final long serialVersionUID = 5035145889651310422L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Initialize JNI field and method IDs for fields that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
       accessed from C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Creates a file dialog for loading a file.  The title of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * file dialog is initially empty.  This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>FileDialog(parent, "", LOAD)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param parent the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public FileDialog(Frame parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this(parent, "", LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Creates a file dialog window with the specified title for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * a file. The files shown are those in the current directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>FileDialog(parent, title, LOAD)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param     title    the title of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public FileDialog(Frame parent, String title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this(parent, title, LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Creates a file dialog window with the specified title for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * or saving a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * If the value of <code>mode</code> is <code>LOAD</code>, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * file dialog is finding a file to read, and the files shown are those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * in the current directory.   If the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * a place to write a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param     title   the title of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param     mode   the mode of the dialog; either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *          <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @exception  IllegalArgumentException if an illegal file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *                 dialog mode is supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see       java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @see       java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public FileDialog(Frame parent, String title, int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        super(parent, title, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this.setMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Creates a file dialog for loading a file.  The title of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * file dialog is initially empty.  This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <code>FileDialog(parent, "", LOAD)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *            <code>GraphicsConfiguration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *            is not from a screen device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @exception java.lang.IllegalArgumentException if <code>parent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *            is <code>null</code>; this exception is always thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *            <code>GraphicsEnvironment.isHeadless</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public FileDialog(Dialog parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this(parent, "", LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Creates a file dialog window with the specified title for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * a file. The files shown are those in the current directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>FileDialog(parent, title, LOAD)</code>.
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; a <code>null</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *                     will be accepted without causing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *                     <code>NullPointerException</code> to be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *            <code>GraphicsConfiguration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *            is not from a screen device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @exception java.lang.IllegalArgumentException if <code>parent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *            is <code>null</code>; this exception is always thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *            <code>GraphicsEnvironment.isHeadless</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @since     1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public FileDialog(Dialog parent, String title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this(parent, title, LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Creates a file dialog window with the specified title for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * or saving a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * If the value of <code>mode</code> is <code>LOAD</code>, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * file dialog is finding a file to read, and the files shown are those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * in the current directory.   If the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * a place to write a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param     parent   the owner of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param     title    the title of the dialog; a <code>null</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *                     will be accepted without causing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *                     <code>NullPointerException</code> to be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param     mode     the mode of the dialog; either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *                     <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @exception java.lang.IllegalArgumentException if an illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *            file dialog mode is supplied;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *            <code>GraphicsConfiguration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *            is not from a screen device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @exception java.lang.IllegalArgumentException if <code>parent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *            is <code>null</code>; this exception is always thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *            <code>GraphicsEnvironment.isHeadless</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see       java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see       java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see       java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @since     1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public FileDialog(Dialog parent, String title, int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        super(parent, title, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        this.setMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Constructs a name for this component. Called by <code>getName()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * when the name is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        synchronized (FileDialog.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return base + nameCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Creates the file dialog's peer.  The peer allows us to change the look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * of the file dialog without changing its functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        synchronized(getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (parent != null && parent.getPeer() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                parent.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (peer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                peer = getToolkit().createFileDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            super.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Indicates whether this file dialog box is for loading from a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * or for saving to a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return   the mode of this file dialog window, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *               <code>FileDialog.LOAD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *               <code>FileDialog.SAVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see      java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @see      java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @see      java.awt.FileDialog#setMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public int getMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Sets the mode of the file dialog.  If <code>mode</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * a legal value, an exception will be thrown and <code>mode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * will not be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param      mode  the mode for this file dialog, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *                 <code>FileDialog.LOAD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *                 <code>FileDialog.SAVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @see        java.awt.FileDialog#LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @see        java.awt.FileDialog#SAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see        java.awt.FileDialog#getMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @exception  IllegalArgumentException if an illegal file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *                 dialog mode is supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void setMode(int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
          case LOAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
          case SAVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            this.mode = mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new IllegalArgumentException("illegal file dialog mode");
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
     * Gets the directory of this file dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @return  the (potentially <code>null</code> or invalid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *          directory of this <code>FileDialog</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see       java.awt.FileDialog#setDirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public String getDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Sets the directory of this file dialog window to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * specified directory. Specifying a <code>null</code> or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * invalid directory implies an implementation-defined default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * This default will not be realized, however, until the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * has selected a file. Until this point, <code>getDirectory()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * will return the value passed into this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Specifying "" as the directory is exactly equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * specifying <code>null</code> as the directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param     dir   the specified directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @see       java.awt.FileDialog#getDirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void setDirectory(String dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        this.dir = (dir != null && dir.equals("")) ? null : dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        FileDialogPeer peer = (FileDialogPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            peer.setDirectory(this.dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Gets the selected file of this file dialog.  If the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * selected <code>CANCEL</code>, the returned file is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return    the currently selected file of this file dialog window,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *                or <code>null</code> if none is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see       java.awt.FileDialog#setFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public String getFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Sets the selected file for this file dialog window to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * specified file. This file becomes the default file if it is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * before the file dialog window is first shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Specifying "" as the file is exactly equivalent to specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * as the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param    file   the file being set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see      java.awt.FileDialog#getFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public void setFile(String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        this.file = (file != null && file.equals("")) ? null : file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        FileDialogPeer peer = (FileDialogPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            peer.setFile(this.file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Determines this file dialog's filename filter. A filename filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * allows the user to specify which files appear in the file dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * window.  Filename filters do not function in Sun's reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * implementation for Microsoft Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @return    this file dialog's filename filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @see       java.io.FilenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @see       java.awt.FileDialog#setFilenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public FilenameFilter getFilenameFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Sets the filename filter for this file dialog window to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * specified filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Filename filters do not function in Sun's reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * implementation for Microsoft Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param   filter   the specified filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @see     java.io.FilenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @see     java.awt.FileDialog#getFilenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public synchronized void setFilenameFilter(FilenameFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        FileDialogPeer peer = (FileDialogPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            peer.setFilenameFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Reads the <code>ObjectInputStream</code> and performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * a backwards compatibility check by converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * either a <code>dir</code> or a <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * equal to an empty string to <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param s the <code>ObjectInputStream</code> to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // 1.1 Compatibility: "" is not converted to null in 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (dir != null && dir.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            dir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (file != null && file.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            file = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Returns a string representing the state of this <code>FileDialog</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * window. This method is intended to be used only for debugging purposes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * and the content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @return  the parameter string of this file dialog window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        String str = super.paramString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        str += ",dir= " + dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        str += ",file= " + file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return str + ((mode == LOAD) ? ",load" : ",save");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    boolean postsOldMouseEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
}