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,18 +23,23 @@
  * 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.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,12 +159,12 @@
     /**
      * 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<>();
+    // 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,21 +234,21 @@
     /**
      * 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<>();
+    // 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.
      */
-    // package for testing
-    final List<StylesheetContainer> platformUserAgentStylesheetContainers = new ArrayList<>();
-    // package for testing
-    boolean hasDefaultUserAgentStylesheet = false;
+    // public for testing
+    public final List<StylesheetContainer> platformUserAgentStylesheetContainers = new ArrayList<>();
+    // public for testing
+    public boolean hasDefaultUserAgentStylesheet = false;
 
     ////////////////////////////////////////////////////////////////////////////
     //
     // stylesheet handling
     //

@@ -300,11 +305,12 @@
                 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.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,12 +423,12 @@
      * 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<>();
+    // public for unit testing
+    public final Map<String,StylesheetContainer> stylesheetContainerMap = new HashMap<>();
 
 
     /**
      * called from Window when the scene is closed.
      */

@@ -1081,17 +1087,17 @@
                 }
 
                 // either we failed to load the .bss file, or parse
                 // was set to true.
                 if ((url != null) && parse) {
-                    stylesheet = new CSSParser().parse(url);
+                    stylesheet = new CssParser().parse(url);
                 }
 
                 if (stylesheet == null) {
                     if (errors != null) {
-                        CssError error =
-                            new CssError(
+                        CssParser.ParseError error =
+                            new CssParser.ParseError(
                                 "Resource \""+fname+"\" not found."
                             );
                         errors.add(error);
                     }
                     if (getLogger().isLoggable(Level.WARNING)) {

@@ -1102,39 +1108,41 @@
                 }
 
                 // 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) {
+                        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) {
-                    CssError error =
-                        new CssError(
+                    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) {
-                        CssError error =
-                            new CssError(
+                        CssParser.ParseError error =
+                            new CssParser.ParseError(
                                 "Could not load stylesheet: " + fname
                             );
                         errors.add(error);
                     }
                 if (getLogger().isLoggable(Level.INFO)) {

@@ -1241,19 +1249,13 @@
         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) {

@@ -1300,22 +1302,16 @@
                 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.

@@ -1338,20 +1334,13 @@
         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) {

@@ -1485,11 +1474,11 @@
         synchronized (styleLock) {
             for (CacheContainer container : cacheContainerMap.values()) {
                 container.clearCache();
             }
 
-            StyleConverterImpl.clearCache();
+            StyleConverter.clearCache();
 
             for (Parent root : cacheContainerMap.keySet()) {
                 if (root == null) {
                     continue;
                 }

@@ -1571,19 +1560,11 @@
         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;
+            return processStylesheets(parentStylesheets, parent);
         }
     }
 
     //
     //

@@ -1599,19 +1580,11 @@
         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;
+            return processStylesheets(sceneStylesheets, scene.getRoot());
         }
     }
 
     // reuse key to avoid creation of numerous small objects
     private Key key = null;

@@ -1850,11 +1823,11 @@
     //
     // CssError reporting
     //
     ////////////////////////////////////////////////////////////////////////////
 
-    private static ObservableList<CssError> errors = null;
+    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,11 +1835,11 @@
      * 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() {
+    public static ObservableList<CssParser.ParseError> errorsProperty() {
         if (errors == null) {
             errors = FXCollections.observableArrayList();
         }
         return errors;
     }

@@ -1877,11 +1850,11 @@
      * 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() {
+    public static ObservableList<CssParser.ParseError> getErrors() {
         return errors;
     }
 
     ////////////////////////////////////////////////////////////////////////////
     //

@@ -2006,20 +1979,21 @@
             if (inlineStylesCache == null) {
                 inlineStylesCache = new HashMap<>();
             }
 
             final Stylesheet inlineStylesheet =
-                    new CSSParser().parse("*{"+inlineStyle+"}");
+                    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.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);