org.jhotdraw8.draw.key.Scale3DStyleableMapAccessor 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!
/*
* @(#)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 super Key>, 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 super Key>, 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 super Key>, 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());
}
}
}