Class AppManager


  • public final class AppManager
    extends Object
    AppManager manages the Application flow. The following is a sample skeleton of how it can be added to an Application class:
     
     public class MyApplication extends Application {
    
         private final AppManager appManager = AppManager.initialize(this::postInit);
    
         public void init() throws Exception {
             appManager.addViewFactory(HOME_VIEW, () -> new View() {...});
         }
    
         public void start(Stage primaryStage) throws Exception {
             appManager.start(primaryStage);
         }
    
         private void postInit(Scene scene) {
             Swatch.BLUE.assignTo(scene);
         }
    
     }
     

    Unlike Application, AppManager does not require any specific methods to be implemented. There are requirements on the developer, however. In particular, the developer of a Glisten-based application has to specify the views and layers in their application, and provide these to the AppManager instance as factories that can be called on-demand.

    The most important view factory is the one that specifies the 'home view', which is shown when the application first starts. For example, here is the code to create a view factory for the home view:

     
     addViewFactory(AppManager.HOME_VIEW, () -> {
         // normal JavaFX code to create a scenegraph of nodes
         Node homeViewContent = ...;
       
         return new View(homeViewContent);
     });

    Note in the code above that we call the addViewFactory(String, Supplier) method of the AppManager class, and we provide two arguments:

    1. The name of the view. In this case, we are using the HOME_VIEW public static field, but this can be any String, as long as it is unique to your application (i.e. don't add multiple view factories with the same name!).
    2. We provide a Supplier implementation that, when called by Glisten, should return a new instance of this particular View. Note that the returned object is a View instance.

    With this view defined (along with other views relevant to your application), switching between views is simply a matter of calling AppManager.getInstance().switchView("view-name"), which "view-name" is replaced by the name of the view you wish to go to (or, if you wish to return home, simply HOME_VIEW).

    Similar functionality exists for layers, which make use of the GlassPane to appear above the rest of your application user interface.

    Once the Application.start(javafx.stage.Stage) method is called, it is important that the the start(javafx.stage.Stage) is called to initialize the scene and the GlassPane.

    The developer can use initialize(Consumer) } to define additional initialization code that will be invoked after the scene has been initialized and right before the stage is shown, like selecting a Swatch, adding an stylesheet or switching to a view, among others.

    AppManager also installs application wide AppBar and NavigationDrawer. A developer can simply call getAppBar() or getDrawer() to access these controls.

    Since:
    6.1.0
    See Also:
    GlassPane, View, Layer
    • Field Detail

      • HOME_VIEW

        public static final String HOME_VIEW
        All Glisten-based applications should define one View with its name specified as HOME_VIEW. This View will then become the default view of the user interface.
        See Also:
        Constant Field Values
      • SPLASH_VIEW

        public static final String SPLASH_VIEW
        A Glisten-based application defines a View with its name specified as SPLASH_VIEW intended to be a one-time splash view. After a given amount of time or certain event, this view will be dismissed giving access to the HOME_VIEW or any other view.
        See Also:
        Constant Field Values
    • Method Detail

      • getInstance

        public static AppManager getInstance()
        Returns the instance of the AppManager, which might be null if initialize() has not been called yet.
        Returns:
        the AppManager single instance
      • initialize

        public static AppManager initialize​(Consumer<Scene> postInit)
        Creates an instance of the AppManager, providing a consumer that will be called for additional initialization code. It should be called only once.
        Parameters:
        postInit - a consumer with additional initialization code
        Returns:
        an instance of AppManager
      • initialize

        public static AppManager initialize()
        Creates an instance of the AppManager. It should be called only once.
        Returns:
        an instance of AppManager
      • start

        public final void start​(Stage primaryStage)
        Once the Application.start(javafx.stage.Stage) method is called, this method should be called to initialize the scene and the GlassPane
        Parameters:
        primaryStage - the primary stage for this application, onto which the application scene will be set.
      • titleProperty

        public final StringProperty titleProperty()
        The title of the application. If this software is run on a desktop machine, this title will appear in the standard title area for applications. On other platforms, it is likely that this title will not be visible.
        See Also:
        getTitle(), setTitle(String)
      • setTitle

        public final void setTitle​(String title)
        Sets the value of the property title.
        Property description:
        The title of the application. If this software is run on a desktop machine, this title will appear in the standard title area for applications. On other platforms, it is likely that this title will not be visible.
      • getTitle

        public final String getTitle()
        Gets the value of the property title.
        Property description:
        The title of the application. If this software is run on a desktop machine, this title will appear in the standard title area for applications. On other platforms, it is likely that this title will not be visible.
      • getView

        public final View getView()
        Gets the value of the property view.
        Property description:
        The currently showing View within this application.
      • getSwatch

        public final Swatch getSwatch()
        Gets the value of the property swatch.
        Property description:
        Specifies the Swatch that should be used for the whole scene. If this property has a null value the default swatch will be used.
      • setSwatch

        public final void setSwatch​(Swatch swatch)
        Sets the value of the property swatch.
        Property description:
        Specifies the Swatch that should be used for the whole scene. If this property has a null value the default swatch will be used.
      • getGlassPane

        public final GlassPane getGlassPane()
        Returns the GlassPane instance that is part of this application.
        Returns:
        the GlassPane instance of this application.
      • getAppBar

        public final AppBar getAppBar()
        Returns the AppBar instance that is a part of this application.
        Returns:
        the AppBar instance of the current application.
      • getStatusBar

        public final StatusBar getStatusBar()
        Returns the StatusBar instance that is a part of this application. Note that not all platforms have a status bar.
        Returns:
        The StatusBar instance of the current application.
        Since:
        4.0.0
      • switchView

        public final <T extends ViewOptional<T> switchView​(String viewName)
        Attempts to switch the view from the current view to a view represented by the given view name. If the view name does not exist, then the current view remains visible to users. If the view name does exist, then the view will be switched to the new view represented by this name. Previous view will be pushed on top of the view stack.
        Type Parameters:
        T - the view type
        Parameters:
        viewName - name of the view to switch to.
        Returns:
        optional view for which switch was requested
      • switchView

        public final <T extends ViewOptional<T> switchView​(String viewName,
                                                             ViewStackPolicy viewStackPolicy)
        Attempts to switch the view from the current view to a view represented by the given view name. If the view name does not exist, then the current view remains visible to users. If the view name does exist, then the view will be switched to the new view represented by this name. The viewStackPolicy that is passed in will dictate what happens to the view stack.
        Type Parameters:
        T - the view type
        Parameters:
        viewName - name of the view to switch to.
        viewStackPolicy - ViewStackPolicy.
        Returns:
        optional view for which switch was requested
      • switchToPreviousView

        public final <T extends ViewOptional<T> switchToPreviousView()
        Attempts to switch to a previously used view if one exists.
        Type Parameters:
        T - the view type
        Returns:
        optional previous view
      • goHome

        public final <T extends ViewOptional<T> goHome()
        Returns to the home view and clears view stack.
        Type Parameters:
        T - the view type
        Returns:
        optional view for which switch was requested
      • retrieveView

        public final Optional<View> retrieveView​(String viewName)
        Attempts to retrieve the view represented by the given view name. The result is wrapped in an Optional, to prevent null pointer exceptions.
        Parameters:
        viewName - The name of the View - this will be used at other points in the application lifecycle to reference this view, and to switch to it.
        Returns:
        an Optional with the View, if not empty
      • addViewFactory

        public final void addViewFactory​(String viewName,
                                         Supplier<View> supplier)
        Adds a new view factory to this application, if the given view name is unique (otherwise an IllegalArgumentException is thrown).
        Parameters:
        viewName - The name of the view - this will be used at other points in the application lifecycle to reference this view, and to switch to it.
        supplier - A Supplier instance that, when called, should return the view. This view is cached inside the mobile application, and as such is reused throughout the life of the application instance.
      • removeViewFactory

        public final boolean removeViewFactory​(String viewName)
        Removes a view factory with the given view name from this application, if it is present (optional operation).
        Parameters:
        viewName - The name of the view which is mapped to the view factory.
        Returns:
        true if the view name is present and is successfully removed from the application. false if the view name contained no mapping for a view factory.
      • isViewPresent

        public final boolean isViewPresent​(String viewName)
        Returns true if a view factory with the view name exists in the application.
        Parameters:
        viewName - The name of the view which is mapped to the view factory.
        Returns:
        true if a view factory with the view name exists in the application.
      • addLayerFactory

        public final void addLayerFactory​(String layerName,
                                          Supplier<Layer> supplier)
        Adds a new layer factory to this application, if the given layer name is unique (otherwise an IllegalArgumentException is thrown).
        Parameters:
        layerName - The name of the layer - this will be used at other points in the application lifecycle to reference this layer, and to show it.
        supplier - A Supplier instance that, when called, should return the Layer. This layer is cached inside the mobile application, and as such is reused throughout the life of the application instance.
      • removeLayerFactory

        public final boolean removeLayerFactory​(String layerName)
        Removes a layer factory with the given layer name from this application, if it is present (optional operation).
        Parameters:
        layerName - The name of the layer which is mapped to the layer factory.
        Returns:
        true if the layer name is present and is successfully removed from the application. false if the layer name contained no mapping for a layer factory.
      • isLayerPresent

        public final boolean isLayerPresent​(String layerName)
        Returns true if a layer factory with the layer name exists in the application.
        Parameters:
        layerName - The name of the layer which is mapped to the layer factory.
        Returns:
        true if a layer factory with the layer name exists in the application.
      • showLayer

        public final void showLayer​(String layerName)
        Attempts to show a layer with the given name. Is equivalent to calling Layer.show().
        Parameters:
        layerName - The name of the layer which is mapped to the layer factory.
      • hideLayer

        public final void hideLayer​(String layerName)
        Attempts to hide a layer with the given name. Is equivalent to calling Layer.hide().
        Parameters:
        layerName - The name of the layer which is mapped to the layer factory.
      • hideAllLayers

        public final void hideAllLayers​(boolean hideAllLayers)
        If set to true, all visible layers, starting from top-most layer, are hidden when user interacts with the opaque region. By default, the value is set to false and only the top most layer is hidden on user interaction.
        Default value:
        false
        Parameters:
        hideAllLayers - boolean argument to hide all layers when user interacts with opaque layer
        Since:
        6.0.7
      • getScreenHeight

        public final double getScreenHeight()
        Returns the height of the screen that this application is running on.
        Returns:
        a double value with the height of the screen
      • getScreenWidth

        public final double getScreenWidth()
        Returns the width of the screen that this application is running on.
        Returns:
        a double value with the width of the screen
      • showMessage

        public void showMessage​(String message)
        Shows a Snackbar with the supplied message.
        Parameters:
        message - A String message
      • showMessage

        public void showMessage​(String message,
                                String buttonText,
                                EventHandler<ActionEvent> evtHandler)
        Shows a Snackbar with the supplied message and an action text.
        Parameters:
        message - A String message.
        buttonText - A String text to set action text caption.
        evtHandler - An EventHandler with the action to be invoked when the user clicks the action text.
      • createUserURLStreamHandlerFactory

        protected URLStreamHandlerFactory createUserURLStreamHandlerFactory()
        URL handling is under control of mobile application. If custom URL handling is required this method should return a URL handler factory.
        Returns:
        the URLStreamHandlerFactory