001: /*
002: * @(#)FileDialog.java 1.21 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package java.awt;
028:
029: import sun.awt.peer.FileDialogPeer;
030: import sun.awt.PeerBasedToolkit;
031: import java.io.FilenameFilter;
032: import java.io.IOException;
033: import java.io.ObjectInputStream;
034:
035: /**
036: * The <code>FileDialog</code> class displays a dialog window
037: * from which the user can select a file.
038: * <p>
039: * Since it is a modal dialog, when the application calls
040: * its <code>show</code> method to display the dialog,
041: * it blocks the rest of the application until the user has
042: * chosen a file.
043: *
044: * @see Window#show
045: *
046: * @version 1.16, 08/19/02
047: * @author Sami Shaio
048: * @author Arthur van Hoff
049: * @since JDK1.0
050: */
051: public class FileDialog extends Dialog {
052: /**
053: * This constant value indicates that the purpose of the file
054: * dialog window is to locate a file from which to read.
055: */
056: public static final int LOAD = 0;
057: /**
058: * This constant value indicates that the purpose of the file
059: * dialog window is to locate a file to which to write.
060: */
061: public static final int SAVE = 1;
062: /**
063: * There are 2 FileDialog Modes : <code>LOAD<code> and
064: * <code>SAVE<code>.
065: * This integer will represent one or the other.
066: * If the mode is not specified it will default to LOAD.
067: *
068: * @serial
069: * @see getMode()
070: * @see setMode()
071: * @see java.awt.FileDialog#LOAD
072: * @see java.awt.FileDialog#SAVE
073: */
074: int mode;
075: /**
076: * The string specifying the directory to display
077: * in the file dialog.
078: * This variable may be null.
079: *
080: * @serial
081: * @see getDirectory()
082: * @see setDirectory()
083: */
084: String dir;
085: /**
086: * The string specifying the initial value of the
087: * filename text field in the file dialog.
088: * This variable may be null.
089: *
090: * @serial
091: * @see getFile()
092: * @see setFile()
093: */
094: String file;
095: /**
096: * The filter used as the file dialog's filename filter.
097: * The file dialog will only be displaying files whose
098: * names are accepted by this filter.
099: * This variable may be null.
100: *
101: * @serial
102: * @see getFilenameFIlter()
103: * @see setFilenameFilter()
104: * @see FileNameFilter
105: */
106: FilenameFilter filter;
107: private static final String base = "filedlg";
108: private static int nameCounter = 0;
109: /*
110: * JDK 1.1 serialVersionUID
111: */
112: private static final long serialVersionUID = 5035145889651310422L;
113:
114: /**
115: * Creates a file dialog for loading a file. The title of the
116: * file dialog is initially empty.
117: * @param parent the owner of the dialog
118: * @since JDK1.1
119: */
120: public FileDialog(Frame parent) {
121: this (parent, "", LOAD);
122: }
123:
124: /**
125: * Creates a file dialog window with the specified title for loading
126: * a file. The files shown are those in the current directory.
127: * @param parent the owner of the dialog.
128: * @param title the title of the dialog.
129: */
130: public FileDialog(Frame parent, String title) {
131: this (parent, title, LOAD);
132: }
133:
134: /**
135: * Creates a file dialog window with the specified title for loading
136: * or saving a file.
137: * <p>
138: * If the value of <code>mode</code> is <code>LOAD</code>, then the
139: * file dialog is finding a file to read. If the value of
140: * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
141: * a place to write a file.
142: * @param parent the owner of the dialog.
143: * @param title the title of the dialog.
144: * @param mode the mode of the dialog.
145: * @see java.awt.FileDialog#LOAD
146: * @see java.awt.FileDialog#SAVE
147: */
148: public FileDialog(Frame parent, String title, int mode) {
149: super (parent, title, true);
150: this .setMode(mode);
151: setLayout(null);
152: }
153:
154: /**
155: * Construct a name for this component. Called by getName() when the
156: * name is null.
157: */
158: String constructComponentName() {
159: synchronized (getClass()) {
160: return base + nameCounter++;
161: }
162: }
163:
164: /**
165: * Creates the file dialog's peer. The peer allows us to change the look
166: * of the file dialog without changing its functionality.
167: */
168: public void addNotify() {
169: synchronized (getTreeLock()) {
170: if (parent != null && parent.peer == null) {
171: parent.addNotify();
172: }
173: if (peer == null)
174: peer = ((PeerBasedToolkit) getToolkit())
175: .createFileDialog(this );
176: super .addNotify();
177: }
178: }
179:
180: /**
181: * Indicates whether this file dialog box is for loading from a file
182: * or for saving to a file.
183: * @return the mode of this file dialog window, either
184: * <code>FileDialog.LOAD</code> or
185: * <code>FileDialog.SAVE</code>.
186: * @see java.awt.FileDialog#LOAD
187: * @see java.awt.FileDialog#SAVE
188: * @see java.awt.FileDialog#setMode
189: */
190: public int getMode() {
191: return mode;
192: }
193:
194: /**
195: * Sets the mode of the file dialog.
196: * @param mode the mode for this file dialog, either
197: * <code>FileDialog.LOAD</code> or
198: * <code>FileDialog.SAVE</code>.
199: * @see java.awt.FileDialog#LOAD
200: * @see java.awt.FileDialog#SAVE
201: * @see java.awt.FileDialog#getMode
202: * @exception IllegalArgumentException if an illegal file
203: * dialog mode is used.
204: * @since JDK1.1
205: */
206: public void setMode(int mode) {
207: switch (mode) {
208: case LOAD:
209: case SAVE:
210: this .mode = mode;
211: break;
212:
213: default:
214: throw new IllegalArgumentException(
215: "illegal file dialog mode");
216: }
217: }
218:
219: /**
220: * Gets the directory of this file dialog.
221: * @return the (potentially null or invalid) directory of this
222: * FileDialog.
223: * @see java.awt.FileDialog#setDirectory
224: */
225: public String getDirectory() {
226: return dir;
227: }
228:
229: /**
230: * Sets the directory of this file dialog window to be the
231: * specified directory. Specifying a <code>null</code> or an
232: * invalid directory implies an implementation-defined default.
233: * This default will not be realized, however, until the user
234: * has selected a file. Until this point, <code>getDirectory()</code>
235: * will return the value passed into this method.<p>
236: * Specifying "" as the directory is exactly equivalent to
237: * specifying <code>null</code> as the directory.
238: * @param dir the specific directory.
239: * @see java.awt.FileDialog#getDirectory
240: */
241: public void setDirectory(String dir) {
242: this .dir = (dir != null && dir.equals("")) ? null : dir;
243: FileDialogPeer peer = (FileDialogPeer) this .peer;
244: if (peer != null) {
245: peer.setDirectory(this .dir);
246: }
247: }
248:
249: /**
250: * Gets the selected file of this file dialog.
251: * @return the currently selected file of this file dialog window,
252: * or <code>null</code> if none is selected.
253: * @see java.awt.FileDialog#setFile
254: */
255: public String getFile() {
256: return file;
257: }
258:
259: /**
260: * Sets the selected file for this file dialog window to be the
261: * specified file. This file becomes the default file if it is set
262: * before the file dialog window is first shown.<p> Specifying "" as
263: * the file is exactly equivalent to specifying <code>null</code>
264: * as the file.
265: * @param file the file being set.
266: * @see java.awt.FileDialog#getFile
267: */
268: public void setFile(String file) {
269: this .file = (file != null && file.equals("")) ? null : file;
270: FileDialogPeer peer = (FileDialogPeer) this .peer;
271: if (peer != null) {
272: peer.setFile(this .file);
273: }
274: }
275:
276: /**
277: * Determines this file dialog's filename filter. A filename filter
278: * allows the user to specify which files appear in the file dialog
279: * window. Filename filters do not function in Sun's reference
280: * implementation for Windows 95, 98, or NT 4.0.
281: * @return this file dialog's filename filter.
282: * @see java.io.FilenameFilter
283: * @see java.awt.FileDialog#setFilenameFilter
284: */
285: public FilenameFilter getFilenameFilter() {
286: return filter;
287: }
288:
289: /**
290: * Sets the filename filter for this file dialog window to the
291: * specified filter.
292: * Filename filters do not function in Sun's reference
293: * implementation for Windows 95, 98, or NT 4.0.
294: * @param filter the specified filter.
295: * @see java.io.FilenameFilter
296: * @see java.awt.FileDialog#getFilenameFilter
297: */
298: public synchronized void setFilenameFilter(FilenameFilter filter) {
299: this .filter = filter;
300: FileDialogPeer peer = (FileDialogPeer) this .peer;
301: if (peer != null) {
302: peer.setFilenameFilter(filter);
303: }
304: }
305:
306: private void readObject(ObjectInputStream s)
307: throws ClassNotFoundException, IOException {
308: s.defaultReadObject();
309: // 1.1 Compatibility: "" is not converted to null in 1.1
310: if (dir != null && dir.equals("")) {
311: dir = null;
312: }
313: if (file != null && file.equals("")) {
314: file = null;
315: }
316: }
317:
318: /**
319: * Returns the parameter string representing the state of this file
320: * dialog window. This string is useful for debugging.
321: * @return the parameter string of this file dialog window.
322: */
323: protected String paramString() {
324: String str = super .paramString();
325: str += ",dir= " + dir;
326: str += ",file= " + file;
327: return str + ((mode == LOAD) ? ",load" : ",save");
328: }
329:
330: boolean postsOldMouseEvents() {
331: return false;
332: }
333: }
|