1. /*
  2. * @(#)FileDialog.java 1.43 00/02/02
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. import java.awt.peer.FileDialogPeer;
  12. import java.io.FilenameFilter;
  13. import java.io.IOException;
  14. import java.io.ObjectInputStream;
  15. /**
  16. * The <code>FileDialog</code> class displays a dialog window
  17. * from which the user can select a file.
  18. * <p>
  19. * Since it is a modal dialog, when the application calls
  20. * its <code>show</code> method to display the dialog,
  21. * it blocks the rest of the application until the user has
  22. * chosen a file.
  23. *
  24. * @see Window#show
  25. *
  26. * @version 1.43, 02/02/00
  27. * @author Sami Shaio
  28. * @author Arthur van Hoff
  29. * @since JDK1.0
  30. */
  31. public class FileDialog extends Dialog {
  32. /**
  33. * This constant value indicates that the purpose of the file
  34. * dialog window is to locate a file from which to read.
  35. */
  36. public static final int LOAD = 0;
  37. /**
  38. * This constant value indicates that the purpose of the file
  39. * dialog window is to locate a file to which to write.
  40. */
  41. public static final int SAVE = 1;
  42. /*
  43. * There are 2 FileDialog Modes : <code>LOAD<code> and
  44. * <code>SAVE<code>.
  45. * This integer will represent one or the other.
  46. * If the mode is not specified it will default to LOAD.
  47. *
  48. * @serial
  49. * @see getMode()
  50. * @see setMode()
  51. * @see java.awt.FileDialog#LOAD
  52. * @see java.awt.FileDialog#SAVE
  53. */
  54. int mode;
  55. /*
  56. * The string specifying the directory to display
  57. * in the file dialog.
  58. * This variable may be null.
  59. *
  60. * @serial
  61. * @see getDirectory()
  62. * @see setDirectory()
  63. */
  64. String dir;
  65. /*
  66. * The string specifying the initial value of the
  67. * filename text field in the file dialog.
  68. * This variable may be null.
  69. *
  70. * @serial
  71. * @see getFile()
  72. * @see setFile()
  73. */
  74. String file;
  75. /*
  76. * The filter used as the file dialog's filename filter.
  77. * The file dialog will only be displaying files whose
  78. * names are accepted by this filter.
  79. * This variable may be null.
  80. *
  81. * @serial
  82. * @see getFilenameFIlter()
  83. * @see setFilenameFilter()
  84. * @see FileNameFilter
  85. */
  86. FilenameFilter filter;
  87. private static final String base = "filedlg";
  88. private static int nameCounter = 0;
  89. /*
  90. * JDK 1.1 serialVersionUID
  91. */
  92. private static final long serialVersionUID = 5035145889651310422L;
  93. static {
  94. /* ensure that the necessary native libraries are loaded */
  95. Toolkit.loadLibraries();
  96. initIDs();
  97. }
  98. /**
  99. * Initialize JNI field and method IDs for fields that may be
  100. accessed from C.
  101. */
  102. private static native void initIDs();
  103. /**
  104. * Creates a file dialog for loading a file. The title of the
  105. * file dialog is initially empty.
  106. * @param parent the owner of the dialog
  107. * @since JDK1.1
  108. */
  109. public FileDialog(Frame parent) {
  110. this(parent, "", LOAD);
  111. }
  112. /**
  113. * Creates a file dialog window with the specified title for loading
  114. * a file. The files shown are those in the current directory.
  115. * @param parent the owner of the dialog.
  116. * @param title the title of the dialog.
  117. */
  118. public FileDialog(Frame parent, String title) {
  119. this(parent, title, LOAD);
  120. }
  121. /**
  122. * Creates a file dialog window with the specified title for loading
  123. * or saving a file.
  124. * <p>
  125. * If the value of <code>mode</code> is <code>LOAD</code>, then the
  126. * file dialog is finding a file to read. If the value of
  127. * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
  128. * a place to write a file.
  129. * @param parent the owner of the dialog.
  130. * @param title the title of the dialog.
  131. * @param mode the mode of the dialog.
  132. * @see java.awt.FileDialog#LOAD
  133. * @see java.awt.FileDialog#SAVE
  134. */
  135. public FileDialog(Frame parent, String title, int mode) {
  136. super(parent, title, true);
  137. this.mode = mode;
  138. setLayout(null);
  139. }
  140. /**
  141. * Construct a name for this component. Called by getName() when the
  142. * name is null.
  143. */
  144. String constructComponentName() {
  145. synchronized (getClass()) {
  146. return base + nameCounter++;
  147. }
  148. }
  149. /**
  150. * Creates the file dialog's peer. The peer allows us to change the look
  151. * of the file dialog without changing its functionality.
  152. */
  153. public void addNotify() {
  154. synchronized(getTreeLock()) {
  155. if (parent != null && parent.getPeer() == null) {
  156. parent.addNotify();
  157. }
  158. if (peer == null)
  159. peer = getToolkit().createFileDialog(this);
  160. super.addNotify();
  161. }
  162. }
  163. /**
  164. * Indicates whether this file dialog box is for loading from a file
  165. * or for saving to a file.
  166. * @return the mode of this file dialog window, either
  167. * <code>FileDialog.LOAD</code> or
  168. * <code>FileDialog.SAVE</code>.
  169. * @see java.awt.FileDialog#LOAD
  170. * @see java.awt.FileDialog#SAVE
  171. * @see java.awt.FileDialog#setMode
  172. */
  173. public int getMode() {
  174. return mode;
  175. }
  176. /**
  177. * Sets the mode of the file dialog.
  178. * @param mode the mode for this file dialog, either
  179. * <code>FileDialog.LOAD</code> or
  180. * <code>FileDialog.SAVE</code>.
  181. * @see java.awt.FileDialog#LOAD
  182. * @see java.awt.FileDialog#SAVE
  183. * @see java.awt.FileDialog#getMode
  184. * @exception IllegalArgumentException if an illegal file
  185. * dialog mode is used.
  186. * @since JDK1.1
  187. */
  188. public void setMode(int mode) {
  189. switch (mode) {
  190. case LOAD:
  191. case SAVE:
  192. this.mode = mode;
  193. break;
  194. default:
  195. throw new IllegalArgumentException("illegal file dialog mode");
  196. }
  197. }
  198. /**
  199. * Gets the directory of this file dialog.
  200. * @return the (potentially null or invalid) directory of this
  201. * FileDialog.
  202. * @see java.awt.FileDialog#setDirectory
  203. */
  204. public String getDirectory() {
  205. return dir;
  206. }
  207. /**
  208. * Sets the directory of this file dialog window to be the
  209. * specified directory. Specifying a <code>null</code> or an
  210. * invalid directory implies an implementation-defined default.
  211. * This default will not be realized, however, until the user
  212. * has selected a file. Until this point, <code>getDirectory()</code>
  213. * will return the value passed into this method.<p>
  214. * Specifying "" as the directory is exactly equivalent to
  215. * specifying <code>null</code> as the directory.
  216. * @param dir the specific directory.
  217. * @see java.awt.FileDialog#getDirectory
  218. */
  219. public void setDirectory(String dir) {
  220. this.dir = (dir != null && dir.equals("")) ? null : dir;
  221. FileDialogPeer peer = (FileDialogPeer)this.peer;
  222. if (peer != null) {
  223. peer.setDirectory(this.dir);
  224. }
  225. }
  226. /**
  227. * Gets the selected file of this file dialog.
  228. * @return the currently selected file of this file dialog window,
  229. * or <code>null</code> if none is selected.
  230. * @see java.awt.FileDialog#setFile
  231. */
  232. public String getFile() {
  233. return file;
  234. }
  235. /**
  236. * Sets the selected file for this file dialog window to be the
  237. * specified file. This file becomes the default file if it is set
  238. * before the file dialog window is first shown.<p> Specifying "" as
  239. * the file is exactly equivalent to specifying <code>null</code>
  240. * as the file.
  241. * @param file the file being set.
  242. * @see java.awt.FileDialog#getFile
  243. */
  244. public void setFile(String file) {
  245. this.file = (file != null && file.equals("")) ? null : file;
  246. FileDialogPeer peer = (FileDialogPeer)this.peer;
  247. if (peer != null) {
  248. peer.setFile(this.file);
  249. }
  250. }
  251. /**
  252. * Determines this file dialog's filename filter. A filename filter
  253. * allows the user to specify which files appear in the file dialog
  254. * window. Filename filters do not function in Sun's reference
  255. * implementation for Windows 95, 98, or NT 4.0.
  256. * @return this file dialog's filename filter.
  257. * @see java.io.FilenameFilter
  258. * @see java.awt.FileDialog#setFilenameFilter
  259. */
  260. public FilenameFilter getFilenameFilter() {
  261. return filter;
  262. }
  263. /**
  264. * Sets the filename filter for this file dialog window to the
  265. * specified filter.
  266. * Filename filters do not function in Sun's reference
  267. * implementation for Windows 95, 98, or NT 4.0.
  268. * @param filter the specified filter.
  269. * @see java.io.FilenameFilter
  270. * @see java.awt.FileDialog#getFilenameFilter
  271. */
  272. public synchronized void setFilenameFilter(FilenameFilter filter) {
  273. this.filter = filter;
  274. FileDialogPeer peer = (FileDialogPeer)this.peer;
  275. if (peer != null) {
  276. peer.setFilenameFilter(filter);
  277. }
  278. }
  279. private void readObject(ObjectInputStream s)
  280. throws ClassNotFoundException, IOException
  281. {
  282. s.defaultReadObject();
  283. // 1.1 Compatibility: "" is not converted to null in 1.1
  284. if (dir != null && dir.equals("")) {
  285. dir = null;
  286. }
  287. if (file != null && file.equals("")) {
  288. file = null;
  289. }
  290. }
  291. /**
  292. * Returns the parameter string representing the state of this file
  293. * dialog window. This string is useful for debugging.
  294. * @return the parameter string of this file dialog window.
  295. */
  296. protected String paramString() {
  297. String str = super.paramString();
  298. str += ",dir= " + dir;
  299. str += ",file= " + file;
  300. return str + ((mode == LOAD) ? ",load" : ",save");
  301. }
  302. boolean postsOldMouseEvents() {
  303. return false;
  304. }
  305. }