import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * Mouse events and the data you can read from them. Everything prints to the console.
 *
 * Two pads, A and B, side by side. Both use the same handlers. Click at the same spot in each:
 * getX / getY are the same (measured from the pad's own corner),
 * getSceneX / getSceneY are different (measured from the window's corner).
 *
 * Event types handled: pressed, released, clicked, entered, exited, dragged.
 * Try: hold a button and move (dragged); right-click; double-click; shift-click or ctrl-click.
 */
public class SimpleMouseEventDemo extends Application {

	// Print the useful data from one mouse event.
	private void report ( String pad, MouseEvent e ) {
		System.out.println(pad + "  " + e.getEventType() + "   getX/getY "
		    + (int) e.getX() + ", " + (int) e.getY() + "   getSceneX/getSceneY "
		    + (int) e.getSceneX() + ", " + (int) e.getSceneY() + "   getButton "
		    + e.getButton() + "   getClickCount " + e.getClickCount() + "   shift "
		    + e.isShiftDown() + " control " + e.isControlDown() + " alt "
		    + e.isAltDown());
	}

	// A pad that reports the mouse events it receives.
	private Label pad ( String name, String color ) {
		Label pad = new Label("pad " + name);
		pad.setPrefSize(180,140);
		pad.setAlignment(Pos.CENTER);
		pad.setStyle("-fx-background-color: " + color
		    + "; -fx-border-color: #333;");

		pad.setOnMousePressed(e -> report(name,e)); // a button goes down
		pad.setOnMouseReleased(e -> report(name,e)); // a button comes up
		pad.setOnMouseClicked(e -> report(name,e)); // pressed and released on the
		                                            // same node
		pad.setOnMouseEntered(e -> report(name,e)); // the pointer moves onto the
		                                            // pad
		pad.setOnMouseExited(e -> report(name,e)); // the pointer moves off the pad
		pad.setOnMouseDragged(e -> report(name,e)); // the pointer moves with a
		                                            // button held
		// try: pad.setOnMouseMoved(e -> report(name, e)); // the pointer moves, no
		// button held (prints a lot)
		return pad;
	}

	@Override
	public void start ( Stage stage ) {
		HBox pads = new HBox(30,pad("A","#cfe8ff"),pad("B","#c8f0c8"));
		VBox root =
		    new VBox(15,
		             new Label("Click, drag and double-click on the pads. Watch the console."),
		             pads);
		root.setPadding(new Insets(20));

		stage.setScene(new Scene(root,440,230));
		stage.setTitle("Mouse events");
		stage.show();
	}

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