Merge from mainline
[official-gcc.git] / libjava / classpath / javax / swing / JFileChooser.java
blob3a9d6a01f38acd782f2bd400bd1d43c692c66e2b
1 /* JFileChooser.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing;
40 import java.awt.Component;
41 import java.awt.Frame;
42 import java.awt.HeadlessException;
43 import java.awt.Insets;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.beans.PropertyChangeEvent;
47 import java.io.File;
48 import java.util.ArrayList;
50 import javax.accessibility.Accessible;
51 import javax.accessibility.AccessibleContext;
52 import javax.accessibility.AccessibleRole;
53 import javax.swing.JDialog;
54 import javax.swing.filechooser.FileFilter;
55 import javax.swing.filechooser.FileSystemView;
56 import javax.swing.filechooser.FileView;
57 import javax.swing.plaf.FileChooserUI;
60 /**
61 * A component that provides the user a dialog box to browse through a
62 * filesystem and choose one or more files or directories.
64 * A JFileChooser can be configured to filter the displayed file list
65 * by adding a {@link FileFilter} instance using
66 * {@link #addChoosableFileFilter(FileFilter)}. Additional components can
67 * be embedded in the file chooser using {@link #setAccessory(JComponent)}.
68 * The JFileChooser properties also provide mechanisms to customize the
69 * behaviour of the file chooser.
71 * @author Kim Ho (kho@luxsci.net)
73 public class JFileChooser extends JComponent implements Accessible
75 private static final long serialVersionUID = 3162921138695327837L;
77 /**
78 * A dialog type for selecting a file to open.
79 * @see #setDialogType(int)
81 public static final int OPEN_DIALOG = 0;
83 /**
84 * A dialog type for selecting a file to save.
85 * @see #setDialogType(int)
87 public static final int SAVE_DIALOG = 1;
89 /**
90 * A dialog type for some custom purpose.
91 * @see #setDialogType(int)
93 public static final int CUSTOM_DIALOG = 2;
95 /**
96 * A return value indicating the file chooser has been closed by cancelling.
98 * @see #showOpenDialog(Component)
99 * @see #showSaveDialog(Component)
101 public static final int CANCEL_OPTION = 1;
103 /**
104 * A return value indicating the file chooser has been closed by approving
105 * the selection.
106 * @see #showOpenDialog(Component)
107 * @see #showSaveDialog(Component)
109 public static final int APPROVE_OPTION = 0;
111 /**
112 * A return value indicating the file chooser has been closed by some error.
113 * @see #showOpenDialog(Component)
114 * @see #showSaveDialog(Component)
116 public static final int ERROR_OPTION = -1;
118 /**
119 * A selection mode constant indicating acceptance of files only.
120 * @see #setFileSelectionMode(int)
122 public static final int FILES_ONLY = 0;
124 /**
125 * A selection mode constant indicating acceptance of directories only.
126 * @see #setFileSelectionMode(int)
128 public static final int DIRECTORIES_ONLY = 1;
130 /**
131 * A selection mode constant indicating acceptance of files and directories.
132 * @see #setFileSelectionMode(int)
134 public static final int FILES_AND_DIRECTORIES = 2;
136 /**
137 * Action command string for cancelling the current selection.
138 * @see #cancelSelection()
140 public static final String CANCEL_SELECTION = "CancelSelection";
142 /**
143 * Action command string for approving the current selection.
144 * @see #cancelSelection()
146 public static final String APPROVE_SELECTION = "ApproveSelection";
149 * The name of the property for the approve button text.
150 * @see #setApproveButtonText(String)
152 public static final String APPROVE_BUTTON_TEXT_CHANGED_PROPERTY =
153 "ApproveButtonTextChangedProperty";
156 * The name of the property for the approve button tool tip text.
157 * @see #setApproveButtonToolTipText(String)
159 public static final String APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY =
160 "ApproveButtonToolTipTextChangedProperty";
163 * The name of the property for the approve button mnemonic.
164 * @see #setApproveButtonMnemonic(int)
166 public static final String APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY =
167 "ApproveButtonMnemonicChangedProperty";
170 * The name of the property for control button visibility.
171 * @see #setControlButtonsAreShown(boolean)
173 public static final String CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY =
174 "ControlButtonsAreShownChangedProperty";
177 * The name of the property for the current directory.
178 * @see #setCurrentDirectory(File)
180 public static final String DIRECTORY_CHANGED_PROPERTY = "directoryChanged";
183 * The name of the property for the selected file.
184 * @see #setSelectedFile(File)
186 public static final String SELECTED_FILE_CHANGED_PROPERTY =
187 "SelectedFileChangedProperty";
190 * The name of the property for the selected files.
191 * @see #setSelectedFiles(File[])
193 public static final String SELECTED_FILES_CHANGED_PROPERTY =
194 "SelectedFilesChangedProperty";
196 /**
197 * The name of the property for multi-selection.
198 * @see #setMultiSelectionEnabled(boolean)
200 public static final String MULTI_SELECTION_ENABLED_CHANGED_PROPERTY =
201 "MultiSelectionEnabledChangedProperty";
204 * The name of the 'file system view' property.
205 * @see #setFileSystemView(FileSystemView)
207 public static final String FILE_SYSTEM_VIEW_CHANGED_PROPERTY =
208 "FileSystemViewChanged";
211 * The name of the 'file view' property.
212 * @see #setFileView(FileView)
214 public static final String FILE_VIEW_CHANGED_PROPERTY = "fileViewChanged";
217 * The name of the 'file hiding enabled' property.
218 * @see #setFileHidingEnabled(boolean)
220 public static final String FILE_HIDING_CHANGED_PROPERTY =
221 "FileHidingChanged";
224 * The name of the 'file filter' property.
225 * @see #setFileFilter(FileFilter)
227 public static final String FILE_FILTER_CHANGED_PROPERTY =
228 "fileFilterChanged";
231 * The name of the 'file selection mode' property.
232 * @see #setFileSelectionMode(int)
234 public static final String FILE_SELECTION_MODE_CHANGED_PROPERTY =
235 "fileSelectionChanged";
238 * The name of the 'accessory' property.
239 * @see #setAccessory(JComponent)
241 public static final String ACCESSORY_CHANGED_PROPERTY =
242 "AccessoryChangedProperty";
245 * The name of the 'accept all file filter used' property.
246 * @see #setAcceptAllFileFilterUsed(boolean)
248 public static final String ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY =
249 "acceptAllFileFilterUsedChanged";
252 * The name of the 'dialog title' property.
253 * @see #setDialogTitle(String)
255 public static final String DIALOG_TITLE_CHANGED_PROPERTY =
256 "DialogTitleChangedProperty";
259 * The name of the 'dialog type' property.
260 * @see #setDialogType(int)
262 public static final String DIALOG_TYPE_CHANGED_PROPERTY =
263 "DialogTypeChangedProperty";
266 * The name of the 'choosable file filters' property.
267 * @see #addChoosableFileFilter(FileFilter)
269 public static final String CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY =
270 "ChoosableFileFilterChangedProperty";
272 /**
273 * The accessible context.
274 * @see #getAccessibleContext()
276 protected AccessibleContext accessibleContext;
278 /**
279 * The file system view.
280 * @see #setFileSystemView(FileSystemView)
282 private FileSystemView fsv;
285 * The accessory component.
286 * @see #setAccessory(JComponent)
288 private JComponent accessory;
291 * The approve button mnemonic.
292 * @see #setApproveButtonMnemonic(int)
294 private int approveButtonMnemonic = 0;
297 * The approve button text.
298 * @see #setApproveButtonText(String)
300 private String approveButtonText;
303 * The approve button tool tip text.
304 * @see #setApproveButtonToolTipText(String)
306 private String approveButtonToolTipText;
309 * The choosable file filters.
310 * @see #addChoosableFileFilter(FileFilter)
312 private ArrayList choosableFilters = new ArrayList();
315 * A flag controlling whether the accept all file filter is used.
316 * @see #setAcceptAllFileFilterUsed(boolean)
318 private boolean isAcceptAll = true;
321 * The dialog title.
322 * @see #setDialogTitle(String)
324 private String dialogTitle;
327 * The dialog type.
328 * @see #setDialogType(int)
330 private int dialogType = OPEN_DIALOG;
333 * The return value for the dialog.
334 * @see #showOpenDialog(Component)
335 * @see #showSaveDialog(Component)
337 private int retval = ERROR_OPTION;
340 * A flag indicating whether the file chooser allows multiple selection.
341 * @see #isMultiSelectionEnabled()
343 private boolean multiSelection = false;
346 * A flag indicating whether file hiding is enabled.
347 * @see #isFileHidingEnabled()
349 private boolean fileHiding = true;
352 * The file selection mode.
353 * @see #setFileSelectionMode(int)
355 private int fileSelectionMode = FILES_AND_DIRECTORIES;
357 /**
358 * The file view.
359 * @see #setFileView(FileView)
361 private FileView fv = null;
363 /**
364 * A flag controlling whether or not the control buttons are visible.
365 * @see #setControlButtonsAreShown(boolean)
367 private boolean controlButtonsShown = true;
369 /**
370 * The current directory.
371 * @see #setCurrentDirectory(File)
373 private File currentDir = null;
375 /**
376 * The current file filter.
377 * @see #setFileFilter(FileFilter)
379 private FileFilter currentFilter = null;
381 /**
382 * An array of selected files.
383 * @see #setSelectedFiles(File[])
385 private File[] selectedFiles;
387 /**
388 * The selected file.
389 * @see #setSelectedFile(File)
391 private File selectedFile;
394 * Creates a new <code>JFileChooser</code> object.
396 public JFileChooser()
398 setup(null);
399 setCurrentDirectory(null);
403 * Creates a new <code>JFileChooser</code> object.
405 * @param currentDirectoryPath the directory that should initially be
406 * shown in the filechooser (if <code>null</code>, the user's home
407 * directory is used).
409 public JFileChooser(String currentDirectoryPath)
411 this(currentDirectoryPath, null);
415 * Creates a new <code>JFileChooser</code> object with the specified
416 * directory and {@link FileSystemView}.
418 * @param currentDirectoryPath the directory that should initially be
419 * shown in the filechooser (if <code>null</code>, the user's home
420 * directory is used).
421 * @param fsv the file system view (if <code>null</code>, the default file
422 * system view is used).
424 public JFileChooser(String currentDirectoryPath, FileSystemView fsv)
426 setup(fsv);
427 File dir = null;
428 if (currentDirectoryPath != null)
429 dir = getFileSystemView().createFileObject(currentDirectoryPath);
430 setCurrentDirectory(dir);
434 * Creates a new <code>JFileChooser</code> object.
436 * @param currentDirectory the directory that should initially be
437 * shown in the filechooser (if <code>null</code>, the user's home
438 * directory is used).
440 public JFileChooser(File currentDirectory)
442 setup(null);
443 setCurrentDirectory(currentDirectory);
447 * Creates a new <code>JFileChooser</code> object.
449 * @param fsv the file system view (if <code>null</code>, the default file
450 * system view is used).
452 public JFileChooser(FileSystemView fsv)
454 setup(fsv);
455 setCurrentDirectory(null);
459 * Creates a new <code>JFileChooser</code> object.
461 * @param currentDirectory the directory that should initially be
462 * shown in the filechooser (if <code>null</code>, the user's home
463 * directory is used).
464 * @param fsv the file system view (if <code>null</code>, the default file
465 * system view is used).
467 public JFileChooser(File currentDirectory, FileSystemView fsv)
469 setup(fsv);
470 setCurrentDirectory(currentDirectory);
474 * Sets up the file chooser. This method is called by all the constructors.
476 * @param view the file system view (if <code>null</code>, the default file
477 * system view is used).
479 * @see FileSystemView#getFileSystemView()
481 protected void setup(FileSystemView view)
483 if (view == null)
484 view = FileSystemView.getFileSystemView();
485 setFileSystemView(view);
486 updateUI();
490 * DOCUMENT ME!
492 * @param b DOCUMENT ME!
494 public void setDragEnabled(boolean b)
496 // FIXME: Implement
500 * DOCUMENT ME!
502 * @return DOCUMENT ME!
504 public boolean getDragEnabled()
506 // FIXME: Implement
507 return false;
511 * Returns the selected file, if there is one.
513 * @return The selected file (possibly <code>null</code>).
515 * @see #setSelectedFile(File)
517 public File getSelectedFile()
519 return selectedFile;
523 * Sets the selected file and sends a {@link PropertyChangeEvent} to all
524 * registered listeners. The property name is
525 * {@link #SELECTED_FILE_CHANGED_PROPERTY}.
527 * @param file the file (<code>null</code> permitted).
529 public void setSelectedFile(File file)
531 if (selectedFile == null || !selectedFile.equals(file))
533 File old = selectedFile;
534 selectedFile = file;
535 firePropertyChange(SELECTED_FILE_CHANGED_PROPERTY, old, selectedFile);
540 * Returns the selected file or files in an array. If no files are selected,
541 * an empty array is returned.
543 * @return An array of the selected files (possibly empty).
545 public File[] getSelectedFiles()
547 if (selectedFiles != null)
548 return selectedFiles;
549 if (selectedFile != null)
550 return new File[] { selectedFile };
551 return new File[0];
555 * Sets the selected files and sends a {@link PropertyChangeEvent} (with the
556 * name {@link #SELECTED_FILES_CHANGED_PROPERTY}) to all registered
557 * listeners.
559 * @param selectedFiles the selected files (<code>null</code> permitted).
561 public void setSelectedFiles(File[] selectedFiles)
563 if (selectedFiles == null)
564 selectedFiles = new File[0];
565 if (selectedFiles.length > 0)
566 setSelectedFile(selectedFiles[0]);
567 else
568 setSelectedFile(null);
569 if (this.selectedFiles != selectedFiles)
571 File[] old = this.selectedFiles;
572 this.selectedFiles = selectedFiles;
573 firePropertyChange(SELECTED_FILES_CHANGED_PROPERTY, old, selectedFiles);
579 * Returns the current directory.
581 * @return The current directory.
583 public File getCurrentDirectory()
585 return currentDir;
589 * Sets the current directory and fires a {@link PropertyChangeEvent} (with
590 * the property name {@link #DIRECTORY_CHANGED_PROPERTY}) to all registered
591 * listeners. If <code>dir</code> is <code>null</code>, the current
592 * directory is set to the default directory returned by the file system
593 * view.
595 * @param dir the new directory (<code>null</code> permitted).
597 * @see FileSystemView#getDefaultDirectory()
599 public void setCurrentDirectory(File dir)
601 if (currentDir != dir || dir == null)
603 if (dir == null)
604 dir = fsv.getDefaultDirectory();
606 File old = currentDir;
607 currentDir = dir;
608 firePropertyChange(DIRECTORY_CHANGED_PROPERTY, old, currentDir);
613 * Called by the UI delegate when the parent directory is changed.
615 public void changeToParentDirectory()
617 setCurrentDirectory(fsv.getParentDirectory(currentDir));
621 * Rescans the current directory (this is handled by the UI delegate).
623 public void rescanCurrentDirectory()
625 getUI().rescanCurrentDirectory(this);
629 * Ensures the the specified file is visible (this is handled by the
630 * UI delegate).
632 * @param f the file.
634 public void ensureFileIsVisible(File f)
636 getUI().ensureFileIsVisible(this, f);
640 * Displays the file chooser in a modal dialog using the
641 * {@link #OPEN_DIALOG} type.
643 * @param parent the parent component.
645 * @return A return value indicating how the dialog was closed (one of
646 * {@link #APPROVE_OPTION}, {@link #CANCEL_OPTION} and
647 * {@link #ERROR_OPTION}).
649 * @throws HeadlessException DOCUMENT ME!
651 public int showOpenDialog(Component parent) throws HeadlessException
653 JDialog d = createDialog(parent);
655 // FIXME: Remove when we get ancestor property
656 d.setTitle("Open");
657 setDialogType(OPEN_DIALOG);
659 retval = ERROR_OPTION;
661 Insets i = d.getInsets();
662 d.setSize(500 + i.top + i.bottom, d.getPreferredSize().height);
663 d.show();
664 return retval;
668 * Displays the file chooser in a modal dialog using the
669 * {@link #SAVE_DIALOG} type.
671 * @param parent the parent component.
673 * @return A return value indicating how the dialog was closed (one of
674 * {@link #APPROVE_OPTION}, {@link #CANCEL_OPTION} and
675 * {@link #ERROR_OPTION}).
677 * @throws HeadlessException DOCUMENT ME!
679 public int showSaveDialog(Component parent) throws HeadlessException
681 JDialog d = createDialog(parent);
682 setDialogType(SAVE_DIALOG);
684 retval = ERROR_OPTION;
686 Insets i = d.getInsets();
687 d.setSize(500 + i.top + i.bottom, d.getPreferredSize().height);
688 d.show();
689 return retval;
693 * Displays the file chooser in a modal dialog using the
694 * {@link #CUSTOM_DIALOG} type.
696 * @param parent the parent component.
698 * @return A return value indicating how the dialog was closed (one of
699 * {@link #APPROVE_OPTION}, {@link #CANCEL_OPTION} and
700 * {@link #ERROR_OPTION}).
702 * @throws HeadlessException DOCUMENT ME!
704 public int showDialog(Component parent, String approveButtonText)
705 throws HeadlessException
707 JDialog d = createDialog(parent);
708 setApproveButtonText(approveButtonText);
709 setDialogType(CUSTOM_DIALOG);
711 retval = ERROR_OPTION;
713 Insets i = d.getInsets();
714 d.setSize(500 + i.top + i.bottom, d.getPreferredSize().height);
715 d.show();
716 return retval;
720 * Creates a modal dialog in which to display the file chooser.
722 * @param parent the parent component.
724 * @return The dialog.
726 * @throws HeadlessException DOCUMENT ME!
728 protected JDialog createDialog(Component parent) throws HeadlessException
730 Frame toUse = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
731 if (toUse == null)
732 toUse = SwingUtilities.getOwnerFrame();
734 JDialog dialog = new JDialog(toUse);
735 setSelectedFile(null);
736 dialog.getContentPane().add(this);
737 dialog.setModal(true);
738 dialog.invalidate();
739 dialog.repaint();
741 return dialog;
745 * Returns the flag that controls whether or not the control buttons are
746 * shown on the file chooser.
748 * @return A boolean.
750 * @see #setControlButtonsAreShown(boolean)
752 public boolean getControlButtonsAreShown()
754 return controlButtonsShown;
758 * Sets the flag that controls whether or not the control buttons are
759 * shown and, if it changes, sends a {@link PropertyChangeEvent} (with the
760 * property name {@link #CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY}) to
761 * all registered listeners.
763 * @param b the new value for the flag.
765 public void setControlButtonsAreShown(boolean b)
767 if (controlButtonsShown != b)
769 controlButtonsShown = b;
770 firePropertyChange(CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY,
771 ! controlButtonsShown, controlButtonsShown);
776 * Returns the type of file chooser.
778 * @return {@link #OPEN_DIALOG}, {@link #SAVE_DIALOG} or
779 * {@link #CUSTOM_DIALOG}.
781 * @see #setDialogType(int)
783 public int getDialogType()
785 return dialogType;
789 * Sets the dialog type and fires a {@link PropertyChangeEvent} (with the
790 * property name {@link #DIALOG_TYPE_CHANGED_PROPERTY}) to all
791 * registered listeners.
793 * @param dialogType the dialog type (one of: {@link #OPEN_DIALOG},
794 * {@link #SAVE_DIALOG}, {@link #CUSTOM_DIALOG}).
796 * @throws IllegalArgumentException if <code>dialogType</code> is not valid.
798 public void setDialogType(int dialogType)
800 if (dialogType != OPEN_DIALOG && dialogType != SAVE_DIALOG
801 && dialogType != CUSTOM_DIALOG)
802 throw new IllegalArgumentException("Choose allowable dialogType.");
804 if (this.dialogType != dialogType)
806 int old = this.dialogType;
807 this.dialogType = dialogType;
808 firePropertyChange(DIALOG_TYPE_CHANGED_PROPERTY, old, this.dialogType);
813 * Sets the dialog title and sends a {@link PropertyChangeEvent} (with the
814 * property name {@link #DIALOG_TITLE_CHANGED_PROPERTY}) to all
815 * registered listeners.
817 * @param dialogTitle the dialog title (<code>null</code> permitted).
819 * @see #getDialogTitle()
821 public void setDialogTitle(String dialogTitle)
823 if (this.dialogTitle != dialogTitle)
825 String old = this.dialogTitle;
826 this.dialogTitle = dialogTitle;
827 firePropertyChange(DIALOG_TITLE_CHANGED_PROPERTY, old, this.dialogTitle);
832 * Returns the dialog title.
834 * @return The dialog title (possibly <code>null</code>).
836 * @see #setDialogTitle(String)
838 public String getDialogTitle()
840 return dialogTitle;
844 * Sets the tool tip text for the approve button and sends a
845 * {@link PropertyChangeEvent} (with the property name
846 * {@link #APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY}) to all
847 * registered listeners.
849 * @param toolTipText the text.
851 public void setApproveButtonToolTipText(String toolTipText)
853 if (approveButtonToolTipText != toolTipText)
855 String oldText = approveButtonToolTipText;
856 approveButtonToolTipText = toolTipText;
857 firePropertyChange(APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY,
858 oldText, approveButtonToolTipText);
863 * Returns the tool tip text for the approve button.
865 * @return The tool tip text for the approve button.
867 * @see #setApproveButtonToolTipText(String)
869 public String getApproveButtonToolTipText()
871 return approveButtonToolTipText;
875 * Returns the approve button mnemonic, or zero if no mnemonic has been set.
877 * @return The approve button mnemonic.
879 * @see #setApproveButtonMnemonic(int)
881 public int getApproveButtonMnemonic()
883 return approveButtonMnemonic;
887 * Sets the mnemonic for the approve button and sends a
888 * {@link PropertyChangeEvent} (with the property name
889 * {@link #APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY}) to all registered
890 * listeners.
892 * @param mnemonic the mnemonic.
894 * @see #setApproveButtonMnemonic(char)
896 public void setApproveButtonMnemonic(int mnemonic)
898 if (approveButtonMnemonic != mnemonic)
900 int oldMnemonic = approveButtonMnemonic;
901 approveButtonMnemonic = mnemonic;
902 firePropertyChange(APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY,
903 oldMnemonic, approveButtonMnemonic);
908 * Sets the mnemonic for the approve button and sends a
909 * {@link PropertyChangeEvent} (with the property name
910 * {@link #APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY}) to all registered
911 * listeners.
913 * @param mnemonic the mnemonic.
915 * @see #setApproveButtonMnemonic(int)
917 public void setApproveButtonMnemonic(char mnemonic)
919 setApproveButtonMnemonic((int) Character.toUpperCase(mnemonic));
923 * Sets the approve button text and fires a {@link PropertyChangeEvent}
924 * (with the property name {@link #APPROVE_BUTTON_TEXT_CHANGED_PROPERTY}) to
925 * all registered listeners.
927 * @param approveButtonText the text (<code>null</code> permitted).
929 * @see #getApproveButtonText()
931 public void setApproveButtonText(String approveButtonText)
933 if (this.approveButtonText != approveButtonText)
935 String oldText = this.approveButtonText;
936 this.approveButtonText = approveButtonText;
937 firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, oldText,
938 this.approveButtonText);
943 * Returns the approve button text.
945 * @return The approve button text (possibly <code>null</code>).
947 * @see #setApproveButtonText(String)
949 public String getApproveButtonText()
951 return approveButtonText;
955 * Returns the available file filters for this file chooser.
957 * @return The available file filters.
959 public FileFilter[] getChoosableFileFilters()
961 return (FileFilter[]) choosableFilters.toArray(new FileFilter[choosableFilters.size()]);
965 * Adds a file filter to the list of available filters and sends a
966 * {@link PropertyChangeEvent} (with the property name
967 * {@link #CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY}) to all registered
968 * listeners.
970 * @param filter the filter (<code>null</code> permitted).
972 public void addChoosableFileFilter(FileFilter filter)
974 if (filter != null)
976 FileFilter[] old = getChoosableFileFilters();
977 choosableFilters.add(filter);
978 FileFilter[] newFilters = getChoosableFileFilters();
979 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, old,
980 newFilters);
982 setFileFilter(filter);
986 * Removes a file filter from the list of available filters and sends a
987 * {@link PropertyChangeEvent} (with the property name
988 * {@link #CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY}) to all registered
989 * listeners.
991 * @param f the file filter.
993 * @return <code>true</code> if the filter was removed and
994 * <code>false</code> otherwise.
996 public boolean removeChoosableFileFilter(FileFilter f)
998 if (f == currentFilter)
999 setFileFilter(null);
1000 FileFilter[] old = getChoosableFileFilters();
1001 if (! choosableFilters.remove(f))
1002 return false;
1003 FileFilter[] newFilters = getChoosableFileFilters();
1004 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, old, newFilters);
1005 return true;
1009 * Clears the list of choosable file filters and installs the 'accept all'
1010 * filter from the UI delegate.
1012 public void resetChoosableFileFilters()
1014 choosableFilters.clear();
1015 choosableFilters.add(getUI().getAcceptAllFileFilter(this));
1016 setFileFilter((FileFilter) choosableFilters.get(0));
1020 * Returns the 'accept all' file filter from the UI delegate.
1022 * @return The 'accept all' file filter.
1024 public FileFilter getAcceptAllFileFilter()
1026 return getUI().getAcceptAllFileFilter(this);
1030 * Returns the flag that controls whether or not the 'accept all' file
1031 * filter is included in the list of filters.
1033 * @return A boolean.
1035 * @see #setAcceptAllFileFilterUsed(boolean)
1037 public boolean isAcceptAllFileFilterUsed()
1039 return isAcceptAll;
1043 * Sets the flag that controls whether or not the 'accept all' file filter
1044 * is included in the list of filters, and sends a
1045 * {@link PropertyChangeEvent} (with the property name
1046 * {@link #ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY}) to all registered
1047 * listeners.
1049 * @param b the new value of the flag.
1051 public void setAcceptAllFileFilterUsed(boolean b)
1053 if (isAcceptAll != b)
1055 isAcceptAll = b;
1056 if (b)
1057 addChoosableFileFilter(getAcceptAllFileFilter());
1058 else
1059 removeChoosableFileFilter(getAcceptAllFileFilter());
1060 firePropertyChange(ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY,
1061 ! isAcceptAll, isAcceptAll);
1066 * Returns the accessory component for the file chooser. The default
1067 * value is <code>null</code>.
1069 * @return The accessory component (possibly <code>null</code>).
1071 * @see #setAccessory(JComponent)
1073 public JComponent getAccessory()
1075 return accessory;
1079 * Sets the accessory component for the file chooser and sends a
1080 * {@link PropertyChangeEvent} to all registered listeners. The property
1081 * name is {@link #ACCESSORY_CHANGED_PROPERTY}.
1083 * @param newAccessory the accessory component.
1085 public void setAccessory(JComponent newAccessory)
1087 if (accessory != newAccessory)
1089 JComponent old = accessory;
1090 accessory = newAccessory;
1091 firePropertyChange(ACCESSORY_CHANGED_PROPERTY, old, accessory);
1096 * Sets the file selection mode and sends a {@link PropertyChangeEvent}
1097 * to all registered listeners. The property name is
1098 * {@link #FILE_SELECTION_MODE_CHANGED_PROPERTY}.
1100 * @param mode the mode ({@link #FILES_ONLY}, {@link #DIRECTORIES_ONLY} or
1101 * {@link #FILES_AND_DIRECTORIES}).
1103 * @throws IllegalArgumentException if the mode is invalid.
1105 public void setFileSelectionMode(int mode)
1107 if (mode != FILES_ONLY && mode != DIRECTORIES_ONLY
1108 && mode != FILES_AND_DIRECTORIES)
1109 throw new IllegalArgumentException("Choose a correct file selection mode.");
1110 if (fileSelectionMode != mode)
1112 int old = fileSelectionMode;
1113 fileSelectionMode = mode;
1114 firePropertyChange(FILE_SELECTION_MODE_CHANGED_PROPERTY, old,
1115 fileSelectionMode);
1120 * Returns the file selection mode, one of: {@link #FILES_ONLY},
1121 * {@link #DIRECTORIES_ONLY} or {@link #FILES_AND_DIRECTORIES}. The
1122 * default is {@link #FILES_ONLY}.
1124 * @return The file selection mode.
1126 * @see #setFileSelectionMode(int)
1128 public int getFileSelectionMode()
1130 return fileSelectionMode;
1134 * Returns <code>true</code> if file selection is enabled, and
1135 * <code>false</code> otherwise. File selection is enabled when the
1136 * file selection mode is {@link #FILES_ONLY} or
1137 * {@link #FILES_AND_DIRECTORIES}.
1139 * @return <code>true</code> if file selection is enabled.
1141 * @see #getFileSelectionMode()
1143 public boolean isFileSelectionEnabled()
1145 return (fileSelectionMode == FILES_ONLY
1146 || fileSelectionMode == FILES_AND_DIRECTORIES);
1150 * Returns <code>true</code> if directory selection is enabled, and
1151 * <code>false</code> otherwise. Directory selection is enabled when the
1152 * file selection mode is {@link #DIRECTORIES_ONLY} or
1153 * {@link #FILES_AND_DIRECTORIES}.
1155 * @return <code>true</code> if file selection is enabled.
1157 * @see #getFileSelectionMode()
1159 public boolean isDirectorySelectionEnabled()
1161 return (fileSelectionMode == DIRECTORIES_ONLY
1162 || fileSelectionMode == FILES_AND_DIRECTORIES);
1166 * Sets the flag that controls whether multiple selections are allowed in
1167 * this filechooser and sends a {@link PropertyChangeEvent} (with the
1168 * property name {@link #MULTI_SELECTION_ENABLED_CHANGED_PROPERTY}) to all
1169 * registered listeners.
1171 * @param b the new value of the flag.
1173 public void setMultiSelectionEnabled(boolean b)
1175 if (multiSelection != b)
1177 multiSelection = b;
1178 firePropertyChange(MULTI_SELECTION_ENABLED_CHANGED_PROPERTY,
1179 ! multiSelection, multiSelection);
1184 * Returns <code>true</code> if multiple selections are allowed within this
1185 * file chooser, and <code>false</code> otherwise.
1187 * @return A boolean.
1189 * @see #setMultiSelectionEnabled(boolean)
1191 public boolean isMultiSelectionEnabled()
1193 return multiSelection;
1197 * Returns <code>true</code> if hidden files are to be hidden, and
1198 * <code>false</code> otherwise.
1200 * @return A boolean.
1202 * @see #setFileHidingEnabled(boolean)
1204 public boolean isFileHidingEnabled()
1206 return fileHiding;
1210 * Sets the flag that controls whether or not hidden files are displayed,
1211 * and sends a {@link PropertyChangeEvent} (with the property name
1212 * {@link #FILE_HIDING_CHANGED_PROPERTY}) to all registered listeners.
1214 * @param b the new value of the flag.
1216 public void setFileHidingEnabled(boolean b)
1218 if (fileHiding != b)
1220 fileHiding = b;
1221 firePropertyChange(FILE_HIDING_CHANGED_PROPERTY, ! fileHiding,
1222 fileHiding);
1227 * Sets the file filter and sends a {@link PropertyChangeEvent} (with the
1228 * property name {@link #FILE_FILTER_CHANGED_PROPERTY}) to all registered
1229 * listeners.
1231 * @param filter the filter (<code>null</code> permitted).
1233 public void setFileFilter(FileFilter filter)
1235 if (currentFilter != filter)
1237 if (filter != null && !choosableFilters.contains(filter))
1238 addChoosableFileFilter(filter);
1239 FileFilter old = currentFilter;
1240 currentFilter = filter;
1241 firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, old, currentFilter);
1246 * Returns the file filter.
1248 * @return The file filter.
1250 * @see #setFileFilter(FileFilter)
1252 public FileFilter getFileFilter()
1254 return currentFilter;
1258 * Sets a custom {@link FileView} for the file chooser and sends a
1259 * {@link PropertyChangeEvent} to all registered listeners. The property
1260 * name is {@link #FILE_VIEW_CHANGED_PROPERTY}.
1262 * @param fileView the file view (<code>null</code> permitted).
1264 * @see #getFileView()
1266 public void setFileView(FileView fileView)
1268 if (fv != fileView)
1270 FileView old = fv;
1271 fv = fileView;
1272 firePropertyChange(FILE_VIEW_CHANGED_PROPERTY, old, fv);
1277 * Returns the custom {@link FileView} for the file chooser.
1279 * @return The file view (possibly <code>null</code>).
1281 public FileView getFileView()
1283 return fv;
1287 * Returns the name of the file, generated by the current (or default)
1288 * {@link FileView}.
1290 * @param f the file.
1292 * @return The file name.
1294 public String getName(File f)
1296 String name = null;
1297 if (fv != null)
1298 name = fv.getName(f);
1299 if (name == null)
1300 name = getUI().getFileView(this).getName(f);
1301 return name;
1305 * Returns the description of the file, generated by the current (or default)
1306 * {@link FileView}.
1308 * @param f the file.
1310 * @return The file description.
1312 public String getDescription(File f)
1314 String result = null;
1315 if (fv != null)
1316 result = fv.getDescription(f);
1317 if (result == null)
1318 result = getUI().getFileView(this).getDescription(f);
1319 return result;
1323 * Returns the type description for the file, generated by the current (or
1324 * default) {@link FileView}.
1326 * @param f the file.
1328 * @return The file type description.
1330 public String getTypeDescription(File f)
1332 String result = null;
1333 if (fv != null)
1334 result = getFileView().getTypeDescription(f);
1335 if (result == null)
1336 result = getUI().getFileView(this).getTypeDescription(f);
1337 return result;
1341 * Returns the icon provided by the current (or default) {@link FileView}.
1343 * @param f the file.
1345 * @return An icon representing the file.
1347 public Icon getIcon(File f)
1349 Icon result = null;
1350 if (fv != null)
1351 result = fv.getIcon(f);
1352 if (result == null)
1353 result = getUI().getFileView(this).getIcon(f);
1354 return result;
1358 * Returns <code>true</code> if the file is traversable, and
1359 * <code>false</code> otherwise.
1361 * @param f the file or directory.
1363 * @return A boolean.
1365 public boolean isTraversable(File f)
1367 return getFileSystemView().isTraversable(f).booleanValue();
1371 * Returns <code>true</code> if the file is accepted by the current
1372 * file filter.
1374 * @param f the file.
1376 * @return A boolean.
1378 public boolean accept(File f)
1380 if (f == null)
1381 return true;
1382 FileFilter ff = getFileFilter();
1383 if (ff != null)
1384 return ff.accept(f);
1385 else
1386 return true;
1390 * Sets the file system view for the file chooser and sends a
1391 * {@link PropertyChangeEvent} to all registered listeners.
1393 * @param fsv the file system view.
1395 public void setFileSystemView(FileSystemView fsv)
1397 if (this.fsv != fsv)
1399 FileSystemView old = this.fsv;
1400 this.fsv = fsv;
1401 firePropertyChange(FILE_SYSTEM_VIEW_CHANGED_PROPERTY, old, this.fsv);
1406 * Returns the file system view being used by this file chooser.
1408 * @return The file system view.
1410 * @see #setFileSystemView(FileSystemView)
1412 public FileSystemView getFileSystemView()
1414 return fsv;
1418 * Approves the selection. An {@link ActionEvent} is sent to all registered
1419 * listeners.
1421 public void approveSelection()
1423 retval = APPROVE_OPTION;
1424 fireActionPerformed(APPROVE_SELECTION);
1428 * Cancels the selection. An {@link ActionEvent} is sent to all registered
1429 * listeners.
1431 public void cancelSelection()
1433 retval = CANCEL_OPTION;
1434 fireActionPerformed(CANCEL_SELECTION);
1438 * Adds an {@link ActionListener} to the file chooser.
1440 * @param l the listener.
1442 public void addActionListener(ActionListener l)
1444 listenerList.add(ActionListener.class, l);
1448 * Removes an {@link ActionListener} from this file chooser.
1450 * @param l the listener.
1452 public void removeActionListener(ActionListener l)
1456 listenerList.remove(ActionListener.class, l);
1458 catch (IllegalArgumentException e)
1460 e.printStackTrace();
1465 * Returns the action listeners registered with this file chooser.
1467 * @return An array of listeners.
1469 public ActionListener[] getActionListeners()
1471 return (ActionListener[]) getListeners(ActionListener.class);
1475 * Sends an @link {ActionEvent} to all registered listeners.
1477 * @param command the action command.
1479 protected void fireActionPerformed(String command)
1481 ActionListener[] list = getActionListeners();
1482 ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1483 command);
1485 for (int i = 0; i < list.length; i++)
1486 list[i].actionPerformed(event);
1490 * Installs the UI delegate for the current look and feel.
1492 public void updateUI()
1494 setUI((FileChooserUI) UIManager.getUI(this));
1495 revalidate();
1499 * Returns the UI delegate class identifier.
1501 * @return <code>FileChooserUI</code>.
1503 public String getUIClassID()
1505 return "FileChooserUI";
1509 * Returns the UI delegate for the component.
1511 * @return The UI delegate.
1513 public FileChooserUI getUI()
1515 return (FileChooserUI) ui;
1519 * DOCUMENT ME!
1521 * @return DOCUMENT ME!
1523 protected String paramString()
1525 return "JFileChooser";
1529 * Returns the accessible context.
1531 * @return The accessible context.
1533 public AccessibleContext getAccessibleContext()
1535 return new AccessibleJFileChooser();
1539 * Accessibility support for JFileChooser
1541 protected class AccessibleJFileChooser
1542 extends JComponent.AccessibleJComponent
1544 protected AccessibleJFileChooser()
1546 // Nothing to do here.
1549 public AccessibleRole getAccessibleRole()
1551 return AccessibleRole.FILE_CHOOSER;