⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicfilechooserui.java

📁 java jdk 1.4的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	public void mouseClicked(MouseEvent e) {	    if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {		int index = list.locationToIndex(e.getPoint());		if(index >= 0) {		    File f = (File) list.getModel().getElementAt(index);		    try {			// Strip trailing ".."			f = f.getCanonicalFile();		    } catch (IOException ex) {			// That's ok, we'll use f as is		    }		    if(getFileChooser().isTraversable(f)) {			list.clearSelection();			getFileChooser().setCurrentDirectory(f);		    } else {			getFileChooser().approveSelection();		    }		}	    }	}    }    protected MouseListener createDoubleClickListener(JFileChooser fc, JList list) {	return new DoubleClickListener(list);    }    /**     * 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;    }    protected class SelectionListener implements ListSelectionListener {	public void valueChanged(ListSelectionEvent e) {	    if(!e.getValueIsAdjusting()) {		JFileChooser chooser = getFileChooser();		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.DIRECTORIES_ONLY				|| !chooser.getFileSystemView().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];				if ((chooser.isFileSelectionEnabled() && f.isFile())				    || (chooser.isDirectorySelectionEnabled() && f.isDirectory())) {				    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)			&& (chooser.getFileSelectionMode() != chooser.DIRECTORIES_ONLY			    || !chooser.getFileSystemView().isFileSystem(file))) {			setDirectorySelected(true);			setDirectory(file);		    } else {			setDirectorySelected(false);			if (file != null) {			    chooser.setSelectedFile(file);			}		    }		}	    }	}    }    // *******************************************************    // ************ 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() {	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("New Folder");	}	public void actionPerformed(ActionEvent e) {	    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();	    fc.setCurrentDirectory(fc.getFileSystemView().getHomeDirectory());	}    }    protected class ChangeToParentDirectoryAction extends AbstractAction {	protected ChangeToParentDirectoryAction() {	    super("Go Up");	}	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("approveSelection");	}	public void actionPerformed(ActionEvent e) {	    if (isDirectorySelected()) {		File dir = getDirectory();		if (dir != null) {		    try {			// Strip trailing ".."			dir = dir.getCanonicalFile();		    } catch (IOException ex) {			// Ok, use f as is		    }		    getFileChooser().setCurrentDirectory(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.			}		    }		    resetGlobFilter();		    // Check for directory change action		    boolean isDir = (selectedFile != null && selectedFile.isDirectory());		    boolean isTrav = (selectedFile != null && chooser.isTraversable(selectedFile));		    boolean isDirSelEnabled = chooser.isDirectorySelectionEnabled();		    boolean isFileSelEnabled = chooser.isFileSelectionEnabled();

⌨️ 快捷键说明

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