changeset 5554:7c10b5a887da

RT-24129: If impl_setStyleMap is called before the styleHelper has be created, create the styleHelper then and there.
author David Grieve<david.grieve@oracle.com>
date Mon, 28 Oct 2013 15:38:24 -0400
parents 98c6c42f6644
children b5ee0d1951b6
files modules/graphics/src/main/java/javafx/scene/CssStyleHelper.java modules/graphics/src/main/java/javafx/scene/Node.java modules/graphics/src/test/java/com/sun/javafx/css/Node_cssStyleMap_Test.java
diffstat 3 files changed, 49 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/javafx/scene/CssStyleHelper.java	Mon Oct 28 14:28:54 2013 +1300
+++ b/modules/graphics/src/main/java/javafx/scene/CssStyleHelper.java	Mon Oct 28 15:38:24 2013 -0400
@@ -35,7 +35,6 @@
 import java.util.Set;
 
 import com.sun.javafx.scene.CssFlags;
-import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.ObjectProperty;
 import javafx.beans.property.SimpleObjectProperty;
 import javafx.beans.value.WritableValue;
@@ -88,7 +87,7 @@
     /**
      * Creates a new StyleHelper.
      */
-    static CssStyleHelper createStyleHelper(final Node node, WritableValue<Boolean> clearCacheOnReuse) {
+    static CssStyleHelper createStyleHelper(final Node node, WritableValue<Boolean> clearCacheOnReuse, ObservableMap<StyleableProperty<?>, List<Style>> styleObserver) {
 
         // need to know how far we are to root in order to init arrays.
         // TODO: should we hang onto depth to avoid this nonsense later?
@@ -156,13 +155,28 @@
 
             if (mightInherit == false) {
 
-                // If this node had a style helper, then reset properties to their initial value
-                // since the node won't have a style helper after this call
-                if (node.styleHelper != null) {
-                    node.styleHelper.resetToInitialValues(node);
+                //
+                // If there is an observer listening for css style changes, then that listener needs to be
+                // preserved, even if there aren't any changes. At some point in the future, this node might
+                // have styles and the observer should start notifying of changes.
+                //
+                // So, if there isn't an observer listening for css style changes, we can just return null here.
+                // If there is, then we'll fall through and create a new StyleHelper which will be assigned
+                // the current StyleHelper's observer.
+                //
+                if ((styleObserver == null) || (FXCollections.emptyObservableMap() == (ObservableMap)styleObserver)) {
+
+                    // If this node had a style helper, then reset properties to their initial value
+                    // since the node won't have a style helper after this call
+                    if (node.styleHelper != null) {
+                        node.styleHelper.resetToInitialValues(node);
+                    }
+
+                    //
+                    // This node didn't have a StyleHelper before and it doesn't need one now since there are
+                    // no styles in the StyleMap and no inherited styles.
+                    return null;
                 }
-
-                return null;
             }
 
         }
@@ -207,6 +221,9 @@
             node.styleHelper.resetToInitialValues(node);
         }
 
+        // Don't lose track of the observer, if any.
+        helper.observableStyleMap = styleObserver;
+
         return helper;
     }
 
@@ -884,6 +901,7 @@
 
         if (cssMetaData.getConverter() == FontConverter.getInstance()) {
 
+            if (styleList != null) styleList.addAll(getMatchingStyles(styleable, cssMetaData));
             return lookupFont(styleable, cssMetaData.getProperty(), styleMap, cachedFont, styleList);
         }
 
--- a/modules/graphics/src/main/java/javafx/scene/Node.java	Mon Oct 28 14:28:54 2013 +1300
+++ b/modules/graphics/src/main/java/javafx/scene/Node.java	Mon Oct 28 15:38:24 2013 -0400
@@ -150,12 +150,7 @@
 import com.sun.javafx.scene.traversal.Direction;
 import com.sun.javafx.sg.prism.NGNode;
 import com.sun.javafx.tk.Toolkit;
-import com.sun.javafx.accessible.providers.AccessibleProvider;
-import com.sun.javafx.geom.transform.Affine2D;
-import com.sun.javafx.geom.transform.AffineBase;
-import com.sun.javafx.geom.transform.Translate2D;
 import com.sun.prism.impl.PrismSettings;
-import javafx.scene.transform.Translate;
 import sun.util.logging.PlatformLogger;
 import sun.util.logging.PlatformLogger.Level;
 
@@ -8548,7 +8543,14 @@
       */
      @Deprecated // SB-dependency: RT-21096 has been filed to track this
      public final void impl_setStyleMap(ObservableMap<StyleableProperty<?>, List<Style>> styleMap) {
-         if (styleHelper != null) {
+         //
+         // Node doesn't have a field for this map. Rather, this is held by CssStyleHelper. If this node
+         // doesn't have a styleHelper, then there is nothing to attach the listener to. So a styleHelper has
+         // to be created.
+         //
+         if (styleHelper == null) {
+             styleHelper = CssStyleHelper.createStyleHelper(this, null, styleMap);
+         } else {
              styleHelper.setObservableStyleMap(styleMap);
          }
      }
@@ -8860,8 +8862,11 @@
                 return;
             }
 
+            ObservableMap<StyleableProperty<?>, List<Style>> styleObserver =
+                    styleHelper != null ? styleHelper.observableStyleMap : null;
+
             // Match new styles if my own indicates I need to reapply
-            styleHelper = CssStyleHelper.createStyleHelper(this, cacheHint);
+            styleHelper = CssStyleHelper.createStyleHelper(this, cacheHint, styleObserver);
 
         }
 
--- a/modules/graphics/src/test/java/com/sun/javafx/css/Node_cssStyleMap_Test.java	Mon Oct 28 14:28:54 2013 +1300
+++ b/modules/graphics/src/test/java/com/sun/javafx/css/Node_cssStyleMap_Test.java	Mon Oct 28 15:38:24 2013 -0400
@@ -46,21 +46,17 @@
 import javafx.scene.text.Text;
 import javafx.stage.Stage;
 import static org.junit.Assert.*;
+
 import org.junit.Test;
 
-
 public class Node_cssStyleMap_Test {
     
     public Node_cssStyleMap_Test() {
     }
 
-    int nchanges = 0;
+    boolean disabled = false;
     int nadds = 0;
     int nremoves = 0;
-    boolean disabled = false;
-    Group group;
-    Rectangle rect;
-    Text text;    
 
     static List<CascadingStyle> createStyleList(List<Declaration> decls) {
         
@@ -100,7 +96,7 @@
         return smap;
     }
     
-    @Test @org.junit.Ignore
+    @Test
     public void testStyleMapTracksChanges() {
                 
         final List<Declaration> declsNoState = new ArrayList<Declaration>();
@@ -120,7 +116,7 @@
         
         Rule rule = new Rule(selsNoState, declsNoState);        
         
-        Stylesheet stylesheet = new Stylesheet();
+        Stylesheet stylesheet = new Stylesheet("testStyleMapTracksChanges");
         stylesheet.setOrigin(StyleOrigin.USER_AGENT);
         stylesheet.getRules().add(rule);
         
@@ -143,7 +139,6 @@
         
         // add to this list on wasAdded, check bean on wasRemoved.
         final List<StyleableProperty<?>> beans = new ArrayList<StyleableProperty<?>>();
-        
         Rectangle rect = new Rectangle(50,50);
         rect.getStyleClass().add("rect");
         rect.impl_setStyleMap(FXCollections.observableMap(new HashMap<StyleableProperty<?>, List<Style>>()));
@@ -206,7 +201,7 @@
         
     }
     
-    @Test @org.junit.Ignore
+    @Test
     public void testRT_21212() {
 
         final List<Declaration> rootDecls = new ArrayList<Declaration>();
@@ -223,7 +218,7 @@
         
         Rule rootRule = new Rule(rootSels, rootDecls);        
         
-        Stylesheet stylesheet = new Stylesheet();
+        Stylesheet stylesheet = new Stylesheet("testRT_21212");
         stylesheet.setOrigin(StyleOrigin.USER_AGENT);
         stylesheet.getRules().add(rootRule);
 
@@ -232,7 +227,7 @@
         final Map<StyleCache.Key, StyleCache> styleCache = 
             new HashMap<StyleCache.Key, StyleCache>();
         
-        group = new Group();
+        Group group = new Group();
         group.getStyleClass().add("root");
         
         
@@ -263,11 +258,11 @@
         final Map<String,List<CascadingStyle>> styleMap = createStyleMap(styles);
         final Map<String,List<CascadingStyle>> emptyMap = createStyleMap(null);
 
-        text = new Text("HelloWorld");
+        Text text = new Text("HelloWorld");
         group.getChildren().add(text);
 
         final List<Declaration> expecteds = new ArrayList<Declaration>();
-//        expecteds.addAll(rootDecls);
+        expecteds.addAll(rootDecls);
         expecteds.addAll(textDecls);
         text.getStyleClass().add("text");
         text.impl_setStyleMap(FXCollections.observableMap(new HashMap<StyleableProperty<?>, List<Style>>()));
@@ -283,7 +278,7 @@
                         assertTrue(expecteds.contains(style.getDeclaration()));
                         expecteds.remove(style.getDeclaration());
                     }
-                } 
+                }
             }
         });
              
@@ -292,18 +287,10 @@
         Stage stage = new Stage();
         stage.setScene(scene);
         stage.show();
-        
+
         assertEquals(18, text.getFont().getSize(),0);
         assertTrue(Integer.toString(expecteds.size()), expecteds.isEmpty());
 
-        text.getStyleClass().clear();
-
-        Toolkit.getToolkit().firePulse();
-        
-        // PENDING RT-25002
-//        assertEquals(12, text.getFont().getSize(),0);
-//        assertTrue(text.impl_getStyleMap().toString(), text.impl_getStyleMap().isEmpty());
-        
     } 
     
 }