KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JFileChooser


1 /*
2  * @(#)JFileChooser.java 1.108 07/03/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import javax.swing.event.*;
11 import javax.swing.filechooser.*;
12 import javax.swing.plaf.FileChooserUI JavaDoc;
13
14 import javax.accessibility.*;
15
16 import java.io.File JavaDoc;
17 import java.io.ObjectOutputStream JavaDoc;
18 import java.io.ObjectInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import java.util.Vector JavaDoc;
22 import java.awt.AWTEvent JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Container JavaDoc;
25 import java.awt.BorderLayout JavaDoc;
26 import java.awt.Window JavaDoc;
27 import java.awt.Dialog JavaDoc;
28 import java.awt.Frame JavaDoc;
29 import java.awt.GraphicsEnvironment JavaDoc;
30 import java.awt.HeadlessException JavaDoc;
31 import java.awt.EventQueue JavaDoc;
32 import java.awt.Toolkit JavaDoc;
33 import java.awt.event.*;
34 import java.beans.PropertyChangeListener JavaDoc;
35 import java.beans.PropertyChangeEvent JavaDoc;
36 import java.lang.ref.WeakReference JavaDoc;
37
38 /**
39  * <code>JFileChooser</code> provides a simple mechanism for the user to
40  * choose a file.
41  * For information about using <code>JFileChooser</code>, see
42  * <a
43  href="http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html">How to Use File Choosers</a>,
44  * a section in <em>The Java Tutorial</em>.
45  *
46  * <p>
47  *
48  * The following code pops up a file chooser for the user's home directory that
49  * sees only .jpg and .gif images:
50  * <pre>
51  * JFileChooser chooser = new JFileChooser();
52  * // Note: source for ExampleFileFilter can be found in FileChooserDemo,
53  * // under the demo/jfc directory in the JDK.
54  * ExampleFileFilter filter = new ExampleFileFilter();
55  * filter.addExtension("jpg");
56  * filter.addExtension("gif");
57  * filter.setDescription("JPG & GIF Images");
58  * chooser.setFileFilter(filter);
59  * int returnVal = chooser.showOpenDialog(parent);
60  * if(returnVal == JFileChooser.APPROVE_OPTION) {
61  * System.out.println("You chose to open this file: " +
62  * chooser.getSelectedFile().getName());
63  * }
64  * </pre>
65  *
66  * @beaninfo
67  * attribute: isContainer false
68  * description: A component which allows for the interactive selection of a file.
69  *
70  * @version 1.108 03/16/07
71  * @author Jeff Dinkins
72  *
73  */

74 public class JFileChooser extends JComponent JavaDoc implements Accessible {
75
76     /**
77      * @see #getUIClassID
78      * @see #readObject
79      */

80     private static final String JavaDoc uiClassID = "FileChooserUI";
81
82     // ************************
83
// ***** Dialog Types *****
84
// ************************
85

86     /**
87      * Type value indicating that the <code>JFileChooser</code> supports an
88      * "Open" file operation.
89      */

90     public static final int OPEN_DIALOG = 0;
91
92     /**
93      * Type value indicating that the <code>JFileChooser</code> supports a
94      * "Save" file operation.
95      */

96     public static final int SAVE_DIALOG = 1;
97
98     /**
99      * Type value indicating that the <code>JFileChooser</code> supports a
100      * developer-specified file operation.
101      */

102     public static final int CUSTOM_DIALOG = 2;
103
104
105     // ********************************
106
// ***** Dialog Return Values *****
107
// ********************************
108

109     /**
110      * Return value if cancel is chosen.
111      */

112     public static final int CANCEL_OPTION = 1;
113
114     /**
115      * Return value if approve (yes, ok) is chosen.
116      */

117     public static final int APPROVE_OPTION = 0;
118
119     /**
120      * Return value if an error occured.
121      */

122     public static final int ERROR_OPTION = -1;
123
124
125     // **********************************
126
// ***** JFileChooser properties *****
127
// **********************************
128

129
130     /** Instruction to display only files. */
131     public static final int FILES_ONLY = 0;
132
133     /** Instruction to display only directories. */
134     public static final int DIRECTORIES_ONLY = 1;
135
136     /** Instruction to display both files and directories. */
137     public static final int FILES_AND_DIRECTORIES = 2;
138
139     /** Instruction to cancel the current selection. */
140     public static final String JavaDoc CANCEL_SELECTION = "CancelSelection";
141
142     /**
143      * Instruction to approve the current selection
144      * (same as pressing yes or ok).
145      */

146     public static final String JavaDoc APPROVE_SELECTION = "ApproveSelection";
147
148     /** Identifies change in the text on the approve (yes, ok) button. */
149     public static final String JavaDoc APPROVE_BUTTON_TEXT_CHANGED_PROPERTY = "ApproveButtonTextChangedProperty";
150
151     /**
152      * Identifies change in the tooltip text for the approve (yes, ok)
153      * button.
154      */

155     public static final String JavaDoc APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY = "ApproveButtonToolTipTextChangedProperty";
156
157     /** Identifies change in the mnemonic for the approve (yes, ok) button. */
158     public static final String JavaDoc APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY = "ApproveButtonMnemonicChangedProperty";
159
160     /** Instruction to display the control buttons. */
161     public static final String JavaDoc CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY = "ControlButtonsAreShownChangedProperty";
162
163     /** Identifies user's directory change. */
164     public static final String JavaDoc DIRECTORY_CHANGED_PROPERTY = "directoryChanged";
165
166     /** Identifies change in user's single-file selection. */
167     public static final String JavaDoc SELECTED_FILE_CHANGED_PROPERTY = "SelectedFileChangedProperty";
168
169     /** Identifies change in user's multiple-file selection. */
170     public static final String JavaDoc SELECTED_FILES_CHANGED_PROPERTY = "SelectedFilesChangedProperty";
171
172     /** Enables multiple-file selections. */
173     public static final String JavaDoc MULTI_SELECTION_ENABLED_CHANGED_PROPERTY = "MultiSelectionEnabledChangedProperty";
174
175     /**
176      * Says that a different object is being used to find available drives
177      * on the system.
178      */

179     public static final String JavaDoc FILE_SYSTEM_VIEW_CHANGED_PROPERTY = "FileSystemViewChanged";
180
181     /**
182      * Says that a different object is being used to retrieve file
183      * information.
184      */

185     public static final String JavaDoc FILE_VIEW_CHANGED_PROPERTY = "fileViewChanged";
186
187     /** Identifies a change in the display-hidden-files property. */
188     public static final String JavaDoc FILE_HIDING_CHANGED_PROPERTY = "FileHidingChanged";
189
190     /** User changed the kind of files to display. */
191     public static final String JavaDoc FILE_FILTER_CHANGED_PROPERTY = "fileFilterChanged";
192
193     /**
194      * Identifies a change in the kind of selection (single,
195      * multiple, etc.).
196      */

197     public static final String JavaDoc FILE_SELECTION_MODE_CHANGED_PROPERTY = "fileSelectionChanged";
198
199     /**
200      * Says that a different accessory component is in use
201      * (for example, to preview files).
202      */

203     public static final String JavaDoc ACCESSORY_CHANGED_PROPERTY = "AccessoryChangedProperty";
204
205     /**
206      * Identifies whether a the AcceptAllFileFilter is used or not.
207      */

208     public static final String JavaDoc ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY = "acceptAllFileFilterUsedChanged";
209
210     /** Identifies a change in the dialog title. */
211     public static final String JavaDoc DIALOG_TITLE_CHANGED_PROPERTY = "DialogTitleChangedProperty";
212
213     /**
214      * Identifies a change in the type of files displayed (files only,
215      * directories only, or both files and directories).
216      */

217     public static final String JavaDoc DIALOG_TYPE_CHANGED_PROPERTY = "DialogTypeChangedProperty";
218
219     /**
220      * Identifies a change in the list of predefined file filters
221      * the user can choose from.
222      */

223     public static final String JavaDoc CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY = "ChoosableFileFilterChangedProperty";
224
225     // ******************************
226
// ***** instance variables *****
227
// ******************************
228

229     private String JavaDoc dialogTitle = null;
230     private String JavaDoc approveButtonText = null;
231     private String JavaDoc approveButtonToolTipText = null;
232     private int approveButtonMnemonic = 0;
233
234     private ActionListener actionListener = null;
235
236     private Vector JavaDoc filters = new Vector JavaDoc(5);
237     private JDialog JavaDoc dialog = null;
238     private int dialogType = OPEN_DIALOG;
239     private int returnValue = ERROR_OPTION;
240     private JComponent JavaDoc accessory = null;
241
242     private FileView fileView = null;
243
244     // uiFileView is not serialized, as it is initialized
245
// by updateUI() after deserialization
246
private transient FileView uiFileView = null;
247
248     private boolean controlsShown = true;
249
250     private boolean useFileHiding = true;
251     private static final String JavaDoc SHOW_HIDDEN_PROP = "awt.file.showHiddenFiles";
252
253     // Listens to changes in the native setting for showing hidden files.
254
// The Listener is removed and the native setting is ignored if
255
// setFileHidingEnabled() is ever called.
256
private PropertyChangeListener JavaDoc showFilesListener = null;
257
258     private int fileSelectionMode = FILES_ONLY;
259
260     private boolean multiSelectionEnabled = false;
261
262     private boolean useAcceptAllFileFilter = true;
263
264     private boolean dragEnabled = false;
265
266     private FileFilter fileFilter = null;
267
268     private FileSystemView fileSystemView = null;
269
270     private File JavaDoc currentDirectory = null;
271     private File JavaDoc selectedFile = null;
272     private File JavaDoc[] selectedFiles;
273
274     // *************************************
275
// ***** JFileChooser Constructors *****
276
// *************************************
277

278     /**
279      * Constructs a <code>JFileChooser</code> pointing to the user's
280      * default directory. This default depends on the operating system.
281      * It is typically the "My Documents" folder on Windows, and the
282      * user's home directory on Unix.
283      */

284     public JFileChooser() {
285     this((File JavaDoc) null, (FileSystemView) null);
286     }
287     
288     /**
289      * Constructs a <code>JFileChooser</code> using the given path.
290      * Passing in a <code>null</code>
291      * string causes the file chooser to point to the user's default directory.
292      * This default depends on the operating system. It is
293      * typically the "My Documents" folder on Windows, and the user's
294      * home directory on Unix.
295      *
296      * @param currentDirectoryPath a <code>String</code> giving the path
297      * to a file or directory
298      */

299     public JFileChooser(String JavaDoc currentDirectoryPath) {
300     this(currentDirectoryPath, (FileSystemView) null);
301     }
302
303     /**
304      * Constructs a <code>JFileChooser</code> using the given <code>File</code>
305      * as the path. Passing in a <code>null</code> file
306      * causes the file chooser to point to the user's default directory.
307      * This default depends on the operating system. It is
308      * typically the "My Documents" folder on Windows, and the user's
309      * home directory on Unix.
310      *
311      * @param currentDirectory a <code>File</code> object specifying
312      * the path to a file or directory
313      */

314     public JFileChooser(File JavaDoc currentDirectory) {
315     this(currentDirectory, (FileSystemView) null);
316     }
317
318     /**
319      * Constructs a <code>JFileChooser</code> using the given
320      * <code>FileSystemView</code>.
321      */

322     public JFileChooser(FileSystemView fsv) {
323     this((File JavaDoc) null, fsv);
324     }
325
326
327     /**
328      * Constructs a <code>JFileChooser</code> using the given current directory
329      * and <code>FileSystemView</code>.
330      */

331     public JFileChooser(File JavaDoc currentDirectory, FileSystemView fsv) {
332     setup(fsv);
333     setCurrentDirectory(currentDirectory);
334     }
335
336     /**
337      * Constructs a <code>JFileChooser</code> using the given current directory
338      * path and <code>FileSystemView</code>.
339      */

340     public JFileChooser(String JavaDoc currentDirectoryPath, FileSystemView fsv) {
341     setup(fsv);
342     if(currentDirectoryPath == null) {
343         setCurrentDirectory(null);
344         } else {
345         setCurrentDirectory(fileSystemView.createFileObject(currentDirectoryPath));
346     }
347     }
348
349     /**
350      * Performs common constructor initialization and setup.
351      */

352     protected void setup(FileSystemView view) {
353         // Track native setting for showing hidden files
354
Toolkit JavaDoc tk = Toolkit.getDefaultToolkit();
355         Object JavaDoc showHiddenProperty = tk.getDesktopProperty(SHOW_HIDDEN_PROP);
356         if (showHiddenProperty instanceof Boolean JavaDoc) {
357             useFileHiding = !((Boolean JavaDoc)showHiddenProperty).booleanValue();
358             showFilesListener = new WeakPCL(this);
359             tk.addPropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener);
360         }
361
362         if(view == null) {
363             view = FileSystemView.getFileSystemView();
364         }
365         setFileSystemView(view);
366         updateUI();
367         if(isAcceptAllFileFilterUsed()) {
368             setFileFilter(getAcceptAllFileFilter());
369         }
370     }
371
372     /**
373      * Sets the <code>dragEnabled</code> property,
374      * which must be <code>true</code> to enable
375      * automatic drag handling (the first part of drag and drop)
376      * on this component.
377      * The <code>transferHandler</code> property needs to be set
378      * to a non-<code>null</code> value for the drag to do
379      * anything. The default value of the <code>dragEnabled</code>
380      * property
381      * is <code>false</code>.
382      *
383      * <p>
384      *
385      * When automatic drag handling is enabled,
386      * most look and feels begin a drag-and-drop operation
387      * whenever the user presses the mouse button over a selection
388      * and then moves the mouse a few pixels.
389      * Setting this property to <code>true</code>
390      * can therefore have a subtle effect on
391      * how selections behave.
392      *
393      * <p>
394      *
395      * Some look and feels might not support automatic drag and drop;
396      * they will ignore this property. You can work around such
397      * look and feels by modifying the component
398      * to directly call the <code>exportAsDrag</code> method of a
399      * <code>TransferHandler</code>.
400      *
401      * @param b the value to set the <code>dragEnabled</code> property to
402      * @exception HeadlessException if
403      * <code>b</code> is <code>true</code> and
404      * <code>GraphicsEnvironment.isHeadless()</code>
405      * returns <code>true</code>
406      * @see java.awt.GraphicsEnvironment#isHeadless
407      * @see #getDragEnabled
408      * @see #setTransferHandler
409      * @see TransferHandler
410      * @since 1.4
411      *
412      * @beaninfo
413      * description: determines whether automatic drag handling is enabled
414      * bound: false
415      */

416     public void setDragEnabled(boolean b) {
417         if (b && GraphicsEnvironment.isHeadless()) {
418             throw new HeadlessException JavaDoc();
419         }
420     dragEnabled = b;
421     }
422
423     /**
424      * Gets the value of the <code>dragEnabled</code> property.
425      *
426      * @return the value of the <code>dragEnabled</code> property
427      * @see #setDragEnabled
428      * @since 1.4
429      */

430     public boolean getDragEnabled() {
431     return dragEnabled;
432     }
433
434     // *****************************
435
// ****** File Operations ******
436
// *****************************
437

438     /**
439      * Returns the selected file. This can be set either by the
440      * programmer via <code>setFile</code> or by a user action, such as
441      * either typing the filename into the UI or selecting the
442      * file from a list in the UI.
443      *
444      * @see #setSelectedFile
445      * @return the selected file
446      */

447     public File JavaDoc getSelectedFile() {
448     return selectedFile;
449     }
450
451     /**
452      * Sets the selected file. If the file's parent directory is
453      * not the current directory, changes the current directory
454      * to be the file's parent directory.
455      *
456      * @beaninfo
457      * preferred: true
458      * bound: true
459      *
460      * @see #getSelectedFile
461      *
462      * @param file the selected file
463      */

464     public void setSelectedFile(File JavaDoc file) {
465     File JavaDoc oldValue = selectedFile;
466     selectedFile = file;
467     if(selectedFile != null) {
468         if (file.isAbsolute() && !getFileSystemView().isParent(getCurrentDirectory(), selectedFile)) {
469         setCurrentDirectory(selectedFile.getParentFile());
470         }
471         if (!isMultiSelectionEnabled() || selectedFiles == null || selectedFiles.length == 1) {
472         ensureFileIsVisible(selectedFile);
473         }
474     }
475     firePropertyChange(SELECTED_FILE_CHANGED_PROPERTY, oldValue, selectedFile);
476     }
477
478     /**
479      * Returns a list of selected files if the file chooser is
480      * set to allow multiple selection.
481      */

482     public File JavaDoc[] getSelectedFiles() {
483     if(selectedFiles == null) {
484         return new File JavaDoc[0];
485     } else {
486         return (File JavaDoc[]) selectedFiles.clone();
487     }
488     }
489
490     /**
491      * Sets the list of selected files if the file chooser is
492      * set to allow multiple selection.
493      *
494      * @beaninfo
495      * bound: true
496      * description: The list of selected files if the chooser is in multiple selection mode.
497      */

498     public void setSelectedFiles(File JavaDoc[] selectedFiles) {
499     File JavaDoc[] oldValue = this.selectedFiles;
500     if (selectedFiles != null && selectedFiles.length == 0) {
501         selectedFiles = null;
502     }
503     this.selectedFiles = selectedFiles;
504     setSelectedFile((selectedFiles != null) ? selectedFiles[0] : null);
505     firePropertyChange(SELECTED_FILES_CHANGED_PROPERTY, oldValue, this.selectedFiles);
506     }
507
508     /**
509      * Returns the current directory.
510      *
511      * @return the current directory
512      * @see #setCurrentDirectory
513      */

514     public File JavaDoc getCurrentDirectory() {
515     return currentDirectory;
516     }
517
518     /**
519      * Sets the current directory. Passing in <code>null</code> sets the
520      * file chooser to point to the user's default directory.
521      * This default depends on the operating system. It is
522      * typically the "My Documents" folder on Windows, and the user's
523      * home directory on Unix.
524      *
525      * If the file passed in as <code>currentDirectory</code> is not a
526      * directory, the parent of the file will be used as the currentDirectory.
527      * If the parent is not traversable, then it will walk up the parent tree
528      * until it finds a traversable directory, or hits the root of the
529      * file system.
530      *
531      * @beaninfo
532      * preferred: true
533      * bound: true
534      * description: The directory that the JFileChooser is showing files of.
535      *
536      * @param dir the current directory to point to
537      * @see #getCurrentDirectory
538      */

539     public void setCurrentDirectory(File JavaDoc dir) {
540     File JavaDoc oldValue = currentDirectory;
541     
542     if (dir != null && !dir.exists()) {
543         dir = currentDirectory;
544     }
545     if (dir == null) {
546         dir = getFileSystemView().getDefaultDirectory();
547     }
548     if (currentDirectory != null) {
549         /* Verify the toString of object */
550         if (this.currentDirectory.equals(dir)) {
551         return;
552         }
553     }
554     
555     File JavaDoc prev = null;
556     while (!isTraversable(dir) && prev != dir) {
557         prev = dir;
558         dir = getFileSystemView().getParentDirectory(dir);
559     }
560     currentDirectory = dir;
561
562     firePropertyChange(DIRECTORY_CHANGED_PROPERTY, oldValue, currentDirectory);
563     }
564
565     /**
566      * Changes the directory to be set to the parent of the
567      * current directory.
568      *
569      * @see #getCurrentDirectory
570      */

571     public void changeToParentDirectory() {
572     selectedFile = null;
573     File JavaDoc oldValue = getCurrentDirectory();
574     setCurrentDirectory(getFileSystemView().getParentDirectory(oldValue));
575     }
576
577     /**
578      * Tells the UI to rescan its files list from the current directory.
579      */

580     public void rescanCurrentDirectory() {
581         getUI().rescanCurrentDirectory(this);
582     }
583
584     /**
585      * Makes sure that the specified file is viewable, and
586      * not hidden.
587      *
588      * @param f a File object
589      */

590     public void ensureFileIsVisible(File JavaDoc f) {
591         getUI().ensureFileIsVisible(this, f);
592     }
593
594     // **************************************
595
// ***** JFileChooser Dialog methods *****
596
// **************************************
597

598     /**
599      * Pops up an "Open File" file chooser dialog. Note that the
600      * text that appears in the approve button is determined by
601      * the L&F.
602      *
603      * @param parent the parent component of the dialog,
604      * can be <code>null</code>;
605      * see <code>showDialog</code> for details
606      * @return the return state of the file chooser on popdown:
607      * <ul>
608      * <li>JFileChooser.CANCEL_OPTION
609      * <li>JFileChooser.APPROVE_OPTION
610      * <li>JFileCHooser.ERROR_OPTION if an error occurs or the
611      * dialog is dismissed
612      * </ul>
613      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
614      * returns true.
615      * @see java.awt.GraphicsEnvironment#isHeadless
616      * @see #showDialog
617      */

618     public int showOpenDialog(Component JavaDoc parent) throws HeadlessException JavaDoc {
619     setDialogType(OPEN_DIALOG);
620     return showDialog(parent, null);
621     }
622
623     /**
624      * Pops up a "Save File" file chooser dialog. Note that the
625      * text that appears in the approve button is determined by
626      * the L&F.
627      *
628      * @param parent the parent component of the dialog,
629      * can be <code>null</code>;
630      * see <code>showDialog</code> for details
631      * @return the return state of the file chooser on popdown:
632      * <ul>
633      * <li>JFileChooser.CANCEL_OPTION
634      * <li>JFileChooser.APPROVE_OPTION
635      * <li>JFileCHooser.ERROR_OPTION if an error occurs or the
636      * dialog is dismissed
637      * </ul>
638      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
639      * returns true.
640      * @see java.awt.GraphicsEnvironment#isHeadless
641      * @see #showDialog
642      */

643     public int showSaveDialog(Component JavaDoc parent) throws HeadlessException JavaDoc {
644     setDialogType(SAVE_DIALOG);
645     return showDialog(parent, null);
646     }
647
648     /**
649      * Pops a custom file chooser dialog with a custom approve button.
650      * For example, the following code
651      * pops up a file chooser with a "Run Application" button
652      * (instead of the normal "Save" or "Open" button):
653      * <pre>
654      * filechooser.showDialog(parentFrame, "Run Application");
655      * </pre>
656      *
657      * Alternatively, the following code does the same thing:
658      * <pre>
659      * JFileChooser chooser = new JFileChooser(null);
660      * chooser.setApproveButtonText("Run Application");
661      * chooser.showDialog(parentFrame, null);
662      * </pre>
663      *
664      * <!--PENDING(jeff) - the following method should be added to the api:
665      * showDialog(Component parent);-->
666      * <!--PENDING(kwalrath) - should specify modality and what
667      * "depends" means.-->
668      *
669      * <p>
670      *
671      * The <code>parent</code> argument determines two things:
672      * the frame on which the open dialog depends and
673      * the component whose position the look and feel
674      * should consider when placing the dialog. If the parent
675      * is a <code>Frame</code> object (such as a <code>JFrame</code>)
676      * then the dialog depends on the frame and
677      * the look and feel positions the dialog
678      * relative to the frame (for example, centered over the frame).
679      * If the parent is a component, then the dialog
680      * depends on the frame containing the component,
681      * and is positioned relative to the component
682      * (for example, centered over the component).
683      * If the parent is <code>null</code>, then the dialog depends on
684      * no visible window, and it's placed in a
685      * look-and-feel-dependent position
686      * such as the center of the screen.
687      *
688      * @param parent the parent component of the dialog;
689      * can be <code>null</code>
690      * @param approveButtonText the text of the <code>ApproveButton</code>
691      * @return the return state of the file chooser on popdown:
692      * <ul>
693      * <li>JFileChooser.CANCEL_OPTION
694      * <li>JFileChooser.APPROVE_OPTION
695      * <li>JFileCHooser.ERROR_OPTION if an error occurs or the
696      * dialog is dismissed
697      * </ul>
698      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
699      * returns true.
700      * @see java.awt.GraphicsEnvironment#isHeadless
701      */

702     public int showDialog(Component JavaDoc parent, String JavaDoc approveButtonText)
703         throws HeadlessException JavaDoc {
704     if(approveButtonText != null) {
705         setApproveButtonText(approveButtonText);
706         setDialogType(CUSTOM_DIALOG);
707     }
708     dialog = createDialog(parent);
709     dialog.addWindowListener(new WindowAdapter() {
710         public void windowClosing(WindowEvent e) {
711         returnValue = CANCEL_OPTION;
712         }
713     });
714     returnValue = ERROR_OPTION;
715     rescanCurrentDirectory();
716
717     dialog.show();
718     firePropertyChange("JFileChooserDialogIsClosingProperty", dialog, null);
719     dialog.removeAll();
720     dialog.dispose();
721     dialog = null;
722     return returnValue;
723     }
724
725     /**
726      * Creates and returns a new <code>JDialog</code> wrapping
727      * <code>this</code> centered on the <code>parent</code>
728      * in the <code>parent</code>'s frame.
729      * This method can be overriden to further manipulate the dialog,
730      * to disable resizing, set the location, etc. Example:
731      * <pre>
732      * class MyFileChooser extends JFileChooser {
733      * protected JDialog createDialog(Component parent) throws HeadlessException {
734      * JDialog dialog = super.createDialog(parent);
735      * dialog.setLocation(300, 200);
736      * dialog.setResizable(false);
737      * return dialog;
738      * }
739      * }
740      * </pre>
741      *
742      * @param parent the parent component of the dialog;
743      * can be <code>null</code>
744      * @return a new <code>JDialog</code> containing this instance
745      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
746      * returns true.
747      * @see java.awt.GraphicsEnvironment#isHeadless
748      * @since 1.4
749      */

750     protected JDialog JavaDoc createDialog(Component JavaDoc parent) throws HeadlessException JavaDoc {
751     String JavaDoc title = getUI().getDialogTitle(this);
752         getAccessibleContext().setAccessibleDescription(title);
753
754         JDialog JavaDoc dialog;
755         Window JavaDoc window = JOptionPane.getWindowForComponent(parent);
756         if (window instanceof Frame JavaDoc) {
757             dialog = new JDialog JavaDoc((Frame JavaDoc)window, title, true);
758         } else {
759             dialog = new JDialog JavaDoc((Dialog JavaDoc)window, title, true);
760         }
761         dialog.setComponentOrientation(this.getComponentOrientation());
762
763         Container JavaDoc contentPane = dialog.getContentPane();
764         contentPane.setLayout(new BorderLayout JavaDoc());
765         contentPane.add(this, BorderLayout.CENTER);
766  
767         if (JDialog.isDefaultLookAndFeelDecorated()) {
768             boolean supportsWindowDecorations =
769             UIManager.getLookAndFeel().getSupportsWindowDecorations();
770             if (supportsWindowDecorations) {
771                 dialog.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG);
772             }
773         }
774         dialog.pack();
775         dialog.setLocationRelativeTo(parent);
776
777     return dialog;
778     }
779
780     // **************************
781
// ***** Dialog Options *****
782
// **************************
783

784     /**
785      * Returns the value of the <code>controlButtonsAreShown</code>
786      * property.
787      *
788      * @return the value of the <code>controlButtonsAreShown</code>
789      * property
790      *
791      * @see #setControlButtonsAreShown
792      * @since 1.3
793      */

794     public boolean getControlButtonsAreShown() {
795     return controlsShown;
796     }
797
798     
799     /**
800      * Sets the property
801      * that indicates whether the <i>approve</i> and <i>cancel</i>
802      * buttons are shown in the file chooser. This property
803      * is <code>true</code> by default. Look and feels
804      * that always show these buttons will ignore the value
805      * of this property.
806      * This method fires a property-changed event,
807      * using the string value of
808      * <code>CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY</code>
809      * as the name of the property.
810      *
811      * @param b <code>false</code> if control buttons should not be
812      * shown; otherwise, <code>true</code>
813      *
814      * @beaninfo
815      * preferred: true
816      * bound: true
817      * description: Sets whether the approve & cancel buttons are shown.
818      *
819      * @see #getControlButtonsAreShown
820      * @see #CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
821      * @since 1.3
822      */

823     public void setControlButtonsAreShown(boolean b) {
824     if(controlsShown == b) {
825         return;
826     }
827     boolean oldValue = controlsShown;
828     controlsShown = b;
829     firePropertyChange(CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY, oldValue, controlsShown);
830     }
831
832     /**
833      * Returns the type of this dialog. The default is
834      * <code>JFileChooser.OPEN_DIALOG</code>.
835      *
836      * @return the type of dialog to be displayed:
837      * <ul>
838      * <li>JFileChooser.OPEN_DIALOG
839      * <li>JFileChooser.SAVE_DIALOG
840      * <li>JFileChooser.CUSTOM_DIALOG
841      * </ul>
842      *
843      * @see #setDialogType
844      */

845     public int getDialogType() {
846     return dialogType;
847     }
848
849     /**
850      * Sets the type of this dialog. Use <code>OPEN_DIALOG</code> when you
851      * want to bring up a file chooser that the user can use to open a file.
852      * Likewise, use <code>SAVE_DIALOG</code> for letting the user choose
853      * a file for saving.
854      * Use <code>CUSTOM_DIALOG</code> when you want to use the file
855      * chooser in a context other than "Open" or "Save".
856      * For instance, you might want to bring up a file chooser that allows
857      * the user to choose a file to execute. Note that you normally would not
858      * need to set the <code>JFileChooser</code> to use
859      * <code>CUSTOM_DIALOG</code>
860      * since a call to <code>setApproveButtonText</code> does this for you.
861      * The default dialog type is <code>JFileChooser.OPEN_DIALOG</code>.
862      *
863      * @param dialogType the type of dialog to be displayed:
864      * <ul>
865      * <li>JFileChooser.OPEN_DIALOG
866      * <li>JFileChooser.SAVE_DIALOG
867      * <li>JFileChooser.CUSTOM_DIALOG
868      * </ul>
869      *
870      * @exception IllegalArgumentException if <code>dialogType</code> is
871      * not legal
872      * @beaninfo
873      * preferred: true
874      * bound: true
875      * description: The type (open, save, custom) of the JFileChooser.
876      * enum:
877      * OPEN_DIALOG JFileChooser.OPEN_DIALOG
878      * SAVE_DIALOG JFileChooser.SAVE_DIALOG
879      * CUSTOM_DIALOG JFileChooser.CUSTOM_DIALOG
880      *
881      * @see #getDialogType
882      * @see #setApproveButtonText
883      */

884     // PENDING(jeff) - fire button text change property
885
public void setDialogType(int dialogType) {
886     if(this.dialogType == dialogType) {
887         return;
888     }
889     if(!(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG || dialogType == CUSTOM_DIALOG)) {
890         throw new IllegalArgumentException JavaDoc("Incorrect Dialog Type: " + dialogType);
891     }
892     int oldValue = this.dialogType;
893     this.dialogType = dialogType;
894     if(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG) {
895         setApproveButtonText(null);
896     }
897     firePropertyChange(DIALOG_TYPE_CHANGED_PROPERTY, oldValue, dialogType);
898     }
899
900     /**
901      * Sets the string that goes in the <code>JFileChooser</code> window's
902      * title bar.
903      *
904      * @param dialogTitle the new <code>String</code> for the title bar
905      *
906      * @beaninfo
907      * preferred: true
908      * bound: true
909      * description: The title of the JFileChooser dialog window.
910      *
911      * @see #getDialogTitle
912      *
913      */

914     public void setDialogTitle(String JavaDoc dialogTitle) {
915     String JavaDoc oldValue = this.dialogTitle;
916     this.dialogTitle = dialogTitle;
917     if(dialog != null) {
918         dialog.setTitle(dialogTitle);
919     }
920     firePropertyChange(DIALOG_TITLE_CHANGED_PROPERTY, oldValue, dialogTitle);
921     }
922
923     /**
924      * Gets the string that goes in the <code>JFileChooser</code>'s titlebar.
925      *
926      * @see #setDialogTitle
927      */

928     public String JavaDoc getDialogTitle() {
929     return dialogTitle;
930     }
931
932     // ************************************
933
// ***** JFileChooser View Options *****
934
// ************************************
935

936
937
938     /**
939      * Sets the tooltip text used in the <code>ApproveButton</code>.
940      * If <code>null</code>, the UI object will determine the button's text.
941      *
942      * @beaninfo
943      * preferred: true
944      * bound: true
945      * description: The tooltip text for the ApproveButton.
946      *
947      * @return the text used in the ApproveButton
948      *
949      * @see #setApproveButtonText
950      * @see #setDialogType
951      * @see #showDialog
952      */

953     public void setApproveButtonToolTipText(String JavaDoc toolTipText) {
954     if(approveButtonToolTipText == toolTipText) {
955         return;
956     }
957     String JavaDoc oldValue = approveButtonToolTipText;
958     approveButtonToolTipText = toolTipText;
959     firePropertyChange(APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY, oldValue, approveButtonToolTipText);
960     }
961
962
963     /**
964      * Returns the tooltip text used in the <code>ApproveButton</code>.
965      * If <code>null</code>, the UI object will determine the button's text.
966      *
967      * @return the text used in the <code>ApproveButton</code>
968      *
969      * @see #setApproveButtonText
970      * @see #setDialogType
971      * @see #showDialog
972      */

973     public String JavaDoc getApproveButtonToolTipText() {
974     return approveButtonToolTipText;
975     }
976
977     /**
978      * Returns the approve button's mnemonic.
979      * @return an integer value for the mnemonic key
980      *
981      * @see #setApproveButtonMnemonic
982      */

983     public int getApproveButtonMnemonic() {
984     return approveButtonMnemonic;
985     }
986
987     /**
988      * Sets the approve button's mnemonic using a numeric keycode.
989      *
990      * @param mnemonic an integer value for the mnemonic key
991      *
992      * @beaninfo
993      * preferred: true
994      * bound: true
995      * description: The mnemonic key accelerator for the ApproveButton.
996      *
997      * @see #getApproveButtonMnemonic
998      */

999     public void setApproveButtonMnemonic(int mnemonic) {
1000    if(approveButtonMnemonic == mnemonic) {
1001       return;
1002    }
1003    int oldValue = approveButtonMnemonic;
1004    approveButtonMnemonic = mnemonic;
1005    firePropertyChange(APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY, oldValue, approveButtonMnemonic);
1006    }
1007
1008    /**
1009     * Sets the approve button's mnemonic using a character.
1010     * @param mnemonic a character value for the mnemonic key
1011     *
1012     * @see #getApproveButtonMnemonic
1013     */

1014    public void setApproveButtonMnemonic(char mnemonic) {
1015        int vk = (int) mnemonic;
1016        if(vk >= 'a' && vk <='z') {
1017        vk -= ('a' - 'A');
1018    }
1019        setApproveButtonMnemonic(vk);
1020    }
1021
1022
1023    /**
1024     * Sets the text used in the <code>ApproveButton</code> in the
1025     * <code>FileChooserUI</code>.
1026     *
1027     * @beaninfo
1028     * preferred: true
1029     * bound: true
1030     * description: The text that goes in the ApproveButton.
1031     *
1032     * @param approveButtonText the text used in the <code>ApproveButton</code>
1033     *
1034     * @see #getApproveButtonText
1035     * @see #setDialogType
1036     * @see #showDialog
1037     */

1038    // PENDING(jeff) - have ui set this on dialog type change
1039
public void setApproveButtonText(String JavaDoc approveButtonText) {
1040    if(this.approveButtonText == approveButtonText) {
1041        return;
1042    }
1043    String JavaDoc oldValue = this.approveButtonText;
1044    this.approveButtonText = approveButtonText;
1045    firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, oldValue, approveButtonText);
1046    }
1047
1048    /**
1049     * Returns the text used in the <code>ApproveButton</code> in the
1050     * <code>FileChooserUI</code>.
1051     * If <code>null</code>, the UI object will determine the button's text.
1052     *
1053     * Typically, this would be "Open" or "Save".
1054     *
1055     * @return the text used in the <code>ApproveButton</code>
1056     *
1057     * @see #setApproveButtonText
1058     * @see #setDialogType
1059     * @see #showDialog
1060     */

1061    public String JavaDoc getApproveButtonText() {
1062    return approveButtonText;
1063    }
1064
1065    /**
1066     * Gets the list of user choosable file filters.
1067     *
1068     * @return a <code>FileFilter</code> array containing all the choosable
1069     * file filters
1070     *
1071     * @see #addChoosableFileFilter
1072     * @see #removeChoosableFileFilter
1073     * @see #resetChoosableFileFilters
1074     */

1075    public FileFilter[] getChoosableFileFilters() {
1076    FileFilter[] filterArray = new FileFilter[filters.size()];
1077    filters.copyInto(filterArray);
1078    return filterArray;
1079    }
1080
1081    /**
1082     * Adds a filter to the list of user choosable file filters.
1083     * For information on setting the file selection mode, see
1084     * {@link #setFileSelectionMode setFileSelectionMode}.
1085     *
1086     * @param filter the <code>FileFilter</code> to add to the choosable file
1087     * filter list
1088     *
1089     * @beaninfo
1090     * preferred: true
1091     * bound: true
1092     * description: Adds a filter to the list of user choosable file filters.
1093     *
1094     * @see #getChoosableFileFilters
1095     * @see #removeChoosableFileFilter
1096     * @see #resetChoosableFileFilters
1097     * @see #setFileSelectionMode
1098     */

1099    public void addChoosableFileFilter(FileFilter filter) {
1100    if(filter != null && !filters.contains(filter)) {
1101        FileFilter[] oldValue = getChoosableFileFilters();
1102        filters.addElement(filter);
1103        firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
1104    }
1105    setFileFilter(filter);
1106    }
1107
1108    /**
1109     * Removes a filter from the list of user choosable file filters. Returns
1110     * true if the file filter was removed.
1111     *
1112     * @see #addChoosableFileFilter
1113     * @see #getChoosableFileFilters
1114     * @see #resetChoosableFileFilters
1115     */

1116    public boolean removeChoosableFileFilter(FileFilter f) {
1117    if(filters.contains(f)) {
1118            if(getFileFilter() == f) {
1119        setFileFilter(null);
1120            }
1121        FileFilter[] oldValue = getChoosableFileFilters();
1122        filters.removeElement(f);
1123        firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
1124        return true;
1125    } else {
1126        return false;
1127    }
1128    }
1129
1130    /**
1131     * Resets the choosable file filter list to its starting state. Normally,
1132     * this removes all added file filters while leaving the
1133     * <code>AcceptAll</code> file filter.
1134     *
1135     * @see #addChoosableFileFilter
1136     * @see #getChoosableFileFilters
1137     * @see #removeChoosableFileFilter
1138     */

1139    public void resetChoosableFileFilters() {
1140    FileFilter[] oldValue = getChoosableFileFilters();
1141    setFileFilter(null);
1142    filters.removeAllElements();
1143    if(isAcceptAllFileFilterUsed()) {
1144       addChoosableFileFilter(getAcceptAllFileFilter());
1145    }
1146    firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
1147    }
1148
1149    /**
1150     * Returns the <code>AcceptAll</code> file filter.
1151     * For example, on Microsoft Windows this would be All Files (*.*).
1152     */

1153    public FileFilter getAcceptAllFileFilter() {
1154    FileFilter filter = null;
1155    if(getUI() != null) {
1156        filter = getUI().getAcceptAllFileFilter(this);
1157    }
1158    return filter;
1159    }
1160
1161   /**
1162    * Returns whether the <code>AcceptAll FileFilter</code> is used.
1163    * @return true if the <code>AcceptAll FileFilter</code> is used
1164    * @see #setAcceptAllFileFilterUsed
1165    * @since 1.3
1166    */

1167    public boolean isAcceptAllFileFilterUsed() {
1168    return useAcceptAllFileFilter;
1169    }
1170
1171   /**
1172    * Determines whether the <code>AcceptAll FileFilter</code> is used
1173    * as an available choice in the choosable filter list.
1174    * If false, the <code>AcceptAll</code> file filter is removed from
1175    * the list of available file filters.
1176    * If true, the <code>AcceptAll</code> file filter will become the
1177    * the actively used file filter.
1178    *
1179    * @beaninfo
1180    * preferred: true
1181    * bound: true
1182    * description: Sets whether the AcceptAll FileFilter is used as an available choice in the choosable filter list.
1183    *
1184    * @see #isAcceptAllFileFilterUsed
1185    * @see #getAcceptAllFileFilter
1186    * @see #setFileFilter
1187    * @since 1.3
1188    */

1189    public void setAcceptAllFileFilterUsed(boolean b) {
1190    boolean oldValue = useAcceptAllFileFilter;
1191    useAcceptAllFileFilter = b;
1192    if(!b) {
1193        removeChoosableFileFilter(getAcceptAllFileFilter());
1194    } else {
1195        removeChoosableFileFilter(getAcceptAllFileFilter());
1196        addChoosableFileFilter(getAcceptAllFileFilter());
1197    }
1198    firePropertyChange(ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY, oldValue, useAcceptAllFileFilter);
1199    }
1200
1201    /**
1202     * Returns the accessory component.
1203     *
1204     * @return this JFileChooser's accessory component, or null
1205     * @see #setAccessory
1206     */

1207    public JComponent JavaDoc getAccessory() {
1208        return accessory;
1209    }
1210
1211    /**
1212     * Sets the accessory component. An accessory is often used to show a
1213     * preview image of the selected file; however, it can be used for anything
1214     * that the programmer wishes, such as extra custom file chooser controls.
1215     *
1216     * <p>
1217     * Note: if there was a previous accessory, you should unregister
1218     * any listeners that the accessory might have registered with the
1219     * file chooser.
1220     *
1221     * @beaninfo
1222     * preferred: true
1223     * bound: true
1224     * description: Sets the accessory component on the JFileChooser.
1225     */

1226    public void setAccessory(JComponent JavaDoc newAccessory) {
1227        JComponent JavaDoc oldValue = accessory;
1228        accessory = newAccessory;
1229    firePropertyChange(ACCESSORY_CHANGED_PROPERTY, oldValue, accessory);
1230    }
1231    
1232    /**
1233     * Sets the <code>JFileChooser</code> to allow the user to just
1234     * select files, just select
1235     * directories, or select both files and directories. The default is
1236     * <code>JFilesChooser.FILES_ONLY</code>.
1237     *
1238     * @param mode the type of files to be displayed:
1239     * <ul>
1240     * <li>JFileChooser.FILES_ONLY
1241     * <li>JFileChooser.DIRECTORIES_ONLY
1242     * <li>JFileChooser.FILES_AND_DIRECTORIES
1243     * </ul>
1244     *
1245     * @exception IllegalArgumentException if <code>mode</code> is an
1246     * illegal file selection mode
1247     * @beaninfo
1248     * preferred: true
1249     * bound: true
1250     * description: Sets the types of files that the JFileChooser can choose.
1251     * enum: FILES_ONLY JFileChooser.FILES_ONLY
1252     * DIRECTORIES_ONLY JFileChooser.DIRECTORIES_ONLY
1253     * FILES_AND_DIRECTORIES JFileChooser.FILES_AND_DIRECTORIES
1254     *
1255     *
1256     * @see #getFileSelectionMode
1257     */

1258    public void setFileSelectionMode(int mode) {
1259    if(fileSelectionMode == mode) {
1260        return;
1261    }
1262
1263        if ((mode == FILES_ONLY) || (mode == DIRECTORIES_ONLY) || (mode == FILES_AND_DIRECTORIES)) {
1264       int oldValue = fileSelectionMode;
1265       fileSelectionMode = mode;
1266       firePropertyChange(FILE_SELECTION_MODE_CHANGED_PROPERTY, oldValue, fileSelectionMode);
1267        } else {
1268       throw new IllegalArgumentException JavaDoc("Incorrect Mode for file selection: " + mode);
1269        }
1270    }
1271
1272    /**
1273     * Returns the current file-selection mode. The default is
1274     * <code>JFilesChooser.FILES_ONLY</code>.
1275     *
1276     * @return the type of files to be displayed, one of the following:
1277     * <ul>
1278     * <li>JFileChooser.FILES_ONLY
1279     * <li>JFileChooser.DIRECTORIES_ONLY
1280     * <li>JFileChooser.FILES_AND_DIRECTORIES
1281     * </ul>
1282     * @see #setFileSelectionMode
1283     */

1284    public int getFileSelectionMode() {
1285    return fileSelectionMode;
1286    }
1287
1288    /**
1289     * Convenience call that determines if files are selectable based on the
1290     * current file selection mode.
1291     *
1292     * @see #setFileSelectionMode
1293     * @see #getFileSelectionMode
1294     */

1295    public boolean isFileSelectionEnabled() {
1296    return ((fileSelectionMode == FILES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES));
1297    }
1298
1299    /**
1300     * Convenience call that determines if directories are selectable based
1301     * on the current file selection mode.
1302     *
1303     * @see #setFileSelectionMode
1304     * @see #getFileSelectionMode
1305     */

1306    public boolean isDirectorySelectionEnabled() {
1307    return ((fileSelectionMode == DIRECTORIES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES));
1308    }
1309
1310    /**
1311     * Sets the file chooser to allow multiple file selections.
1312     *
1313     * @param b true if multiple files may be selected
1314     * @beaninfo
1315     * bound: true
1316     * description: Sets multiple file selection mode.
1317     *
1318     * @see #isMultiSelectionEnabled
1319     */

1320    public void setMultiSelectionEnabled(boolean b) {
1321    if(multiSelectionEnabled == b) {
1322        return;
1323    }
1324    boolean oldValue = multiSelectionEnabled;
1325    multiSelectionEnabled = b;
1326    firePropertyChange(MULTI_SELECTION_ENABLED_CHANGED_PROPERTY, oldValue, multiSelectionEnabled);
1327    }
1328
1329    /**
1330     * Returns true if multiple files can be selected.
1331     * @return true if multiple files can be selected
1332     * @see #setMultiSelectionEnabled
1333     */

1334    public boolean isMultiSelectionEnabled() {
1335    return multiSelectionEnabled;
1336    }
1337
1338    
1339    /**
1340     * Returns true if hidden files are not shown in the file chooser;
1341     * otherwise, returns false.
1342     *
1343     * @return the status of the file hiding property
1344     * @see #setFileHidingEnabled
1345     */

1346    public boolean isFileHidingEnabled() {
1347    return useFileHiding;
1348    }
1349
1350    /**
1351     * Sets file hiding on or off. If true, hidden files are not shown
1352     * in the file chooser. The job of determining which files are
1353     * shown is done by the <code>FileView</code>.
1354     *
1355     * @beaninfo
1356     * preferred: true
1357     * bound: true
1358     * description: Sets file hiding on or off.
1359     *
1360     * @param b the boolean value that determines whether file hiding is
1361     * turned on
1362     * @see #isFileHidingEnabled
1363     */

1364    public void setFileHidingEnabled(boolean b) {
1365        // Dump showFilesListener since we'll ignore it from now on
1366
if (showFilesListener != null) {
1367            Toolkit.getDefaultToolkit().removePropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener);
1368            showFilesListener = null;
1369        }
1370        boolean oldValue = useFileHiding;
1371        useFileHiding = b;
1372        firePropertyChange(FILE_HIDING_CHANGED_PROPERTY, oldValue, useFileHiding);
1373    }
1374
1375    /**
1376     * Sets the current file filter. The file filter is used by the
1377     * file chooser to filter out files from the user's view.
1378     *
1379     * @beaninfo
1380     * preferred: true
1381     * bound: true
1382     * description: Sets the File Filter used to filter out files of type.
1383     *
1384     * @param filter the new current file filter to use
1385     * @see #getFileFilter
1386     */

1387    public void setFileFilter(FileFilter filter) {
1388    FileFilter oldValue = fileFilter;
1389    fileFilter = filter;
1390    if (filter != null) {
1391        if (isMultiSelectionEnabled() && selectedFiles != null && selectedFiles.length > 0) {
1392        Vector JavaDoc fList = new Vector JavaDoc();
1393        boolean failed = false;
1394        for (int i = 0; i < selectedFiles.length; i++) {
1395            if (filter.accept(selectedFiles[i])) {
1396            fList.add(selectedFiles[i]);
1397            } else {
1398            failed = true;
1399            }
1400        }
1401        if (failed) {
1402            setSelectedFiles((fList.size() == 0) ? null : (File JavaDoc[])fList.toArray(new File JavaDoc[fList.size()]));
1403        }
1404        } else if (selectedFile != null && !filter.accept(selectedFile)) {
1405        setSelectedFile(null);
1406        }
1407    }
1408    firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, oldValue, fileFilter);
1409    }
1410    
1411
1412    /**
1413     * Returns the currently selected file filter.
1414     *
1415     * @return the current file filter
1416     * @see #setFileFilter
1417     * @see #addChoosableFileFilter
1418     */

1419    public FileFilter getFileFilter() {
1420    return fileFilter;
1421    }
1422
1423    /**
1424     * Sets the file view to used to retrieve UI information, such as
1425     * the icon that represents a file or the type description of a file.
1426     *
1427     * @beaninfo
1428     * preferred: true
1429     * bound: true
1430     * description: Sets the File View used to get file type information.
1431     *
1432     * @see #getFileView
1433     */

1434    public void setFileView(FileView fileView) {
1435    FileView oldValue = this.fileView;
1436    this.fileView = fileView;
1437    firePropertyChange(FILE_VIEW_CHANGED_PROPERTY, oldValue, fileView);
1438    }
1439
1440    /**
1441     * Returns the current file view.
1442     *
1443     * @see #setFileView
1444     */

1445    public FileView getFileView() {
1446    return fileView;
1447    }
1448    
1449    // ******************************
1450
// *****FileView delegation *****
1451
// ******************************
1452

1453    // NOTE: all of the following methods attempt to delegate
1454
// first to the client set fileView, and if <code>null</code> is returned
1455
// (or there is now client defined fileView) then calls the
1456
// UI's default fileView.
1457

1458    /**
1459     * Returns the filename.
1460     * @param f the <code>File</code>
1461     * @return the <code>String</code> containing the filename for
1462     * <code>f</code>
1463     * @see FileView#getName
1464     */

1465    public String JavaDoc getName(File JavaDoc f) {
1466    String JavaDoc filename = null;
1467    if(f != null) {
1468        if(getFileView() != null) {
1469        filename = getFileView().getName(f);
1470        }
1471        if(filename == null && uiFileView != null) {
1472        filename = uiFileView.getName(f);
1473        }
1474        }
1475    return filename;
1476    }
1477
1478    /**
1479     * Returns the file description.
1480     * @param f the <code>File</code>
1481     * @return the <code>String</code> containing the file description for
1482     * <code>f</code>
1483     * @see FileView#getDescription
1484     */

1485    public String JavaDoc getDescription(File JavaDoc f) {
1486    String JavaDoc description = null;
1487    if(f != null) {
1488        if(getFileView() != null) {
1489        description = getFileView().getDescription(f);
1490        }
1491        if(description == null && uiFileView != null) {
1492        description = uiFileView.getDescription(f);
1493        }
1494        }
1495    return description;
1496    }
1497
1498    /**
1499     * Returns the file type.
1500     * @param f the <code>File</code>
1501     * @return the <code>String</code> containing the file type description for
1502     * <code>f</code>
1503     * @see FileView#getTypeDescription
1504     */

1505    public String JavaDoc getTypeDescription(File JavaDoc f) {
1506    String JavaDoc typeDescription = null;
1507    if(f != null) {
1508        if(getFileView() != null) {
1509        typeDescription = getFileView().getTypeDescription(f);
1510        }
1511        if(typeDescription == null && uiFileView != null) {
1512        typeDescription = uiFileView.getTypeDescription(f);
1513        }
1514        }
1515    return typeDescription;
1516    }
1517
1518    /**
1519     * Returns the icon for this file or type of file, depending
1520     * on the system.
1521     * @param f the <code>File</code>
1522     * @return the <code>Icon</code> for this file, or type of file
1523     * @see FileView#getIcon
1524     */

1525    public Icon JavaDoc getIcon(File JavaDoc f) {
1526    Icon JavaDoc icon = null;
1527    if (f != null) {
1528        if(getFileView() != null) {
1529        icon = getFileView().getIcon(f);
1530        }
1531        if(icon == null && uiFileView != null) {
1532        icon = uiFileView.getIcon(f);
1533        }
1534    }
1535    return icon;
1536    }
1537
1538    /**
1539     * Returns true if the file (directory) can be visited.
1540     * Returns false if the directory cannot be traversed.
1541     * @param f the <code>File</code>
1542     * @return true if the file/directory can be traversed, otherwise false
1543     * @see FileView#isTraversable
1544     */

1545    public boolean isTraversable(File JavaDoc f) {
1546    Boolean JavaDoc traversable = null;
1547    if (f != null) {
1548        if (getFileView() != null) {
1549        traversable = getFileView().isTraversable(f);
1550        }
1551        if (traversable == null && uiFileView != null) {
1552        traversable = uiFileView.isTraversable(f);
1553        }
1554        if (traversable == null) {
1555        traversable = getFileSystemView().isTraversable(f);
1556        }
1557    }
1558    return (traversable != null && traversable.booleanValue());
1559    }
1560
1561    /**
1562     * Returns true if the file should be displayed.
1563     * @param f the <code>File</code>
1564     * @return true if the file should be displayed, otherwise false
1565     * @see FileFilter#accept
1566     */

1567    public boolean accept(File JavaDoc f) {
1568    boolean shown = true;
1569    if(f != null && fileFilter != null) {
1570        shown = fileFilter.accept(f);
1571    }
1572    return shown;
1573    }
1574
1575    /**
1576     * Sets the file system view that the <code>JFileChooser</code> uses for
1577     * accessing and creating file system resources, such as finding
1578     * the floppy drive and getting a list of root drives.
1579     * @param fsv the new <code>FileSystemView</code>
1580     *
1581     * @beaninfo
1582     * expert: true
1583     * bound: true
1584     * description: Sets the FileSytemView used to get filesystem information.
1585     *
1586     * @see FileSystemView
1587     */

1588    public void setFileSystemView(FileSystemView fsv) {
1589    FileSystemView oldValue = fileSystemView;
1590    fileSystemView = fsv;
1591    firePropertyChange(FILE_SYSTEM_VIEW_CHANGED_PROPERTY, oldValue, fileSystemView);
1592    }
1593
1594    /**
1595     * Returns the file system view.
1596     * @return the <code>FileSystemView</code> object
1597     * @see #setFileSystemView
1598     */

1599    public FileSystemView getFileSystemView() {
1600    return fileSystemView;
1601    }
1602
1603    // **************************
1604
// ***** Event Handling *****
1605
// **************************
1606

1607    /**
1608     * Called by the UI when the user hits the Approve button
1609     * (labeled "Open" or "Save", by default). This can also be
1610     * called by the programmer.
1611     * This method causes an action event to fire
1612     * with the command string equal to
1613     * <code>APPROVE_SELECTION</code>.
1614     *
1615     * @see #APPROVE_SELECTION
1616     */

1617    public void approveSelection() {
1618    returnValue = APPROVE_OPTION;
1619    if(dialog != null) {
1620        dialog.setVisible(false);
1621    }
1622    fireActionPerformed(APPROVE_SELECTION);
1623    }
1624
1625    /**
1626     * Called by the UI when the user chooses the Cancel button.
1627     * This can also be called by the programmer.
1628     * This method causes an action event to fire
1629     * with the command string equal to
1630     * <code>CANCEL_SELECTION</code>.
1631     *
1632     * @see #CANCEL_SELECTION
1633     */

1634    public void cancelSelection() {
1635    returnValue = CANCEL_OPTION;
1636    if(dialog != null) {
1637        dialog.setVisible(false);
1638    }
1639    fireActionPerformed(CANCEL_SELECTION);
1640    }
1641
1642    /**
1643     * Adds an <code>ActionListener</code> to the file chooser.
1644     *
1645     * @param l the listener to be added
1646     *
1647     * @see #approveSelection
1648     * @see #cancelSelection
1649     */

1650    public void addActionListener(ActionListener l) {
1651        listenerList.add(ActionListener.class, l);
1652    }
1653 
1654    /**
1655     * Removes an <code>ActionListener</code> from the file chooser.
1656     *
1657     * @param l the listener to be removed
1658     *
1659     * @see #addActionListener
1660     */

1661    public void removeActionListener(ActionListener l) {
1662        listenerList.remove(ActionListener.class, l);
1663    }
1664 
1665    /**
1666     * Returns an array of all the action listeners
1667     * registered on this file chooser.
1668     *
1669     * @return all of this file chooser's <code>ActionListener</code>s
1670     * or an empty
1671     * array if no action listeners are currently registered
1672     *
1673     * @see #addActionListener
1674     * @see #removeActionListener
1675     *
1676     * @since 1.4
1677     */

1678    public ActionListener[] getActionListeners() {
1679        return (ActionListener[])listenerList.getListeners(
1680                ActionListener.class);
1681    }
1682
1683    /**
1684     * Notifies all listeners that have registered interest for
1685     * notification on this event type. The event instance
1686     * is lazily created using the <code>command</code> parameter.
1687     *
1688     * @see EventListenerList
1689     */

1690    protected void fireActionPerformed(String JavaDoc command) {
1691        // Guaranteed to return a non-null array
1692
Object JavaDoc[] listeners = listenerList.getListenerList();
1693        long mostRecentEventTime = EventQueue.getMostRecentEventTime();
1694        int modifiers = 0;
1695        AWTEvent JavaDoc currentEvent = EventQueue.getCurrentEvent();
1696        if (currentEvent instanceof InputEvent) {
1697            modifiers = ((InputEvent)currentEvent).getModifiers();
1698        } else if (currentEvent instanceof ActionEvent) {
1699            modifiers = ((ActionEvent)currentEvent).getModifiers();
1700        }
1701        ActionEvent e = null;
1702        // Process the listeners last to first, notifying
1703
// those that are interested in this event
1704
for (int i = listeners.length-2; i>=0; i-=2) {
1705            if (listeners[i]==ActionListener.class) {
1706                // Lazily create the event:
1707
if (e == null) {
1708                    e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1709                                        command, mostRecentEventTime,
1710                                        modifiers);
1711                }
1712                ((ActionListener)listeners[i+1]).actionPerformed(e);
1713            }
1714        }
1715    }
1716
1717    private static class WeakPCL implements PropertyChangeListener JavaDoc {
1718        WeakReference JavaDoc<JFileChooser JavaDoc> jfcRef;
1719
1720        public WeakPCL(JFileChooser JavaDoc jfc) {
1721            jfcRef = new WeakReference JavaDoc(jfc);
1722        }
1723        public void propertyChange(PropertyChangeEvent JavaDoc ev) {
1724            assert ev.getPropertyName().equals(SHOW_HIDDEN_PROP);
1725            JFileChooser JavaDoc jfc = jfcRef.get();
1726            if (jfc == null) {
1727                // Our JFileChooser is no longer around, so we no longer need to
1728
// listen for PropertyChangeEvents.
1729
Toolkit.getDefaultToolkit().removePropertyChangeListener(SHOW_HIDDEN_PROP, this);
1730            }
1731            else {
1732                boolean oldValue = jfc.useFileHiding;
1733                jfc.useFileHiding = !((Boolean JavaDoc)ev.getNewValue()).booleanValue();
1734                jfc.firePropertyChange(FILE_HIDING_CHANGED_PROPERTY, oldValue, jfc.useFileHiding);
1735            }
1736        }
1737    }
1738
1739    // *********************************
1740
// ***** Pluggable L&F methods *****
1741
// *********************************
1742

1743    /**
1744     * Resets the UI property to a value from the current look and feel.
1745     *
1746     * @see JComponent#updateUI
1747     */

1748    public void updateUI() {
1749    if (isAcceptAllFileFilterUsed()) {
1750        removeChoosableFileFilter(getAcceptAllFileFilter());
1751    }
1752    FileChooserUI JavaDoc ui = ((FileChooserUI JavaDoc)UIManager.getUI(this));
1753    if (fileSystemView == null) {
1754        // We were probably deserialized
1755
setFileSystemView(FileSystemView.getFileSystemView());
1756    }
1757    setUI(ui);
1758
1759    uiFileView = getUI().getFileView(this);
1760    if(isAcceptAllFileFilterUsed()) {
1761        addChoosableFileFilter(getAcceptAllFileFilter());
1762    }
1763    }
1764
1765    /**
1766     * Returns a string that specifies the name of the L&F class
1767     * that renders this component.
1768     *
1769     * @return the string "FileChooserUI"
1770     * @see JComponent#getUIClassID
1771     * @see UIDefaults#getUI
1772     * @beaninfo
1773     * expert: true
1774     * description: A string that specifies the name of the L&F class.
1775     */

1776    public String JavaDoc getUIClassID() {
1777        return uiClassID;
1778    }
1779
1780    /**
1781     * Gets the UI object which implements the L&F for this component.
1782     *
1783     * @return the FileChooserUI object that implements the FileChooserUI L&F
1784     */

1785    public FileChooserUI JavaDoc getUI() {
1786        return (FileChooserUI JavaDoc) ui;
1787    }
1788
1789    /**
1790     * See <code>readObject</code> and <code>writeObject</code> in
1791     * <code>JComponent</code> for more
1792     * information about serialization in Swing.
1793     */

1794    private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
1795    FileSystemView fsv = null;
1796
1797    if (isAcceptAllFileFilterUsed()) {
1798        //The AcceptAllFileFilter is UI specific, it will be reset by
1799
//updateUI() after deserialization
1800
removeChoosableFileFilter(getAcceptAllFileFilter());
1801    }
1802    if (fileSystemView.equals(FileSystemView.getFileSystemView())) {
1803        //The default FileSystemView is platform specific, it will be
1804
//reset by updateUI() after deserialization
1805
fsv = fileSystemView;
1806        fileSystemView = null;
1807    }
1808        s.defaultWriteObject();
1809    if (fsv != null) {
1810        fileSystemView = fsv;
1811    }
1812    if (isAcceptAllFileFilterUsed()) {
1813        addChoosableFileFilter(getAcceptAllFileFilter());
1814    }
1815        if (getUIClassID().equals(uiClassID)) {
1816            byte count = JComponent.getWriteObjCounter(this);
1817            JComponent.setWriteObjCounter(this, --count);
1818            if (count == 0 && ui != null) {
1819                ui.installUI(this);
1820            }
1821        }
1822    }
1823
1824
1825    /**
1826     * Returns a string representation of this <code>JFileChooser</code>.
1827     * This method
1828     * is intended to be used only for debugging purposes, and the
1829     * content and format of the returned string may vary between
1830     * implementations. The returned string may be empty but may not
1831     * be <code>null</code>.
1832     *
1833     * @return a string representation of this <code>JFileChooser</code>
1834     */

1835    protected String JavaDoc paramString() {
1836        String JavaDoc approveButtonTextString = (approveButtonText != null ?
1837                      approveButtonText: "");
1838        String JavaDoc dialogTitleString = (dialogTitle != null ?
1839                    dialogTitle: "");
1840        String JavaDoc dialogTypeString;
1841        if (dialogType == OPEN_DIALOG) {
1842            dialogTypeString = "OPEN_DIALOG";
1843        } else if (dialogType == SAVE_DIALOG) {
1844            dialogTypeString = "SAVE_DIALOG";
1845        } else if (dialogType == CUSTOM_DIALOG) {
1846            dialogTypeString = "CUSTOM_DIALOG";
1847        } else dialogTypeString = "";
1848        String JavaDoc returnValueString;
1849        if (returnValue == CANCEL_OPTION) {
1850            returnValueString = "CANCEL_OPTION";
1851        } else if (returnValue == APPROVE_OPTION) {
1852            returnValueString = "APPROVE_OPTION";
1853        } else if (returnValue == ERROR_OPTION) {
1854            returnValueString = "ERROR_OPTION";
1855        } else returnValueString = "";
1856        String JavaDoc useFileHidingString = (useFileHiding ?
1857                                    "true" : "false");
1858        String JavaDoc fileSelectionModeString;
1859        if (fileSelectionMode == FILES_ONLY) {
1860            fileSelectionModeString = "FILES_ONLY";
1861        } else if (fileSelectionMode == DIRECTORIES_ONLY) {
1862            fileSelectionModeString = "DIRECTORIES_ONLY";
1863        } else if (fileSelectionMode == FILES_AND_DIRECTORIES) {
1864            fileSelectionModeString = "FILES_AND_DIRECTORIES";
1865        } else fileSelectionModeString = "";
1866        String JavaDoc currentDirectoryString = (currentDirectory != null ?
1867                     currentDirectory.toString() : "");
1868        String JavaDoc selectedFileString = (selectedFile != null ?
1869                     selectedFile.toString() : "");
1870
1871        return super.paramString() +
1872        ",approveButtonText=" + approveButtonTextString +
1873        ",currentDirectory=" + currentDirectoryString +
1874        ",dialogTitle=" + dialogTitleString +
1875        ",dialogType=" + dialogTypeString +
1876        ",fileSelectionMode=" + fileSelectionModeString +
1877        ",returnValue=" + returnValueString +
1878        ",selectedFile=" + selectedFileString +
1879        ",useFileHiding=" + useFileHidingString;
1880    }
1881
1882/////////////////
1883
// Accessibility support
1884
////////////////
1885

1886    protected AccessibleContext accessibleContext = null;
1887
1888    /**
1889     * Gets the AccessibleContext associated with this JFileChooser.
1890     * For file choosers, the AccessibleContext takes the form of an
1891     * AccessibleJFileChooser.
1892     * A new AccessibleJFileChooser instance is created if necessary.
1893     *
1894     * @return an AccessibleJFileChooser that serves as the
1895     * AccessibleContext of this JFileChooser
1896     */

1897    public AccessibleContext getAccessibleContext() {
1898        if (accessibleContext == null) {
1899            accessibleContext = new AccessibleJFileChooser();
1900        }
1901        return accessibleContext;
1902    }
1903
1904    /**
1905     * This class implements accessibility support for the
1906     * <code>JFileChooser</code> class. It provides an implementation of the
1907     * Java Accessibility API appropriate to file chooser user-interface
1908     * elements.
1909     */

1910    protected class AccessibleJFileChooser extends AccessibleJComponent {
1911
1912        /**
1913         * Gets the role of this object.
1914         *
1915         * @return an instance of AccessibleRole describing the role of the
1916         * object
1917         * @see AccessibleRole
1918         */

1919        public AccessibleRole getAccessibleRole() {
1920            return AccessibleRole.FILE_CHOOSER;
1921        }
1922
1923    } // inner class AccessibleJFileChooser
1924

1925}
1926
Popular Tags