Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowser.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowser.java?rev=1913470&r1=1913469&r2=1913470&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowser.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowser.java Tue Oct 31 19:15:47 2023 @@ -1,443 +1,443 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.pivot.wtk; - -import java.io.File; -import java.net.URI; - -import org.apache.commons.vfs2.FileName; -import org.apache.commons.vfs2.FileObject; -import org.apache.commons.vfs2.FileSystemException; -import org.apache.commons.vfs2.FileSystemManager; -import org.apache.commons.vfs2.FileType; -import org.apache.commons.vfs2.VFS; -import org.apache.pivot.collections.ArrayList; -import org.apache.pivot.collections.Sequence; -import org.apache.pivot.collections.immutable.ImmutableList; -import org.apache.pivot.io.FileObjectList; -import org.apache.pivot.util.Filter; -import org.apache.pivot.util.ListenerList; -import org.apache.pivot.util.Utils; - -/** - * A file browser that uses the Apache Commons VFS (Virtual File System) to be - * able to browse local and remote file systems, and browse inside of .zip, - * .tar, etc. archives as well. - */ -public class VFSBrowser extends Container { - /** - * Commons VFS browser skin interface. - */ - public interface Skin extends org.apache.pivot.wtk.Skin { - public FileObject getFileAt(int x, int y); - public void addActionComponent(Component component); - } - - private static final URI USER_HOME = new File(System.getProperty("user.home")).toURI(); - - private FileSystemManager manager; - private FileName baseFileName; - private FileObject rootDirectory; - private FileObject homeDirectory; - private FileObjectList selectedFiles = new FileObjectList(); - private boolean multiSelect = false; - private Filter<FileObject> disabledFileFilter = null; - - private VFSBrowserListener.Listeners fileBrowserListeners = new VFSBrowserListener.Listeners(); - - /** - * Creates a new VFSBrowser <p> Note that this version sets, by default, - * the mode to open. - * - * @throws FileSystemException if there are problems. - */ - public VFSBrowser() throws FileSystemException { - this(null, USER_HOME, null); - } - - /** - * Creates a new VFSBrowser <p> Note that this version of the constructor - * must be used when a custom root folder (that may include a completely - * different URI scheme) has to be set. - * - * @param manager The virtual file system we're going to manage. - * @param rootFolder The root folder full name. - * @param homeFolder The default home folder full name. - * @throws FileSystemException if there are problems. - */ - public VFSBrowser(FileSystemManager manager, URI rootFolder, URI homeFolder) throws FileSystemException { - this(manager, - rootFolder == null ? null : rootFolder.toString(), - homeFolder == null ? null : homeFolder.toString()); - } - - /** - * Creates a new VFSBrowser <p> Note that this version of the constructor - * must be used when a custom root folder has to be set. - * - * @param manager The virtual file system we're going to manage. - * @param rootFolder The root folder full name. - * @param homeFolder The home folder full name. - * @throws FileSystemException if there are problems. - */ - public VFSBrowser(FileSystemManager manager, String rootFolder, String homeFolder) throws FileSystemException { - Utils.checkNull(rootFolder, "Root folder"); - - // Note: these methods all could trigger events, but since we're - // in the constructor and the skin isn't set yet, there will not - // be any listeners registered yet - setManager(manager); - setRootDirectory(rootFolder); - setHomeDirectory(homeFolder == null ? USER_HOME.toString() : homeFolder); - - installSkin(VFSBrowser.class); - } - - /** - * @return The current file system manager. - */ - public FileSystemManager getManager() { - return manager; - } - - public void setManager(FileSystemManager manager) throws FileSystemException { - FileSystemManager previousManager = this.manager; - - if (manager == null) { - this.manager = VFS.getManager(); - } else { - this.manager = manager; - } - FileObject baseFile = this.manager.getBaseFile(); - if (baseFile != null) { - baseFileName = baseFile.getName(); - } - - if (previousManager != null && previousManager != this.manager) { - fileBrowserListeners.managerChanged(this, previousManager); - } - } - - /** - * @return The current root directory. - */ - public FileObject getRootDirectory() { - return rootDirectory; - } - - /** - * Sets the root directory from a string. Clears any existing file - * selection. - * - * @param rootDirectory The new root directory string for this browser. - * @throws FileSystemException if there are any problems. - */ - public void setRootDirectory(String rootDirectory) throws FileSystemException { - setRootDirectory(manager.resolveFile(rootDirectory)); - } - - /** - * Sets the root directory. Clears any existing file selection. - * - * @param rootDirectory The new root directory for this browser. - * @throws FileSystemException if there are any problems. - */ - public void setRootDirectory(FileObject rootDirectory) throws FileSystemException { - Utils.checkNull(rootDirectory, "Root directory"); - - // Give some grace to set the root folder to an actual file and - // have it work (by using the parent folder instead) - if (rootDirectory.getType() != FileType.FOLDER) { - rootDirectory = rootDirectory.getParent(); - if (rootDirectory == null || rootDirectory.getType() != FileType.FOLDER) { - throw new IllegalArgumentException("Root file is not a directory."); - } - } - - if (rootDirectory.exists()) { - FileObject previousRootDirectory = this.rootDirectory; - - if (!rootDirectory.equals(previousRootDirectory)) { - this.rootDirectory = rootDirectory; - selectedFiles.clear(); - fileBrowserListeners.rootDirectoryChanged(this, previousRootDirectory); - } - } else { - setRootDirectory(rootDirectory.getParent()); - } - } - - /** - * @return The current home directory. - */ - public FileObject getHomeDirectory() { - return homeDirectory; - } - - /** - * Sets the home directory from a string. - * - * @param homeDirectory The new home directory string for this browser. - * @throws FileSystemException if there are any problems. - */ - public void setHomeDirectory(String homeDirectory) throws FileSystemException { - setHomeDirectory(manager.resolveFile(homeDirectory)); - } - - /** - * Sets the home directory. - * - * @param homeDirectory The new home directory for this browser. - * @throws FileSystemException if there are any problems. - */ - public void setHomeDirectory(FileObject homeDirectory) throws FileSystemException { - Utils.checkNull(homeDirectory, "Home directory"); - - // Give some grace to set the home folder to an actual file and - // have it work (by using the parent folder instead) - if (homeDirectory.getType() != FileType.FOLDER) { - homeDirectory = homeDirectory.getParent(); - if (homeDirectory == null || homeDirectory.getType() != FileType.FOLDER) { - throw new IllegalArgumentException("Home file is not a directory."); - } - } - - if (homeDirectory.exists()) { - FileObject previousHomeDirectory = this.homeDirectory; - - if (!homeDirectory.equals(previousHomeDirectory)) { - this.homeDirectory = homeDirectory; - fileBrowserListeners.homeDirectoryChanged(this, previousHomeDirectory); - } - } else { - setHomeDirectory(homeDirectory.getParent()); - } - } - - /** - * Adds a file to the file selection. - * - * @param file The new file to be selected. - * @return {@code true} if the file was added; {@code false} if it was - * already selected. - * @throws FileSystemException if there are any problems. - */ - public boolean addSelectedFile(FileObject file) throws FileSystemException { - Utils.checkNull(file, "Selected file"); - - // TODO: is this a good way to do this? - // if (file.isAbsolute()) { - if (baseFileName != null && baseFileName.isAncestor(file.getName())) { - if (!file.getParent().equals(rootDirectory)) { - throw new IllegalArgumentException("Selected file is not in the root directory"); - } - } else { - file = manager.resolveFile(rootDirectory, file.getName().getBaseName()); - } - - int index = selectedFiles.add(file); - if (index != -1) { - fileBrowserListeners.selectedFileAdded(this, file); - } - - return (index != -1); - } - - /** - * Removes a file from the file selection. - * - * @param file The file to be unselected. - * @return {@code true} if the file was removed; {@code false} if it was - * not already selected. - */ - public boolean removeSelectedFile(FileObject file) { - Utils.checkNull(file, "Selected file"); - - int index = selectedFiles.remove(file); - if (index != -1) { - fileBrowserListeners.selectedFileRemoved(this, file); - } - - return (index != -1); - } - - /** - * When in single-select mode, returns the currently selected file. - * - * @return The currently selected file. - */ - public FileObject getSelectedFile() { - if (multiSelect) { - throw new IllegalStateException("File browser is not in single-select mode."); - } - - return (selectedFiles.getLength() == 0) ? null : selectedFiles.get(0); - } - - /** - * Sets the selection to a single file. - * - * @param file The new single file selection (or {@code null} to clear the selection). - * @throws FileSystemException if there are any problems. - */ - public void setSelectedFile(FileObject file) throws FileSystemException { - if (file == null) { - clearSelection(); - } else { - // TODO: adequate replacement for "isAbsolute"? - // if (file.isAbsolute()) { - if (baseFileName != null && baseFileName.isAncestor(file.getName())) { - setRootDirectory(file.getParent()); - } - - setSelectedFiles(new ArrayList<>(file)); - } - } - - /** - * Returns the currently selected files. - * - * @return An immutable list containing the currently selected files. Note - * that the returned list is a wrapper around the actual selection, not a - * copy. Any changes made to the selection state will be reflected in the - * list, but events will not be fired. - */ - public ImmutableList<FileObject> getSelectedFiles() { - return new ImmutableList<>(selectedFiles); - } - - /** - * Sets the selected files. - * - * @param selectedFiles The files to select. - * @return The files that were selected, with duplicates eliminated. - * @throws FileSystemException if there are any problems. - */ - public Sequence<FileObject> setSelectedFiles(Sequence<FileObject> selectedFiles) - throws FileSystemException { - Utils.checkNull(selectedFiles, "Selected files"); - - if (!multiSelect && selectedFiles.getLength() > 1) { - throw new IllegalArgumentException("Multi-select is not enabled."); - } - - // Update the selection - Sequence<FileObject> previousSelectedFiles = getSelectedFiles(); - - FileObjectList fileList = new FileObjectList(); - for (int i = 0, n = selectedFiles.getLength(); i < n; i++) { - FileObject file = selectedFiles.get(i); - - Utils.checkNull(file, "Selected file"); - - // TODO: is this correct? - // if (!file.isAbsolute()) { - if (baseFileName == null || !baseFileName.isAncestor(file.getName())) { - file = manager.resolveFile(rootDirectory, file.getName().getBaseName()); - } - - // TODO: don't do this for now -- revisit later - // if (!file.getParent().equals(rootDirectory)) { - // throw new IllegalArgumentException(); - // } - - fileList.add(file); - } - - this.selectedFiles = fileList; - - // Notify listeners - fileBrowserListeners.selectedFilesChanged(this, previousSelectedFiles); - - return getSelectedFiles(); - } - - /** - * Clears the selection. - * - * @throws FileSystemException if there are any problems. - */ - public void clearSelection() throws FileSystemException { - setSelectedFiles(new ArrayList<FileObject>()); - } - - public boolean isFileSelected(FileObject file) { - return (selectedFiles.indexOf(file) != -1); - } - - /** - * @return The file browser's multi-select state. - */ - public boolean isMultiSelect() { - return multiSelect; - } - - /** - * Sets the file browser's multi-select state. - * - * @param multiSelect {@code true} if multi-select is enabled; - * {@code false}, otherwise. - */ - public void setMultiSelect(boolean multiSelect) { - if (this.multiSelect != multiSelect) { - // Clear any existing selection - selectedFiles.clear(); - - this.multiSelect = multiSelect; - - fileBrowserListeners.multiSelectChanged(this); - } - } - - /** - * Returns the current file filter. - * - * @return The current file filter, or {@code null} if no filter is set. - */ - public Filter<FileObject> getDisabledFileFilter() { - return disabledFileFilter; - } - - /** - * Sets the file filter. - * - * @param disabledFileFilter The file filter to use, or {@code null} for no - * filter. - */ - public void setDisabledFileFilter(Filter<FileObject> disabledFileFilter) { - Filter<FileObject> previousDisabledFileFilter = this.disabledFileFilter; - - if (previousDisabledFileFilter != disabledFileFilter) { - this.disabledFileFilter = disabledFileFilter; - fileBrowserListeners.disabledFileFilterChanged(this, previousDisabledFileFilter); - } - } - - public FileObject getFileAt(int x, int y) { - VFSBrowser.Skin fileBrowserSkin = (VFSBrowser.Skin) getSkin(); - return fileBrowserSkin.getFileAt(x, y); - } - - public void addActionComponent(Component component) { - VFSBrowser.Skin fileBrowserSkin = (VFSBrowser.Skin) getSkin(); - fileBrowserSkin.addActionComponent(component); - } - - public ListenerList<VFSBrowserListener> getFileBrowserListeners() { - return fileBrowserListeners; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.pivot.wtk; + +import java.io.File; +import java.net.URI; + +import org.apache.commons.vfs2.FileName; +import org.apache.commons.vfs2.FileObject; +import org.apache.commons.vfs2.FileSystemException; +import org.apache.commons.vfs2.FileSystemManager; +import org.apache.commons.vfs2.FileType; +import org.apache.commons.vfs2.VFS; +import org.apache.pivot.collections.ArrayList; +import org.apache.pivot.collections.Sequence; +import org.apache.pivot.collections.immutable.ImmutableList; +import org.apache.pivot.io.FileObjectList; +import org.apache.pivot.util.Filter; +import org.apache.pivot.util.ListenerList; +import org.apache.pivot.util.Utils; + +/** + * A file browser that uses the Apache Commons VFS (Virtual File System) to be + * able to browse local and remote file systems, and browse inside of .zip, + * .tar, etc. archives as well. + */ +public class VFSBrowser extends Container { + /** + * Commons VFS browser skin interface. + */ + public interface Skin extends org.apache.pivot.wtk.Skin { + public FileObject getFileAt(int x, int y); + public void addActionComponent(Component component); + } + + private static final URI USER_HOME = new File(System.getProperty("user.home")).toURI(); + + private FileSystemManager manager; + private FileName baseFileName; + private FileObject rootDirectory; + private FileObject homeDirectory; + private FileObjectList selectedFiles = new FileObjectList(); + private boolean multiSelect = false; + private Filter<FileObject> disabledFileFilter = null; + + private VFSBrowserListener.Listeners fileBrowserListeners = new VFSBrowserListener.Listeners(); + + /** + * Creates a new VFSBrowser <p> Note that this version sets, by default, + * the mode to open. + * + * @throws FileSystemException if there are problems. + */ + public VFSBrowser() throws FileSystemException { + this(null, USER_HOME, null); + } + + /** + * Creates a new VFSBrowser <p> Note that this version of the constructor + * must be used when a custom root folder (that may include a completely + * different URI scheme) has to be set. + * + * @param manager The virtual file system we're going to manage. + * @param rootFolder The root folder full name. + * @param homeFolder The default home folder full name. + * @throws FileSystemException if there are problems. + */ + public VFSBrowser(FileSystemManager manager, URI rootFolder, URI homeFolder) throws FileSystemException { + this(manager, + rootFolder == null ? null : rootFolder.toString(), + homeFolder == null ? null : homeFolder.toString()); + } + + /** + * Creates a new VFSBrowser <p> Note that this version of the constructor + * must be used when a custom root folder has to be set. + * + * @param manager The virtual file system we're going to manage. + * @param rootFolder The root folder full name. + * @param homeFolder The home folder full name. + * @throws FileSystemException if there are problems. + */ + public VFSBrowser(FileSystemManager manager, String rootFolder, String homeFolder) throws FileSystemException { + Utils.checkNull(rootFolder, "Root folder"); + + // Note: these methods all could trigger events, but since we're + // in the constructor and the skin isn't set yet, there will not + // be any listeners registered yet + setManager(manager); + setRootDirectory(rootFolder); + setHomeDirectory(homeFolder == null ? USER_HOME.toString() : homeFolder); + + installSkin(VFSBrowser.class); + } + + /** + * @return The current file system manager. + */ + public FileSystemManager getManager() { + return manager; + } + + public void setManager(FileSystemManager manager) throws FileSystemException { + FileSystemManager previousManager = this.manager; + + if (manager == null) { + this.manager = VFS.getManager(); + } else { + this.manager = manager; + } + FileObject baseFile = this.manager.getBaseFile(); + if (baseFile != null) { + baseFileName = baseFile.getName(); + } + + if (previousManager != null && previousManager != this.manager) { + fileBrowserListeners.managerChanged(this, previousManager); + } + } + + /** + * @return The current root directory. + */ + public FileObject getRootDirectory() { + return rootDirectory; + } + + /** + * Sets the root directory from a string. Clears any existing file + * selection. + * + * @param rootDirectory The new root directory string for this browser. + * @throws FileSystemException if there are any problems. + */ + public void setRootDirectory(String rootDirectory) throws FileSystemException { + setRootDirectory(manager.resolveFile(rootDirectory)); + } + + /** + * Sets the root directory. Clears any existing file selection. + * + * @param rootDirectory The new root directory for this browser. + * @throws FileSystemException if there are any problems. + */ + public void setRootDirectory(FileObject rootDirectory) throws FileSystemException { + Utils.checkNull(rootDirectory, "Root directory"); + + // Give some grace to set the root folder to an actual file and + // have it work (by using the parent folder instead) + if (rootDirectory.getType() != FileType.FOLDER) { + rootDirectory = rootDirectory.getParent(); + if (rootDirectory == null || rootDirectory.getType() != FileType.FOLDER) { + throw new IllegalArgumentException("Root file is not a directory."); + } + } + + if (rootDirectory.exists()) { + FileObject previousRootDirectory = this.rootDirectory; + + if (!rootDirectory.equals(previousRootDirectory)) { + this.rootDirectory = rootDirectory; + selectedFiles.clear(); + fileBrowserListeners.rootDirectoryChanged(this, previousRootDirectory); + } + } else { + setRootDirectory(rootDirectory.getParent()); + } + } + + /** + * @return The current home directory. + */ + public FileObject getHomeDirectory() { + return homeDirectory; + } + + /** + * Sets the home directory from a string. + * + * @param homeDirectory The new home directory string for this browser. + * @throws FileSystemException if there are any problems. + */ + public void setHomeDirectory(String homeDirectory) throws FileSystemException { + setHomeDirectory(manager.resolveFile(homeDirectory)); + } + + /** + * Sets the home directory. + * + * @param homeDirectory The new home directory for this browser. + * @throws FileSystemException if there are any problems. + */ + public void setHomeDirectory(FileObject homeDirectory) throws FileSystemException { + Utils.checkNull(homeDirectory, "Home directory"); + + // Give some grace to set the home folder to an actual file and + // have it work (by using the parent folder instead) + if (homeDirectory.getType() != FileType.FOLDER) { + homeDirectory = homeDirectory.getParent(); + if (homeDirectory == null || homeDirectory.getType() != FileType.FOLDER) { + throw new IllegalArgumentException("Home file is not a directory."); + } + } + + if (homeDirectory.exists()) { + FileObject previousHomeDirectory = this.homeDirectory; + + if (!homeDirectory.equals(previousHomeDirectory)) { + this.homeDirectory = homeDirectory; + fileBrowserListeners.homeDirectoryChanged(this, previousHomeDirectory); + } + } else { + setHomeDirectory(homeDirectory.getParent()); + } + } + + /** + * Adds a file to the file selection. + * + * @param file The new file to be selected. + * @return {@code true} if the file was added; {@code false} if it was + * already selected. + * @throws FileSystemException if there are any problems. + */ + public boolean addSelectedFile(FileObject file) throws FileSystemException { + Utils.checkNull(file, "Selected file"); + + // TODO: is this a good way to do this? + // if (file.isAbsolute()) { + if (baseFileName != null && baseFileName.isAncestor(file.getName())) { + if (!file.getParent().equals(rootDirectory)) { + throw new IllegalArgumentException("Selected file is not in the root directory"); + } + } else { + file = manager.resolveFile(rootDirectory, file.getName().getBaseName()); + } + + int index = selectedFiles.add(file); + if (index != -1) { + fileBrowserListeners.selectedFileAdded(this, file); + } + + return (index != -1); + } + + /** + * Removes a file from the file selection. + * + * @param file The file to be unselected. + * @return {@code true} if the file was removed; {@code false} if it was + * not already selected. + */ + public boolean removeSelectedFile(FileObject file) { + Utils.checkNull(file, "Selected file"); + + int index = selectedFiles.remove(file); + if (index != -1) { + fileBrowserListeners.selectedFileRemoved(this, file); + } + + return (index != -1); + } + + /** + * When in single-select mode, returns the currently selected file. + * + * @return The currently selected file. + */ + public FileObject getSelectedFile() { + if (multiSelect) { + throw new IllegalStateException("File browser is not in single-select mode."); + } + + return (selectedFiles.getLength() == 0) ? null : selectedFiles.get(0); + } + + /** + * Sets the selection to a single file. + * + * @param file The new single file selection (or {@code null} to clear the selection). + * @throws FileSystemException if there are any problems. + */ + public void setSelectedFile(FileObject file) throws FileSystemException { + if (file == null) { + clearSelection(); + } else { + // TODO: adequate replacement for "isAbsolute"? + // if (file.isAbsolute()) { + if (baseFileName != null && baseFileName.isAncestor(file.getName())) { + setRootDirectory(file.getParent()); + } + + setSelectedFiles(new ArrayList<>(file)); + } + } + + /** + * Returns the currently selected files. + * + * @return An immutable list containing the currently selected files. Note + * that the returned list is a wrapper around the actual selection, not a + * copy. Any changes made to the selection state will be reflected in the + * list, but events will not be fired. + */ + public ImmutableList<FileObject> getSelectedFiles() { + return new ImmutableList<>(selectedFiles); + } + + /** + * Sets the selected files. + * + * @param selectedFiles The files to select. + * @return The files that were selected, with duplicates eliminated. + * @throws FileSystemException if there are any problems. + */ + public Sequence<FileObject> setSelectedFiles(Sequence<FileObject> selectedFiles) + throws FileSystemException { + Utils.checkNull(selectedFiles, "Selected files"); + + if (!multiSelect && selectedFiles.getLength() > 1) { + throw new IllegalArgumentException("Multi-select is not enabled."); + } + + // Update the selection + Sequence<FileObject> previousSelectedFiles = getSelectedFiles(); + + FileObjectList fileList = new FileObjectList(); + for (int i = 0, n = selectedFiles.getLength(); i < n; i++) { + FileObject file = selectedFiles.get(i); + + Utils.checkNull(file, "Selected file"); + + // TODO: is this correct? + // if (!file.isAbsolute()) { + if (baseFileName == null || !baseFileName.isAncestor(file.getName())) { + file = manager.resolveFile(rootDirectory, file.getName().getBaseName()); + } + + // TODO: don't do this for now -- revisit later + // if (!file.getParent().equals(rootDirectory)) { + // throw new IllegalArgumentException(); + // } + + fileList.add(file); + } + + this.selectedFiles = fileList; + + // Notify listeners + fileBrowserListeners.selectedFilesChanged(this, previousSelectedFiles); + + return getSelectedFiles(); + } + + /** + * Clears the selection. + * + * @throws FileSystemException if there are any problems. + */ + public void clearSelection() throws FileSystemException { + setSelectedFiles(new ArrayList<FileObject>()); + } + + public boolean isFileSelected(FileObject file) { + return (selectedFiles.indexOf(file) != -1); + } + + /** + * @return The file browser's multi-select state. + */ + public boolean isMultiSelect() { + return multiSelect; + } + + /** + * Sets the file browser's multi-select state. + * + * @param multiSelect {@code true} if multi-select is enabled; + * {@code false}, otherwise. + */ + public void setMultiSelect(boolean multiSelect) { + if (this.multiSelect != multiSelect) { + // Clear any existing selection + selectedFiles.clear(); + + this.multiSelect = multiSelect; + + fileBrowserListeners.multiSelectChanged(this); + } + } + + /** + * Returns the current file filter. + * + * @return The current file filter, or {@code null} if no filter is set. + */ + public Filter<FileObject> getDisabledFileFilter() { + return disabledFileFilter; + } + + /** + * Sets the file filter. + * + * @param disabledFileFilter The file filter to use, or {@code null} for no + * filter. + */ + public void setDisabledFileFilter(Filter<FileObject> disabledFileFilter) { + Filter<FileObject> previousDisabledFileFilter = this.disabledFileFilter; + + if (previousDisabledFileFilter != disabledFileFilter) { + this.disabledFileFilter = disabledFileFilter; + fileBrowserListeners.disabledFileFilterChanged(this, previousDisabledFileFilter); + } + } + + public FileObject getFileAt(int x, int y) { + VFSBrowser.Skin fileBrowserSkin = (VFSBrowser.Skin) getSkin(); + return fileBrowserSkin.getFileAt(x, y); + } + + public void addActionComponent(Component component) { + VFSBrowser.Skin fileBrowserSkin = (VFSBrowser.Skin) getSkin(); + fileBrowserSkin.addActionComponent(component); + } + + public ListenerList<VFSBrowserListener> getFileBrowserListeners() { + return fileBrowserListeners; + } +}
Propchange: pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowser.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserListener.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserListener.java?rev=1913470&r1=1913469&r2=1913470&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserListener.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserListener.java Tue Oct 31 19:15:47 2023 @@ -1,199 +1,199 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.pivot.wtk; - -import org.apache.commons.vfs2.FileObject; -import org.apache.commons.vfs2.FileSystemManager; -import org.apache.pivot.collections.Sequence; -import org.apache.pivot.util.Filter; -import org.apache.pivot.util.ListenerList; - -/** - * Commons VFS browser listener interface. - */ -public interface VFSBrowserListener { - /** - * VFS Browser listeners. - */ - public static class Listeners extends ListenerList<VFSBrowserListener> - implements VFSBrowserListener { - @Override - public void managerChanged(VFSBrowser fileBrowser, FileSystemManager previousManager) { - forEach(listener -> listener.managerChanged(fileBrowser, previousManager)); - } - - @Override - public void rootDirectoryChanged(VFSBrowser fileBrowser, FileObject previousRootDirectory) { - forEach(listener -> listener.rootDirectoryChanged(fileBrowser, previousRootDirectory)); - } - - @Override - public void homeDirectoryChanged(VFSBrowser fileBrowser, FileObject previousHomeDirectory) { - forEach(listener -> listener.homeDirectoryChanged(fileBrowser, previousHomeDirectory)); - } - - @Override - public void selectedFileAdded(VFSBrowser fileBrowser, FileObject file) { - forEach(listener -> listener.selectedFileAdded(fileBrowser, file)); - } - - @Override - public void selectedFileRemoved(VFSBrowser fileBrowser, FileObject file) { - forEach(listener -> listener.selectedFileRemoved(fileBrowser, file)); - } - - @Override - public void selectedFilesChanged(VFSBrowser fileBrowser, - Sequence<FileObject> previousSelectedFiles) { - forEach(listener -> listener.selectedFilesChanged(fileBrowser, previousSelectedFiles)); - } - - @Override - public void multiSelectChanged(VFSBrowser fileBrowser) { - forEach(listener -> listener.multiSelectChanged(fileBrowser)); - } - - @Override - public void disabledFileFilterChanged(VFSBrowser fileBrowser, - Filter<FileObject> previousDisabledFileFilter) { - forEach(listener -> listener.disabledFileFilterChanged(fileBrowser, previousDisabledFileFilter)); - } - } - - /** - * Commons VFS browser listener adapter. - * @deprecated Since 2.1 and Java 8 the interface itself has default implementations. - */ - @Deprecated - public static class Adapter implements VFSBrowserListener { - @Override - public void managerChanged(VFSBrowser fileBrowser, FileSystemManager previousManager) { - // empty block - } - - @Override - public void rootDirectoryChanged(VFSBrowser fileBrowser, FileObject previousRootDirectory) { - // empty block - } - - @Override - public void homeDirectoryChanged(VFSBrowser fileBrowser, FileObject previousHomeDirectory) { - // empty block - } - - @Override - public void selectedFileAdded(VFSBrowser fileBrowser, FileObject file) { - // empty block - } - - @Override - public void selectedFileRemoved(VFSBrowser fileBrowser, FileObject file) { - // empty block - } - - @Override - public void selectedFilesChanged(VFSBrowser fileBrowser, - Sequence<FileObject> previousSelectedFiles) { - // empty block - } - - @Override - public void multiSelectChanged(VFSBrowser fileBrowser) { - // empty block - } - - @Override - public void disabledFileFilterChanged(VFSBrowser fileBrowser, - Filter<FileObject> previousDisabledFileFilter) { - // empty block - } - } - - /** - * Called when a file browser's FileSystemManager has changed, (such as when - * a nested VirtualFileSystem is opened). - * - * @param fileBrowser The browser that has changed. - * @param previousManager The previous file manager for this browser. - */ - default void managerChanged(VFSBrowser fileBrowser, FileSystemManager previousManager) { - } - - /** - * Called when a file browser's root directory has changed. - * - * @param fileBrowser The browser that has changed. - * @param previousRootDirectory The previous root directory that we were browsing. - */ - default void rootDirectoryChanged(VFSBrowser fileBrowser, FileObject previousRootDirectory) { - } - - /** - * Called when a file browser's home directory has changed. - * - * @param fileBrowser The browser that has changed. - * @param previousHomeDirectory The previous home directory. - */ - default void homeDirectoryChanged(VFSBrowser fileBrowser, FileObject previousHomeDirectory) { - } - - /** - * Called when a file has been added to a file browser's selection. - * - * @param fileBrowser The source of this event. - * @param file The file newly added to the selection. - */ - default void selectedFileAdded(VFSBrowser fileBrowser, FileObject file) { - } - - /** - * Called when a file has been removed from a file browser's selection. - * - * @param fileBrowser The source of this event. - * @param file The file just removed from the selection. - */ - default void selectedFileRemoved(VFSBrowser fileBrowser, FileObject file) { - } - - /** - * Called when a file browser's selection state has been reset. - * - * @param fileBrowser The source of this event. - * @param previousSelectedFiles The sequence of files that were previously selected. - */ - default void selectedFilesChanged(VFSBrowser fileBrowser, - Sequence<FileObject> previousSelectedFiles) { - } - - /** - * Called when a file browser's multi-select flag has changed. - * - * @param fileBrowser The browser that has changed selection modes. - */ - default void multiSelectChanged(VFSBrowser fileBrowser) { - } - - /** - * Called when a file browser's file filter has changed. - * - * @param fileBrowser The source of this event. - * @param previousDisabledFileFilter The previous filter for disabled files (if any). - */ - default void disabledFileFilterChanged(VFSBrowser fileBrowser, - Filter<FileObject> previousDisabledFileFilter) { - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.pivot.wtk; + +import org.apache.commons.vfs2.FileObject; +import org.apache.commons.vfs2.FileSystemManager; +import org.apache.pivot.collections.Sequence; +import org.apache.pivot.util.Filter; +import org.apache.pivot.util.ListenerList; + +/** + * Commons VFS browser listener interface. + */ +public interface VFSBrowserListener { + /** + * VFS Browser listeners. + */ + public static class Listeners extends ListenerList<VFSBrowserListener> + implements VFSBrowserListener { + @Override + public void managerChanged(VFSBrowser fileBrowser, FileSystemManager previousManager) { + forEach(listener -> listener.managerChanged(fileBrowser, previousManager)); + } + + @Override + public void rootDirectoryChanged(VFSBrowser fileBrowser, FileObject previousRootDirectory) { + forEach(listener -> listener.rootDirectoryChanged(fileBrowser, previousRootDirectory)); + } + + @Override + public void homeDirectoryChanged(VFSBrowser fileBrowser, FileObject previousHomeDirectory) { + forEach(listener -> listener.homeDirectoryChanged(fileBrowser, previousHomeDirectory)); + } + + @Override + public void selectedFileAdded(VFSBrowser fileBrowser, FileObject file) { + forEach(listener -> listener.selectedFileAdded(fileBrowser, file)); + } + + @Override + public void selectedFileRemoved(VFSBrowser fileBrowser, FileObject file) { + forEach(listener -> listener.selectedFileRemoved(fileBrowser, file)); + } + + @Override + public void selectedFilesChanged(VFSBrowser fileBrowser, + Sequence<FileObject> previousSelectedFiles) { + forEach(listener -> listener.selectedFilesChanged(fileBrowser, previousSelectedFiles)); + } + + @Override + public void multiSelectChanged(VFSBrowser fileBrowser) { + forEach(listener -> listener.multiSelectChanged(fileBrowser)); + } + + @Override + public void disabledFileFilterChanged(VFSBrowser fileBrowser, + Filter<FileObject> previousDisabledFileFilter) { + forEach(listener -> listener.disabledFileFilterChanged(fileBrowser, previousDisabledFileFilter)); + } + } + + /** + * Commons VFS browser listener adapter. + * @deprecated Since 2.1 and Java 8 the interface itself has default implementations. + */ + @Deprecated + public static class Adapter implements VFSBrowserListener { + @Override + public void managerChanged(VFSBrowser fileBrowser, FileSystemManager previousManager) { + // empty block + } + + @Override + public void rootDirectoryChanged(VFSBrowser fileBrowser, FileObject previousRootDirectory) { + // empty block + } + + @Override + public void homeDirectoryChanged(VFSBrowser fileBrowser, FileObject previousHomeDirectory) { + // empty block + } + + @Override + public void selectedFileAdded(VFSBrowser fileBrowser, FileObject file) { + // empty block + } + + @Override + public void selectedFileRemoved(VFSBrowser fileBrowser, FileObject file) { + // empty block + } + + @Override + public void selectedFilesChanged(VFSBrowser fileBrowser, + Sequence<FileObject> previousSelectedFiles) { + // empty block + } + + @Override + public void multiSelectChanged(VFSBrowser fileBrowser) { + // empty block + } + + @Override + public void disabledFileFilterChanged(VFSBrowser fileBrowser, + Filter<FileObject> previousDisabledFileFilter) { + // empty block + } + } + + /** + * Called when a file browser's FileSystemManager has changed, (such as when + * a nested VirtualFileSystem is opened). + * + * @param fileBrowser The browser that has changed. + * @param previousManager The previous file manager for this browser. + */ + default void managerChanged(VFSBrowser fileBrowser, FileSystemManager previousManager) { + } + + /** + * Called when a file browser's root directory has changed. + * + * @param fileBrowser The browser that has changed. + * @param previousRootDirectory The previous root directory that we were browsing. + */ + default void rootDirectoryChanged(VFSBrowser fileBrowser, FileObject previousRootDirectory) { + } + + /** + * Called when a file browser's home directory has changed. + * + * @param fileBrowser The browser that has changed. + * @param previousHomeDirectory The previous home directory. + */ + default void homeDirectoryChanged(VFSBrowser fileBrowser, FileObject previousHomeDirectory) { + } + + /** + * Called when a file has been added to a file browser's selection. + * + * @param fileBrowser The source of this event. + * @param file The file newly added to the selection. + */ + default void selectedFileAdded(VFSBrowser fileBrowser, FileObject file) { + } + + /** + * Called when a file has been removed from a file browser's selection. + * + * @param fileBrowser The source of this event. + * @param file The file just removed from the selection. + */ + default void selectedFileRemoved(VFSBrowser fileBrowser, FileObject file) { + } + + /** + * Called when a file browser's selection state has been reset. + * + * @param fileBrowser The source of this event. + * @param previousSelectedFiles The sequence of files that were previously selected. + */ + default void selectedFilesChanged(VFSBrowser fileBrowser, + Sequence<FileObject> previousSelectedFiles) { + } + + /** + * Called when a file browser's multi-select flag has changed. + * + * @param fileBrowser The browser that has changed selection modes. + */ + default void multiSelectChanged(VFSBrowser fileBrowser) { + } + + /** + * Called when a file browser's file filter has changed. + * + * @param fileBrowser The source of this event. + * @param previousDisabledFileFilter The previous filter for disabled files (if any). + */ + default void disabledFileFilterChanged(VFSBrowser fileBrowser, + Filter<FileObject> previousDisabledFileFilter) { + } +} Propchange: pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserListener.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserSheet.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserSheet.java?rev=1913470&r1=1913469&r2=1913470&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserSheet.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserSheet.java Tue Oct 31 19:15:47 2023 @@ -1,395 +1,395 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.pivot.wtk; - -import org.apache.commons.vfs2.FileName; -import org.apache.commons.vfs2.FileObject; -import org.apache.commons.vfs2.FileSystemException; -import org.apache.commons.vfs2.FileSystemManager; -import org.apache.commons.vfs2.FileType; -import org.apache.commons.vfs2.VFS; -import org.apache.pivot.collections.ArrayList; -import org.apache.pivot.collections.Sequence; -import org.apache.pivot.collections.immutable.ImmutableList; -import org.apache.pivot.io.FileObjectList; -import org.apache.pivot.util.Filter; -import org.apache.pivot.util.ListenerList; -import org.apache.pivot.util.Utils; - -/** - * A file browser sheet that uses the Apache Commons VFS (Virtual File System) - * to be able to browse local and remote file systems, and browse inside of - * .zip, .tar, etc. archives as well. - */ -public class VFSBrowserSheet extends Sheet { - /** - * Enumeration defining supported modes. - */ - public enum Mode { - OPEN, - OPEN_MULTIPLE, - SAVE_AS, - SAVE_TO - } - - private static final String USER_HOME = System.getProperty("user.home"); - - private Mode mode; - private FileSystemManager manager; - private FileName baseFileName; - private FileObject rootDirectory; - private FileObject homeDirectory; - private FileObjectList selectedFiles = new FileObjectList(); - private Filter<FileObject> disabledFileFilter = null; - - private VFSBrowserSheetListener.Listeners fileBrowserSheetListeners = - new VFSBrowserSheetListener.Listeners(); - - /** - * Creates a new VFSBrowserSheet <p> Note that this version set by default - * mode to open and user home as root folder. - * - * @throws FileSystemException if there are any problems. - */ - public VFSBrowserSheet() throws FileSystemException { - this(Mode.OPEN); - } - - /** - * Creates a new VFSBrowserSheet <p> Note that this version sets by default - * the user home as root folder, which is probably not that useful. - * - * @param mode The mode for opening the sheet. - * @throws FileSystemException if there are any problems. - * @see Mode - */ - public VFSBrowserSheet(Mode mode) throws FileSystemException { - this(mode, USER_HOME); - } - - /** - * Creates a new VFSBrowserSheet <p> Note that this version of the - * constructor must be used when a custom root folder has to be set. - * - * @param mode The mode for opening the sheet. - * @param rootFolder The root folder full name. - * @throws FileSystemException if there are any problems. - * @see Mode - */ - public VFSBrowserSheet(Mode mode, String rootFolder) throws FileSystemException { - this(null, mode, rootFolder); - } - - /** - * Creates a new VFSBrowserSheet <p> Note that this version of the - * constructor must be used when a custom root folder has to be set. - * - * @param manager The VFS FileSystemManager that we will be browsing. If - * {@code null} the default (local) will be used. - * @param mode The mode for opening the sheet. - * @param rootFolder The root folder full name. - * @throws FileSystemException if there are any problems. - * @see Mode - */ - public VFSBrowserSheet(FileSystemManager manager, Mode mode, String rootFolder) - throws FileSystemException { - this(manager, mode, rootFolder, null); - } - - /** - * Creates a new VFSBrowserSheet. - * <p> Note that this version of the constructor must be used when a - * custom home folder has to be set. - * - * @param manager The VFS FileSystemManager that we will be browsing. - * If {@code null} the default (local) will be used. - * @param mode The mode for opening the sheet. - * @param rootFolder The root folder full name. - * @param homeFolder The default for the "home" folder (full name). - * @throws FileSystemException if there are any problems. - * @see Mode - */ - public VFSBrowserSheet(FileSystemManager manager, Mode mode, String rootFolder, String homeFolder) - throws FileSystemException { - Utils.checkNull(mode, "Mode"); - Utils.checkNull(rootFolder, "Root folder"); - - this.mode = mode; - - // Note: these three methods all could trigger events, but since we're - // in the constructor and the skin isn't set yet, there will not be any - // listeners registered yet - setManager(manager); - setRootDirectory(rootFolder); - setHomeDirectory(homeFolder == null ? USER_HOME : homeFolder); - - installSkin(VFSBrowserSheet.class); - } - - /** - * Creates a new VFSBrowserSheet. - * <p> Note that this version of the constructor must be used when a - * custom home folder has to be set. - * - * @param manager The VFS FileSystemManager that we will be browsing. - * If {@code null} the default (local) will be used. - * @param mode The mode for opening the sheet. - * @param rootFolder The root folder object. - * @param homeFolder The default for the "home" folder. - * @throws FileSystemException if there are any problems. - * @see Mode - */ - public VFSBrowserSheet(FileSystemManager manager, Mode mode, FileObject rootFolder, FileObject homeFolder) - throws FileSystemException { - Utils.checkNull(mode, "Mode"); - Utils.checkNull(rootFolder, "Root folder"); - - this.mode = mode; - - // Note: these three methods all could trigger events, but since we're - // in the constructor and the skin isn't set yet, there will not be any - // listeners registered yet - setManager(manager); - setRootDirectory(rootFolder); - setHomeDirectory(homeFolder == null ? rootFolder : homeFolder); - - installSkin(VFSBrowserSheet.class); - } - - public FileSystemManager getManager() { - return manager; - } - - public void setManager(FileSystemManager manager) throws FileSystemException { - FileSystemManager previousManager = this.manager; - - if (manager == null) { - this.manager = VFS.getManager(); - } else { - this.manager = manager; - } - FileObject baseFile = this.manager.getBaseFile(); - if (baseFile != null) { - baseFileName = baseFile.getName(); - } - - if (previousManager != null && previousManager != this.manager) { - fileBrowserSheetListeners.managerChanged(this, previousManager); - } - } - - public FileName getBaseFileName() { - return baseFileName; - } - - public Mode getMode() { - return mode; - } - - public void setMode(Mode mode) { - Utils.checkNull(mode, "Mode"); - - Mode previousMode = this.mode; - - if (previousMode != mode) { - this.mode = mode; - fileBrowserSheetListeners.modeChanged(this, previousMode); - } - } - - public FileObject getRootDirectory() { - return rootDirectory; - } - - public void setRootDirectory(String rootDirectory) throws FileSystemException { - setRootDirectory(manager.resolveFile(rootDirectory)); - } - - public void setRootDirectory(FileObject rootDirectory) throws FileSystemException { - Utils.checkNull(rootDirectory, "Root directory"); - - // Give some grace to set the root folder to an actual file and - // have it work (by using the parent folder instead) - if (rootDirectory.getType() != FileType.FOLDER) { - rootDirectory = rootDirectory.getParent(); - if (rootDirectory == null || rootDirectory.getType() != FileType.FOLDER) { - throw new IllegalArgumentException("Root file is not a directory."); - } - } - - if (rootDirectory.exists()) { - FileObject previousRootDirectory = this.rootDirectory; - - if (!rootDirectory.equals(previousRootDirectory)) { - this.rootDirectory = rootDirectory; - selectedFiles.clear(); - fileBrowserSheetListeners.rootDirectoryChanged(this, previousRootDirectory); - } - } else { - setRootDirectory(rootDirectory.getParent()); - } - } - - public FileObject getHomeDirectory() { - return homeDirectory; - } - - public void setHomeDirectory(String homeDirectory) throws FileSystemException { - setHomeDirectory(manager.resolveFile(homeDirectory)); - } - - public void setHomeDirectory(FileObject homeDirectory) throws FileSystemException { - Utils.checkNull(homeDirectory, "Home directory"); - - // Give some grace to set the home folder to an actual file and - // have it work (by using the parent folder instead) - if (homeDirectory.getType() != FileType.FOLDER) { - homeDirectory = homeDirectory.getParent(); - if (homeDirectory == null || homeDirectory.getType() != FileType.FOLDER) { - throw new IllegalArgumentException("Home file is not a directory."); - } - } - - if (homeDirectory.exists()) { - FileObject previousHomeDirectory = this.homeDirectory; - - if (!homeDirectory.equals(previousHomeDirectory)) { - this.homeDirectory = homeDirectory; - fileBrowserSheetListeners.homeDirectoryChanged(this, previousHomeDirectory); - } - } else { - setHomeDirectory(homeDirectory.getParent()); - } - } - - /** - * When in single-select mode, returns the currently selected file. - * - * @return The currently selected file. - */ - public FileObject getSelectedFile() { - if (mode == Mode.OPEN_MULTIPLE) { - throw new IllegalStateException("File browser sheet is not in single-select mode."); - } - - return (selectedFiles.getLength() == 0) ? null : selectedFiles.get(0); - } - - /** - * Sets the selection to a single file. - * - * @param file The new file to be selected (or {@code null} to clear the selection). - * @throws FileSystemException if there are any problems. - */ - public void setSelectedFile(FileObject file) throws FileSystemException { - if (file == null) { - clearSelection(); - } else { - // TODO: will this work right? - // if (file.isAbsolute()) { - if (baseFileName != null && baseFileName.isAncestor(file.getName())) { - setRootDirectory(file.getParent()); - } - - setSelectedFiles(new ArrayList<>(file)); - } - } - - /** - * Returns the currently selected files. - * - * @return An immutable list containing the currently selected files. Note - * that the returned list is a wrapper around the actual selection, not a - * copy. Any changes made to the selection state will be reflected in the - * list, but events will not be fired. - */ - public ImmutableList<FileObject> getSelectedFiles() { - return new ImmutableList<>(selectedFiles); - } - - /** - * Sets the selected files. - * - * @param selectedFiles The files to select. - * @return The files that were selected, with duplicates eliminated. - * @throws FileSystemException if there are any problems. - */ - public Sequence<FileObject> setSelectedFiles(Sequence<FileObject> selectedFiles) - throws FileSystemException { - Utils.checkNull(selectedFiles, "Selected files"); - - if (mode != Mode.OPEN_MULTIPLE && selectedFiles.getLength() > 1) { - throw new IllegalArgumentException("Multi-select is not enabled."); - } - - // Update the selection - Sequence<FileObject> previousSelectedFiles = getSelectedFiles(); - - FileObjectList fileList = new FileObjectList(); - for (int i = 0, n = selectedFiles.getLength(); i < n; i++) { - FileObject file = selectedFiles.get(i); - - Utils.checkNull(file, "Selected file"); - - // TODO: is this correct? - // if (!file.isAbsolute()) { - if (baseFileName == null || !baseFileName.isAncestor(file.getName())) { - file = manager.resolveFile(rootDirectory, file.getName().getBaseName()); - } - - if (!file.getParent().equals(rootDirectory)) { - throw new IllegalArgumentException( - "Selected file doesn't appear to belong to the current directory."); - } - - fileList.add(file); - } - - this.selectedFiles = fileList; - - // Notify listeners - fileBrowserSheetListeners.selectedFilesChanged(this, previousSelectedFiles); - - return getSelectedFiles(); - } - - /** - * Clears the selection. - * - * @throws FileSystemException if there are any problems. - */ - public void clearSelection() throws FileSystemException { - setSelectedFiles(new ArrayList<FileObject>()); - } - - public Filter<FileObject> getDisabledFileFilter() { - return disabledFileFilter; - } - - public void setDisabledFileFilter(Filter<FileObject> disabledFileFilter) { - Filter<FileObject> previousDisabledFileFilter = this.disabledFileFilter; - - if (previousDisabledFileFilter != disabledFileFilter) { - this.disabledFileFilter = disabledFileFilter; - fileBrowserSheetListeners.disabledFileFilterChanged(this, previousDisabledFileFilter); - } - } - - public ListenerList<VFSBrowserSheetListener> getFileBrowserSheetListeners() { - return fileBrowserSheetListeners; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.pivot.wtk; + +import org.apache.commons.vfs2.FileName; +import org.apache.commons.vfs2.FileObject; +import org.apache.commons.vfs2.FileSystemException; +import org.apache.commons.vfs2.FileSystemManager; +import org.apache.commons.vfs2.FileType; +import org.apache.commons.vfs2.VFS; +import org.apache.pivot.collections.ArrayList; +import org.apache.pivot.collections.Sequence; +import org.apache.pivot.collections.immutable.ImmutableList; +import org.apache.pivot.io.FileObjectList; +import org.apache.pivot.util.Filter; +import org.apache.pivot.util.ListenerList; +import org.apache.pivot.util.Utils; + +/** + * A file browser sheet that uses the Apache Commons VFS (Virtual File System) + * to be able to browse local and remote file systems, and browse inside of + * .zip, .tar, etc. archives as well. + */ +public class VFSBrowserSheet extends Sheet { + /** + * Enumeration defining supported modes. + */ + public enum Mode { + OPEN, + OPEN_MULTIPLE, + SAVE_AS, + SAVE_TO + } + + private static final String USER_HOME = System.getProperty("user.home"); + + private Mode mode; + private FileSystemManager manager; + private FileName baseFileName; + private FileObject rootDirectory; + private FileObject homeDirectory; + private FileObjectList selectedFiles = new FileObjectList(); + private Filter<FileObject> disabledFileFilter = null; + + private VFSBrowserSheetListener.Listeners fileBrowserSheetListeners = + new VFSBrowserSheetListener.Listeners(); + + /** + * Creates a new VFSBrowserSheet <p> Note that this version set by default + * mode to open and user home as root folder. + * + * @throws FileSystemException if there are any problems. + */ + public VFSBrowserSheet() throws FileSystemException { + this(Mode.OPEN); + } + + /** + * Creates a new VFSBrowserSheet <p> Note that this version sets by default + * the user home as root folder, which is probably not that useful. + * + * @param mode The mode for opening the sheet. + * @throws FileSystemException if there are any problems. + * @see Mode + */ + public VFSBrowserSheet(Mode mode) throws FileSystemException { + this(mode, USER_HOME); + } + + /** + * Creates a new VFSBrowserSheet <p> Note that this version of the + * constructor must be used when a custom root folder has to be set. + * + * @param mode The mode for opening the sheet. + * @param rootFolder The root folder full name. + * @throws FileSystemException if there are any problems. + * @see Mode + */ + public VFSBrowserSheet(Mode mode, String rootFolder) throws FileSystemException { + this(null, mode, rootFolder); + } + + /** + * Creates a new VFSBrowserSheet <p> Note that this version of the + * constructor must be used when a custom root folder has to be set. + * + * @param manager The VFS FileSystemManager that we will be browsing. If + * {@code null} the default (local) will be used. + * @param mode The mode for opening the sheet. + * @param rootFolder The root folder full name. + * @throws FileSystemException if there are any problems. + * @see Mode + */ + public VFSBrowserSheet(FileSystemManager manager, Mode mode, String rootFolder) + throws FileSystemException { + this(manager, mode, rootFolder, null); + } + + /** + * Creates a new VFSBrowserSheet. + * <p> Note that this version of the constructor must be used when a + * custom home folder has to be set. + * + * @param manager The VFS FileSystemManager that we will be browsing. + * If {@code null} the default (local) will be used. + * @param mode The mode for opening the sheet. + * @param rootFolder The root folder full name. + * @param homeFolder The default for the "home" folder (full name). + * @throws FileSystemException if there are any problems. + * @see Mode + */ + public VFSBrowserSheet(FileSystemManager manager, Mode mode, String rootFolder, String homeFolder) + throws FileSystemException { + Utils.checkNull(mode, "Mode"); + Utils.checkNull(rootFolder, "Root folder"); + + this.mode = mode; + + // Note: these three methods all could trigger events, but since we're + // in the constructor and the skin isn't set yet, there will not be any + // listeners registered yet + setManager(manager); + setRootDirectory(rootFolder); + setHomeDirectory(homeFolder == null ? USER_HOME : homeFolder); + + installSkin(VFSBrowserSheet.class); + } + + /** + * Creates a new VFSBrowserSheet. + * <p> Note that this version of the constructor must be used when a + * custom home folder has to be set. + * + * @param manager The VFS FileSystemManager that we will be browsing. + * If {@code null} the default (local) will be used. + * @param mode The mode for opening the sheet. + * @param rootFolder The root folder object. + * @param homeFolder The default for the "home" folder. + * @throws FileSystemException if there are any problems. + * @see Mode + */ + public VFSBrowserSheet(FileSystemManager manager, Mode mode, FileObject rootFolder, FileObject homeFolder) + throws FileSystemException { + Utils.checkNull(mode, "Mode"); + Utils.checkNull(rootFolder, "Root folder"); + + this.mode = mode; + + // Note: these three methods all could trigger events, but since we're + // in the constructor and the skin isn't set yet, there will not be any + // listeners registered yet + setManager(manager); + setRootDirectory(rootFolder); + setHomeDirectory(homeFolder == null ? rootFolder : homeFolder); + + installSkin(VFSBrowserSheet.class); + } + + public FileSystemManager getManager() { + return manager; + } + + public void setManager(FileSystemManager manager) throws FileSystemException { + FileSystemManager previousManager = this.manager; + + if (manager == null) { + this.manager = VFS.getManager(); + } else { + this.manager = manager; + } + FileObject baseFile = this.manager.getBaseFile(); + if (baseFile != null) { + baseFileName = baseFile.getName(); + } + + if (previousManager != null && previousManager != this.manager) { + fileBrowserSheetListeners.managerChanged(this, previousManager); + } + } + + public FileName getBaseFileName() { + return baseFileName; + } + + public Mode getMode() { + return mode; + } + + public void setMode(Mode mode) { + Utils.checkNull(mode, "Mode"); + + Mode previousMode = this.mode; + + if (previousMode != mode) { + this.mode = mode; + fileBrowserSheetListeners.modeChanged(this, previousMode); + } + } + + public FileObject getRootDirectory() { + return rootDirectory; + } + + public void setRootDirectory(String rootDirectory) throws FileSystemException { + setRootDirectory(manager.resolveFile(rootDirectory)); + } + + public void setRootDirectory(FileObject rootDirectory) throws FileSystemException { + Utils.checkNull(rootDirectory, "Root directory"); + + // Give some grace to set the root folder to an actual file and + // have it work (by using the parent folder instead) + if (rootDirectory.getType() != FileType.FOLDER) { + rootDirectory = rootDirectory.getParent(); + if (rootDirectory == null || rootDirectory.getType() != FileType.FOLDER) { + throw new IllegalArgumentException("Root file is not a directory."); + } + } + + if (rootDirectory.exists()) { + FileObject previousRootDirectory = this.rootDirectory; + + if (!rootDirectory.equals(previousRootDirectory)) { + this.rootDirectory = rootDirectory; + selectedFiles.clear(); + fileBrowserSheetListeners.rootDirectoryChanged(this, previousRootDirectory); + } + } else { + setRootDirectory(rootDirectory.getParent()); + } + } + + public FileObject getHomeDirectory() { + return homeDirectory; + } + + public void setHomeDirectory(String homeDirectory) throws FileSystemException { + setHomeDirectory(manager.resolveFile(homeDirectory)); + } + + public void setHomeDirectory(FileObject homeDirectory) throws FileSystemException { + Utils.checkNull(homeDirectory, "Home directory"); + + // Give some grace to set the home folder to an actual file and + // have it work (by using the parent folder instead) + if (homeDirectory.getType() != FileType.FOLDER) { + homeDirectory = homeDirectory.getParent(); + if (homeDirectory == null || homeDirectory.getType() != FileType.FOLDER) { + throw new IllegalArgumentException("Home file is not a directory."); + } + } + + if (homeDirectory.exists()) { + FileObject previousHomeDirectory = this.homeDirectory; + + if (!homeDirectory.equals(previousHomeDirectory)) { + this.homeDirectory = homeDirectory; + fileBrowserSheetListeners.homeDirectoryChanged(this, previousHomeDirectory); + } + } else { + setHomeDirectory(homeDirectory.getParent()); + } + } + + /** + * When in single-select mode, returns the currently selected file. + * + * @return The currently selected file. + */ + public FileObject getSelectedFile() { + if (mode == Mode.OPEN_MULTIPLE) { + throw new IllegalStateException("File browser sheet is not in single-select mode."); + } + + return (selectedFiles.getLength() == 0) ? null : selectedFiles.get(0); + } + + /** + * Sets the selection to a single file. + * + * @param file The new file to be selected (or {@code null} to clear the selection). + * @throws FileSystemException if there are any problems. + */ + public void setSelectedFile(FileObject file) throws FileSystemException { + if (file == null) { + clearSelection(); + } else { + // TODO: will this work right? + // if (file.isAbsolute()) { + if (baseFileName != null && baseFileName.isAncestor(file.getName())) { + setRootDirectory(file.getParent()); + } + + setSelectedFiles(new ArrayList<>(file)); + } + } + + /** + * Returns the currently selected files. + * + * @return An immutable list containing the currently selected files. Note + * that the returned list is a wrapper around the actual selection, not a + * copy. Any changes made to the selection state will be reflected in the + * list, but events will not be fired. + */ + public ImmutableList<FileObject> getSelectedFiles() { + return new ImmutableList<>(selectedFiles); + } + + /** + * Sets the selected files. + * + * @param selectedFiles The files to select. + * @return The files that were selected, with duplicates eliminated. + * @throws FileSystemException if there are any problems. + */ + public Sequence<FileObject> setSelectedFiles(Sequence<FileObject> selectedFiles) + throws FileSystemException { + Utils.checkNull(selectedFiles, "Selected files"); + + if (mode != Mode.OPEN_MULTIPLE && selectedFiles.getLength() > 1) { + throw new IllegalArgumentException("Multi-select is not enabled."); + } + + // Update the selection + Sequence<FileObject> previousSelectedFiles = getSelectedFiles(); + + FileObjectList fileList = new FileObjectList(); + for (int i = 0, n = selectedFiles.getLength(); i < n; i++) { + FileObject file = selectedFiles.get(i); + + Utils.checkNull(file, "Selected file"); + + // TODO: is this correct? + // if (!file.isAbsolute()) { + if (baseFileName == null || !baseFileName.isAncestor(file.getName())) { + file = manager.resolveFile(rootDirectory, file.getName().getBaseName()); + } + + if (!file.getParent().equals(rootDirectory)) { + throw new IllegalArgumentException( + "Selected file doesn't appear to belong to the current directory."); + } + + fileList.add(file); + } + + this.selectedFiles = fileList; + + // Notify listeners + fileBrowserSheetListeners.selectedFilesChanged(this, previousSelectedFiles); + + return getSelectedFiles(); + } + + /** + * Clears the selection. + * + * @throws FileSystemException if there are any problems. + */ + public void clearSelection() throws FileSystemException { + setSelectedFiles(new ArrayList<FileObject>()); + } + + public Filter<FileObject> getDisabledFileFilter() { + return disabledFileFilter; + } + + public void setDisabledFileFilter(Filter<FileObject> disabledFileFilter) { + Filter<FileObject> previousDisabledFileFilter = this.disabledFileFilter; + + if (previousDisabledFileFilter != disabledFileFilter) { + this.disabledFileFilter = disabledFileFilter; + fileBrowserSheetListeners.disabledFileFilterChanged(this, previousDisabledFileFilter); + } + } + + public ListenerList<VFSBrowserSheetListener> getFileBrowserSheetListeners() { + return fileBrowserSheetListeners; + } +} Propchange: pivot/trunk/wtk/src/org/apache/pivot/wtk/VFSBrowserSheet.java ------------------------------------------------------------------------------ svn:eol-style = native