// Explicit implementation of readObject, but called implicitly as a result of recursive calls to
 // readObject() based on Serializable interface
 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
   name = new SimpleStringProperty((String) in.readObject());
   strength = new SimpleDoubleProperty(in.readDouble());
   health = new SimpleDoubleProperty(in.readDouble());
   speed = new SimpleDoubleProperty(in.readDouble());
   createAvatar();
   getAvatar().setTranslateX(in.readDouble());
   getAvatar().setTranslateY(in.readDouble());
   tooltip = new Tooltip(toString());
   Tooltip.install(getAvatar(), tooltip);
   resetAvatarAttributes();
 } // end readObject() to support serialization
  public void addImageTab(Path imagePath) {

    TabPane previewTabPane = controller.getPreviewTabPane();

    ImageTab tab = new ImageTab();
    tab.setPath(imagePath);
    tab.setText(imagePath.getFileName().toString());

    if (previewTabPane.getTabs().contains(tab)) {
      previewTabPane.getSelectionModel().select(tab);
      return;
    }

    Image image = new Image(IOHelper.pathToUrl(imagePath));
    ImageView imageView = new ImageView(image);
    imageView.setPreserveRatio(true);

    imageView.setFitWidth(previewTabPane.getWidth());

    previewTabPane
        .widthProperty()
        .addListener(
            (observable, oldValue, newValue) -> {
              imageView.setFitWidth(previewTabPane.getWidth());
            });

    Tooltip tip = new Tooltip(imagePath.toString());
    Tooltip.install(tab.getGraphic(), tip);

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
    scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
    scrollPane.setContent(imageView);
    scrollPane.addEventFilter(
        ScrollEvent.SCROLL,
        e -> {
          if (e.isControlDown() && e.getDeltaY() > 0) {
            // zoom in
            imageView.setFitWidth(imageView.getFitWidth() + 16.0);
          } else if (e.isControlDown() && e.getDeltaY() < 0) {
            // zoom out
            imageView.setFitWidth(imageView.getFitWidth() - 16.0);
          }
        });

    tab.setContent(scrollPane);

    TabPane tabPane = previewTabPane;
    tabPane.getTabs().add(tab);
    tabPane.getSelectionModel().select(tab);
  }
 /**
  * <i>Actor</i> constructor is used when building <i>Actor</i> objects automatically:
  * <i>strength</i>, <i>health</i>, <i>speed</i> fields are given randomly generated values within
  * their range; <i>name</i> is given a sequentially numbered name: <i>Auto:<b>n</b></i> where
  * <i><b>n</b></i> is the sequence number. The <i>name</i> can be edited to create an unique
  * <i>Actor</i>.
  *
  * @param subclassCount used to support automatic naming (which includes a unique serial number).
  * @param armyAllegiance used to support the <i>Army</i>-specific <i>DropShadow</i> glow around
  *     this Actor object.
  */
 public Actor(int subclassCount, Army armyAllegiance) {
   this.armyAllegiance = armyAllegiance;
   ++actorSerialNumber; // static class-oriented variable. There is one-and-only-one instance of
                        // this variable regardless of the number of Actor objects in existence
                        // (from none to infinity).
   setName(
       String.format(
           "%d:%s:%d:",
           actorSerialNumber,
           getClass().getSimpleName(),
           subclassCount)); // An alternate way to assemble a String to use as a name. Because of
                            // polymorphism "getClass().getName()" will return the subclass name
                            // when they exist.
   setStrength(SingletonRandom.instance.getNormalDistribution(MIN_STRENGTH, MAX_STRENGTH, 2.0));
   setHealth(SingletonRandom.instance.getNormalDistribution(MIN_HEALTH, MAX_HEALTH, 2.0));
   setSpeed(SingletonRandom.instance.getNormalDistribution(MIN_SPEED, MAX_SPEED, 2.0));
   createAvatar();
   tooltip = new Tooltip(toString());
   Tooltip.install(getAvatar(), tooltip);
   tt = new TranslateTransition();
   tt.setNode(getAvatar()); // reuse
 } // end Actor constructor
Exemple #4
0
  /**
   * Drawing function for Circles and Lines
   *
   * @param drawPane
   * @param coords Circle Coordinates
   * @param edges array of edges
   * @param startIndex NumberOfNodes
   */
  private void drawShapes(Pane drawPane, double[][] coords, int[][] edges, int startIndex) {

    // clear previous pane
    drawPane.getChildren().clear();
    circleList = new ArrayList<>();

    // generate Circles
    for (int i = 0; i < coords.length; i++) {
      Circle currentCircle = new Circle(coords[i][0], coords[i][1], nodeSize, Color.BLACK);
      String toolTipText = "Node " + Integer.toString(i + 1);
      // expand toolTip text with nucleotide and Circle color, if possible
      if (sequenceField.getText().length() > i) {
        toolTipText += ": " + sequenceField.getText().charAt(i);
        currentCircle.setFill(getNodeColor(sequenceField.getText().charAt(i)));
      }
      Tooltip.install(currentCircle, new Tooltip(toolTipText));
      if (isAnimated) {
        currentCircle.setOnMousePressed(circleOnMousePressedEventHandler);
        currentCircle.setOnMouseDragged(circleOnMouseDraggedEventHandler);
        currentCircle.setOnMouseReleased(circleOnMouseReleasedEventHandler);
      }

      circleList.add(currentCircle);
    }

    // generate  basic Lines
    ArrayList<Line> lineList = new ArrayList<>();
    for (int i = 0; i < circleList.size() - 1; i++) {
      Line line = new Line();
      line.setStroke(Color.BLACK);
      line.setFill(Color.BLACK);
      Circle circle1 = circleList.get(i);
      Circle circle2 = circleList.get(i + 1);

      // bind ends of line:
      line.startXProperty().bind(circle1.centerXProperty().add(circle1.translateXProperty()));
      line.startYProperty().bind(circle1.centerYProperty().add(circle1.translateYProperty()));
      line.endXProperty().bind(circle2.centerXProperty().add(circle2.translateXProperty()));
      line.endYProperty().bind(circle2.centerYProperty().add(circle2.translateYProperty()));

      lineList.add(line);
    }

    // generate edges
    for (int i = startIndex - 1; i < edges.length; i++) {
      Line line = new Line();
      line.setStroke(Color.ORANGE);
      Circle circle1 = circleList.get(edges[i][0]);
      Circle circle2 = circleList.get(edges[i][1]);

      line.startXProperty().bind(circle1.centerXProperty().add(circle1.translateXProperty()));
      line.startYProperty().bind(circle1.centerYProperty().add(circle1.translateYProperty()));
      line.endXProperty().bind(circle2.centerXProperty().add(circle2.translateXProperty()));
      line.endYProperty().bind(circle2.centerYProperty().add(circle2.translateYProperty()));

      lineList.add(line);
    }

    drawPane.getChildren().addAll(lineList);
    drawPane.getChildren().addAll(circleList);
  }
  public void addTab(Path path, Runnable... runnables) {

    ObservableList<String> recentFiles = controller.getRecentFilesList();
    if (Files.notExists(path)) {
      recentFiles.remove(path.toString());
      return;
    }

    ObservableList<Tab> tabs = controller.getTabPane().getTabs();
    for (Tab tab : tabs) {
      MyTab myTab = (MyTab) tab;
      Path currentPath = myTab.getPath();
      if (Objects.nonNull(currentPath))
        if (currentPath.equals(path)) {
          myTab.select(); // Select already added tab
          return;
        }
    }

    AnchorPane anchorPane = new AnchorPane();
    EditorPane editorPane = webviewService.createWebView();

    MyTab tab = createTab();
    tab.setEditorPane(editorPane);
    tab.setTabText(path.getFileName().toString());

    editorPane.confirmHandler(
        param -> {
          if ("command:ready".equals(param)) {
            JSObject window = editorPane.getWindow();
            window.setMember("afx", controller);
            window.call("updateOptions", new Object[] {});
            Map<String, String> shortCuts = controller.getShortCuts();
            Set<String> keySet = shortCuts.keySet();
            for (String key : keySet) {
              window.call("addNewCommand", new Object[] {key, shortCuts.get(key)});
            }
            if (Objects.isNull(path)) return true;
            threadService.runTaskLater(
                () -> {
                  String content = IOHelper.readFile(path);
                  threadService.runActionLater(
                      () -> {
                        window.call("changeEditorMode", path.toUri().toString());
                        window.call("setInitialized");
                        window.call("setEditorValue", new Object[] {content});
                        for (Runnable runnable : runnables) {
                          runnable.run();
                        }
                      });
                });
          }
          return false;
        });

    threadService.runActionLater(
        () -> {
          TabPane tabPane = controller.getTabPane();
          tabPane.getTabs().add(tab);
          tab.select();
        });

    Node editorVBox = editorService.createEditorVBox(editorPane, tab);
    controller.fitToParent(editorVBox);

    anchorPane.getChildren().add(editorVBox);
    tab.setContent(anchorPane);
    tab.setPath(path);

    Tooltip tip = new Tooltip(path.toString());
    Tooltip.install(tab.getGraphic(), tip);

    recentFiles.remove(path.toString());
    recentFiles.add(0, path.toString());

    editorPane.focus();
  }
  // drawing
  public void drawGraph() {

    Timeline timeline = new Timeline();

    if (graph != null && finalCoordinates != null) {

      // reset pane
      drawing.getChildren().clear();
      SpringEmbedder.centerCoordinates(finalCoordinates, 20, 380, 20, 380);
      SpringEmbedder.centerCoordinates(startingCoordinates, 20, 380, 20, 380);

      // draw nucleotides
      circles = new Circle[finalCoordinates.length];
      for (int i = 0; i < finalCoordinates.length; i++) {
        // create nodes
        Circle circle = new Circle(5);
        circle.setCenterX(finalCoordinates[i][0]);
        circle.setCenterY(finalCoordinates[i][1]);
        switch (graph.getSequence().charAt(i)) {
          case 'A':
            circle.setFill(Color.YELLOW);
            break;
          case 'a':
            circle.setFill(Color.YELLOW);
            break;
          case 'U':
            circle.setFill(Color.GREEN);
            break;
          case 'u':
            circle.setFill(Color.GREEN);
            break;
          case 'C':
            circle.setFill(Color.BLUE);
            break;
          case 'c':
            circle.setFill(Color.BLUE);
            break;
          case 'G':
            circle.setFill(Color.RED);
            break;
          case 'g':
            circle.setFill(Color.RED);
            break;
        }
        circle.setStroke(Color.BLACK);
        // add tooltip
        Tooltip t =
            new Tooltip("Nucleotide: " + graph.getSequence().charAt(i) + "\nPosition: " + i);
        Tooltip.install(circle, t);

        // add drag-and-drop behaviour
        circle.setOnMousePressed(
            event -> {
              if (isAnimationActivated) {
                dragStartX = circle.getCenterX();
                dragStartY = circle.getCenterY();
              }
            });

        circle.setOnMouseDragged(
            event -> {
              if (isAnimationActivated) {
                circle.setCenterX(event.getX());
                circle.setCenterY(event.getY());
              }
            });

        circle.setOnMouseReleased(
            event -> {
              if (isAnimationActivated) {
                Timeline dragTimeline = new Timeline();
                KeyValue circlePositionXKey = new KeyValue(circle.centerXProperty(), dragStartX);
                KeyValue circlePositionYKey = new KeyValue(circle.centerYProperty(), dragStartY);
                dragTimeline
                    .getKeyFrames()
                    .add(new KeyFrame(Duration.millis(animationTime), circlePositionXKey));
                dragTimeline
                    .getKeyFrames()
                    .add(new KeyFrame(Duration.millis(animationTime), circlePositionYKey));
                dragTimeline.play();
              }
            });

        // save circle locally
        circles[i] = circle;

        // animate circles
        KeyValue startingXKey = new KeyValue(circle.centerXProperty(), startingCoordinates[i][0]);
        KeyValue finalXKey = new KeyValue(circle.centerXProperty(), circle.getCenterX());
        KeyValue startingYKey = new KeyValue(circle.centerYProperty(), startingCoordinates[i][1]);
        KeyValue finalYKey = new KeyValue(circle.centerYProperty(), circle.getCenterY());
        timeline.getKeyFrames().add(new KeyFrame(Duration.millis(0), startingXKey));
        timeline.getKeyFrames().add(new KeyFrame(Duration.millis(0), startingYKey));
        timeline.getKeyFrames().add(new KeyFrame(Duration.millis(animationTime), finalXKey));
        timeline.getKeyFrames().add(new KeyFrame(Duration.millis(animationTime), finalYKey));
      }

      // create edges
      int[][] edges = graph.getEdges();
      for (int i = 0; i < edges.length; i++) {
        // bind edges to nodes
        Line line =
            new Line(
                finalCoordinates[edges[i][0]][0],
                finalCoordinates[edges[i][0]][1],
                finalCoordinates[edges[i][1]][0],
                finalCoordinates[edges[i][1]][1]);
        line.startYProperty().bind(circles[edges[i][0]].centerYProperty());
        line.startXProperty().bind(circles[edges[i][0]].centerXProperty());
        line.endYProperty().bind(circles[edges[i][1]].centerYProperty());
        line.endXProperty().bind(circles[edges[i][1]].centerXProperty());

        // draw edges
        if (edges[i][1] - edges[i][0] != 1) {
          line.setStroke(Color.CADETBLUE);
        } else {
          line.setFill(Color.DARKGRAY);
        }
        drawing.getChildren().add(line);
      }

      // draw all circles in order to overlap the edges
      drawing.getChildren().addAll(circles);

      if (isAnimationActivated) {
        timeline.play();
      }

    } else {
      throw new IllegalArgumentException(
          "graph is null: "
              + graph.equals(null)
              + "\ncoordinates is null: "
              + finalCoordinates.equals(null));
    }
  }