Skip to content

Commit

Permalink
- implemented #73
Browse files Browse the repository at this point in the history
-resetting editor supported
  • Loading branch information
miho committed Jan 22, 2025
1 parent ff9e2ca commit 3344513
Show file tree
Hide file tree
Showing 11 changed files with 532 additions and 47 deletions.
5 changes: 4 additions & 1 deletion config/common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
# publication.version = 0.3-SNAPSHOT
publication.version=0.2.9.6-SNAPSHOT
#publication.version=0.2.9.3
#publication.version=0.2.8.8
#publication.version=0.2.8.8

# jackson version
deps.jackson.version=2.17.2
6 changes: 3 additions & 3 deletions jackson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ dependencies {
testImplementation 'com.github.victools:jsonschema-generator:4.36.0'

// jackson
implementation 'com.fasterxml.jackson.core:jackson-core:2.17.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.17.2'
implementation "com.fasterxml.jackson.core:jackson-core:${commonProps.get('deps.jackson.version')}"
implementation "com.fasterxml.jackson.core:jackson-databind:${commonProps.get('deps.jackson.version')}"
implementation "com.fasterxml.jackson.core:jackson-annotations:${commonProps.get('deps.jackson.version')}"
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ private void addPropertyOrder(Property property, Map<String, Object> propertySch
if (order != null) {
propertySchema.put("propertyOrder", Integer.parseInt(order));
}
} else{
// TODO reuse property order from vmf (traversal order via @PropertyOrder, vmf should report th order
// as @Annotation("key="...", value = "...") as well
}
} catch (Exception e) {
throw new RuntimeException("Failed to parse property order", e);
Expand Down
5 changes: 5 additions & 0 deletions vmfedit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ dependencies {

testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")

// add jackson dependencies, use jackson as specified in the common.properties file
implementation "com.fasterxml.jackson.core:jackson-core:${commonProps.get('deps.jackson.version')}"
implementation "com.fasterxml.jackson.core:jackson-databind:${commonProps.get('deps.jackson.version')}"
implementation "com.fasterxml.jackson.core:jackson-annotations:${commonProps.get('deps.jackson.version')}"
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package eu.mihosoft.vmf.vmfedit;

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

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.Optional;


public class JsonEditorAppController {

Expand All @@ -19,11 +20,15 @@ public class JsonEditorAppController {

@FXML
private TextField schemaField;
@FXML
private Button browseSchemaButton;

private JsonEditorController jsonEditorControl;

private File currentFile;

private JsonUtils.SchemaInfo schemaInfo;

@FXML
public void initialize() {

Expand All @@ -45,6 +50,45 @@ public void initialize() {

}

@FXML
private void handleNewDocument() {
// ask user whether to save current document or not (yes/no/cancel)
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Save Document");
alert.setHeaderText("Do you want to save the current document?");
alert.setContentText("Choose your option.");

// Set up the button types
ButtonType buttonTypeYes = new ButtonType("Yes");
ButtonType buttonTypeNo = new ButtonType("No");
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeYes, buttonTypeNo, buttonTypeCancel);

// Show the dialog and wait for response
Optional<ButtonType> result = alert.showAndWait();

if (result.isPresent()) {
if (result.get() == buttonTypeYes) {
// User chose Yes, save the document
handleSaveDocument();
} else if (result.get() == buttonTypeCancel) {
// User chose Cancel, do nothing and return
return;
}
// If user chose No, continue with creating new document
}

// clear editor
schemaField.setText("");
schemaField.setDisable(false);
browseSchemaButton.setDisable(false);
currentFile = null;
jsonEditorControl.reset();
// get stage and set title
Stage stage = (Stage) webView.getScene().getWindow();
stage.setTitle("VMF JSON Editor");
}

@FXML
private void handleLoadDocument() {
FileChooser fileChooser = new FileChooser();
Expand All @@ -61,6 +105,31 @@ private void handleLoadDocument() {
if (file != null) {
try {
String content = new String(Files.readAllBytes(file.toPath()));

// remove comments
content = content.replaceAll("//.*", "");

// baseURI from parent directory
URI baseURI = file.getParentFile().toURI();

JsonUtils.SchemaInfo schemaInfo = JsonUtils.extractSchema(content, baseURI);

this.schemaInfo = schemaInfo;

if (schemaInfo.schemaUri() != null) {
schemaField.setDisable(true);
browseSchemaButton.setDisable(true);
schemaField.setText(schemaInfo.schemaUri().toString());
} else if (schemaInfo.schemaContent() != null) {
schemaField.setDisable(true);
browseSchemaButton.setDisable(true);
jsonEditorControl.setSchema(schemaInfo.schemaContent());
} else {
schemaField.setDisable(false);
browseSchemaButton.setDisable(false);
jsonEditorControl.setSchema(null);
}

jsonEditorControl.setValue(content);

currentFile = file;
Expand All @@ -82,6 +151,12 @@ private void handleSaveDocument() {
try {
String content = jsonEditorControl.getValue();
System.out.println("Saving document: " + content);

// add schema as was specified in the loaded file (as URI or content, see JsonUtils.extractSchema)
if (this.schemaInfo!=null) {
content = JsonUtils.injectSchema(content, this.schemaInfo);
}

Files.write(currentFile.toPath(), content.getBytes());

// get stage and set title
Expand Down Expand Up @@ -133,6 +208,12 @@ private void handleSaveAsDocument() {
try {
String content = jsonEditorControl.getValue();
System.out.println("Saving document as: " + content);

// add schema as was specified in the loaded file (as URI or content, see JsonUtils.extractSchema)
if (this.schemaInfo!=null) {
content = JsonUtils.injectSchema(content, this.schemaInfo);
}

Files.write(file.toPath(), content.getBytes());

currentFile = file;
Expand Down Expand Up @@ -171,6 +252,8 @@ private void handleBrowseSchema() {
File file = fileChooser.showOpenDialog(webView.getScene().getWindow());
if (file != null) {
schemaField.setText(file.getAbsolutePath());
// update schemaInfo
this.schemaInfo = new JsonUtils.SchemaInfo(file.toURI(), null, JsonUtils.SchemaEmbeddingType.EXTERNAL);
}
}

Expand Down
Loading

0 comments on commit 3344513

Please sign in to comment.