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