JFileChooser with custom implementation of FileSystemView

I have expanded FileSystemViewand overwritten 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 object directoryServicereturns directories from a remote UNIX server. Then I create JFileChooser.

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

The dialog box displays the deleted files and the files correctly, but then I double-click on one of the displayed folders, I plan to go to this folder, but instead the folder path is displayed in the "File name" field and what it is. I cannot change to a directory other than root (/). Should I implement something else in JFileChooser, and not just in FileSystemView?

+4
source share
1 answer

, FileSystemView java.io.File.

VirtualFile, java.io.File true boolean exists(), wraps VirtualFile java.io.File .

, . java.nio.Path, . , , .

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, String child) {
            super(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;
        }

    }

}
0

Source: https://habr.com/ru/post/1623939/


All Articles