1 7 8 package javax.swing; 9 10 import javax.swing.event.*; 11 import javax.swing.filechooser.*; 12 import javax.swing.plaf.FileChooserUI ; 13 14 import javax.accessibility.*; 15 16 import java.io.File ; 17 import java.io.ObjectOutputStream ; 18 import java.io.ObjectInputStream ; 19 import java.io.IOException ; 20 21 import java.util.Vector ; 22 import java.awt.AWTEvent ; 23 import java.awt.Component ; 24 import java.awt.Container ; 25 import java.awt.BorderLayout ; 26 import java.awt.Window ; 27 import java.awt.Dialog ; 28 import java.awt.Frame ; 29 import java.awt.GraphicsEnvironment ; 30 import java.awt.HeadlessException ; 31 import java.awt.EventQueue ; 32 import java.awt.Toolkit ; 33 import java.awt.event.*; 34 import java.beans.PropertyChangeListener ; 35 import java.beans.PropertyChangeEvent ; 36 import java.lang.ref.WeakReference ; 37 38 74 public class JFileChooser extends JComponent implements Accessible { 75 76 80 private static final String uiClassID = "FileChooserUI"; 81 82 86 90 public static final int OPEN_DIALOG = 0; 91 92 96 public static final int SAVE_DIALOG = 1; 97 98 102 public static final int CUSTOM_DIALOG = 2; 103 104 105 109 112 public static final int CANCEL_OPTION = 1; 113 114 117 public static final int APPROVE_OPTION = 0; 118 119 122 public static final int ERROR_OPTION = -1; 123 124 125 129 130 131 public static final int FILES_ONLY = 0; 132 133 134 public static final int DIRECTORIES_ONLY = 1; 135 136 137 public static final int FILES_AND_DIRECTORIES = 2; 138 139 140 public static final String CANCEL_SELECTION = "CancelSelection"; 141 142 146 public static final String APPROVE_SELECTION = "ApproveSelection"; 147 148 149 public static final String APPROVE_BUTTON_TEXT_CHANGED_PROPERTY = "ApproveButtonTextChangedProperty"; 150 151 155 public static final String APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY = "ApproveButtonToolTipTextChangedProperty"; 156 157 158 public static final String APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY = "ApproveButtonMnemonicChangedProperty"; 159 160 161 public static final String CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY = "ControlButtonsAreShownChangedProperty"; 162 163 164 public static final String DIRECTORY_CHANGED_PROPERTY = "directoryChanged"; 165 166 167 public static final String SELECTED_FILE_CHANGED_PROPERTY = "SelectedFileChangedProperty"; 168 169 170 public static final String SELECTED_FILES_CHANGED_PROPERTY = "SelectedFilesChangedProperty"; 171 172 173 public static final String MULTI_SELECTION_ENABLED_CHANGED_PROPERTY = "MultiSelectionEnabledChangedProperty"; 174 175 179 public static final String FILE_SYSTEM_VIEW_CHANGED_PROPERTY = "FileSystemViewChanged"; 180 181 185 public static final String FILE_VIEW_CHANGED_PROPERTY = "fileViewChanged"; 186 187 188 public static final String FILE_HIDING_CHANGED_PROPERTY = "FileHidingChanged"; 189 190 191 public static final String FILE_FILTER_CHANGED_PROPERTY = "fileFilterChanged"; 192 193 197 public static final String FILE_SELECTION_MODE_CHANGED_PROPERTY = "fileSelectionChanged"; 198 199 203 public static final String ACCESSORY_CHANGED_PROPERTY = "AccessoryChangedProperty"; 204 205 208 public static final String ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY = "acceptAllFileFilterUsedChanged"; 209 210 211 public static final String DIALOG_TITLE_CHANGED_PROPERTY = "DialogTitleChangedProperty"; 212 213 217 public static final String DIALOG_TYPE_CHANGED_PROPERTY = "DialogTypeChangedProperty"; 218 219 223 public static final String CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY = "ChoosableFileFilterChangedProperty"; 224 225 229 private String dialogTitle = null; 230 private String approveButtonText = null; 231 private String approveButtonToolTipText = null; 232 private int approveButtonMnemonic = 0; 233 234 private ActionListener actionListener = null; 235 236 private Vector filters = new Vector (5); 237 private JDialog dialog = null; 238 private int dialogType = OPEN_DIALOG; 239 private int returnValue = ERROR_OPTION; 240 private JComponent accessory = null; 241 242 private FileView fileView = null; 243 244 private transient FileView uiFileView = null; 247 248 private boolean controlsShown = true; 249 250 private boolean useFileHiding = true; 251 private static final String SHOW_HIDDEN_PROP = "awt.file.showHiddenFiles"; 252 253 private PropertyChangeListener showFilesListener = null; 257 258 private int fileSelectionMode = FILES_ONLY; 259 260 private boolean multiSelectionEnabled = false; 261 262 private boolean useAcceptAllFileFilter = true; 263 264 private boolean dragEnabled = false; 265 266 private FileFilter fileFilter = null; 267 268 private FileSystemView fileSystemView = null; 269 270 private File currentDirectory = null; 271 private File selectedFile = null; 272 private File [] selectedFiles; 273 274 278 284 public JFileChooser() { 285 this((File ) null, (FileSystemView) null); 286 } 287 288 299 public JFileChooser(String currentDirectoryPath) { 300 this(currentDirectoryPath, (FileSystemView) null); 301 } 302 303 314 public JFileChooser(File currentDirectory) { 315 this(currentDirectory, (FileSystemView) null); 316 } 317 318 322 public JFileChooser(FileSystemView fsv) { 323 this((File ) null, fsv); 324 } 325 326 327 331 public JFileChooser(File currentDirectory, FileSystemView fsv) { 332 setup(fsv); 333 setCurrentDirectory(currentDirectory); 334 } 335 336 340 public JFileChooser(String currentDirectoryPath, FileSystemView fsv) { 341 setup(fsv); 342 if(currentDirectoryPath == null) { 343 setCurrentDirectory(null); 344 } else { 345 setCurrentDirectory(fileSystemView.createFileObject(currentDirectoryPath)); 346 } 347 } 348 349 352 protected void setup(FileSystemView view) { 353 Toolkit tk = Toolkit.getDefaultToolkit(); 355 Object showHiddenProperty = tk.getDesktopProperty(SHOW_HIDDEN_PROP); 356 if (showHiddenProperty instanceof Boolean ) { 357 useFileHiding = !((Boolean )showHiddenProperty).booleanValue(); 358 showFilesListener = new WeakPCL(this); 359 tk.addPropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener); 360 } 361 362 if(view == null) { 363 view = FileSystemView.getFileSystemView(); 364 } 365 setFileSystemView(view); 366 updateUI(); 367 if(isAcceptAllFileFilterUsed()) { 368 setFileFilter(getAcceptAllFileFilter()); 369 } 370 } 371 372 416 public void setDragEnabled(boolean b) { 417 if (b && GraphicsEnvironment.isHeadless()) { 418 throw new HeadlessException (); 419 } 420 dragEnabled = b; 421 } 422 423 430 public boolean getDragEnabled() { 431 return dragEnabled; 432 } 433 434 438 447 public File getSelectedFile() { 448 return selectedFile; 449 } 450 451 464 public void setSelectedFile(File file) { 465 File oldValue = selectedFile; 466 selectedFile = file; 467 if(selectedFile != null) { 468 if (file.isAbsolute() && !getFileSystemView().isParent(getCurrentDirectory(), selectedFile)) { 469 setCurrentDirectory(selectedFile.getParentFile()); 470 } 471 if (!isMultiSelectionEnabled() || selectedFiles == null || selectedFiles.length == 1) { 472 ensureFileIsVisible(selectedFile); 473 } 474 } 475 firePropertyChange(SELECTED_FILE_CHANGED_PROPERTY, oldValue, selectedFile); 476 } 477 478 482 public File [] getSelectedFiles() { 483 if(selectedFiles == null) { 484 return new File [0]; 485 } else { 486 return (File []) selectedFiles.clone(); 487 } 488 } 489 490 498 public void setSelectedFiles(File [] selectedFiles) { 499 File [] oldValue = this.selectedFiles; 500 if (selectedFiles != null && selectedFiles.length == 0) { 501 selectedFiles = null; 502 } 503 this.selectedFiles = selectedFiles; 504 setSelectedFile((selectedFiles != null) ? selectedFiles[0] : null); 505 firePropertyChange(SELECTED_FILES_CHANGED_PROPERTY, oldValue, this.selectedFiles); 506 } 507 508 514 public File getCurrentDirectory() { 515 return currentDirectory; 516 } 517 518 539 public void setCurrentDirectory(File dir) { 540 File oldValue = currentDirectory; 541 542 if (dir != null && !dir.exists()) { 543 dir = currentDirectory; 544 } 545 if (dir == null) { 546 dir = getFileSystemView().getDefaultDirectory(); 547 } 548 if (currentDirectory != null) { 549 550 if (this.currentDirectory.equals(dir)) { 551 return; 552 } 553 } 554 555 File prev = null; 556 while (!isTraversable(dir) && prev != dir) { 557 prev = dir; 558 dir = getFileSystemView().getParentDirectory(dir); 559 } 560 currentDirectory = dir; 561 562 firePropertyChange(DIRECTORY_CHANGED_PROPERTY, oldValue, currentDirectory); 563 } 564 565 571 public void changeToParentDirectory() { 572 selectedFile = null; 573 File oldValue = getCurrentDirectory(); 574 setCurrentDirectory(getFileSystemView().getParentDirectory(oldValue)); 575 } 576 577 580 public void rescanCurrentDirectory() { 581 getUI().rescanCurrentDirectory(this); 582 } 583 584 590 public void ensureFileIsVisible(File f) { 591 getUI().ensureFileIsVisible(this, f); 592 } 593 594 598 618 public int showOpenDialog(Component parent) throws HeadlessException { 619 setDialogType(OPEN_DIALOG); 620 return showDialog(parent, null); 621 } 622 623 643 public int showSaveDialog(Component parent) throws HeadlessException { 644 setDialogType(SAVE_DIALOG); 645 return showDialog(parent, null); 646 } 647 648 702 public int showDialog(Component parent, String approveButtonText) 703 throws HeadlessException { 704 if(approveButtonText != null) { 705 setApproveButtonText(approveButtonText); 706 setDialogType(CUSTOM_DIALOG); 707 } 708 dialog = createDialog(parent); 709 dialog.addWindowListener(new WindowAdapter() { 710 public void windowClosing(WindowEvent e) { 711 returnValue = CANCEL_OPTION; 712 } 713 }); 714 returnValue = ERROR_OPTION; 715 rescanCurrentDirectory(); 716 717 dialog.show(); 718 firePropertyChange("JFileChooserDialogIsClosingProperty", dialog, null); 719 dialog.removeAll(); 720 dialog.dispose(); 721 dialog = null; 722 return returnValue; 723 } 724 725 750 protected JDialog createDialog(Component parent) throws HeadlessException { 751 String title = getUI().getDialogTitle(this); 752 getAccessibleContext().setAccessibleDescription(title); 753 754 JDialog dialog; 755 Window window = JOptionPane.getWindowForComponent(parent); 756 if (window instanceof Frame ) { 757 dialog = new JDialog ((Frame )window, title, true); 758 } else { 759 dialog = new JDialog ((Dialog )window, title, true); 760 } 761 dialog.setComponentOrientation(this.getComponentOrientation()); 762 763 Container contentPane = dialog.getContentPane(); 764 contentPane.setLayout(new BorderLayout ()); 765 contentPane.add(this, BorderLayout.CENTER); 766 767 if (JDialog.isDefaultLookAndFeelDecorated()) { 768 boolean supportsWindowDecorations = 769 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 770 if (supportsWindowDecorations) { 771 dialog.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG); 772 } 773 } 774 dialog.pack(); 775 dialog.setLocationRelativeTo(parent); 776 777 return dialog; 778 } 779 780 784 794 public boolean getControlButtonsAreShown() { 795 return controlsShown; 796 } 797 798 799 823 public void setControlButtonsAreShown(boolean b) { 824 if(controlsShown == b) { 825 return; 826 } 827 boolean oldValue = controlsShown; 828 controlsShown = b; 829 firePropertyChange(CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY, oldValue, controlsShown); 830 } 831 832 845 public int getDialogType() { 846 return dialogType; 847 } 848 849 884 public void setDialogType(int dialogType) { 886 if(this.dialogType == dialogType) { 887 return; 888 } 889 if(!(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG || dialogType == CUSTOM_DIALOG)) { 890 throw new IllegalArgumentException ("Incorrect Dialog Type: " + dialogType); 891 } 892 int oldValue = this.dialogType; 893 this.dialogType = dialogType; 894 if(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG) { 895 setApproveButtonText(null); 896 } 897 firePropertyChange(DIALOG_TYPE_CHANGED_PROPERTY, oldValue, dialogType); 898 } 899 900 914 public void setDialogTitle(String dialogTitle) { 915 String oldValue = this.dialogTitle; 916 this.dialogTitle = dialogTitle; 917 if(dialog != null) { 918 dialog.setTitle(dialogTitle); 919 } 920 firePropertyChange(DIALOG_TITLE_CHANGED_PROPERTY, oldValue, dialogTitle); 921 } 922 923 928 public String getDialogTitle() { 929 return dialogTitle; 930 } 931 932 936 937 938 953 public void setApproveButtonToolTipText(String toolTipText) { 954 if(approveButtonToolTipText == toolTipText) { 955 return; 956 } 957 String oldValue = approveButtonToolTipText; 958 approveButtonToolTipText = toolTipText; 959 firePropertyChange(APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY, oldValue, approveButtonToolTipText); 960 } 961 962 963 973 public String getApproveButtonToolTipText() { 974 return approveButtonToolTipText; 975 } 976 977 983 public int getApproveButtonMnemonic() { 984 return approveButtonMnemonic; 985 } 986 987 999 public void setApproveButtonMnemonic(int mnemonic) { 1000 if(approveButtonMnemonic == mnemonic) { 1001 return; 1002 } 1003 int oldValue = approveButtonMnemonic; 1004 approveButtonMnemonic = mnemonic; 1005 firePropertyChange(APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY, oldValue, approveButtonMnemonic); 1006 } 1007 1008 1014 public void setApproveButtonMnemonic(char mnemonic) { 1015 int vk = (int) mnemonic; 1016 if(vk >= 'a' && vk <='z') { 1017 vk -= ('a' - 'A'); 1018 } 1019 setApproveButtonMnemonic(vk); 1020 } 1021 1022 1023 1038 public void setApproveButtonText(String approveButtonText) { 1040 if(this.approveButtonText == approveButtonText) { 1041 return; 1042 } 1043 String oldValue = this.approveButtonText; 1044 this.approveButtonText = approveButtonText; 1045 firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, oldValue, approveButtonText); 1046 } 1047 1048 1061 public String getApproveButtonText() { 1062 return approveButtonText; 1063 } 1064 1065 1075 public FileFilter[] getChoosableFileFilters() { 1076 FileFilter[] filterArray = new FileFilter[filters.size()]; 1077 filters.copyInto(filterArray); 1078 return filterArray; 1079 } 1080 1081 1099 public void addChoosableFileFilter(FileFilter filter) { 1100 if(filter != null && !filters.contains(filter)) { 1101 FileFilter[] oldValue = getChoosableFileFilters(); 1102 filters.addElement(filter); 1103 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters()); 1104 } 1105 setFileFilter(filter); 1106 } 1107 1108 1116 public boolean removeChoosableFileFilter(FileFilter f) { 1117 if(filters.contains(f)) { 1118 if(getFileFilter() == f) { 1119 setFileFilter(null); 1120 } 1121 FileFilter[] oldValue = getChoosableFileFilters(); 1122 filters.removeElement(f); 1123 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters()); 1124 return true; 1125 } else { 1126 return false; 1127 } 1128 } 1129 1130 1139 public void resetChoosableFileFilters() { 1140 FileFilter[] oldValue = getChoosableFileFilters(); 1141 setFileFilter(null); 1142 filters.removeAllElements(); 1143 if(isAcceptAllFileFilterUsed()) { 1144 addChoosableFileFilter(getAcceptAllFileFilter()); 1145 } 1146 firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters()); 1147 } 1148 1149 1153 public FileFilter getAcceptAllFileFilter() { 1154 FileFilter filter = null; 1155 if(getUI() != null) { 1156 filter = getUI().getAcceptAllFileFilter(this); 1157 } 1158 return filter; 1159 } 1160 1161 1167 public boolean isAcceptAllFileFilterUsed() { 1168 return useAcceptAllFileFilter; 1169 } 1170 1171 1189 public void setAcceptAllFileFilterUsed(boolean b) { 1190 boolean oldValue = useAcceptAllFileFilter; 1191 useAcceptAllFileFilter = b; 1192 if(!b) { 1193 removeChoosableFileFilter(getAcceptAllFileFilter()); 1194 } else { 1195 removeChoosableFileFilter(getAcceptAllFileFilter()); 1196 addChoosableFileFilter(getAcceptAllFileFilter()); 1197 } 1198 firePropertyChange(ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY, oldValue, useAcceptAllFileFilter); 1199 } 1200 1201 1207 public JComponent getAccessory() { 1208 return accessory; 1209 } 1210 1211 1226 public void setAccessory(JComponent newAccessory) { 1227 JComponent oldValue = accessory; 1228 accessory = newAccessory; 1229 firePropertyChange(ACCESSORY_CHANGED_PROPERTY, oldValue, accessory); 1230 } 1231 1232 1258 public void setFileSelectionMode(int mode) { 1259 if(fileSelectionMode == mode) { 1260 return; 1261 } 1262 1263 if ((mode == FILES_ONLY) || (mode == DIRECTORIES_ONLY) || (mode == FILES_AND_DIRECTORIES)) { 1264 int oldValue = fileSelectionMode; 1265 fileSelectionMode = mode; 1266 firePropertyChange(FILE_SELECTION_MODE_CHANGED_PROPERTY, oldValue, fileSelectionMode); 1267 } else { 1268 throw new IllegalArgumentException ("Incorrect Mode for file selection: " + mode); 1269 } 1270 } 1271 1272 1284 public int getFileSelectionMode() { 1285 return fileSelectionMode; 1286 } 1287 1288 1295 public boolean isFileSelectionEnabled() { 1296 return ((fileSelectionMode == FILES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES)); 1297 } 1298 1299 1306 public boolean isDirectorySelectionEnabled() { 1307 return ((fileSelectionMode == DIRECTORIES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES)); 1308 } 1309 1310 1320 public void setMultiSelectionEnabled(boolean b) { 1321 if(multiSelectionEnabled == b) { 1322 return; 1323 } 1324 boolean oldValue = multiSelectionEnabled; 1325 multiSelectionEnabled = b; 1326 firePropertyChange(MULTI_SELECTION_ENABLED_CHANGED_PROPERTY, oldValue, multiSelectionEnabled); 1327 } 1328 1329 1334 public boolean isMultiSelectionEnabled() { 1335 return multiSelectionEnabled; 1336 } 1337 1338 1339 1346 public boolean isFileHidingEnabled() { 1347 return useFileHiding; 1348 } 1349 1350 1364 public void setFileHidingEnabled(boolean b) { 1365 if (showFilesListener != null) { 1367 Toolkit.getDefaultToolkit().removePropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener); 1368 showFilesListener = null; 1369 } 1370 boolean oldValue = useFileHiding; 1371 useFileHiding = b; 1372 firePropertyChange(FILE_HIDING_CHANGED_PROPERTY, oldValue, useFileHiding); 1373 } 1374 1375 1387 public void setFileFilter(FileFilter filter) { 1388 FileFilter oldValue = fileFilter; 1389 fileFilter = filter; 1390 if (filter != null) { 1391 if (isMultiSelectionEnabled() && selectedFiles != null && selectedFiles.length > 0) { 1392 Vector fList = new Vector (); 1393 boolean failed = false; 1394 for (int i = 0; i < selectedFiles.length; i++) { 1395 if (filter.accept(selectedFiles[i])) { 1396 fList.add(selectedFiles[i]); 1397 } else { 1398 failed = true; 1399 } 1400 } 1401 if (failed) { 1402 setSelectedFiles((fList.size() == 0) ? null : (File [])fList.toArray(new File [fList.size()])); 1403 } 1404 } else if (selectedFile != null && !filter.accept(selectedFile)) { 1405 setSelectedFile(null); 1406 } 1407 } 1408 firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, oldValue, fileFilter); 1409 } 1410 1411 1412 1419 public FileFilter getFileFilter() { 1420 return fileFilter; 1421 } 1422 1423 1434 public void setFileView(FileView fileView) { 1435 FileView oldValue = this.fileView; 1436 this.fileView = fileView; 1437 firePropertyChange(FILE_VIEW_CHANGED_PROPERTY, oldValue, fileView); 1438 } 1439 1440 1445 public FileView getFileView() { 1446 return fileView; 1447 } 1448 1449 1453 1458 1465 public String getName(File f) { 1466 String filename = null; 1467 if(f != null) { 1468 if(getFileView() != null) { 1469 filename = getFileView().getName(f); 1470 } 1471 if(filename == null && uiFileView != null) { 1472 filename = uiFileView.getName(f); 1473 } 1474 } 1475 return filename; 1476 } 1477 1478 1485 public String getDescription(File f) { 1486 String description = null; 1487 if(f != null) { 1488 if(getFileView() != null) { 1489 description = getFileView().getDescription(f); 1490 } 1491 if(description == null && uiFileView != null) { 1492 description = uiFileView.getDescription(f); 1493 } 1494 } 1495 return description; 1496 } 1497 1498 1505 public String getTypeDescription(File f) { 1506 String typeDescription = null; 1507 if(f != null) { 1508 if(getFileView() != null) { 1509 typeDescription = getFileView().getTypeDescription(f); 1510 } 1511 if(typeDescription == null && uiFileView != null) { 1512 typeDescription = uiFileView.getTypeDescription(f); 1513 } 1514 } 1515 return typeDescription; 1516 } 1517 1518 1525 public Icon getIcon(File f) { 1526 Icon icon = null; 1527 if (f != null) { 1528 if(getFileView() != null) { 1529 icon = getFileView().getIcon(f); 1530 } 1531 if(icon == null && uiFileView != null) { 1532 icon = uiFileView.getIcon(f); 1533 } 1534 } 1535 return icon; 1536 } 1537 1538 1545 public boolean isTraversable(File f) { 1546 Boolean traversable = null; 1547 if (f != null) { 1548 if (getFileView() != null) { 1549 traversable = getFileView().isTraversable(f); 1550 } 1551 if (traversable == null && uiFileView != null) { 1552 traversable = uiFileView.isTraversable(f); 1553 } 1554 if (traversable == null) { 1555 traversable = getFileSystemView().isTraversable(f); 1556 } 1557 } 1558 return (traversable != null && traversable.booleanValue()); 1559 } 1560 1561 1567 public boolean accept(File f) { 1568 boolean shown = true; 1569 if(f != null && fileFilter != null) { 1570 shown = fileFilter.accept(f); 1571 } 1572 return shown; 1573 } 1574 1575 1588 public void setFileSystemView(FileSystemView fsv) { 1589 FileSystemView oldValue = fileSystemView; 1590 fileSystemView = fsv; 1591 firePropertyChange(FILE_SYSTEM_VIEW_CHANGED_PROPERTY, oldValue, fileSystemView); 1592 } 1593 1594 1599 public FileSystemView getFileSystemView() { 1600 return fileSystemView; 1601 } 1602 1603 1607 1617 public void approveSelection() { 1618 returnValue = APPROVE_OPTION; 1619 if(dialog != null) { 1620 dialog.setVisible(false); 1621 } 1622 fireActionPerformed(APPROVE_SELECTION); 1623 } 1624 1625 1634 public void cancelSelection() { 1635 returnValue = CANCEL_OPTION; 1636 if(dialog != null) { 1637 dialog.setVisible(false); 1638 } 1639 fireActionPerformed(CANCEL_SELECTION); 1640 } 1641 1642 1650 public void addActionListener(ActionListener l) { 1651 listenerList.add(ActionListener.class, l); 1652 } 1653 1654 1661 public void removeActionListener(ActionListener l) { 1662 listenerList.remove(ActionListener.class, l); 1663 } 1664 1665 1678 public ActionListener[] getActionListeners() { 1679 return (ActionListener[])listenerList.getListeners( 1680 ActionListener.class); 1681 } 1682 1683 1690 protected void fireActionPerformed(String command) { 1691 Object [] listeners = listenerList.getListenerList(); 1693 long mostRecentEventTime = EventQueue.getMostRecentEventTime(); 1694 int modifiers = 0; 1695 AWTEvent currentEvent = EventQueue.getCurrentEvent(); 1696 if (currentEvent instanceof InputEvent) { 1697 modifiers = ((InputEvent)currentEvent).getModifiers(); 1698 } else if (currentEvent instanceof ActionEvent) { 1699 modifiers = ((ActionEvent)currentEvent).getModifiers(); 1700 } 1701 ActionEvent e = null; 1702 for (int i = listeners.length-2; i>=0; i-=2) { 1705 if (listeners[i]==ActionListener.class) { 1706 if (e == null) { 1708 e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, 1709 command, mostRecentEventTime, 1710 modifiers); 1711 } 1712 ((ActionListener)listeners[i+1]).actionPerformed(e); 1713 } 1714 } 1715 } 1716 1717 private static class WeakPCL implements PropertyChangeListener { 1718 WeakReference <JFileChooser > jfcRef; 1719 1720 public WeakPCL(JFileChooser jfc) { 1721 jfcRef = new WeakReference (jfc); 1722 } 1723 public void propertyChange(PropertyChangeEvent ev) { 1724 assert ev.getPropertyName().equals(SHOW_HIDDEN_PROP); 1725 JFileChooser jfc = jfcRef.get(); 1726 if (jfc == null) { 1727 Toolkit.getDefaultToolkit().removePropertyChangeListener(SHOW_HIDDEN_PROP, this); 1730 } 1731 else { 1732 boolean oldValue = jfc.useFileHiding; 1733 jfc.useFileHiding = !((Boolean )ev.getNewValue()).booleanValue(); 1734 jfc.firePropertyChange(FILE_HIDING_CHANGED_PROPERTY, oldValue, jfc.useFileHiding); 1735 } 1736 } 1737 } 1738 1739 1743 1748 public void updateUI() { 1749 if (isAcceptAllFileFilterUsed()) { 1750 removeChoosableFileFilter(getAcceptAllFileFilter()); 1751 } 1752 FileChooserUI ui = ((FileChooserUI )UIManager.getUI(this)); 1753 if (fileSystemView == null) { 1754 setFileSystemView(FileSystemView.getFileSystemView()); 1756 } 1757 setUI(ui); 1758 1759 uiFileView = getUI().getFileView(this); 1760 if(isAcceptAllFileFilterUsed()) { 1761 addChoosableFileFilter(getAcceptAllFileFilter()); 1762 } 1763 } 1764 1765 1776 public String getUIClassID() { 1777 return uiClassID; 1778 } 1779 1780 1785 public FileChooserUI getUI() { 1786 return (FileChooserUI ) ui; 1787 } 1788 1789 1794 private void writeObject(ObjectOutputStream s) throws IOException { 1795 FileSystemView fsv = null; 1796 1797 if (isAcceptAllFileFilterUsed()) { 1798 removeChoosableFileFilter(getAcceptAllFileFilter()); 1801 } 1802 if (fileSystemView.equals(FileSystemView.getFileSystemView())) { 1803 fsv = fileSystemView; 1806 fileSystemView = null; 1807 } 1808 s.defaultWriteObject(); 1809 if (fsv != null) { 1810 fileSystemView = fsv; 1811 } 1812 if (isAcceptAllFileFilterUsed()) { 1813 addChoosableFileFilter(getAcceptAllFileFilter()); 1814 } 1815 if (getUIClassID().equals(uiClassID)) { 1816 byte count = JComponent.getWriteObjCounter(this); 1817 JComponent.setWriteObjCounter(this, --count); 1818 if (count == 0 && ui != null) { 1819 ui.installUI(this); 1820 } 1821 } 1822 } 1823 1824 1825 1835 protected String paramString() { 1836 String approveButtonTextString = (approveButtonText != null ? 1837 approveButtonText: ""); 1838 String dialogTitleString = (dialogTitle != null ? 1839 dialogTitle: ""); 1840 String dialogTypeString; 1841 if (dialogType == OPEN_DIALOG) { 1842 dialogTypeString = "OPEN_DIALOG"; 1843 } else if (dialogType == SAVE_DIALOG) { 1844 dialogTypeString = "SAVE_DIALOG"; 1845 } else if (dialogType == CUSTOM_DIALOG) { 1846 dialogTypeString = "CUSTOM_DIALOG"; 1847 } else dialogTypeString = ""; 1848 String returnValueString; 1849 if (returnValue == CANCEL_OPTION) { 1850 returnValueString = "CANCEL_OPTION"; 1851 } else if (returnValue == APPROVE_OPTION) { 1852 returnValueString = "APPROVE_OPTION"; 1853 } else if (returnValue == ERROR_OPTION) { 1854 returnValueString = "ERROR_OPTION"; 1855 } else returnValueString = ""; 1856 String useFileHidingString = (useFileHiding ? 1857 "true" : "false"); 1858 String fileSelectionModeString; 1859 if (fileSelectionMode == FILES_ONLY) { 1860 fileSelectionModeString = "FILES_ONLY"; 1861 } else if (fileSelectionMode == DIRECTORIES_ONLY) { 1862 fileSelectionModeString = "DIRECTORIES_ONLY"; 1863 } else if (fileSelectionMode == FILES_AND_DIRECTORIES) { 1864 fileSelectionModeString = "FILES_AND_DIRECTORIES"; 1865 } else fileSelectionModeString = ""; 1866 String currentDirectoryString = (currentDirectory != null ? 1867 currentDirectory.toString() : ""); 1868 String selectedFileString = (selectedFile != null ? 1869 selectedFile.toString() : ""); 1870 1871 return super.paramString() + 1872 ",approveButtonText=" + approveButtonTextString + 1873 ",currentDirectory=" + currentDirectoryString + 1874 ",dialogTitle=" + dialogTitleString + 1875 ",dialogType=" + dialogTypeString + 1876 ",fileSelectionMode=" + fileSelectionModeString + 1877 ",returnValue=" + returnValueString + 1878 ",selectedFile=" + selectedFileString + 1879 ",useFileHiding=" + useFileHidingString; 1880 } 1881 1882 1886 protected AccessibleContext accessibleContext = null; 1887 1888 1897 public AccessibleContext getAccessibleContext() { 1898 if (accessibleContext == null) { 1899 accessibleContext = new AccessibleJFileChooser(); 1900 } 1901 return accessibleContext; 1902 } 1903 1904 1910 protected class AccessibleJFileChooser extends AccessibleJComponent { 1911 1912 1919 public AccessibleRole getAccessibleRole() { 1920 return AccessibleRole.FILE_CHOOSER; 1921 } 1922 1923 } 1925} 1926 | Popular Tags |