package bugs;
    
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.event.EventHandler;
import javafx.scene.Scene; 
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox;
import javafx.scene.shape.Ellipse; 
import javafx.stage.Stage; 
import javafx.util.Duration;


public class JDK8137252_StackPane1 extends Application { 

    enum ExecType {
        DIRECT,
        RUN_LATER,
        ANIMATION
    }

    private ExecType execType = ExecType.DIRECT;

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

    @Override 
    public void start(Stage stage) throws Exception {

        StackPane sp = new StackPane();
        Label l1 = new Label("1 2");
//        Rectangle l1 = new Rectangle(100, 50);
        Ellipse e1 = new Ellipse(20, 30); 
        e1.setOpacity(0.5); 
        sp.getChildren().addAll(l1, e1);
        l1.widthProperty().addListener((o, oVal, newVal) -> e1.setRadiusX(l1.getWidth()));
        l1.heightProperty().addListener((o, oVal, newVal) -> e1.setRadiusY(l1.getHeight()));
//        l1.widthProperty().addListener((o) -> e1.setRadiusX(l1.getWidth()));
//        l1.heightProperty().addListener((o) -> e1.setRadiusY(l1.getHeight()));

//        e1.radiusXProperty().bind(l1.widthProperty()); 
//        e1.radiusYProperty().bind(l1.heightProperty());

        Pane topPane = new Pane();
        VBox rootPane = new VBox(10);

        Button btn1 = new Button();
        btn1.setText("Set Label to \"A B C D E F G\"");
        btn1.setOnAction(new EventHandler<javafx.event.ActionEvent>() {

            @Override
            public void handle(javafx.event.ActionEvent event) {
                switch (execType) {
                    case DIRECT:
                        l1.setText("A B C D E F G");                     
                        break;
                    case RUN_LATER:
                        Platform.runLater(() -> l1.setText("A B C D E F G"));
                        break;
                    case ANIMATION:
                        KeyFrame kFrame = new KeyFrame(Duration.millis(1), e -> l1.setText("A B C D E F G"));
                        Timeline tl = new Timeline(kFrame);
                        tl.play();
                        break;
                }

            }
        });

        Button btn2 = new Button();
        btn2.setText("Set Label to \"1 2 3\"");
        btn2.setOnAction(new EventHandler<javafx.event.ActionEvent>() {

            @Override
            public void handle(javafx.event.ActionEvent event) {
                switch (execType) {
                    case DIRECT:
                        l1.setText("1 2 3");                     
                        break;
                    case RUN_LATER:
                        Platform.runLater(() -> l1.setText("1 2 3"));
                        break;
                    case ANIMATION:
                        KeyFrame kFrame = new KeyFrame(Duration.millis(1), e -> l1.setText("1 2 3"));
                        Timeline tl = new Timeline(kFrame);
                        tl.play();
                        break;
                }
            }
        });

        Button btn3 = new Button();
        btn3.setText("sp.requestLayout()");
        btn3.setOnAction(new EventHandler<javafx.event.ActionEvent>() {

            @Override
            public void handle(javafx.event.ActionEvent event) {
                sp.requestLayout();
            }
        });

        final ChoiceBox<ExecType> choiceBox = new ChoiceBox<ExecType>();
        choiceBox.getItems().addAll(ExecType.DIRECT, ExecType.RUN_LATER, ExecType.ANIMATION);
        choiceBox.getSelectionModel().select(0);
        choiceBox.selectionModelProperty().addListener((o, oldVal, newVal) -> execType = newVal.getSelectedItem());

        rootPane.getChildren().addAll(choiceBox, btn1, btn2, btn3, topPane);
        Scene scene = new Scene(rootPane, 600, 400);
        topPane.getChildren().add(sp);
        sp.relocate(200, 100); 

        sp.setStyle("-fx-border-color: RED;"); 

//        Platform.runLater(() -> {
////            e1.setRadiusX(l1.getWidth());
////            e1.setRadiusY(l1.getHeight());
//            
//            e1.setCenterX(0.01);
//        });

        Platform.runLater(() -> {
            System.err.println("e1's layoutBounds = " + e1.getLayoutBounds());
            System.err.println("l1's layoutBounds = " + l1.getLayoutBounds());
            System.err.println("sp's layoutBounds = " + sp.getLayoutBounds());
            // Workaround 1:
//            l1.setText("123"); 
//            l1.setText("1 2"); 
        }); 

        stage.setScene(scene); 
        stage.show();
        // Workaround 2:
//        sp.requestLayout();
    } 
} 