package mytime.gui.controller;
import javafx.animation.Animation;
import javafx.animation.Transition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
/**
* Animates a node on and off screen to the top, right, bottom or left side.
*/
public class SlidePanelAnimation extends VBox
{
// private final String CSS = "/" + this.getClass().getSimpleName() + ".css";
private double expandedSize;
// private Pos flapbarLocation;
/**
* Creates a sidebar panel in a BorderPane, containing an horizontal
* alignment of the given nodes.
*
* <pre>
* <code>
* Example:
*
* BorderSlideBar topFlapBar = new BorderSlideBar(
* 100, button, Pos.TOP_LEFT, new contentController());
* mainBorderPane.setTop(topFlapBar);
* </code>
* </pre>
*
* @param expandedSize The size of the panel.
* @param nodes Nodes inside the panel.
*/
public SlidePanelAnimation(double expandedSize,
Node... nodes)
{
getStyleClass().add("sidebar");
// getStylesheets().add(CSS);
setExpandedSize(expandedSize);
setVisible(false);
// Set location
// Add nodes in the vbox
getChildren().addAll(nodes);
// Create an animation to hide the panel.
// Create an animation to hide the panel.
final Animation hidePanel = new Transition()
{
{
setCycleDuration(Duration.millis(250));
}
@Override
protected void interpolate(double frac)
{
final double size = getExpandedSize() * (1.0 - frac);
translateByPos(size);
}
};
hidePanel.onFinishedProperty().set(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent actionEvent)
{
setVisible(false);
}
});
// Create an animation to show the panel.
final Animation showPanel = new Transition()
{
{
setCycleDuration(Duration.millis(250));
}
@Override
protected void interpolate(double frac)
{
final double size = getExpandedSize() * frac;
translateByPos(size);
}
};
showPanel.onFinishedProperty().set(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent actionEvent)
{
}
});
if (showPanel.statusProperty().get() == Animation.Status.STOPPED
&& hidePanel.statusProperty().get() == Animation.Status.STOPPED)
{
if (isVisible())
{
hidePanel.play();
} else
{
setVisible(true);
showPanel.play();
}
}
}
/**
* Initialize position orientation.
*/
/**
* Translate the VBox according to location Pos.
*
* @param size
*/
private void translateByPos(double size)
{
setPrefWidth(size);
}
/**
* @return the expandedSize
*/
public double getExpandedSize()
{
return expandedSize;
}
/**
* @param expandedSize the expandedSize to set
*/
public void setExpandedSize(double expandedSize)
{
this.expandedSize = expandedSize;
}
}