Skip to content

Commit 6ed989a

Browse files
committed
Moved window management into ViewController for switching scenes
1 parent 8b41de7 commit 6ed989a

File tree

17 files changed

+84
-32
lines changed

17 files changed

+84
-32
lines changed

src/main/java/com/codeboy/Main.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codeboy;
22

33
import com.codeboy.controller.FileController;
4+
import com.codeboy.controller.ViewController;
45
import com.codeboy.controller.VulnerabilityController;
56
import com.codeboy.controller.WeaknessController;
67
import com.codeboy.utility.FileReaderUtility;
@@ -30,33 +31,13 @@ public void start(Stage stage) {
3031
WebView webView = new WebView();
3132
WebEngine webEngine = webView.getEngine();
3233

34+
ViewController viewController = new ViewController(webEngine);
35+
3336
webEngine.setOnError(event -> LOGGER.warning("[Frontend] An error occured on the frontend: " + event.getMessage()));
3437
File htmlFile = new File(getClass().getResource("/html/index.html").getFile());
3538
webEngine.load(htmlFile.toURI().toString());
3639
webEngine.setJavaScriptEnabled(true);
3740

38-
// Wait until WebView is fully loaded before injecting WeaknessController
39-
webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
40-
if (newState == javafx.concurrent.Worker.State.SUCCEEDED) {
41-
JSObject window = (JSObject) webEngine.executeScript("window");
42-
window.setMember("fileController", FileController.getInstance());
43-
window.setMember("weaknessController", WeaknessController.getInstance());
44-
window.setMember("vulnerabilityController", VulnerabilityController.getInstance());
45-
window.setMember("fileReader", FileReaderUtility.getInstance());
46-
47-
// TODO: remove later - just for debugging purposes
48-
window.setMember("javascriptBridge", new JavaScriptBridge());
49-
webEngine.executeScript("""
50-
console.log = function(msg) {
51-
javascriptBridge.log(msg);
52-
};
53-
console.error = function(msg) {
54-
javascriptBridge.error(msg);
55-
};
56-
""");
57-
}
58-
});
59-
6041
StackPane root = new StackPane(webView);
6142
Scene scene = new Scene(root, 1500, 900);
6243
stage.setScene(scene);
@@ -74,4 +55,6 @@ public void start(Stage stage) {
7455
}
7556
stage.show();
7657
}
58+
59+
7760
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.codeboy.controller;
2+
3+
import com.codeboy.utility.FileReaderUtility;
4+
import com.codeboy.utility.JavaScriptBridge;
5+
import javafx.concurrent.Worker;
6+
import javafx.scene.web.WebEngine;
7+
import netscape.javascript.JSObject;
8+
9+
import java.io.File;
10+
import java.util.function.Consumer;
11+
12+
// TODO:
13+
// https://stackoverflow.com/questions/32564195/load-a-new-page-in-javafx-webview
14+
public class ViewController {
15+
16+
private JSObject window;
17+
18+
public ViewController(WebEngine webEngine) {
19+
webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
20+
if (newState == javafx.concurrent.Worker.State.SUCCEEDED) {
21+
JSObject window = (JSObject) webEngine.executeScript("window");
22+
window.setMember("viewController", new ViewController(webEngine));
23+
window.setMember("fileController", FileController.getInstance());
24+
window.setMember("weaknessController", WeaknessController.getInstance());
25+
window.setMember("vulnerabilityController", VulnerabilityController.getInstance());
26+
window.setMember("fileReader", FileReaderUtility.getInstance());
27+
28+
// TODO: remove later - just for debugging purposes
29+
window.setMember("javascriptBridge", new JavaScriptBridge());
30+
webEngine.executeScript("""
31+
console.log = function(msg) {
32+
javascriptBridge.log(msg);
33+
};
34+
console.error = function(msg) {
35+
javascriptBridge.error(msg);
36+
};
37+
""");
38+
}
39+
});
40+
}
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Analytics</title>
6+
</head>
7+
<body>
8+
<h1>Test</h1>
9+
</body>
10+
</html>

src/main/resources/html/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ <h3>Recent Activity</h3>
107107
<section>
108108
<ol id="list"></ol>
109109
</section>
110+
111+
<button onclick="goTo('analytics.html')"></button>
112+
110113
</body>
111114

112115
</html>

src/main/resources/js/script.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function handleFileProcessing() {
88
handleFileSelection(filePath);
99
addToRecentFiles(filePath);
1010
loadRecentFiles();
11+
goTo('analytics.html')
1112
}
1213

1314
function handleFileSelection(filePath) {
@@ -135,3 +136,7 @@ function loadRecentFiles() {
135136
}
136137
}
137138

139+
function goTo(path) {
140+
window.location.href = path;
141+
}
142+

target/classes/html/analytics.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Analytics</title>
6+
</head>
7+
<body>
8+
<h1>Test</h1>
9+
</body>
10+
</html>

target/classes/html/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ <h3>Recent Activity</h3>
107107
<section>
108108
<ol id="list"></ol>
109109
</section>
110+
111+
<button onclick="goTo('analytics.html')"></button>
112+
110113
</body>
111114

112115
</html>

target/classes/icons/list.png

-186 Bytes
Binary file not shown.

target/classes/js/script.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function handleFileProcessing() {
88
handleFileSelection(filePath);
99
addToRecentFiles(filePath);
1010
loadRecentFiles();
11+
goTo('analytics.html')
1112
}
1213

1314
function handleFileSelection(filePath) {
@@ -135,3 +136,7 @@ function loadRecentFiles() {
135136
}
136137
}
137138

139+
function goTo(path) {
140+
window.location.href = path;
141+
}
142+
-1.17 MB
Binary file not shown.

0 commit comments

Comments
 (0)