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