Package javafx.css

Class CssMetaData<S extends Styleable,V>

java.lang.Object
javafx.css.CssMetaData<S,V>
Type Parameters:
S - The type of Styleable
V - The type into which the parsed value is converted.
Direct Known Subclasses:
FontCssMetaData

public abstract class CssMetaData<S extends Styleable,V> extends Object
A CssMetaData instance provides information about the CSS style and provides the hooks that allow CSS to set a property value. It encapsulates the CSS property name, the type into which the CSS value is converted, and the default value of the property.

CssMetaData is the bridge between a value that can be represented syntactically in a .css file, and a StyleableProperty. There is a one-to-one correspondence between a CssMetaData and a StyleableProperty. Typically, the CssMetaData of a Node will include the CssMetaData of its ancestors. For example, the CssMetaData of a Rectangle includes the CssMetaData of Shape and also of Node. During CSS processing, the CSS engine iterates over the Node's CssMetaData, looks up the parsed value of each property, converts the parsed value, and sets the value on the StyleableProperty.

The method Node.getCssMetaData() is called to obtain the List<CssMetaData>. This method is called frequently and it is prudent to return a static list rather than creating the list on each call. By convention, node classes that have CssMetaData will implement a static method getClassCssMetaData() and it is customary to have getCssMetaData() simply return getClassCssMetaData(). The purpose of getClassCssMetaData() is to allow sub-classes to easily include the CssMetaData of some ancestor.

The StyleablePropertyFactory greatly simplifies creating a StyleableProperty and its corresponding CssMetaData.

This example is a typical implementation.


 private DoubleProperty gapProperty = new StyleableDoubleProperty(0) {
     @Override
      public CssMetaData<MyWidget,Number> getCssMetaData() {
          return GAP_META_DATA;
      }

      @Override
      public Object getBean() {
          return MyWidget.this;
      }

      @Override
      public String getName() {
          return "gap";
      }
 };

 private static final CssMetaData GAP_META_DATA =
     new CssMetaData<MyWidget,Number>("-my-gap", StyleConverter.getSizeConverter(), 0d) {

        @Override
        public boolean isSettable(MyWidget node) {
            return node.gapProperty == null || !node.gapProperty.isBound();
        }

        @Override
        public StyleableProperty<Number> getStyleableProperty(MyWidget node) {
            return (StyleableProperty<Number>)node.gapProperty;
        }
 };

 private static final List<CssMetaData<? extends Node, ?>> cssMetaDataList;
 static {
     List<CssMetaData<? extends Node, ?>> temp =
         new ArrayList<CssMetaData<? extends Node, ?>>(Control.getClassCssMetaData());
     temp.add(GAP_META_DATA);
     cssMetaDataList = Collections.unmodifiableList(temp);
 }

 public static List<CssMetaData<? extends Node, ?>> getClassCssMetaData() {
     return cssMetaDataList;
 }

 @Override
 public List<CssMetaData<? extends Node, ?>> getCssMetaData() {
     return getClassCssMetaData();
 }
 
Since:
JavaFX 8.0
See Also: