Skip to content

Commit e6050b2

Browse files
committed
#68 is enhanced with generic controller class. application logic is separated into another controller class.
1 parent 45b3357 commit e6050b2

File tree

7 files changed

+512
-198
lines changed

7 files changed

+512
-198
lines changed

vmfedit/build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
22
id 'application'
3-
id 'org.openjfx.javafxplugin' version '0.0.13'
3+
id 'org.openjfx.javafxplugin' version '0.1.0'
44
id 'org.beryx.jlink' version '2.25.0'
55
id 'com.github.node-gradle.node' version '3.3.0'
66
}
@@ -27,8 +27,8 @@ ext {
2727
junitVersion = '5.8.2'
2828
}
2929

30-
sourceCompatibility = '17'
31-
targetCompatibility = '17'
30+
sourceCompatibility = '23'
31+
targetCompatibility = '23'
3232

3333
tasks.withType(JavaCompile) {
3434
options.encoding = 'UTF-8'
@@ -40,7 +40,7 @@ application {
4040
}
4141

4242
javafx {
43-
version = '17.0.2'
43+
version = '23.0.1'
4444
modules = ['javafx.controls', 'javafx.fxml', 'javafx.web']
4545
}
4646

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package eu.mihosoft.vmf.vmfedit;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.scene.control.Alert;
5+
import javafx.scene.control.TextField;
6+
import javafx.scene.web.WebView;
7+
import javafx.stage.FileChooser;
8+
import javafx.stage.Stage;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
14+
public class JsonEditorAppController {
15+
16+
@FXML
17+
private WebView webView;
18+
19+
@FXML
20+
private TextField schemaField;
21+
22+
private JsonEditorController jsonEditorControl;
23+
24+
private File currentFile;
25+
26+
@FXML
27+
public void initialize() {
28+
29+
jsonEditorControl = new JsonEditorController(webView);
30+
jsonEditorControl.initialize();
31+
32+
// on schemaField text change update schema
33+
schemaField.textProperty().addListener((observable, oldValue, newValue) -> {
34+
try {
35+
String schema = new String(Files.readAllBytes(new File(newValue).toPath()));
36+
37+
String value = jsonEditorControl.getValue();
38+
jsonEditorControl.setSchema(schema);
39+
40+
} catch (IOException e) {
41+
// showError("Error loading schema", e.getMessage());
42+
}
43+
});
44+
45+
}
46+
47+
@FXML
48+
private void handleLoadDocument() {
49+
FileChooser fileChooser = new FileChooser();
50+
fileChooser.setTitle("Open JSON Document");
51+
52+
if (currentFile != null) {
53+
fileChooser.setInitialDirectory(currentFile.getParentFile());
54+
}
55+
56+
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON files (*.json)", "*.json");
57+
fileChooser.getExtensionFilters().add(extFilter);
58+
File file = fileChooser.showOpenDialog(webView.getScene().getWindow());
59+
if (file != null) {
60+
try {
61+
String content = new String(Files.readAllBytes(file.toPath()));
62+
jsonEditorControl.setValue(content);
63+
64+
currentFile = file;
65+
// get stage and set title
66+
Stage stage = (Stage) webView.getScene().getWindow();
67+
stage.setTitle("VMF JSON Editor - " + currentFile.getName());
68+
} catch (IOException e) {
69+
showError("Error loading document", e.getMessage());
70+
}
71+
}
72+
}
73+
74+
@FXML
75+
private void handleSaveDocument() {
76+
77+
if(currentFile != null) {
78+
try {
79+
String content = jsonEditorControl.getValue();
80+
System.out.println("Saving document: " + content);
81+
Files.write(currentFile.toPath(), content.getBytes());
82+
83+
// get stage and set title
84+
Stage stage = (Stage) webView.getScene().getWindow();
85+
stage.setTitle("VMF JSON Editor - " + currentFile.getName());
86+
87+
} catch (IOException e) {
88+
showError("Error saving document", e.getMessage());
89+
}
90+
return;
91+
} else {
92+
FileChooser fileChooser = new FileChooser();
93+
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON files (*.json)", "*.json");
94+
fileChooser.getExtensionFilters().add(extFilter);
95+
fileChooser.setTitle("Save JSON Document");
96+
File file = fileChooser.showSaveDialog(webView.getScene().getWindow());
97+
if (file != null) {
98+
try {
99+
String content = jsonEditorControl.getValue();
100+
System.out.println("Saving document: " + content);
101+
Files.write(file.toPath(), content.getBytes());
102+
103+
currentFile = file;
104+
// get stage and set title
105+
Stage stage = (Stage) webView.getScene().getWindow();
106+
stage.setTitle("VMF JSON Editor - " + currentFile.getName());
107+
108+
} catch (IOException e) {
109+
showError("Error saving document", e.getMessage());
110+
}
111+
}
112+
}
113+
}
114+
115+
@FXML
116+
private void handleSaveAsDocument() {
117+
FileChooser fileChooser = new FileChooser();
118+
119+
if (currentFile != null) {
120+
fileChooser.setInitialDirectory(currentFile.getParentFile());
121+
}
122+
123+
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON files (*.json)", "*.json");
124+
fileChooser.getExtensionFilters().add(extFilter);
125+
fileChooser.setTitle("Save JSON Document as");
126+
File file = fileChooser.showSaveDialog(webView.getScene().getWindow());
127+
128+
if (file != null) {
129+
try {
130+
String content = jsonEditorControl.getValue();
131+
System.out.println("Saving document as: " + content);
132+
Files.write(file.toPath(), content.getBytes());
133+
134+
currentFile = file;
135+
// get stage and set title
136+
Stage stage = (Stage) webView.getScene().getWindow();
137+
stage.setTitle("VMF JSON Editor - " + currentFile.getName());
138+
139+
} catch (IOException e) {
140+
showError("Error saving document", e.getMessage());
141+
}
142+
}
143+
}
144+
145+
@FXML
146+
private void handleQuit() {
147+
System.exit(0);
148+
}
149+
150+
@FXML
151+
private void handleBrowseSchema() {
152+
153+
FileChooser fileChooser = new FileChooser();
154+
155+
// set current directory from schemaField
156+
File currentDir = new File(schemaField.getText()).getParentFile();
157+
if (currentDir != null) {
158+
fileChooser.setInitialDirectory(currentDir);
159+
} else {
160+
//
161+
}
162+
163+
fileChooser.setTitle("Open JSON Schema");
164+
// set json extension filter
165+
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON Schema files (*.json)", "*.json");
166+
fileChooser.getExtensionFilters().add(extFilter);
167+
File file = fileChooser.showOpenDialog(webView.getScene().getWindow());
168+
if (file != null) {
169+
schemaField.setText(file.getAbsolutePath());
170+
}
171+
}
172+
173+
private String escapeJavaScript(String str) {
174+
return str.replace("\\", "\\\\")
175+
.replace("'", "\\'")
176+
.replace("\n", "\\n")
177+
.replace("\r", "\\r")
178+
.replace("\t", "\\t");
179+
}
180+
181+
private void showError(String title, String message) {
182+
Alert alert = new Alert(Alert.AlertType.ERROR);
183+
alert.setTitle("Error");
184+
alert.setHeaderText(title);
185+
alert.setContentText(message);
186+
alert.showAndWait();
187+
}
188+
}

vmfedit/src/main/java/eu/mihosoft/vmf/vmfedit/JsonEditorApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void start(Stage stage) throws IOException {
1515
FXMLLoader fxmlLoader = new FXMLLoader(JsonEditorApplication.class.getResource("json-editor-view.fxml"));
1616
Parent n = fxmlLoader.load();
1717

18-
JsonEditorController controller = fxmlLoader.getController();
18+
JsonEditorAppController controller = fxmlLoader.getController();
1919

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

0 commit comments

Comments
 (0)