1 /*
   2  * Copyright (c) 2011, 2016, 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 test.com.sun.javafx.css;
  27 
  28 import javafx.css.converter.BooleanConverter;
  29 import javafx.css.converter.SizeConverter;
  30 import javafx.css.converter.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 public class TestNodeBase extends Node {
  54     static {
  55          // This is used by classes in different packages to get access to
  56          // private and package private methods.
  57         TestNodeBaseHelper.setTestNodeBaseAccessor(new TestNodeBaseHelper.TestNodeBaseAccessor() {
  58             @Override
  59             public NGNode doCreatePeer(Node node) {
  60                 return ((TestNodeBase) node).doCreatePeer();
  61             }
  62         });
  63     }
  64 
  65     protected TestNodeBase() {
  66         TestNodeBaseHelper.initHelper(this);
  67     }
  68 
  69     @Override
  70     protected boolean impl_computeContains(double d, double d1) {
  71         throw new UnsupportedOperationException("Not supported yet.");
  72     }
  73 
  74     @Override
  75     public BaseBounds impl_computeGeomBounds(BaseBounds bb, BaseTransform bt) {
  76         throw new UnsupportedOperationException("Not supported yet.");
  77     }
  78 
  79     private NGNode doCreatePeer() {
  80         throw new UnsupportedOperationException("Not supported yet.");
  81     }
  82 
  83     private BooleanProperty test;
  84     private BooleanProperty testProperty() {
  85         if (test == null) {
  86             test = new StyleableBooleanProperty(true) {
  87 
  88                 @Override
  89                 public Object getBean() {
  90                     return TestNodeBase.this;
  91                 }
  92 
  93                 @Override
  94                 public String getName() {
  95                     return "test";
  96                 }
  97 
  98                 @Override
  99                 public CssMetaData getCssMetaData() {
 100                     return TestNodeBase.StyleableProperties.TEST;
 101                 }
 102 
 103             };
 104         }
 105         return test;
 106     }
 107 
 108     public void setTest(boolean value) {
 109         testProperty().set(value);
 110     }
 111 
 112     public boolean getTest() {
 113         return (test == null ? true : test.get());
 114     }
 115 
 116     private StringProperty string;
 117     private StringProperty stringProperty() {
 118         if (string == null) {
 119             string = new StyleableStringProperty("init string") {
 120 
 121                 @Override
 122                 public Object getBean() {
 123                     return TestNodeBase.this;
 124                 }
 125 
 126                 @Override
 127                 public String getName() {
 128                     return "string";
 129                 }
 130 
 131                 @Override
 132                 public CssMetaData getCssMetaData() {
 133                     return TestNodeBase.StyleableProperties.STRING;
 134                 }
 135 
 136             };
 137         }
 138         return string;
 139     }
 140 
 141     public void setString(String value) {
 142         stringProperty().set(value);
 143     }
 144 
 145     public String getString() {
 146         return (string == null ? "init string" : string.get());
 147     }
 148 
 149     private DoubleProperty doubleProperty;
 150     private DoubleProperty doublePropertyProperty() {
 151         if (doubleProperty == null) {
 152             doubleProperty = new StyleableDoubleProperty(0) {
 153 
 154                 @Override
 155                 public Object getBean() {
 156                     return TestNodeBase.this;
 157                 }
 158 
 159                 @Override
 160                 public String getName() {
 161                     return "doubleProperty";
 162                 }
 163 
 164                 @Override
 165                 public CssMetaData getCssMetaData() {
 166                     return TestNodeBase.StyleableProperties.DOUBLE_PROPERTY;
 167                 }
 168 
 169             };
 170         }
 171         return doubleProperty;
 172     }
 173 
 174     public void setDoubleProperty(double number) {
 175         doublePropertyProperty().set(number);
 176     }
 177 
 178     public double getDoubleProperty() {
 179         return (doubleProperty == null ? 0 : doubleProperty.get());
 180     }
 181 
 182 
 183     public static class StyleableProperties {
 184         public final static CssMetaData<TestNodeBase,Boolean> TEST =
 185                 new CssMetaData<TestNodeBase,Boolean>("-fx-test",
 186                 BooleanConverter.getInstance(), Boolean.TRUE) {
 187 
 188             @Override
 189             public boolean isSettable(TestNodeBase n) {
 190                 return n.test == null || !n.test.isBound();
 191             }
 192 
 193             @Override
 194             public StyleableProperty<Boolean> getStyleableProperty(TestNodeBase n) {
 195                 return (StyleableProperty)n.testProperty();
 196             }
 197         };
 198 
 199         public final static CssMetaData<TestNodeBase,String> STRING =
 200                 new CssMetaData<TestNodeBase,String>("-fx-string",
 201                 StringConverter.getInstance(), "init string") {
 202 
 203             @Override
 204             public boolean isSettable(TestNodeBase n) {
 205                 return n.string == null || !n.string.isBound();
 206             }
 207 
 208             @Override
 209             public StyleableProperty<String> getStyleableProperty(TestNodeBase n) {
 210                 return (StyleableProperty)n.stringProperty();
 211             }
 212         };
 213 
 214         public final static CssMetaData<TestNodeBase,Number> DOUBLE_PROPERTY =
 215                 new CssMetaData<TestNodeBase,Number>("-fx-double-property",
 216                 SizeConverter.getInstance(), 0) {
 217 
 218             @Override
 219             public boolean isSettable(TestNodeBase n) {
 220                 return n.doubleProperty == null || !n.doubleProperty.isBound();
 221             }
 222 
 223             @Override
 224             public StyleableProperty<Number> getStyleableProperty(TestNodeBase n) {
 225                 return (StyleableProperty)n.doublePropertyProperty();
 226             }
 227         };
 228 
 229         static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
 230         static {
 231             List<CssMetaData<? extends Styleable, ?>> list =
 232                 new ArrayList<CssMetaData<? extends Styleable, ?>>(Node.getClassCssMetaData());
 233             Collections.addAll(list,
 234                 TEST,
 235                 STRING,
 236                 DOUBLE_PROPERTY
 237             );
 238             STYLEABLES = Collections.unmodifiableList(list);
 239         }
 240     }
 241 
 242     /**
 243      * {@inheritDoc}
 244      */
 245     public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
 246         return StyleableProperties.STYLEABLES;
 247     }
 248 
 249     /**
 250      * {@inheritDoc}
 251      *
 252      */
 253 
 254 
 255     @Override
 256     public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
 257         return getClassCssMetaData();
 258     }
 259 
 260     @Override
 261     public Object impl_processMXNode(MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) {
 262         return alg.processLeafNode(this, ctx);
 263     }
 264 }