Use Webview based logger

This commit is contained in:
XePeleato 2020-09-17 02:15:58 +02:00
parent 5ffe2dca43
commit c6f2cd75b3
3 changed files with 82 additions and 44 deletions

View File

@ -6,18 +6,16 @@ import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.ui.logger.loggerdisplays.PacketLogger; import gearth.ui.logger.loggerdisplays.PacketLogger;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.InvalidationListener; import javafx.concurrent.Worker;
import javafx.beans.Observable;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.CheckMenuItem; import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane; import javafx.scene.layout.FlowPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.StyleClassedTextArea;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
@ -37,7 +35,7 @@ public class UiLoggerController implements Initializable {
public CheckMenuItem chkMessageHash; public CheckMenuItem chkMessageHash;
public Label lblHarbleAPI; public Label lblHarbleAPI;
private StyleClassedTextArea area; private WebView webView;
private Stage stage; private Stage stage;
@ -55,20 +53,22 @@ public class UiLoggerController implements Initializable {
@Override @Override
public void initialize(URL arg0, ResourceBundle arg1) { public void initialize(URL arg0, ResourceBundle arg1) {
area = new StyleClassedTextArea(); webView = new WebView();
area.getStyleClass().add("dark");
area.setWrapText(true);
VirtualizedScrollPane<StyleClassedTextArea> vsPane = new VirtualizedScrollPane<>(area); borderPane.setCenter(webView);
borderPane.setCenter(vsPane);
synchronized (appendLater) { webView.getEngine().getLoadWorker().stateProperty().addListener((observableValue, oldState, newState) -> {
initialized = true; if (newState == Worker.State.SUCCEEDED)
if (!appendLater.isEmpty()) { synchronized (appendLater) {
appendLog(appendLater); initialized = true;
appendLater.clear(); if (!appendLater.isEmpty()) {
} appendLog(appendLater);
} appendLater.clear();
}
}
});
webView.getEngine().load(getClass().getResource("/gearth/ui/logger/uilogger/logger.html").toString());
} }
private static String cleanTextContent(String text) { private static String cleanTextContent(String text) {
@ -168,27 +168,54 @@ public class UiLoggerController implements Initializable {
private synchronized void appendLog(List<Element> elements) { private synchronized void appendLog(List<Element> elements) {
Platform.runLater(() -> { Platform.runLater(() -> {
StringBuilder sb = new StringBuilder();
StyleSpansBuilder<Collection<String>> styleSpansBuilder = new StyleSpansBuilder<>(0);
for (Element element : elements) { for (Element element : elements) {
sb.append(element.text); String script = "$('#output').append('<span class=\"" + element.className + "\">"
+ escapeMessage(element.text) + "</span>');";
styleSpansBuilder.add(Collections.singleton(element.className), element.text.length()); try {
executejQuery(webView.getEngine(), script);
} catch (Exception e) {
System.out.println("Malformed JS message " + script);
}
} }
int oldLen = area.getLength();
area.appendText(sb.toString());
// System.out.println(sb.toString());
area.setStyleSpans(oldLen, styleSpansBuilder.create());
if (autoScroll) { if (autoScroll) {
// area.moveTo(area.getLength()); webView.getEngine().executeScript("window.scrollTo(0, document.body.scrollHeight);");
area.requestFollowCaret();
} }
}); });
} }
// escapes logger text so that there are no javascript errors
private String escapeMessage(String text) {
return text
.replace("\n\r", "<br />")
.replace("\n", "<br />")
.replace("\r", "<br />")
.replace("'", "\\'");
}
private static Object executejQuery(final WebEngine engine, String script) {
return engine.executeScript(
"(function(window, document, version, callback) { "
+ "var j, d;"
+ "var loaded = false;"
+ "if (!(j = window.jQuery) || version > j.fn.jquery || callback(j, loaded)) {"
+ " var script = document.createElement(\"script\");"
+ " script.type = \"text/javascript\";"
+ " script.src = \"http://code.jquery.com/jquery-1.7.2.min.js\";"
+ " script.onload = script.onreadystatechange = function() {"
+ " if (!loaded && (!(d = this.readyState) || d == \"loaded\" || d == \"complete\")) {"
+ " callback((j = window.jQuery).noConflict(1), loaded = true);"
+ " j(script).remove();"
+ " }"
+ " };"
+ " document.documentElement.childNodes[0].appendChild(script) "
+ "} "
+ "})(window, document, \"1.7.2\", function($, jquery_loaded) {" + script + "});"
);
}
public void setStage(Stage stage) { public void setStage(Stage stage) {
this.stage = stage; this.stage = stage;
} }
@ -233,6 +260,6 @@ public class UiLoggerController implements Initializable {
} }
public void clearText(ActionEvent actionEvent) { public void clearText(ActionEvent actionEvent) {
area.clear(); webView.getEngine().executeScript("$('#output').html = \\'\\'");
} }
} }

View File

@ -1,47 +1,47 @@
/* packet logger css */ /* packet logger css */
.text { .text {
-fx-fill: #a9a9a9; color: #a9a9a9;
} }
.messageinfo { .messageinfo {
-fx-fill: #D0D3D4; color: #D0D3D4;
} }
.blocked, .replaced { .blocked, .replaced {
-fx-fill: #ffff00; color: #ffff00;
} }
.incoming { .incoming {
-fx-fill: #b22222; color: #b22222;
} }
.outgoing { .outgoing {
-fx-fill: #0066cc; color: #0066cc;
} }
.structure, .skipped { .structure, .skipped {
-fx-fill: cyan; color: cyan;
} }
.dark { .dark {
-fx-background-color: #000000; background-color: #000000;
color: #a9a9a9;
} }
.caret { .caret {
-fx-stroke: #ffffff; stroke: #ffffff;
} }
.label { .label {
-fx-text-fill: #000000 !important; color: #000000 !important;
} }
.menu-bar .text, .menu .text { .menu-bar .text, .menu .text {
-fx-text-fill: #000000 !important; color: #000000 !important;
-fx-fill: #000000 !important; padding: -2px 0 -2px 0 !important;
-fx-padding: -2 0 -2 0 !important;
} }
.scroll-bar:vertical { .scroll-bar:vertical {
-fx-pref-width: 16.5; width: 17px;
-fx-padding: 1; padding: 1px;
} }

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>G-Earth Logger</title>
<link rel="stylesheet" href="logger.css">
</head>
<body>
<div id="output" class="dark"></div>
</body>
</html>