private void bindServiceToControl(ServiceAndTask demoControl, Service<String> service) { demoControl.startService.setOnMouseClicked( (val) -> { if (!service.isRunning()) { service.reset(); service.start(); demoControl.labelService.textProperty().bind(service.messageProperty()); } }); demoControl.labelService.textProperty().bind(service.messageProperty()); service.addEventHandler( WorkerStateEvent.WORKER_STATE_SUCCEEDED, (val) -> { demoControl.labelService.textProperty().unbind(); demoControl.labelService.setText(service.getValue()); }); demoControl.progressService.progressProperty().bind(service.progressProperty()); }
@FXML private void updateRender() { if (renderTask != null) { if (renderTask.isRunning()) { renderTask.cancel(); } } WebEngine webEngine = renderView.getEngine(); webEngine .getLoadWorker() .stateProperty() .addListener( (ObservableValue<? extends State> ov, State oldState, State newState) -> { if (newState == State.SUCCEEDED) { scrollTo(renderView, xRenderPosition, yRenderPosition); } }); renderTask = new Service<String>() { @Override protected Task<String> createTask() { return new Task<String>() { @Override protected String call() { StringBuilder content = new StringBuilder(); content.append("<!doctype html><html><head><meta charset='utf-8'><base href='"); if (!isCancelled()) { content.append(MainApp.class.getResource("assets").toExternalForm()); } content.append("/' />"); content.append("<link rel=\"stylesheet\" href=\""); content.append(MainApp.class.getResource("css/content.css").toExternalForm()); content.append("\" />"); content.append("<link rel=\"stylesheet\" href=\""); content.append(MainApp.class.getResource("assets/static").toExternalForm()); content.append("/js/katex/katex.min.css\" />"); content .append("<script src=\"") .append(MainApp.class.getResource("assets/static").toExternalForm()); content.append("/js/katex/katex.min.js\"></script>"); content .append("<script src=\"") .append(MainApp.class.getResource("assets/static").toExternalForm()); content.append("/js/katex/contrib/auto-render.min.js\"></script>"); content.append("</head><body>"); if (!isCancelled()) { content.append(markdownToHtml(SourceText.getText())); } content.append( "<script>" + "renderMathInElement(" + "document.body," + "{" + "delimiters: [" + "{left: \"$$\", right: \"$$\", display: true}," + "{left: \"$\", right: \"$\", display: false}," + "]" + "}" + ");" + "</script>"); content.append("</body></html>"); return content.toString(); } }; } }; renderTask .stateProperty() .addListener( (ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> { switch (newValue) { case FAILED: break; case CANCELLED: break; case SUCCEEDED: yRenderPosition = getVScrollValue(renderView); xRenderPosition = getHScrollValue(renderView); webEngine.loadContent(renderTask.valueProperty().getValue()); break; } }); renderTask.start(); }