Skip to content

Commit

Permalink
Update the TextEditor sample to accommodate the last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Nov 19, 2024
1 parent 26e670d commit 7ff18d4
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public class TextEditorSample extends Application {
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 TEXT_EXTENSION_FILTER = ExtensionFilter.of("Text files", ".txt", ".srt", ".md", ".csv");
private static final ExtensionFilter SUBTITLE_EXTENSION_FILTER = ExtensionFilter.of("Subtitle files", ".srt");
private static final ExtensionFilter MARKDOWN_EXTENSION_FILTER = ExtensionFilter.of("Markdown files", ".md");
private static final ExtensionFilter CSV_EXTENSION_FILTER = ExtensionFilter.of("CSV files", ".csv");
private final ObjectProperty<File> lastOpenedFile = new SimpleObjectProperty<>(this, "lastOpenedFile");

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

public Parent createRoot(Stage stage) {
Label dropLabel = new Label("Drop " + TEXT_EXTENSION_FILTER.description().toLowerCase() + " here!");
Label dropLabel = new Label("Drop " + SUBTITLE_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(TEXT_EXTENSION_FILTER);
fileDropper.setExtensionFilter(SUBTITLE_EXTENSION_FILTER);
fileDropper.setOnDragEntered(event -> {
dropPane.pseudoClassStateChanged(FILES_DRAG_OVER_PSEUDO_CLASS, true);
contentPane.getChildren().setAll(textArea, dropPane);
Expand All @@ -106,7 +108,8 @@ public Parent createRoot(Stage stage) {

Button openButton = new Button("Open", new FontIcon(Material2AL.FOLDER_OPEN));
FileOpenPicker fileOpenPicker = FileOpenPicker.create(openButton);
fileOpenPicker.setSelectedExtensionFilter(TEXT_EXTENSION_FILTER);
fileOpenPicker.getExtensionFilters().addAll(SUBTITLE_EXTENSION_FILTER,
MARKDOWN_EXTENSION_FILTER, CSV_EXTENSION_FILTER);
fileOpenPicker.setOnFilesSelected(fileSources -> {
openFile(fileSources, textArea);
contentPane.getChildren().setAll(textArea);
Expand Down Expand Up @@ -134,7 +137,8 @@ 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(TEXT_EXTENSION_FILTER);
fileSavePicker.getExtensionFilters().addAll(SUBTITLE_EXTENSION_FILTER,
MARKDOWN_EXTENSION_FILTER, CSV_EXTENSION_FILTER);
fileSavePicker.setOnFileSelected(file -> saveToFile(textArea).apply(file));

BorderPane rootPane = new BorderPane(contentPane);
Expand Down

0 comments on commit 7ff18d4

Please sign in to comment.