1 /*
   2  * Copyright (c) 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 com.sun.javafx.scene.shape;
  27 
  28 import com.sun.javafx.geom.BaseBounds;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 import com.sun.javafx.jmx.MXNodeAlgorithm;
  31 import com.sun.javafx.jmx.MXNodeAlgorithmContext;
  32 import com.sun.javafx.scene.DirtyBits;
  33 import com.sun.javafx.scene.NodeHelper;
  34 import com.sun.javafx.sg.prism.NGNode;
  35 import com.sun.javafx.sg.prism.NGShape;
  36 import com.sun.javafx.util.Utils;
  37 import javafx.scene.Node;
  38 import javafx.scene.paint.Paint;
  39 import javafx.scene.shape.Shape;
  40 
  41 /**
  42  * Used to access internal methods of Shape.
  43  */
  44 public abstract class ShapeHelper extends NodeHelper {
  45     private static ShapeAccessor shapeAccessor;
  46 
  47     static {
  48         Utils.forceInit(Shape.class);
  49     }
  50 
  51     /*
  52      * Static helper methods for cases where the implementation is done in an
  53      * instance method that is overridden by subclasses.
  54      * These methods exist in the base class only.
  55      */
  56 
  57     public static Paint cssGetFillInitialValue(Shape shape) {
  58         return ((ShapeHelper) getHelper(shape)).cssGetFillInitialValueImpl(shape);
  59     }
  60 
  61     public static Paint cssGetStrokeInitialValue(Shape shape) {
  62         return ((ShapeHelper) getHelper(shape)).cssGetStrokeInitialValueImpl(shape);
  63     }
  64 
  65     public static com.sun.javafx.geom.Shape configShape(Shape shape) {
  66         return ((ShapeHelper) getHelper(shape)).configShapeImpl(shape);
  67     }
  68 
  69     /*
  70      * Methods that will be overridden by subclasses
  71      */
  72 
  73     @Override
  74     protected void updatePeerImpl(Node node) {
  75         super.updatePeerImpl(node);
  76         shapeAccessor.doUpdatePeer(node);
  77     }
  78 
  79     @Override
  80     protected void markDirtyImpl(Node node, DirtyBits dirtyBit) {
  81         shapeAccessor.doMarkDirty(node, dirtyBit);
  82         super.markDirtyImpl(node, dirtyBit);
  83     }
  84 
  85     @Override
  86     protected BaseBounds computeGeomBoundsImpl(Node node, BaseBounds bounds,
  87             BaseTransform tx) {
  88         return shapeAccessor.doComputeGeomBounds(node, bounds, tx);
  89     }
  90 
  91     @Override
  92     protected boolean computeContainsImpl(Node node, double localX, double localY) {
  93         return shapeAccessor.doComputeContains(node, localX, localY);
  94     }
  95 
  96     @Override
  97     protected Object processMXNodeImpl(Node node, MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) {
  98         return shapeAccessor.doProcessMXNode(node, alg, ctx);
  99     }
 100 
 101     protected Paint cssGetFillInitialValueImpl(Shape shape) {
 102         return shapeAccessor.doCssGetFillInitialValue(shape);
 103     }
 104 
 105     protected Paint cssGetStrokeInitialValueImpl(Shape shape) {
 106         return shapeAccessor.doCssGetStrokeInitialValue(shape);
 107     }
 108 
 109     protected abstract com.sun.javafx.geom.Shape configShapeImpl(Shape shape);
 110 
 111     /*
 112      * Methods used by Shape (base) class only
 113      */
 114 
 115     public static NGShape.Mode getMode(Shape shape) {
 116         return shapeAccessor.getMode(shape);
 117     }
 118 
 119     public static void setMode(Shape shape, NGShape.Mode mode) {
 120         shapeAccessor.setMode(shape, mode);
 121     }
 122 
 123     public static void setShapeChangeListener(Shape shape, Runnable listener) {
 124         shapeAccessor.setShapeChangeListener(shape, listener);
 125     }
 126 
 127     public static void setShapeAccessor(final ShapeAccessor newAccessor) {
 128         if (shapeAccessor != null) {
 129             throw new IllegalStateException();
 130         }
 131 
 132         shapeAccessor = newAccessor;
 133     }
 134 
 135     public interface ShapeAccessor {
 136         void doUpdatePeer(Node node);
 137         void doMarkDirty(Node node, DirtyBits dirtyBit);
 138         BaseBounds doComputeGeomBounds(Node node, BaseBounds bounds, BaseTransform tx);
 139         boolean doComputeContains(Node node, double localX, double localY);
 140         Object doProcessMXNode(Node node, MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx);
 141         Paint doCssGetFillInitialValue(Shape shape);
 142         Paint doCssGetStrokeInitialValue(Shape shape);
 143         NGShape.Mode getMode(Shape shape);
 144         void setMode(Shape shape, NGShape.Mode mode);
 145         void setShapeChangeListener(Shape shape, Runnable listener);
 146     }
 147 
 148 }