1. /*
  2. * @(#)FileDialog.java 1.50 03/01/23
  3. *
  4. * Copyright 2003 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.50, 01/23/03
  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 two <code>FileDialog</code> 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 <code>LOAD</code>.
  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. This variable may be <code>null</code>.
  55. *
  56. * @serial
  57. * @see getDirectory()
  58. * @see setDirectory()
  59. */
  60. String dir;
  61. /*
  62. * The string specifying the initial value of the
  63. * filename text field in the file dialog.
  64. * This variable may be <code>null</code>.
  65. *
  66. * @serial
  67. * @see getFile()
  68. * @see setFile()
  69. */
  70. String file;
  71. /*
  72. * The filter used as the file dialog's filename filter.
  73. * The file dialog will only be displaying files whose
  74. * names are accepted by this filter.
  75. * This variable may be <code>null</code>.
  76. *
  77. * @serial
  78. * @see getFilenameFIlter()
  79. * @see setFilenameFilter()
  80. * @see FileNameFilter
  81. */
  82. FilenameFilter filter;
  83. private static final String base = "filedlg";
  84. private static int nameCounter = 0;
  85. /*
  86. * JDK 1.1 serialVersionUID
  87. */
  88. private static final long serialVersionUID = 5035145889651310422L;
  89. static {
  90. /* ensure that the necessary native libraries are loaded */
  91. Toolkit.loadLibraries();
  92. if (!GraphicsEnvironment.isHeadless()) {
  93. initIDs();
  94. }
  95. }
  96. /**
  97. * Initialize JNI field and method IDs for fields that may be
  98. accessed from C.
  99. */
  100. private static native void initIDs();
  101. /**
  102. * Creates a file dialog for loading a file. The title of the
  103. * file dialog is initially empty. This is a convenience method for
  104. * <code>FileDialog(parent, "", LOAD)</code>.
  105. *
  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. * This is a convenience method for
  116. * <code>FileDialog(parent, title, LOAD)</code>.
  117. *
  118. * @param parent the owner of the dialog
  119. * @param title the title of the dialog
  120. */
  121. public FileDialog(Frame parent, String title) {
  122. this(parent, title, LOAD);
  123. }
  124. /**
  125. * Creates a file dialog window with the specified title for loading
  126. * or saving a file.
  127. * <p>
  128. * If the value of <code>mode</code> is <code>LOAD</code>, then the
  129. * file dialog is finding a file to read, and the files shown are those
  130. * in the current directory. If the value of
  131. * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
  132. * a place to write a file.
  133. *
  134. * @param parent the owner of the dialog
  135. * @param title the title of the dialog
  136. * @param mode the mode of the dialog; either
  137. * <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
  138. * @exception IllegalArgumentException if an illegal file
  139. * dialog mode is supplied
  140. * @see java.awt.FileDialog#LOAD
  141. * @see java.awt.FileDialog#SAVE
  142. */
  143. public FileDialog(Frame parent, String title, int mode) {
  144. super(parent, title, true);
  145. this.setMode(mode);
  146. setLayout(null);
  147. }
  148. /**
  149. * Constructs a name for this component. Called by <code>getName()</code>
  150. * when the name is <code>null</code>.
  151. */
  152. String constructComponentName() {
  153. synchronized (getClass()) {
  154. return base + nameCounter++;
  155. }
  156. }
  157. /**
  158. * Creates the file dialog's peer. The peer allows us to change the look
  159. * of the file dialog without changing its functionality.
  160. */
  161. public void addNotify() {
  162. synchronized(getTreeLock()) {
  163. if (parent != null && parent.getPeer() == null) {
  164. parent.addNotify();
  165. }
  166. if (peer == null)
  167. peer = getToolkit().createFileDialog(this);
  168. super.addNotify();
  169. }
  170. }
  171. /**
  172. * Indicates whether this file dialog box is for loading from a file
  173. * or for saving to a file.
  174. *
  175. * @return the mode of this file dialog window, 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#setMode
  181. */
  182. public int getMode() {
  183. return mode;
  184. }
  185. /**
  186. * Sets the mode of the file dialog. If <code>mode</code> is not
  187. * a legal value, an exception will be thrown and <code>mode</code>
  188. * will not be set.
  189. *
  190. * @param mode the mode for this file dialog, either
  191. * <code>FileDialog.LOAD</code> or
  192. * <code>FileDialog.SAVE</code>
  193. * @see java.awt.FileDialog#LOAD
  194. * @see java.awt.FileDialog#SAVE
  195. * @see java.awt.FileDialog#getMode
  196. * @exception IllegalArgumentException if an illegal file
  197. * dialog mode is supplied
  198. * @since JDK1.1
  199. */
  200. public void setMode(int mode) {
  201. switch (mode) {
  202. case LOAD:
  203. case SAVE:
  204. this.mode = mode;
  205. break;
  206. default:
  207. throw new IllegalArgumentException("illegal file dialog mode");
  208. }
  209. }
  210. /**
  211. * Gets the directory of this file dialog.
  212. *
  213. * @return the (potentially <code>null</code> or invalid)
  214. * directory of this <code>FileDialog</code>
  215. * @see java.awt.FileDialog#setDirectory
  216. */
  217. public String getDirectory() {
  218. return dir;
  219. }
  220. /**
  221. * Sets the directory of this file dialog window to be the
  222. * specified directory. Specifying a <code>null</code> or an
  223. * invalid directory implies an implementation-defined default.
  224. * This default will not be realized, however, until the user
  225. * has selected a file. Until this point, <code>getDirectory()</code>
  226. * will return the value passed into this method.
  227. * <p>
  228. * Specifying "" as the directory is exactly equivalent to
  229. * specifying <code>null</code> as the directory.
  230. *
  231. * @param dir the specified directory
  232. * @see java.awt.FileDialog#getDirectory
  233. */
  234. public void setDirectory(String dir) {
  235. this.dir = (dir != null && dir.equals("")) ? null : dir;
  236. FileDialogPeer peer = (FileDialogPeer)this.peer;
  237. if (peer != null) {
  238. peer.setDirectory(this.dir);
  239. }
  240. }
  241. /**
  242. * Gets the selected file of this file dialog. If the user
  243. * selected <code>CANCEL</code>, the returned file is <code>null</code>.
  244. *
  245. * @return the currently selected file of this file dialog window,
  246. * or <code>null</code> if none is selected
  247. * @see java.awt.FileDialog#setFile
  248. */
  249. public String getFile() {
  250. return file;
  251. }
  252. /**
  253. * Sets the selected file for this file dialog window to be the
  254. * specified file. This file becomes the default file if it is set
  255. * before the file dialog window is first shown.
  256. * <p>
  257. * Specifying "" as the file is exactly equivalent to specifying
  258. * <code>null</code>
  259. * as the file.
  260. *
  261. * @param file the file being set
  262. * @see java.awt.FileDialog#getFile
  263. */
  264. public void setFile(String file) {
  265. this.file = (file != null && file.equals("")) ? null : file;
  266. FileDialogPeer peer = (FileDialogPeer)this.peer;
  267. if (peer != null) {
  268. peer.setFile(this.file);
  269. }
  270. }
  271. /**
  272. * Determines this file dialog's filename filter. A filename filter
  273. * allows the user to specify which files appear in the file dialog
  274. * window. Filename filters do not function in Sun's reference
  275. * implementation for Windows 95, 98, or NT 4.0.
  276. *
  277. * @return this file dialog's filename filter
  278. * @see java.io.FilenameFilter
  279. * @see java.awt.FileDialog#setFilenameFilter
  280. */
  281. public FilenameFilter getFilenameFilter() {
  282. return filter;
  283. }
  284. /**
  285. * Sets the filename filter for this file dialog window to the
  286. * specified filter.
  287. * Filename filters do not function in Sun's reference
  288. * implementation for Windows 95, 98, or NT 4.0.
  289. *
  290. * @param filter the specified filter
  291. * @see java.io.FilenameFilter
  292. * @see java.awt.FileDialog#getFilenameFilter
  293. */
  294. public synchronized void setFilenameFilter(FilenameFilter filter) {
  295. this.filter = filter;
  296. FileDialogPeer peer = (FileDialogPeer)this.peer;
  297. if (peer != null) {
  298. peer.setFilenameFilter(filter);
  299. }
  300. }
  301. /**
  302. * Reads the <code>ObjectInputStream</code> and performs
  303. * a backwards compatibility check by converting
  304. * either a <code>dir</code> or a <code>file</code>
  305. * equal to an empty string to <code>null</code>.
  306. *
  307. * @param s the <code>ObjectInputStream</code> to read
  308. */
  309. private void readObject(ObjectInputStream s)
  310. throws ClassNotFoundException, IOException
  311. {
  312. s.defaultReadObject();
  313. // 1.1 Compatibility: "" is not converted to null in 1.1
  314. if (dir != null && dir.equals("")) {
  315. dir = null;
  316. }
  317. if (file != null && file.equals("")) {
  318. file = null;
  319. }
  320. }
  321. /**
  322. * Returns a string representing the state of this <code>FileDialog</code>
  323. * window. This method is intended to be used only for debugging purposes,
  324. * and the content and format of the returned string may vary between
  325. * implementations. The returned string may be empty but may not be
  326. * <code>null</code>.
  327. *
  328. * @return the parameter string of this file dialog window
  329. */
  330. protected String paramString() {
  331. String str = super.paramString();
  332. str += ",dir= " + dir;
  333. str += ",file= " + file;
  334. return str + ((mode == LOAD) ? ",load" : ",save");
  335. }
  336. boolean postsOldMouseEvents() {
  337. return false;
  338. }
  339. }