1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| import javafx.animation.ParallelTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.TranslateTransition;
import javafx.event.Event;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.util.Duration;
/**
* CustomContextMenu<br>
* <br>
* Etends ContextMenu afin d'implémenter les animations souhaitées pour l'application
*
* @author Valentin
*
*/
public class CustomContextMenu extends ContextMenu {
private static final int DURATION = 200;
private ParallelTransition openingParallel;
private ParallelTransition closingParallel;
/**
* Constructor
*/
public CustomContextMenu(){
super();
}
/**
* A appeler après avoir mis à jour la liste des items
*/
void initTransition(){
//On crée les animations d'apparition/disparition
TranslateTransition translation = new TranslateTransition(Duration.millis(DURATION));
translation.setFromY(250);
translation.setToY(0);
ScaleTransition scale = new ScaleTransition(Duration.millis(DURATION));
scale.setFromX(0);
scale.setFromY(0);
scale.setToX(1);
scale.setToY(1);
openingParallel = new ParallelTransition();
openingParallel.getChildren().addAll(scale, translation);
openingParallel.setAutoReverse(true);
//TODO : Trouver un moyen plus propre de récupérer notre node
openingParallel.setNode(this.getItems().get(0).getGraphic());
translation = new TranslateTransition(Duration.millis(DURATION));
translation.setFromY(0);
translation.setToY(250);
scale = new ScaleTransition(Duration.millis(DURATION));
scale.setFromX(1);
scale.setFromY(1);
scale.setToX(0);
scale.setToY(0);
closingParallel = new ParallelTransition();
closingParallel.getChildren().addAll(scale, translation);
closingParallel.setAutoReverse(true);
closingParallel.setCycleCount(1);
//TODO : Trouver un moyen plus propre de récupérer notre node
closingParallel.setNode(this.getItems().get(0).getGraphic());
closingParallel.setOnFinished((event)->{
super.hide();
});
this.setOnShown((event)->openingParallel.play());
}
@Override
public void hide(){
if (!isShowing())
return;
Event.fireEvent(this, new Event(Menu.ON_HIDING));
closingParallel.play();
Event.fireEvent(this, new Event(Menu.ON_HIDDEN));
}
} |
Partager