gtkfilechooserui.java

来自「JAVA 所有包」· Java 代码 · 共 1,363 行 · 第 1/4 页

JAVA
1,363
字号
	}    }    public Dimension getMinimumSize(JComponent x)  {	return new Dimension(MIN_SIZE);    }    public Dimension getMaximumSize(JComponent x) {	return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);    }    protected void align(JComponent c) {	c.setAlignmentX(JComponent.LEFT_ALIGNMENT);	c.setAlignmentY(JComponent.TOP_ALIGNMENT);    }    public Action getNewFolderAction() {        if (newFolderAction == null) {            newFolderAction = new NewFolderAction();            newFolderAction.setEnabled(!readOnly);        }        return newFolderAction;    }    //    // DataModel for DirectoryComboxbox    //    protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {	return new DirectoryComboBoxModel();    }    /**     * Data model for a type-face selection combo-box.     */    protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {	Vector directories = new Vector();	File selectedDirectory = null;	JFileChooser chooser = getFileChooser();	FileSystemView fsv = chooser.getFileSystemView();	public DirectoryComboBoxModel() {	    // Add the current directory to the model, and make it the	    // selectedDirectory	    File dir = getFileChooser().getCurrentDirectory();	    if (dir != null) {		addItem(dir);	    }	}	/**	 * Adds the directory to the model and sets it to be selected,	 * additionally clears out the previous selected directory and	 * the paths leading up to it, if any.	 */	private void addItem(File directory) {	    if (directory == null) {		return;	    }            int oldSize = directories.size();	    directories.clear();            if (oldSize > 0) {                fireIntervalRemoved(this, 0, oldSize);            }	    // Get the canonical (full) path. This has the side	    // benefit of removing extraneous chars from the path,	    // for example /foo/bar/ becomes /foo/bar	    File canonical = null;	    try {		canonical = fsv.createFileObject(ShellFolder.getNormalizedFile(directory).getPath());	    } catch (IOException e) {		// Maybe drive is not ready. Can't abort here.		canonical = directory;	    }	    // create File instances of each directory leading up to the top	    File f = canonical;	    do {		directories.add(f);	    } while ((f = f.getParentFile()) != null);            int newSize = directories.size();            if (newSize > 0) {                fireIntervalAdded(this, 0, newSize);            }	    setSelectedItem(canonical);	}	public void setSelectedItem(Object selectedDirectory) {	    this.selectedDirectory = (File)selectedDirectory;            fireContentsChanged(this, -1, -1);	}	public Object getSelectedItem() {	    return selectedDirectory;	}	public int getSize() {	    return directories.size();	}	public Object getElementAt(int index) {	    return directories.elementAt(index);	}    }    /**     * Acts when DirectoryComboBox has changed the selected item.     */    protected class DirectoryComboBoxAction extends AbstractAction {	protected DirectoryComboBoxAction() {	    super("DirectoryComboBoxAction");	}	public void actionPerformed(ActionEvent e) {	    File f = (File)directoryComboBox.getSelectedItem();	    getFileChooser().setCurrentDirectory(f);	}    }    /**     * Creates a new folder.     */    private 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();            String dirName = (String) JOptionPane.showInputDialog(fc,                     newFolderDialogText, newFolderButtonText,                     JOptionPane.PLAIN_MESSAGE);                        if (dirName != null) {                File newDir = fc.getFileSystemView().createFileObject                        (currentDirectory, dirName);                if (newDir == null || !newDir.mkdir()) {                    JOptionPane.showMessageDialog(fc,                            newFolderErrorText + newFolderErrorSeparator + " \"" +                             dirName + "\"",                            newFolderErrorText, JOptionPane.ERROR_MESSAGE);                }                fc.rescanCurrentDirectory();            }        }    }    private class GTKApproveSelectionAction extends ApproveSelectionAction {        public void actionPerformed(ActionEvent e) {            if (isDirectorySelected()) {                File dir = getDirectory();                try {                    // Strip trailing ".."                    if (dir != null) {                        dir = ShellFolder.getNormalizedFile(dir);                    }                } catch (IOException ex) {                    // Ok, use f as is                }                if (getFileChooser().getCurrentDirectory().equals(dir)) {                    directoryList.clearSelection();                    fileList.clearSelection();	            ListSelectionModel sm = fileList.getSelectionModel();                    if (sm instanceof DefaultListSelectionModel) {	                ((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);                               ((DefaultListSelectionModel)sm).setAnchorSelectionIndex(0);	            }	            rescanCurrentDirectory(getFileChooser());                    return;                }            }            super.actionPerformed(e);        }    }    /**     * Renames file     */    private class RenameFileAction extends AbstractAction {        protected RenameFileAction() {            super(FilePane.ACTION_EDIT_FILE_NAME);        }        public void actionPerformed(ActionEvent e) {            if (getFileName().equals("")) {                return;            }            JFileChooser fc = getFileChooser();            File currentDirectory = fc.getCurrentDirectory();            String newFileName = (String) JOptionPane.showInputDialog                   (fc, new MessageFormat(renameFileDialogText).format                           (new Object[] { getFileName() }),                           renameFileButtonText, JOptionPane.PLAIN_MESSAGE, null, null,                           getFileName());                        if (newFileName != null) {                File oldFile = fc.getFileSystemView().createFileObject                        (currentDirectory, getFileName());                File newFile = fc.getFileSystemView().createFileObject                        (currentDirectory, newFileName);                if (oldFile == null || newFile == null ||                         !getModel().renameFile(oldFile, newFile)) {                    JOptionPane.showMessageDialog(fc,                            new MessageFormat(renameFileErrorText).                            format(new Object[] { getFileName(), newFileName}),                            renameFileErrorTitle, JOptionPane.ERROR_MESSAGE);                } else {                    setFileName(getFileChooser().getName(newFile));                    fc.rescanCurrentDirectory();                }            }        }    }    //    // Renderer for Filter ComboBox    //    protected FilterComboBoxRenderer createFilterComboBoxRenderer() {        return new FilterComboBoxRenderer();    }    /**     * Render different filters     */    public class FilterComboBoxRenderer extends DefaultListCellRenderer implements UIResource {        public String getName() {            // As SynthComboBoxRenderer's are asked for a size BEFORE they            // are parented getName is overriden to force the name to be            // ComboBox.renderer if it isn't set. If we didn't do this the            // wrong style could be used for size calculations.            String name = super.getName();            if (name == null) {                return "ComboBox.renderer";            }            return name;        }        public Component getListCellRendererComponent(JList list, Object value,                                                      int index, boolean isSelected,                                                      boolean cellHasFocus) {            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);            setName("ComboBox.listRenderer");            if (value != null) {                if (value instanceof FileFilter) {                    setText(((FileFilter) value).getDescription());                }            } else {                setText("");            }            return this;        }    }    //    // DataModel for Filter Combobox    //    protected FilterComboBoxModel createFilterComboBoxModel() {        return new FilterComboBoxModel();    }    /**     * Data model for filter combo-box.     */    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);            } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {                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;            }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?