<commit_before>package codeOrchestra.colt.core.ui.components.logVisualizer

import codeOrchestra.colt.core.ui.components.log.LogFilter
import codeOrchestra.colt.core.ui.components.log.LogMessage
import javafx.application.Platform
import javafx.beans.value.ChangeListener
import javafx.beans.value.ObservableValue
import javafx.collections.FXCollections
import javafx.collections.ListChangeListener
import javafx.collections.ObservableList as OL
import javafx.event.EventHandler
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.layout.Priority
import javafx.scene.layout.VBox
import javafx.scene.text.Font
import javafx.scene.web.WebEngine
import javafx.scene.web.WebEvent
import javafx.scene.web.WebView
import netscape.javascript.JSObject

import static codeOrchestra.colt.core.logging.Level.*

/**
 * @author <NAME>
 */
class LogVisualizer extends VBox {

    private WebView webView = new WebView(contextMenuEnabled: false, prefHeight: 130)
    OL<LogMessage> logMessages
    private boolean htmlLoaded;
    private boolean layoutInited;

    LogVisualizer() {

        String htmlPage = this.class.getResource("html/log-visualizer-webview.html").toExternalForm()
        WebEngine engine = webView.engine
        engine.documentProperty().addListener({ o, oldValue, newValue ->
            htmlLoaded = true
            if (layoutInited && htmlLoaded) {
                // init logic
            }

        } as ChangeListener)
        engine.load(htmlPage)
        children.add(webView)
        setVgrow(webView, Priority.ALWAYS)

        engine.onAlert = new EventHandler<WebEvent<String>>() {
            @Override
            void handle(WebEvent<String> event) {
                println("alert >> " + event.data)
            }
        }
    }

    void setLogMessages(OL<LogMessage> logMessages) {
        this.logMessages = logMessages
        logMessages.addListener({ ListChangeListener.Change<? extends LogMessage> c ->
            synchronized (logMessages) {
                if (htmlLoaded) {
                    while (c.next()) {
                        if (c.wasRemoved()) {
                            println("clear log")
                        } else if (c.wasPermutated()) {
                            println "permutated"
                        } else if (c.wasUpdated()) {
                            println "updated"
                        } else {
                            println("added")
                            addLogMessages()
                        }
                    }
                }
            }

        } as ListChangeListener)
    }

    private JSObject getJSTopObject() {
        (JSObject) webView.engine.executeScript("window")
    }

    private void addLogMessages(List<LogMessage> messages) {
        getJSTopObject().call("alert", [messages])
    }

}<commit_msg>log visualizer v1<commit_after>package codeOrchestra.colt.core.ui.components.logVisualizer

import codeOrchestra.colt.core.ui.components.log.LogFilter
import codeOrchestra.colt.core.ui.components.log.LogMessage
import javafx.application.Platform
import javafx.beans.value.ChangeListener
import javafx.beans.value.ObservableValue
import javafx.collections.FXCollections
import javafx.collections.ListChangeListener
import javafx.collections.ObservableList as OL
import javafx.event.EventHandler
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.layout.Priority
import javafx.scene.layout.VBox
import javafx.scene.text.Font
import javafx.scene.web.WebEngine
import javafx.scene.web.WebEvent
import javafx.scene.web.WebView
import netscape.javascript.JSObject

import static codeOrchestra.colt.core.logging.Level.*

/**
 * @author <NAME>
 */
class LogVisualizer extends VBox {

    private WebView webView = new WebView(contextMenuEnabled: false, prefHeight: 130)
    OL<LogMessage> logMessages
    private boolean htmlLoaded
    final private List flushList = []

    LogVisualizer() {

        String htmlPage = this.class.getResource("html/log-visualizer-webview.html").toExternalForm()
        WebEngine engine = webView.engine
        engine.documentProperty().addListener({ o, oldValue, newValue ->
            // too early here
            //htmlLoaded = true

        } as ChangeListener)
        engine.load(htmlPage)
        children.add(webView)
        setVgrow(webView, Priority.ALWAYS)

        engine.onAlert = new EventHandler<WebEvent<String>>() {
            @Override
            void handle(WebEvent<String> event) {
                if (event.data == "ready") {
                    htmlLoaded = true;
                    flush()
                } else {
                    println("alert >> " + event.data)
                }
            }
        }
    }

    void setLogMessages(OL<LogMessage> logMessages) {
        this.logMessages = logMessages
        logMessages.addListener({ ListChangeListener.Change<? extends LogMessage> c ->
            synchronized (logMessages) {
                // htmlLoaded check moved to flush()
                while (c.next()) {
                    if (c.wasRemoved()) {
                        println("clear log")
                    } else if (c.wasPermutated()) {
                        println "permutated"
                    } else if (c.wasUpdated()) {
                        println "updated"
                    } else {
                        addLogMessages(c.getAddedSubList().asList())
                    }
                }
            }

        } as ListChangeListener)
    }

    private JSObject getJSTopObject() {
        (JSObject) webView.engine.executeScript("window")
    }

    private void addLogMessages(List<LogMessage> messages) {
        synchronized (flushList) {
            flushList.addAll(messages*.level)
            Platform.runLater {
                flush()
            }
        }
    }

    private flush() {
        synchronized (flushList) {
            if (htmlLoaded && (flushList.size() > 0)) {
                getJSTopObject().call("addLogMessages", flushList)
                flushList.clear()
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09403520208604954, 'fraction_numerical': 0.001303780964797914, 'mean_word_length': 2.051715564395823, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1118735', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1610, 'n_words': 312}