📄 basicfilechooserui.java
字号:
} resetGlobFilter(); // Check for directory change action boolean isDir = (selectedFile != null && selectedFile.isDirectory()); boolean isTrav = (selectedFile != null && chooser.isTraversable(selectedFile)); boolean isDirSelEnabled = chooser.isDirectorySelectionEnabled(); boolean isFileSelEnabled = chooser.isFileSelectionEnabled(); boolean isCtrl = (e != null && (e.getModifiers() & ActionEvent.CTRL_MASK) != 0); if (isDir && isTrav && (isCtrl || !isDirSelEnabled)) { changeDirectory(selectedFile); return; } else if ((isDir || !isFileSelEnabled) && (!isDir || !isDirSelEnabled) && (!isDirSelEnabled || selectedFile.exists())) { selectedFile = null; } } } if (selectedFiles != null || selectedFile != null) { if (selectedFiles != null || chooser.isMultiSelectionEnabled()) { if (selectedFiles == null) { selectedFiles = new File[] { selectedFile }; } chooser.setSelectedFiles(selectedFiles); // Do it again. This is a fix for bug 4949273 to force the // selected value in case the ListSelectionModel clears it // for non-existing file names. chooser.setSelectedFiles(selectedFiles); } else { chooser.setSelectedFile(selectedFile); } chooser.approveSelection(); } else { if (chooser.isMultiSelectionEnabled()) { chooser.setSelectedFiles(null); } else { chooser.setSelectedFile(null); } chooser.cancelSelection(); } } } private void resetGlobFilter() { if (actualFileFilter != null) { JFileChooser chooser = getFileChooser(); FileFilter currentFilter = chooser.getFileFilter(); if (currentFilter != null && currentFilter.equals(globFilter)) { chooser.setFileFilter(actualFileFilter); chooser.removeChoosableFileFilter(globFilter); } actualFileFilter = null; } } private static boolean isGlobPattern(String filename) { return ((File.separatorChar == '\\' && (filename.indexOf('*') >= 0 || filename.indexOf('?') >= 0)) || (File.separatorChar == '/' && (filename.indexOf('*') >= 0 || filename.indexOf('?') >= 0 || filename.indexOf('[') >= 0))); } /* A file filter which accepts file patterns containing * the special wildcards *? on Windows and *?[] on Unix. */ class GlobFilter extends FileFilter { Pattern pattern; String globPattern; public void setPattern(String globPattern) { char[] gPat = globPattern.toCharArray(); char[] rPat = new char[gPat.length * 2]; boolean isWin32 = (File.separatorChar == '\\'); boolean inBrackets = false; int j = 0; this.globPattern = globPattern; if (isWin32) { // On windows, a pattern ending with *.* is equal to ending with * int len = gPat.length; if (globPattern.endsWith("*.*")) { len -= 2; } for (int i = 0; i < len; i++) { switch(gPat[i]) { case '*': rPat[j++] = '.'; rPat[j++] = '*'; break; case '?': rPat[j++] = '.'; break; case '\\': rPat[j++] = '\\'; rPat[j++] = '\\'; break; default: if ("+()^$.{}[]".indexOf(gPat[i]) >= 0) { rPat[j++] = '\\'; } rPat[j++] = gPat[i]; break; } } } else { for (int i = 0; i < gPat.length; i++) { switch(gPat[i]) { case '*': if (!inBrackets) { rPat[j++] = '.'; } rPat[j++] = '*'; break; case '?': rPat[j++] = inBrackets ? '?' : '.'; break; case '[': inBrackets = true; rPat[j++] = gPat[i]; if (i < gPat.length - 1) { switch (gPat[i+1]) { case '!': case '^': rPat[j++] = '^'; i++; break; case ']': rPat[j++] = gPat[++i]; break; } } break; case ']': rPat[j++] = gPat[i]; inBrackets = false; break; case '\\': if (i == 0 && gPat.length > 1 && gPat[1] == '~') { rPat[j++] = gPat[++i]; } else { rPat[j++] = '\\'; if (i < gPat.length - 1 && "*?[]".indexOf(gPat[i+1]) >= 0) { rPat[j++] = gPat[++i]; } else { rPat[j++] = '\\'; } } break; default: //if ("+()|^$.{}<>".indexOf(gPat[i]) >= 0) { if (!Character.isLetterOrDigit(gPat[i])) { rPat[j++] = '\\'; } rPat[j++] = gPat[i]; break; } } } this.pattern = Pattern.compile(new String(rPat, 0, j), Pattern.CASE_INSENSITIVE); } public boolean accept(File f) { if (f == null) { return false; } if (f.isDirectory()) { return true; } return pattern.matcher(f.getName()).matches(); } public String getDescription() { return globPattern; } } /** * Responds to a cancel request. */ protected class CancelSelectionAction extends AbstractAction { public void actionPerformed(ActionEvent e) { getFileChooser().cancelSelection(); } } /** * Rescans the files in the current directory */ protected class UpdateAction extends AbstractAction { public void actionPerformed(ActionEvent e) { JFileChooser fc = getFileChooser(); fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName())); fc.rescanCurrentDirectory(); } } private void changeDirectory(File dir) { JFileChooser fc = getFileChooser(); // Traverse shortcuts on Windows if (dir != null) { try { ShellFolder shellFolder = ShellFolder.getShellFolder(dir); if (shellFolder.isLink()) { File linkedTo = shellFolder.getLinkLocation(); if (linkedTo != null && fc.isTraversable(linkedTo)) { dir = linkedTo; } else { return; } } } catch (FileNotFoundException ex) { return; } } fc.setCurrentDirectory(dir); if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES && fc.getFileSystemView().isFileSystem(dir)) { setFileName(dir.getAbsolutePath()); } } // ***************************************** // ***** default AcceptAll file filter ***** // ***************************************** protected class AcceptAllFileFilter extends FileFilter { public AcceptAllFileFilter() { } public boolean accept(File f) { return true; } public String getDescription() { return UIManager.getString("FileChooser.acceptAllFileFilterText"); } } // *********************** // * FileView operations * // *********************** protected class BasicFileView extends FileView { /* FileView type descriptions */ // PENDING(jeff) - pass in the icon cache size protected Hashtable<File,Icon> iconCache = new Hashtable<File,Icon>(); public BasicFileView() { } public void clearIconCache() { iconCache = new Hashtable<File,Icon>(); } public String getName(File f) { // Note: Returns display name rather than file name String fileName = null; if(f != null) { fileName = getFileChooser().getFileSystemView().getSystemDisplayName(f); } return fileName; } public String getDescription(File f) { return f.getName(); } public String getTypeDescription(File f) { String type = getFileChooser().getFileSystemView().getSystemTypeDescription(f); if (type == null) { if (f.isDirectory()) { type = directoryDescriptionText; } else { type = fileDescriptionText; } } return type; } public Icon getCachedIcon(File f) { return (Icon) iconCache.get(f); } public void cacheIcon(File f, Icon i) { if(f == null || i == null) { return; } iconCache.put(f, i); } public Icon getIcon(File f) { Icon icon = getCachedIcon(f); if(icon != null) { return icon; } icon = fileIcon; if (f != null) { FileSystemView fsv = getFileChooser().getFileSystemView(); if (fsv.isFloppyDrive(f)) { icon = floppyDriveIcon; } else if (fsv.isDrive(f)) { icon = hardDriveIcon; } else if (fsv.isComputerNode(f)) { icon = computerIcon; } else if (f.isDirectory()) { icon = directoryIcon; } } cacheIcon(f, icon); return icon; } public Boolean isHidden(File f) { String name = f.getName(); if(name != null && name.charAt(0) == '.') { return Boolean.TRUE; } else { return Boolean.FALSE; } } } private static final TransferHandler defaultTransferHandler = new FileTransferHandler(); /** * Data transfer support for the file chooser. Since files are currently presented * as a list, the list support is reused with the added flavor of DataFlavor.javaFileListFlavor */ static class FileTransferHandler extends TransferHandler implements UIResource { /** * Create a Transferable to use as the source for a data transfer. * * @param c The component holding the data to be transfered. This * argument is provided to enable sharing of TransferHandlers by * multiple components. * @return The representation of the data to be transfered. * */ protected Transferable createTransferable(JComponent c) { Object[] values = null; if (c instanceof JList) { values = ((JList)c).getSelectedValues(); } else if (c instanceof JTable) { JTable table = (JTable)c; int[] rows = table.getSelectedRows(); if (rows != null) { values = new Object[rows.length]; for (int i=0; i<rows.length; i++) { values[i] = table.getValueAt(rows[i], 0); } } } if (values == null || values.length == 0) { return null; } StringBuffer plainBuf = new StringBuffer(); StringBuffer htmlBuf = new StringBuffer(); htmlBuf.append("<html>\n<body>\n<ul>\n"); for (int i = 0; i < values.length; i++) { Object obj = values[i]; String val = ((obj == null) ? "" : obj.toString()); plainBuf.append(val + "\n"); htmlBuf.append(" <li>" + val + "\n"); } // remove the last newline plainBuf.deleteCharAt(plainBuf.length() - 1); htmlBuf.append("</ul>\n</body>\n</html>"); return new FileTransferable(plainBuf.toString(), htmlBuf.toString(), values); } public int getSourceActions(JComponent c) { return COPY; } static class FileTransferable extends BasicTransferable { Object[] fileData; FileTransferable(String plainData, String htmlData, Object[] fileData) { super(plainData, htmlData); this.fileData = fileData; } /** * Best format of the file chooser is DataFlavor.javaFileListFlavor. */ protected DataFlavor[] getRicherFlavors() { DataFlavor[] flavors = new DataFlavor[1]; flavors[0] = DataFlavor.javaFileListFlavor; return flavors; } /** * The only richer format supported is the file list flavor */ protected Object getRicherData(DataFlavor flavor) { if (DataFlavor.javaFileListFlavor.equals(flavor)) { ArrayList files = new ArrayList(); for (int i = 0; i < fileData.length; i++) { files.add(fileData[i]); } return files; } return null; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -