org.jhotdraw8.draw.css.converter.SymmetricPoint2DConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.jhotdraw8.draw Show documentation
Show all versions of org.jhotdraw8.draw Show documentation
JHotDraw8 Drawing Framework
The newest version!
/*
* @(#)SymmetricPoint2DConverter.java
* Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
*/
package org.jhotdraw8.draw.css.converter;
import javafx.geometry.Point2D;
import org.jhotdraw8.base.converter.IdResolver;
import org.jhotdraw8.base.converter.IdSupplier;
import org.jhotdraw8.css.converter.AbstractCssConverter;
import org.jhotdraw8.css.parser.CssToken;
import org.jhotdraw8.css.parser.CssTokenType;
import org.jhotdraw8.css.parser.CssTokenizer;
import org.jspecify.annotations.Nullable;
import java.io.IOException;
import java.text.ParseException;
import java.util.function.Consumer;
/**
* Converts a {@code javafx.geometry.Point2D} into a {@code String} and vice
* versa. If the X and the Y-value are identical, then only one value is output.
*
* @author Werner Randelshofer
*/
public class SymmetricPoint2DConverter extends AbstractCssConverter {
private final boolean withSpace;
private final boolean withComma;
public SymmetricPoint2DConverter() {
this(false, true, false);
}
public SymmetricPoint2DConverter(boolean nullable) {
this(nullable, true, false);
}
public SymmetricPoint2DConverter(boolean nullable, boolean withSpace, boolean withComma) {
super(nullable);
this.withSpace = withSpace;
this.withComma = withComma || !withSpace;
}
@Override
public Point2D parseNonNull(CssTokenizer tt, @Nullable IdResolver idResolver) throws ParseException, IOException {
final double x, y;
tt.requireNextToken(CssTokenType.TT_NUMBER, "x");
x = tt.currentNumberNonNull().doubleValue();
if (tt.next() == CssTokenType.TT_EOF) {
y = x;
} else {
tt.pushBack();
tt.skipIfPresent(CssTokenType.TT_COMMA);
tt.requireNextToken(CssTokenType.TT_NUMBER, "y");
y = tt.currentNumberNonNull().doubleValue();
}
return new Point2D(x, y);
}
@Override
protected void produceTokensNonNull(TT value, @Nullable IdSupplier idSupplier, Consumer out) {
double x = value.getX();
double y = value.getY();
out.accept(new CssToken(CssTokenType.TT_NUMBER, x));
if (x != y) {
if (withComma) {
out.accept(new CssToken(CssTokenType.TT_COMMA));
}
if (withSpace) {
out.accept(new CssToken(CssTokenType.TT_S, " "));
}
out.accept(new CssToken(CssTokenType.TT_NUMBER, y));
}
}
@Override
public @Nullable Point2D getDefaultValue() {
return new Point2D(0, 0);
}
@Override
public @Nullable String getHelpText() {
return "Format of ⟨SymmetricPoint2D⟩: ⟨xy⟩ | ⟨x⟩ ⟨y⟩";
}
}