package com.slmn.visitor.gui;
import javafx.event.Event;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.HBox;
import com.slmn.visitor.gui.Component.HChild;
//import java.awt.Robot;
public class Leaf extends Component implements HChild {
private final TextField input = new TextField("Leaf.Input");
private final Label display = new Label("Leaf.Display");
private final HBox leaf = new HBox();
// private static Robot robot;
public Leaf(String value) {
display.setFocusTraversable(true);
leaf.setAlignment(Pos.CENTER);
leaf.setPadding(new Insets(0, SPACE, 0, SPACE));
// leaf.setStyle("-fx-border-color: red;");
// display.getStyleClass().setAll("display");
// input.getStyleClass().setAll("input");
input.setPrefWidth(100);
display.setText(value);
input.setText(value);
leaf.getChildren().add(display);
display.setOnMouseClicked(mouseEvent -> {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 2) {
leaf.getChildren().clear();
leaf.getChildren().add(input);
leaf.setStyle(null);
input.requestFocus();
} else {
display.requestFocus();
}
mouseEvent.consume();
}
});
input.setOnKeyPressed(keyEvent -> {
if (keyEvent.getCode() == KeyCode.ENTER) {
keyEvent.consume();
KeyEvent tab = new KeyEvent(keyEvent.getEventType(), null,
null, KeyCode.TAB, false, false, false, false);
Event.fireEvent(keyEvent.getTarget(), tab);
// if (robot == null)
// try {
// robot = new Robot();
// } catch (Exception e) {
// e.printStackTrace();
// }
// robot.keyPress( java.awt.event.KeyEvent.VK_TAB );
// robot.keyRelease( java.awt.event.KeyEvent.VK_TAB );
// keyEvent.consume();
}
});
input.focusedProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue == oldValue) {
return;
}
if (!newValue) {
display.setStyle(null);
leaf.getChildren().clear();
display.setText(input.getText());
leaf.getChildren().add(display);
}
});
display.focusedProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue == oldValue) {
return;
}
if (!newValue) {
Cursor.clearCursor();
} else {
Cursor.setCursor(this);
}
});
}
@Override
public final Node getNode() {
return leaf;
}
@Override
public HComposite getHParent() {
return (HComposite) getParent();
}
@Override
public Bounds getCBounds() {
return display.localToScene(display.getLayoutBounds());
}
@Override
public void addCursor() {
if (!leaf.getChildren().contains(Cursor.prompt)) {
leaf.getChildren().add(Cursor.prompt);
}
}
@Override
public void setCNodesBG(CNodeState state) {
display.setStyle(state.getStyle());
}
}