io.github.palexdev.mfxeffects.beans.properties.styleable.StyleablePositionProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of materialfx-all Show documentation
Show all versions of materialfx-all Show documentation
Material Design/Modern components for JavaFX, now packed as a single Jar
package io.github.palexdev.mfxeffects.beans.properties.styleable;
import io.github.palexdev.mfxeffects.beans.Position;
import javafx.css.*;
import javafx.scene.text.Font;
import java.util.Arrays;
import java.util.function.Function;
/**
* Convenience {@link StyleableObjectProperty} for {@link Position}, settable via CSS thanks to
* {@link PositionConverter}.
*/
public class StyleablePositionProperty extends SimpleStyleableObjectProperty {
//================================================================================
// Constructors
//================================================================================
public StyleablePositionProperty(CssMetaData extends Styleable, Position> cssMetaData) {
super(cssMetaData);
}
public StyleablePositionProperty(CssMetaData extends Styleable, Position> cssMetaData, Position initialValue) {
super(cssMetaData, initialValue);
}
public StyleablePositionProperty(CssMetaData extends Styleable, Position> cssMetaData, Object bean, String name) {
super(cssMetaData, bean, name);
}
public StyleablePositionProperty(CssMetaData extends Styleable, Position> cssMetaData, Object bean, String name, Position initialValue) {
super(cssMetaData, bean, name, initialValue);
}
//================================================================================
// Methods
//================================================================================
public void setPosition(double x, double y) {
set(Position.of(x, y));
}
@Override
public void applyStyle(StyleOrigin origin, Position v) {
if (v == null) return;
super.applyStyle(origin, v);
}
public static CssMetaData metaDataFor(
String propId, Function property, Position initialValue
) {
return new CssMetaData<>(propId, PositionConverter.getInstance(), initialValue) {
@Override
public boolean isSettable(S styleable) {
return !property.apply(styleable).isBound();
}
@Override
public StyleableProperty getStyleableProperty(S styleable) {
return property.apply(styleable);
}
};
}
//================================================================================
// Overridden Methods
//================================================================================
@Override
public StyleOrigin getStyleOrigin() {
return StyleOrigin.USER_AGENT;
}
//================================================================================
// Internal Classes
//================================================================================
/**
* Style converter implementation to make {@link Position} settable via CSS.
* The related property is {@link StyleablePositionProperty}.
*
* For this to properly work, you must use a specific format. The converter expects a string value,
* with two double numbers which will be in order the x and the y for the new {@code Position}, so:
*
* {@code
* .node {
* -fx-property-name: "100 30";
* }
* }
*
*/
public static class PositionConverter extends StyleConverter {
// lazy, thread-safe instantiation
private static class Holder {
static final PositionConverter INSTANCE = new PositionConverter();
}
/**
* Gets the {@code SizeConverter} instance.
*
* @return the {@code SizeConverter} instance
*/
public static StyleConverter getInstance() {
return Holder.INSTANCE;
}
private PositionConverter() {
super();
}
@Override
public Position convert(ParsedValue value, Font font) {
try {
double[] sizes = Arrays.stream(value.getValue().split(" "))
.mapToDouble(Double::parseDouble)
.toArray();
return Position.of(sizes[0], sizes[1]);
} catch (Exception ex) {
System.out.println(ex.getMessage());
return null;
}
}
@Override
public String toString() {
return "PositionConverter";
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy