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 whose "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 | Class and Description |
|---|---|
| private static class | StyleManager.
Creates and caches maps of styles, reusing them as often as practical. |
| pack-priv static class | |
| private static class | |
| private static class | |
| private static class | StyleManager.
The key used in the cacheMap of the StylesheetContainer |
| pack-priv static class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| public static final Map | cacheContainerMap
A map from a parent to its style cache. |
| private static List | |
| private static ObservableList | |
| public boolean | |
| private final StyleManager. | |
| private StyleManager. | |
| private static PlatformLogger | |
| public final List | platformUserAgentStylesheetContainers
A list of user-agent stylesheet urls from calling setDefaultUserAgentStylesheet and addUserAgentStylesheet. |
| private static final String | |
| private static final String | |
| private static final Object | styleLock
Global lock object for the StyleManager. |
| public final Map | stylesheetContainerMap
A map from String => Stylesheet. |
| public final List | userAgentStylesheetContainers
A list of user-agent stylesheets from Scene or SubScene. |
| private final WeakHashMap |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private boolean | |
| private boolean | |
| public void | addUserAgentStylesheet(String
The file URL, either relative or absolute, as a String. fname)Add a user agent stylesheet, possibly overriding styles in the default user agent stylesheet. |
| public void | addUserAgentStylesheet(Scene
Only used in CssError for tracking back to the scene that loaded the stylesheet scene, String The file URL, either relative or absolute, as a String. url)Add a user agent stylesheet, possibly overriding styles in the default user agent stylesheet. |
| public void | addUserAgentStylesheet(Scene
Only used in CssError for tracking back to the scene that loaded the stylesheet scene, Stylesheet The stylesheet to add as a user-agent stylesheet ua_stylesheet)Add a user agent stylesheet, possibly overriding styles in the default user agent stylesheet. |
| pack-priv static byte[] | |
| public static ObservableList | |
| public StyleMap | findMatchingStyles(Node node, SubScene subScene, Set<PseudoClass>[] triggerStates)
Finds matching styles for this Node. |
| public void | |
| public void | |
| public void | |
| private List | |
| private List | |
| pack-priv StyleManager. | |
| public Image | |
| public static ObservableList | |
| public static StyleManager | |
| private static PlatformLogger | |
| public StyleCache | getSharedCache(Styleable styleable, SubScene subScene, StyleCache.
StyleHelper uses this cache but it lives here so it can be cleared when style-sheets change. |
| public StyleMap | |
| private static URL | |
| public static Stylesheet | |
| private List | |
| private void | |
| public void | removeUserAgentStylesheet(String
The file URL, either relative or absolute, as a String. url)Removes the specified stylesheet from the application default user agent stylesheet list. |
| public void | setDefaultUserAgentStylesheet(String
The file URL, either relative or absolute, as a String. fname)Set the default user agent stylesheet. |
| public void | setDefaultUserAgentStylesheet(Scene
Only used in CssError for tracking back to the scene that loaded the stylesheet scene, String The file URL, either relative or absolute, as a String. url)Set the default user agent stylesheet |
| public void | |
| public void | setUserAgentStylesheets(List<String>
The list of stylesheet URLs as Strings. urls)Set a bunch of user agent stylesheets all at once. |
| private void | |
| private void | |
| public void | stylesheetsChanged(Scene scene, ListChangeListener.
called from Scene's stylesheets property's onChanged method |
| public void | stylesheetsChanged(Parent parent, ListChangeListener.
called from Parent's stylesheets property's onChanged method |
| private void |