Skip to content

Commit

Permalink
#68 is enhanced with generic controller class. application logic is s…
Browse files Browse the repository at this point in the history
…eparated into another controller class.
  • Loading branch information
miho committed Nov 27, 2024
1 parent 45b3357 commit e6050b2
Show file tree
Hide file tree
Showing 7 changed files with 512 additions and 198 deletions.
8 changes: 4 additions & 4 deletions vmfedit/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.13'
id 'org.openjfx.javafxplugin' version '0.1.0'
id 'org.beryx.jlink' version '2.25.0'
id 'com.github.node-gradle.node' version '3.3.0'
}
Expand All @@ -27,8 +27,8 @@ ext {
junitVersion = '5.8.2'
}

sourceCompatibility = '17'
targetCompatibility = '17'
sourceCompatibility = '23'
targetCompatibility = '23'

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
Expand All @@ -40,7 +40,7 @@ application {
}

javafx {
version = '17.0.2'
version = '23.0.1'
modules = ['javafx.controls', 'javafx.fxml', 'javafx.web']
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package eu.mihosoft.vmf.vmfedit;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class JsonEditorAppController {

@FXML
private WebView webView;

@FXML
private TextField schemaField;

private JsonEditorController jsonEditorControl;

private File currentFile;

@FXML
public void initialize() {

jsonEditorControl = new JsonEditorController(webView);
jsonEditorControl.initialize();

// on schemaField text change update schema
schemaField.textProperty().addListener((observable, oldValue, newValue) -> {
try {
String schema = new String(Files.readAllBytes(new File(newValue).toPath()));

String value = jsonEditorControl.getValue();
jsonEditorControl.setSchema(schema);

} catch (IOException e) {
// showError("Error loading schema", e.getMessage());
}
});

}

@FXML
private void handleLoadDocument() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open JSON Document");

if (currentFile != null) {
fileChooser.setInitialDirectory(currentFile.getParentFile());
}

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON files (*.json)", "*.json");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(webView.getScene().getWindow());
if (file != null) {
try {
String content = new String(Files.readAllBytes(file.toPath()));
jsonEditorControl.setValue(content);

currentFile = file;
// get stage and set title
Stage stage = (Stage) webView.getScene().getWindow();
stage.setTitle("VMF JSON Editor - " + currentFile.getName());
} catch (IOException e) {
showError("Error loading document", e.getMessage());
}
}
}

@FXML
private void handleSaveDocument() {

if(currentFile != null) {
try {
String content = jsonEditorControl.getValue();
System.out.println("Saving document: " + content);
Files.write(currentFile.toPath(), content.getBytes());

// get stage and set title
Stage stage = (Stage) webView.getScene().getWindow();
stage.setTitle("VMF JSON Editor - " + currentFile.getName());

} catch (IOException e) {
showError("Error saving document", e.getMessage());
}
return;
} else {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON files (*.json)", "*.json");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setTitle("Save JSON Document");
File file = fileChooser.showSaveDialog(webView.getScene().getWindow());
if (file != null) {
try {
String content = jsonEditorControl.getValue();
System.out.println("Saving document: " + content);
Files.write(file.toPath(), content.getBytes());

currentFile = file;
// get stage and set title
Stage stage = (Stage) webView.getScene().getWindow();
stage.setTitle("VMF JSON Editor - " + currentFile.getName());

} catch (IOException e) {
showError("Error saving document", e.getMessage());
}
}
}
}

@FXML
private void handleSaveAsDocument() {
FileChooser fileChooser = new FileChooser();

if (currentFile != null) {
fileChooser.setInitialDirectory(currentFile.getParentFile());
}

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON files (*.json)", "*.json");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setTitle("Save JSON Document as");
File file = fileChooser.showSaveDialog(webView.getScene().getWindow());

if (file != null) {
try {
String content = jsonEditorControl.getValue();
System.out.println("Saving document as: " + content);
Files.write(file.toPath(), content.getBytes());

currentFile = file;
// get stage and set title
Stage stage = (Stage) webView.getScene().getWindow();
stage.setTitle("VMF JSON Editor - " + currentFile.getName());

} catch (IOException e) {
showError("Error saving document", e.getMessage());
}
}
}

@FXML
private void handleQuit() {
System.exit(0);
}

@FXML
private void handleBrowseSchema() {

FileChooser fileChooser = new FileChooser();

// set current directory from schemaField
File currentDir = new File(schemaField.getText()).getParentFile();
if (currentDir != null) {
fileChooser.setInitialDirectory(currentDir);
} else {
//
}

fileChooser.setTitle("Open JSON Schema");
// set json extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON Schema files (*.json)", "*.json");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(webView.getScene().getWindow());
if (file != null) {
schemaField.setText(file.getAbsolutePath());
}
}

private String escapeJavaScript(String str) {
return str.replace("\\", "\\\\")
.replace("'", "\\'")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t");
}

private void showError(String title, String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(title);
alert.setContentText(message);
alert.showAndWait();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(JsonEditorApplication.class.getResource("json-editor-view.fxml"));
Parent n = fxmlLoader.load();

JsonEditorController controller = fxmlLoader.getController();
JsonEditorAppController controller = fxmlLoader.getController();

Scene scene = new Scene(n, 800, 600);

Expand Down
Loading

0 comments on commit e6050b2

Please sign in to comment.