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 javafx.scene.layout.BorderWidths;
  29 import javafx.scene.text.Font;
  30 import javafx.css.ParsedValue;
  31 import javafx.css.Size;
  32 import javafx.css.SizeUnits;
  33 import javafx.css.StyleConverter;
  34 
  35 /**
  36  * User: richardbair
  37  * Date: 8/10/12
  38  * Time: 8:07 PM
  39  */
  40 public class BorderImageWidthConverter extends StyleConverter<ParsedValue[], BorderWidths> {
  41     private static final BorderImageWidthConverter CONVERTER_INSTANCE = new BorderImageWidthConverter();
  42 
  43     public static BorderImageWidthConverter getInstance() {
  44         return CONVERTER_INSTANCE;
  45     }
  46 
  47     private BorderImageWidthConverter() { }
  48 
  49     @Override
  50     public BorderWidths convert(ParsedValue<ParsedValue[], BorderWidths> value, Font font) {
  51         ParsedValue[] sides = value.getValue();
  52         assert sides.length == 4;
  53 
  54         double top = 1, right = 1, bottom = 1, left = 1;
  55         boolean topPercent = false, rightPercent = false, bottomPercent = false, leftPercent = false;
  56         ParsedValue val = sides[0];
  57         if ("auto".equals(val.getValue())) {
  58             top = BorderWidths.AUTO;
  59         } else {
  60             Size size = (Size)val.convert(font);
  61             top = size.pixels(font);
  62             topPercent = size.getUnits() == SizeUnits.PERCENT;
  63         }
  64 
  65         val = sides[1];
  66         if ("auto".equals(val.getValue())) {
  67             right = BorderWidths.AUTO;
  68         } else {
  69             Size size = (Size)val.convert(font);
  70             right = size.pixels(font);
  71             rightPercent = size.getUnits() == SizeUnits.PERCENT;
  72         }
  73 
  74         val = sides[2];
  75         if ("auto".equals(val.getValue())) {
  76             bottom = BorderWidths.AUTO;
  77         } else {
  78             Size size = (Size)val.convert(font);
  79             bottom = size.pixels(font);
  80             bottomPercent = size.getUnits() == SizeUnits.PERCENT;
  81         }
  82 
  83         val = sides[3];
  84         if ("auto".equals(val.getValue())) {
  85             left = BorderWidths.AUTO;
  86         } else {
  87             Size size = (Size)val.convert(font);
  88             left = size.pixels(font);
  89             leftPercent = size.getUnits() == SizeUnits.PERCENT;
  90         }
  91 
  92         return new BorderWidths(top, right, bottom, left, topPercent, rightPercent, bottomPercent, leftPercent);
  93     }
  94 
  95     @Override
  96     public String toString() {
  97         return "BorderImageWidthConverter";
  98     }
  99 }