Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/FileDialog.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.awt;2526import java.awt.peer.FileDialogPeer;27import java.io.FilenameFilter;28import java.io.IOException;29import java.io.ObjectInputStream;30import java.io.File;31import sun.awt.AWTAccessor;3233/**34* The <code>FileDialog</code> class displays a dialog window35* from which the user can select a file.36* <p>37* Since it is a modal dialog, when the application calls38* its <code>show</code> method to display the dialog,39* it blocks the rest of the application until the user has40* chosen a file.41*42* @see Window#show43*44* @author Sami Shaio45* @author Arthur van Hoff46* @since JDK1.047*/48public class FileDialog extends Dialog {4950/**51* This constant value indicates that the purpose of the file52* dialog window is to locate a file from which to read.53*/54public static final int LOAD = 0;5556/**57* This constant value indicates that the purpose of the file58* dialog window is to locate a file to which to write.59*/60public static final int SAVE = 1;6162/*63* There are two <code>FileDialog</code> modes: <code>LOAD</code> and64* <code>SAVE</code>.65* This integer will represent one or the other.66* If the mode is not specified it will default to <code>LOAD</code>.67*68* @serial69* @see getMode()70* @see setMode()71* @see java.awt.FileDialog#LOAD72* @see java.awt.FileDialog#SAVE73*/74int mode;7576/*77* The string specifying the directory to display78* in the file dialog. This variable may be <code>null</code>.79*80* @serial81* @see getDirectory()82* @see setDirectory()83*/84String dir;8586/*87* The string specifying the initial value of the88* filename text field in the file dialog.89* This variable may be <code>null</code>.90*91* @serial92* @see getFile()93* @see setFile()94*/95String file;9697/**98* Contains the File instances for all the files that the user selects.99*100* @serial101* @see #getFiles102* @since 1.7103*/104private File[] files;105106/**107* Represents whether the file dialog allows the multiple file selection.108*109* @serial110* @see #setMultipleMode111* @see #isMultipleMode112* @since 1.7113*/114private boolean multipleMode = false;115116/*117* The filter used as the file dialog's filename filter.118* The file dialog will only be displaying files whose119* names are accepted by this filter.120* This variable may be <code>null</code>.121*122* @serial123* @see #getFilenameFilter()124* @see #setFilenameFilter()125* @see FileNameFilter126*/127FilenameFilter filter;128129private static final String base = "filedlg";130private static int nameCounter = 0;131132/*133* JDK 1.1 serialVersionUID134*/135private static final long serialVersionUID = 5035145889651310422L;136137138static {139/* ensure that the necessary native libraries are loaded */140Toolkit.loadLibraries();141if (!GraphicsEnvironment.isHeadless()) {142initIDs();143}144}145146static {147AWTAccessor.setFileDialogAccessor(148new AWTAccessor.FileDialogAccessor() {149public void setFiles(FileDialog fileDialog, File files[]) {150fileDialog.setFiles(files);151}152public void setFile(FileDialog fileDialog, String file) {153fileDialog.file = ("".equals(file)) ? null : file;154}155public void setDirectory(FileDialog fileDialog, String directory) {156fileDialog.dir = ("".equals(directory)) ? null : directory;157}158public boolean isMultipleMode(FileDialog fileDialog) {159synchronized (fileDialog.getObjectLock()) {160return fileDialog.multipleMode;161}162}163});164}165166/**167* Initialize JNI field and method IDs for fields that may be168accessed from C.169*/170private static native void initIDs();171172/**173* Creates a file dialog for loading a file. The title of the174* file dialog is initially empty. This is a convenience method for175* <code>FileDialog(parent, "", LOAD)</code>.176*177* @param parent the owner of the dialog178* @since JDK1.1179*/180public FileDialog(Frame parent) {181this(parent, "", LOAD);182}183184/**185* Creates a file dialog window with the specified title for loading186* a file. The files shown are those in the current directory.187* This is a convenience method for188* <code>FileDialog(parent, title, LOAD)</code>.189*190* @param parent the owner of the dialog191* @param title the title of the dialog192*/193public FileDialog(Frame parent, String title) {194this(parent, title, LOAD);195}196197/**198* Creates a file dialog window with the specified title for loading199* or saving a file.200* <p>201* If the value of <code>mode</code> is <code>LOAD</code>, then the202* file dialog is finding a file to read, and the files shown are those203* in the current directory. If the value of204* <code>mode</code> is <code>SAVE</code>, the file dialog is finding205* a place to write a file.206*207* @param parent the owner of the dialog208* @param title the title of the dialog209* @param mode the mode of the dialog; either210* <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>211* @exception IllegalArgumentException if an illegal file212* dialog mode is supplied213* @see java.awt.FileDialog#LOAD214* @see java.awt.FileDialog#SAVE215*/216public FileDialog(Frame parent, String title, int mode) {217super(parent, title, true);218this.setMode(mode);219setLayout(null);220}221222/**223* Creates a file dialog for loading a file. The title of the224* file dialog is initially empty. This is a convenience method for225* <code>FileDialog(parent, "", LOAD)</code>.226*227* @param parent the owner of the dialog228* @exception java.lang.IllegalArgumentException if the <code>parent</code>'s229* <code>GraphicsConfiguration</code>230* is not from a screen device;231* @exception java.lang.IllegalArgumentException if <code>parent</code>232* is <code>null</code>; this exception is always thrown when233* <code>GraphicsEnvironment.isHeadless</code>234* returns <code>true</code>235* @see java.awt.GraphicsEnvironment#isHeadless236* @since 1.5237*/238public FileDialog(Dialog parent) {239this(parent, "", LOAD);240}241242/**243* Creates a file dialog window with the specified title for loading244* a file. The files shown are those in the current directory.245* This is a convenience method for246* <code>FileDialog(parent, title, LOAD)</code>.247*248* @param parent the owner of the dialog249* @param title the title of the dialog; a <code>null</code> value250* will be accepted without causing a251* <code>NullPointerException</code> to be thrown252* @exception java.lang.IllegalArgumentException if the <code>parent</code>'s253* <code>GraphicsConfiguration</code>254* is not from a screen device;255* @exception java.lang.IllegalArgumentException if <code>parent</code>256* is <code>null</code>; this exception is always thrown when257* <code>GraphicsEnvironment.isHeadless</code>258* returns <code>true</code>259* @see java.awt.GraphicsEnvironment#isHeadless260* @since 1.5261*/262public FileDialog(Dialog parent, String title) {263this(parent, title, LOAD);264}265266/**267* Creates a file dialog window with the specified title for loading268* or saving a file.269* <p>270* If the value of <code>mode</code> is <code>LOAD</code>, then the271* file dialog is finding a file to read, and the files shown are those272* in the current directory. If the value of273* <code>mode</code> is <code>SAVE</code>, the file dialog is finding274* a place to write a file.275*276* @param parent the owner of the dialog277* @param title the title of the dialog; a <code>null</code> value278* will be accepted without causing a279* <code>NullPointerException</code> to be thrown280* @param mode the mode of the dialog; either281* <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>282* @exception java.lang.IllegalArgumentException if an illegal283* file dialog mode is supplied;284* @exception java.lang.IllegalArgumentException if the <code>parent</code>'s285* <code>GraphicsConfiguration</code>286* is not from a screen device;287* @exception java.lang.IllegalArgumentException if <code>parent</code>288* is <code>null</code>; this exception is always thrown when289* <code>GraphicsEnvironment.isHeadless</code>290* returns <code>true</code>291* @see java.awt.GraphicsEnvironment#isHeadless292* @see java.awt.FileDialog#LOAD293* @see java.awt.FileDialog#SAVE294* @since 1.5295*/296public FileDialog(Dialog parent, String title, int mode) {297super(parent, title, true);298this.setMode(mode);299setLayout(null);300}301302/**303* Constructs a name for this component. Called by <code>getName()</code>304* when the name is <code>null</code>.305*/306String constructComponentName() {307synchronized (FileDialog.class) {308return base + nameCounter++;309}310}311312/**313* Creates the file dialog's peer. The peer allows us to change the look314* of the file dialog without changing its functionality.315*/316public void addNotify() {317synchronized(getTreeLock()) {318if (parent != null && parent.getPeer() == null) {319parent.addNotify();320}321if (peer == null)322peer = getToolkit().createFileDialog(this);323super.addNotify();324}325}326327/**328* Indicates whether this file dialog box is for loading from a file329* or for saving to a file.330*331* @return the mode of this file dialog window, either332* <code>FileDialog.LOAD</code> or333* <code>FileDialog.SAVE</code>334* @see java.awt.FileDialog#LOAD335* @see java.awt.FileDialog#SAVE336* @see java.awt.FileDialog#setMode337*/338public int getMode() {339return mode;340}341342/**343* Sets the mode of the file dialog. If <code>mode</code> is not344* a legal value, an exception will be thrown and <code>mode</code>345* will not be set.346*347* @param mode the mode for this file dialog, either348* <code>FileDialog.LOAD</code> or349* <code>FileDialog.SAVE</code>350* @see java.awt.FileDialog#LOAD351* @see java.awt.FileDialog#SAVE352* @see java.awt.FileDialog#getMode353* @exception IllegalArgumentException if an illegal file354* dialog mode is supplied355* @since JDK1.1356*/357public void setMode(int mode) {358switch (mode) {359case LOAD:360case SAVE:361this.mode = mode;362break;363default:364throw new IllegalArgumentException("illegal file dialog mode");365}366}367368/**369* Gets the directory of this file dialog.370*371* @return the (potentially <code>null</code> or invalid)372* directory of this <code>FileDialog</code>373* @see java.awt.FileDialog#setDirectory374*/375public String getDirectory() {376return dir;377}378379/**380* Sets the directory of this file dialog window to be the381* specified directory. Specifying a <code>null</code> or an382* invalid directory implies an implementation-defined default.383* This default will not be realized, however, until the user384* has selected a file. Until this point, <code>getDirectory()</code>385* will return the value passed into this method.386* <p>387* Specifying "" as the directory is exactly equivalent to388* specifying <code>null</code> as the directory.389*390* @param dir the specified directory391* @see java.awt.FileDialog#getDirectory392*/393public void setDirectory(String dir) {394this.dir = (dir != null && dir.equals("")) ? null : dir;395FileDialogPeer peer = (FileDialogPeer)this.peer;396if (peer != null) {397peer.setDirectory(this.dir);398}399}400401/**402* Gets the selected file of this file dialog. If the user403* selected <code>CANCEL</code>, the returned file is <code>null</code>.404*405* @return the currently selected file of this file dialog window,406* or <code>null</code> if none is selected407* @see java.awt.FileDialog#setFile408*/409public String getFile() {410return file;411}412413/**414* Returns files that the user selects.415* <p>416* If the user cancels the file dialog,417* then the method returns an empty array.418*419* @return files that the user selects or an empty array420* if the user cancels the file dialog.421* @see #setFile(String)422* @see #getFile423* @since 1.7424*/425public File[] getFiles() {426synchronized (getObjectLock()) {427if (files != null) {428return files.clone();429} else {430return new File[0];431}432}433}434435/**436* Stores the names of all the files that the user selects.437*438* Note that the method is private and it's intended to be used439* by the peers through the AWTAccessor API.440*441* @param files the array that contains the short names of442* all the files that the user selects.443*444* @see #getFiles445* @since 1.7446*/447private void setFiles(File files[]) {448synchronized (getObjectLock()) {449this.files = files;450}451}452453/**454* Sets the selected file for this file dialog window to be the455* specified file. This file becomes the default file if it is set456* before the file dialog window is first shown.457* <p>458* When the dialog is shown, the specified file is selected. The kind of459* selection depends on the file existence, the dialog type, and the native460* platform. E.g., the file could be highlighted in the file list, or a461* file name editbox could be populated with the file name.462* <p>463* This method accepts either a full file path, or a file name with an464* extension if used together with the {@code setDirectory} method.465* <p>466* Specifying "" as the file is exactly equivalent to specifying467* {@code null} as the file.468*469* @param file the file being set470* @see #getFile471* @see #getFiles472*/473public void setFile(String file) {474this.file = (file != null && file.equals("")) ? null : file;475FileDialogPeer peer = (FileDialogPeer)this.peer;476if (peer != null) {477peer.setFile(this.file);478}479}480481/**482* Enables or disables multiple file selection for the file dialog.483*484* @param enable if {@code true}, multiple file selection is enabled;485* {@code false} - disabled.486* @see #isMultipleMode487* @since 1.7488*/489public void setMultipleMode(boolean enable) {490synchronized (getObjectLock()) {491this.multipleMode = enable;492}493}494495/**496* Returns whether the file dialog allows the multiple file selection.497*498* @return {@code true} if the file dialog allows the multiple499* file selection; {@code false} otherwise.500* @see #setMultipleMode501* @since 1.7502*/503public boolean isMultipleMode() {504synchronized (getObjectLock()) {505return multipleMode;506}507}508509/**510* Determines this file dialog's filename filter. A filename filter511* allows the user to specify which files appear in the file dialog512* window. Filename filters do not function in Sun's reference513* implementation for Microsoft Windows.514*515* @return this file dialog's filename filter516* @see java.io.FilenameFilter517* @see java.awt.FileDialog#setFilenameFilter518*/519public FilenameFilter getFilenameFilter() {520return filter;521}522523/**524* Sets the filename filter for this file dialog window to the525* specified filter.526* Filename filters do not function in Sun's reference527* implementation for Microsoft Windows.528*529* @param filter the specified filter530* @see java.io.FilenameFilter531* @see java.awt.FileDialog#getFilenameFilter532*/533public synchronized void setFilenameFilter(FilenameFilter filter) {534this.filter = filter;535FileDialogPeer peer = (FileDialogPeer)this.peer;536if (peer != null) {537peer.setFilenameFilter(filter);538}539}540541/**542* Reads the <code>ObjectInputStream</code> and performs543* a backwards compatibility check by converting544* either a <code>dir</code> or a <code>file</code>545* equal to an empty string to <code>null</code>.546*547* @param s the <code>ObjectInputStream</code> to read548*/549private void readObject(ObjectInputStream s)550throws ClassNotFoundException, IOException551{552s.defaultReadObject();553554// 1.1 Compatibility: "" is not converted to null in 1.1555if (dir != null && dir.equals("")) {556dir = null;557}558if (file != null && file.equals("")) {559file = null;560}561}562563/**564* Returns a string representing the state of this <code>FileDialog</code>565* window. This method is intended to be used only for debugging purposes,566* and the content and format of the returned string may vary between567* implementations. The returned string may be empty but may not be568* <code>null</code>.569*570* @return the parameter string of this file dialog window571*/572protected String paramString() {573String str = super.paramString();574str += ",dir= " + dir;575str += ",file= " + file;576return str + ((mode == LOAD) ? ",load" : ",save");577}578579boolean postsOldMouseEvents() {580return false;581}582}583584585