1 /*
   2  * Copyright (c) 2011, 2015, 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.css.converter;
  27 
  28 import javafx.css.Size;
  29 import javafx.css.ParsedValue;
  30 import javafx.css.StyleConverter;
  31 import javafx.geometry.Insets;
  32 import javafx.scene.text.Font;
  33 
  34 /**
  35  * Convert <size>{1,4} to Insets. The size values are interpreted as
  36  * top, right, bottom, left.
  37  * If only top is given, that value is used on all sides.
  38  * If there is only top and right, then bottom is set to top and left is set to right.
  39  * If top, right and bottom are given, then left is set to right.
  40  *
  41  * @since 9
  42  */
  43 public final class InsetsConverter extends StyleConverter<ParsedValue[], Insets> {
  44 
  45     // lazy, thread-safe instatiation
  46     private static class Holder {
  47         static final InsetsConverter INSTANCE = new InsetsConverter();
  48         static final SequenceConverter SEQUENCE_INSTANCE = new SequenceConverter();
  49     }
  50 
  51     public static StyleConverter<ParsedValue[], Insets> getInstance() {
  52         return Holder.INSTANCE;
  53     }
  54 
  55     private InsetsConverter() {
  56         super();
  57     }
  58 
  59     @Override
  60     public Insets convert(ParsedValue<ParsedValue[], Insets> value, Font font) {
  61         ParsedValue[] sides = value.getValue();
  62         double top = ((Size)sides[0].convert(font)).pixels(font);
  63         double right = (sides.length > 1) ? ((Size)sides[1].convert(font)).pixels(font) : top;
  64         double bottom = (sides.length > 2) ? ((Size)sides[2].convert(font)).pixels(font) : top;
  65         double left = (sides.length > 3) ? ((Size)sides[3].convert(font)).pixels(font) : right;
  66         return new Insets(top, right, bottom, left);
  67     }
  68 
  69     @Override
  70     public String toString() {
  71         return "InsetsConverter";
  72     }
  73 
  74     /**
  75      * Convert a <size>{1,4} [,<size>{1,4}]*  an Insets[]
  76      */
  77     public static final class SequenceConverter extends StyleConverter<ParsedValue<ParsedValue[], Insets>[], Insets[]> {
  78 
  79         public static SequenceConverter getInstance() {
  80             return Holder.SEQUENCE_INSTANCE;
  81         }
  82 
  83         private SequenceConverter() {
  84             super();
  85         }
  86 
  87         @Override
  88         public Insets[] convert(ParsedValue<ParsedValue<ParsedValue[], Insets>[], Insets[]> value, Font font) {
  89             ParsedValue<ParsedValue[], Insets>[] layers = value.getValue();
  90             Insets[] insets = new Insets[layers.length];
  91             for (int layer = 0; layer < layers.length; layer++) {
  92                 insets[layer] = InsetsConverter.getInstance().convert(layers[layer], font);
  93             }
  94             return insets;
  95         }
  96 
  97         @Override
  98         public String toString() {
  99             return "InsetsSequenceConverter";
 100         }
 101     }
 102 }
 103