Example #1
0
 private void nextPane(Service<Rectangle> recBuilder) {
   loading[counter].textProperty().bind(recBuilder.messageProperty());
   indicators[counter]
       .visibleProperty()
       .bind(
           recBuilder
               .progressProperty()
               .isNotEqualTo(new SimpleDoubleProperty(ProgressBar.INDETERMINATE_PROGRESS)));
   recBuilder.restart();
 }
  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());
  }
Example #3
0
  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();
  }