Class Gui
The basic idea is that all the colors, fonts, and icons used in an application should be
accessed indirectly via an "id" string. Then the actual color, font, or icon can be changed
without changing the source code. The default mapping of the id strings to a value is defined
in {name}.theme.properties files which are dynamically discovered by searching the module's
data directory. Also, these files can optionally define a dark default value for an id which
would replace the standard default value in the event that the current theme specifies that it
is a dark theme. Themes are used to specify the application's LookAndFeel
, whether or
not it is dark, and any customized values for colors, fonts, or icons. There are several
"built-in" themes, one for each supported LookAndFeel
, but additional themes can
be defined and stored in the users application home directory as a {name}.theme file.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addThemeListener
(ThemeListener listener) Adds aThemeListener
to be notified of theme changes.static Color
Returns a brighter version of the given color or darker if the current theme is dark.static Color
Returns a darker version of the given color or brighter if the current theme is dark.static Color
Returns theColor
registered for the given id.static Font
Returns the currentFont
associated with the given id.static Icon
Returns the Icon registered for the given id.static boolean
Returns true if an color for the given Id has been definedstatic boolean
Returns true if an font for the given Id has been definedstatic boolean
Returns true if an icon for the given Id has been definedstatic boolean
Returns true if the application should allow blinking cursors, false otherwise.static boolean
Returns true if the active theme is using dark defaultsstatic boolean
isSystemId
(String id) Returns true if the given id is a system-defined id, such as those starting withlaf.color
orsystem.color
.static boolean
Returns true if the theme system is in the process of updatingstatic void
registerFont
(Component component, String fontId) Binds the component to the font identified by the given font id.static void
registerFont
(JComponent component, int fontStyle) Registers the given component with the given font style.static void
removeThemeListener
(ThemeListener listener) Removes the givenThemeListener
from the list of listeners to be notified of theme changes.static void
setBlinkingCursors
(boolean b) Sets application's blinking cursor state.static void
unRegisterFont
(JComponent component, String fontId) Removes the component and font id binding made in a previous call toregisterFont(Component, String)
.
-
Method Details
-
getFont
Returns the currentFont
associated with the given id. A default font will be returned if the font can't be resolved and an error message will be printed to the console.- Parameters:
id
- the id for the desired font- Returns:
- the current
Font
associated with the given id.
-
getColor
Returns theColor
registered for the given id. Will output an error message if the id can't be resolved.- Parameters:
id
- the id to get the direct color for- Returns:
- the
Color
registered for the given id.
-
addThemeListener
Adds aThemeListener
to be notified of theme changes.- Parameters:
listener
- the listener to be notified
-
removeThemeListener
Removes the givenThemeListener
from the list of listeners to be notified of theme changes.- Parameters:
listener
- the listener to be removed
-
getIcon
Returns the Icon registered for the given id. If no icon is registered for the id, the default icon will be returned and an error message will be dumped to the console- Parameters:
id
- the id to get the registered icon for- Returns:
- the actual icon registered for the given id
-
hasColor
Returns true if an color for the given Id has been defined- Parameters:
id
- the id to check for an existing color.- Returns:
- true if an color for the given Id has been defined
-
hasFont
Returns true if an font for the given Id has been defined- Parameters:
id
- the id to check for an existing font.- Returns:
- true if an font for the given Id has been defined
-
hasIcon
Returns true if an icon for the given Id has been defined- Parameters:
id
- the id to check for an existing icon.- Returns:
- true if an icon for the given Id has been defined
-
darker
Returns a darker version of the given color or brighter if the current theme is dark.- Parameters:
color
- the color to get a darker version of- Returns:
- a darker version of the given color or brighter if the current theme is dark
-
brighter
Returns a brighter version of the given color or darker if the current theme is dark.- Parameters:
color
- the color to get a brighter version of- Returns:
- a brighter version of the given color or darker if the current theme is dark
-
registerFont
Binds the component to the font identified by the given font id. Whenever the font for the font id changes, the component will updated with the new font.Calling this method will trigger a call to
JComponent.setFont(Font)
.- Parameters:
component
- the component to set/update the fontfontId
- the id of the font to register with the given component
-
registerFont
Registers the given component with the given font style. This method allows clients to not define a font id in the theme system, but instead to signal that they want the default font for the given component, modified with the given style. As the underlying font is changed, the client will be updated with that new font with the given style applied.Most clients should not be using this method. Instead, use
registerFont(JComponent, int)
.The downside of using this method is that the end user cannot modify the style of the font. By using the standard theming mechanism for registering fonts, the end user has full control.
- Parameters:
component
- the component to set/update the fontfontStyle
- the font style, one of Font.BOLD, Font.ITALIC,
-
unRegisterFont
Removes the component and font id binding made in a previous call toregisterFont(Component, String)
.Clients need to call this method if they decide to change the font id being used for a given component. Must clients do not need to use this method.
- Parameters:
component
- the component to removefontId
- the id of the font previously registered
-
isDarkTheme
public static boolean isDarkTheme()Returns true if the active theme is using dark defaults- Returns:
- true if the active theme is using dark defaults
-
isUpdatingTheme
public static boolean isUpdatingTheme()Returns true if the theme system is in the process of updating- Returns:
- true if the theme system is in the process of updating
-
isSystemId
Returns true if the given id is a system-defined id, such as those starting withlaf.color
orsystem.color
.- Parameters:
id
- the id- Returns:
- true if the given id is a system-defined id
-
setBlinkingCursors
public static void setBlinkingCursors(boolean b) Sets application's blinking cursor state. This will affect all JTextFields, JTextAreas, JTextPanes viaUIDefaults
. Custom components can also respect this setting by either adding aThemeListener
or overridingJComponent.updateUI()
NOTE: This method is a bit odd here as it doesn't really apply to a theme. But it requires manipulation of the look and feel which is managed by the theme. If other application level properties come along and also require changing the UIDefaults, perhaps a more general solution might be to add a way for clients to register a callback so that they get a chance to change the UIDefaults map as the look and feel is loaded.
- Parameters:
b
- true for blinking text cursors, false for non-blinking text cursors
-
isBlinkingCursors
public static boolean isBlinkingCursors()Returns true if the application should allow blinking cursors, false otherwise. Custom components can use this method to determine if they should have a blinking cursor or not.- Returns:
- true if the application should allow blinking cursors, false otherwise.
-