All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jhotdraw8.draw.css.converter.PaintCssConverter Maven / Gradle / Ivy

The newest version!
/*
 * @(#)CssPaintConverter.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */
package org.jhotdraw8.draw.css.converter;

import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Paint;
import javafx.scene.paint.RadialGradient;
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.CssTokenizer;
import org.jhotdraw8.draw.css.value.CssColor;
import org.jhotdraw8.draw.css.value.CssLinearGradient;
import org.jhotdraw8.draw.css.value.CssRadialGradient;
import org.jhotdraw8.draw.css.value.Paintable;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.text.ParseException;
import java.util.function.Consumer;

/**
 * CssPaintableConverter.
 * 

* Parses the following EBNF from the * JavaFX * CSS Reference Guide. *

*
 * Paintable := (Color|LinearGradient|RadialGradient|ImagePattern RepeatingImagePattern) ;
 * 
*

* FIXME currently only parses the Color and the LinearGradient productions *

* * @author Werner Randelshofer */ public class PaintCssConverter extends AbstractCssConverter { protected static final PaintableCssConverter paintableConverter = new PaintableCssConverter(false); public PaintCssConverter() { this(false); } public PaintCssConverter(boolean nullable) { super(nullable); } @Override public Paint parseNonNull(CssTokenizer tt, @Nullable IdResolver idResolver) throws ParseException, IOException { Paintable p = paintableConverter.parseNonNull(tt, idResolver); if (p.getPaint() == null) { throw new ParseException("paint", 0); } return p.getPaint(); } @Override public @Nullable String getHelpText() { return paintableConverter.getHelpText(); } @Override protected void produceTokensNonNull(TT value, @Nullable IdSupplier idSupplier, Consumer out) throws IOException { Paintable p = switch (value) { case Color color -> new CssColor(color); case LinearGradient linearGradient -> new CssLinearGradient(linearGradient); case RadialGradient radialGradient -> new CssRadialGradient(radialGradient); default -> throw new UnsupportedOperationException("unsupported value:" + value); }; paintableConverter.produceTokensNonNull(p, idSupplier, out); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy