Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.css;
027
028import com.sun.javafx.css.converters.BooleanConverter;
029import com.sun.javafx.css.converters.ColorConverter;
030import com.sun.javafx.css.converters.EffectConverter;
031import com.sun.javafx.css.converters.EnumConverter;
032import com.sun.javafx.css.converters.FontConverter;
033import com.sun.javafx.css.converters.InsetsConverter;
034import com.sun.javafx.css.converters.PaintConverter;
035import com.sun.javafx.css.converters.SizeConverter;
036import com.sun.javafx.css.converters.StringConverter;
037import com.sun.javafx.css.converters.URLConverter;
038import javafx.geometry.Insets;
039import javafx.scene.effect.Effect;
040import javafx.scene.paint.Color;
041import javafx.scene.paint.Paint;
042import javafx.scene.text.Font;
043
044/**
045 * Converter converts {@code ParsedValue<F,T>} from type F to type T. the
046 * {@link CssMetaData} API requires a {@code StyleConverter} which is used
047 * when computing a value for the {@see StyleableProperty}. There are
048 * a number of predefined converters which are accessible by the static 
049 * methods of this class.
050 * @see ParsedValue
051 * @see StyleableProperty
052 */
053public class StyleConverter<F, T> {
054
055    /**
056     * Convert from the parsed CSS value to the target property type. 
057     *
058     * @param value        The {@link ParsedValue} to convert
059     * @param font         The {@link Font} to use when converting a
060     * <a href="http://www.w3.org/TR/css3-values/#relative-lengths">relative</a>
061     * value.
062     */
063    @SuppressWarnings("unchecked")
064    public T convert(ParsedValue<F,T> value, Font font) {
065        // unchecked!
066        return (T) value.getValue();
067    }
068
069    /** 
070     * @return A {@code StyleConverter} that converts &quot;true&quot; or &quot;false&quot; to {@code Boolean}
071     * @see Boolean#valueOf(java.lang.String) 
072     */
073    public static StyleConverter<String,Boolean> getBooleanConverter() {
074        return BooleanConverter.getInstance();
075    }
076    
077    /** 
078     * @return A {@code StyleConverter} that converts a String 
079     * representation of a web color to a {@code Color}
080     * @see Color#web(java.lang.String) 
081     */
082    public static StyleConverter<String,Color> getColorConverter() {
083        return ColorConverter.getInstance();
084    }
085    
086    /** 
087     * @return A {@code StyleConverter} that converts a parsed representation
088     * of an {@code Effect} to an {@code Effect}
089     * @see Effect
090     */
091    public static StyleConverter<ParsedValue[], Effect> getEffectConverter() {
092        return EffectConverter.getInstance();
093    }
094    
095    /** 
096     * @return A {@code StyleConverter} that converts a String representation
097     * of an {@code Enum} to an {@code Enum}
098     * @see Enum#valueOf(java.lang.Class, java.lang.String) 
099     */
100    public static <E extends Enum<E>> StyleConverter<String, ? extends Enum<?>> getEnumConverter(Class<E> enumClass) {        
101        // TODO: reuse EnumConverter instances
102        EnumConverter<E> converter;
103        converter = new EnumConverter<E>(enumClass);
104        return converter;
105    }    
106
107    /** 
108     * @return A {@code StyleConverter} that converts a parsed representation
109     * of a {@code Font} to an {@code Font}. 
110     * @see Font#font(java.lang.String, javafx.scene.text.FontWeight, javafx.scene.text.FontPosture, double)  
111     */
112    public static StyleConverter<ParsedValue[], Font> getFontConverter() {
113        return FontConverter.getInstance();
114    }
115    
116    /** 
117     * @return A {@code StyleConverter} that converts a [&lt;length&gt; |
118     * &lt;percentage&gt;]{1,4} to an {@code Insets}. 
119     */
120    public static StyleConverter<ParsedValue[], Insets> getInsetsConverter() {
121        return InsetsConverter.getInstance();
122    }
123
124    /** 
125     * @return A {@code StyleConverter} that converts a parsed representation
126     * of a {@code Paint} to a {@code Paint}.
127     */
128    public static StyleConverter<ParsedValue<?, Paint>, Paint> getPaintConverter() {
129        return PaintConverter.getInstance();
130    }
131    
132    /** 
133     * CSS length and number values are parsed into a Size object that is
134     * converted to a Number before the value is applied. If the property is
135     * a {@code Number} type other than Double, the 
136     * {@link CssMetaData#set(javafx.scene.Node, java.lang.Object, javafx.css.Origin) set}
137     * method of ({@code CssMetaData} can be over-ridden to convert the Number
138     * to the correct type. For example, if the property is an {@code IntegerProperty}:
139     * <code><pre>
140     *     {@literal @}Override public void set(MyNode node, Number value, Origin origin) {
141     *         if (value != null) {
142     *             super.set(node, value.intValue(), origin);
143     *         } else {
144     *             super.set(node, value, origin);
145     *         }
146     *     }
147     * </pre></code>
148     * @return A {@code StyleConverter} that converts a parsed representation
149     * of a CSS length or number value to a {@code Number} that is an instance
150     * of {@code Double}. 
151     */
152    public static StyleConverter<?, Number> getSizeConverter() {
153        return SizeConverter.getInstance();
154    }
155    
156    /** 
157     * A converter for quoted strings which may have embedded unicode characters.
158     * @return A {@code StyleConverter} that converts a representation of a
159     * CSS string value to a {@code String}.
160     */
161    public static StyleConverter<String,String> getStringConverter() {
162        return StringConverter.getInstance();
163    }
164    
165    /** 
166     * A converter for URL strings.
167     * @return A {@code StyleConverter} that converts a representation of a
168     * CSS URL value to a {@code String}.
169     */
170    public static StyleConverter<ParsedValue[], String> getUrlConverter() {
171        return URLConverter.getInstance();
172    }
173    
174
175}