Skip to content

Commit

Permalink
Use binding to change the selection mode in the examples during `Mult…
Browse files Browse the repository at this point in the history
…iple` checkbox selection
  • Loading branch information
besidev committed Oct 18, 2023
1 parent 4fdc8e0 commit 0cbed6c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
* This class represents a sample application for file dropper operations.
Expand Down Expand Up @@ -67,12 +66,6 @@ public Parent createRoot(Stage stage) {
rootPane.setCenter(textArea);
});

CheckBox multipleCheckBox = new CheckBox("Multiple");
multipleCheckBox.setOnAction(event ->
fileDropper.setSelectionMode(multipleCheckBox.isSelected() ?
SelectionMode.MULTIPLE : SelectionMode.SINGLE));
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
Button openButton = new Button("Open");
openButton.setDefaultButton(true);
final var filePicker = FilePicker.create(openButton);
Expand All @@ -82,6 +75,14 @@ public Parent createRoot(Stage stage) {
appendFilesContent(fileSources, textArea);
rootPane.setCenter(textArea);
});

CheckBox multipleCheckBox = new CheckBox("Multiple");
fileDropper.selectionModeProperty().bind(multipleCheckBox.selectedProperty().map(selected ->
selected ? SelectionMode.MULTIPLE : SelectionMode.SINGLE));
filePicker.selectionModeProperty().bind(multipleCheckBox.selectedProperty().map(selected ->
selected ? SelectionMode.MULTIPLE : SelectionMode.SINGLE));
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
Button clearButton = new Button("Clear");
clearButton.setOnAction(event -> rootPane.setCenter(dropPane));

Expand All @@ -93,17 +94,16 @@ public Parent createRoot(Stage stage) {
}

private void appendFilesContent(List<? extends FileSource<?>> fileSources, TextArea textArea) {
fileSources.forEach(fileSource -> fileSource.uploadFileAsync().thenApplyAsync(file -> {
final StringBuilder content = new StringBuilder();
fileSources.forEach(fileSource -> fileSource.uploadFileAsync().thenAcceptAsync(file -> {
try {
final StringBuilder content = new StringBuilder();
String fileContent = new String(Files.readAllBytes(file.toPath()));
content.append(fileContent);
content.append("\n===============================================\n");
return content;
Platform.runLater(() -> textArea.setText(content.toString()));
} catch (IOException ex) {
logger.error("Error reading file: " + ex.getMessage(), ex);
return CompletableFuture.completedFuture(ex);
}
}).thenAcceptAsync(content -> Platform.runLater(() -> textArea.setText(content.toString()))));
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,29 @@ public void start(Stage stage) {

public Parent createRoot(Stage stage) {
Label clickOnMeLabel = new Label("Click on me to open the file picker!");
var placeholderPane = new StackPane(clickOnMeLabel);
StackPane placeholderPane = new StackPane(clickOnMeLabel);
placeholderPane.getStyleClass().add("placeholder-pane");
var fileTableView = new FileTableView();
FileTableView fileTableView = new FileTableView();
fileTableView.setPlaceholder(placeholderPane);

var filePicker = FilePicker.create(placeholderPane);
final var filePicker = FilePicker.create(placeholderPane);
filePicker.getExtensionFilters().addAll(textExtensionFilter,
audioExtensionFilter, videoExtensionFilter, imageExtensionFilter);
filePicker.setSelectedExtensionFilter(audioExtensionFilter);
filePicker.setOnFilesSelected(fileSources -> fileTableView.getItems().addAll(fileSources));

var rootPane = new BorderPane(fileTableView);
BorderPane rootPane = new BorderPane(fileTableView);
rootPane.getStyleClass().add("root-pane");

var multipleCheckBox = new CheckBox("Multiple");
multipleCheckBox.setOnAction(event ->
filePicker.setSelectionMode(multipleCheckBox.isSelected() ?
SelectionMode.MULTIPLE : SelectionMode.SINGLE));
var spacer = new Region();
CheckBox multipleCheckBox = new CheckBox("Multiple");
filePicker.selectionModeProperty().bind(multipleCheckBox.selectedProperty().map(selected ->
selected ? SelectionMode.MULTIPLE : SelectionMode.SINGLE));
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
var clearButton = new Button("Clear");
Button clearButton = new Button("Clear");
clearButton.setOnAction(event -> fileTableView.getItems().clear());

var controlsBox = new HBox(multipleCheckBox, spacer, clearButton);
HBox controlsBox = new HBox(multipleCheckBox, spacer, clearButton);
controlsBox.getStyleClass().add("controls-box");
rootPane.setTop(controlsBox);

Expand Down

0 comments on commit 0cbed6c

Please sign in to comment.