Java – Jfilechooser with custom filesystemview implementation

I extend filesystemview and override every method in this class The model is as follows:

public class RemoteSystemFilesView extends FileSystemView {

   private IDirectoryService directoryService;

   public RemoteSystemFilesView(IDirectoryService aDirectoryService){ 
      this.directoryService = aDirectoryService; 
   }
   ....
}

The directoryservice object returns a directory from a remote UNIX server Then I created Jfilechooser

JFileChooser fc = new JFileChooser(new RemoteSystemFilesView(new DirectoryService()));
int returnVal = fc.showOpenDialog(this);

The dialog box displays the remote directories and files correctly, but then I double-click one of the displayed folders. I want to navigate to the folder, but the folder path appears in the file name field, that's it I cannot remove any directories other than root (/) Should I implement something else in Jfilechooser, not just in filesystemview?

Solution

The problem may be that your filesystemview is actually returning normal Java io. File object

Instead, try to return an extension Java io. File and returns true for public Boolean exists(), while wraps returns virtualfile instead of Java for all required methods io. File.

This is an example of the virtualfilesystem I developed It uses Java nio. Path, because my code is mainly based on them I hope it gives you a good starting point for understanding how to modify your code

private static class VirtualFileSystemView extends FileSystemView {

    final Path base;

    final Set<Path> choices;

    private VirtualFileSystemView(final Path base,final Set<Path> choices) {
        this.base = base;
        this.choices = choices;
    }

    @Override
    protected File createFileSystemRoot(File f) {
        return new VirtualFile(f);
    }

    @Override
    public boolean isComputerNode(File dir) {
        return false;
    }

    @Override
    public boolean isFloppyDrive(File dir) {
        return false;
    }

    @Override
    public boolean isDrive(File dir) {
        return false;
    }

    @Override
    public Icon getSystemIcon(File f) {
        return null;
    }

    @Override
    public String getSystemTypeDescription(File f) {
        return f.toPath().toString();
    }

    @Override
    public String getSystemDisplayName(File f) {
        return f.getName();
    }

    @Override
    public File getParentDirectory(final File dir) {
        return new VirtualFile(dir.getParentFile());
    }

    @Override
    public File[] getFiles(final File dir,boolean useFileHiding) {
        final List<File> files = new ArrayList<>(choices.size());

        choices.stream()
                    .filter((path) -> (path.getParent().equals(dir.toPath()))).
                    forEach((path) -> {
                        files.add(new VirtualFile(path.toFile()));
                    });

        return files.toArray(new File[files.size()]);
    }

    @Override
    public File createFileObject(final String path) {
        return new VirtualFile(path);
    }

    @Override
    public File createFileObject(final File dir,final String filename) {
        Path fileObject;

        if (dir != null) {
            fileObject = Paths.get(dir.toPath().toString(),filename);
        } else {
            fileObject = Paths.get(filename);
        }
        return new VirtualFile(fileObject.toFile());
    }

    @Override
    public File getDefaultDirectory() {
        return new VirtualFile(base.toFile());
    }

    @Override
    public File getHomeDirectory() {
        return new VirtualFile(base.toFile());
    }

    @Override
    public File[] getRoots() {
        final List<File> files = new ArrayList<>(choices.size());

        files.add(new VirtualFile(base.toFile()));
        return files.toArray(new File[files.size()]);
    }

    @Override
    public boolean isFileSystemRoot(final File dir) {
        boolean isRoot = dir.toPath().getParent() == null;
        return isRoot;
    }

    @Override
    public boolean isHiddenFile(final File f) {
        return false;
    }

    @Override
    public boolean isFileSystem(final File f) {
        return !isFileSystemRoot(f);
    }

    @Override
    public File getChild(final File parent,final String fileName) {
        return new VirtualFile(parent,fileName);
    }

    @Override
    public boolean isParent(final File folder,final File file) {
        return file.toPath().getParent().equals(folder.toPath());
    }

    @Override
    public Boolean isTraversable(final File f) {
        boolean isTraversable = false;

        for (final Path path : choices) {
            if (path.startsWith(f.toPath())) {
                isTraversable = true;
                break;
            }
        }
        return isTraversable;
    }

    @Override
    public boolean isRoot(final File f) {
        boolean isRoot = false;

        for (final Path path : choices) {
            if (path.getParent().equals(f.toPath())) {
                isRoot = true;
            }
        }
        return isRoot;
    }

    @Override
    public File createNewFolder(final File containingDir) throws IOException {
        return new VirtualFile(containingDir);
    }


    private class VirtualFile extends File {

        private static final long serialVersionUID = -1752685357864733168L;

        private VirtualFile(final File file) {
            super(file.toString());
        }

        private VirtualFile(String pathname) {
            super(pathname);
        }

        private VirtualFile(String parent,String child) {
            super(parent,child);
        }

        private VirtualFile(File parent,child);
        }

        @Override
        public boolean exists() {
            return true;
        }

        @Override
        public boolean isDirectory() {
            return VirtualFileSystemView.this.isTraversable(this);
        }

        @Override
        public File getCanonicalFile() throws IOException {
            return new VirtualFile(super.getCanonicalFile());
        }

        @Override
        public File getAbsoluteFile() {
            return new VirtualFile(super.getAbsoluteFile());
        }

        @Override
        public File getParentFile() {
            File parent = super.getParentFile();

            if (parent != null) {
                parent = new VirtualFile(super.getParentFile());
            }
            return parent;
        }

    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>