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 javafx.scene.layout; 27 28 import com.sun.javafx.css.StyleConverterImpl; 29 import com.sun.javafx.css.StyleManager; 30 import com.sun.javafx.scene.layout.region.RepeatStruct; 31 import java.util.Map; 32 import javafx.css.CssMetaData; 33 import javafx.css.ParsedValue; 34 import javafx.css.StyleConverter; 35 import javafx.css.Styleable; 36 import javafx.geometry.Insets; 37 import javafx.scene.image.Image; 38 import javafx.scene.paint.Paint; 39 40 /** 41 * Converts the CSS for -fx-background items into a Background. 42 */ 43 class BackgroundConverter extends StyleConverterImpl<ParsedValue[], Background> { 44 45 static final StyleConverter<ParsedValue[], Background> INSTANCE = new BackgroundConverter(); 46 47 @Override public Background convert(Map<CssMetaData<? extends Styleable, ?>,Object> convertedValues) { 48 final Paint[] fills = (Paint[]) convertedValues.get(Background.BACKGROUND_COLOR); 49 final String[] imageUrls = (String[]) convertedValues.get(Background.BACKGROUND_IMAGE); 50 final boolean hasFills = fills != null && fills.length > 0; 51 final boolean hasImages = imageUrls != null && imageUrls.length > 0; 52 53 // If there are neither background fills nor images, then there is nothing for us to construct. 54 if (!hasFills && !hasImages) return null; 55 56 // Iterate over all of the fills, and create BackgroundFill objects for each. 57 BackgroundFill[] backgroundFills = null; 58 if (hasFills) { 59 backgroundFills = new BackgroundFill[fills.length]; 60 61 Object tmp = convertedValues.get(Background.BACKGROUND_INSETS); 62 final Insets[] insets = tmp == null ? new Insets[0] : (Insets[]) tmp; 63 64 tmp = convertedValues.get(Background.BACKGROUND_RADIUS); 65 final CornerRadii[] radii = tmp == null ? new CornerRadii[0] : (CornerRadii[]) tmp; 66 67 final int lastInsetsIndex = insets.length - 1; 68 final int lastRadiiIndex = radii.length - 1; 69 for (int i=0; i<fills.length; i++) { 70 Insets in = insets.length > 0 ? insets[i <= lastInsetsIndex ? i : lastInsetsIndex] : Insets.EMPTY; 71 CornerRadii ra = radii.length > 0 ? radii[i <= lastRadiiIndex ? i : lastRadiiIndex] : CornerRadii.EMPTY; 72 backgroundFills[i] = new BackgroundFill(fills[i], ra, in); 73 } 74 } 75 76 // Iterate over all of the image, and create BackgroundImage objects for each. 77 BackgroundImage[] backgroundImages = null; 78 if (hasImages) { 79 // TODO convert image urls into image objects! 80 backgroundImages = new BackgroundImage[imageUrls.length]; 81 82 Object tmp = convertedValues.get(Background.BACKGROUND_REPEAT); 83 final RepeatStruct[] repeats = tmp == null ? new RepeatStruct[0] : (RepeatStruct[]) tmp; 84 85 tmp = convertedValues.get(Background.BACKGROUND_POSITION); 86 final BackgroundPosition[] positions = tmp == null ? new BackgroundPosition[0] : (BackgroundPosition[]) tmp; 87 88 tmp = convertedValues.get(Background.BACKGROUND_SIZE); 89 final BackgroundSize[] sizes = tmp == null ? new BackgroundSize[0] : (BackgroundSize[]) tmp; 90 91 final int lastRepeatIndex = repeats.length - 1; 92 final int lastPositionIndex = positions.length - 1; 93 final int lastSizeIndex = sizes.length - 1; 94 for (int i = 0; i < imageUrls.length; i++) { 95 // RT-21335: skip background and border images whose image url is null 96 if (imageUrls[i] == null) continue; 97 98 final Image image = StyleManager.getInstance().getCachedImage(imageUrls[i]); 99 if (image == null) continue; 100 101 final RepeatStruct repeat = (repeats.length > 0) ? 102 repeats[i <= lastRepeatIndex ? i : lastRepeatIndex] : null; // min 103 final BackgroundPosition position = (positions.length > 0) ? 104 positions[i <= lastPositionIndex ? i : lastPositionIndex] : null; // min 105 final BackgroundSize size = (sizes.length > 0) ? 106 sizes[i <= lastSizeIndex ? i : lastSizeIndex] : null; // min 107 backgroundImages[i] = new BackgroundImage(image, 108 repeat == null ? null : repeat.repeatX, 109 repeat == null ? null : repeat.repeatY, 110 position, size); 111 } 112 } 113 114 // Give the background fills and background images to a newly constructed BackgroundConverter, 115 // and return it. 116 return new Background(backgroundFills, backgroundImages); 117 } 118 }