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.css.ParsedValue;
  29 import com.sun.javafx.css.Size;
  30 import com.sun.javafx.css.SizeUnits;
  31 import com.sun.javafx.css.StyleConverterImpl;
  32 import com.sun.javafx.css.converters.BooleanConverter;
  33 import javafx.scene.layout.BackgroundSize;
  34 import javafx.scene.text.Font;
  35 
  36 /**
  37  */
  38 public final class BackgroundSizeConverter  extends StyleConverterImpl<ParsedValue[], BackgroundSize> {
  39     private static final BackgroundSizeConverter BACKGROUND_SIZE_CONVERTER =
  40             new BackgroundSizeConverter();
  41 
  42     public static BackgroundSizeConverter getInstance() {
  43         return BACKGROUND_SIZE_CONVERTER;
  44     }
  45 
  46     // Disallow instantiation
  47     private BackgroundSizeConverter() { }
  48 
  49     @Override
  50     public BackgroundSize convert(ParsedValue<ParsedValue[], BackgroundSize> value, Font font) {
  51         ParsedValue[] values = value.getValue();
  52 
  53         // A Size that is null represents that we are "auto" for that dimension
  54         final Size wSize = (values[0] != null)
  55                 ? ((ParsedValue<?, Size>) values[0]).convert(font) : null;
  56         final Size hSize = (values[1] != null)
  57                 ? ((ParsedValue<?, Size>) values[1]).convert(font) : null;
  58 
  59         boolean proportionalWidth = true;
  60         boolean proportionalHeight = true;
  61 
  62         if (wSize != null) {
  63             proportionalWidth = wSize.getUnits() == SizeUnits.PERCENT;
  64         }
  65         if (hSize != null) {
  66             // wSize will be null if wSize is AUTO
  67             proportionalHeight = hSize.getUnits() == SizeUnits.PERCENT;
  68         }
  69 
  70         double w = (wSize != null) ? wSize.pixels(font) : BackgroundSize.AUTO;
  71         double h = (hSize != null) ? hSize.pixels(font) : BackgroundSize.AUTO;
  72 
  73         boolean cover = (values[2] != null)
  74                 ? BooleanConverter.getInstance().convert(values[2], font) : false;
  75 
  76         boolean contain = (values[3] != null)
  77                 ? BooleanConverter.getInstance().convert(values[3], font) : false;
  78 
  79         return new BackgroundSize(w, h, proportionalWidth, proportionalHeight, contain, cover);
  80     }
  81 
  82     /**
  83      * @inheritDoc
  84      */
  85     @Override public String toString() {
  86         return "BackgroundSizeConverter";
  87     }
  88 }