public final class StyleManager extends Object
One of the key responsibilities of the StylesheetContainer is to create and maintain an admittedly elaborate series of caches so as to minimize the amount of time it takes to match a Node to its eventual StyleHelper, and to reuse the StyleHelper as much as possible.
Initially, the cache is empty. It is recreated whenever the userStylesheets on the container change, or whenever the userAgentStylesheet changes. The cache is built up as nodes are looked for, and thus there is some overhead associated with the first lookup but which is then not repeated for subsequent lookups.
The cache system used is a two level cache. The first level cache simply maps the classname/id/styleclass combination of the request node to a 2nd level cache. If the node has "styles" specified then we still use this 2nd level cache, but must combine its selectors with the selectors specified in "styles" and perform more work to cascade properly.
The 2nd level cache contains a data structure called the Cache. The Cache contains an ordered sequence of Rules, a Long, and a Map. The ordered sequence of selectors are the selectors that *may* match a node with the given classname, id, and style class. For example, selectors which may apply are any selector where the simple selector of the selector contains a reference to the id, style class, or classname of the Node, or a compound selector who's "descendant" part is a simple selector which contains a reference to the id, style class, or classname of the Node.
During lookup, we will iterate over all the potential selectors and discover if they apply to this particular node. If so, then we toggle a bit position in the Long corresponding to the position of the selector that matched. This long then becomes our key into the final map.
Once we have established our key, we will visit the map and look for an existing StyleHelper. If we find a StyleHelper, then we will return it. If not, then we will take the Rules that matched and construct a new StyleHelper from their various parts.
This system, while elaborate, also provides for numerous fast paths and sharing of data structures which should dramatically reduce the memory and runtime performance overhead associated with CSS by reducing the matching overhead and caching as much as possible. We make no attempt to use weak references here, so if memory issues result one work around would be to toggle the root user agent stylesheet or stylesheets on the scene to cause the cache to be flushed.
Modifier and Type | Method and Description |
---|---|
void |
addUserAgentStylesheet(Scene scene,
String url)
Add a user agent stylesheet, possibly overriding styles in the default
user agent stylesheet.
|
void |
addUserAgentStylesheet(Scene scene,
Stylesheet ua_stylesheet)
Add a user agent stylesheet, possibly overriding styles in the default
user agent stylesheet.
|
void |
addUserAgentStylesheet(String fname)
Add a user agent stylesheet, possibly overriding styles in the default
user agent stylesheet.
|
static ObservableList<CssError> |
errorsProperty()
Errors that may have occurred during css processing.
|
StyleMap |
findMatchingStyles(Node node,
SubScene subScene,
Set<PseudoClass>[] triggerStates)
Finds matching styles for this Node.
|
void |
forget(Parent parent)
Called from Parent's scenesChanged method when the Parent's scene is set to null.
|
void |
forget(Scene scene)
called from Window when the scene is closed.
|
void |
forget(SubScene subScene)
called from Window when the scene is closed.
|
Image |
getCachedImage(String url) |
static ObservableList<CssError> |
getErrors()
Errors that may have occurred during css processing.
|
static StyleManager |
getInstance()
Return the StyleManager instance.
|
StyleCache |
getSharedCache(Styleable styleable,
SubScene subScene,
StyleCache.Key key)
StyleHelper uses this cache but it lives here so it can be cleared
when style-sheets change.
|
StyleMap |
getStyleMap(Styleable styleable,
SubScene subScene,
int smapId) |
static Stylesheet |
loadStylesheet(String fname) |
void |
removeUserAgentStylesheet(String url)
Removes the specified stylesheet from the application default user agent
stylesheet list.
|
void |
setDefaultUserAgentStylesheet(Scene scene,
String url)
Set the default user agent stylesheet
|
void |
setDefaultUserAgentStylesheet(String fname)
Set the default user agent stylesheet.
|
void |
setDefaultUserAgentStylesheet(Stylesheet ua_stylesheet)
Set the user agent stylesheet.
|
void |
setUserAgentStylesheets(List<String> urls)
Set a bunch of user agent stylesheets all at once.
|
void |
stylesheetsChanged(Parent parent,
ListChangeListener.Change<String> c)
called from Parent's stylesheets property's onChanged method
|
void |
stylesheetsChanged(Scene scene,
ListChangeListener.Change<String> c)
called from Scene's stylesheets property's onChanged method
|
public static StyleManager getInstance()
public StyleCache getSharedCache(Styleable styleable, SubScene subScene, StyleCache.Key key)
public void forget(Scene scene)
public void stylesheetsChanged(Scene scene, ListChangeListener.Change<String> c)
public void forget(Parent parent)
parent
- The Parent being removed from the scene-graphpublic void stylesheetsChanged(Parent parent, ListChangeListener.Change<String> c)
public void forget(SubScene subScene)
public static Stylesheet loadStylesheet(String fname)
public void setUserAgentStylesheets(List<String> urls)
urls
- The list of stylesheet URLs as Strings.public void addUserAgentStylesheet(String fname)
fname
- The file URL, either relative or absolute, as a String.public void addUserAgentStylesheet(Scene scene, String url)
scene
- Only used in CssError for tracking back to the scene that loaded the stylesheeturl
- The file URL, either relative or absolute, as a String.public void addUserAgentStylesheet(Scene scene, Stylesheet ua_stylesheet)
scene
- Only used in CssError for tracking back to the scene that loaded the stylesheetua_stylesheet
- The stylesheet to add as a user-agent stylesheetpublic void setDefaultUserAgentStylesheet(String fname)
fname
- The file URL, either relative or absolute, as a String.public void setDefaultUserAgentStylesheet(Scene scene, String url)
scene
- Only used in CssError for tracking back to the scene that loaded the stylesheeturl
- The file URL, either relative or absolute, as a String.public void removeUserAgentStylesheet(String url)
url
- The file URL, either relative or absolute, as a String.public void setDefaultUserAgentStylesheet(Stylesheet ua_stylesheet)
public StyleMap findMatchingStyles(Node node, SubScene subScene, Set<PseudoClass>[] triggerStates)
public static ObservableList<CssError> errorsProperty()
public static ObservableList<CssError> getErrors()
Copyright © 2020. All rights reserved.