modules/graphics/src/main/java/javafx/css/StyleablePropertyFactory.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


   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.EnumConverter;
  29 import javafx.beans.property.Property;
  30 import javafx.geometry.Insets;
  31 import javafx.scene.effect.Effect;
  32 import javafx.scene.paint.Color;
  33 import javafx.scene.paint.Paint;
  34 import javafx.scene.text.Font;
  35 import javafx.util.Duration;
  36 import javafx.util.Pair;
  37 
  38 import java.util.ArrayList;
  39 import java.util.Collections;
  40 import java.util.HashMap;
  41 import java.util.List;
  42 import java.util.Map;
  43 import java.util.NoSuchElementException;
  44 import java.util.function.Function;
  45 
  46 /**
  47  * Methods for creating instances of StyleableProperty with corresponding CssMetaData created behind the scenes.
  48  * These methods greatly reduce the amount of boiler-plate code needed to implement the StyleableProperty


 138      // Typical JavaFX property implementation
 139      public{@literal ObservableValue<Boolean>} selectedProperty() { return ({@literal ObservableValue<Boolean>})selected; }
 140      public final boolean isSelected() { return selected.getValue(); }
 141      public final void setSelected(boolean isSelected) { selected.setValue(isSelected); }
 142 
 143      // StyleableProperty implementation reduced to one line
 144      private final{@literal StyleableProperty<Boolean>} selected =
 145          new SimpleStyleableBooleanProperty(this, "selected", "my-selected");
 146 
 147      public static {@literal List<CssMetaData<? extends Styleable, ?>>} getClassCssMetaData() {
 148          return FACTORY.getCssMetaData();
 149      }
 150 
 151      {@literal @}Override
 152      public{@literal List<CssMetaData<? extends Styleable, ?>>} getControlCssMetaData() {
 153          return FACTORY.getCssMetaData();
 154      }
 155 
 156  }
 157  * </pre></code>
 158  * <p><span class="simpleTagLabel">Caveats:</span></p>
 159  * The only option for creating a StyleableProperty with a number value is to create a StyleableProperty&lt;Number&gt;</Number>.
 160  * The return value from the <code>getValue()</code> method of the StyleableProperty is a Number. Therefore,
 161  * the <code>get</code> method of the JavaFX property needs to call the correct <code>value</code> method for the return type.
 162  * For example,
 163  * <code><pre>
 164      public ObservableValue<Double> offsetProperty() { return (ObservableValue<Double>)offset; }
 165      public Double getOffset() { return offset.getValue().doubleValue(); }
 166      public void setOffset(Double value) { offset.setValue(value); }
 167      private final StyleableProperty<Number> offset = FACTORY.createStyleableNumberProperty(this, "offset", "-my-offset", s -> ((MyButton)s).offset);
 168  * <br>
 169  * </pre></code>
 170  * @param <S> The type of Styleable
 171  * @since JavaFX 8u40
 172  */
 173 public class StyleablePropertyFactory<S extends Styleable> {
 174 
 175     /**
 176      * The constructor is passed the CssMetaData of the parent class of &lt;S&gt;, typically by calling the
 177      * static <code>getClassCssMetaData()</code> method of the parent.
 178      * @param parentCssMetaData The CssMetaData of the parent class of &lt;S&gt;, or null.




   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.css.converter.EnumConverter;
  29 import javafx.beans.property.Property;
  30 import javafx.geometry.Insets;
  31 import javafx.scene.effect.Effect;
  32 import javafx.scene.paint.Color;
  33 import javafx.scene.paint.Paint;
  34 import javafx.scene.text.Font;
  35 import javafx.util.Duration;
  36 import javafx.util.Pair;
  37 
  38 import java.util.ArrayList;
  39 import java.util.Collections;
  40 import java.util.HashMap;
  41 import java.util.List;
  42 import java.util.Map;
  43 import java.util.NoSuchElementException;
  44 import java.util.function.Function;
  45 
  46 /**
  47  * Methods for creating instances of StyleableProperty with corresponding CssMetaData created behind the scenes.
  48  * These methods greatly reduce the amount of boiler-plate code needed to implement the StyleableProperty


 138      // Typical JavaFX property implementation
 139      public{@literal ObservableValue<Boolean>} selectedProperty() { return ({@literal ObservableValue<Boolean>})selected; }
 140      public final boolean isSelected() { return selected.getValue(); }
 141      public final void setSelected(boolean isSelected) { selected.setValue(isSelected); }
 142 
 143      // StyleableProperty implementation reduced to one line
 144      private final{@literal StyleableProperty<Boolean>} selected =
 145          new SimpleStyleableBooleanProperty(this, "selected", "my-selected");
 146 
 147      public static {@literal List<CssMetaData<? extends Styleable, ?>>} getClassCssMetaData() {
 148          return FACTORY.getCssMetaData();
 149      }
 150 
 151      {@literal @}Override
 152      public{@literal List<CssMetaData<? extends Styleable, ?>>} getControlCssMetaData() {
 153          return FACTORY.getCssMetaData();
 154      }
 155 
 156  }
 157  * </pre></code>
 158  * <p><span class="simpleTagLabel">Caveats:</span></p>
 159  * The only option for creating a StyleableProperty with a number value is to create a StyleableProperty&lt;Number&gt;</Number>.
 160  * The return value from the <code>getValue()</code> method of the StyleableProperty is a Number. Therefore,
 161  * the <code>get</code> method of the JavaFX property needs to call the correct <code>value</code> method for the return type.
 162  * For example,
 163  * <code><pre>
 164      public ObservableValue<Double> offsetProperty() { return (ObservableValue<Double>)offset; }
 165      public Double getOffset() { return offset.getValue().doubleValue(); }
 166      public void setOffset(Double value) { offset.setValue(value); }
 167      private final StyleableProperty<Number> offset = FACTORY.createStyleableNumberProperty(this, "offset", "-my-offset", s -> ((MyButton)s).offset);
 168  * <br>
 169  * </pre></code>
 170  * @param <S> The type of Styleable
 171  * @since JavaFX 8u40
 172  */
 173 public class StyleablePropertyFactory<S extends Styleable> {
 174 
 175     /**
 176      * The constructor is passed the CssMetaData of the parent class of &lt;S&gt;, typically by calling the
 177      * static <code>getClassCssMetaData()</code> method of the parent.
 178      * @param parentCssMetaData The CssMetaData of the parent class of &lt;S&gt;, or null.