public class FadeFlowContainer implements FlowContainer<StackPane>{
private Timeline timeline;
private final StackPane root;
private final Duration fadeTime;
public FadeFlowContainer(StackPane root, Duration dur) {
this.root = root;
this.fadeTime = dur;
}
@Override
public <U> void setViewContext(ViewContext<U> context) {
if(timeline != null){
timeline.stop();
}
timeline = new Timeline();
timeline.getKeyFrames().addAll(
//start fade out
new KeyFrame(Duration.ZERO, new KeyValue(root.opacityProperty(), 1.0, Interpolator.EASE_BOTH)),
new KeyFrame(fadeTime.divide(2), e ->{
root.getChildren().clear();
root.getChildren().add(context.getRootNode());
}, new KeyValue(root.opacityProperty(), 0.0, Interpolator.EASE_BOTH)),
//start fade in
new KeyFrame(fadeTime, new KeyValue(root.opacityProperty(), 0.0, Interpolator.EASE_BOTH)),
new KeyFrame(fadeTime.multiply(2), new KeyValue(root.opacityProperty(), 1.0, Interpolator.EASE_BOTH))
);
timeline.play();
}
@Override
public StackPane getView() {
return root;