Search in sources :

Example 1 with FileSelect

use of org.vaadin.filesystemdataprovider.FileSelect in project ArchCNL by Mari-Wie.

the class FileSelectionComponent method createFileSelect.

@SuppressWarnings("unchecked")
private FileSelect createFileSelect(File rootFile) {
    FileSelect newFileSelect = new FileSelect(rootFile, "adoc");
    newFileSelect.getChildren().filter(TreeGrid.class::isInstance).map(TreeGrid.class::cast).forEach(treeGrid -> treeGrid.expand(treeGrid.getTreeData().getRootItems()));
    newFileSelect.addValueChangeListener(event -> {
        Optional<File> file = newFileSelect.getOptionalValue();
        if (selectDirectory) {
            handleDirectoryChange(file);
        } else {
            if (file.isPresent() && file.get().isFile()) {
                selectedFile = file;
                dialog.setConfirmButtonEnabled(true);
                fileSelectErrorLabel.setVisible(false);
            } else {
                selectedFile = Optional.empty();
                showErrorMessage("Please select a file.");
            }
        }
    });
    // or an empty directory is selected
    if (selectDirectory) {
        for (Component child : newFileSelect.getChildren().collect(Collectors.toList())) {
            if (child instanceof TreeGrid) {
                TreeGrid<File> grid = (TreeGrid<File>) child;
                grid.addExpandListener(event -> {
                    File file = event.getItems().iterator().next();
                    handleDirectoryChange(Optional.of(file));
                });
            }
        }
    }
    newFileSelect.setWidth("500px");
    newFileSelect.setHeight("500px");
    return newFileSelect;
}
Also used : FileSelect(org.vaadin.filesystemdataprovider.FileSelect) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) Component(com.vaadin.flow.component.Component) File(java.io.File)

Aggregations

Component (com.vaadin.flow.component.Component)1 TreeGrid (com.vaadin.flow.component.treegrid.TreeGrid)1 File (java.io.File)1 FileSelect (org.vaadin.filesystemdataprovider.FileSelect)1