Skip to content

Commit

Permalink
Attempt to do it differently
Browse files Browse the repository at this point in the history
  • Loading branch information
hjohn committed Aug 9, 2024
1 parent abef432 commit a8e111b
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package com.sun.javafx.css;

import javafx.animation.Interpolator;
import javafx.css.CompositeStyleConverter;
import javafx.css.CssMetaData;
import javafx.css.ParsedValue;
import javafx.css.Size;
Expand Down Expand Up @@ -85,7 +86,9 @@ public TransitionDefinition convert(ParsedValue<ParsedValue[], TransitionDefinit
* Converts a sequence of parsed values to an array of {@link TransitionDefinition} instances.
*/
public static final class SequenceConverter
extends StyleConverter<ParsedValue<ParsedValue[], TransitionDefinition>[], TransitionDefinition[]> {
extends StyleConverter<ParsedValue<ParsedValue[], TransitionDefinition>[], TransitionDefinition[]>
implements CompositeStyleConverter<TransitionDefinition[]>
{
private static final TransitionDefinition[] EMPTY_TRANSITION = new TransitionDefinition[0];
private static final String[] EMPTY_STRING = new String[0];
private static final Duration[] EMPTY_DURATION = new Duration[0];
Expand Down Expand Up @@ -133,6 +136,12 @@ public TransitionDefinition[] convert(Map<CssMetaData<? extends Styleable, ?>, O
return transitions;
}

@Override
public Map<CssMetaData<? extends Styleable, ?>, Object> convertBack(TransitionDefinition[] value) {
throw new UnsupportedOperationException("TODO implement?"); // TODO Auto-generated method stub
// return null;
}

@Override
public TransitionDefinition[] convert(
ParsedValue<ParsedValue<ParsedValue[], TransitionDefinition>[], TransitionDefinition[]> value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.sun.javafx.scene.NodeHelper;
import javafx.animation.Interpolator;
import javafx.css.CompositeStyleConverter;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.css.StyleableProperty;
Expand All @@ -52,7 +53,7 @@ public static TransitionDefinitionCssMetaData getInstance() {
}

public TransitionDefinitionCssMetaData() {
super("transition", TransitionDefinitionConverter.SequenceConverter.getInstance(),
super("transition", (CompositeStyleConverter<TransitionDefinition[]>) TransitionDefinitionConverter.SequenceConverter.getInstance(),
new TransitionDefinition[0], false, createSubProperties());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package javafx.css;

import java.util.Map;

public interface CompositeStyleConverter<T> {

/**
* Converts a map of CSS values to the target type.
*
* @param values the constituent values
* @return the converted object
*/
T convert(Map<CssMetaData<? extends Styleable, ?>, Object> values);

/**
* Converts an object back to a map of its constituent values (deconstruction).
* The returned map can be passed into {@link #convert(Map)} to reconstruct the object.
*
* @param value the object
* @return a {@code Map} of the constituent values
*/
Map<CssMetaData<? extends Styleable, ?>, Object> convertBack(T value);
}
26 changes: 26 additions & 0 deletions modules/javafx.graphics/src/main/java/javafx/css/CssMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ protected CssMetaData(

this.property = property;
this.converter = converter;
this.compositeStyleConverter = null;
this.initialValue = initialValue;
this.inherits = inherits;
this.subProperties = subProperties != null ? Collections.unmodifiableList(subProperties) : null;
Expand All @@ -248,6 +249,31 @@ protected CssMetaData(
}
}

protected CssMetaData(
final String property,
final CompositeStyleConverter<V> compositeStyleConverter,
final V initialValue,
boolean inherits,
final List<CssMetaData<? extends Styleable, ?>> subProperties) {

this.property = property;
this.converter = null;
this.compositeStyleConverter = compositeStyleConverter;
this.initialValue = initialValue;
this.inherits = inherits;
this.subProperties = subProperties != null ? Collections.unmodifiableList(subProperties) : null;

if (this.property == null || this.compositeStyleConverter == null) {
throw new IllegalArgumentException("neither property nor compositeStyleConverter can be null");
}
}

private final CompositeStyleConverter<V> compositeStyleConverter;

public CompositeStyleConverter<V> getCompositeStyleConverter() {
return compositeStyleConverter;
}

/**
* Construct a CssMetaData with the given parameters and no sub-properties.
* @param property the CSS property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,6 @@
*/
public class StyleConverter<F, T> {

/**
* Defines the {@code convert} and {@code convertBack} operations that enable object
* decomposition and reconstruction. Note that the following invariant must always be
* satisfied: {@code convert(convertBack(value)).equals(value)}
*
* @param <T> the target type
* @since 24
*/
public interface WithReconstructionSupport<T> {
/**
* Converts a map of CSS values to the target type.
*
* @param values the constituent values
* @return the converted object
*/
T convert(Map<CssMetaData<? extends Styleable, ?>, Object> values);

/**
* Converts an object back to a map of its constituent values (deconstruction).
* The returned map can be passed into {@link #convert(Map)} to reconstruct the object.
*
* @param value the object
* @return a {@code Map} of the constituent values
*/
Map<CssMetaData<? extends Styleable, ?>, Object> convertBack(T value);
}

/**
* Creates a {@code StyleConverter}.
*/
Expand Down Expand Up @@ -276,9 +249,10 @@ public static StyleConverter<ParsedValue[], String> getUrlConverter() {
* @return the target property type
* @since 9
*/
public T convert(Map<CssMetaData<? extends Styleable, ?>,Object> convertedValues) {
return null;
}
// just to proof it is never used in this way
// public T convert(Map<CssMetaData<? extends Styleable, ?>,Object> convertedValues) {
// return null;
// }

/**
* Write binary data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ public StyleableObjectProperty(T initialValue) {
super(initialValue);
}

/** {@inheritDoc} */
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void applyStyle(StyleOrigin origin, T newValue) {
CssMetaData<? extends Styleable, T> metadata = getCssMetaData();
StyleConverter<?, T> converter = metadata.getConverter();
CompositeStyleConverter<T> compositeStyleConverter = metadata.getCompositeStyleConverter();

if (converter instanceof StyleConverter.WithReconstructionSupport c) {
applyValueComponents(newValue, metadata, c);
} else {
if(compositeStyleConverter != null) {
applyValueComponents(newValue, metadata, compositeStyleConverter);
}
else {
applyValue(newValue, metadata);
}

Expand Down Expand Up @@ -142,7 +141,7 @@ private void applyValue(T newValue, CssMetaData<? extends Styleable, T> metadata
*/
private void applyValueComponents(T newValue,
CssMetaData<? extends Styleable, T> metadata,
StyleConverter.WithReconstructionSupport<T> converter) {
CompositeStyleConverter<T> converter) {
// If this.origin == null, we're setting the value for the first time.
// No transition should be started in this case.
Map<CssMetaData<? extends Styleable, ?>, TransitionDefinition> transitions =
Expand All @@ -167,7 +166,7 @@ private void applyValueComponents(T newValue,
}
}

@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings("unchecked")
private void processComponent(AggregatingTransitionController controller,
CssMetaData<? extends Styleable, ?> metadata,
Map<CssMetaData<? extends Styleable, ?>, TransitionDefinition> transitions,
Expand Down Expand Up @@ -474,7 +473,7 @@ public void onUpdate(CssMetaData<? extends Styleable, ?> metadata, Object value)
if (--remainingValues == 0) {
try {
updating = true;
set(getCssMetaData().getConverter().convert(cssValues));
set(getCssMetaData().getCompositeStyleConverter().convert(cssValues));
} finally {
updating = false;
}
Expand Down Expand Up @@ -554,7 +553,7 @@ public void run(long now) {
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings("unchecked")
public void onUpdate(double progress) {
Object value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@

import com.sun.javafx.util.Utils;
import javafx.css.Size;
import javafx.css.CssMetaData;
import javafx.css.ParsedValue;
import javafx.css.StyleConverter;
import javafx.css.Styleable;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;

import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

/**
* Converter to convert a parsed representation of a {@code Font} to a {@code Font}.
Expand Down Expand Up @@ -81,35 +77,6 @@ public Font convert(ParsedValue<ParsedValue[], Font> value, Font font) {
return f;
}

@Override
public Font convert(Map<CssMetaData<? extends Styleable, ?>, Object> convertedValues) {
Font font = Font.getDefault();
double size = font.getSize();
String family = font.getFamily();
FontWeight weight = FontWeight.NORMAL;
FontPosture style = FontPosture.REGULAR;

for (Entry<CssMetaData<? extends Styleable, ?>, Object> entry : convertedValues.entrySet()) {

Object value = entry.getValue();
if (value == null) {
continue;
}
final String prop = entry.getKey().getProperty();
if (prop.endsWith("font-size")) {
size = ((Number) value).doubleValue();
} else if (prop.endsWith("font-family")) {
family = Utils.stripQuotes((String) value);
} else if (prop.endsWith("font-weight")) {
weight = (FontWeight) value;
} else if (prop.endsWith("font-style")) {
style = (FontPosture) value;
}
}
final Font f = Font.font(family, weight, style, size);
return f;
}

@Override
public String toString() {
return "FontConverter";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@
import java.util.Map.Entry;
import java.util.Set;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.WritableValue;
import com.sun.javafx.css.CascadingStyle;
import com.sun.javafx.css.ImmutablePseudoClassSetsCache;

import javafx.css.CompositeStyleConverter;
import javafx.css.CssMetaData;
import javafx.css.CssParser;
import javafx.css.FontCssMetaData;
Expand Down Expand Up @@ -1158,7 +1157,7 @@ private CalculatedValue lookup(final Styleable styleable,
}

try {
final StyleConverter keyType = cssMetaData.getConverter();
final CompositeStyleConverter keyType = cssMetaData.getCompositeStyleConverter();
Object ret = keyType.convert(subs);
return new CalculatedValue(ret, origin, isRelative);
} catch (ClassCastException cce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import com.sun.javafx.scene.layout.region.RepeatStruct;
import java.util.List;
import java.util.Map;

import javafx.css.CompositeStyleConverter;
import javafx.css.CssMetaData;
import javafx.css.ParsedValue;
import javafx.css.StyleConverter;
import javafx.css.Styleable;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
Expand All @@ -40,10 +40,9 @@
/**
* Converts the CSS for -fx-background items into a Background.
*/
class BackgroundConverter extends StyleConverter<ParsedValue[], Background>
implements StyleConverter.WithReconstructionSupport<Background> {
class BackgroundConverter implements CompositeStyleConverter<Background> {

static final StyleConverter<ParsedValue[], Background> INSTANCE = new BackgroundConverter();
static final CompositeStyleConverter<Background> INSTANCE = new BackgroundConverter();

@Override public Background convert(Map<CssMetaData<? extends Styleable, ?>,Object> convertedValues) {
final Paint[] fills = (Paint[]) convertedValues.get(Background.BACKGROUND_COLOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
import com.sun.javafx.scene.layout.region.RepeatStruct;
import java.util.List;
import java.util.Map;

import javafx.css.CompositeStyleConverter;
import javafx.css.CssMetaData;
import javafx.css.ParsedValue;
import javafx.css.Styleable;
import javafx.css.StyleConverter;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
Expand All @@ -43,8 +43,8 @@
/**
* Converts -fx-border components into a {@link Border}.
*/
class BorderConverter extends StyleConverter<ParsedValue[], Border>
implements StyleConverter.WithReconstructionSupport<Border> {
class BorderConverter //extends StyleConverter<ParsedValue[], Border>
implements CompositeStyleConverter<Border> {

private static final BorderConverter BORDER_IMAGE_CONVERTER =
new BorderConverter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@

package javafx.scene.layout;

import javafx.css.CompositeStyleConverter;
import javafx.css.CssMetaData;
import javafx.css.ParsedValue;
import javafx.css.StyleConverter;
import javafx.scene.Node;
import javafx.scene.image.Image;

public class BackgroundShim {

public static final CssMetaData<Node, Image[]> BACKGROUND_IMAGE = Background.BACKGROUND_IMAGE;

public static StyleConverter<ParsedValue[], Background> getConverter() {
public static CompositeStyleConverter<Background> getConverter() {
return BackgroundConverter.INSTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@

package javafx.scene.layout;

import javafx.css.CompositeStyleConverter;
import javafx.css.CssMetaData;
import javafx.css.ParsedValue;
import javafx.css.StyleConverter;
import javafx.scene.Node;

public class BorderShim {

public static final CssMetaData<Node, String[]> BORDER_IMAGE_SOURCE = Border.BORDER_IMAGE_SOURCE;

public static StyleConverter<ParsedValue[], Border> getConverter() {
public static CompositeStyleConverter<Border> getConverter() {
return BorderConverter.getInstance();
}
}
Loading

0 comments on commit a8e111b

Please sign in to comment.