1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.css;
  27 
  28 import com.sun.javafx.css.converters.BooleanConverter;
  29 import com.sun.javafx.css.converters.SizeConverter;
  30 import com.sun.javafx.css.converters.StringConverter;
  31 import java.util.ArrayList;
  32 import java.util.Collections;
  33 import java.util.List;
  34 
  35 import com.sun.javafx.sg.prism.NGNode;
  36 import javafx.beans.property.BooleanProperty;
  37 import javafx.beans.property.DoubleProperty;
  38 import javafx.beans.property.StringProperty;
  39 import javafx.css.CssMetaData;
  40 import javafx.css.StyleableBooleanProperty;
  41 import javafx.css.StyleableDoubleProperty;
  42 import javafx.css.StyleableStringProperty;
  43 import javafx.scene.Node;
  44 
  45 import com.sun.javafx.geom.BaseBounds;
  46 import com.sun.javafx.geom.transform.BaseTransform;
  47 import com.sun.javafx.jmx.MXNodeAlgorithm;
  48 import com.sun.javafx.jmx.MXNodeAlgorithmContext;
  49 import javafx.css.Styleable;
  50 import javafx.css.StyleableProperty;
  51 
  52 /** Test Node with styleable properties and an getClassCssMetaData method */
  53 class TestNodeBase extends Node {
  54 
  55     protected TestNodeBase() {
  56     }
  57 
  58     @Override
  59     protected boolean impl_computeContains(double d, double d1) {
  60         throw new UnsupportedOperationException("Not supported yet.");
  61     }
  62 
  63     @Override
  64     public BaseBounds impl_computeGeomBounds(BaseBounds bb, BaseTransform bt) {
  65         throw new UnsupportedOperationException("Not supported yet.");
  66     }
  67 
  68     @Override
  69     protected NGNode impl_createPeer() {
  70         throw new UnsupportedOperationException("Not supported yet.");
  71     }
  72 
  73     private BooleanProperty test;
  74     private BooleanProperty testProperty() {
  75         if (test == null) {
  76             test = new StyleableBooleanProperty(true) {
  77 
  78                 @Override
  79                 public Object getBean() {
  80                     return TestNodeBase.this;
  81                 }
  82 
  83                 @Override
  84                 public String getName() {
  85                     return "test";
  86                 }
  87 
  88                 @Override
  89                 public CssMetaData getCssMetaData() {
  90                     return TestNodeBase.StyleableProperties.TEST;
  91                 }
  92                 
  93             };
  94         }
  95         return test;
  96     }
  97     
  98     public void setTest(boolean value) {
  99         testProperty().set(value);
 100     }
 101     
 102     public boolean getTest() {
 103         return (test == null ? true : test.get());
 104     }
 105     
 106     private StringProperty string;
 107     private StringProperty stringProperty() {
 108         if (string == null) {
 109             string = new StyleableStringProperty("init string") {
 110 
 111                 @Override
 112                 public Object getBean() {
 113                     return TestNodeBase.this;
 114                 }
 115 
 116                 @Override
 117                 public String getName() {
 118                     return "string";
 119                 }
 120 
 121                 @Override
 122                 public CssMetaData getCssMetaData() {
 123                     return TestNodeBase.StyleableProperties.STRING;
 124                 }
 125                 
 126             };
 127         }
 128         return string;
 129     }
 130     
 131     public void setString(String value) {
 132         stringProperty().set(value);
 133     }
 134     
 135     public String getString() {
 136         return (string == null ? "init string" : string.get());
 137     }  
 138     
 139     private DoubleProperty doubleProperty;
 140     private DoubleProperty doublePropertyProperty() {
 141         if (doubleProperty == null) {
 142             doubleProperty = new StyleableDoubleProperty(0) {
 143 
 144                 @Override
 145                 public Object getBean() {
 146                     return TestNodeBase.this;
 147                 }
 148 
 149                 @Override
 150                 public String getName() {
 151                     return "doubleProperty";
 152                 }
 153 
 154                 @Override
 155                 public CssMetaData getCssMetaData() {
 156                     return TestNodeBase.StyleableProperties.DOUBLE_PROPERTY;
 157                 }
 158                 
 159             };
 160         }
 161         return doubleProperty;
 162     }
 163     
 164     public void setDoubleProperty(double number) {
 165         doublePropertyProperty().set(number);
 166     }
 167     
 168     public double getDoubleProperty() {
 169         return (doubleProperty == null ? 0 : doubleProperty.get());
 170     }
 171     
 172 
 173     static class StyleableProperties {
 174         public final static CssMetaData<TestNodeBase,Boolean> TEST =
 175                 new CssMetaData<TestNodeBase,Boolean>("-fx-test", 
 176                 BooleanConverter.getInstance(), Boolean.TRUE) {
 177 
 178             @Override
 179             public boolean isSettable(TestNodeBase n) {
 180                 return n.test == null || !n.test.isBound();
 181             }
 182 
 183             @Override
 184             public StyleableProperty<Boolean> getStyleableProperty(TestNodeBase n) {
 185                 return (StyleableProperty)n.testProperty();
 186             }
 187         };
 188 
 189         public final static CssMetaData<TestNodeBase,String> STRING =
 190                 new CssMetaData<TestNodeBase,String>("-fx-string", 
 191                 StringConverter.getInstance(), "init string") {
 192 
 193             @Override
 194             public boolean isSettable(TestNodeBase n) {
 195                 return n.string == null || !n.string.isBound();
 196             }
 197 
 198             @Override
 199             public StyleableProperty<String> getStyleableProperty(TestNodeBase n) {
 200                 return (StyleableProperty)n.stringProperty();
 201             }
 202         };
 203 
 204         public final static CssMetaData<TestNodeBase,Number> DOUBLE_PROPERTY =
 205                 new CssMetaData<TestNodeBase,Number>("-fx-double-property", 
 206                 SizeConverter.getInstance(), 0) {
 207 
 208             @Override
 209             public boolean isSettable(TestNodeBase n) {
 210                 return n.doubleProperty == null || !n.doubleProperty.isBound();
 211             }
 212 
 213             @Override
 214             public StyleableProperty<Number> getStyleableProperty(TestNodeBase n) {
 215                 return (StyleableProperty)n.doublePropertyProperty();
 216             }
 217         };
 218         
 219         static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
 220         static {
 221             List<CssMetaData<? extends Styleable, ?>> list = 
 222                 new ArrayList<CssMetaData<? extends Styleable, ?>>(Node.getClassCssMetaData());
 223             Collections.addAll(list,                
 224                 TEST,
 225                 STRING,
 226                 DOUBLE_PROPERTY
 227             );
 228             STYLEABLES = Collections.unmodifiableList(list);
 229         }
 230     }
 231                 
 232     /**
 233      * {@inheritDoc}
 234      */
 235     public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
 236         return StyleableProperties.STYLEABLES;
 237     }
 238 
 239     /**
 240      * {@inheritDoc}
 241      *
 242      */
 243     
 244     
 245     @Override
 246     public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
 247         return getClassCssMetaData();
 248     }
 249 
 250     @Override
 251     public Object impl_processMXNode(MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) {
 252         return alg.processLeafNode(this, ctx);
 253     }
 254 }