changeset 2327:521518548a66

Fixed RT-27804: Add support for creating graphic region from css
author "Jasper Potts"
date Fri, 18 Jan 2013 15:36:58 -0800
parents b11de5fd3149
children 11eea5a7907d
files javafx-ui-common/src/com/sun/javafx/css/parser/CSSParser.java javafx-ui-controls/src/javafx/scene/control/Labeled.java
diffstat 2 files changed, 50 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-common/src/com/sun/javafx/css/parser/CSSParser.java	Fri Jan 18 21:36:43 2013 +0000
+++ b/javafx-ui-common/src/com/sun/javafx/css/parser/CSSParser.java	Fri Jan 18 15:36:58 2013 -0800
@@ -54,6 +54,7 @@
 import javafx.scene.text.Font;
 import javafx.scene.text.FontPosture;
 import javafx.scene.text.FontWeight;
+import com.sun.javafx.Utils;
 import com.sun.javafx.css.Combinator;
 import com.sun.javafx.css.CompoundSelector;
 import com.sun.javafx.css.CssError;
@@ -1379,6 +1380,8 @@
             return parseRepeatingImagePattern(root);
         } else if ("ladder".regionMatches(true, 0, fcn, 0, 6)) {
             return parseLadder(root);
+        } else if ("region".regionMatches(true, 0, fcn, 0, 6)) {
+            return parseRegion(root);
         } else if ("url".regionMatches(true, 0, fcn, 0, 3)) {
             return parseURI(root);
         } else {
@@ -3298,7 +3301,28 @@
         return new ParsedValueImpl<ParsedValue<ParsedValue<?,Size>[],BorderWidths>[],BorderWidths[]> (layers, BorderImageWidthsSequenceConverter.getInstance());
     }
 
-
+    // parse a Region value
+    // i.e., region(".styleClassForRegion") or region("#idForRegion")
+    public static final String SPECIAL_REGION_URL_PREFIX = "SPECIAL-REGION-URL:";
+    private ParsedValueImpl<String,String> parseRegion(Term root)
+            throws ParseException {
+        // first term in the chain is the function name...
+        final String fn = (root.token != null) ? root.token.getText() : null;
+        if (!"region".regionMatches(true, 0, fn, 0, 6)) {
+            error(root,"Expected \'region\'");
+        }
+
+        Term arg = root.firstArg;
+        if (arg == null) error(root, "Expected \'region(\"<styleclass-or-id-string>\")\'");
+
+        if (arg.token == null ||
+                arg.token.getType() != CSSLexer.STRING ||
+                arg.token.getText() == null ||
+                arg.token.getText().isEmpty())  error(root, "Expected \'region(\"<styleclass-or-id-string>\")\'");
+
+        final String styleClassOrId = SPECIAL_REGION_URL_PREFIX+ Utils.stripQuotes(arg.token.getText());
+        return new ParsedValueImpl<String,String>(styleClassOrId, StringConverter.getInstance());
+    }
 
     // parse a URI value
     // i.e., url("<uri>")
--- a/javafx-ui-controls/src/javafx/scene/control/Labeled.java	Fri Jan 18 21:36:43 2013 +0000
+++ b/javafx-ui-controls/src/javafx/scene/control/Labeled.java	Fri Jan 18 15:36:58 2013 -0800
@@ -26,12 +26,15 @@
 package javafx.scene.control;
 
 
+import com.sun.javafx.css.Selector;
+import com.sun.javafx.css.SimpleSelector;
 import com.sun.javafx.css.converters.BooleanConverter;
 import com.sun.javafx.css.converters.EnumConverter;
 import com.sun.javafx.css.converters.InsetsConverter;
 import com.sun.javafx.css.converters.PaintConverter;
 import com.sun.javafx.css.converters.SizeConverter;
 import com.sun.javafx.css.converters.StringConverter;
+import com.sun.javafx.css.parser.CSSParser;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -41,7 +44,6 @@
 import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.DoubleProperty;
 import javafx.beans.property.ObjectProperty;
-import javafx.beans.property.ObjectPropertyBase;
 import javafx.beans.property.ReadOnlyObjectProperty;
 import javafx.beans.property.SimpleBooleanProperty;
 import javafx.beans.property.SimpleStringProperty;
@@ -52,6 +54,7 @@
 import javafx.scene.Node;
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
+import javafx.scene.layout.Region;
 import javafx.scene.paint.Color;
 import javafx.scene.paint.Paint;
 import javafx.scene.text.Font;
@@ -414,28 +417,36 @@
         if (imageUrl == null) {
             imageUrl = new StyleableStringProperty() {
 
-            @Override
-            public void applyStyle(StyleOrigin origin, String v) {
-
-                super.applyStyle(origin, v);
-                
-                    String imageUrl = null;
-                    if (v != null) {
+                @Override
+                public void applyStyle(StyleOrigin origin, String v) {
+                    super.applyStyle(origin, v);
+                    if (v == null) {
+                        ((StyleableProperty)graphicProperty()).applyStyle(origin, null);
+                    } else if (v.startsWith(CSSParser.SPECIAL_REGION_URL_PREFIX)) {
+                        final Region region = new Region();
+                        final String styleClassOrId = v.substring(CSSParser.SPECIAL_REGION_URL_PREFIX.length());
+                        if (styleClassOrId.length() > 0) {
+                            Selector s = Selector.createSelector(styleClassOrId);
+                            if (s instanceof SimpleSelector) {
+                                SimpleSelector ss = (SimpleSelector)s;
+                                region.setId(ss.getId());
+                                region.getStyleClass().addAll(ss.getStyleClasses());
+                            }
+                        }
+                        ((StyleableProperty)graphicProperty()).applyStyle(origin, region);
+                    } else {
                         URL url = null;
                         try {
                             url = new URL(v);
                         } catch (MalformedURLException malf) {
-                            // This may be a relative URL, so try resolving
-                            // it using the application classloader
+                            // This may be a relative URL, so try resolving it using the application classloader
                             final ClassLoader cl = Thread.currentThread().getContextClassLoader();
                             url = cl.getResource(v);
                         }
                         if (url != null) {
-                            ((StyleableProperty)graphicProperty()).applyStyle(origin, new ImageView(new Image(url.toExternalForm())));                            
+                            ((StyleableProperty)graphicProperty()).applyStyle(origin, new ImageView(new Image(url.toExternalForm())));
                         }
-                    } else {
-                        ((StyleableProperty)graphicProperty()).applyStyle(origin, null);
-                    }                    
+                    }
                 }
 
                 @Override