Skip to content

Commit

Permalink
Keep the filename of the last opened file during save operation
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Oct 24, 2023
1 parent 04b7c71 commit 685ddff
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import one.jpro.platform.file.dropper.FileDropper;
import one.jpro.platform.file.picker.FileOpenPicker;
import one.jpro.platform.file.picker.FileSavePicker;
import org.apache.commons.io.FilenameUtils;
import org.kordamp.ikonli.javafx.FontIcon;
import org.kordamp.ikonli.material2.Material2AL;
import org.kordamp.ikonli.material2.Material2MZ;
Expand Down Expand Up @@ -56,8 +57,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 final ExtensionFilter textExtensionFilter = ExtensionFilter.of("Text files", ".txt", ".srt", ".md", ".csv");
private File lastSavedFile;
private static final ExtensionFilter textExtensionFilter = ExtensionFilter.of("Text files", ".txt", ".srt", ".md", ".csv");
private File lastOpenedFile;
private FileSavePicker fileSavePicker;

@Override
public void start(Stage stage) {
Expand Down Expand Up @@ -120,17 +122,16 @@ public Parent createRoot(Stage stage) {
} else {
Button saveButton = new Button("Save", new FontIcon(Material2MZ.SAVE_ALT));
saveButton.disableProperty().bind(textArea.textProperty().isEmpty());
saveButton.setOnAction(event -> saveToFile(textArea).apply(lastSavedFile));
saveButton.setOnAction(event -> saveToFile(textArea).apply(lastOpenedFile));
saveAsButton = new Button("Save As", new FontIcon(Material2MZ.SAVE));
controlsBox.getChildren().addAll(saveButton, saveAsButton);
}
saveAsButton.disableProperty().bind(textArea.textProperty().isEmpty());

FileSavePicker fileSavePicker = FileSavePicker.create(saveAsButton);
fileSavePicker.setInitialFileName("subtitle");
fileSavePicker = FileSavePicker.create(saveAsButton);
fileSavePicker.setSelectedExtensionFilter(ExtensionFilter.of("Subtitle format (.srt)", ".srt"));
fileSavePicker.setOnFileSelected(file -> saveToFile(textArea).apply(file)
.thenApply(saveToFile -> lastSavedFile = saveToFile));
.thenApply(saveToFile -> lastOpenedFile = saveToFile));

BorderPane rootPane = new BorderPane(contentPane);
rootPane.getStyleClass().add("root-pane");
Expand All @@ -145,17 +146,20 @@ public Parent createRoot(Stage stage) {
* @param textArea the TextArea where the content of the file will be displayed
*/
private void openFile(List<? extends FileSource> fileSources, TextArea textArea) {
fileSources.stream().findFirst().ifPresentOrElse(fileSource ->
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);
}
}).thenApply(file -> lastSavedFile = file), () -> logger.warn("No file selected"));
fileSources.stream().findFirst().ifPresentOrElse(fileSource -> 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);
}
}).thenApply(file -> lastOpenedFile = file)
.thenAccept(file -> fileSavePicker.setInitialFileName(
FilenameUtils.getBaseName(file.getName()))),
() -> logger.warn("No file selected"));
}

/**
Expand Down

0 comments on commit 685ddff

Please sign in to comment.