Class SwingTree
SwingTree
is a singleton that holds global configuration context for the SwingTree library.
This includes the EventProcessor
that is used to process events, as well as the
StyleSheet
that is used to style components.
You may access the singleton instance of the
SwingTree
class through the get()
method.-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a property change listener to the user scale factor, so when the user scale factor changes, the property "swingtree.uiScale" is fired.static void
clear()
Clears the singleton instance of theSwingTree
.static SwingTree
get()
Returns the singleton instance of theSwingTree
.long
Returns the default animation interval in milliseconds which is a property that determines the delay between two consecutive animation steps.TheEventProcessor
is a simple interface whose implementations delegate tasks to threads that are capable of processing GUI or application events.The icon cash is a hash map that uses anIconDeclaration
as a key and anImageIcon
as a value.TheStyleSheet
is an abstract class whose extensions are used to declare component styles through a CSS like DSL API.Exposes a set of system properties in the form of a nicely formatted string.double
Returns the system scale factor.double
Returns the system scale factor for the given graphics context.float
Returns the user scale factor is a scaling factor is used by SwingTree's style engine to scale the UI during painting.static void
initialiseUsing
(SwingTreeConfigurator configurator) A lazy initialization of the singleton instance of theSwingTree
class causing it to be recreated the next time it is requested throughget()
,
but with aSwingTreeConfigurator
that is used to configure theSwingTree
instance.
This is useful for testing purposes, but also in cases where the UI scale must be initialized or changed manually (through the reference font).
Also seeinitialize()
.static void
A lazy initialization of the singleton instance of theSwingTree
class causing it to be recreated the next time it is requested throughget()
.
This is useful for testing purposes, also in cases where the UI scale changes (through the reference font).
Also seeinitialiseUsing(SwingTreeConfigurator)
.boolean
Returns whether system scaling is enabled.void
Removes the provided property change listener from the user scale factor property with the name "swingtree.uiScale".void
setDefaultAnimationInterval
(long defaultAnimationInterval) Sets the default animation interval in milliseconds which is a property that determines the delay between two consecutive animation steps.void
setEventProcessor
(EventProcessor eventProcessor) Sets theEventProcessor
that is used to process GUI and application events.void
setStyleSheet
(StyleSheet styleSheet) Sets theStyleSheet
that is used to style components.void
setUiScaleFactor
(float scaleFactor) Sets the user scale factor is a scaling factor that is used by SwingTree's style engine to scale the UI during painting.
-
Method Details
-
get
Returns the singleton instance of theSwingTree
. Note that this method will create the singleton if it does not exist.- Returns:
- the singleton instance of the
SwingTree
.
-
clear
public static void clear()Clears the singleton instance of theSwingTree
. This is useful for testing purposes, or if you want to reconfigure your application with a differentSwingTreeInitConfig
. (seeinitialiseUsing(SwingTreeConfigurator)
). -
initialize
public static void initialize()A lazy initialization of the singleton instance of theSwingTree
class causing it to be recreated the next time it is requested throughget()
.
This is useful for testing purposes, also in cases where the UI scale changes (through the reference font).
Also seeinitialiseUsing(SwingTreeConfigurator)
. -
initialiseUsing
A lazy initialization of the singleton instance of theSwingTree
class causing it to be recreated the next time it is requested throughget()
,
but with aSwingTreeConfigurator
that is used to configure theSwingTree
instance.
This is useful for testing purposes, but also in cases where the UI scale must be initialized or changed manually (through the reference font).
Also seeinitialize()
.- Parameters:
configurator
- theSwingTreeConfigurator
that is used to configure theSwingTree
instance.
-
getIconCache
The icon cash is a hash map that uses anIconDeclaration
as a key and anImageIcon
as a value. This is used to cache icons that are loaded from the file system using convenience methods likeUIFactoryMethods.findIcon(String)
andUIFactoryMethods.findIcon(IconDeclaration)
orUIFactoryMethods.findSvgIcon(String)
,UIFactoryMethods.findSvgIcon(IconDeclaration)
.
Note that the map returned by this method is mutable and can be used to add or remove icons from the cache. You may also want to consider this as a possible source of memory leaks.- Returns:
- The icon cache of this context, which is used to cache icons that are loaded from the file system.
-
getUiScaleFactor
public float getUiScaleFactor()Returns the user scale factor is a scaling factor is used by SwingTree's style engine to scale the UI during painting. Note that this is different from the system/Graphics2D scale factor, which is the scale factor that the JRE uses to scale everything through theAffineTransform
of theGraphics2D
.Use this scaling factor for painting operations that are not performed by SwingTree's style engine, e.g. custom painting (see
ComponentStyleDelegate.painter(UI.Layer, Painter)
).You can configure this scaling factor through the library initialization method
initialiseUsing(SwingTreeConfigurator)
, or directly through the system property "swingtree.uiScale".- Returns:
- The user scale factor.
-
setUiScaleFactor
public void setUiScaleFactor(float scaleFactor) Sets the user scale factor is a scaling factor that is used by SwingTree's style engine to scale the UI during painting. Note that this is different from the system/Graphics2D scale factor, which is the scale factor that the JRE uses to scale everything through theAffineTransform
of theGraphics2D
.Use this scaling factor for painting operations that are not performed by SwingTree's style engine, e.g. custom painting (see
ComponentStyleDelegate.painter(UI.Layer, Painter)
).You can configure this scaling factor through the library initialization method
initialiseUsing(SwingTreeConfigurator)
, or directly through the system property "swingtree.uiScale".- Parameters:
scaleFactor
- The user scale factor.
-
addUiScaleChangeListener
Adds a property change listener to the user scale factor, so when the user scale factor changes, the property "swingtree.uiScale" is fired. The user scale factor is a scaling factor that is used by SwingTree's style engine to scale the UI during painting. Note that this is different from the system/Graphics2D scale factor, which is the scale factor that the JRE uses to scale everything through theAffineTransform
of theGraphics2D
.Use this scaling factor for painting operations that are not performed by SwingTree's style engine, e.g. custom painting (see
ComponentStyleDelegate.painter(UI.Layer, Painter)
).You can configure this scaling factor through the library initialization method
initialiseUsing(SwingTreeConfigurator)
, or directly through the system property "swingtree.uiScale".- Parameters:
listener
- The property change listener to add.
-
removeUiScaleChangeListener
Removes the provided property change listener from the user scale factor property with the name "swingtree.uiScale". The user scale factor is a scaling factor that is used by SwingTree's style engine to scale the UI during painting. Note that this is different from the system/Graphics2D scale factor, which is the scale factor that the JRE uses to scale everything through theAffineTransform
of theGraphics2D
.Use this scaling factor for painting operations that are not performed by SwingTree's style engine, e.g. custom painting (see
ComponentStyleDelegate.painter(UI.Layer, Painter)
).You can configure this scaling factor through the library initialization method
initialiseUsing(SwingTreeConfigurator)
, or directly through the system property "swingtree.uiScale".- Parameters:
listener
- The property change listener to remove.
-
isSystemScalingEnabled
public boolean isSystemScalingEnabled()Returns whether system scaling is enabled. System scaling means that the JRE scales everything through theAffineTransform
of theGraphics2D
. If this is the case, then we do not have to do scaled painting and can use the original size of icons, gaps, etc.- Returns:
- true if system scaling is enabled.
-
getSystemScaleFactorOf
Returns the system scale factor for the given graphics context. The system scale factor is the scale factor that the JRE uses to scale everything (text, icons, gaps, etc).- Parameters:
g
- The graphics context to get the system scale factor for.- Returns:
- The system scale factor for the given graphics context.
-
getSystemScaleFactor
public double getSystemScaleFactor()Returns the system scale factor. The system scale factor is the scale factor that the JRE uses to scale everything (text, icons, gaps, etc) irrespective of the current look and feel, as this is the scale factor that is used by theAffineTransform
of theGraphics2D
.- Returns:
- The system scale factor.
-
getEventProcessor
TheEventProcessor
is a simple interface whose implementations delegate tasks to threads that are capable of processing GUI or application events. As part of this singleton, the SwingTree library maintains a globalEventProcessor
that is used consistently by all declarative builders.- Returns:
- The currently configured
EventProcessor
that is used to process GUI and application events.
-
setEventProcessor
Sets theEventProcessor
that is used to process GUI and application events. You may not pass null as an argument, because SwingTree requires an event processor to function.- Parameters:
eventProcessor
- theEventProcessor
that is used to process GUI and application events.- Throws:
NullPointerException
- if eventProcessor is null!
-
getStyleSheet
TheStyleSheet
is an abstract class whose extensions are used to declare component styles through a CSS like DSL API. As part of this singleton, the SwingTree library maintains a globalStyleSheet
that is used consistently by all declarative builders. Use this method to access this global style sheet.- Returns:
- The currently configured
StyleSheet
that is used to style components.
-
setStyleSheet
Sets theStyleSheet
that is used to style components. UseStyleSheet.none()
instead of null to switch off global styling.- Parameters:
styleSheet
- TheStyleSheet
that is used to style components.- Throws:
NullPointerException
- if styleSheet is null!
-
getDefaultAnimationInterval
public long getDefaultAnimationInterval()Returns the default animation interval in milliseconds which is a property that determines the delay between two consecutive animation steps. You can think of it as the time between the heartbeats of the animation. The smaller the interval, the higher the refresh rate and the smoother the animation will look. However, the smaller the interval, the more CPU time will be used. The default interval is 16 ms which corresponds to almost 60 fps.
This property is used as default value by theLifeTime
object which is used to define the duration of anAnimation
. The value returned by this is used by animations if no other interval is specified throughLifeTime.withInterval(long, TimeUnit)
.- Returns:
- The default animation interval in milliseconds.
-
setDefaultAnimationInterval
public void setDefaultAnimationInterval(long defaultAnimationInterval) Sets the default animation interval in milliseconds which is a property that determines the delay between two consecutive animation steps. You can think of it as the time between the heartbeats of the animation. The smaller the interval, the higher the refresh rate and the smoother the animation will look. However, the smaller the interval, the more CPU time will be used. The default interval is 16 ms which corresponds to almost 60 fps.
This property is used as default value by theLifeTime
object which is used to define the duration of anAnimation
. The value returned by this is used by animations if no other interval is specified throughLifeTime.withInterval(long, TimeUnit)
.- Parameters:
defaultAnimationInterval
- The default animation interval in milliseconds.
-
getSystemInfo
Exposes a set of system properties in the form of a nicely formatted string. These are used by the SwingTree library to determine the system configuration and to adjust the UI accordingly.- Returns:
- A string containing system information.
-