Module MaterialFX

Class AbstractMFXNotificationSystem

java.lang.Object
io.github.palexdev.materialfx.notifications.base.AbstractMFXNotificationSystem
All Implemented Interfaces:
INotificationSystem
Direct Known Subclasses:
MFXNotificationCenterSystem, MFXNotificationSystem

public abstract class AbstractMFXNotificationSystem extends Object implements INotificationSystem
Base class to define a notification system.

A notification system has the following info and features:

- The Screen on which to show the notification

- The Window on which the notification system's stage depends (more on that later)

- A MFXPopup which contains the notification

- The NotificationPos to compute the shown notifications' position

- Allows to specify some extra spacing between the screen's borders and the notification

- By default is animated but it can also be disabled

- By default notifications will close automatically after 3 seconds (by default)

To fix issue #80 and similar, the notification system do not uses the focused window anymore but it has its own "dummy" stage. It is a UTILITY stage, so that no icon appear in the taskbar, and its opacity is 0. While this is a great improvement over the old design there's also a downside. When the JavaFX's primary stage is closed the app won't close because the "dummy" stage is still open, to fix this a notification system must be initialized with an owner window, so that when owner is closed the "dummy" stage is closed too.

Any notification system ideally must implement the following behaviors:

- publish: the method that accepts and manages new notifications

- show: ideally called by the publish method, serves to specify how to show the new notification

- close: serves to specify how a notification should be closed

- scheduleReopen: this method specifies how the notification system behaves when a new notification is published but it already is in a showing/closing state. Ideally this method should instruct the notification system to ignore the new notification and recall publish as soon as the current notification is being closed.

- computePosition: computes the position of the notification as a TransitionPositionBean for use with animations too. (for non animated systems just subtract the deltas from their respective coordinates)

Side note on the close method: it's highly recommended to also close the popup since it is not transparent and mouse won't work through. (may block OS windows)

Side notes on the scheduleReopen mechanism: this base class offers two ResettableBooleanProperty to specify that the notification is showing/closing a notification. Show and hide methods should set these properties to true as the first operation, and then reset them once the notification is closed (see implementations source code for examples). The scheduleReopen should instruct the system to call publish as soon as the closing property becomes false.

Last note: notification systems ideally should be singletons.
  • Field Details

  • Constructor Details

    • AbstractMFXNotificationSystem

      protected AbstractMFXNotificationSystem()
  • Method Details

    • show

      protected abstract void show()
      Shows a notification by manipulating the popup's coordinates and content.
    • close

      protected abstract void close()
      Closes a notification by manipulating the popup's coordinates and content.

      The popup should be closed as well!

    • scheduleReopen

      protected abstract void scheduleReopen(INotification notification)
      Instructs the notification system to shown the specified notification when possible.
    • computePosition

      protected abstract TransitionPositionBean computePosition()
      Responsible for computing the popup's coordinates.
    • init

      protected void init()
      Default implementation is empty.
    • getScreen

      public Screen getScreen()
      Returns:
      the screen on which to show the notifications
    • setScreen

      public AbstractMFXNotificationSystem setScreen(Screen screen)
      Sets the screen on which to show the notifications.
    • getPosition

      public NotificationPos getPosition()
      Returns:
      the position at which notifications will be shown
    • setPosition

      public AbstractMFXNotificationSystem setPosition(NotificationPos position)
      Sets the position at which notifications will be shown.
    • delaySetPosition

      public AbstractMFXNotificationSystem delaySetPosition(NotificationPos position)
      Safer version of setPosition(NotificationPos). If the notification system is currently showing it's not a good idea to change the position as the close method could then misbehave, this method changes the position as soon as the notification system has been closed. If it's already closed then the position is set immediately.
    • getSpacing

      public Insets getSpacing()
      Returns:
      the Insets object that specifies the spacing between notifications and the screen borders
    • setSpacing

      public AbstractMFXNotificationSystem setSpacing(Insets spacing)
      Sets the Insets object that specifies the spacing between notifications and the screen borders.
    • isAnimated

      public boolean isAnimated()
      Returns:
      whether the notification system is animated
    • setAnimated

      public AbstractMFXNotificationSystem setAnimated(boolean animated)
      Enables/Disables animations.
    • isCloseAutomatically

      public boolean isCloseAutomatically()
      Returns:
      whether notifications should close automatically
    • setCloseAutomatically

      public AbstractMFXNotificationSystem setCloseAutomatically(boolean closeAutomatically)
      Enables/Disables notifications automatic close.
    • getCloseAfter

      public Duration getCloseAfter()
      Returns:
      the duration of time after which the notifications are automatically closed if isCloseAutomatically() is true
    • setCloseAfter

      public AbstractMFXNotificationSystem setCloseAfter(Duration closeAfter)
      Sets the duration of time after which the notifications are automatically closed if isCloseAutomatically() is true
    • isShowing

      public boolean isShowing()
      Returns:
      whether the notification system is showing a notification
    • isClosing

      public boolean isClosing()
      Returns:
      whether the notification system is closing