private void changeDirectory(File dir) { JFileChooser fc = getFileChooser(); // Traverse shortcuts on Windows if (dir != null && FilePane.usesShellFolder(fc)) { try { ShellFolder shellFolder = ShellFolder.getShellFolder(dir); if (shellFolder.isLink()) { File linkedTo = shellFolder.getLinkLocation(); // If linkedTo is null we try to use dir if (linkedTo != null) { if (fc.isTraversable(linkedTo)) { dir = linkedTo; } else { return; } } else { dir = shellFolder; } } } catch (FileNotFoundException ex) { return; } } fc.setCurrentDirectory(dir); if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES && fc.getFileSystemView().isFileSystem(dir)) { setFileName(dir.getAbsolutePath()); } }
/** Throws {@code FileNotFoundException} if file not found or current thread was interrupted */ ShellFolder getShellFolder(File f) throws FileNotFoundException { if (!(f instanceof ShellFolder) && !(f instanceof FileSystemRoot) && isFileSystemRoot(f)) { f = createFileSystemRoot(f); } try { return ShellFolder.getShellFolder(f); } catch (InternalError e) { System.err.println("FileSystemView.getShellFolder: f=" + f); e.printStackTrace(); return null; } }
private String getSystemDisplayName(File f) { String name; try { name = ShellFolder.getShellFolder(f).getDisplayName(); } catch (FileNotFoundException e) { return null; } if (name == null || name.length() == 0) { name = f.getPath(); // the "/" directory } return name; }
/** Gets the list of shown (i.e. not hidden) files. */ public File[] getFiles(File dir, boolean useFileHiding) { List<File> files = new ArrayList<File>(); // add all files in dir if (!(dir instanceof ShellFolder)) { try { dir = getShellFolder(dir); } catch (FileNotFoundException e) { return new File[0]; } } File[] names = ((ShellFolder) dir).listFiles(!useFileHiding); if (names == null) { return new File[0]; } for (File f : names) { if (Thread.currentThread().isInterrupted()) { break; } if (!(f instanceof ShellFolder)) { if (isFileSystemRoot(f)) { f = createFileSystemRoot(f); } try { f = ShellFolder.getShellFolder(f); } catch (FileNotFoundException e) { // Not a valid file (wouldn't show in native file chooser) // Example: C:\pagefile.sys continue; } catch (InternalError e) { // Not a valid file (wouldn't show in native file chooser) // Example C:\Winnt\Profiles\joe\history\History.IE5 continue; } } if (!useFileHiding || !isHiddenFile(f)) { files.add(f); } } return files.toArray(new File[files.size()]); }