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