private void loadPanels(Group root) { // change to loadPanel: root.getChildren().set(0, createLoadPane()); // Service: final Service<Rectangle> RecBuilder = new Service<Rectangle>() { @Override protected Task<Rectangle> createTask() { return new Task<Rectangle>() { @Override protected Rectangle call() throws InterruptedException { updateMessage("loading rectangle . . ."); updateProgress(0, 10); for (int i = 0; i < 10; i++) { Thread.sleep(100); } updateMessage("Finish!"); return new Rectangle((380) / 3, (380) / 3, colors[counter]); } }; } }; // StateListener RecBuilder.stateProperty() .addListener( new ChangeListener<Worker.State>() { @Override public void changed( ObservableValue<? extends Worker.State> observableValue, Worker.State oldState, Worker.State newState) { switch (newState) { case SCHEDULED: break; case READY: case RUNNING: break; case SUCCEEDED: Rectangle rec = RecBuilder.valueProperty().getValue(); indicators[counter].progressProperty().unbind(); loading[counter].textProperty().unbind(); loadPane.getChildren().set(counter, rec); if (counter < 8) { counter++; nextPane(RecBuilder); } break; case CANCELLED: case FAILED: loading[counter].textProperty().unbind(); loading[counter].setText("Failed!"); if (counter < 8) { counter++; nextPane(RecBuilder); } break; } } }); // begin PanelBuilding: nextPane(RecBuilder); }
@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(); }