windowsfilechooserui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,088 行 · 第 1/5 页
JAVA
2,088 行
} editCell.requestFocus(); editCell.selectAll(); } else if (detailsViewPanel.isVisible()) { detailsTable.editCellAt(index, COLUMN_FILENAME); } } protected class SingleClickListener extends MouseAdapter { JList list; public SingleClickListener(JList list) { this.list = list; } public void mouseClicked(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { if (e.getClickCount() == 1) { JFileChooser fc = getFileChooser(); int index = list.locationToIndex(e.getPoint()); if ((!fc.isMultiSelectionEnabled() || fc.getSelectedFiles().length <= 1) && index >= 0 && list.isSelectedIndex(index) && getEditIndex() == index && editFile == null) { editFileName(index); } else { if (index >= 0) { setEditIndex(index); } else { resetEditIndex(); } } } else { // on double click (open or drill down one directory) be // sure to clear the edit index resetEditIndex(); } } } } public Action getNewFolderAction() { return newFolderAction; } /** * Creates a new folder. */ protected class WindowsNewFolderAction extends NewFolderAction { public void actionPerformed(ActionEvent e) { JFileChooser fc = getFileChooser(); File oldFile = fc.getSelectedFile(); super.actionPerformed(e); File newFile = fc.getSelectedFile(); if (newFile != null && !newFile.equals(oldFile) && newFile.isDirectory()) { newFolderFile = newFile; } } } class EditActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { applyEdit(); } } private void applyEdit() { if (editFile != null && editFile.exists()) { JFileChooser chooser = getFileChooser(); String oldDisplayName = chooser.getName(editFile); String oldFileName = editFile.getName(); String newDisplayName = editCell.getText().trim(); String newFileName; if (!newDisplayName.equals(oldDisplayName)) { newFileName = newDisplayName; //Check if extension is hidden from user int i1 = oldFileName.length(); int i2 = oldDisplayName.length(); if (i1 > i2 && oldFileName.charAt(i2) == '.') { newFileName = newDisplayName + oldFileName.substring(i2); } // rename FileSystemView fsv = chooser.getFileSystemView(); File f2 = fsv.createFileObject(editFile.getParentFile(), newFileName); if (!f2.exists() && getModel().renameFile(editFile, f2)) { if (fsv.isParent(chooser.getCurrentDirectory(), f2)) { if (chooser.isMultiSelectionEnabled()) { chooser.setSelectedFiles(new File[] { f2 }); } else { chooser.setSelectedFile(f2); } } else { //Could be because of delay in updating Desktop folder //chooser.setSelectedFile(null); } } else { // PENDING(jeff) - show a dialog indicating failure } } } if (detailsTable != null && detailsTable.isEditing()) { detailsTable.getCellEditor().stopCellEditing(); } cancelEdit(); } protected class FileRenderer extends DefaultListCellRenderer { public void setBounds(int x, int y, int width, int height) { super.setBounds(x, y, Math.min(width, this.getPreferredSize().width+4), height); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); File file = (File) value; String fileName = getFileChooser().getName(file); setText(fileName); Icon icon = getFileChooser().getIcon(file); setIcon(icon); if(isSelected) { // PENDING(jeff) - grab padding (4) below from defaults table. editX = icon.getIconWidth() + 4; } return this; } } public void uninstallUI(JComponent c) { // Remove listeners c.removePropertyChangeListener(filterComboBoxModel); cancelButton.removeActionListener(getCancelSelectionAction()); approveButton.removeActionListener(getApproveSelectionAction()); filenameTextField.removeActionListener(getApproveSelectionAction()); super.uninstallUI(c); } /** * Returns the preferred size of the specified * <code>JFileChooser</code>. * The preferred size is at least as large, * in both height and width, * as the preferred size recommended * by the file chooser's layout manager. * * @param c a <code>JFileChooser</code> * @return a <code>Dimension</code> specifying the preferred * width and height of the file chooser */ public Dimension getPreferredSize(JComponent c) { int prefWidth = PREF_SIZE.width; Dimension d = c.getLayout().preferredLayoutSize(c); if (d != null) { return new Dimension(d.width < prefWidth ? prefWidth : d.width, d.height < PREF_SIZE.height ? PREF_SIZE.height : d.height); } else { return new Dimension(prefWidth, PREF_SIZE.height); } } /** * Returns the minimum size of the <code>JFileChooser</code>. * * @param c a <code>JFileChooser</code> * @return a <code>Dimension</code> specifying the minimum * width and height of the file chooser */ public Dimension getMinimumSize(JComponent c) { return MIN_SIZE; } /** * Returns the maximum size of the <code>JFileChooser</code>. * * @param c a <code>JFileChooser</code> * @return a <code>Dimension</code> specifying the maximum * width and height of the file chooser */ public Dimension getMaximumSize(JComponent c) { return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); } void setFileSelected() { if (getFileChooser().isMultiSelectionEnabled() && !isDirectorySelected()) { File[] files = getFileChooser().getSelectedFiles(); // Should be selected Object[] selectedObjects = list.getSelectedValues(); // Are actually selected if (ShellFolder.disableFileChooserSpeedFix()) { // Remove files that shouldn't be selected for (int j = 0; j < selectedObjects.length; j++) { boolean found = false; for (int i = 0; i < files.length; i++) { if (files[i].equals(selectedObjects[j])) { found = true; break; } } if (!found) { int index = getModel().indexOf(selectedObjects[j]); if (index >= 0) { listSelectionModel.removeSelectionInterval(index, index); } } } // Add files that should be selected for (int i = 0; i < files.length; i++) { boolean found = false; for (int j = 0; j < selectedObjects.length; j++) { if (files[i].equals(selectedObjects[j])) { found = true; break; } } if (!found) { int index = getModel().indexOf(files[i]); if (index >= 0) { listSelectionModel.addSelectionInterval(index, index); } } } } else { listSelectionModel.setValueIsAdjusting(true); try { Arrays.sort(files); Arrays.sort(selectedObjects); int shouldIndex = 0; int actuallyIndex = 0; // Remove files that shouldn't be selected and add files which should be selected // Note: Assume files are already sorted in compareTo order. while (shouldIndex < files.length && actuallyIndex < selectedObjects.length) { int comparison = files[shouldIndex].compareTo(selectedObjects[actuallyIndex]); if (comparison < 0) { int index = getModel().indexOf(files[shouldIndex]); listSelectionModel.addSelectionInterval(index, index); shouldIndex++; } else if (comparison > 0) { int index = getModel().indexOf(selectedObjects[actuallyIndex]); listSelectionModel.removeSelectionInterval(index, index); actuallyIndex++; } else { // Do nothing shouldIndex++; actuallyIndex++; } } while (shouldIndex < files.length) { int index = getModel().indexOf(files[shouldIndex]); listSelectionModel.addSelectionInterval(index, index); shouldIndex++; } while (actuallyIndex < selectedObjects.length) { int index = getModel().indexOf(selectedObjects[actuallyIndex]); listSelectionModel.removeSelectionInterval(index, index); actuallyIndex++; } } finally { listSelectionModel.setValueIsAdjusting(false); } } } else { JFileChooser chooser = getFileChooser(); File f = null; if (isDirectorySelected()) { f = getDirectory(); } else { f = chooser.getSelectedFile(); } int i; if (f != null && (i = getModel().indexOf(f)) >= 0) { listSelectionModel.setSelectionInterval(i, i); ensureIndexIsVisible(i); } else { listSelectionModel.clearSelection(); } } } private String fileNameString(File file) { if (file == null) { return null; } else { JFileChooser fc = getFileChooser(); if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) { return file.getPath(); } else { return file.getName(); } } } private String fileNameString(File[] files) { StringBuffer buf = new StringBuffer(); for (int i = 0; files != null && i < files.length; i++) { if (i > 0) { buf.append(" "); } if (files.length > 1) { buf.append("\""); } buf.append(fileNameString(files[i])); if (files.length > 1) { buf.append("\""); } } return buf.toString(); } /* The following methods are used by the PropertyChange Listener */ private void doSelectedFileChanged(PropertyChangeEvent e) { applyEdit(); File f = (File) e.getNewValue(); JFileChooser fc = getFileChooser(); if (f != null && ((fc.isFileSelectionEnabled() && !f.isDirectory()) || (f.isDirectory() && fc.isDirectorySelectionEnabled()))) { setFileName(fileNameString(f)); setFileSelected(); } } private void doSelectedFilesChanged(PropertyChangeEvent e) { applyEdit(); File[] files = (File[]) e.getNewValue(); JFileChooser fc = getFileChooser(); if (files != null && files.length > 0 && (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) { setFileName(fileNameString(files)); setFileSelected(); } } private void doDirectoryChanged(PropertyChangeEvent e) { JFileChooser fc = getFileChooser(); FileSystemView fsv = fc.getFileSystemView(); applyEdit(); resetEditIndex(); clearIconCache(); listSelectionModel.clearSelection(); ensureIndexIsVisible(0); File currentDirectory = fc.getCurrentDirectory(); if (shortCutPanel != null) { shortCutPanel.doDirectoryChanged(currentDirectory); } if(currentDirectory != null) { directoryComboBoxModel.addItem(currentDirectory); getNewFolderAction().setEnabled(currentDirectory.canWrite()); getChangeToParentDirectoryAction().setEnabled(!fsv.isRoot(currentDirectory)); if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) { if (fsv.isFileSystem(currentDirectory)) { setFileName(currentDirectory.getPath()); } else { setFileName(null); } } } } private void doFilterChanged(PropertyChangeEvent e) { applyEdit(); resetEditIndex(); clearIconCache(); listSelectionModel.clearSelection(); } private void doFileSelectionModeChanged(PropertyChangeEvent e) { applyEdit(); resetEditIndex(); clearIconCache(); listSelectionModel.clearSelection(); JFileChooser fc = getFileChooser(); File currentDirectory = fc.getCurrentDirectory(); if (currentDirectory != null && fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled() && fc.getFileSystemView().isFileSystem(currentDirectory)) { setFileName(currentDirectory.getPath()); } else { setFileName(null); } } private void doMultiSelectionChanged(PropertyChangeEvent e) { if (getFileChooser().isMultiSelectionEnabled()) { listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } else { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.clearSelection(); getFileChooser().setSelectedFiles(null); } } private void doAccessoryChanged(PropertyChangeEvent e) { if(getAccessoryPanel() != null) { if(e.getOldValue() != null) { getAccessoryPanel().remove((JComponent) e.getOldValue());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?