1 /* 2 * Copyright (c) 2011, 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.geometry.Side; 29 import javafx.scene.layout.BackgroundPosition; 30 import javafx.scene.text.Font; 31 import javafx.css.ParsedValue; 32 import javafx.css.Size; 33 import javafx.css.SizeUnits; 34 import javafx.css.StyleConverter; 35 36 /** 37 * Given four Sizes from the Parser, this converter will produce a BackgroundPosition object. 38 */ 39 public final class BackgroundPositionConverter extends StyleConverter<ParsedValue[], BackgroundPosition> { 40 private static final BackgroundPositionConverter BACKGROUND_POSITION_CONVERTER = 41 new BackgroundPositionConverter(); 42 43 public static BackgroundPositionConverter getInstance() { 44 return BACKGROUND_POSITION_CONVERTER; 45 } 46 47 // Disallow instantiation 48 private BackgroundPositionConverter() { } 49 50 @Override 51 public BackgroundPosition convert(ParsedValue<ParsedValue[], BackgroundPosition> value, Font font) { 52 ParsedValue[] positions = value.getValue(); 53 54 // The parser gives us 4 values, none of them null 55 final Size top = (Size)positions[0].convert(font); 56 final Size right = (Size)positions[1].convert(font); 57 final Size bottom = (Size)positions[2].convert(font); 58 final Size left = (Size)positions[3].convert(font); 59 60 boolean verticalEdgeProportional = 61 (bottom.getValue() > 0 && bottom.getUnits() == SizeUnits.PERCENT) 62 || (top.getValue() > 0 && top.getUnits() == SizeUnits.PERCENT) 63 || (top.getValue() == 0 && bottom.getValue() == 0); 64 65 // either left or right will be set, not both 66 boolean horizontalEdgeProportional = 67 (right.getValue() > 0 && right.getUnits() == SizeUnits.PERCENT) 68 || ( left.getValue() > 0 && left.getUnits() == SizeUnits.PERCENT) 69 || (left.getValue() == 0 && right.getValue() == 0); 70 71 final double t = top.pixels(font); 72 final double r = right.pixels(font); 73 final double b = bottom.pixels(font); 74 final double l = left.pixels(font); 75 76 return new BackgroundPosition( 77 (l == 0 && r != 0) ? Side.RIGHT : Side.LEFT, 78 (l == 0 && r != 0) ? r : l, 79 horizontalEdgeProportional, 80 (t == 0 && b != 0) ? Side.BOTTOM : Side.TOP, 81 (t == 0 && b != 0) ? b : t, 82 verticalEdgeProportional); 83 } 84 85 /** 86 * @inheritDoc 87 */ 88 @Override public String toString() { 89 return "BackgroundPositionConverter"; 90 } 91 }