1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.swing;
27
28 import javax.swing.event.*;
29 import javax.swing.filechooser.*;
30 import javax.swing.plaf.FileChooserUI;
31
32 import javax.accessibility.*;
33
34 import java.io.File;
35 import java.io.ObjectOutputStream;
36 import java.io.IOException;
37
38 import java.util.Vector;
39 import java.awt.AWTEvent;
40 import java.awt.Component;
41 import java.awt.Container;
42 import java.awt.BorderLayout;
43 import java.awt.Window;
44 import java.awt.Dialog;
45 import java.awt.Frame;
46 import java.awt.GraphicsEnvironment;
47 import java.awt.HeadlessException;
48 import java.awt.EventQueue;
49 import java.awt.Toolkit;
50 import java.awt.event.*;
51 import java.beans.PropertyChangeListener;
52 import java.beans.PropertyChangeEvent;
53 import java.io.InvalidObjectException;
54 import java.io.ObjectInputStream;
55 import java.lang.ref.WeakReference;
56
57 /**
58 * <code>JFileChooser</code> provides a simple mechanism for the user to
59 * choose a file.
60 * For information about using <code>JFileChooser</code>, see
61 * <a
62 href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html">How to Use File Choosers</a>,
63 * a section in <em>The Java Tutorial</em>.
64 *
65 * <p>
66 *
67 * The following code pops up a file chooser for the user's home directory that
68 * sees only .jpg and .gif images:
69 * <pre>
70 * JFileChooser chooser = new JFileChooser();
71 * FileNameExtensionFilter filter = new FileNameExtensionFilter(
72 * "JPG & GIF Images", "jpg", "gif");
73 * chooser.setFileFilter(filter);
74 * int returnVal = chooser.showOpenDialog(parent);
75 * if(returnVal == JFileChooser.APPROVE_OPTION) {
76 * System.out.println("You chose to open this file: " +
77 * chooser.getSelectedFile().getName());
78 * }
79 * </pre>
80 * <p>
81 * <strong>Warning:</strong> Swing is not thread safe. For more
82 * information see <a
83 * href="package-summary.html#threading">Swing's Threading
84 * Policy</a>.
85 *
86 * @beaninfo
87 * attribute: isContainer false
88 * description: A component which allows for the interactive selection of a file.
89 *
90 * @author Jeff Dinkins
91 * @since 1.2
92 */
93 @SuppressWarnings("serial") // Superclass is not serializable across versions
94 public class JFileChooser extends JComponent implements Accessible {
95
96 /**
97 * @see #getUIClassID
98 * @see #readObject
99 */
100 private static final String uiClassID = "FileChooserUI";
101
102 // ************************
103 // ***** Dialog Types *****
104 // ************************
105
106 /**
107 * Type value indicating that the <code>JFileChooser</code> supports an
108 * "Open" file operation.
109 */
110 public static final int OPEN_DIALOG = 0;
111
112 /**
439 * how selections behave.
440 *
441 * <p>
442 *
443 * Some look and feels might not support automatic drag and drop;
444 * they will ignore this property. You can work around such
445 * look and feels by modifying the component
446 * to directly call the <code>exportAsDrag</code> method of a
447 * <code>TransferHandler</code>.
448 *
449 * @param b the value to set the <code>dragEnabled</code> property to
450 * @exception HeadlessException if
451 * <code>b</code> is <code>true</code> and
452 * <code>GraphicsEnvironment.isHeadless()</code>
453 * returns <code>true</code>
454 * @see java.awt.GraphicsEnvironment#isHeadless
455 * @see #getDragEnabled
456 * @see #setTransferHandler
457 * @see TransferHandler
458 * @since 1.4
459 *
460 * @beaninfo
461 * description: determines whether automatic drag handling is enabled
462 * bound: false
463 */
464 public void setDragEnabled(boolean b) {
465 checkDragEnabled(b);
466 dragEnabled = b;
467 }
468
469 private static void checkDragEnabled(boolean b) {
470 if (b && GraphicsEnvironment.isHeadless()) {
471 throw new HeadlessException();
472 }
473 }
474
475 /**
476 * Gets the value of the <code>dragEnabled</code> property.
477 *
478 * @return the value of the <code>dragEnabled</code> property
479 * @see #setDragEnabled
480 * @since 1.4
481 */
482 public boolean getDragEnabled() {
483 return dragEnabled;
488 // *****************************
489
490 /**
491 * Returns the selected file. This can be set either by the
492 * programmer via <code>setSelectedFile</code> or by a user action, such as
493 * either typing the filename into the UI or selecting the
494 * file from a list in the UI.
495 *
496 * @see #setSelectedFile
497 * @return the selected file
498 */
499 public File getSelectedFile() {
500 return selectedFile;
501 }
502
503 /**
504 * Sets the selected file. If the file's parent directory is
505 * not the current directory, changes the current directory
506 * to be the file's parent directory.
507 *
508 * @beaninfo
509 * preferred: true
510 * bound: true
511 *
512 * @see #getSelectedFile
513 *
514 * @param file the selected file
515 */
516 public void setSelectedFile(File file) {
517 File oldValue = selectedFile;
518 selectedFile = file;
519 if(selectedFile != null) {
520 if (file.isAbsolute() && !getFileSystemView().isParent(getCurrentDirectory(), selectedFile)) {
521 setCurrentDirectory(selectedFile.getParentFile());
522 }
523 if (!isMultiSelectionEnabled() || selectedFiles == null || selectedFiles.length == 1) {
524 ensureFileIsVisible(selectedFile);
525 }
526 }
527 firePropertyChange(SELECTED_FILE_CHANGED_PROPERTY, oldValue, selectedFile);
528 }
529
530 /**
531 * Returns a list of selected files if the file chooser is
532 * set to allow multiple selection.
533 *
534 * @return an array of selected {@code File}s
535 */
536 public File[] getSelectedFiles() {
537 if(selectedFiles == null) {
538 return new File[0];
539 } else {
540 return selectedFiles.clone();
541 }
542 }
543
544 /**
545 * Sets the list of selected files if the file chooser is
546 * set to allow multiple selection.
547 *
548 * @param selectedFiles an array {@code File}s to be selected
549 * @beaninfo
550 * bound: true
551 * description: The list of selected files if the chooser is in multiple selection mode.
552 */
553 public void setSelectedFiles(File[] selectedFiles) {
554 File[] oldValue = this.selectedFiles;
555 if (selectedFiles == null || selectedFiles.length == 0) {
556 selectedFiles = null;
557 this.selectedFiles = null;
558 setSelectedFile(null);
559 } else {
560 this.selectedFiles = selectedFiles.clone();
561 setSelectedFile(this.selectedFiles[0]);
562 }
563 firePropertyChange(SELECTED_FILES_CHANGED_PROPERTY, oldValue, selectedFiles);
564 }
565
566 /**
567 * Returns the current directory.
568 *
569 * @return the current directory
570 * @see #setCurrentDirectory
571 */
572 public File getCurrentDirectory() {
573 return currentDirectory;
574 }
575
576 /**
577 * Sets the current directory. Passing in <code>null</code> sets the
578 * file chooser to point to the user's default directory.
579 * This default depends on the operating system. It is
580 * typically the "My Documents" folder on Windows, and the user's
581 * home directory on Unix.
582 *
583 * If the file passed in as <code>currentDirectory</code> is not a
584 * directory, the parent of the file will be used as the currentDirectory.
585 * If the parent is not traversable, then it will walk up the parent tree
586 * until it finds a traversable directory, or hits the root of the
587 * file system.
588 *
589 * @beaninfo
590 * preferred: true
591 * bound: true
592 * description: The directory that the JFileChooser is showing files of.
593 *
594 * @param dir the current directory to point to
595 * @see #getCurrentDirectory
596 */
597 public void setCurrentDirectory(File dir) {
598 File oldValue = currentDirectory;
599
600 if (dir != null && !dir.exists()) {
601 dir = currentDirectory;
602 }
603 if (dir == null) {
604 dir = getFileSystemView().getDefaultDirectory();
605 }
606 if (currentDirectory != null) {
607 /* Verify the toString of object */
608 if (this.currentDirectory.equals(dir)) {
609 return;
610 }
611 }
612
613 File prev = null;
614 while (!isTraversable(dir) && prev != dir) {
615 prev = dir;
616 dir = getFileSystemView().getParentDirectory(dir);
864 public boolean getControlButtonsAreShown() {
865 return controlsShown;
866 }
867
868
869 /**
870 * Sets the property
871 * that indicates whether the <i>approve</i> and <i>cancel</i>
872 * buttons are shown in the file chooser. This property
873 * is <code>true</code> by default. Look and feels
874 * that always show these buttons will ignore the value
875 * of this property.
876 * This method fires a property-changed event,
877 * using the string value of
878 * <code>CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY</code>
879 * as the name of the property.
880 *
881 * @param b <code>false</code> if control buttons should not be
882 * shown; otherwise, <code>true</code>
883 *
884 * @beaninfo
885 * preferred: true
886 * bound: true
887 * description: Sets whether the approve & cancel buttons are shown.
888 *
889 * @see #getControlButtonsAreShown
890 * @see #CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
891 * @since 1.3
892 */
893 public void setControlButtonsAreShown(boolean b) {
894 if(controlsShown == b) {
895 return;
896 }
897 boolean oldValue = controlsShown;
898 controlsShown = b;
899 firePropertyChange(CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY, oldValue, controlsShown);
900 }
901
902 /**
903 * Returns the type of this dialog. The default is
904 * <code>JFileChooser.OPEN_DIALOG</code>.
905 *
906 * @return the type of dialog to be displayed:
907 * <ul>
908 * <li>JFileChooser.OPEN_DIALOG
909 * <li>JFileChooser.SAVE_DIALOG
910 * <li>JFileChooser.CUSTOM_DIALOG
911 * </ul>
912 *
922 * Likewise, use <code>SAVE_DIALOG</code> for letting the user choose
923 * a file for saving.
924 * Use <code>CUSTOM_DIALOG</code> when you want to use the file
925 * chooser in a context other than "Open" or "Save".
926 * For instance, you might want to bring up a file chooser that allows
927 * the user to choose a file to execute. Note that you normally would not
928 * need to set the <code>JFileChooser</code> to use
929 * <code>CUSTOM_DIALOG</code>
930 * since a call to <code>setApproveButtonText</code> does this for you.
931 * The default dialog type is <code>JFileChooser.OPEN_DIALOG</code>.
932 *
933 * @param dialogType the type of dialog to be displayed:
934 * <ul>
935 * <li>JFileChooser.OPEN_DIALOG
936 * <li>JFileChooser.SAVE_DIALOG
937 * <li>JFileChooser.CUSTOM_DIALOG
938 * </ul>
939 *
940 * @exception IllegalArgumentException if <code>dialogType</code> is
941 * not legal
942 * @beaninfo
943 * preferred: true
944 * bound: true
945 * description: The type (open, save, custom) of the JFileChooser.
946 * enum:
947 * OPEN_DIALOG JFileChooser.OPEN_DIALOG
948 * SAVE_DIALOG JFileChooser.SAVE_DIALOG
949 * CUSTOM_DIALOG JFileChooser.CUSTOM_DIALOG
950 *
951 * @see #getDialogType
952 * @see #setApproveButtonText
953 */
954 // PENDING(jeff) - fire button text change property
955 public void setDialogType(int dialogType) {
956 if(this.dialogType == dialogType) {
957 return;
958 }
959 checkDialogType(dialogType);
960 int oldValue = this.dialogType;
961 this.dialogType = dialogType;
962 if(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG) {
963 setApproveButtonText(null);
964 }
965 firePropertyChange(DIALOG_TYPE_CHANGED_PROPERTY, oldValue, dialogType);
966 }
967
968 private static void checkDialogType(int dialogType) {
969 if (!(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG
970 || dialogType == CUSTOM_DIALOG)) {
971 throw new IllegalArgumentException(
972 "Incorrect Dialog Type: " + dialogType);
973 }
974 }
975
976 /**
977 * Sets the string that goes in the <code>JFileChooser</code> window's
978 * title bar.
979 *
980 * @param dialogTitle the new <code>String</code> for the title bar
981 *
982 * @beaninfo
983 * preferred: true
984 * bound: true
985 * description: The title of the JFileChooser dialog window.
986 *
987 * @see #getDialogTitle
988 *
989 */
990 public void setDialogTitle(String dialogTitle) {
991 String oldValue = this.dialogTitle;
992 this.dialogTitle = dialogTitle;
993 if(dialog != null) {
994 dialog.setTitle(dialogTitle);
995 }
996 firePropertyChange(DIALOG_TITLE_CHANGED_PROPERTY, oldValue, dialogTitle);
997 }
998
999 /**
1000 * Gets the string that goes in the <code>JFileChooser</code>'s titlebar.
1001 *
1002 * @return the string from the {@code JFileChooser} window's title bar
1003 * @see #setDialogTitle
1004 */
1005 public String getDialogTitle() {
1006 return dialogTitle;
1007 }
1008
1009 // ************************************
1010 // ***** JFileChooser View Options *****
1011 // ************************************
1012
1013
1014
1015 /**
1016 * Sets the tooltip text used in the <code>ApproveButton</code>.
1017 * If <code>null</code>, the UI object will determine the button's text.
1018 *
1019 * @beaninfo
1020 * preferred: true
1021 * bound: true
1022 * description: The tooltip text for the ApproveButton.
1023 *
1024 * @param toolTipText the tooltip text for the approve button
1025 * @see #setApproveButtonText
1026 * @see #setDialogType
1027 * @see #showDialog
1028 */
1029 public void setApproveButtonToolTipText(String toolTipText) {
1030 if(approveButtonToolTipText == toolTipText) {
1031 return;
1032 }
1033 String oldValue = approveButtonToolTipText;
1034 approveButtonToolTipText = toolTipText;
1035 firePropertyChange(APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY, oldValue, approveButtonToolTipText);
1036 }
1037
1038
1039 /**
1040 * Returns the tooltip text used in the <code>ApproveButton</code>.
1041 * If <code>null</code>, the UI object will determine the button's text.
1042 *
1043 * @return the tooltip text used for the approve button
1044 *
1045 * @see #setApproveButtonText
1046 * @see #setDialogType
1047 * @see #showDialog
1048 */
1049 public String getApproveButtonToolTipText() {
1050 return approveButtonToolTipText;
1051 }
1052
1053 /**
1054 * Returns the approve button's mnemonic.
1055 * @return an integer value for the mnemonic key
1056 *
1057 * @see #setApproveButtonMnemonic
1058 */
1059 public int getApproveButtonMnemonic() {
1060 return approveButtonMnemonic;
1061 }
1062
1063 /**
1064 * Sets the approve button's mnemonic using a numeric keycode.
1065 *
1066 * @param mnemonic an integer value for the mnemonic key
1067 *
1068 * @beaninfo
1069 * preferred: true
1070 * bound: true
1071 * description: The mnemonic key accelerator for the ApproveButton.
1072 *
1073 * @see #getApproveButtonMnemonic
1074 */
1075 public void setApproveButtonMnemonic(int mnemonic) {
1076 if(approveButtonMnemonic == mnemonic) {
1077 return;
1078 }
1079 int oldValue = approveButtonMnemonic;
1080 approveButtonMnemonic = mnemonic;
1081 firePropertyChange(APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY, oldValue, approveButtonMnemonic);
1082 }
1083
1084 /**
1085 * Sets the approve button's mnemonic using a character.
1086 * @param mnemonic a character value for the mnemonic key
1087 *
1088 * @see #getApproveButtonMnemonic
1089 */
1090 public void setApproveButtonMnemonic(char mnemonic) {
1091 int vk = (int) mnemonic;
1092 if(vk >= 'a' && vk <='z') {
1093 vk -= ('a' - 'A');
1094 }
1095 setApproveButtonMnemonic(vk);
1096 }
1097
1098
1099 /**
1100 * Sets the text used in the <code>ApproveButton</code> in the
1101 * <code>FileChooserUI</code>.
1102 *
1103 * @beaninfo
1104 * preferred: true
1105 * bound: true
1106 * description: The text that goes in the ApproveButton.
1107 *
1108 * @param approveButtonText the text used in the <code>ApproveButton</code>
1109 *
1110 * @see #getApproveButtonText
1111 * @see #setDialogType
1112 * @see #showDialog
1113 */
1114 // PENDING(jeff) - have ui set this on dialog type change
1115 public void setApproveButtonText(String approveButtonText) {
1116 if(this.approveButtonText == approveButtonText) {
1117 return;
1118 }
1119 String oldValue = this.approveButtonText;
1120 this.approveButtonText = approveButtonText;
1121 firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, oldValue, approveButtonText);
1122 }
1123
1124 /**
1125 * Returns the text used in the <code>ApproveButton</code> in the
1126 * <code>FileChooserUI</code>.
1127 * If <code>null</code>, the UI object will determine the button's text.
1128 *
1129 * Typically, this would be "Open" or "Save".
1130 *
1131 * @return the text used in the <code>ApproveButton</code>
1132 *
1133 * @see #setApproveButtonText
1134 * @see #setDialogType
1135 * @see #showDialog
1136 */
1137 public String getApproveButtonText() {
1138 return approveButtonText;
1139 }
1140
1141 /**
1142 * Gets the list of user choosable file filters.
1143 *
1144 * @return a <code>FileFilter</code> array containing all the choosable
1145 * file filters
1146 *
1147 * @see #addChoosableFileFilter
1148 * @see #removeChoosableFileFilter
1149 * @see #resetChoosableFileFilters
1150 */
1151 public FileFilter[] getChoosableFileFilters() {
1152 FileFilter[] filterArray = new FileFilter[filters.size()];
1153 filters.copyInto(filterArray);
1154 return filterArray;
1155 }
1156
1157 /**
1158 * Adds a filter to the list of user choosable file filters.
1159 * For information on setting the file selection mode, see
1160 * {@link #setFileSelectionMode setFileSelectionMode}.
1161 *
1162 * @param filter the <code>FileFilter</code> to add to the choosable file
1163 * filter list
1164 *
1165 * @beaninfo
1166 * preferred: true
1167 * bound: true
1168 * description: Adds a filter to the list of user choosable file filters.
1169 *
1170 * @see #getChoosableFileFilters
1171 * @see #removeChoosableFileFilter
1172 * @see #resetChoosableFileFilters
1173 * @see #setFileSelectionMode
1174 */
1175 public void addChoosableFileFilter(FileFilter filter) {
1176 if(filter != null && !filters.contains(filter)) {
1177 FileFilter[] oldValue = getChoosableFileFilters();
1178 filters.addElement(filter);
1179 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
1180 if (fileFilter == null && filters.size() == 1) {
1181 setFileFilter(filter);
1182 }
1183 }
1184 }
1185
1186 /**
1187 * Removes a filter from the list of user choosable file filters. Returns
1188 * true if the file filter was removed.
1189 *
1190 * @param f the file filter to be removed
1191 * @return true if the file filter was removed, false otherwise
1192 * @see #addChoosableFileFilter
1193 * @see #getChoosableFileFilters
1194 * @see #resetChoosableFileFilters
1232 * @see #addChoosableFileFilter
1233 * @see #getChoosableFileFilters
1234 * @see #removeChoosableFileFilter
1235 */
1236 public void resetChoosableFileFilters() {
1237 FileFilter[] oldValue = getChoosableFileFilters();
1238 setFileFilter(null);
1239 filters.removeAllElements();
1240 if(isAcceptAllFileFilterUsed()) {
1241 addChoosableFileFilter(getAcceptAllFileFilter());
1242 }
1243 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
1244 }
1245
1246 /**
1247 * Returns the <code>AcceptAll</code> file filter.
1248 * For example, on Microsoft Windows this would be All Files (*.*).
1249 *
1250 * @return the {@code AcceptAll} file filter
1251 */
1252 public FileFilter getAcceptAllFileFilter() {
1253 FileFilter filter = null;
1254 if(getUI() != null) {
1255 filter = getUI().getAcceptAllFileFilter(this);
1256 }
1257 return filter;
1258 }
1259
1260 /**
1261 * Returns whether the <code>AcceptAll FileFilter</code> is used.
1262 * @return true if the <code>AcceptAll FileFilter</code> is used
1263 * @see #setAcceptAllFileFilterUsed
1264 * @since 1.3
1265 */
1266 public boolean isAcceptAllFileFilterUsed() {
1267 return useAcceptAllFileFilter;
1268 }
1269
1270 /**
1271 * Determines whether the <code>AcceptAll FileFilter</code> is used
1272 * as an available choice in the choosable filter list.
1273 * If false, the <code>AcceptAll</code> file filter is removed from
1274 * the list of available file filters.
1275 * If true, the <code>AcceptAll</code> file filter will become the
1276 * actively used file filter.
1277 *
1278 * @param b a {@code boolean} which determines whether the {@code AcceptAll}
1279 * file filter is an available choice in the choosable filter list
1280 * @beaninfo
1281 * preferred: true
1282 * bound: true
1283 * description: Sets whether the AcceptAll FileFilter is used as an available choice in the choosable filter list.
1284 *
1285 * @see #isAcceptAllFileFilterUsed
1286 * @see #getAcceptAllFileFilter
1287 * @see #setFileFilter
1288 * @since 1.3
1289 */
1290 public void setAcceptAllFileFilterUsed(boolean b) {
1291 boolean oldValue = useAcceptAllFileFilter;
1292 useAcceptAllFileFilter = b;
1293 if(!b) {
1294 removeChoosableFileFilter(getAcceptAllFileFilter());
1295 } else {
1296 removeChoosableFileFilter(getAcceptAllFileFilter());
1297 addChoosableFileFilter(getAcceptAllFileFilter());
1298 }
1299 firePropertyChange(ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY, oldValue, useAcceptAllFileFilter);
1300 }
1301
1302 /**
1303 * Returns the accessory component.
1304 *
1305 * @return this JFileChooser's accessory component, or null
1306 * @see #setAccessory
1307 */
1308 public JComponent getAccessory() {
1309 return accessory;
1310 }
1311
1312 /**
1313 * Sets the accessory component. An accessory is often used to show a
1314 * preview image of the selected file; however, it can be used for anything
1315 * that the programmer wishes, such as extra custom file chooser controls.
1316 *
1317 * <p>
1318 * Note: if there was a previous accessory, you should unregister
1319 * any listeners that the accessory might have registered with the
1320 * file chooser.
1321 *
1322 * @param newAccessory the accessory component to be set
1323 * @beaninfo
1324 * preferred: true
1325 * bound: true
1326 * description: Sets the accessory component on the JFileChooser.
1327 */
1328 public void setAccessory(JComponent newAccessory) {
1329 JComponent oldValue = accessory;
1330 accessory = newAccessory;
1331 firePropertyChange(ACCESSORY_CHANGED_PROPERTY, oldValue, accessory);
1332 }
1333
1334 /**
1335 * Sets the <code>JFileChooser</code> to allow the user to just
1336 * select files, just select
1337 * directories, or select both files and directories. The default is
1338 * <code>JFilesChooser.FILES_ONLY</code>.
1339 *
1340 * @param mode the type of files to be displayed:
1341 * <ul>
1342 * <li>JFileChooser.FILES_ONLY
1343 * <li>JFileChooser.DIRECTORIES_ONLY
1344 * <li>JFileChooser.FILES_AND_DIRECTORIES
1345 * </ul>
1346 *
1347 * @exception IllegalArgumentException if <code>mode</code> is an
1348 * illegal file selection mode
1349 * @beaninfo
1350 * preferred: true
1351 * bound: true
1352 * description: Sets the types of files that the JFileChooser can choose.
1353 * enum: FILES_ONLY JFileChooser.FILES_ONLY
1354 * DIRECTORIES_ONLY JFileChooser.DIRECTORIES_ONLY
1355 * FILES_AND_DIRECTORIES JFileChooser.FILES_AND_DIRECTORIES
1356 *
1357 *
1358 * @see #getFileSelectionMode
1359 */
1360 public void setFileSelectionMode(int mode) {
1361 if(fileSelectionMode == mode) {
1362 return;
1363 }
1364
1365 checkFileSelectionMode(mode);
1366 int oldValue = fileSelectionMode;
1367 fileSelectionMode = mode;
1368 firePropertyChange(FILE_SELECTION_MODE_CHANGED_PROPERTY, oldValue, fileSelectionMode);
1369 }
1370
1371 private static void checkFileSelectionMode(int mode) {
1372 if ((mode != FILES_ONLY) && (mode != DIRECTORIES_ONLY)
1373 && (mode != FILES_AND_DIRECTORIES)) {
1374 throw new IllegalArgumentException(
1375 "Incorrect Mode for file selection: " + mode);
1376 }
1377 }
1378
1379 /**
1383 * @return the type of files to be displayed, one of the following:
1384 * <ul>
1385 * <li>JFileChooser.FILES_ONLY
1386 * <li>JFileChooser.DIRECTORIES_ONLY
1387 * <li>JFileChooser.FILES_AND_DIRECTORIES
1388 * </ul>
1389 * @see #setFileSelectionMode
1390 */
1391 public int getFileSelectionMode() {
1392 return fileSelectionMode;
1393 }
1394
1395 /**
1396 * Convenience call that determines if files are selectable based on the
1397 * current file selection mode.
1398 *
1399 * @return true if files are selectable, false otherwise
1400 * @see #setFileSelectionMode
1401 * @see #getFileSelectionMode
1402 */
1403 public boolean isFileSelectionEnabled() {
1404 return ((fileSelectionMode == FILES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES));
1405 }
1406
1407 /**
1408 * Convenience call that determines if directories are selectable based
1409 * on the current file selection mode.
1410 *
1411 * @return true if directories are selectable, false otherwise
1412 * @see #setFileSelectionMode
1413 * @see #getFileSelectionMode
1414 */
1415 public boolean isDirectorySelectionEnabled() {
1416 return ((fileSelectionMode == DIRECTORIES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES));
1417 }
1418
1419 /**
1420 * Sets the file chooser to allow multiple file selections.
1421 *
1422 * @param b true if multiple files may be selected
1423 * @beaninfo
1424 * bound: true
1425 * description: Sets multiple file selection mode.
1426 *
1427 * @see #isMultiSelectionEnabled
1428 */
1429 public void setMultiSelectionEnabled(boolean b) {
1430 if(multiSelectionEnabled == b) {
1431 return;
1432 }
1433 boolean oldValue = multiSelectionEnabled;
1434 multiSelectionEnabled = b;
1435 firePropertyChange(MULTI_SELECTION_ENABLED_CHANGED_PROPERTY, oldValue, multiSelectionEnabled);
1436 }
1437
1438 /**
1439 * Returns true if multiple files can be selected.
1440 * @return true if multiple files can be selected
1441 * @see #setMultiSelectionEnabled
1442 */
1443 public boolean isMultiSelectionEnabled() {
1444 return multiSelectionEnabled;
1445 }
1446
1447
1448 /**
1449 * Returns true if hidden files are not shown in the file chooser;
1450 * otherwise, returns false.
1451 *
1452 * @return the status of the file hiding property
1453 * @see #setFileHidingEnabled
1454 */
1455 public boolean isFileHidingEnabled() {
1456 return useFileHiding;
1457 }
1458
1459 /**
1460 * Sets file hiding on or off. If true, hidden files are not shown
1461 * in the file chooser. The job of determining which files are
1462 * shown is done by the <code>FileView</code>.
1463 *
1464 * @beaninfo
1465 * preferred: true
1466 * bound: true
1467 * description: Sets file hiding on or off.
1468 *
1469 * @param b the boolean value that determines whether file hiding is
1470 * turned on
1471 * @see #isFileHidingEnabled
1472 */
1473 public void setFileHidingEnabled(boolean b) {
1474 // Dump showFilesListener since we'll ignore it from now on
1475 if (showFilesListener != null) {
1476 Toolkit.getDefaultToolkit().removePropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener);
1477 showFilesListener = null;
1478 }
1479 boolean oldValue = useFileHiding;
1480 useFileHiding = b;
1481 firePropertyChange(FILE_HIDING_CHANGED_PROPERTY, oldValue, useFileHiding);
1482 }
1483
1484 /**
1485 * Sets the current file filter. The file filter is used by the
1486 * file chooser to filter out files from the user's view.
1487 *
1488 * @beaninfo
1489 * preferred: true
1490 * bound: true
1491 * description: Sets the File Filter used to filter out files of type.
1492 *
1493 * @param filter the new current file filter to use
1494 * @see #getFileFilter
1495 */
1496 public void setFileFilter(FileFilter filter) {
1497 FileFilter oldValue = fileFilter;
1498 fileFilter = filter;
1499 if (filter != null) {
1500 if (isMultiSelectionEnabled() && selectedFiles != null && selectedFiles.length > 0) {
1501 Vector<File> fList = new Vector<File>();
1502 boolean failed = false;
1503 for (File file : selectedFiles) {
1504 if (filter.accept(file)) {
1505 fList.add(file);
1506 } else {
1507 failed = true;
1508 }
1509 }
1510 if (failed) {
1511 setSelectedFiles((fList.size() == 0) ? null : fList.toArray(new File[fList.size()]));
1512 }
1513 } else if (selectedFile != null && !filter.accept(selectedFile)) {
1514 setSelectedFile(null);
1515 }
1517 firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, oldValue, fileFilter);
1518 }
1519
1520
1521 /**
1522 * Returns the currently selected file filter.
1523 *
1524 * @return the current file filter
1525 * @see #setFileFilter
1526 * @see #addChoosableFileFilter
1527 */
1528 public FileFilter getFileFilter() {
1529 return fileFilter;
1530 }
1531
1532 /**
1533 * Sets the file view to be used to retrieve UI information, such as
1534 * the icon that represents a file or the type description of a file.
1535 *
1536 * @param fileView a {@code FileView} to be used to retrieve UI information
1537 * @beaninfo
1538 * preferred: true
1539 * bound: true
1540 * description: Sets the File View used to get file type information.
1541 *
1542 * @see #getFileView
1543 */
1544 public void setFileView(FileView fileView) {
1545 FileView oldValue = this.fileView;
1546 this.fileView = fileView;
1547 firePropertyChange(FILE_VIEW_CHANGED_PROPERTY, oldValue, fileView);
1548 }
1549
1550 /**
1551 * Returns the current file view.
1552 *
1553 * @return the current file view
1554 * @see #setFileView
1555 */
1556 public FileView getFileView() {
1557 return fileView;
1558 }
1559
1560 // ******************************
1561 // *****FileView delegation *****
1562 // ******************************
1563
1687 /**
1688 * Returns true if the file should be displayed.
1689 * @param f the <code>File</code>
1690 * @return true if the file should be displayed, otherwise false
1691 * @see FileFilter#accept
1692 */
1693 public boolean accept(File f) {
1694 boolean shown = true;
1695 if(f != null && fileFilter != null) {
1696 shown = fileFilter.accept(f);
1697 }
1698 return shown;
1699 }
1700
1701 /**
1702 * Sets the file system view that the <code>JFileChooser</code> uses for
1703 * accessing and creating file system resources, such as finding
1704 * the floppy drive and getting a list of root drives.
1705 * @param fsv the new <code>FileSystemView</code>
1706 *
1707 * @beaninfo
1708 * expert: true
1709 * bound: true
1710 * description: Sets the FileSytemView used to get filesystem information.
1711 *
1712 * @see FileSystemView
1713 */
1714 public void setFileSystemView(FileSystemView fsv) {
1715 FileSystemView oldValue = fileSystemView;
1716 fileSystemView = fsv;
1717 firePropertyChange(FILE_SYSTEM_VIEW_CHANGED_PROPERTY, oldValue, fileSystemView);
1718 }
1719
1720 /**
1721 * Returns the file system view.
1722 * @return the <code>FileSystemView</code> object
1723 * @see #setFileSystemView
1724 */
1725 public FileSystemView getFileSystemView() {
1726 return fileSystemView;
1727 }
1728
1729 // **************************
1730 // ***** Event Handling *****
1731 // **************************
1732
1733 /**
1784 *
1785 * @see #addActionListener
1786 */
1787 public void removeActionListener(ActionListener l) {
1788 listenerList.remove(ActionListener.class, l);
1789 }
1790
1791 /**
1792 * Returns an array of all the action listeners
1793 * registered on this file chooser.
1794 *
1795 * @return all of this file chooser's <code>ActionListener</code>s
1796 * or an empty
1797 * array if no action listeners are currently registered
1798 *
1799 * @see #addActionListener
1800 * @see #removeActionListener
1801 *
1802 * @since 1.4
1803 */
1804 public ActionListener[] getActionListeners() {
1805 return listenerList.getListeners(ActionListener.class);
1806 }
1807
1808 /**
1809 * Notifies all listeners that have registered interest for
1810 * notification on this event type. The event instance
1811 * is lazily created using the <code>command</code> parameter.
1812 *
1813 * @param command a string that may specify a command associated with
1814 * the event
1815 * @see EventListenerList
1816 */
1817 protected void fireActionPerformed(String command) {
1818 // Guaranteed to return a non-null array
1819 Object[] listeners = listenerList.getListenerList();
1820 long mostRecentEventTime = EventQueue.getMostRecentEventTime();
1821 int modifiers = 0;
1822 AWTEvent currentEvent = EventQueue.getCurrentEvent();
1823 if (currentEvent instanceof InputEvent) {
1878 }
1879 FileChooserUI ui = ((FileChooserUI)UIManager.getUI(this));
1880 if (fileSystemView == null) {
1881 // We were probably deserialized
1882 setFileSystemView(FileSystemView.getFileSystemView());
1883 }
1884 setUI(ui);
1885
1886 if(isAcceptAllFileFilterUsed()) {
1887 addChoosableFileFilter(getAcceptAllFileFilter());
1888 }
1889 }
1890
1891 /**
1892 * Returns a string that specifies the name of the L&F class
1893 * that renders this component.
1894 *
1895 * @return the string "FileChooserUI"
1896 * @see JComponent#getUIClassID
1897 * @see UIDefaults#getUI
1898 * @beaninfo
1899 * expert: true
1900 * description: A string that specifies the name of the L&F class.
1901 */
1902 public String getUIClassID() {
1903 return uiClassID;
1904 }
1905
1906 /**
1907 * Gets the UI object which implements the L&F for this component.
1908 *
1909 * @return the FileChooserUI object that implements the FileChooserUI L&F
1910 */
1911 public FileChooserUI getUI() {
1912 return (FileChooserUI) ui;
1913 }
1914
1915 /**
1916 * See <code>readObject</code> and <code>writeObject</code> in
1917 * <code>JComponent</code> for more
1918 * information about serialization in Swing.
1919 */
1920 private void readObject(java.io.ObjectInputStream in)
1921 throws IOException, ClassNotFoundException {
1922 ObjectInputStream.GetField f = in.readFields();
1923
1924 dialogTitle = (String) f.get("dialogTitle", null);
1925 approveButtonText = (String) f.get("approveButtonText", null);
1926 approveButtonToolTipText =
1927 (String) f.get("approveButtonToolTipText", null);
1928 approveButtonMnemonic = f.get("approveButtonMnemonic", 0);
1929 @SuppressWarnings("unchecked")
1930 Vector<FileFilter> newFilters = (Vector<FileFilter>) f.get("filters", null);
2053 }
2054
2055 /////////////////
2056 // Accessibility support
2057 ////////////////
2058
2059 /**
2060 * {@code AccessibleContext} associated with this {@code JFileChooser}
2061 */
2062 protected AccessibleContext accessibleContext = null;
2063
2064 /**
2065 * Gets the AccessibleContext associated with this JFileChooser.
2066 * For file choosers, the AccessibleContext takes the form of an
2067 * AccessibleJFileChooser.
2068 * A new AccessibleJFileChooser instance is created if necessary.
2069 *
2070 * @return an AccessibleJFileChooser that serves as the
2071 * AccessibleContext of this JFileChooser
2072 */
2073 public AccessibleContext getAccessibleContext() {
2074 if (accessibleContext == null) {
2075 accessibleContext = new AccessibleJFileChooser();
2076 }
2077 return accessibleContext;
2078 }
2079
2080 /**
2081 * This class implements accessibility support for the
2082 * <code>JFileChooser</code> class. It provides an implementation of the
2083 * Java Accessibility API appropriate to file chooser user-interface
2084 * elements.
2085 */
2086 @SuppressWarnings("serial") // Superclass is not serializable across versions
2087 protected class AccessibleJFileChooser extends AccessibleJComponent {
2088
2089 /**
2090 * Gets the role of this object.
2091 *
2092 * @return an instance of AccessibleRole describing the role of the
|
1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package javax.swing;
26
27 import javax.swing.event.*;
28 import javax.swing.filechooser.*;
29 import javax.swing.plaf.FileChooserUI;
30
31 import javax.accessibility.*;
32
33 import java.io.File;
34 import java.io.ObjectOutputStream;
35 import java.io.IOException;
36
37 import java.util.Vector;
38 import java.awt.AWTEvent;
39 import java.awt.Component;
40 import java.awt.Container;
41 import java.awt.BorderLayout;
42 import java.awt.Window;
43 import java.awt.Dialog;
44 import java.awt.Frame;
45 import java.awt.GraphicsEnvironment;
46 import java.awt.HeadlessException;
47 import java.awt.EventQueue;
48 import java.awt.Toolkit;
49 import java.awt.event.*;
50 import java.beans.JavaBean;
51 import java.beans.BeanProperty;
52 import java.beans.PropertyChangeListener;
53 import java.beans.PropertyChangeEvent;
54 import java.io.InvalidObjectException;
55 import java.io.ObjectInputStream;
56 import java.lang.ref.WeakReference;
57
58 /**
59 * <code>JFileChooser</code> provides a simple mechanism for the user to
60 * choose a file.
61 * For information about using <code>JFileChooser</code>, see
62 * <a
63 href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html">How to Use File Choosers</a>,
64 * a section in <em>The Java Tutorial</em>.
65 *
66 * <p>
67 *
68 * The following code pops up a file chooser for the user's home directory that
69 * sees only .jpg and .gif images:
70 * <pre>
71 * JFileChooser chooser = new JFileChooser();
72 * FileNameExtensionFilter filter = new FileNameExtensionFilter(
73 * "JPG & GIF Images", "jpg", "gif");
74 * chooser.setFileFilter(filter);
75 * int returnVal = chooser.showOpenDialog(parent);
76 * if(returnVal == JFileChooser.APPROVE_OPTION) {
77 * System.out.println("You chose to open this file: " +
78 * chooser.getSelectedFile().getName());
79 * }
80 * </pre>
81 * <p>
82 * <strong>Warning:</strong> Swing is not thread safe. For more
83 * information see <a
84 * href="package-summary.html#threading">Swing's Threading
85 * Policy</a>.
86 *
87 * @author Jeff Dinkins
88 * @since 1.2
89 */
90 @JavaBean(defaultProperty = "UI", description = "A component which allows for the interactive selection of a file.")
91 @SwingContainer(false)
92 @SuppressWarnings("serial") // Superclass is not serializable across versions
93 public class JFileChooser extends JComponent implements Accessible {
94
95 /**
96 * @see #getUIClassID
97 * @see #readObject
98 */
99 private static final String uiClassID = "FileChooserUI";
100
101 // ************************
102 // ***** Dialog Types *****
103 // ************************
104
105 /**
106 * Type value indicating that the <code>JFileChooser</code> supports an
107 * "Open" file operation.
108 */
109 public static final int OPEN_DIALOG = 0;
110
111 /**
438 * how selections behave.
439 *
440 * <p>
441 *
442 * Some look and feels might not support automatic drag and drop;
443 * they will ignore this property. You can work around such
444 * look and feels by modifying the component
445 * to directly call the <code>exportAsDrag</code> method of a
446 * <code>TransferHandler</code>.
447 *
448 * @param b the value to set the <code>dragEnabled</code> property to
449 * @exception HeadlessException if
450 * <code>b</code> is <code>true</code> and
451 * <code>GraphicsEnvironment.isHeadless()</code>
452 * returns <code>true</code>
453 * @see java.awt.GraphicsEnvironment#isHeadless
454 * @see #getDragEnabled
455 * @see #setTransferHandler
456 * @see TransferHandler
457 * @since 1.4
458 */
459 @BeanProperty(bound = false, description
460 = "determines whether automatic drag handling is enabled")
461 public void setDragEnabled(boolean b) {
462 checkDragEnabled(b);
463 dragEnabled = b;
464 }
465
466 private static void checkDragEnabled(boolean b) {
467 if (b && GraphicsEnvironment.isHeadless()) {
468 throw new HeadlessException();
469 }
470 }
471
472 /**
473 * Gets the value of the <code>dragEnabled</code> property.
474 *
475 * @return the value of the <code>dragEnabled</code> property
476 * @see #setDragEnabled
477 * @since 1.4
478 */
479 public boolean getDragEnabled() {
480 return dragEnabled;
485 // *****************************
486
487 /**
488 * Returns the selected file. This can be set either by the
489 * programmer via <code>setSelectedFile</code> or by a user action, such as
490 * either typing the filename into the UI or selecting the
491 * file from a list in the UI.
492 *
493 * @see #setSelectedFile
494 * @return the selected file
495 */
496 public File getSelectedFile() {
497 return selectedFile;
498 }
499
500 /**
501 * Sets the selected file. If the file's parent directory is
502 * not the current directory, changes the current directory
503 * to be the file's parent directory.
504 *
505 * @see #getSelectedFile
506 *
507 * @param file the selected file
508 */
509 @BeanProperty(preferred = true)
510 public void setSelectedFile(File file) {
511 File oldValue = selectedFile;
512 selectedFile = file;
513 if(selectedFile != null) {
514 if (file.isAbsolute() && !getFileSystemView().isParent(getCurrentDirectory(), selectedFile)) {
515 setCurrentDirectory(selectedFile.getParentFile());
516 }
517 if (!isMultiSelectionEnabled() || selectedFiles == null || selectedFiles.length == 1) {
518 ensureFileIsVisible(selectedFile);
519 }
520 }
521 firePropertyChange(SELECTED_FILE_CHANGED_PROPERTY, oldValue, selectedFile);
522 }
523
524 /**
525 * Returns a list of selected files if the file chooser is
526 * set to allow multiple selection.
527 *
528 * @return an array of selected {@code File}s
529 */
530 public File[] getSelectedFiles() {
531 if(selectedFiles == null) {
532 return new File[0];
533 } else {
534 return selectedFiles.clone();
535 }
536 }
537
538 /**
539 * Sets the list of selected files if the file chooser is
540 * set to allow multiple selection.
541 *
542 * @param selectedFiles an array {@code File}s to be selected
543 */
544 @BeanProperty(description
545 = "The list of selected files if the chooser is in multiple selection mode.")
546 public void setSelectedFiles(File[] selectedFiles) {
547 File[] oldValue = this.selectedFiles;
548 if (selectedFiles == null || selectedFiles.length == 0) {
549 selectedFiles = null;
550 this.selectedFiles = null;
551 setSelectedFile(null);
552 } else {
553 this.selectedFiles = selectedFiles.clone();
554 setSelectedFile(this.selectedFiles[0]);
555 }
556 firePropertyChange(SELECTED_FILES_CHANGED_PROPERTY, oldValue, selectedFiles);
557 }
558
559 /**
560 * Returns the current directory.
561 *
562 * @return the current directory
563 * @see #setCurrentDirectory
564 */
565 public File getCurrentDirectory() {
566 return currentDirectory;
567 }
568
569 /**
570 * Sets the current directory. Passing in <code>null</code> sets the
571 * file chooser to point to the user's default directory.
572 * This default depends on the operating system. It is
573 * typically the "My Documents" folder on Windows, and the user's
574 * home directory on Unix.
575 *
576 * If the file passed in as <code>currentDirectory</code> is not a
577 * directory, the parent of the file will be used as the currentDirectory.
578 * If the parent is not traversable, then it will walk up the parent tree
579 * until it finds a traversable directory, or hits the root of the
580 * file system.
581 *
582 * @param dir the current directory to point to
583 * @see #getCurrentDirectory
584 */
585 @BeanProperty(preferred = true, description
586 = "The directory that the JFileChooser is showing files of.")
587 public void setCurrentDirectory(File dir) {
588 File oldValue = currentDirectory;
589
590 if (dir != null && !dir.exists()) {
591 dir = currentDirectory;
592 }
593 if (dir == null) {
594 dir = getFileSystemView().getDefaultDirectory();
595 }
596 if (currentDirectory != null) {
597 /* Verify the toString of object */
598 if (this.currentDirectory.equals(dir)) {
599 return;
600 }
601 }
602
603 File prev = null;
604 while (!isTraversable(dir) && prev != dir) {
605 prev = dir;
606 dir = getFileSystemView().getParentDirectory(dir);
854 public boolean getControlButtonsAreShown() {
855 return controlsShown;
856 }
857
858
859 /**
860 * Sets the property
861 * that indicates whether the <i>approve</i> and <i>cancel</i>
862 * buttons are shown in the file chooser. This property
863 * is <code>true</code> by default. Look and feels
864 * that always show these buttons will ignore the value
865 * of this property.
866 * This method fires a property-changed event,
867 * using the string value of
868 * <code>CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY</code>
869 * as the name of the property.
870 *
871 * @param b <code>false</code> if control buttons should not be
872 * shown; otherwise, <code>true</code>
873 *
874 * @see #getControlButtonsAreShown
875 * @see #CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
876 * @since 1.3
877 */
878 @BeanProperty(preferred = true, description
879 = "Sets whether the approve & cancel buttons are shown.")
880 public void setControlButtonsAreShown(boolean b) {
881 if(controlsShown == b) {
882 return;
883 }
884 boolean oldValue = controlsShown;
885 controlsShown = b;
886 firePropertyChange(CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY, oldValue, controlsShown);
887 }
888
889 /**
890 * Returns the type of this dialog. The default is
891 * <code>JFileChooser.OPEN_DIALOG</code>.
892 *
893 * @return the type of dialog to be displayed:
894 * <ul>
895 * <li>JFileChooser.OPEN_DIALOG
896 * <li>JFileChooser.SAVE_DIALOG
897 * <li>JFileChooser.CUSTOM_DIALOG
898 * </ul>
899 *
909 * Likewise, use <code>SAVE_DIALOG</code> for letting the user choose
910 * a file for saving.
911 * Use <code>CUSTOM_DIALOG</code> when you want to use the file
912 * chooser in a context other than "Open" or "Save".
913 * For instance, you might want to bring up a file chooser that allows
914 * the user to choose a file to execute. Note that you normally would not
915 * need to set the <code>JFileChooser</code> to use
916 * <code>CUSTOM_DIALOG</code>
917 * since a call to <code>setApproveButtonText</code> does this for you.
918 * The default dialog type is <code>JFileChooser.OPEN_DIALOG</code>.
919 *
920 * @param dialogType the type of dialog to be displayed:
921 * <ul>
922 * <li>JFileChooser.OPEN_DIALOG
923 * <li>JFileChooser.SAVE_DIALOG
924 * <li>JFileChooser.CUSTOM_DIALOG
925 * </ul>
926 *
927 * @exception IllegalArgumentException if <code>dialogType</code> is
928 * not legal
929 *
930 * @see #getDialogType
931 * @see #setApproveButtonText
932 */
933 // PENDING(jeff) - fire button text change property
934 @BeanProperty(preferred = true, enumerationValues = {
935 "JFileChooser.OPEN_DIALOG",
936 "JFileChooser.SAVE_DIALOG",
937 "JFileChooser.CUSTOM_DIALOG"}, description
938 = "The type (open, save, custom) of the JFileChooser.")
939 public void setDialogType(int dialogType) {
940 if(this.dialogType == dialogType) {
941 return;
942 }
943 checkDialogType(dialogType);
944 int oldValue = this.dialogType;
945 this.dialogType = dialogType;
946 if(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG) {
947 setApproveButtonText(null);
948 }
949 firePropertyChange(DIALOG_TYPE_CHANGED_PROPERTY, oldValue, dialogType);
950 }
951
952 private static void checkDialogType(int dialogType) {
953 if (!(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG
954 || dialogType == CUSTOM_DIALOG)) {
955 throw new IllegalArgumentException(
956 "Incorrect Dialog Type: " + dialogType);
957 }
958 }
959
960 /**
961 * Sets the string that goes in the <code>JFileChooser</code> window's
962 * title bar.
963 *
964 * @param dialogTitle the new <code>String</code> for the title bar
965 *
966 * @see #getDialogTitle
967 *
968 */
969 @BeanProperty(preferred = true, description
970 = "The title of the JFileChooser dialog window.")
971 public void setDialogTitle(String dialogTitle) {
972 String oldValue = this.dialogTitle;
973 this.dialogTitle = dialogTitle;
974 if(dialog != null) {
975 dialog.setTitle(dialogTitle);
976 }
977 firePropertyChange(DIALOG_TITLE_CHANGED_PROPERTY, oldValue, dialogTitle);
978 }
979
980 /**
981 * Gets the string that goes in the <code>JFileChooser</code>'s titlebar.
982 *
983 * @return the string from the {@code JFileChooser} window's title bar
984 * @see #setDialogTitle
985 */
986 public String getDialogTitle() {
987 return dialogTitle;
988 }
989
990 // ************************************
991 // ***** JFileChooser View Options *****
992 // ************************************
993
994
995
996 /**
997 * Sets the tooltip text used in the <code>ApproveButton</code>.
998 * If <code>null</code>, the UI object will determine the button's text.
999 *
1000 * @param toolTipText the tooltip text for the approve button
1001 * @see #setApproveButtonText
1002 * @see #setDialogType
1003 * @see #showDialog
1004 */
1005 @BeanProperty(preferred = true, description
1006 = "The tooltip text for the ApproveButton.")
1007 public void setApproveButtonToolTipText(String toolTipText) {
1008 if(approveButtonToolTipText == toolTipText) {
1009 return;
1010 }
1011 String oldValue = approveButtonToolTipText;
1012 approveButtonToolTipText = toolTipText;
1013 firePropertyChange(APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY, oldValue, approveButtonToolTipText);
1014 }
1015
1016
1017 /**
1018 * Returns the tooltip text used in the <code>ApproveButton</code>.
1019 * If <code>null</code>, the UI object will determine the button's text.
1020 *
1021 * @return the tooltip text used for the approve button
1022 *
1023 * @see #setApproveButtonText
1024 * @see #setDialogType
1025 * @see #showDialog
1026 */
1027 public String getApproveButtonToolTipText() {
1028 return approveButtonToolTipText;
1029 }
1030
1031 /**
1032 * Returns the approve button's mnemonic.
1033 * @return an integer value for the mnemonic key
1034 *
1035 * @see #setApproveButtonMnemonic
1036 */
1037 public int getApproveButtonMnemonic() {
1038 return approveButtonMnemonic;
1039 }
1040
1041 /**
1042 * Sets the approve button's mnemonic using a numeric keycode.
1043 *
1044 * @param mnemonic an integer value for the mnemonic key
1045 *
1046 * @see #getApproveButtonMnemonic
1047 */
1048 @BeanProperty(preferred = true, description
1049 = "The mnemonic key accelerator for the ApproveButton.")
1050 public void setApproveButtonMnemonic(int mnemonic) {
1051 if(approveButtonMnemonic == mnemonic) {
1052 return;
1053 }
1054 int oldValue = approveButtonMnemonic;
1055 approveButtonMnemonic = mnemonic;
1056 firePropertyChange(APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY, oldValue, approveButtonMnemonic);
1057 }
1058
1059 /**
1060 * Sets the approve button's mnemonic using a character.
1061 * @param mnemonic a character value for the mnemonic key
1062 *
1063 * @see #getApproveButtonMnemonic
1064 */
1065 public void setApproveButtonMnemonic(char mnemonic) {
1066 int vk = (int) mnemonic;
1067 if(vk >= 'a' && vk <='z') {
1068 vk -= ('a' - 'A');
1069 }
1070 setApproveButtonMnemonic(vk);
1071 }
1072
1073
1074 /**
1075 * Sets the text used in the <code>ApproveButton</code> in the
1076 * <code>FileChooserUI</code>.
1077 *
1078 * @param approveButtonText the text used in the <code>ApproveButton</code>
1079 *
1080 * @see #getApproveButtonText
1081 * @see #setDialogType
1082 * @see #showDialog
1083 */
1084 // PENDING(jeff) - have ui set this on dialog type change
1085 @BeanProperty(preferred = true, description
1086 = "The text that goes in the ApproveButton.")
1087 public void setApproveButtonText(String approveButtonText) {
1088 if(this.approveButtonText == approveButtonText) {
1089 return;
1090 }
1091 String oldValue = this.approveButtonText;
1092 this.approveButtonText = approveButtonText;
1093 firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, oldValue, approveButtonText);
1094 }
1095
1096 /**
1097 * Returns the text used in the <code>ApproveButton</code> in the
1098 * <code>FileChooserUI</code>.
1099 * If <code>null</code>, the UI object will determine the button's text.
1100 *
1101 * Typically, this would be "Open" or "Save".
1102 *
1103 * @return the text used in the <code>ApproveButton</code>
1104 *
1105 * @see #setApproveButtonText
1106 * @see #setDialogType
1107 * @see #showDialog
1108 */
1109 public String getApproveButtonText() {
1110 return approveButtonText;
1111 }
1112
1113 /**
1114 * Gets the list of user choosable file filters.
1115 *
1116 * @return a <code>FileFilter</code> array containing all the choosable
1117 * file filters
1118 *
1119 * @see #addChoosableFileFilter
1120 * @see #removeChoosableFileFilter
1121 * @see #resetChoosableFileFilters
1122 */
1123 @BeanProperty(bound = false)
1124 public FileFilter[] getChoosableFileFilters() {
1125 FileFilter[] filterArray = new FileFilter[filters.size()];
1126 filters.copyInto(filterArray);
1127 return filterArray;
1128 }
1129
1130 /**
1131 * Adds a filter to the list of user choosable file filters.
1132 * For information on setting the file selection mode, see
1133 * {@link #setFileSelectionMode setFileSelectionMode}.
1134 *
1135 * @param filter the <code>FileFilter</code> to add to the choosable file
1136 * filter list
1137 *
1138 * @see #getChoosableFileFilters
1139 * @see #removeChoosableFileFilter
1140 * @see #resetChoosableFileFilters
1141 * @see #setFileSelectionMode
1142 */
1143 @BeanProperty(preferred = true, description
1144 = "Adds a filter to the list of user choosable file filters.")
1145 public void addChoosableFileFilter(FileFilter filter) {
1146 if(filter != null && !filters.contains(filter)) {
1147 FileFilter[] oldValue = getChoosableFileFilters();
1148 filters.addElement(filter);
1149 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
1150 if (fileFilter == null && filters.size() == 1) {
1151 setFileFilter(filter);
1152 }
1153 }
1154 }
1155
1156 /**
1157 * Removes a filter from the list of user choosable file filters. Returns
1158 * true if the file filter was removed.
1159 *
1160 * @param f the file filter to be removed
1161 * @return true if the file filter was removed, false otherwise
1162 * @see #addChoosableFileFilter
1163 * @see #getChoosableFileFilters
1164 * @see #resetChoosableFileFilters
1202 * @see #addChoosableFileFilter
1203 * @see #getChoosableFileFilters
1204 * @see #removeChoosableFileFilter
1205 */
1206 public void resetChoosableFileFilters() {
1207 FileFilter[] oldValue = getChoosableFileFilters();
1208 setFileFilter(null);
1209 filters.removeAllElements();
1210 if(isAcceptAllFileFilterUsed()) {
1211 addChoosableFileFilter(getAcceptAllFileFilter());
1212 }
1213 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
1214 }
1215
1216 /**
1217 * Returns the <code>AcceptAll</code> file filter.
1218 * For example, on Microsoft Windows this would be All Files (*.*).
1219 *
1220 * @return the {@code AcceptAll} file filter
1221 */
1222 @BeanProperty(bound = false)
1223 public FileFilter getAcceptAllFileFilter() {
1224 FileFilter filter = null;
1225 if(getUI() != null) {
1226 filter = getUI().getAcceptAllFileFilter(this);
1227 }
1228 return filter;
1229 }
1230
1231 /**
1232 * Returns whether the <code>AcceptAll FileFilter</code> is used.
1233 * @return true if the <code>AcceptAll FileFilter</code> is used
1234 * @see #setAcceptAllFileFilterUsed
1235 * @since 1.3
1236 */
1237 public boolean isAcceptAllFileFilterUsed() {
1238 return useAcceptAllFileFilter;
1239 }
1240
1241 /**
1242 * Determines whether the <code>AcceptAll FileFilter</code> is used
1243 * as an available choice in the choosable filter list.
1244 * If false, the <code>AcceptAll</code> file filter is removed from
1245 * the list of available file filters.
1246 * If true, the <code>AcceptAll</code> file filter will become the
1247 * actively used file filter.
1248 *
1249 * @param b a {@code boolean} which determines whether the {@code AcceptAll}
1250 * file filter is an available choice in the choosable filter list
1251 *
1252 * @see #isAcceptAllFileFilterUsed
1253 * @see #getAcceptAllFileFilter
1254 * @see #setFileFilter
1255 * @since 1.3
1256 */
1257 @BeanProperty(preferred = true, description
1258 = "Sets whether the AcceptAll FileFilter is used as an available choice in the choosable filter list.")
1259 public void setAcceptAllFileFilterUsed(boolean b) {
1260 boolean oldValue = useAcceptAllFileFilter;
1261 useAcceptAllFileFilter = b;
1262 if(!b) {
1263 removeChoosableFileFilter(getAcceptAllFileFilter());
1264 } else {
1265 removeChoosableFileFilter(getAcceptAllFileFilter());
1266 addChoosableFileFilter(getAcceptAllFileFilter());
1267 }
1268 firePropertyChange(ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY, oldValue, useAcceptAllFileFilter);
1269 }
1270
1271 /**
1272 * Returns the accessory component.
1273 *
1274 * @return this JFileChooser's accessory component, or null
1275 * @see #setAccessory
1276 */
1277 public JComponent getAccessory() {
1278 return accessory;
1279 }
1280
1281 /**
1282 * Sets the accessory component. An accessory is often used to show a
1283 * preview image of the selected file; however, it can be used for anything
1284 * that the programmer wishes, such as extra custom file chooser controls.
1285 *
1286 * <p>
1287 * Note: if there was a previous accessory, you should unregister
1288 * any listeners that the accessory might have registered with the
1289 * file chooser.
1290 *
1291 * @param newAccessory the accessory component to be set
1292 */
1293 @BeanProperty(preferred = true, description
1294 = "Sets the accessory component on the JFileChooser.")
1295 public void setAccessory(JComponent newAccessory) {
1296 JComponent oldValue = accessory;
1297 accessory = newAccessory;
1298 firePropertyChange(ACCESSORY_CHANGED_PROPERTY, oldValue, accessory);
1299 }
1300
1301 /**
1302 * Sets the <code>JFileChooser</code> to allow the user to just
1303 * select files, just select
1304 * directories, or select both files and directories. The default is
1305 * <code>JFilesChooser.FILES_ONLY</code>.
1306 *
1307 * @param mode the type of files to be displayed:
1308 * <ul>
1309 * <li>JFileChooser.FILES_ONLY
1310 * <li>JFileChooser.DIRECTORIES_ONLY
1311 * <li>JFileChooser.FILES_AND_DIRECTORIES
1312 * </ul>
1313 *
1314 * @exception IllegalArgumentException if <code>mode</code> is an
1315 * illegal file selection mode
1316 *
1317 * @see #getFileSelectionMode
1318 */
1319 @BeanProperty(preferred = true, enumerationValues = {
1320 "JFileChooser.FILES_ONLY",
1321 "JFileChooser.DIRECTORIES_ONLY",
1322 "JFileChooser.FILES_AND_DIRECTORIES"}, description
1323 = "Sets the types of files that the JFileChooser can choose.")
1324 public void setFileSelectionMode(int mode) {
1325 if(fileSelectionMode == mode) {
1326 return;
1327 }
1328
1329 checkFileSelectionMode(mode);
1330 int oldValue = fileSelectionMode;
1331 fileSelectionMode = mode;
1332 firePropertyChange(FILE_SELECTION_MODE_CHANGED_PROPERTY, oldValue, fileSelectionMode);
1333 }
1334
1335 private static void checkFileSelectionMode(int mode) {
1336 if ((mode != FILES_ONLY) && (mode != DIRECTORIES_ONLY)
1337 && (mode != FILES_AND_DIRECTORIES)) {
1338 throw new IllegalArgumentException(
1339 "Incorrect Mode for file selection: " + mode);
1340 }
1341 }
1342
1343 /**
1347 * @return the type of files to be displayed, one of the following:
1348 * <ul>
1349 * <li>JFileChooser.FILES_ONLY
1350 * <li>JFileChooser.DIRECTORIES_ONLY
1351 * <li>JFileChooser.FILES_AND_DIRECTORIES
1352 * </ul>
1353 * @see #setFileSelectionMode
1354 */
1355 public int getFileSelectionMode() {
1356 return fileSelectionMode;
1357 }
1358
1359 /**
1360 * Convenience call that determines if files are selectable based on the
1361 * current file selection mode.
1362 *
1363 * @return true if files are selectable, false otherwise
1364 * @see #setFileSelectionMode
1365 * @see #getFileSelectionMode
1366 */
1367 @BeanProperty(bound = false)
1368 public boolean isFileSelectionEnabled() {
1369 return ((fileSelectionMode == FILES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES));
1370 }
1371
1372 /**
1373 * Convenience call that determines if directories are selectable based
1374 * on the current file selection mode.
1375 *
1376 * @return true if directories are selectable, false otherwise
1377 * @see #setFileSelectionMode
1378 * @see #getFileSelectionMode
1379 */
1380 @BeanProperty(bound = false)
1381 public boolean isDirectorySelectionEnabled() {
1382 return ((fileSelectionMode == DIRECTORIES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES));
1383 }
1384
1385 /**
1386 * Sets the file chooser to allow multiple file selections.
1387 *
1388 * @param b true if multiple files may be selected
1389 *
1390 * @see #isMultiSelectionEnabled
1391 */
1392 @BeanProperty(description
1393 = "Sets multiple file selection mode.")
1394 public void setMultiSelectionEnabled(boolean b) {
1395 if(multiSelectionEnabled == b) {
1396 return;
1397 }
1398 boolean oldValue = multiSelectionEnabled;
1399 multiSelectionEnabled = b;
1400 firePropertyChange(MULTI_SELECTION_ENABLED_CHANGED_PROPERTY, oldValue, multiSelectionEnabled);
1401 }
1402
1403 /**
1404 * Returns true if multiple files can be selected.
1405 * @return true if multiple files can be selected
1406 * @see #setMultiSelectionEnabled
1407 */
1408 public boolean isMultiSelectionEnabled() {
1409 return multiSelectionEnabled;
1410 }
1411
1412
1413 /**
1414 * Returns true if hidden files are not shown in the file chooser;
1415 * otherwise, returns false.
1416 *
1417 * @return the status of the file hiding property
1418 * @see #setFileHidingEnabled
1419 */
1420 public boolean isFileHidingEnabled() {
1421 return useFileHiding;
1422 }
1423
1424 /**
1425 * Sets file hiding on or off. If true, hidden files are not shown
1426 * in the file chooser. The job of determining which files are
1427 * shown is done by the <code>FileView</code>.
1428 *
1429 * @param b the boolean value that determines whether file hiding is
1430 * turned on
1431 * @see #isFileHidingEnabled
1432 */
1433 @BeanProperty(preferred = true, description
1434 = "Sets file hiding on or off.")
1435 public void setFileHidingEnabled(boolean b) {
1436 // Dump showFilesListener since we'll ignore it from now on
1437 if (showFilesListener != null) {
1438 Toolkit.getDefaultToolkit().removePropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener);
1439 showFilesListener = null;
1440 }
1441 boolean oldValue = useFileHiding;
1442 useFileHiding = b;
1443 firePropertyChange(FILE_HIDING_CHANGED_PROPERTY, oldValue, useFileHiding);
1444 }
1445
1446 /**
1447 * Sets the current file filter. The file filter is used by the
1448 * file chooser to filter out files from the user's view.
1449 *
1450 * @param filter the new current file filter to use
1451 * @see #getFileFilter
1452 */
1453 @BeanProperty(preferred = true, description
1454 = "Sets the File Filter used to filter out files of type.")
1455 public void setFileFilter(FileFilter filter) {
1456 FileFilter oldValue = fileFilter;
1457 fileFilter = filter;
1458 if (filter != null) {
1459 if (isMultiSelectionEnabled() && selectedFiles != null && selectedFiles.length > 0) {
1460 Vector<File> fList = new Vector<File>();
1461 boolean failed = false;
1462 for (File file : selectedFiles) {
1463 if (filter.accept(file)) {
1464 fList.add(file);
1465 } else {
1466 failed = true;
1467 }
1468 }
1469 if (failed) {
1470 setSelectedFiles((fList.size() == 0) ? null : fList.toArray(new File[fList.size()]));
1471 }
1472 } else if (selectedFile != null && !filter.accept(selectedFile)) {
1473 setSelectedFile(null);
1474 }
1476 firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, oldValue, fileFilter);
1477 }
1478
1479
1480 /**
1481 * Returns the currently selected file filter.
1482 *
1483 * @return the current file filter
1484 * @see #setFileFilter
1485 * @see #addChoosableFileFilter
1486 */
1487 public FileFilter getFileFilter() {
1488 return fileFilter;
1489 }
1490
1491 /**
1492 * Sets the file view to be used to retrieve UI information, such as
1493 * the icon that represents a file or the type description of a file.
1494 *
1495 * @param fileView a {@code FileView} to be used to retrieve UI information
1496 *
1497 * @see #getFileView
1498 */
1499 @BeanProperty(preferred = true, description
1500 = "Sets the File View used to get file type information.")
1501 public void setFileView(FileView fileView) {
1502 FileView oldValue = this.fileView;
1503 this.fileView = fileView;
1504 firePropertyChange(FILE_VIEW_CHANGED_PROPERTY, oldValue, fileView);
1505 }
1506
1507 /**
1508 * Returns the current file view.
1509 *
1510 * @return the current file view
1511 * @see #setFileView
1512 */
1513 public FileView getFileView() {
1514 return fileView;
1515 }
1516
1517 // ******************************
1518 // *****FileView delegation *****
1519 // ******************************
1520
1644 /**
1645 * Returns true if the file should be displayed.
1646 * @param f the <code>File</code>
1647 * @return true if the file should be displayed, otherwise false
1648 * @see FileFilter#accept
1649 */
1650 public boolean accept(File f) {
1651 boolean shown = true;
1652 if(f != null && fileFilter != null) {
1653 shown = fileFilter.accept(f);
1654 }
1655 return shown;
1656 }
1657
1658 /**
1659 * Sets the file system view that the <code>JFileChooser</code> uses for
1660 * accessing and creating file system resources, such as finding
1661 * the floppy drive and getting a list of root drives.
1662 * @param fsv the new <code>FileSystemView</code>
1663 *
1664 * @see FileSystemView
1665 */
1666 @BeanProperty(expert = true, description
1667 = "Sets the FileSytemView used to get filesystem information.")
1668 public void setFileSystemView(FileSystemView fsv) {
1669 FileSystemView oldValue = fileSystemView;
1670 fileSystemView = fsv;
1671 firePropertyChange(FILE_SYSTEM_VIEW_CHANGED_PROPERTY, oldValue, fileSystemView);
1672 }
1673
1674 /**
1675 * Returns the file system view.
1676 * @return the <code>FileSystemView</code> object
1677 * @see #setFileSystemView
1678 */
1679 public FileSystemView getFileSystemView() {
1680 return fileSystemView;
1681 }
1682
1683 // **************************
1684 // ***** Event Handling *****
1685 // **************************
1686
1687 /**
1738 *
1739 * @see #addActionListener
1740 */
1741 public void removeActionListener(ActionListener l) {
1742 listenerList.remove(ActionListener.class, l);
1743 }
1744
1745 /**
1746 * Returns an array of all the action listeners
1747 * registered on this file chooser.
1748 *
1749 * @return all of this file chooser's <code>ActionListener</code>s
1750 * or an empty
1751 * array if no action listeners are currently registered
1752 *
1753 * @see #addActionListener
1754 * @see #removeActionListener
1755 *
1756 * @since 1.4
1757 */
1758 @BeanProperty(bound = false)
1759 public ActionListener[] getActionListeners() {
1760 return listenerList.getListeners(ActionListener.class);
1761 }
1762
1763 /**
1764 * Notifies all listeners that have registered interest for
1765 * notification on this event type. The event instance
1766 * is lazily created using the <code>command</code> parameter.
1767 *
1768 * @param command a string that may specify a command associated with
1769 * the event
1770 * @see EventListenerList
1771 */
1772 protected void fireActionPerformed(String command) {
1773 // Guaranteed to return a non-null array
1774 Object[] listeners = listenerList.getListenerList();
1775 long mostRecentEventTime = EventQueue.getMostRecentEventTime();
1776 int modifiers = 0;
1777 AWTEvent currentEvent = EventQueue.getCurrentEvent();
1778 if (currentEvent instanceof InputEvent) {
1833 }
1834 FileChooserUI ui = ((FileChooserUI)UIManager.getUI(this));
1835 if (fileSystemView == null) {
1836 // We were probably deserialized
1837 setFileSystemView(FileSystemView.getFileSystemView());
1838 }
1839 setUI(ui);
1840
1841 if(isAcceptAllFileFilterUsed()) {
1842 addChoosableFileFilter(getAcceptAllFileFilter());
1843 }
1844 }
1845
1846 /**
1847 * Returns a string that specifies the name of the L&F class
1848 * that renders this component.
1849 *
1850 * @return the string "FileChooserUI"
1851 * @see JComponent#getUIClassID
1852 * @see UIDefaults#getUI
1853 */
1854 @BeanProperty(bound = false, expert = true, description
1855 = "A string that specifies the name of the L&F class.")
1856 public String getUIClassID() {
1857 return uiClassID;
1858 }
1859
1860 /**
1861 * Gets the UI object which implements the L&F for this component.
1862 *
1863 * @return the FileChooserUI object that implements the FileChooserUI L&F
1864 */
1865 @BeanProperty(bound = false)
1866 public FileChooserUI getUI() {
1867 return (FileChooserUI) ui;
1868 }
1869
1870 /**
1871 * See <code>readObject</code> and <code>writeObject</code> in
1872 * <code>JComponent</code> for more
1873 * information about serialization in Swing.
1874 */
1875 private void readObject(java.io.ObjectInputStream in)
1876 throws IOException, ClassNotFoundException {
1877 ObjectInputStream.GetField f = in.readFields();
1878
1879 dialogTitle = (String) f.get("dialogTitle", null);
1880 approveButtonText = (String) f.get("approveButtonText", null);
1881 approveButtonToolTipText =
1882 (String) f.get("approveButtonToolTipText", null);
1883 approveButtonMnemonic = f.get("approveButtonMnemonic", 0);
1884 @SuppressWarnings("unchecked")
1885 Vector<FileFilter> newFilters = (Vector<FileFilter>) f.get("filters", null);
2008 }
2009
2010 /////////////////
2011 // Accessibility support
2012 ////////////////
2013
2014 /**
2015 * {@code AccessibleContext} associated with this {@code JFileChooser}
2016 */
2017 protected AccessibleContext accessibleContext = null;
2018
2019 /**
2020 * Gets the AccessibleContext associated with this JFileChooser.
2021 * For file choosers, the AccessibleContext takes the form of an
2022 * AccessibleJFileChooser.
2023 * A new AccessibleJFileChooser instance is created if necessary.
2024 *
2025 * @return an AccessibleJFileChooser that serves as the
2026 * AccessibleContext of this JFileChooser
2027 */
2028 @BeanProperty(bound = false)
2029 public AccessibleContext getAccessibleContext() {
2030 if (accessibleContext == null) {
2031 accessibleContext = new AccessibleJFileChooser();
2032 }
2033 return accessibleContext;
2034 }
2035
2036 /**
2037 * This class implements accessibility support for the
2038 * <code>JFileChooser</code> class. It provides an implementation of the
2039 * Java Accessibility API appropriate to file chooser user-interface
2040 * elements.
2041 */
2042 @SuppressWarnings("serial") // Superclass is not serializable across versions
2043 protected class AccessibleJFileChooser extends AccessibleJComponent {
2044
2045 /**
2046 * Gets the role of this object.
2047 *
2048 * @return an instance of AccessibleRole describing the role of the
|