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

org.jhotdraw8.draw.key.Scale3DStyleableMapAccessor Maven / Gradle / Ivy

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

import javafx.geometry.Point3D;
import org.jhotdraw8.base.converter.Converter;
import org.jhotdraw8.draw.css.converter.Scale3DCssConverter;
import org.jhotdraw8.fxcollection.typesafekey.Key;
import org.jhotdraw8.fxcollection.typesafekey.MapAccessor;
import org.jhotdraw8.fxcollection.typesafekey.NonNullMapAccessor;
import org.jhotdraw8.icollection.immutable.ImmutableMap;
import org.jspecify.annotations.Nullable;

import java.util.Map;

/**
 * Scale3DStyleableMapAccessor.
 *
 * @author Werner Randelshofer
 */
public class Scale3DStyleableMapAccessor extends AbstractStyleableMapAccessor
        implements NonNullMapAccessor {


    private final MapAccessor xKey;
    private final MapAccessor yKey;
    private final MapAccessor zKey;
    private final Converter converter;

    /**
     * Creates a new instance with the specified name.
     *
     * @param name the name of the accessor
     * @param xKey the key for the x coordinate of the point
     * @param yKey the key for the y coordinate of the point
     * @param zKey the key for the u coordinate of the point
     */
    public Scale3DStyleableMapAccessor(String name, MapAccessor xKey, MapAccessor yKey, MapAccessor zKey) {
        this(name, xKey, yKey, zKey, new Scale3DCssConverter(false));
    }

    /**
     * Creates a new instance with the specified name.
     *
     * @param name      the name of the accessor
     * @param xKey      the key for the x coordinate of the point
     * @param yKey      the key for the y coordinate of the point
     * @param zKey      the key for the u coordinate of the point
     * @param converter String converter for the scale factor with 3 coordinates (x-factor, y-factor, z-factor).
     */
    public Scale3DStyleableMapAccessor(String name, MapAccessor xKey, MapAccessor yKey, MapAccessor zKey, Converter converter) {
        super(name, Point3D.class, new MapAccessor[]{xKey, yKey, zKey}, new Point3D(xKey.getDefaultValue(), yKey.getDefaultValue(), zKey.getDefaultValue()));
        this.converter = converter;

        this.xKey = xKey;
        this.yKey = yKey;
        this.zKey = zKey;
    }

    @Override
    public Point3D get(Map, Object> a) {
        Double x = xKey.get(a);
        Double y = yKey.get(a);
        Double z = zKey.get(a);
        return new Point3D(x == null ? 0 : x, y == null ? 0 : y, z == null ? 0 : z);
    }

    @Override
    public ImmutableMap, Object> put(ImmutableMap, Object> a, @Nullable Point3D value) {
        if (value == null) {
            return remove(a);
        } else {
            a = xKey.put(a, value.getX());
            a = yKey.put(a, value.getY());
            return zKey.put(a, value.getZ());
        }
    }

    @Override
    public Converter getCssConverter() {
        return converter;
    }

    @Override
    public Point3D remove(Map, Object> a) {
        Point3D oldValue = get(a);
        xKey.remove(a);
        yKey.remove(a);
        zKey.remove(a);
        return oldValue;
    }

    @Override
    public ImmutableMap, Object> remove(ImmutableMap, Object> a) {
        a = xKey.remove(a);
        a = yKey.remove(a);
        return zKey.remove(a);
    }

    @Override
    public void set(Map, Object> a, @Nullable Point3D value) {
        if (value == null) {
            remove(a);
        } else {
            xKey.put(a, value.getX());
            yKey.put(a, value.getY());
            zKey.put(a, value.getZ());
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy