[8u40] Review request: (RT-36510) [CSS] Reduce CssMetaData boilerplate code

David Grieve david.grieve at oracle.com
Thu Jun 19 18:27:13 UTC 2014


I have attached some java files to RT-36510 that are aimed at reducing 
the amount of boiler-plate code needed to implement StyleableProperty 
and CssMetaData. The code is not complete - it can only create a 
StyleableProperty<Boolean> right now - but it is sufficiently far along 
to warrant a review.

Please see https://javafx-jira.kenai.com/browse/RT-36510#comment-414209 
for more information.

Example with the RT-36510 code:

public class MyButton extends Button implements 
StyleableSupplier<MyButton> {

     public MyButton(String text) {
         super();
         getStyleClass().add("foo");
     }

     public MyButton get() { return this; }

     public ObservableValue<Boolean> selectedProperty() { return 
(ObservableValue<Boolean>)selected; }
     public boolean isSelected() { return selected.getValue(); }
     public void setSelected(boolean isSelected) { 
selected.setValue(isSelected); }
     private final StyleableProperty<Boolean> selected = 
StyleablePropertyFactory.createStyleableBooleanProperty(this, 
"-my-selected", s -> ((MyButton) s).selected);

     @Override
     public List<CssMetaData<? extends Styleable, ?>> 
getControlCssMetaData() {
         return StyleablePropertyFactory.getCssMetaData(this);
     }

}

The same example without the RT-36510 code:

public class MyButton extends Button {

     public MyButton(String text) {
         super();
         getStyleClass().add("foo");
     }

     public ObservableValue<Boolean> selectedProperty() { return 
(ObservableValue<Boolean>)selected; }
     public boolean isSelected() { return selected.getValue(); }
     public void setSelected(boolean isSelected) { 
selected.setValue(isSelected); }
     private final StyleableProperty<Boolean> selected = new 
SimpleStyleableBooleanProperty(SELECTED);

     private static CssMetaData<MyButton, Boolean> SELECTED = new 
CssMetaData<MyButton, Boolean>("-my-selected", 
StyleConverter.getBooleanConverter(), Boolean.FALSE) {
         @Override
         public boolean isSettable(MyButton styleable) {
             return 
!((Property<Boolean>)styleable.selectedProperty()).isBound();
         }

         @Override
         public StyleableProperty<Boolean> getStyleableProperty(MyButton 
styleable) {
             return styleable.selected;
         }
     };

     private final static List<CssMetaData<? extends Styleable, ?>> 
CSS_META_DATA;
     static {
         List<CssMetaData<? extends Styleable, ?>> list = new 
ArrayList<>(Button.getClassCssMetaData());
         list.add(SELECTED);
         CSS_META_DATA = Collections.unmodifiableList(list);
     }

     @Override
     public List<CssMetaData<? extends Styleable, ?>> 
getControlCssMetaData() {
         return CSS_META_DATA;
     }

}




More information about the openjfx-dev mailing list