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 javafx.scene.text.Font;
  29 
  30 /**
  31  * A representation of a parsed CSS value. {@code V} is the type of the parsed
  32  * value, {@code T} is the {@code StyleableProperty} type of the converted value.
  33  * Instances of {@code ParsedValue} are created by the CSS parser. For example,
  34  * the parser creates a {@code ParsedValue<String,Color>} when it parses a
  35  * web Color. 
  36  * <p>
  37  * A ParsedValue is meaningful to the code that calculates actual values from
  38  * parsed CSS values. Elsewhere the value returned by 
  39  * {@link #getValue()} is likely to be obscure, abstruse and perplexing. 
  40  * @since JavaFX 8.0
  41  */
  42 public class ParsedValue<V, T> {
  43 
  44     /**
  45      * The CSS property value as created by the parser.
  46      */
  47     final protected V value;
  48     
  49     /**
  50      * @return The CSS property value as created by the parser, which may be null
  51      * or otherwise incomprehensible.
  52      */
  53     public final V getValue() { return value; }
  54 
  55     /**
  56      * The {@code StyleConverter} which converts the parsed value to 
  57      * the type of the {@link StyleableProperty}. This may be null, in which
  58      * case {@link #convert(javafx.scene.text.Font) convert}
  59      * will return {@link #getValue() getValue()}
  60      */
  61     final protected StyleConverter<V, T> converter;
  62     
  63     /**
  64      * A {@code StyleConverter} converts the parsed value to 
  65      * the type of the {@link StyleableProperty}. If the {@code StyleConverter}
  66      * is null, {@link #convert(javafx.scene.text.Font)}
  67      * will return {@link #getValue()}
  68      * @return The {@code StyleConverter} which converts the parsed value to 
  69      * the type of the {@link StyleableProperty}. May return null.
  70      */
  71     public final StyleConverter<V, T> getConverter() { return converter; }
  72 
  73     /**
  74      * Convenience method for calling 
  75      * {@link StyleConverter#convert(javafx.css.ParsedValue, javafx.scene.text.Font) convert}
  76      * on this {@code ParsedValue}.
  77      * @param font         The {@link Font} to use when converting a
  78      * <a href="http://www.w3.org/TR/css3-values/#relative-lengths">relative</a>
  79      * value.
  80      * @return The value converted to the type of the {@link StyleableProperty}
  81      * @see #getConverter() 
  82      */
  83     @SuppressWarnings("unchecked")
  84     public T convert(Font font) {
  85         // unchecked!
  86         return (T)((converter != null) ? converter.convert(this, font) : value);
  87     }
  88     
  89     /**
  90      * If value is itself a ParsedValue or sequence of values, and should any of
  91      * those values need to be looked up, then this flag is set. This
  92      * does not mean that this particular value needs to be looked up, but
  93      * that this value contains a value that needs to be looked up.
  94      *
  95      * @since 9
  96      */
  97     public boolean isContainsLookups() { return false; }
  98 
  99      /**
 100      * If value references another property, then the real value needs to
 101      * be looked up.
 102      *
 103      * @since 9
 104      */
 105     public boolean isLookup() { return false; }
 106 
 107     /**
 108      * Create an instance of ParsedValue where the value type V is converted to
 109      * the target type T using the given converter.
 110      * If {@code converter} is null, then it is assumed that the type of value 
 111      * {@code V} and the type of target {@code T} are the same and
 112      * do not need converted.
 113      */
 114     protected ParsedValue(V value, StyleConverter<V, T> converter) {
 115         this.value = value;
 116         this.converter = converter;
 117     }
 118 
 119 }