@Override
  public void start(InputEvent e, Observer observer) {
    assert e != null;
    assert e instanceof MouseEvent;
    assert e.getEventType() == MouseEvent.MOUSE_PRESSED;
    assert e.getTarget() instanceof Node;
    assert observer != null;

    this.observer = observer;
    this.eventTarget = (Node) e.getTarget();

    assert eventTarget.getOnDragDetected() == null;
    assert eventTarget.getOnMouseReleased() == null;
    assert eventTarget.getOnMouseExited() == null;

    eventTarget.setOnDragDetected(
        e1 -> {
          try {
            mouseDragDetected(e1);
          } finally {
            performTermination();
          }
        });
    eventTarget.setOnMouseReleased(
        e1 -> {
          try {
            mouseReleased(e1);
          } finally {
            performTermination();
          }
        });
    eventTarget.setOnMouseExited(
        e1 -> {
          try {
            mouseExited(e1);
          } finally {
            performTermination();
          }
        });

    try {
      mousePressed((MouseEvent) e);
    } catch (RuntimeException x) {
      performTermination();
      throw x;
    }
  }
  @Override
  public void start(InputEvent e, Observer observer) {
    assert e != null;
    assert e instanceof MouseEvent;
    assert e.getEventType() == MouseEvent.MOUSE_PRESSED;
    assert observer != null;
    assert mouseDidDrag == false;

    final Node glassLayer = contentPanelController.getGlassLayer();
    assert glassLayer.getOnDragDetected() == null;
    assert glassLayer.getOnMouseDragged() == null;
    assert glassLayer.getOnMouseReleased() == null;
    assert glassLayer.getOnKeyPressed() == null;
    assert glassLayer.getOnKeyReleased() == null;

    glassLayer.setOnDragDetected(
        new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent e) {
            lastMouseEvent = e;
            mouseDidDrag = true;
            mouseDragStarted();
            glassLayer.setOnMouseDragged(
                new EventHandler<MouseEvent>() {
                  @Override
                  public void handle(MouseEvent e) {
                    lastMouseEvent = e;
                    mouseDragged();
                  }
                });
          }
        });
    glassLayer.setOnMouseReleased(
        new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent e) {
            lastMouseEvent = e;
            try {
              if (mouseDidDrag) {
                try {
                  mouseDragEnded();
                } finally {
                  glassLayer.setOnMouseDragged(null);
                }
              }
              mouseReleased();
            } finally {
              performTermination();
            }
          }
        });
    glassLayer.setOnKeyPressed(
        new EventHandler<KeyEvent>() {
          @Override
          public void handle(KeyEvent e) {
            handleKeyPressed(e);
          }
        });
    glassLayer.setOnKeyReleased(
        new EventHandler<KeyEvent>() {
          @Override
          public void handle(KeyEvent e) {
            handleKeyReleased(e);
          }
        });

    this.mousePressedEvent = (MouseEvent) e;
    this.lastMouseEvent = mousePressedEvent;
    this.observer = observer;

    try {
      mousePressed();
    } catch (RuntimeException x) {
      performTermination();
      throw x;
    }
  }
Ejemplo n.º 3
0
  protected void fakeClick(String query) {
    baseFXRobot.waitForIdle();
    Node node = waitForNode(query);
    Point2D pos = robot.point(node).getPosition();
    baseFXRobot.mouseMove((int) pos.getX(), (int) pos.getY());
    Point2D screenPos = node.localToScreen(pos.getX(), pos.getY());
    Point2D scenePos = node.localToScene(pos.getX(), pos.getY());

    if (node instanceof Region && !(node instanceof Control)) {
      throw new IllegalArgumentException("cannot fake clicks on random regions");
    }

    runLaterAndWait(() -> waitForNode(query).requestFocus());
    if (focusSufficesClick(node)) {
      return;
    }

    MouseEvent event =
        new MouseEvent(
            MouseEvent.MOUSE_CLICKED,
            pos.getX(),
            pos.getY(),
            screenPos.getX(),
            screenPos.getY(),
            MouseButton.PRIMARY,
            1,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            true,
            false,
            false,
            new PickResult(node, scenePos.getX(), scenePos.getY()));

    EventHandler<? super MouseEvent> pressedHandler = node.getOnMousePressed();
    EventHandler<? super MouseEvent> clickedHandler = node.getOnMouseClicked();
    EventHandler<? super MouseEvent> releasedHandler = node.getOnMouseReleased();

    Platform.runLater(
        () -> {
          if (pressedHandler != null) {
            pressedHandler.handle(event);
          }
          if (clickedHandler != null) {
            clickedHandler.handle(event);
          }
          if (releasedHandler != null) {
            releasedHandler.handle(event);
          }
          if (pressedHandler == null && clickedHandler == null && releasedHandler == null) {
            node.buildEventDispatchChain(new EventDispatchChainImpl()).dispatchEvent(event);
          }
        });

    if (fireSufficesClick(node)) {
      Platform.runLater(((ButtonBase) node)::fire);
      return;
    }
  }