📄 basicfilechooserui.java
字号:
TransferHandler th2 = list.getTransferHandler(); if (th1 != th2) { list.setTransferHandler(th1); } if (getFileChooser().getDragEnabled() != list.getDragEnabled()) { list.setDragEnabled(getFileChooser().getDragEnabled()); } } } public void mouseExited(MouseEvent evt) { } public void mousePressed(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void valueChanged(ListSelectionEvent evt) { if(!evt.getValueIsAdjusting()) { JFileChooser chooser = getFileChooser(); FileSystemView fsv = chooser.getFileSystemView(); JList list = (JList)evt.getSource(); int fsm = chooser.getFileSelectionMode(); boolean useSetDirectory = usesSingleFilePane && (fsm == JFileChooser.FILES_ONLY); if (chooser.isMultiSelectionEnabled()) { File[] files = null; Object[] objects = list.getSelectedValues(); if (objects != null) { if (objects.length == 1 && ((File)objects[0]).isDirectory() && chooser.isTraversable(((File)objects[0])) && (useSetDirectory || !fsv.isFileSystem(((File)objects[0])))) { setDirectorySelected(true); setDirectory(((File)objects[0])); } else { ArrayList fList = new ArrayList(objects.length); for (int i = 0; i < objects.length; i++) { File f = (File)objects[i]; boolean isDir = f.isDirectory(); if ((chooser.isFileSelectionEnabled() && !isDir) || (chooser.isDirectorySelectionEnabled() && fsv.isFileSystem(f) && isDir)) { fList.add(f); } } if (fList.size() > 0) { files = (File[])fList.toArray(new File[fList.size()]); } setDirectorySelected(false); } } chooser.setSelectedFiles(files); } else { File file = (File)list.getSelectedValue(); if (file != null && file.isDirectory() && chooser.isTraversable(file) && (useSetDirectory || !fsv.isFileSystem(file))) { setDirectorySelected(true); setDirectory(file); if (usesSingleFilePane) { chooser.setSelectedFile(null); } } else { setDirectorySelected(false); if (file != null) { chooser.setSelectedFile(file); } } } } } } protected class DoubleClickListener extends MouseAdapter { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. Handler handler; public DoubleClickListener(JList list) { handler = new Handler(list); } /** * The JList used for representing the files is created by subclasses, but the * selection is monitored in this class. The TransferHandler installed in the * JFileChooser is also installed in the file list as it is used as the actual * transfer source. The list is updated on a mouse enter to reflect the current * data transfer state of the file chooser. */ public void mouseEntered(MouseEvent e) { handler.mouseEntered(e); } public void mouseClicked(MouseEvent e) { handler.mouseClicked(e); } } protected class SelectionListener implements ListSelectionListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void valueChanged(ListSelectionEvent e) { getHandler().valueChanged(e); } } /** * Property to remember whether a directory is currently selected in the UI. * * @return <code>true</code> iff a directory is currently selected. * @since 1.4 */ protected boolean isDirectorySelected() { return directorySelected; } /** * Property to remember whether a directory is currently selected in the UI. * This is normally called by the UI on a selection event. * * @param b iff a directory is currently selected. * @since 1.4 */ protected void setDirectorySelected(boolean b) { directorySelected = b; } /** * Property to remember the directory that is currently selected in the UI. * * @return the value of the <code>directory</code> property * @see #setDirectory * @since 1.4 */ protected File getDirectory() { return directory; } /** * Property to remember the directory that is currently selected in the UI. * This is normally called by the UI on a selection event. * * @param f the <code>File</code> object representing the directory that is * currently selected * @since 1.4 */ protected void setDirectory(File f) { directory = f; } /** * Returns the mnemonic for the given key. */ private int getMnemonic(String key, Locale l) { return SwingUtilities2.getUIDefaultsInt(key, l); } // ******************************************************* // ************ FileChooser UI PLAF methods ************** // ******************************************************* /** * Returns the default accept all file filter */ public FileFilter getAcceptAllFileFilter(JFileChooser fc) { return acceptAllFileFilter; } public FileView getFileView(JFileChooser fc) { return fileView; } /** * Returns the title of this dialog */ public String getDialogTitle(JFileChooser fc) { String dialogTitle = fc.getDialogTitle(); if (dialogTitle != null) { return dialogTitle; } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) { return openDialogTitleText; } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) { return saveDialogTitleText; } else { return getApproveButtonText(fc); } } public int getApproveButtonMnemonic(JFileChooser fc) { int mnemonic = fc.getApproveButtonMnemonic(); if (mnemonic > 0) { return mnemonic; } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) { return openButtonMnemonic; } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) { return saveButtonMnemonic; } else { return mnemonic; } } public String getApproveButtonText(JFileChooser fc) { String buttonText = fc.getApproveButtonText(); if (buttonText != null) { return buttonText; } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) { return openButtonText; } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) { return saveButtonText; } else { return null; } } // ***************************** // ***** Directory Actions ***** // ***************************** public Action getNewFolderAction() { if (newFolderAction == null) { newFolderAction = new NewFolderAction(); // Note: Don't return null for readOnly, it might // break older apps. if (readOnly) { newFolderAction.setEnabled(false); } } return newFolderAction; } public Action getGoHomeAction() { return goHomeAction; } public Action getChangeToParentDirectoryAction() { return changeToParentDirectoryAction; } public Action getApproveSelectionAction() { return approveSelectionAction; } public Action getCancelSelectionAction() { return cancelSelectionAction; } public Action getUpdateAction() { return updateAction; } /** * Creates a new folder. */ protected class NewFolderAction extends AbstractAction { protected NewFolderAction() { super(FilePane.ACTION_NEW_FOLDER); } public void actionPerformed(ActionEvent e) { if (readOnly) { return; } JFileChooser fc = getFileChooser(); File currentDirectory = fc.getCurrentDirectory(); File newFolder = null; try { newFolder = fc.getFileSystemView().createNewFolder(currentDirectory); if (fc.isMultiSelectionEnabled()) { fc.setSelectedFiles(new File[] { newFolder }); } else { fc.setSelectedFile(newFolder); } } catch (IOException exc) { JOptionPane.showMessageDialog( fc, newFolderErrorText + newFolderErrorSeparator + exc, newFolderErrorText, JOptionPane.ERROR_MESSAGE); return; } fc.rescanCurrentDirectory(); } } /** * Acts on the "home" key event or equivalent event. */ protected class GoHomeAction extends AbstractAction { protected GoHomeAction() { super("Go Home"); } public void actionPerformed(ActionEvent e) { JFileChooser fc = getFileChooser(); changeDirectory(fc.getFileSystemView().getHomeDirectory()); } } protected class ChangeToParentDirectoryAction extends AbstractAction { protected ChangeToParentDirectoryAction() { super("Go Up"); putValue(Action.ACTION_COMMAND_KEY, FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY); } public void actionPerformed(ActionEvent e) { Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); if (focusOwner == null || !(focusOwner instanceof javax.swing.text.JTextComponent)) { getFileChooser().changeToParentDirectory(); } } } /** * Responds to an Open or Save request */ protected class ApproveSelectionAction extends AbstractAction { protected ApproveSelectionAction() { super(FilePane.ACTION_APPROVE_SELECTION); } public void actionPerformed(ActionEvent e) { if (isDirectorySelected()) { File dir = getDirectory(); if (dir != null) { try { // Strip trailing ".." dir = ShellFolder.getNormalizedFile(dir); } catch (IOException ex) { // Ok, use f as is } changeDirectory(dir); return; } } JFileChooser chooser = getFileChooser(); String filename = getFileName(); FileSystemView fs = chooser.getFileSystemView(); File dir = chooser.getCurrentDirectory(); if (filename != null) { // Remove whitespace from beginning and end of filename filename = filename.trim(); } if (filename == null || filename.equals("")) { // no file selected, multiple selection off, therefore cancel the approve action resetGlobFilter(); return; } File selectedFile = null; File[] selectedFiles = null; if (filename != null && !filename.equals("")) { // Unix: Resolve '~' to user's home directory if (File.separatorChar == '/') { if (filename.startsWith("~/")) { filename = System.getProperty("user.home") + filename.substring(1); } else if (filename.equals("~")) { filename = System.getProperty("user.home"); } } if (chooser.isMultiSelectionEnabled() && filename.startsWith("\"")) { ArrayList fList = new ArrayList(); filename = filename.substring(1); if (filename.endsWith("\"")) { filename = filename.substring(0, filename.length()-1); } File[] children = null; int childIndex = 0; do { String str; int i = filename.indexOf("\" \""); if (i > 0) { str = filename.substring(0, i); filename = filename.substring(i+3); } else { str = filename; filename = ""; } File file = fs.createFileObject(str); if (!file.isAbsolute()) { if (children == null) { children = fs.getFiles(dir, false); Arrays.sort(children); } for (int k = 0; k < children.length; k++) { int l = (childIndex + k) % children.length; if (children[l].getName().equals(str)) { file = children[l]; childIndex = l + 1; break; } } } fList.add(file); } while (filename.length() > 0); if (fList.size() > 0) { selectedFiles = (File[])fList.toArray(new File[fList.size()]); } resetGlobFilter(); } else { selectedFile = fs.createFileObject(filename); if(!selectedFile.isAbsolute()) { selectedFile = fs.getChild(dir, filename); } // check for wildcard pattern FileFilter currentFilter = chooser.getFileFilter(); if (!selectedFile.exists() && isGlobPattern(filename)) { if (globFilter == null) { globFilter = new GlobFilter(); } try { globFilter.setPattern(filename); if (!(currentFilter instanceof GlobFilter)) { actualFileFilter = currentFilter; } chooser.setFileFilter(null); chooser.setFileFilter(globFilter); return; } catch (PatternSyntaxException pse) { // Not a valid glob pattern. Abandon filter. }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -