From e57b6029daf1fe34351ffd307751a0c162a43dc9 Mon Sep 17 00:00:00 2001 From: ArthusWQZ Date: Mon, 16 Oct 2023 19:08:45 -0400 Subject: [PATCH] [#508] Added notification spawning and rearranging methods --- src/org/jetuml/gui/NotificationHandler.java | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/org/jetuml/gui/NotificationHandler.java b/src/org/jetuml/gui/NotificationHandler.java index a608f8e0b..697d9240d 100644 --- a/src/org/jetuml/gui/NotificationHandler.java +++ b/src/org/jetuml/gui/NotificationHandler.java @@ -10,6 +10,10 @@ public final class NotificationHandler { + private static final int NOTIFICATION_STACK_SPACING = 8; + private static final int NOTIFICATION_STACK_X_MARGIN = 18; + private static final int NOTIFICATION_STACK_Y_MARGIN = 18; + /** * A callback for Notification objects called when they reached the end of their lifetime. * It removes them from the handler notification list. @@ -22,6 +26,7 @@ private CleanUpCallback() /** * Removes the specified notification object from the handler notification list. + * * @param pNotification The notification object that calls this function */ public void execute(Notification pNotification) @@ -50,6 +55,7 @@ public static NotificationHandler instance() /** * Sets the parent stage of all the notification stages. + * * @param pStage The target parent stage of the notification objects */ public void setMainStage(Stage pStage) @@ -57,4 +63,47 @@ public void setMainStage(Stage pStage) this.aMainStage = pStage; } + /** + * Rearranges the notifications so that they are stacked properly and do not overlap. + */ + public void updatePosition() + { + + double yBuf = this.aMainStage.getY() + this.aMainStage.getHeight() - NOTIFICATION_STACK_Y_MARGIN; + double x = this.aMainStage.getX() + NOTIFICATION_STACK_X_MARGIN; + + for (int i = aNotificationList.size() - 1; i >= 0; i--) + { + Notification notification = aNotificationList.get(i); + notification.setX(x); + notification.setY(yBuf); + yBuf = yBuf - notification.getHeight() - NOTIFICATION_STACK_SPACING; + } + } + + /** + * Spawns a new toast notification. + * + * @param pText The text to show on the toast + */ + public void spawn(String pText) + { + + if (this.aMainStage == null) + { + return; + } + + double x = this.aMainStage.getX() + NOTIFICATION_STACK_X_MARGIN; + double y = this.aMainStage.getY() + this.aMainStage.getHeight() - NOTIFICATION_STACK_Y_MARGIN; + + ToastNotification toast = new ToastNotification(pText, this.aMainStage); + + aNotificationList.add(toast); + + toast.show(x, y, new CleanUpCallback()); + this.updatePosition(); + + } + }