0N/A/*
3261N/A * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.awt;
0N/A
0N/Aimport java.awt.peer.FileDialogPeer;
0N/Aimport java.io.FilenameFilter;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
2153N/Aimport java.io.File;
2153N/Aimport sun.awt.AWTAccessor;
0N/A
0N/A/**
0N/A * The <code>FileDialog</code> class displays a dialog window
0N/A * from which the user can select a file.
0N/A * <p>
0N/A * Since it is a modal dialog, when the application calls
0N/A * its <code>show</code> method to display the dialog,
0N/A * it blocks the rest of the application until the user has
0N/A * chosen a file.
0N/A *
0N/A * @see Window#show
0N/A *
0N/A * @author Sami Shaio
0N/A * @author Arthur van Hoff
0N/A * @since JDK1.0
0N/A */
0N/Apublic class FileDialog extends Dialog {
0N/A
0N/A /**
0N/A * This constant value indicates that the purpose of the file
0N/A * dialog window is to locate a file from which to read.
0N/A */
0N/A public static final int LOAD = 0;
0N/A
0N/A /**
0N/A * This constant value indicates that the purpose of the file
0N/A * dialog window is to locate a file to which to write.
0N/A */
0N/A public static final int SAVE = 1;
0N/A
0N/A /*
0N/A * There are two <code>FileDialog</code> modes: <code>LOAD</code> and
0N/A * <code>SAVE</code>.
0N/A * This integer will represent one or the other.
0N/A * If the mode is not specified it will default to <code>LOAD</code>.
0N/A *
0N/A * @serial
0N/A * @see getMode()
0N/A * @see setMode()
0N/A * @see java.awt.FileDialog#LOAD
0N/A * @see java.awt.FileDialog#SAVE
0N/A */
0N/A int mode;
0N/A
0N/A /*
0N/A * The string specifying the directory to display
0N/A * in the file dialog. This variable may be <code>null</code>.
0N/A *
0N/A * @serial
0N/A * @see getDirectory()
0N/A * @see setDirectory()
0N/A */
0N/A String dir;
0N/A
0N/A /*
0N/A * The string specifying the initial value of the
0N/A * filename text field in the file dialog.
0N/A * This variable may be <code>null</code>.
0N/A *
0N/A * @serial
0N/A * @see getFile()
0N/A * @see setFile()
0N/A */
0N/A String file;
0N/A
2153N/A /**
2153N/A * Contains the File instances for all the files that the user selects.
2153N/A *
2153N/A * @serial
3076N/A * @see #getFiles
2153N/A * @since 1.7
2153N/A */
2153N/A private File[] files;
2153N/A
2153N/A /**
2153N/A * Represents whether the file dialog allows the multiple file selection.
2153N/A *
2153N/A * @serial
2153N/A * @see #setMultipleMode
2153N/A * @see #isMultipleMode
2153N/A * @since 1.7
2153N/A */
2153N/A private boolean multipleMode = false;
2153N/A
0N/A /*
0N/A * The filter used as the file dialog's filename filter.
0N/A * The file dialog will only be displaying files whose
0N/A * names are accepted by this filter.
0N/A * This variable may be <code>null</code>.
0N/A *
0N/A * @serial
0N/A * @see #getFilenameFilter()
0N/A * @see #setFilenameFilter()
0N/A * @see FileNameFilter
0N/A */
0N/A FilenameFilter filter;
0N/A
0N/A private static final String base = "filedlg";
0N/A private static int nameCounter = 0;
0N/A
0N/A /*
0N/A * JDK 1.1 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 5035145889651310422L;
0N/A
0N/A
0N/A static {
0N/A /* ensure that the necessary native libraries are loaded */
0N/A Toolkit.loadLibraries();
0N/A if (!GraphicsEnvironment.isHeadless()) {
0N/A initIDs();
0N/A }
0N/A }
0N/A
2153N/A static {
2153N/A AWTAccessor.setFileDialogAccessor(
2153N/A new AWTAccessor.FileDialogAccessor() {
4787N/A public void setFiles(FileDialog fileDialog, File files[]) {
4787N/A fileDialog.setFiles(files);
2153N/A }
2153N/A public void setFile(FileDialog fileDialog, String file) {
2153N/A fileDialog.file = ("".equals(file)) ? null : file;
2153N/A }
2153N/A public void setDirectory(FileDialog fileDialog, String directory) {
2153N/A fileDialog.dir = ("".equals(directory)) ? null : directory;
2153N/A }
2153N/A public boolean isMultipleMode(FileDialog fileDialog) {
2153N/A synchronized (fileDialog.getObjectLock()) {
2153N/A return fileDialog.multipleMode;
2153N/A }
2153N/A }
2153N/A });
2153N/A }
2153N/A
0N/A /**
0N/A * Initialize JNI field and method IDs for fields that may be
0N/A accessed from C.
0N/A */
0N/A private static native void initIDs();
0N/A
0N/A /**
0N/A * Creates a file dialog for loading a file. The title of the
0N/A * file dialog is initially empty. This is a convenience method for
0N/A * <code>FileDialog(parent, "", LOAD)</code>.
0N/A *
0N/A * @param parent the owner of the dialog
0N/A * @since JDK1.1
0N/A */
0N/A public FileDialog(Frame parent) {
0N/A this(parent, "", LOAD);
0N/A }
0N/A
0N/A /**
0N/A * Creates a file dialog window with the specified title for loading
0N/A * a file. The files shown are those in the current directory.
0N/A * This is a convenience method for
0N/A * <code>FileDialog(parent, title, LOAD)</code>.
0N/A *
0N/A * @param parent the owner of the dialog
0N/A * @param title the title of the dialog
0N/A */
0N/A public FileDialog(Frame parent, String title) {
0N/A this(parent, title, LOAD);
0N/A }
0N/A
0N/A /**
0N/A * Creates a file dialog window with the specified title for loading
0N/A * or saving a file.
0N/A * <p>
0N/A * If the value of <code>mode</code> is <code>LOAD</code>, then the
0N/A * file dialog is finding a file to read, and the files shown are those
0N/A * in the current directory. If the value of
0N/A * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
0N/A * a place to write a file.
0N/A *
0N/A * @param parent the owner of the dialog
0N/A * @param title the title of the dialog
0N/A * @param mode the mode of the dialog; either
0N/A * <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
0N/A * @exception IllegalArgumentException if an illegal file
0N/A * dialog mode is supplied
0N/A * @see java.awt.FileDialog#LOAD
0N/A * @see java.awt.FileDialog#SAVE
0N/A */
0N/A public FileDialog(Frame parent, String title, int mode) {
0N/A super(parent, title, true);
0N/A this.setMode(mode);
0N/A setLayout(null);
0N/A }
0N/A
0N/A /**
0N/A * Creates a file dialog for loading a file. The title of the
0N/A * file dialog is initially empty. This is a convenience method for
0N/A * <code>FileDialog(parent, "", LOAD)</code>.
0N/A *
0N/A * @param parent the owner of the dialog
0N/A * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
0N/A * <code>GraphicsConfiguration</code>
0N/A * is not from a screen device;
0N/A * @exception java.lang.IllegalArgumentException if <code>parent</code>
0N/A * is <code>null</code>; this exception is always thrown when
0N/A * <code>GraphicsEnvironment.isHeadless</code>
0N/A * returns <code>true</code>
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @since 1.5
0N/A */
0N/A public FileDialog(Dialog parent) {
0N/A this(parent, "", LOAD);
0N/A }
0N/A
0N/A /**
0N/A * Creates a file dialog window with the specified title for loading
0N/A * a file. The files shown are those in the current directory.
0N/A * This is a convenience method for
0N/A * <code>FileDialog(parent, title, LOAD)</code>.
0N/A *
0N/A * @param parent the owner of the dialog
0N/A * @param title the title of the dialog; a <code>null</code> value
0N/A * will be accepted without causing a
0N/A * <code>NullPointerException</code> to be thrown
0N/A * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
0N/A * <code>GraphicsConfiguration</code>
0N/A * is not from a screen device;
0N/A * @exception java.lang.IllegalArgumentException if <code>parent</code>
0N/A * is <code>null</code>; this exception is always thrown when
0N/A * <code>GraphicsEnvironment.isHeadless</code>
0N/A * returns <code>true</code>
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @since 1.5
0N/A */
0N/A public FileDialog(Dialog parent, String title) {
0N/A this(parent, title, LOAD);
0N/A }
0N/A
0N/A /**
0N/A * Creates a file dialog window with the specified title for loading
0N/A * or saving a file.
0N/A * <p>
0N/A * If the value of <code>mode</code> is <code>LOAD</code>, then the
0N/A * file dialog is finding a file to read, and the files shown are those
0N/A * in the current directory. If the value of
0N/A * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
0N/A * a place to write a file.
0N/A *
0N/A * @param parent the owner of the dialog
0N/A * @param title the title of the dialog; a <code>null</code> value
0N/A * will be accepted without causing a
0N/A * <code>NullPointerException</code> to be thrown
0N/A * @param mode the mode of the dialog; either
0N/A * <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
0N/A * @exception java.lang.IllegalArgumentException if an illegal
0N/A * file dialog mode is supplied;
0N/A * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
0N/A * <code>GraphicsConfiguration</code>
0N/A * is not from a screen device;
0N/A * @exception java.lang.IllegalArgumentException if <code>parent</code>
0N/A * is <code>null</code>; this exception is always thrown when
0N/A * <code>GraphicsEnvironment.isHeadless</code>
0N/A * returns <code>true</code>
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @see java.awt.FileDialog#LOAD
0N/A * @see java.awt.FileDialog#SAVE
0N/A * @since 1.5
0N/A */
0N/A public FileDialog(Dialog parent, String title, int mode) {
0N/A super(parent, title, true);
0N/A this.setMode(mode);
0N/A setLayout(null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a name for this component. Called by <code>getName()</code>
0N/A * when the name is <code>null</code>.
0N/A */
0N/A String constructComponentName() {
0N/A synchronized (FileDialog.class) {
0N/A return base + nameCounter++;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates the file dialog's peer. The peer allows us to change the look
0N/A * of the file dialog without changing its functionality.
0N/A */
0N/A public void addNotify() {
0N/A synchronized(getTreeLock()) {
0N/A if (parent != null && parent.getPeer() == null) {
0N/A parent.addNotify();
0N/A }
0N/A if (peer == null)
0N/A peer = getToolkit().createFileDialog(this);
0N/A super.addNotify();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Indicates whether this file dialog box is for loading from a file
0N/A * or for saving to a file.
0N/A *
0N/A * @return the mode of this file dialog window, either
0N/A * <code>FileDialog.LOAD</code> or
0N/A * <code>FileDialog.SAVE</code>
0N/A * @see java.awt.FileDialog#LOAD
0N/A * @see java.awt.FileDialog#SAVE
0N/A * @see java.awt.FileDialog#setMode
0N/A */
0N/A public int getMode() {
0N/A return mode;
0N/A }
0N/A
0N/A /**
0N/A * Sets the mode of the file dialog. If <code>mode</code> is not
0N/A * a legal value, an exception will be thrown and <code>mode</code>
0N/A * will not be set.
0N/A *
0N/A * @param mode the mode for this file dialog, either
0N/A * <code>FileDialog.LOAD</code> or
0N/A * <code>FileDialog.SAVE</code>
0N/A * @see java.awt.FileDialog#LOAD
0N/A * @see java.awt.FileDialog#SAVE
0N/A * @see java.awt.FileDialog#getMode
0N/A * @exception IllegalArgumentException if an illegal file
0N/A * dialog mode is supplied
0N/A * @since JDK1.1
0N/A */
0N/A public void setMode(int mode) {
0N/A switch (mode) {
0N/A case LOAD:
0N/A case SAVE:
0N/A this.mode = mode;
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException("illegal file dialog mode");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the directory of this file dialog.
0N/A *
0N/A * @return the (potentially <code>null</code> or invalid)
0N/A * directory of this <code>FileDialog</code>
0N/A * @see java.awt.FileDialog#setDirectory
0N/A */
0N/A public String getDirectory() {
0N/A return dir;
0N/A }
0N/A
0N/A /**
0N/A * Sets the directory of this file dialog window to be the
0N/A * specified directory. Specifying a <code>null</code> or an
0N/A * invalid directory implies an implementation-defined default.
0N/A * This default will not be realized, however, until the user
0N/A * has selected a file. Until this point, <code>getDirectory()</code>
0N/A * will return the value passed into this method.
0N/A * <p>
0N/A * Specifying "" as the directory is exactly equivalent to
0N/A * specifying <code>null</code> as the directory.
0N/A *
0N/A * @param dir the specified directory
0N/A * @see java.awt.FileDialog#getDirectory
0N/A */
0N/A public void setDirectory(String dir) {
0N/A this.dir = (dir != null && dir.equals("")) ? null : dir;
0N/A FileDialogPeer peer = (FileDialogPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setDirectory(this.dir);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the selected file of this file dialog. If the user
0N/A * selected <code>CANCEL</code>, the returned file is <code>null</code>.
0N/A *
0N/A * @return the currently selected file of this file dialog window,
0N/A * or <code>null</code> if none is selected
0N/A * @see java.awt.FileDialog#setFile
0N/A */
0N/A public String getFile() {
0N/A return file;
0N/A }
0N/A
0N/A /**
2153N/A * Returns files that the user selects.
2153N/A * <p>
2153N/A * If the user cancels the file dialog,
2153N/A * then the method returns an empty array.
2153N/A *
2153N/A * @return files that the user selects or an empty array
2153N/A * if the user cancels the file dialog.
2153N/A * @see #setFile(String)
2153N/A * @see #getFile
2153N/A * @since 1.7
2153N/A */
2153N/A public File[] getFiles() {
2153N/A synchronized (getObjectLock()) {
2153N/A if (files != null) {
2153N/A return files.clone();
2153N/A } else {
2153N/A return new File[0];
2153N/A }
2153N/A }
2153N/A }
2153N/A
2153N/A /**
2153N/A * Stores the names of all the files that the user selects.
2153N/A *
2153N/A * Note that the method is private and it's intended to be used
2153N/A * by the peers through the AWTAccessor API.
2153N/A *
2153N/A * @param directory the current directory
2153N/A * @param files the array that contains the short names of
2153N/A * all the files that the user selects.
2153N/A *
2153N/A * @see #getFiles
2153N/A * @since 1.7
2153N/A */
4787N/A private void setFiles(File files[]) {
2153N/A synchronized (getObjectLock()) {
4787N/A this.files = files;
2153N/A }
2153N/A }
2153N/A
2153N/A /**
0N/A * Sets the selected file for this file dialog window to be the
0N/A * specified file. This file becomes the default file if it is set
0N/A * before the file dialog window is first shown.
0N/A * <p>
0N/A * Specifying "" as the file is exactly equivalent to specifying
0N/A * <code>null</code>
0N/A * as the file.
0N/A *
0N/A * @param file the file being set
2153N/A * @see #getFile
2153N/A * @see #getFiles
0N/A */
0N/A public void setFile(String file) {
0N/A this.file = (file != null && file.equals("")) ? null : file;
0N/A FileDialogPeer peer = (FileDialogPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setFile(this.file);
0N/A }
0N/A }
0N/A
0N/A /**
2153N/A * Enables or disables multiple file selection for the file dialog.
2153N/A *
2153N/A * @param enable if {@code true}, multiple file selection is enabled;
2153N/A * {@code false} - disabled.
2153N/A * @see #isMultipleMode
2153N/A * @since 1.7
2153N/A */
2153N/A public void setMultipleMode(boolean enable) {
2153N/A synchronized (getObjectLock()) {
2153N/A this.multipleMode = enable;
2153N/A }
2153N/A }
2153N/A
2153N/A /**
2153N/A * Returns whether the file dialog allows the multiple file selection.
2153N/A *
2153N/A * @return {@code true} if the file dialog allows the multiple
2153N/A * file selection; {@code false} otherwise.
2153N/A * @see #setMultipleMode
2153N/A * @since 1.7
2153N/A */
2153N/A public boolean isMultipleMode() {
2153N/A synchronized (getObjectLock()) {
2153N/A return multipleMode;
2153N/A }
2153N/A }
2153N/A
2153N/A /**
0N/A * Determines this file dialog's filename filter. A filename filter
0N/A * allows the user to specify which files appear in the file dialog
0N/A * window. Filename filters do not function in Sun's reference
0N/A * implementation for Microsoft Windows.
0N/A *
0N/A * @return this file dialog's filename filter
0N/A * @see java.io.FilenameFilter
0N/A * @see java.awt.FileDialog#setFilenameFilter
0N/A */
0N/A public FilenameFilter getFilenameFilter() {
0N/A return filter;
0N/A }
0N/A
0N/A /**
0N/A * Sets the filename filter for this file dialog window to the
0N/A * specified filter.
0N/A * Filename filters do not function in Sun's reference
0N/A * implementation for Microsoft Windows.
0N/A *
0N/A * @param filter the specified filter
0N/A * @see java.io.FilenameFilter
0N/A * @see java.awt.FileDialog#getFilenameFilter
0N/A */
0N/A public synchronized void setFilenameFilter(FilenameFilter filter) {
0N/A this.filter = filter;
0N/A FileDialogPeer peer = (FileDialogPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setFilenameFilter(filter);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads the <code>ObjectInputStream</code> and performs
0N/A * a backwards compatibility check by converting
0N/A * either a <code>dir</code> or a <code>file</code>
0N/A * equal to an empty string to <code>null</code>.
0N/A *
0N/A * @param s the <code>ObjectInputStream</code> to read
0N/A */
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException
0N/A {
0N/A s.defaultReadObject();
0N/A
0N/A // 1.1 Compatibility: "" is not converted to null in 1.1
0N/A if (dir != null && dir.equals("")) {
0N/A dir = null;
0N/A }
0N/A if (file != null && file.equals("")) {
0N/A file = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representing the state of this <code>FileDialog</code>
0N/A * window. This method is intended to be used only for debugging purposes,
0N/A * and the content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not be
0N/A * <code>null</code>.
0N/A *
0N/A * @return the parameter string of this file dialog window
0N/A */
0N/A protected String paramString() {
0N/A String str = super.paramString();
0N/A str += ",dir= " + dir;
0N/A str += ",file= " + file;
0N/A return str + ((mode == LOAD) ? ",load" : ",save");
0N/A }
0N/A
0N/A boolean postsOldMouseEvents() {
0N/A return false;
0N/A }
0N/A}