Class ThemeUtilities

java.lang.Object
org.tentackle.fx.ThemeUtilities
Direct Known Subclasses:
RdcThemeUtilities

@Service(ThemeUtilities.class) public class ThemeUtilities extends Object
Utility methods for theme management.
Maintains the dark mode and the theme name, applies the stylesheets to the scenes, and saves/loads the user's theme preferences. The default implementation supports the JavaFX default theme Modena only. Theme modules such as tentackle-fx-atlanta replace the singleton to provide more themes.
Author:
harald
  • Property Details

  • Field Details

  • Constructor Details

    • ThemeUtilities

      public ThemeUtilities(String defaultThemeName, int fontSize)
      Creates the theme utilities.
      Parameters:
      defaultThemeName - the default theme name
      fontSize - the default font size
    • ThemeUtilities

      public ThemeUtilities()
      Creates the theme utilities with the default theme name.
  • Method Details

    • getInstance

      public static ThemeUtilities getInstance()
      The singleton.
      Returns:
      the singleton
    • isDarkMode

      public boolean isDarkMode()
      Returns whether dark mode is enabled.
      Returns:
      true if dark mode, false if light mode (default)
    • setDarkMode

      public void setDarkMode(boolean darkMode)
      Enables dark mode.
      Parameters:
      darkMode - true to enable dark theme, false for light theme (default)
    • darkModeProperty

      public javafx.beans.property.BooleanProperty darkModeProperty()
      Gets the dark mode property.
      Changing the property updates the stylesheets of all scenes.
      Returns:
      the property
      See Also:
    • getSupportedThemes

      public List<String> getSupportedThemes()
      Gets the list of supported themes.
      Returns:
      the names of the supported themes, never null
    • getThemeName

      public String getThemeName()
      Gets the current theme name.
      Returns:
      the theme name, never null
    • setThemeName

      public void setThemeName(String themeName)
      Sets the theme name.

      Throws FxRuntimeException if the theme is not supported.

      Parameters:
      themeName - the theme name
    • themeNameProperty

      public javafx.beans.property.StringProperty themeNameProperty()
      Gets the theme name property.
      Changing the property updates the stylesheets of all scenes.
      Returns:
      the property
      See Also:
    • setTheme

      public void setTheme(boolean darkMode, String themeName)
      Sets the theme name and dark mode simultaneously.
      The stylesheets are updated only once.
      Parameters:
      darkMode - the dark mode flag
      themeName - the theme name
    • savePreferences

      public void savePreferences(Class<?> clazz, ThemeUtilities.ThemePreferences preferences)
      Saves the theme name and dark mode to the user preferences on the local machine.
      Parameters:
      clazz - the application class
      preferences - the preferences to save
    • loadPreferences

      public ThemeUtilities.ThemePreferences loadPreferences(Class<?> clazz, Boolean defaultDarkMode, String defaultThemeName)
      Loads the theme name and dark mode from the user preferences on the local machine.
      Parameters:
      clazz - the application class
      defaultDarkMode - the default dark mode value, null for none
      defaultThemeName - the default theme name, null for none
      Returns:
      the preferences, never null
    • loadPreferences

      public ThemeUtilities.ThemePreferences loadPreferences(Class<?> clazz)
      Loads the theme name and dark mode from the user preferences on the local machine.
      Parameters:
      clazz - the application class
      Returns:
      the preferences, never null
    • applyThemePreferences

      public void applyThemePreferences(ThemeUtilities.ThemePreferences preferences)
      Applies the theme preferences.
      Preferences that are null leave the current setting unchanged.
      Parameters:
      preferences - the preferences
    • applyUserAgentStylesheet

      public void applyUserAgentStylesheet()
      Applies the user agent stylesheet.
      The default implementation does nothing, i.e. keeps the JavaFX default Modena. Overridden by theme implementations.
    • applyStylesheets

      public void applyStylesheets(javafx.scene.Scene scene)
      Applies the default stylesheets to a scene.
      Invoked for all newly created scenes or via reapplyStyleSheets().
      Parameters:
      scene - the scene
    • reapplyStyleSheets

      public void reapplyStyleSheets()
      Resets and applies the stylesheets to all scenes.
      Useful when the dark mode configuration changed, for example.
    • getResourceURL

      public URL getResourceURL(Class<?> clazz, String name)
      Gets the resource URL.
      The method is provided to allow redirects to other resources, usually CSS files.
      Parameters:
      clazz - the reference class
      name - the resource name
      Returns:
      the resource URL
    • getDefaultFont

      public javafx.scene.text.Font getDefaultFont()
      Gets the default font.
      Returns:
      the default font
    • getAutoAdjustPadding

      public double getAutoAdjustPadding()
      Gets the padding for auto-adjusting the width of a table column or tree table column.
      Returns:
      the padding in pixels
    • updateStyles

      protected void updateStyles()
      Updates the stylesheets according to the dark mode and theme setting.
      Does nothing if not invoked from the FX application thread or while setTheme(boolean, String) is in progress.