Skip to content

Commit

Permalink
Add a New button to the text editor sample
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Oct 23, 2023
1 parent 0c2ebfd commit 04b7c71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,19 @@ public class TextEditorSample extends Application {

@Override
public void start(Stage stage) {
stage.setTitle("JPro File Dropper");
stage.setTitle("JPro Text Editor");
Scene scene = new Scene(createRoot(stage), 1140, 640);
Optional.ofNullable(CupertinoLight.class.getResource(new CupertinoLight().getUserAgentStylesheet()))
.map(URL::toExternalForm)
.ifPresent(scene::setUserAgentStylesheet);
Optional.ofNullable(TextEditorSample.class.getResource("/one/jpro/platform/file/example/css/file_dropper.css"))
.map(URL::toExternalForm)
.ifPresent(scene.getStylesheets()::add);
scene.getStylesheets().add(TextEditorSample.class
.getResource("/one/jpro/platform/file/example/css/text_editor.css").toExternalForm());
stage.setScene(scene);
stage.show();
}

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

Expand All @@ -94,8 +93,14 @@ public Parent createRoot(Stage stage) {
contentPane.getChildren().setAll(textArea);
});

Button newButton = new Button("New", new FontIcon(Material2AL.INSERT_DRIVE_FILE));
newButton.setDefaultButton(true);
newButton.setOnAction(event -> {
textArea.clear();
contentPane.getChildren().setAll(textArea);
});

Button openButton = new Button("Open", new FontIcon(Material2AL.FOLDER_OPEN));
openButton.setDefaultButton(true);
FileOpenPicker fileOpenPicker = FileOpenPicker.create(openButton);
fileOpenPicker.setSelectedExtensionFilter(textExtensionFilter);
fileOpenPicker.setOnFilesSelected(fileSources -> {
Expand All @@ -105,22 +110,23 @@ public Parent createRoot(Stage stage) {

Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
HBox controlsBox = new HBox(openButton, spacer);
HBox controlsBox = new HBox(newButton, openButton, spacer);
controlsBox.getStyleClass().add("controls-box");

final Button retrieveButton;
final Button saveAsButton;
if (WebAPI.isBrowser()) {
retrieveButton = new Button("Download", new FontIcon(Material2AL.CLOUD_DOWNLOAD));
controlsBox.getChildren().add(retrieveButton);
saveAsButton = new Button("Download", new FontIcon(Material2AL.CLOUD_DOWNLOAD));
controlsBox.getChildren().add(saveAsButton);
} else {
Button saveButton = new Button("Save", new FontIcon(Material2MZ.SAVE_ALT));
saveButton.disableProperty().bind(textArea.textProperty().isEmpty());
saveButton.setOnAction(event -> saveToFile(textArea).apply(lastSavedFile));
retrieveButton = new Button("Save As", new FontIcon(Material2MZ.SAVE));
controlsBox.getChildren().addAll(saveButton, retrieveButton);
saveAsButton = new Button("Save As", new FontIcon(Material2MZ.SAVE));
controlsBox.getChildren().addAll(saveButton, saveAsButton);
}
retrieveButton.disableProperty().bind(textArea.textProperty().isEmpty());
FileSavePicker fileSavePicker = FileSavePicker.create(retrieveButton);
saveAsButton.disableProperty().bind(textArea.textProperty().isEmpty());

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

0 comments on commit 04b7c71

Please sign in to comment.