Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

First, the copyrightAngle is made styleable by having it extend one of the StyleableProperty classes; in this case, StyleableDoubleProperty. The code shown here follows the typical pattern. Notice that the only difference between a DoubleProperty and the StyleableDoubleProperty is the addition of the getCssMetaData method which links the copyrightAngle property to its corresponding CssMetaData instance.

Code Block

    private DoubleProperty copyrightAngle = new StyleableDoubleProperty(45d) {

                 /** Link this property with its CssMetaData */
                @Override
                public CssMetaData getCssMetaData() {
                    return COPYRIGHT_ANGLE;
                }

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

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

    public final void setCopyrightAngle(double value) {
        copyrightAngle.set(value);
    }
    public final double getCopyrightAngle() {
        return copyrightAngle.get();
    }

    public final DoubleProperty copyrightAngleProperty() {
        return copyrightAngle;
    }

The CssMetaData instance provides information about the CSS style and some methods that allow CSS to set the property's value. The convention is to instantiate the CssMetaData as a singleton (note that this example code is not thread safe, but is sufficient). Here, the CSS property name is "-my-copyright-angle" and the initial value of 45 degrees with the default value of the copyrightAngle property.

Code Block
    private static final CssMetaData<Watermark,Number> COPYRIGHT_ANGLE = new CssMetaData("-my-copyright-angle", 45d) {

        public abstract boolean isSettable(Watermark node) {
            return copyrightAngle == null || copyrightAngle.isBound() == false;
        }

        public abstract WritableValue<Number> getWritableValue(Watermark node) {
            return copyrightAngleProperty();
        }
    };

In order for the CSS engine to know about the styleable properties of Watermark, the methods getClassCssMetaData() and getCssMetaData() need to be implemented. The first is a static method that returns the CssMetaData of the Watermark class and of its super class; in Watermark's case, the super class is Control. The second method returns the same value but is implemented as an instance method so that it is not necessary to use reflection to call getClassCssMetaData(). These methods are called quite frequently; for efficiency, the List<CssMetaData> is only created once. The following code shows a typical implementation.

Code Block
    private static final List<CssMetaData> CSS_META_DATA;
    static {
             final List<CssMetaData> metaData = new ArrayList<CssMetaData>(Control.getClassCssMetaData());
             Collections.addAll(metaData,
                 COPYRIGHT_ANGLE
             );
             CSS_META_DATA = Collections.unmodifiableList(metaData);

    }

    public static List<CssMetaData> getClassCssMetaData() {
        return CSS_META_DATA;
    }

    @Override public List<CssMetaData> getCssMetaData() {
        return getClassCssMetaData();
     }

...

Code Block
/**
  * The constructor of the {@code StyleableBooleanProperty}.
  */
  public StyleableBooleanProperty(CssMetaData CssMetaData) {
    super();
    this.CssMetaData = CssMetaData;
  }

  /**
  * The constructor of the {@code StyleableBooleanProperty}.
  *
  * @param CssMetaData
  *      the {@code CssMetaData} that corresponds to this {@code StyleableProperty}
  * @param initialValue
  *      the initial value of the wrapped {@code Object}
  */
  public StyleableBooleanProperty(CssMetaData CssMetaData, boolean initialValue) {
    super(initialValue);
    this.CssMetaData = CssMetaData;
  }

public class SImpleStyleableBooleanProperty SimpleStyleableBooleanProperty extends SImpleBooleanProperty SimpleBooleanProperty implements StyleableProperty<Boolean>
public class SImpleStyleableFloatProperty SimpleStyleableFloatProperty extends SImpleFloatProperty SimpleFloatProperty implements StyleableProperty<Float>
public class SImpleStyleableDoubleProperty SimpleStyleableDoubleProperty extends SImpleDoubleProperty SimpleDoubleProperty implements StyleableProperty<Double>
public class SImpleStyleableIntegerProperty SimpleStyleableIntegerProperty extends SImpleIntegerProperty SimpleIntegerProperty implements StyleableProperty<Integer>
public class SImpleStyleableLongProperty SimpleStyleableLongProperty extends SImpleLongProperty SimpleLongProperty implements StyleableProperty<Long>
public class SImpleStyleableStringProperty SimpleStyleableStringProperty extends SImpleStringProperty SimpleStringProperty implements StyleableProperty<String>
public class SImpleStyleableObjectProperty<T> SimpleStyleableObjectProperty<T> extends SImpleObjectProperty<T> SimpleObjectProperty<T> implements StyleableProperty<T>

...