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.FontConverter;
  29 import com.sun.javafx.css.converters.SizeConverter;
  30 import com.sun.javafx.css.converters.StringConverter;
  31 import java.util.ArrayList;
  32 import java.util.Collections;
  33 import java.util.List;
  34 import javafx.scene.text.Font;
  35 import javafx.scene.text.FontPosture;
  36 import javafx.scene.text.FontWeight;
  37 
  38 /**
  39  * An partial implementation of CssMetaData for Font properties which
  40  * includes the font sub-properties: weight, style, family and size.
  41  * @param S The type of Styleable
  42  * @since JavaFX 8.0
  43  */
  44 public abstract class FontCssMetaData<S extends Styleable> extends CssMetaData<S, Font> {
  45 
  46     /**
  47      * The property name is concatenated with &quot;-weight&quot;,
  48      * &quot;-style&quot;, &quot;-family&quot; and &quot;-size&quot; to
  49      * create the sub-properties. For example,
  50      * {@code new FontCssMetaData<Text>("-fx-font", Font.getDefault());}
  51      * will create a CssMetaData for &quot;-fx-font&quot; with
  52      * sub-properties: &quot;-fx-font-weight&quot;,
  53      * &quot;-fx-font-style&quot;, &quot;-fx-font-family&quot;
  54      * and &quot;-fx-font-size&quot;
  55      * @param property
  56      * @param initial
  57      */
  58     /**
  59      * The property name is concatenated with &quot;-weight&quot;,
  60      * &quot;-style&quot;, &quot;-family&quot; and &quot;-size&quot; to
  61      * create the sub-properties. For example,
  62      * {@code new FontCssMetaData<Text>("-fx-font", Font.getDefault());}
  63      * will create a CssMetaData for &quot;-fx-font&quot; with
  64      * sub-properties: &quot;-fx-font-weight&quot;,
  65      * &quot;-fx-font-style&quot;, &quot;-fx-font-family&quot;
  66      * and &quot;-fx-font-size&quot;
  67      * @param property
  68      * @param initial
  69      */
  70     public FontCssMetaData(String property, Font initial) {
  71         super(property, FontConverter.getInstance(), initial, true, createSubProperties(property, initial));
  72     }
  73 
  74     private static <S extends Styleable> List<CssMetaData<? extends Styleable, ?>> createSubProperties(String property, Font initial) {
  75         
  76         final List<CssMetaData<S, ?>> subProperties = 
  77                 new ArrayList<>();
  78         
  79         final Font defaultFont = initial != null ? initial : Font.getDefault();
  80         
  81         final CssMetaData<S, String> FAMILY = 
  82                 new CssMetaData<S, String>(property.concat("-family"), 
  83                 StringConverter.getInstance(), defaultFont.getFamily(), true) {
  84             @Override
  85             public boolean isSettable(S styleable) {
  86                 return false;
  87             }
  88 
  89             @Override
  90             public StyleableProperty<String> getStyleableProperty(S styleable) {
  91                 return null;
  92             }
  93         };
  94         subProperties.add(FAMILY);
  95         
  96         final CssMetaData<S, Number> SIZE = 
  97                 new CssMetaData<S, Number>(property.concat("-size"), 
  98                 SizeConverter.getInstance(), defaultFont.getSize(), true) {
  99             @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 }