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.FontConverter;
029import com.sun.javafx.css.converters.SizeConverter;
030import com.sun.javafx.css.converters.StringConverter;
031import java.util.ArrayList;
032import java.util.Collections;
033import java.util.List;
034import javafx.scene.Node;
035import javafx.scene.text.Font;
036import javafx.scene.text.FontPosture;
037import javafx.scene.text.FontWeight;
038
039/**
040 * An partial implementation of CssMetaData for Font properties which
041 * includes the font sub-properties: weight, style, family and size.
042 * @param N The type of Node
043 */
044public abstract class FontCssMetaData<S extends Styleable> extends CssMetaData<S, Font> {
045
046    /**
047     * The property name is concatenated with &quot;-weight&quot;,
048     * &quot;-style&quot;, &quot;-family&quot; and &quot;-size&quot; to
049     * create the sub-properties. For example,
050     * {@code new FontCssMetaData<Text>("-fx-font", Font.getDefault());}
051     * will create a CssMetaData for &quot;-fx-font&quot; with
052     * sub-properties: &quot;-fx-font-weight&quot;,
053     * &quot;-fx-font-style&quot;, &quot;-fx-font-family&quot;
054     * and &quot;-fx-font-size&quot;
055     * @param property
056     * @param initial
057     */
058    /**
059     * The property name is concatenated with &quot;-weight&quot;,
060     * &quot;-style&quot;, &quot;-family&quot; and &quot;-size&quot; to
061     * create the sub-properties. For example,
062     * {@code new FontCssMetaData<Text>("-fx-font", Font.getDefault());}
063     * will create a CssMetaData for &quot;-fx-font&quot; with
064     * sub-properties: &quot;-fx-font-weight&quot;,
065     * &quot;-fx-font-style&quot;, &quot;-fx-font-family&quot;
066     * and &quot;-fx-font-size&quot;
067     * @param property
068     * @param initial
069     */
070    public FontCssMetaData(String property, Font initial) {
071        super(property, FontConverter.getInstance(), initial, true, createSubProperties(property, initial));
072    }
073
074    private static <S extends Styleable> List<CssMetaData<? extends Styleable, ?>> createSubProperties(String property, Font initial) {
075        
076        final List<CssMetaData<S, ?>> subProperties = 
077                new ArrayList<CssMetaData<S, ?>>();
078        
079        final Font defaultFont = initial != null ? initial : Font.getDefault();
080        
081        final CssMetaData<S, String> FAMILY = 
082                new CssMetaData<S, String>(property.concat("-family"), 
083                StringConverter.getInstance(), defaultFont.getFamily(), true) {
084            @Override
085            public boolean isSettable(S styleable) {
086                return false;
087            }
088
089            @Override
090            public StyleableProperty<String> getStyleableProperty(S styleable) {
091                return null;
092            }
093        };
094        subProperties.add(FAMILY);
095        
096        final CssMetaData<S, Number> SIZE = 
097                new CssMetaData<S, Number>(property.concat("-size"), 
098                SizeConverter.getInstance(), defaultFont.getSize(), true) {
099            @Override
100            public boolean isSettable(S styleable) {
101                return false;
102            }
103
104            @Override
105            public StyleableProperty<Number> getStyleableProperty(S styleable) {
106                return null;
107            }
108        };
109        subProperties.add(SIZE);
110        
111        final CssMetaData<S, FontPosture> STYLE = 
112                new CssMetaData<S, FontPosture>(property.concat("-style"), 
113                FontConverter.FontStyleConverter.getInstance(), FontPosture.REGULAR, true) {
114            @Override
115            public boolean isSettable(S styleable) {
116                return false;
117            }
118
119            @Override
120            public StyleableProperty<FontPosture> getStyleableProperty(S styleable) {
121                return null;
122            }
123        };
124        subProperties.add(STYLE);
125        
126        final CssMetaData<S, FontWeight> WEIGHT = 
127                new CssMetaData<S, FontWeight>(property.concat("-weight"), 
128                FontConverter.FontWeightConverter.getInstance(), FontWeight.NORMAL, true) {
129            @Override
130            public boolean isSettable(S styleable) {
131                return false;
132            }
133
134            @Override
135            public StyleableProperty<FontWeight> getStyleableProperty(S styleable) {
136                return null;
137            }
138        };
139        subProperties.add(WEIGHT);
140        
141        return Collections.<CssMetaData<? extends Styleable, ?>>unmodifiableList(subProperties);
142    }
143    
144}