modules/graphics/src/main/java/com/sun/javafx/css/StyleManager.java
Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization
*** 23,40 ****
* questions.
*/
package com.sun.javafx.css;
- import com.sun.javafx.css.parser.CSSParser;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.css.PseudoClass;
import javafx.css.StyleOrigin;
import javafx.css.Styleable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.SubScene;
import javafx.scene.image.Image;
--- 23,45 ----
* questions.
*/
package com.sun.javafx.css;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
+ import javafx.css.CssParser;
+ import javafx.css.FontFace;
import javafx.css.PseudoClass;
+ import javafx.css.Rule;
+ import javafx.css.Selector;
import javafx.css.StyleOrigin;
import javafx.css.Styleable;
+ import javafx.css.StyleConverter;
+ import javafx.css.Stylesheet;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.SubScene;
import javafx.scene.image.Image;
*** 154,165 ****
/**
* A map from a parent to its style cache. The parent is either a Scene root, or a
* Parent with author stylesheets. If a Scene or Parent is removed from the scene,
* it's cache is annihilated.
*/
! // package for testing
! static final Map<Parent, CacheContainer> cacheContainerMap = new WeakHashMap<>();
// package for testing
CacheContainer getCacheContainer(Styleable styleable, SubScene subScene) {
if (styleable == null && subScene == null) return null;
--- 159,170 ----
/**
* A map from a parent to its style cache. The parent is either a Scene root, or a
* Parent with author stylesheets. If a Scene or Parent is removed from the scene,
* it's cache is annihilated.
*/
! // public for testing
! public static final Map<Parent, CacheContainer> cacheContainerMap = new WeakHashMap<>();
// package for testing
CacheContainer getCacheContainer(Styleable styleable, SubScene subScene) {
if (styleable == null && subScene == null) return null;
*** 229,249 ****
/**
* A list of user-agent stylesheets from Scene or SubScene.
* The order of the entries in this list does not matter since a Scene or
* SubScene will only have zero or one user-agent stylesheets.
*/
! // package for testing
! final List<StylesheetContainer> userAgentStylesheetContainers = new ArrayList<>();
/**
* A list of user-agent stylesheet urls from calling setDefaultUserAgentStylesheet and
* addUserAgentStylesheet. The order of entries this list matters. The zeroth element is
* _the_ platform default.
*/
! // package for testing
! final List<StylesheetContainer> platformUserAgentStylesheetContainers = new ArrayList<>();
! // package for testing
! boolean hasDefaultUserAgentStylesheet = false;
////////////////////////////////////////////////////////////////////////////
//
// stylesheet handling
//
--- 234,254 ----
/**
* A list of user-agent stylesheets from Scene or SubScene.
* The order of the entries in this list does not matter since a Scene or
* SubScene will only have zero or one user-agent stylesheets.
*/
! // public for testing
! public final List<StylesheetContainer> userAgentStylesheetContainers = new ArrayList<>();
/**
* A list of user-agent stylesheet urls from calling setDefaultUserAgentStylesheet and
* addUserAgentStylesheet. The order of entries this list matters. The zeroth element is
* _the_ platform default.
*/
! // public for testing
! public final List<StylesheetContainer> platformUserAgentStylesheetContainers = new ArrayList<>();
! // public for testing
! public boolean hasDefaultUserAgentStylesheet = false;
////////////////////////////////////////////////////////////////////////////
//
// stylesheet handling
//
*** 300,310 ****
final List<Rule> rules = stylesheet.getRules();
final int rMax = rules == null || rules.isEmpty() ? 0 : rules.size();
for (int r=0; r<rMax; r++) {
final Rule rule = rules.get(r);
! final List<Selector> selectors = rule.getUnobservedSelectorList();
final int sMax = selectors == null || selectors.isEmpty() ? 0 : selectors.size();
for (int s=0; s < sMax; s++) {
final Selector selector = selectors.get(s);
selectorPartitioning.partition(selector);
--- 305,316 ----
final List<Rule> rules = stylesheet.getRules();
final int rMax = rules == null || rules.isEmpty() ? 0 : rules.size();
for (int r=0; r<rMax; r++) {
final Rule rule = rules.get(r);
! // final List<Selector> selectors = rule.getUnobservedSelectorList();
! final List<Selector> selectors = rule.getSelectors();
final int sMax = selectors == null || selectors.isEmpty() ? 0 : selectors.size();
for (int s=0; s < sMax; s++) {
final Selector selector = selectors.get(s);
selectorPartitioning.partition(selector);
*** 417,428 ****
* given URL has already been loaded then we'll simply reuse the stylesheet
* rather than loading a duplicate.
* This list is for author stylesheets and not for user-agent stylesheets. User-agent
* stylesheets are either platformUserAgentStylesheetContainers or userAgentStylesheetContainers
*/
! // package for unit testing
! final Map<String,StylesheetContainer> stylesheetContainerMap = new HashMap<>();
/**
* called from Window when the scene is closed.
*/
--- 423,434 ----
* given URL has already been loaded then we'll simply reuse the stylesheet
* rather than loading a duplicate.
* This list is for author stylesheets and not for user-agent stylesheets. User-agent
* stylesheets are either platformUserAgentStylesheetContainers or userAgentStylesheetContainers
*/
! // public for unit testing
! public final Map<String,StylesheetContainer> stylesheetContainerMap = new HashMap<>();
/**
* called from Window when the scene is closed.
*/
*** 1081,1097 ****
}
// either we failed to load the .bss file, or parse
// was set to true.
if ((url != null) && parse) {
! stylesheet = new CSSParser().parse(url);
}
if (stylesheet == null) {
if (errors != null) {
! CssError error =
! new CssError(
"Resource \""+fname+"\" not found."
);
errors.add(error);
}
if (getLogger().isLoggable(Level.WARNING)) {
--- 1087,1103 ----
}
// either we failed to load the .bss file, or parse
// was set to true.
if ((url != null) && parse) {
! stylesheet = new CssParser().parse(url);
}
if (stylesheet == null) {
if (errors != null) {
! CssParser.ParseError error =
! new CssParser.ParseError(
"Resource \""+fname+"\" not found."
);
errors.add(error);
}
if (getLogger().isLoggable(Level.WARNING)) {
*** 1102,1140 ****
}
// load any fonts from @font-face
if (stylesheet != null) {
faceLoop: for(FontFace fontFace: stylesheet.getFontFaces()) {
! for(FontFace.FontFaceSrc src: fontFace.getSources()) {
! if (src.getType() == FontFace.FontFaceSrcType.URL) {
Font loadedFont = Font.loadFont(src.getSrc(),10);
if (loadedFont == null) {
getLogger().info("Could not load @font-face font [" + src.getSrc() + "]");
}
continue faceLoop;
}
}
}
}
return stylesheet;
} catch (FileNotFoundException fnfe) {
if (errors != null) {
! CssError error =
! new CssError(
"Stylesheet \""+fname+"\" not found."
);
errors.add(error);
}
if (getLogger().isLoggable(Level.INFO)) {
getLogger().info("Could not find stylesheet: " + fname);//, fnfe);
}
} catch (IOException ioe) {
if (errors != null) {
! CssError error =
! new CssError(
"Could not load stylesheet: " + fname
);
errors.add(error);
}
if (getLogger().isLoggable(Level.INFO)) {
--- 1108,1148 ----
}
// load any fonts from @font-face
if (stylesheet != null) {
faceLoop: for(FontFace fontFace: stylesheet.getFontFaces()) {
! if (fontFace instanceof FontFaceImpl) {
! for(FontFaceImpl.FontFaceSrc src: ((FontFaceImpl)fontFace).getSources()) {
! if (src.getType() == FontFaceImpl.FontFaceSrcType.URL) {
Font loadedFont = Font.loadFont(src.getSrc(),10);
if (loadedFont == null) {
getLogger().info("Could not load @font-face font [" + src.getSrc() + "]");
}
continue faceLoop;
}
}
}
}
+ }
return stylesheet;
} catch (FileNotFoundException fnfe) {
if (errors != null) {
! CssParser.ParseError error =
! new CssParser.ParseError(
"Stylesheet \""+fname+"\" not found."
);
errors.add(error);
}
if (getLogger().isLoggable(Level.INFO)) {
getLogger().info("Could not find stylesheet: " + fname);//, fnfe);
}
} catch (IOException ioe) {
if (errors != null) {
! CssParser.ParseError error =
! new CssParser.ParseError(
"Could not load stylesheet: " + fname
);
errors.add(error);
}
if (getLogger().isLoggable(Level.INFO)) {
*** 1241,1259 ****
if (fname == null || fname.isEmpty()) {
return;
}
synchronized (styleLock) {
- // RT-20643
- CssError.setCurrentScene(scene);
-
if (_addUserAgentStylesheet(fname)) {
userAgentStylesheetsChanged();
}
-
- // RT-20643
- CssError.setCurrentScene(null);
}
}
// fname is assumed to be non null and non empty
private boolean _addUserAgentStylesheet(String fname) {
--- 1249,1261 ----
*** 1300,1321 ****
if (fname.equals(container.fname)) {
return;
}
}
- // RT-20643
- CssError.setCurrentScene(scene);
-
platformUserAgentStylesheetContainers.add(new StylesheetContainer(fname, ua_stylesheet));
if (ua_stylesheet != null) {
ua_stylesheet.setOrigin(StyleOrigin.USER_AGENT);
}
userAgentStylesheetsChanged();
-
- // RT-20643
- CssError.setCurrentScene(null);
}
}
/**
* Set the default user agent stylesheet.
--- 1302,1317 ----
*** 1338,1357 ****
if (fname == null || fname.isEmpty()) {
return;
}
synchronized (styleLock) {
- // RT-20643
-
- CssError.setCurrentScene(scene);
-
if(_setDefaultUserAgentStylesheet(fname)) {
userAgentStylesheetsChanged();
}
-
- // RT-20643
- CssError.setCurrentScene(null);
}
}
// fname is expected to be non null and non empty
private boolean _setDefaultUserAgentStylesheet(String fname) {
--- 1334,1346 ----
*** 1485,1495 ****
synchronized (styleLock) {
for (CacheContainer container : cacheContainerMap.values()) {
container.clearCache();
}
! StyleConverterImpl.clearCache();
for (Parent root : cacheContainerMap.keySet()) {
if (root == null) {
continue;
}
--- 1474,1484 ----
synchronized (styleLock) {
for (CacheContainer container : cacheContainerMap.values()) {
container.clearCache();
}
! StyleConverter.clearCache();
for (Parent root : cacheContainerMap.keySet()) {
if (root == null) {
continue;
}
*** 1571,1589 ****
if (parentStylesheets == null || parentStylesheets.isEmpty()) {
return Collections.<StylesheetContainer>emptyList();
}
synchronized (styleLock) {
! // RT-20643
! CssError.setCurrentScene(parent.getScene());
!
! final List<StylesheetContainer> list = processStylesheets(parentStylesheets, parent);
!
! // RT-20643
! CssError.setCurrentScene(null);
!
! return list;
}
}
//
//
--- 1560,1570 ----
if (parentStylesheets == null || parentStylesheets.isEmpty()) {
return Collections.<StylesheetContainer>emptyList();
}
synchronized (styleLock) {
! return processStylesheets(parentStylesheets, parent);
}
}
//
//
*** 1599,1617 ****
if (sceneStylesheets == null || sceneStylesheets.isEmpty()) {
return Collections.<StylesheetContainer>emptyList();
}
synchronized (styleLock) {
! // RT-20643
! CssError.setCurrentScene(scene);
!
! final List<StylesheetContainer> list = processStylesheets(sceneStylesheets, scene.getRoot());
!
! // RT-20643
! CssError.setCurrentScene(null);
!
! return list;
}
}
// reuse key to avoid creation of numerous small objects
private Key key = null;
--- 1580,1590 ----
if (sceneStylesheets == null || sceneStylesheets.isEmpty()) {
return Collections.<StylesheetContainer>emptyList();
}
synchronized (styleLock) {
! return processStylesheets(sceneStylesheets, scene.getRoot());
}
}
// reuse key to avoid creation of numerous small objects
private Key key = null;
*** 1850,1860 ****
//
// CssError reporting
//
////////////////////////////////////////////////////////////////////////////
! private static ObservableList<CssError> errors = null;
/**
* Errors that may have occurred during css processing.
* This list is null until errorsProperty() is called.
*
* NOTE: this is not thread-safe, and cannot readily be made so given the
--- 1823,1833 ----
//
// CssError reporting
//
////////////////////////////////////////////////////////////////////////////
! private static ObservableList<CssParser.ParseError> errors = null;
/**
* Errors that may have occurred during css processing.
* This list is null until errorsProperty() is called.
*
* NOTE: this is not thread-safe, and cannot readily be made so given the
*** 1862,1872 ****
* Currently it is only used by SceneBuilder. If a public API is ever
* needed, then a new API should be designed.
*
* @return
*/
! public static ObservableList<CssError> errorsProperty() {
if (errors == null) {
errors = FXCollections.observableArrayList();
}
return errors;
}
--- 1835,1845 ----
* Currently it is only used by SceneBuilder. If a public API is ever
* needed, then a new API should be designed.
*
* @return
*/
! public static ObservableList<CssParser.ParseError> errorsProperty() {
if (errors == null) {
errors = FXCollections.observableArrayList();
}
return errors;
}
*** 1877,1887 ****
* internally to figure out whether or not anyone is interested in
* receiving CssError.
* Not meant for general use - call errorsProperty() instead.
* @return
*/
! public static ObservableList<CssError> getErrors() {
return errors;
}
////////////////////////////////////////////////////////////////////////////
//
--- 1850,1860 ----
* internally to figure out whether or not anyone is interested in
* receiving CssError.
* Not meant for general use - call errorsProperty() instead.
* @return
*/
! public static ObservableList<CssParser.ParseError> getErrors() {
return errors;
}
////////////////////////////////////////////////////////////////////////////
//
*** 2006,2025 ****
if (inlineStylesCache == null) {
inlineStylesCache = new HashMap<>();
}
final Stylesheet inlineStylesheet =
! new CSSParser().parse("*{"+inlineStyle+"}");
if (inlineStylesheet != null) {
inlineStylesheet.setOrigin(StyleOrigin.INLINE);
List<Rule> rules = inlineStylesheet.getRules();
Rule rule = rules != null && !rules.isEmpty() ? rules.get(0) : null;
! List<Selector> selectors = rule != null ? rule.getUnobservedSelectorList() : null;
Selector selector = selectors != null && !selectors.isEmpty() ? selectors.get(0) : null;
// selector might be null if parser throws some exception
if (selector != null) {
selector.setOrdinal(-1);
--- 1979,1999 ----
if (inlineStylesCache == null) {
inlineStylesCache = new HashMap<>();
}
final Stylesheet inlineStylesheet =
! new CssParser().parse("*{"+inlineStyle+"}");
if (inlineStylesheet != null) {
inlineStylesheet.setOrigin(StyleOrigin.INLINE);
List<Rule> rules = inlineStylesheet.getRules();
Rule rule = rules != null && !rules.isEmpty() ? rules.get(0) : null;
! //List<Selector> selectors = rule != null ? rule.getUnobservedSelectorList() : null;
! List<Selector> selectors = rule != null ? rule.getSelectors() : null;
Selector selector = selectors != null && !selectors.isEmpty() ? selectors.get(0) : null;
// selector might be null if parser throws some exception
if (selector != null) {
selector.setOrdinal(-1);