1 /*
   2  * Copyright (c) 2011, 2015, 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.javafx.scene.bounds;
  27 
  28 import com.sun.javafx.geom.BaseBounds;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 import com.sun.javafx.sg.prism.NGNode;
  31 import com.sun.javafx.sg.prism.NGRectangle;
  32 import javafx.beans.property.FloatProperty;
  33 import javafx.beans.property.SimpleFloatProperty;
  34 import javafx.scene.Node;
  35 
  36 import com.sun.javafx.jmx.MXNodeAlgorithm;
  37 import com.sun.javafx.jmx.MXNodeAlgorithmContext;
  38 import com.sun.javafx.scene.NodeHelper;
  39 import test.com.sun.javafx.scene.bounds.PerfNodeHelper;
  40 
  41 /**
  42  * A special node used for performance tests to make sure that the minimum
  43  * amount of bounds computation work happens as possible.
  44  */
  45 public class PerfNode extends Node {
  46     static {
  47          // This is used by classes in different packages to get access to
  48          // private and package private methods.
  49         PerfNodeHelper.setPerfNodeAccessor(new PerfNodeHelper.PerfNodeAccessor() {
  50             @Override
  51             public NGNode doCreatePeer(Node node) {
  52                 return ((PerfNode) node).doCreatePeer();
  53             }
  54 
  55             @Override
  56             public BaseBounds doComputeGeomBounds(Node node,
  57             BaseBounds bounds, BaseTransform tx) {
  58                 return ((PerfNode) node).doComputeGeomBounds(bounds, tx);
  59             }
  60 
  61             @Override
  62             public boolean doComputeContains(Node node, double localX, double localY) {
  63                 return ((PerfNode) node).doComputeContains(localX, localY);
  64             }
  65 
  66             @Override
  67             public Object doProcessMXNode(Node node, MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) {
  68                 return ((PerfNode) node).doProcessMXNode(alg, ctx);
  69             }
  70         });
  71     }
  72 
  73     {
  74         // To initialize the class helper at the begining each constructor of this class
  75         PerfNodeHelper.initHelper(this);
  76     }
  77     public PerfNode() {
  78     }
  79 
  80     public PerfNode(float x, float y, float width, float height) {
  81         setX(x);
  82         setY(y);
  83         setWidth(width);
  84         setHeight(height);
  85     }
  86     private FloatProperty x;
  87 
  88     public final void setX(float value) {
  89         xProperty().set(value);
  90     }
  91 
  92     public final float getX() {
  93         return x == null ? 0 : x.get();
  94     }
  95 
  96     public FloatProperty xProperty() {
  97         if (x == null) {
  98             x = new SimpleFloatProperty() {
  99 
 100                 @Override
 101                 protected void invalidated() {
 102                     NodeHelper.geomChanged(PerfNode.this);
 103                 }
 104             };
 105         }
 106         return x;
 107     }
 108     private FloatProperty y;
 109 
 110     public final void setY(float value) {
 111         yProperty().set(value);
 112     }
 113 
 114     public final float getY() {
 115         return y == null ? 0 : y.get();
 116     }
 117 
 118     public FloatProperty yProperty() {
 119         if (y == null) {
 120             y = new SimpleFloatProperty() {
 121 
 122                 @Override
 123                 protected void invalidated() {
 124                     NodeHelper.geomChanged(PerfNode.this);
 125                 }
 126             };
 127         }
 128         return y;
 129     }
 130     private FloatProperty width;
 131 
 132     public final void setWidth(float value) {
 133         widthProperty().set(value);
 134     }
 135 
 136     public final float getWidth() {
 137         return width == null ? 100 : width.get();
 138     }
 139 
 140     public FloatProperty widthProperty() {
 141         if (width == null) {
 142             width = new SimpleFloatProperty() {
 143 
 144                 @Override
 145                 protected void invalidated() {
 146                     impl_storeWidth(width, get());
 147                 }
 148             };
 149         }
 150         return width;
 151     }
 152 
 153     protected void impl_storeWidth(FloatProperty model, float value) {
 154         NodeHelper.geomChanged(this);
 155     }
 156 
 157     private FloatProperty height;
 158 
 159     public final void setHeight(float value) {
 160         heightProperty().set(value);
 161     }
 162 
 163     public final float getHeight() {
 164         return height == null ? 100 : height.get();
 165     }
 166 
 167     public FloatProperty heightProperty() {
 168         if (height == null) {
 169             height = new SimpleFloatProperty() {
 170 
 171                 @Override
 172                 protected void invalidated() {
 173                     impl_storeHeight(height, get());
 174                 }
 175             };
 176         }
 177         return height;
 178     }
 179 
 180     protected void impl_storeHeight(FloatProperty model, float value) {
 181         NodeHelper.geomChanged(this);
 182     }
 183 
 184     int geomComputeCount = 0;
 185 
 186     /*
 187      * Note: This method MUST only be called via its accessor method.
 188      */
 189     private BaseBounds doComputeGeomBounds(com.sun.javafx.geom.BaseBounds bounds, com.sun.javafx.geom.transform.BaseTransform tx) {
 190         geomComputeCount++;
 191         bounds = bounds.deriveWithNewBounds(0, 0, 0, 100, 100, 0);
 192         return bounds;
 193     }
 194 
 195     /*
 196      * Note: This method MUST only be called via its accessor method.
 197      */
 198     private boolean doComputeContains(double localX, double localY) {
 199         // Stub
 200         return false;
 201     }
 202 
 203     private NGNode doCreatePeer() {
 204         return new NGRectangle();
 205     }
 206 
 207     /*
 208      * Note: This method MUST only be called via its accessor method.
 209      */
 210     private Object doProcessMXNode(MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) {
 211         return null;
 212     }
 213 }