import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

import com.sun.glass.ui.Robot;

import java.awt.event.MouseEvent;

public class RobotTest extends Application {

    public void delay(int delay) {
        try { Thread.sleep(delay); } catch (Exception e) {}
    }

    @Override
    public void start(Stage primaryStage) {

        final Button btn = new Button();
        btn.setText("Click to make this button be clicked by a robot");
        btn.setOnAction(buttonActionHandler(btn));

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private EventHandler<ActionEvent> buttonActionHandler(final Button button) {

        return new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("CLICKED");
                final Scene scene = button.getScene();
                final Point2D windowCoordinates = new Point2D(scene.getWindow().getX(), scene.getWindow().getY());
                final Point2D sceneCoordinates = new Point2D(scene.getX(), scene.getY());
                final Point2D nodeCoordinates = button.localToScene(0.0, 0.0);
                final double clickX = Math.round(windowCoordinates.getX() + sceneCoordinates.getX() + nodeCoordinates.getX());
                final double clickY = Math.round(windowCoordinates.getY() + sceneCoordinates.getY() + nodeCoordinates.getY());
                clickMeAgainLater(clickX, clickY);
            }

            private void clickMeAgainLater(double x, double y) {
                Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(2), clickIt(x, y)));
                timeline.play();
            }

            private EventHandler<ActionEvent> clickIt(final double x, final double y) {
                return (ActionEvent t) -> {
                    try {
                        Robot r = com.sun.glass.ui.Application.GetApplication().createRobot();
                        r.mouseMove((int) x + 10, (int) y + 10);
                        r.mousePress(MouseEvent.BUTTON1);
                        delay(100);
                        r.mouseRelease(MouseEvent.BUTTON1);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                };
            }
        };
    }

    public static void main(String[] args) { launch(args); }
}
