1 /*
   2  * Copyright (c) 2010, 2014, 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;
  27 
  28 import com.sun.javafx.css.converters.BooleanConverter;
  29 import com.sun.javafx.css.converters.ColorConverter;
  30 import com.sun.javafx.css.converters.DurationConverter;
  31 import com.sun.javafx.css.converters.EffectConverter;
  32 import com.sun.javafx.css.converters.EnumConverter;
  33 import com.sun.javafx.css.converters.FontConverter;
  34 import com.sun.javafx.css.converters.InsetsConverter;
  35 import com.sun.javafx.css.converters.PaintConverter;
  36 import com.sun.javafx.css.converters.SizeConverter;
  37 import com.sun.javafx.css.converters.StringConverter;
  38 import com.sun.javafx.css.converters.URLConverter;
  39 import javafx.geometry.Insets;
  40 import javafx.scene.effect.Effect;
  41 import javafx.scene.paint.Color;
  42 import javafx.scene.paint.Paint;
  43 import javafx.scene.text.Font;
  44 import javafx.util.Duration;
  45 
  46 /**
  47  * Converter converts {@code ParsedValue&tl;F,T>} from type F to type T. the
  48  * {@link CssMetaData} API requires a {@code StyleConverter} which is used
  49  * when computing a value for the {@see StyleableProperty}. There are
  50  * a number of predefined converters which are accessible by the static 
  51  * methods of this class.
  52  * @see ParsedValue
  53  * @see StyleableProperty
  54  * @since JavaFX 8.0
  55  */
  56 public class StyleConverter<F, T> {
  57 
  58     /**
  59      * Convert from the parsed CSS value to the target property type. 
  60      *
  61      * @param value        The {@link ParsedValue} to convert
  62      * @param font         The {@link Font} to use when converting a
  63      * <a href="http://www.w3.org/TR/css3-values/#relative-lengths">relative</a>
  64      * value.
  65      */
  66     @SuppressWarnings("unchecked")
  67     public T convert(ParsedValue<F,T> value, Font font) {
  68         // unchecked!
  69         return (T) value.getValue();
  70     }
  71 
  72     /** 
  73      * @return A {@code StyleConverter} that converts &quot;true&quot; or &quot;false&quot; to {@code Boolean}
  74      * @see Boolean#valueOf(java.lang.String) 
  75      */
  76     public static StyleConverter<String,Boolean> getBooleanConverter() {
  77         return BooleanConverter.getInstance();
  78     }
  79     
  80     /** 
  81      * @return A {@code StyleConverter} that converts a String 
  82      * representation of a duration to a {@link Duration}
  83      *
  84      * @since JavaFX 8u40
  85      */
  86     public static StyleConverter<?,Duration> getDurationConverter() {
  87         return DurationConverter.getInstance();
  88     }
  89 
  90     /**
  91      * @return A {@code StyleConverter} that converts a String
  92      * representation of a web color to a {@code Color}
  93      * @see Color#web(java.lang.String)
  94      */
  95     public static StyleConverter<String,Color> getColorConverter() {
  96         return ColorConverter.getInstance();
  97     }
  98 
  99     /**
 100      * @return A {@code StyleConverter} that converts a parsed representation
 101      * of an {@code Effect} to an {@code Effect}
 102      * @see Effect
 103      */
 104     public static StyleConverter<ParsedValue[], Effect> getEffectConverter() {
 105         return EffectConverter.getInstance();
 106     }
 107     
 108     /** 
 109      * @return A {@code StyleConverter} that converts a String representation
 110      * of an {@code Enum} to an {@code Enum}
 111      * @see Enum#valueOf(java.lang.Class, java.lang.String) 
 112      */
 113     public static <E extends Enum<E>> StyleConverter<String, ? extends Enum<?>> getEnumConverter(Class<E> enumClass) {        
 114         // TODO: reuse EnumConverter instances
 115         EnumConverter<E> converter;
 116         converter = new EnumConverter<>(enumClass);
 117         return converter;
 118     }    
 119 
 120     /** 
 121      * @return A {@code StyleConverter} that converts a parsed representation
 122      * of a {@code Font} to an {@code Font}. 
 123      * @see Font#font(java.lang.String, javafx.scene.text.FontWeight, javafx.scene.text.FontPosture, double)  
 124      */
 125     public static StyleConverter<ParsedValue[], Font> getFontConverter() {
 126         return FontConverter.getInstance();
 127     }
 128     
 129     /** 
 130      * @return A {@code StyleConverter} that converts a [&lt;length&gt; |
 131      * &lt;percentage&gt;]{1,4} to an {@code Insets}. 
 132      */
 133     public static StyleConverter<ParsedValue[], Insets> getInsetsConverter() {
 134         return InsetsConverter.getInstance();
 135     }
 136 
 137     /** 
 138      * @return A {@code StyleConverter} that converts a parsed representation
 139      * of a {@code Paint} to a {@code Paint}.
 140      */
 141     public static StyleConverter<ParsedValue<?, Paint>, Paint> getPaintConverter() {
 142         return PaintConverter.getInstance();
 143     }
 144     
 145     /** 
 146      * CSS length and number values are parsed into a Size object that is
 147      * converted to a Number before the value is applied. If the property is
 148      * a {@code Number} type other than Double, the 
 149      * {@link CssMetaData#set(javafx.scene.Node, java.lang.Object, javafx.css.Origin) set}
 150      * method of ({@code CssMetaData} can be over-ridden to convert the Number
 151      * to the correct type. For example, if the property is an {@code IntegerProperty}:
 152      * <code><pre>
 153      *     {@literal @}Override public void set(MyNode node, Number value, Origin origin) {
 154      *         if (value != null) {
 155      *             super.set(node, value.intValue(), origin);
 156      *         } else {
 157      *             super.set(node, value, origin);
 158      *         }
 159      *     }
 160      * </pre></code>
 161      * @return A {@code StyleConverter} that converts a parsed representation
 162      * of a CSS length or number value to a {@code Number} that is an instance
 163      * of {@code Double}. 
 164      */
 165     public static StyleConverter<?, Number> getSizeConverter() {
 166         return SizeConverter.getInstance();
 167     }
 168     
 169     /** 
 170      * A converter for quoted strings which may have embedded unicode characters.
 171      * @return A {@code StyleConverter} that converts a representation of a
 172      * CSS string value to a {@code String}.
 173      */
 174     public static StyleConverter<String,String> getStringConverter() {
 175         return StringConverter.getInstance();
 176     }
 177     
 178     /** 
 179      * A converter for URL strings.
 180      * @return A {@code StyleConverter} that converts a representation of a
 181      * CSS URL value to a {@code String}.
 182      */
 183     public static StyleConverter<ParsedValue[], String> getUrlConverter() {
 184         return URLConverter.getInstance();
 185     }
 186     
 187 
 188 }