Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

WIP: Additional stylable properties support for ImageView #30

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

import com.sun.javafx.beans.event.AbstractNotifyListener;
import com.sun.javafx.css.StyleManager;
import javafx.css.ParsedValue;
import javafx.css.StyleableBooleanProperty;
import javafx.css.StyleableDoubleProperty;
import javafx.css.converter.BooleanConverter;
import javafx.css.converter.SizeConverter;
import javafx.css.converter.URLConverter;
import com.sun.javafx.geom.BaseBounds;
import com.sun.javafx.geom.transform.BaseTransform;
Expand All @@ -47,6 +52,8 @@
import javafx.geometry.Rectangle2D;
import javafx.scene.AccessibleRole;
import javafx.scene.Node;
import javafx.scene.text.Font;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -421,7 +428,7 @@ public final double getFitWidth() {

public final DoubleProperty fitWidthProperty() {
if (fitWidth == null) {
fitWidth = new DoublePropertyBase() {
fitWidth = new StyleableDoubleProperty() {

@Override
protected void invalidated() {
Expand All @@ -430,6 +437,11 @@ protected void invalidated() {
NodeHelper.geomChanged(ImageView.this);
}

@Override
public CssMetaData getCssMetaData() {
return StyleableProperties.FIT_WIDTH;
}

@Override
public Object getBean() {
return ImageView.this;
Expand Down Expand Up @@ -469,7 +481,7 @@ public final double getFitHeight() {

public final DoubleProperty fitHeightProperty() {
if (fitHeight == null) {
fitHeight = new DoublePropertyBase() {
fitHeight = new StyleableDoubleProperty() {

@Override
protected void invalidated() {
Expand All @@ -478,6 +490,11 @@ protected void invalidated() {
NodeHelper.geomChanged(ImageView.this);
}

@Override
public CssMetaData getCssMetaData() {
return StyleableProperties.FIT_HEIGHT;
}

@Override
public Object getBean() {
return ImageView.this;
Expand Down Expand Up @@ -534,7 +551,7 @@ public final boolean isPreserveRatio() {

public final BooleanProperty preserveRatioProperty() {
if (preserveRatio == null) {
preserveRatio = new BooleanPropertyBase() {
preserveRatio = new StyleableBooleanProperty() {

@Override
protected void invalidated() {
Expand All @@ -543,6 +560,11 @@ protected void invalidated() {
NodeHelper.geomChanged(ImageView.this);
}

@Override
public CssMetaData getCssMetaData() {
return StyleableProperties.PRESERVE_RATIO;
}

@Override
public Object getBean() {
return ImageView.this;
Expand Down Expand Up @@ -586,13 +608,18 @@ public final boolean isSmooth() {

public final BooleanProperty smoothProperty() {
if (smooth == null) {
smooth = new BooleanPropertyBase(SMOOTH_DEFAULT) {
smooth = new StyleableBooleanProperty(SMOOTH_DEFAULT) {

@Override
protected void invalidated() {
NodeHelper.markDirty(ImageView.this, DirtyBits.NODE_SMOOTH);
}

@Override
public CssMetaData getCssMetaData() {
return StyleableProperties.SMOOTH;
}

@Override
public Object getBean() {
return ImageView.this;
Expand Down Expand Up @@ -826,13 +853,77 @@ public StyleableProperty<String> getStyleableProperty(ImageView n) {
}
};

private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
static {
private static final CssMetaData<ImageView,Number> FIT_WIDTH =
new CssMetaData<ImageView,Number>("-fx-fit-width",
SizeConverter.getInstance(), 0.0) {

@Override
public boolean isSettable(ImageView n) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

general question: should we define params in new code as final?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this code was C&P from Node thus it follows the same code semantics and structure as previously existing code. Changing ImageView would require updating all other classes that follow the same programming idioms.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, that's why I said 'general question' ;) Do not make sense to do it here. Just something that came in my mind when I saw your PR.

return n.fitWidth == null || !n.fitWidth.isBound();
}

@Override
public StyleableProperty<Number> getStyleableProperty(ImageView n) {
return (StyleableProperty<Number>)n.fitWidthProperty();
}
};

private static final CssMetaData<ImageView,Number> FIT_HEIGHT =
new CssMetaData<ImageView,Number>("-fx-fit-height",
SizeConverter.getInstance(), 0.0) {

@Override
public boolean isSettable(ImageView n) {
return n.fitHeight == null || !n.fitHeight.isBound();
}

@Override
public StyleableProperty<Number> getStyleableProperty(ImageView n) {
return (StyleableProperty<Number>)n.fitHeightProperty();
}
};

private static final CssMetaData<ImageView,Boolean> SMOOTH =
new CssMetaData<ImageView,Boolean>("-fx-smooth",
BooleanConverter.getInstance(), SMOOTH_DEFAULT) {

@Override
public boolean isSettable(ImageView n) {
return n.smooth == null || !n.smooth.isBound();
}

@Override
public StyleableProperty<Boolean> getStyleableProperty(ImageView n) {
return (StyleableProperty<Boolean>)n.smoothProperty();
}
};

private static final CssMetaData<ImageView,Boolean> PRESERVE_RATIO =
new CssMetaData<ImageView,Boolean>("-fx-preserve-ratio",
BooleanConverter.getInstance(), Boolean.FALSE) {

@Override
public boolean isSettable(ImageView n) {
return n.preserveRatio == null || !n.preserveRatio.isBound();
}

@Override
public StyleableProperty<Boolean> getStyleableProperty(ImageView n) {
return (StyleableProperty<Boolean>)n.preserveRatioProperty();
}
};

private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
static {
final List<CssMetaData<? extends Styleable, ?>> styleables =
new ArrayList<CssMetaData<? extends Styleable, ?>>(Node.getClassCssMetaData());
styleables.add(IMAGE);
styleables.add(FIT_WIDTH);
styleables.add(FIT_HEIGHT);
styleables.add(SMOOTH);
styleables.add(PRESERVE_RATIO);
STYLEABLES = Collections.unmodifiableList(styleables);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public final class ImageView_cssMethods_Test extends CssMethodsTestBase {
private static final ImageView TEST_IMAGE_VIEW = new ImageView();
private static final String TEST_IMAGE_URL1 = "file:test_image_1.png";
private static final String TEST_IMAGE_URL2 = "file:test_image_2.png";
public static final boolean SMOOTH_DEFAULT = Toolkit.getToolkit().getDefaultImageSmooth();

private static final ValueComparator IMAGE_COMPARATOR =
new ValueComparator() {
Expand Down Expand Up @@ -81,7 +82,15 @@ public static Collection data() {
TestImages.TEST_IMAGE_32x32,
"-fx-image", TEST_IMAGE_URL2, IMAGE_COMPARATOR),
config(TEST_IMAGE_VIEW, "translateX", 0.0,
"-fx-translate-x", 10.0)
"-fx-translate-x", 10.0),
config(TEST_IMAGE_VIEW, "fitWidth", 0,
"-fx-fit-width", 1.0),
config(TEST_IMAGE_VIEW, "fitHeight", 0,
"-fx-fit-height", 1.0),
config(TEST_IMAGE_VIEW, "smooth", SMOOTH_DEFAULT,
"-fx-smooth", !SMOOTH_DEFAULT),
config(TEST_IMAGE_VIEW, "preserveRatio", false,
"-fx-preserve-ratio", true)
});
}

Expand Down