📄 metalfilechooserui.java
字号:
} }); JList fakeList = new JList(detailsTableModel.listModel) { JTable table = detailsTable; public int locationToIndex(Point location) { return table.rowAtPoint(location); } public Rectangle getCellBounds(int index0, int index1) { Rectangle r0 = table.getCellRect(index0, COLUMN_FILENAME, false); Rectangle r1 = table.getCellRect(index1, COLUMN_FILENAME, false); return r0.union(r1); } public Object getSelectedValue() { return table.getValueAt(table.getSelectedRow(), COLUMN_FILENAME); } public Component add(Component comp) { if (comp instanceof JTextField) { return table.add(comp); } else { return super.add(comp); } } public void repaint() { if (table != null) table.repaint(); } public TransferHandler getTransferHandler() { if (table != null) { return table.getTransferHandler(); } else { return super.getTransferHandler(); } } public void setTransferHandler(TransferHandler newHandler) { if (table != null) { table.setTransferHandler(newHandler); } else { super.setTransferHandler(newHandler); } } public boolean getDragEnabled() { if (table != null) { return table.getDragEnabled(); } else { return super.getDragEnabled(); } } public void setDragEnabled(boolean b) { if (table != null) { table.setDragEnabled(b); } else { super.setDragEnabled(b); } } }; fakeList.setSelectionModel(listSelectionModel); detailsTable.addMouseListener(createDoubleClickListener(chooser, fakeList)); //detailsTable.addMouseListener(createSingleClickListener(chooser, fakeList)); JScrollPane scrollpane = new JScrollPane(detailsTable); scrollpane.setComponentOrientation(chooser.getComponentOrientation()); LookAndFeel.installColors(scrollpane.getViewport(), "Table.background", "Table.foreground"); // Adjust width of first column so the table fills the viewport when // first displayed (temporary listener). scrollpane.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { JScrollPane sp = (JScrollPane)e.getComponent(); fixNameColumnWidth(sp.getViewport().getSize().width); sp.removeComponentListener(this); } }); p.add(scrollpane, BorderLayout.CENTER); return p; } private void fixNameColumnWidth(int viewWidth) { TableColumn nameCol = detailsTable.getColumnModel().getColumn(COLUMN_FILENAME); int tableWidth = detailsTable.getPreferredSize().width; if (tableWidth < viewWidth) { nameCol.setPreferredWidth(nameCol.getPreferredWidth() + viewWidth - tableWidth); } } private class DelayedSelectionUpdater implements Runnable { DelayedSelectionUpdater() { SwingUtilities.invokeLater(this); } public void run() { setFileSelected(); } } /** * Creates a selection listener for the list of files and directories. * * @param fc a <code>JFileChooser</code> * @return a <code>ListSelectionListener</code> */ public ListSelectionListener createListSelectionListener(JFileChooser fc) { return new SelectionListener() { public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { JFileChooser chooser = getFileChooser(); FileSystemView fsv = chooser.getFileSystemView(); JList list = (JList) e.getSource(); 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])) && (chooser.getFileSelectionMode() == chooser.FILES_ONLY || !fsv.isFileSystem(((File)objects[0])))) { setDirectorySelected(true); setDirectory(((File)objects[0])); } else { files = new File[objects.length]; int j = 0; for (int i = 0; i < objects.length; i++) { File f = (File)objects[i]; boolean isDir = f.isDirectory(); boolean isFile = ShellFolder.disableFileChooserSpeedFix() ? f.isFile() : !isDir; if ((chooser.isFileSelectionEnabled() && isFile) || (chooser.isDirectorySelectionEnabled() && fsv.isFileSystem(f) && isDir)) { files[j++] = f; } } if (j == 0) { files = null; } else if (j < objects.length) { File[] tmpFiles = new File[j]; System.arraycopy(files, 0, tmpFiles, 0, j); files = tmpFiles; } setDirectorySelected(false); } } chooser.setSelectedFiles(files); } else { File file = (File)list.getSelectedValue(); if (file != null && file.isDirectory() && chooser.isTraversable(file) && (chooser.getFileSelectionMode() == chooser.FILES_ONLY || !fsv.isFileSystem(file))) { setDirectorySelected(true); setDirectory(file); chooser.setSelectedFile(null); } else { setDirectorySelected(false); if (file != null) { chooser.setSelectedFile(file); } } } } } }; } private MouseListener createSingleClickListener(JFileChooser fc, JList list) { return new SingleClickListener(list); } int lastIndex = -1; File editFile = null; int editX = 20; private int getEditIndex() { return lastIndex; } private void setEditIndex(int i) { lastIndex = i; } private void resetEditIndex() { lastIndex = -1; } private void cancelEdit() { if (editFile != null) { editFile = null; list.remove(editCell); centerPanel.repaint(); } else if (detailsTable != null && detailsTable.isEditing()) { detailsTable.getCellEditor().cancelCellEditing(); } } JTextField editCell = null; private void editFileName(int index) { ensureIndexIsVisible(index); if (listViewPanel.isVisible()) { editFile = (File)getModel().getElementAt(index); Rectangle r = list.getCellBounds(index, index); if (editCell == null) { editCell = new JTextField(); editCell.addActionListener(new EditActionListener()); editCell.addFocusListener(editorFocusListener); editCell.setNextFocusableComponent(list); } list.add(editCell); editCell.setText(getFileChooser().getName(editFile)); if (list.getComponentOrientation().isLeftToRight()) { editCell.setBounds(editX + r.x, r.y, r.width - editX, r.height); } else { editCell.setBounds(r.x, r.y, r.width - editX, r.height); } 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(); } } } } 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 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -