📄 oyoahafilechooserui.java
字号:
} super.paint(g); } public Dimension getPreferredSize() { if (OyoahaUtilities.isVersion("1.4")) { Dimension d = super.getPreferredSize(); d.width = 200; return d; } return super.getPreferredSize(); } } public void uninstallUI(JComponent c) { c.removePropertyChangeListener(filterComboBoxModel); cancelButton.removeActionListener(getCancelSelectionAction()); approveButton.removeActionListener(getApproveSelectionAction()); filenameTextField.removeActionListener(getApproveSelectionAction()); super.uninstallUI(c); } protected void setFileSelected() { File f = getFileChooser().getSelectedFile(); if(f != null && getOyoahaModel().contains(f)) { list.setSelectedIndex(getOyoahaModel().indexOf(f)); list.ensureIndexIsVisible(list.getSelectedIndex()); } else { list.clearSelection(); } } protected void doSelectedFileChanged(PropertyChangeEvent e) { cancelEdit(); File f = (File) e.getNewValue(); if(f != null) { setFileName(getFileChooser().getName(f)); } else { setFileName(null); } setFileSelected(); } protected void doDirectoryChanged(PropertyChangeEvent e) { cancelEdit(); resetEditIndex(); clearIconCache(); list.clearSelection(); File currentDirectory = getFileChooser().getCurrentDirectory(); if(currentDirectory != null) { directoryComboBoxModel.addItem(currentDirectory); // Enable the newFolder action if the current directory // is writable. // PENDING(jeff) - broken - fix getNewFolderAction().setEnabled(currentDirectory.canWrite()); if(currentDirectory.getParent() == null) { upFolderButton.setEnabled(false); } else { upFolderButton.setEnabled(true); } } } protected void doFilterChanged(PropertyChangeEvent e) { cancelEdit(); resetEditIndex(); clearIconCache(); list.clearSelection(); } protected void doFileSelectionModeChanged(PropertyChangeEvent e) { cancelEdit(); resetEditIndex(); clearIconCache(); list.clearSelection(); } protected void doMultiSelectionChanged(PropertyChangeEvent e) { if(getFileChooser().isMultiSelectionEnabled()) { list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } else { list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.clearSelection(); getFileChooser().setSelectedFiles(null); } } protected void doAccessoryChanged(PropertyChangeEvent e) { if(getAccessoryPanel() != null) { if(e.getOldValue() != null) { getAccessoryPanel().remove((JComponent) e.getOldValue()); } JComponent accessory = (JComponent) e.getNewValue(); if(accessory != null) { getAccessoryPanel().add(accessory, BorderLayout.CENTER); } } } protected void doApproveButtonTextChanged(PropertyChangeEvent e) { JFileChooser chooser = getFileChooser(); approveButton.setText(getApproveButtonText(chooser)); approveButton.setToolTipText(getApproveButtonToolTipText(chooser)); approveButton.setMnemonic(getApproveButtonMnemonic(chooser)); } protected void doDialogTypeChanged(PropertyChangeEvent e) { JFileChooser chooser = getFileChooser(); approveButton.setText(getApproveButtonText(chooser)); approveButton.setToolTipText(getApproveButtonToolTipText(chooser)); approveButton.setMnemonic(getApproveButtonMnemonic(chooser)); } protected void doApproveButtonMnemonicChanged(PropertyChangeEvent e) { approveButton.setMnemonic(getApproveButtonMnemonic(getFileChooser())); } protected void doControlButtonsChanged(PropertyChangeEvent e) { if(getControlButtonsAreShown()) { addControlButtons(); } else { removeControlButtons(); } } public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) { return new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { String s = e.getPropertyName(); if(s.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { doSelectedFileChanged(e); } else if(s.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) { doDirectoryChanged(e); } else if(s.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) { doFilterChanged(e); } else if(s.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) { doFileSelectionModeChanged(e); } else if(s.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) { doMultiSelectionChanged(e); } else if(s.equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY)) { doAccessoryChanged(e); } else if(s.equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY)) { doApproveButtonTextChanged(e); } else if(s.equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY)) { doDialogTypeChanged(e); } else if(s.equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY)) { doApproveButtonMnemonicChanged(e); } /*else if(s.equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) { doControlButtonsChanged(e); }*/ } }; } protected void removeControlButtons() { approveButton.setVisible(false); cancelButton.setVisible(false); } protected void addControlButtons() { approveButton.setVisible(true); cancelButton.setVisible(true); } public void ensureFileIsVisible(JFileChooser fc, File f) { if(getOyoahaModel().contains(f)) { list.ensureIndexIsVisible(getOyoahaModel().indexOf(f)); } } public void rescanCurrentDirectory(JFileChooser fc) { getOyoahaModel().invalidateFileCache(); getOyoahaModel().validateFileCache(); } public String getFileName() { if(filenameTextField != null) { return filenameTextField.getText(); } else { return null; } } public void setFileName(String filename) { if(filenameTextField != null) { filenameTextField.setText(filename); } } public String getDirectoryName() { //TODO return null; } public void setDirectoryName(String dirname) { //TODO } protected OyoahaDirectoryComboBoxRenderer createDirectoryComboBoxRenderer(JFileChooser fc) { return new OyoahaDirectoryComboBoxRenderer(fc); } protected OyoahaDirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {//System.out.println("createDirectoryComboBoxModel " + getOyoahaFileView(fc) + " " + getOyoahaFileView(fc).getFileSystem()); return getOyoahaFileView(fc).getFileSystem(); } protected int getDepth(File file) { int depth = 0; while(file.getParent() != null) { depth++; file = getFileChooser().getFileSystemView().createFileObject(file.getParent()); } return depth; } protected FilterComboBoxRenderer createFilterComboBoxRenderer() { return new FilterComboBoxRenderer(); } public class FilterComboBoxRenderer extends OyoahaListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); FileFilter filter = (FileFilter)value; if(filter != null) { ((JLabel)c).setText(filter.getDescription()); } return c; } } protected FilterComboBoxModel createFilterComboBoxModel() { return new FilterComboBoxModel(); } protected class FilterComboBoxModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener { protected FileFilter[] filters; protected FilterComboBoxModel() { super(); filters = getFileChooser().getChoosableFileFilters(); } public void propertyChange(PropertyChangeEvent e) { String prop = e.getPropertyName(); if(prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) { filters = (FileFilter[]) e.getNewValue(); fireContentsChanged(this, -1, -1); } } public void setSelectedItem(Object filter) { if(filter != null) { getFileChooser().setFileFilter((FileFilter) filter); fireContentsChanged(this, -1, -1); } } public Object getSelectedItem() { // Ensure that the current filter is in the list. // NOTE: we shouldnt' have to do this, since JFileChooser adds // the filter to the choosable filters list when the filter // is set. Lets be paranoid just in case someone overrides // setFileFilter in JFileChooser. FileFilter currentFilter = getFileChooser().getFileFilter(); boolean found = false; if(currentFilter != null) { for(int i=0; i < filters.length; i++) { if(filters[i] == currentFilter) { found = true; } } if(found == false) { getFileChooser().addChoosableFileFilter(currentFilter); } } return getFileChooser().getFileFilter(); } public int getSize() { if(filters != null) { return filters.length; } else { return 0; } } public Object getElementAt(int index) { if(index > getSize() - 1) { // This shouldn't happen. Try to recover gracefully. return getFileChooser().getFileFilter(); } if(filters != null) { return filters[index]; } else { return null; } } } public void valueChanged(ListSelectionEvent e) { File f = getFileChooser().getSelectedFile(); if (!e.getValueIsAdjusting() && f != null && !getFileChooser().isTraversable(f)) { setFileName(getFileChooser().getName(f)); } } protected class DirectoryComboBoxAction extends AbstractAction { protected DirectoryComboBoxAction() { super("DirectoryComboBoxAction"); } public void actionPerformed(ActionEvent e) { getFileChooser().setCurrentDirectory(((OyoahaDirectoryComboBoxNode)directoryComboBox.getSelectedItem()).getFile()); } } protected JButton getApproveButton(JFileChooser fc) { return approveButton; } public Action getNewFolderAction() { if(newFolderAction==null) { newFolderAction = new OyoahaNewFolderAction(); } return newFolderAction; } protected class OyoahaNewFolderAction extends AbstractAction { protected OyoahaNewFolderAction() { super("New Folder"); } public void actionPerformed(ActionEvent e) { JFileChooser fc = getFileChooser(); File currentDirectory = fc.getCurrentDirectory(); File newFolder = null; list.clearSelection(); try { newFolder = fc.getFileSystemView().createNewFolder(currentDirectory); } catch (IOException exc) { //JOptionPane.showMessageDialog(fc, newFolderErrorText + newFolderErrorSeparator + exc, newFolderErrorText, JOptionPane.ERROR_MESSAGE); return; } fc.rescanCurrentDirectory(); fc.ensureFileIsVisible(newFolder); } } protected Action changeToParentDirectoryAction; public Action getChangeToParentDirectoryAction() { if(changeToParentDirectoryAction==null) { changeToParentDirectoryAction = new OyoahaChangeToParentDirectoryAction(); } return changeToParentDirectoryAction; } protected MouseListener createDoubleClickListener(JFileChooser fc, JList list) { return new OyoahaDoubleClickListener(list); } public ListSelectionListener createListSelectionListener(JFileChooser fc) { return new OyoahaSelectionListener(); } protected int lastRowPosition; protected class OyoahaChangeToParentDirectoryAction extends AbstractAction { protected OyoahaChangeToParentDirectoryAction() { super("Go Up"); } public void actionPerformed(ActionEvent e) { getFileChooser().changeToParentDirectory(); //list.ensureIndexIsVisible(lastRowPosition); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -