Class ThemeManager
- Direct Known Subclasses:
ApplicationThemeManager
,HeadlessThemeManager
,StubThemeManager
ApplicationThemeManager.initialize()
.
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.
Clients that just need to access the colors, fonts, and icons from the theme can use the
convenience methods in the Gui
class. Clients that need to directly manipulate the
themes and values will need to directly use the ThemeManager which and be retrieved using the
static getInstance()
method.
-
Field Summary
Modifier and TypeFieldDescriptionprotected LafType
protected GTheme
protected ApplicationThemeDefaults
protected GThemeValueMap
protected static ThemeManager
protected GThemeValueMap
static final String
protected boolean
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the given theme to set of all themes.void
addThemeListener
(ThemeListener listener) Adds aThemeListener
to be notified of theme changes.void
adjustFonts
(int amount) Adjust the size of all fonts by the given amount.protected void
This method is called to create the internal set of theme value used by the application.void
deleteTheme
(GTheme theme) Removes the theme from the set of all themes.protected void
Returns the active theme.Returns a set of all known themes.Returns theGThemeValueMap
containing all the dark default values defined in theme.properties files.Returns theGThemeValueMap
containing all the standard default values defined in theme.properties files.Returns theColor
registered for the given id.Returns aGThemeValueMap
of all current theme values including unsaved changes to the theme.Returns aGThemeValueMap
containing all default values for the current theme.static GTheme
Returns the default theme for the current platform.Returns the currentFont
associated with the given id.Returns the Icon registered for the given id.static ThemeManager
Returns theGThemeValueMap
containing all the default theme values defined by the currentLookAndFeel
.Returns theLafType
for the currently activeLookAndFeel
Returns aGThemeValueMap
contains all values that differ from the default values (values defined by theLookAndFeel
or in the theme.properties files.Returns a set of all known themes that are supported on the current platform.Returns the known theme that has the given name.Returns the theme values as defined by the current theme, ignoring any unsaved changes that are currently applied to the application.boolean
Returns true if an color for the given Id has been definedboolean
Returns true if an font for the given Id has been definedboolean
Returns true if an icon for the given Id has been definedboolean
Returns true if there are any unsaved changes to the current theme.boolean
Returns true if any theme values have changed.protected void
protected boolean
Returns true if the application should allow blinking cursors, false otherwise.boolean
isChangedColor
(String id) Returns true if the color associated with the given id has been changed from the current theme value for that id.boolean
isChangedFont
(String id) Returns true if the font associated with the given id has been changed from the current theme value for that id.boolean
isChangedIcon
(String id) Returns true if the Icon associated with the given id has been changed from the current theme value for that id.boolean
Returns true if the current theme use dark default values.boolean
Returns true if the theme system is in the process of updatingboolean
Returns true if the given UI object is using the Aqua Look and Feel.boolean
Returns true if 'Nimbus' is the current Look and Feelprotected ApplicationThemeDefaults
protected void
notifyThemeChanged
(ThemeEvent event) void
registerFont
(Component component, String fontId) Binds the component to the font identified by the given font id.void
registerFont
(Component component, String fontId, int fontStyle) Binds the component to the font identified by the given font id.void
removeThemeListener
(ThemeListener listener) Removes the givenThemeListener
from the list of listeners to be notified of theme changes.void
restoreColor
(String id) Restores the current color value for the given color id to the value established by the current theme.void
restoreFont
(String id) Restores the current font value for the given font id to the value established by the current theme.void
restoreIcon
(String id) Restores the current icon value for the given icon id to the value established by the current theme.void
Restores all the current application back to the values as specified by the active theme.protected void
setBlinkingCursors
(boolean b) Sets application's blinking cursor state.void
setColor
(ColorValue newValue) Updates the current value for the color id in the newValuevoid
Updates the current color for the given id.void
Updates the current value for the font id in the newValuevoid
Updates the current font for the given id.void
Updates the current value for theIcon
id in the newValuevoid
Updates the currentIcon
for the given id.void
setLookAndFeel
(LafType lafType, boolean useDarkDefaults) Sets the currentLookAndFeel
.void
Sets the application's active theme to the given theme.void
unRegisterFont
(JComponent component, String fontId) Removes the component and font id binding made in a previous call toregisterFont(Component, String)
.protected void
-
Field Details
-
THEME_DIR
- See Also:
-
INSTANCE
-
activeTheme
-
activeLafType
-
useDarkDefaults
protected boolean useDarkDefaults -
javaDefaults
-
currentValues
-
applicationDefaults
-
-
Constructor Details
-
ThemeManager
public ThemeManager()
-
-
Method Details
-
getInstance
-
loadApplicationDefaults
-
installInGui
protected void installInGui() -
buildCurrentValues
protected void buildCurrentValues()This method is called to create the internal set of theme value used by the application. To do this, we use a layered approach to install values, with the last values added overwriting any pre-existing values with the same key. The values are added in the following order:java defaults -> light values -> dark values -> look and feel values -> property file values -> theme values
At the point this method is called, this is the state of these various values:
- The 'javaValues' are normalized in the form of 'laf.font.TextArea'
- The 'applicationDefaults' contains values loaded from the
theme.properties
files:font.listing.base font.monospaced [color]Viewport.background = color.bg [laf.font]TextArea.font = font.monospaced [laf.boolean]Button.rollover = true
- The 'activeTheme' values are those loaded by the current theme, which has any changes made to the default values
-
restoreThemeValues
public void restoreThemeValues()Restores all the current application back to the values as specified by the active theme. In other words, reverts any changes to the active theme that haven't been saved. -
restoreColor
Restores the current color value for the given color id to the value established by the current theme.- Parameters:
id
- the color id to restore back to the original theme value
-
restoreFont
Restores the current font value for the given font id to the value established by the current theme.- Parameters:
id
- the font id to restore back to the original theme value
-
restoreIcon
Restores the current icon value for the given icon id to the value established by the current theme.- Parameters:
id
- the icon id to restore back to the original theme value
-
isChangedColor
Returns true if the color associated with the given id has been changed from the current theme value for that id.- Parameters:
id
- the color id to check if it has been changed- Returns:
- true if the color associated with the given id has been changed from the current theme value for that id.
-
isChangedFont
Returns true if the font associated with the given id has been changed from the current theme value for that id.- Parameters:
id
- the font id to check if it has been changed- Returns:
- true if the font associated with the given id has been changed from the current theme value for that id.
-
isChangedIcon
Returns true if the Icon associated with the given id has been changed from the current theme value for that id.- Parameters:
id
- the Icon id to check if it has been changed- Returns:
- true if the Icon associated with the given id has been changed from the current theme value for that id.
-
setTheme
Sets the application's active theme to the given theme.- Parameters:
theme
- the theme to make active
-
setLookAndFeel
Sets the currentLookAndFeel
. This is used by theme editors to allow users to see the effects of changing LookAndFeels when configuring a theme. Setting this different from the activeTheme's LookAndFeel setting means the the current theme is in an unsaved state and causes thehasThemeChanges()
method to return true.- Parameters:
lafType
- theLafType
to set the LookAndFeel touseDarkDefaults
- true if the application should used dark defaults with this LookAndFeel
-
addTheme
Adds the given theme to set of all themes.- Parameters:
newTheme
- the theme to add
-
deleteTheme
Removes the theme from the set of all themes. Also, if the theme has an associated file, the file will be deleted.- Parameters:
theme
- the theme to delete
-
getAllThemes
Returns a set of all known themes.- Returns:
- a set of all known themes.
-
getSupportedThemes
Returns a set of all known themes that are supported on the current platform.- Returns:
- a set of all known themes that are supported on the current platform.
-
getActiveTheme
Returns the active theme.- Returns:
- the active theme.
-
getLookAndFeelType
Returns theLafType
for the currently activeLookAndFeel
- Returns:
- the
LafType
for the currently activeLookAndFeel
-
getTheme
Returns the known theme that has the given name.- Parameters:
themeName
- the name of the theme to retrieve- Returns:
- the known theme that has the given name
-
getCurrentValues
Returns aGThemeValueMap
of all current theme values including unsaved changes to the theme.- Returns:
- a
GThemeValueMap
of all current theme values
-
getThemeValues
Returns the theme values as defined by the current theme, ignoring any unsaved changes that are currently applied to the application.- Returns:
- the theme values as defined by the current theme, ignoring any unsaved changes that are currently applied to the application
-
getNonDefaultValues
Returns aGThemeValueMap
contains all values that differ from the default values (values defined by theLookAndFeel
or in the theme.properties files.- Returns:
- a
GThemeValueMap
contains all values that differ from the defaults.
-
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.
-
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.
-
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
-
setFont
Updates the current font for the given id.- Parameters:
id
- the font id to update to the new colorfont
- the new font for the id
-
setFont
Updates the current value for the font id in the newValue- Parameters:
newValue
- the newFontValue
to install in the current values.
-
setColor
Updates the current color for the given id.- Parameters:
id
- the color id to update to the new colorcolor
- the new color for the id
-
setColor
Updates the current value for the color id in the newValue- Parameters:
newValue
- the newColorValue
to install in the current values.
-
setIcon
Updates the currentIcon
for the given id.- Parameters:
id
- the icon id to update to the new iconicon
- the newIcon
for the id
-
setIcon
Updates the current value for theIcon
id in the newValue- Parameters:
newValue
- the newIconValue
to install in the current values.
-
getJavaDefaults
Returns theGThemeValueMap
containing all the default theme values defined by the currentLookAndFeel
.- Returns:
- the
GThemeValueMap
containing all the default theme values defined by the currentLookAndFeel
-
getApplicationDarkDefaults
Returns theGThemeValueMap
containing all the dark default values defined in theme.properties files. Note that dark defaults includes light defaults that haven't been overridden by a dark default with the same id.- Returns:
- the
GThemeValueMap
containing all the dark values defined in theme.properties files
-
getApplicationLightDefaults
Returns theGThemeValueMap
containing all the standard default values defined in theme.properties files.- Returns:
- the
GThemeValueMap
containing all the standard values defined in theme.properties files
-
getDefaults
Returns aGThemeValueMap
containing all default values for the current theme. It is a combination of application defined defaults and javaLookAndFeel
defaults.- Returns:
- the current set of defaults.
-
isUsingAquaUI
Returns true if the given UI object is using the Aqua Look and Feel.- Parameters:
UI
- the UI to examine.- Returns:
- true if the UI is using Aqua
-
isUsingNimbusUI
public boolean isUsingNimbusUI()Returns true if 'Nimbus' is the current Look and Feel- Returns:
- true if 'Nimbus' is the current Look and Feel
-
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
-
hasThemeChanges
public boolean hasThemeChanges()Returns true if there are any unsaved changes to the current theme.- Returns:
- true if there are any unsaved changes to the current theme.
-
hasThemeValueChanges
public boolean hasThemeValueChanges()Returns true if any theme values have changed. This does not take into account the current Look and Feel. UsehasThemeChanges()
to also account for changes to the Look and Feel.- Returns:
- true if any theme values have changed
-
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
-
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.- Parameters:
component
- the component to set/update the fontfontId
- the id of the font to register with the given component
-
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.This method is fairly niche and should not be called by most clients. Instead, call
registerFont(Component, String)
.- Parameters:
component
- the component to set/update the fontfontId
- the id of the font to register with the given componentfontStyle
- the font style
-
unRegisterFont
Removes the component and font id binding made in a previous call toregisterFont(Component, String)
.- Parameters:
component
- the component to removefontId
- the id of the font previously registered
-
isDarkTheme
public boolean isDarkTheme()Returns true if the current theme use dark default values.- Returns:
- true if the current theme use dark default values.
-
getDefaultTheme
Returns the default theme for the current platform.- Returns:
- the default theme for the current platform.
-
isUpdatingTheme
public 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
-
update
-
notifyThemeChanged
-
error
-
adjustFonts
public void adjustFonts(int amount) Adjust the size of all fonts by the given amount.- Parameters:
amount
- the number to add to the current font size;
-
setBlinkingCursors
protected 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
protected 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.
-