1. /*
  2. * @(#)FileDialog.java 1.53 03/12/19
  3. *
  4. * Copyright 2004 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.53, 12/19/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. * Creates a file dialog for loading a file. The title of the
  150. * file dialog is initially empty. This is a convenience method for
  151. * <code>FileDialog(parent, "", LOAD)</code>.
  152. *
  153. * @param parent the owner of the dialog
  154. * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
  155. * <code>GraphicsConfiguration</code>
  156. * is not from a screen device;
  157. * @exception java.lang.IllegalArgumentException if <code>parent</code>
  158. * is <code>null</code> this exception is always thrown when
  159. * <code>GraphicsEnvironment.isHeadless</code>
  160. * returns <code>true</code>
  161. * @see java.awt.GraphicsEnvironment#isHeadless
  162. * @since 1.5
  163. */
  164. public FileDialog(Dialog parent) {
  165. this(parent, "", LOAD);
  166. }
  167. /**
  168. * Creates a file dialog window with the specified title for loading
  169. * a file. The files shown are those in the current directory.
  170. * This is a convenience method for
  171. * <code>FileDialog(parent, title, LOAD)</code>.
  172. *
  173. * @param parent the owner of the dialog
  174. * @param title the title of the dialog; a <code>null</code> value
  175. * will be accepted without causing a
  176. * <code>NullPointerException</code> to be thrown
  177. * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
  178. * <code>GraphicsConfiguration</code>
  179. * is not from a screen device;
  180. * @exception java.lang.IllegalArgumentException if <code>parent</code>
  181. * is <code>null</code> this exception is always thrown when
  182. * <code>GraphicsEnvironment.isHeadless</code>
  183. * returns <code>true</code>
  184. * @see java.awt.GraphicsEnvironment#isHeadless
  185. * @since 1.5
  186. */
  187. public FileDialog(Dialog parent, String title) {
  188. this(parent, title, LOAD);
  189. }
  190. /**
  191. * Creates a file dialog window with the specified title for loading
  192. * or saving a file.
  193. * <p>
  194. * If the value of <code>mode</code> is <code>LOAD</code>, then the
  195. * file dialog is finding a file to read, and the files shown are those
  196. * in the current directory. If the value of
  197. * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
  198. * a place to write a file.
  199. *
  200. * @param parent the owner of the dialog
  201. * @param title the title of the dialog; a <code>null</code> value
  202. * will be accepted without causing a
  203. * <code>NullPointerException</code> to be thrown
  204. * @param mode the mode of the dialog; either
  205. * <code>FileDialog.LOAD</code> or <code>FileDialog.SAVE</code>
  206. * @exception java.lang.IllegalArgumentException if an illegal
  207. * file dialog mode is supplied;
  208. * @exception java.lang.IllegalArgumentException if the <code>parent</code>'s
  209. * <code>GraphicsConfiguration</code>
  210. * is not from a screen device;
  211. * @exception java.lang.IllegalArgumentException if <code>parent</code>
  212. * is <code>null</code> this exception is always thrown when
  213. * <code>GraphicsEnvironment.isHeadless</code>
  214. * returns <code>true</code>
  215. * @see java.awt.GraphicsEnvironment#isHeadless
  216. * @see java.awt.FileDialog#LOAD
  217. * @see java.awt.FileDialog#SAVE
  218. * @since 1.5
  219. */
  220. public FileDialog(Dialog parent, String title, int mode) {
  221. super(parent, title, true);
  222. this.setMode(mode);
  223. setLayout(null);
  224. }
  225. /**
  226. * Constructs a name for this component. Called by <code>getName()</code>
  227. * when the name is <code>null</code>.
  228. */
  229. String constructComponentName() {
  230. synchronized (getClass()) {
  231. return base + nameCounter++;
  232. }
  233. }
  234. /**
  235. * Creates the file dialog's peer. The peer allows us to change the look
  236. * of the file dialog without changing its functionality.
  237. */
  238. public void addNotify() {
  239. synchronized(getTreeLock()) {
  240. if (parent != null && parent.getPeer() == null) {
  241. parent.addNotify();
  242. }
  243. if (peer == null)
  244. peer = getToolkit().createFileDialog(this);
  245. super.addNotify();
  246. }
  247. }
  248. /**
  249. * Indicates whether this file dialog box is for loading from a file
  250. * or for saving to a file.
  251. *
  252. * @return the mode of this file dialog window, either
  253. * <code>FileDialog.LOAD</code> or
  254. * <code>FileDialog.SAVE</code>
  255. * @see java.awt.FileDialog#LOAD
  256. * @see java.awt.FileDialog#SAVE
  257. * @see java.awt.FileDialog#setMode
  258. */
  259. public int getMode() {
  260. return mode;
  261. }
  262. /**
  263. * Sets the mode of the file dialog. If <code>mode</code> is not
  264. * a legal value, an exception will be thrown and <code>mode</code>
  265. * will not be set.
  266. *
  267. * @param mode the mode for this file dialog, either
  268. * <code>FileDialog.LOAD</code> or
  269. * <code>FileDialog.SAVE</code>
  270. * @see java.awt.FileDialog#LOAD
  271. * @see java.awt.FileDialog#SAVE
  272. * @see java.awt.FileDialog#getMode
  273. * @exception IllegalArgumentException if an illegal file
  274. * dialog mode is supplied
  275. * @since JDK1.1
  276. */
  277. public void setMode(int mode) {
  278. switch (mode) {
  279. case LOAD:
  280. case SAVE:
  281. this.mode = mode;
  282. break;
  283. default:
  284. throw new IllegalArgumentException("illegal file dialog mode");
  285. }
  286. }
  287. /**
  288. * Gets the directory of this file dialog.
  289. *
  290. * @return the (potentially <code>null</code> or invalid)
  291. * directory of this <code>FileDialog</code>
  292. * @see java.awt.FileDialog#setDirectory
  293. */
  294. public String getDirectory() {
  295. return dir;
  296. }
  297. /**
  298. * Sets the directory of this file dialog window to be the
  299. * specified directory. Specifying a <code>null</code> or an
  300. * invalid directory implies an implementation-defined default.
  301. * This default will not be realized, however, until the user
  302. * has selected a file. Until this point, <code>getDirectory()</code>
  303. * will return the value passed into this method.
  304. * <p>
  305. * Specifying "" as the directory is exactly equivalent to
  306. * specifying <code>null</code> as the directory.
  307. *
  308. * @param dir the specified directory
  309. * @see java.awt.FileDialog#getDirectory
  310. */
  311. public void setDirectory(String dir) {
  312. this.dir = (dir != null && dir.equals("")) ? null : dir;
  313. FileDialogPeer peer = (FileDialogPeer)this.peer;
  314. if (peer != null) {
  315. peer.setDirectory(this.dir);
  316. }
  317. }
  318. /**
  319. * Gets the selected file of this file dialog. If the user
  320. * selected <code>CANCEL</code>, the returned file is <code>null</code>.
  321. *
  322. * @return the currently selected file of this file dialog window,
  323. * or <code>null</code> if none is selected
  324. * @see java.awt.FileDialog#setFile
  325. */
  326. public String getFile() {
  327. return file;
  328. }
  329. /**
  330. * Sets the selected file for this file dialog window to be the
  331. * specified file. This file becomes the default file if it is set
  332. * before the file dialog window is first shown.
  333. * <p>
  334. * Specifying "" as the file is exactly equivalent to specifying
  335. * <code>null</code>
  336. * as the file.
  337. *
  338. * @param file the file being set
  339. * @see java.awt.FileDialog#getFile
  340. */
  341. public void setFile(String file) {
  342. this.file = (file != null && file.equals("")) ? null : file;
  343. FileDialogPeer peer = (FileDialogPeer)this.peer;
  344. if (peer != null) {
  345. peer.setFile(this.file);
  346. }
  347. }
  348. /**
  349. * Determines this file dialog's filename filter. A filename filter
  350. * allows the user to specify which files appear in the file dialog
  351. * window. Filename filters do not function in Sun's reference
  352. * implementation for Microsoft Windows.
  353. *
  354. * @return this file dialog's filename filter
  355. * @see java.io.FilenameFilter
  356. * @see java.awt.FileDialog#setFilenameFilter
  357. */
  358. public FilenameFilter getFilenameFilter() {
  359. return filter;
  360. }
  361. /**
  362. * Sets the filename filter for this file dialog window to the
  363. * specified filter.
  364. * Filename filters do not function in Sun's reference
  365. * implementation for Microsoft Windows.
  366. *
  367. * @param filter the specified filter
  368. * @see java.io.FilenameFilter
  369. * @see java.awt.FileDialog#getFilenameFilter
  370. */
  371. public synchronized void setFilenameFilter(FilenameFilter filter) {
  372. this.filter = filter;
  373. FileDialogPeer peer = (FileDialogPeer)this.peer;
  374. if (peer != null) {
  375. peer.setFilenameFilter(filter);
  376. }
  377. }
  378. /**
  379. * Reads the <code>ObjectInputStream</code> and performs
  380. * a backwards compatibility check by converting
  381. * either a <code>dir</code> or a <code>file</code>
  382. * equal to an empty string to <code>null</code>.
  383. *
  384. * @param s the <code>ObjectInputStream</code> to read
  385. */
  386. private void readObject(ObjectInputStream s)
  387. throws ClassNotFoundException, IOException
  388. {
  389. s.defaultReadObject();
  390. // 1.1 Compatibility: "" is not converted to null in 1.1
  391. if (dir != null && dir.equals("")) {
  392. dir = null;
  393. }
  394. if (file != null && file.equals("")) {
  395. file = null;
  396. }
  397. }
  398. /**
  399. * Returns a string representing the state of this <code>FileDialog</code>
  400. * window. This method is intended to be used only for debugging purposes,
  401. * and the content and format of the returned string may vary between
  402. * implementations. The returned string may be empty but may not be
  403. * <code>null</code>.
  404. *
  405. * @return the parameter string of this file dialog window
  406. */
  407. protected String paramString() {
  408. String str = super.paramString();
  409. str += ",dir= " + dir;
  410. str += ",file= " + file;
  411. return str + ((mode == LOAD) ? ",load" : ",save");
  412. }
  413. boolean postsOldMouseEvents() {
  414. return false;
  415. }
  416. }