1 /*
   2  * Copyright (c) 2012, 2013, 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.layout.region;
  27 
  28 import java.util.ArrayList;
  29 import java.util.Collections;
  30 import java.util.List;
  31 import javafx.scene.layout.BorderStrokeStyle;
  32 import javafx.scene.shape.StrokeLineCap;
  33 import javafx.scene.shape.StrokeLineJoin;
  34 import javafx.scene.shape.StrokeType;
  35 import javafx.scene.text.Font;
  36 import javafx.css.ParsedValue;
  37 import com.sun.javafx.css.ParsedValueImpl;
  38 import com.sun.javafx.css.Size;
  39 import com.sun.javafx.css.StyleConverterImpl;
  40 
  41 /**
  42  */
  43 public class BorderStyleConverter  extends StyleConverterImpl<ParsedValue[], BorderStrokeStyle> {
  44 //    private static final ParsedValue<ParsedValue<?,Size>[],Double[]> DASHED =
  45 //            new ParsedValue<ParsedValue<?,Size>[],Double[]>(
  46 //                    new ParsedValue[] {
  47 //                            new ParsedValue<Size,Size>(new Size(5.0f, SizeUnits.PX), null),
  48 //                            new ParsedValue<Size,Size>(new Size(3.0f, SizeUnits.PX), null)
  49 //                    }, SizeConverter.SequenceConverter.getInstance());
  50 //
  51 //    private static final ParsedValue<ParsedValue<?,Size>[],Double[]> DOTTED =
  52 //            new ParsedValue<ParsedValue<?,Size>[],Double[]>(
  53 //                    new ParsedValue[]{
  54 //                            new ParsedValue<Size,Size>(new Size(1.0f, SizeUnits.PX), null),
  55 //                            new ParsedValue<Size,Size>(new Size(3.0f, SizeUnits.PX), null)
  56 //                    }, SizeConverter.SequenceConverter.getInstance());
  57 //
  58 //    private static final ParsedValue<ParsedValue<?,Size>[],Double[]> SOLID =
  59 //            new ParsedValue<ParsedValue<?,Size>[],Double[]>(
  60 //                    new ParsedValue[]{
  61 //                            /* empty array */
  62 //                    }, SizeConverter.SequenceConverter.getInstance());
  63 
  64 
  65     public static final ParsedValueImpl<ParsedValue[],Number[]> NONE = new ParsedValueImpl<ParsedValue[],Number[]>(null, null);
  66     public static final ParsedValueImpl<ParsedValue[],Number[]> HIDDEN = new ParsedValueImpl<ParsedValue[],Number[]>(null, null);
  67     public static final ParsedValueImpl<ParsedValue[],Number[]> DOTTED = new ParsedValueImpl<ParsedValue[],Number[]>(null, null);
  68     public static final ParsedValueImpl<ParsedValue[],Number[]> DASHED = new ParsedValueImpl<ParsedValue[],Number[]>(null, null);
  69     public static final ParsedValueImpl<ParsedValue[],Number[]> SOLID = new ParsedValueImpl<ParsedValue[],Number[]>(null, null);
  70 
  71     /**
  72      * Convert a sequence of values to a BorderStyle.
  73      */
  74     private static final BorderStyleConverter BORDER_STYLE_CONVERTER =
  75             new BorderStyleConverter();
  76 
  77     public static BorderStyleConverter getInstance() {
  78         return BORDER_STYLE_CONVERTER;
  79     }
  80 
  81     // Prevent instantiation
  82     private BorderStyleConverter() { }
  83 
  84     @Override
  85     public BorderStrokeStyle convert(ParsedValue<ParsedValue[],BorderStrokeStyle> value, Font font) {
  86 
  87         final ParsedValue[] values = value.getValue();
  88 
  89         // The first value may be some named style, such as DOTTED, DASHED, SOLID, or NONE.
  90         // However even if named, there might be additional style information such as the
  91         // round cap to use, etc. But most of the time, people will only define the name and
  92         // nothing more. So we special case this so that if you use "solid" in CSS and that
  93         // is all, then we map it to BorderStrokeStyle.SOLID and quite early.
  94         Object v = values[0];
  95         final boolean onlyNamed = values[1] == null &&
  96                 values[2] == null &&
  97                 values[3] == null &&
  98                 values[4] == null &&
  99                 values[5] == null;
 100 
 101         if (NONE == v) return BorderStrokeStyle.NONE;
 102         if (DOTTED == v && onlyNamed) {
 103             return BorderStrokeStyle.DOTTED;
 104         } else if (DASHED == v && onlyNamed) {
 105             return BorderStrokeStyle.DASHED;
 106         } else if (SOLID == v && onlyNamed) {
 107             return BorderStrokeStyle.SOLID;
 108         }
 109 
 110         // We have some custom specified value
 111         ParsedValue<?,Size>[] dash_vals =
 112                 ((ParsedValue<ParsedValue<?,Size>[],Number[]>)values[0]).getValue();
 113 
 114         final List<Double> dashes;
 115         if (dash_vals == null) {
 116             if (DOTTED == v) {
 117                 dashes = BorderStrokeStyle.DOTTED.getDashArray();
 118             } else if (DASHED == v) {
 119                 dashes = BorderStrokeStyle.DASHED.getDashArray();
 120             } else if (SOLID == v) {
 121                 dashes = BorderStrokeStyle.SOLID.getDashArray();
 122             } else {
 123                 dashes = Collections.emptyList();
 124             }
 125         } else {
 126             dashes = new ArrayList<Double>(dash_vals.length);
 127             for(int dash=0; dash<dash_vals.length; dash++) {
 128                 final Size size = dash_vals[dash].convert(font);
 129                 dashes.add(size.pixels(font));
 130             }
 131         }
 132 
 133         final double dash_phase =
 134                 (values[1] != null) ? (Double)values[1].convert(font) : 0;
 135 
 136         final StrokeType stroke_type =
 137                 (values[2] != null) ? (StrokeType)values[2].convert(font) : StrokeType.INSIDE;
 138 
 139         final StrokeLineJoin line_join =
 140                 (values[3] != null) ? (StrokeLineJoin)values[3].convert(font) : StrokeLineJoin.MITER;
 141 
 142         final double miter_limit =
 143                 (values[4] != null) ? (Double)values[4].convert(font) : 10;
 144 
 145         final StrokeLineCap line_cap =
 146                 (values[5] != null) ? (StrokeLineCap)values[5].convert(font) : DOTTED == v ? StrokeLineCap.ROUND : StrokeLineCap.BUTT;
 147 
 148         final BorderStrokeStyle borderStyle = new BorderStrokeStyle(stroke_type, line_join, line_cap,
 149                 miter_limit, dash_phase, dashes);
 150 
 151         if (BorderStrokeStyle.SOLID.equals(borderStyle)) {
 152             return BorderStrokeStyle.SOLID;
 153         } else {
 154             return borderStyle;
 155         }
 156     }
 157 
 158     /**
 159      * @inheritDoc
 160      */
 161     @Override public String toString() {
 162         return "BorderStyleConverter";
 163     }
 164 
 165 }