1 /*
   2  * Copyright (c) 2010, 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 javafx.scene.shape;
  27 
  28 import com.sun.javafx.collections.TrackableObservableList;
  29 import com.sun.javafx.geom.BaseBounds;
  30 import com.sun.javafx.geom.Path2D;
  31 import com.sun.javafx.geom.transform.BaseTransform;
  32 import com.sun.javafx.scene.DirtyBits;
  33 import com.sun.javafx.scene.NodeHelper;
  34 import com.sun.javafx.scene.shape.PolylineHelper;
  35 import com.sun.javafx.scene.shape.ShapeHelper;
  36 import com.sun.javafx.sg.prism.NGNode;
  37 import com.sun.javafx.sg.prism.NGPolyline;
  38 import com.sun.javafx.sg.prism.NGShape;
  39 import javafx.collections.ListChangeListener.Change;
  40 import javafx.collections.ObservableList;
  41 import javafx.css.StyleableProperty;
  42 import javafx.scene.Node;
  43 import javafx.scene.paint.Color;
  44 import javafx.scene.paint.Paint;
  45 
  46 /**
  47  * Creates a polyline, defined by the array of the segment points. The Polyline
  48  * class is similar to the Polygon class, except that it is not automatically
  49  * closed.
  50  *
  51 <PRE>
  52 import javafx.scene.shape.*;
  53 
  54 Polyline polyline = new Polyline();
  55 polyline.getPoints().addAll(new Double[]{
  56     0.0, 0.0,
  57     20.0, 10.0,
  58     10.0, 20.0 });
  59 </PRE>
  60  * @since JavaFX 2.0
  61  */
  62 public  class Polyline extends Shape {
  63     static {
  64         PolylineHelper.setPolylineAccessor(new PolylineHelper.PolylineAccessor() {
  65             @Override
  66             public NGNode doCreatePeer(Node node) {
  67                 return ((Polyline) node).doCreatePeer();
  68             }
  69 
  70             @Override
  71             public void doUpdatePeer(Node node) {
  72                 ((Polyline) node).doUpdatePeer();
  73             }
  74 
  75             @Override
  76             public BaseBounds doComputeGeomBounds(Node node,
  77             BaseBounds bounds, BaseTransform tx) {
  78                 return ((Polyline) node).doComputeGeomBounds(bounds, tx);
  79             }
  80 
  81             @Override
  82             public Paint doCssGetFillInitialValue(Shape shape) {
  83                 return ((Polyline) shape).doCssGetFillInitialValue();
  84             }
  85 
  86             @Override
  87             public Paint doCssGetStrokeInitialValue(Shape shape) {
  88                 return ((Polyline) shape).doCssGetStrokeInitialValue();
  89             }
  90 
  91             @Override
  92             public com.sun.javafx.geom.Shape doConfigShape(Shape shape) {
  93                 return ((Polyline) shape).doConfigShape();
  94             }
  95         });
  96     }
  97 
  98     private final Path2D shape = new Path2D();
  99 
 100     {
 101         // To initialize the class helper at the begining each constructor of this class
 102         PolylineHelper.initHelper(this);
 103 
 104         // overriding default values for fill and stroke
 105         // Set through CSS property so that it appears to be a UA style rather
 106         // that a USER style so that fill and stroke can still be set from CSS.
 107         ((StyleableProperty)fillProperty()).applyStyle(null, null);
 108         ((StyleableProperty)strokeProperty()).applyStyle(null, Color.BLACK);
 109     }
 110 
 111     /**
 112      * Creates an empty instance of Polyline.
 113      */
 114     public Polyline() {
 115     }
 116 
 117     /**
 118      * Creates a new instance of Polyline.
 119      * @param points the coordinates of the polyline segments
 120      */
 121     public Polyline(double... points) {
 122         if (points != null) {
 123             for (double p : points) {
 124                 this.getPoints().add(p);
 125             }
 126         }
 127     }
 128 
 129     /**
 130      * Defines the coordinates of the polyline segments.
 131      *
 132      * @defaultValue empty
 133      */
 134     private final ObservableList<Double> points = new TrackableObservableList<Double>() {
 135         @Override
 136         protected void onChanged(Change<Double> c) {
 137             NodeHelper.markDirty(Polyline.this, DirtyBits.NODE_GEOMETRY);
 138             NodeHelper.geomChanged(Polyline.this);
 139         }
 140     };
 141 
 142     /**
 143      * Gets the coordinates of the {@code PolyLine} segments.
 144      * @return An observable list of points constituting segments of this
 145      * {@code PolyLine}
 146      */
 147     public final ObservableList<Double> getPoints() { return points; }
 148 
 149     /*
 150      * Note: This method MUST only be called via its accessor method.
 151      */
 152     private NGNode doCreatePeer() {
 153         return new NGPolyline();
 154     }
 155 
 156     /*
 157      * Note: This method MUST only be called via its accessor method.
 158      */
 159     private BaseBounds doComputeGeomBounds(BaseBounds bounds, BaseTransform tx) {
 160         if (getMode() == NGShape.Mode.EMPTY || getPoints().size() <= 1) {
 161             return bounds.makeEmpty();
 162         }
 163 
 164         if (getPoints().size() == 2) {
 165             if (getMode() == NGShape.Mode.FILL || getStrokeType() == StrokeType.INSIDE) {
 166                 return bounds.makeEmpty();
 167             }
 168             double upad = getStrokeWidth();
 169             if (getStrokeType() == StrokeType.CENTERED) {
 170                 upad /= 2.0f;
 171             }
 172             return computeBounds(bounds, tx, upad, 0.5f,
 173                 getPoints().get(0), getPoints().get(1), 0.0f, 0.0f);
 174         } else {
 175             return computeShapeBounds(bounds, tx, ShapeHelper.configShape(this));
 176         }
 177     }
 178 
 179     /*
 180      * Note: This method MUST only be called via its accessor method.
 181      */
 182     private Path2D doConfigShape() {
 183         double p1 = getPoints().get(0);
 184         double p2 = getPoints().get(1);
 185         shape.reset();
 186         shape.moveTo((float)p1, (float)p2);
 187         final int numValidPoints = getPoints().size() & ~1;
 188         for (int i = 2; i < numValidPoints; i += 2) {
 189             p1 = getPoints().get(i); p2 = getPoints().get(i+1);
 190             shape.lineTo((float)p1, (float)p2);
 191         }
 192         return shape;
 193     }
 194 
 195     /*
 196      * Note: This method MUST only be called via its accessor method.
 197      */
 198     private void doUpdatePeer() {
 199         if (NodeHelper.isDirty(this, DirtyBits.NODE_GEOMETRY)) {
 200             final int numValidPoints = getPoints().size() & ~1;
 201             float points_array[] = new float[numValidPoints];
 202             for (int i = 0; i < numValidPoints; i++) {
 203                 points_array[i] = (float)getPoints().get(i).doubleValue();
 204             }
 205             final NGPolyline peer = NodeHelper.getPeer(this);
 206             peer.updatePolyline(points_array);
 207         }
 208     }
 209 
 210     /***************************************************************************
 211      *                                                                         *
 212      *                         Stylesheet Handling                             *
 213      *                                                                         *
 214      **************************************************************************/
 215 
 216     /*
 217      * Some sub-class of Shape, such as {@link Line}, override the
 218      * default value for the {@link Shape#fill} property. This allows
 219      * CSS to get the correct initial value.
 220      *
 221      * Note: This method MUST only be called via its accessor method.
 222      */
 223     private Paint doCssGetFillInitialValue() {
 224         return null;
 225     }
 226 
 227     /*
 228      * Some sub-class of Shape, such as {@link Line}, override the
 229      * default value for the {@link Shape#stroke} property. This allows
 230      * CSS to get the correct initial value.
 231      *
 232      * Note: This method MUST only be called via its accessor method.
 233      */
 234     private Paint doCssGetStrokeInitialValue() {
 235         return Color.BLACK;
 236     }
 237 
 238     /**
 239      * Returns a string representation of this {@code Polyline} object.
 240      * @return a string representation of this {@code Polyline} object.
 241      */
 242     @Override
 243     public String toString() {
 244         final StringBuilder sb = new StringBuilder("Polyline[");
 245 
 246         String id = getId();
 247         if (id != null) {
 248             sb.append("id=").append(id).append(", ");
 249         }
 250 
 251         sb.append("points=").append(getPoints());
 252 
 253         sb.append(", fill=").append(getFill());
 254 
 255         Paint stroke = getStroke();
 256         if (stroke != null) {
 257             sb.append(", stroke=").append(stroke);
 258             sb.append(", strokeWidth=").append(getStrokeWidth());
 259         }
 260 
 261         return sb.append("]").toString();
 262     }
 263 }