Skip to content

Commit

Permalink
Update TextEditor sample
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Oct 3, 2024
1 parent e5e7fd1 commit b7f0724
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@
*/
public class TextEditorSample extends Application {

private static final Logger logger = LoggerFactory.getLogger(TextEditorSample.class);
private static final Logger LOGGER = LoggerFactory.getLogger(TextEditorSample.class);

private static final PseudoClass FILES_DRAG_OVER_PSEUDO_CLASS = PseudoClass.getPseudoClass("files-drag-over");
private static final ExtensionFilter textExtensionFilter = ExtensionFilter.of("Text files", ".txt", ".srt", ".md", ".csv");
private static final ExtensionFilter TEXT_EXTENSION_FILTER = ExtensionFilter.of("Text files", ".txt", ".srt", ".md", ".csv");
private final ObjectProperty<File> lastOpenedFile = new SimpleObjectProperty<>(this, "lastOpenedFile");

@Override
Expand All @@ -76,15 +77,15 @@ public void start(Stage stage) {
}

public Parent createRoot(Stage stage) {
Label dropLabel = new Label("Drop " + textExtensionFilter.description().toLowerCase() + " here!");
Label dropLabel = new Label("Drop " + TEXT_EXTENSION_FILTER.description().toLowerCase() + " here!");
StackPane dropPane = new StackPane(dropLabel);
dropPane.getStyleClass().add("drop-pane");

TextArea textArea = new TextArea();
StackPane contentPane = new StackPane(textArea, dropPane);

FileDropper fileDropper = FileDropper.create(contentPane);
fileDropper.setExtensionFilter(textExtensionFilter);
fileDropper.setExtensionFilter(TEXT_EXTENSION_FILTER);
fileDropper.setOnDragEntered(event -> {
dropPane.pseudoClassStateChanged(FILES_DRAG_OVER_PSEUDO_CLASS, true);
contentPane.getChildren().setAll(textArea, dropPane);
Expand All @@ -105,7 +106,7 @@ public Parent createRoot(Stage stage) {

Button openButton = new Button("Open", new FontIcon(Material2AL.FOLDER_OPEN));
FileOpenPicker fileOpenPicker = FileOpenPicker.create(openButton);
fileOpenPicker.setSelectedExtensionFilter(textExtensionFilter);
fileOpenPicker.setSelectedExtensionFilter(TEXT_EXTENSION_FILTER);
fileOpenPicker.setOnFilesSelected(fileSources -> {
openFile(fileSources, textArea);
contentPane.getChildren().setAll(textArea);
Expand Down Expand Up @@ -133,7 +134,7 @@ public Parent createRoot(Stage stage) {
fileSavePicker.initialFileNameProperty().bind(lastOpenedFile.map(file ->
FilenameUtils.getName(file.getName())).orElse("subtitle"));
fileSavePicker.initialDirectoryProperty().bind(lastOpenedFile.map(File::getParentFile));
fileSavePicker.setSelectedExtensionFilter(ExtensionFilter.of("Subtitle format (.srt)", ".srt"));
fileSavePicker.setSelectedExtensionFilter(TEXT_EXTENSION_FILTER);
fileSavePicker.setOnFileSelected(file -> saveToFile(textArea).apply(file));

BorderPane rootPane = new BorderPane(contentPane);
Expand All @@ -151,16 +152,16 @@ public Parent createRoot(Stage stage) {
private void openFile(List<? extends FileSource> fileSources, TextArea textArea) {
fileSources.stream().findFirst().ifPresentOrElse(fileSource -> // Set the last opened file
fileSource.uploadFileAsync()
.thenCompose(file -> {
try {
final String fileContent = new String(Files.readAllBytes(file.toPath()));
Platform.runLater(() -> textArea.setText(fileContent));
return CompletableFuture.completedFuture(file);
} catch (IOException ex) {
logger.error("Error reading file: " + ex.getMessage(), ex);
return CompletableFuture.failedFuture(ex);
}
}).thenAccept(lastOpenedFile::set), () -> logger.warn("No file selected"));
.thenCompose(file -> {
try {
final String fileContent = new String(Files.readAllBytes(file.toPath()));
Platform.runLater(() -> textArea.setText(fileContent));
return CompletableFuture.completedFuture(file);
} catch (IOException ex) {
LOGGER.error("Error reading file: {}", file.getAbsolutePath(), ex);
return CompletableFuture.failedFuture(ex);
}
}).thenAccept(lastOpenedFile::set), () -> LOGGER.warn("No file selected"));
}

/**
Expand All @@ -174,10 +175,10 @@ private Function<File, CompletableFuture<Void>> saveToFile(TextArea textArea) {
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(textArea.getText().getBytes());
} catch (IOException ex) {
logger.error("Error writing file: " + ex.getMessage(), ex);
LOGGER.error("Error writing file: {}", file.getAbsolutePath(), ex);
}
lastOpenedFile.set(file);
System.out.println("Saved file: " + file.getAbsolutePath());
LOGGER.info("Saved to file: {}", file.getAbsolutePath());
});
}
}

0 comments on commit b7f0724

Please sign in to comment.