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

org.jhotdraw8.draw.render.SimpleRenderContext Maven / Gradle / Ivy

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

import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import javafx.scene.Node;
import org.jspecify.annotations.Nullable;
import org.jhotdraw8.draw.figure.Figure;
import org.jhotdraw8.fxcollection.typesafekey.Key;
import org.jhotdraw8.fxcollection.typesafekey.MapAccessor;
import org.jhotdraw8.fxcollection.typesafekey.NonNullMapAccessor;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class SimpleRenderContext implements WritableRenderContext {
    private final Map nodeMap = new HashMap<>();
    private final ObservableMap, Object> properties = FXCollections.observableHashMap();

    public SimpleRenderContext() {
    }

    @Override
    public Node getNode(Figure figure) {
        return nodeMap.computeIfAbsent(figure, f -> f.createNode(this));
    }

    public ObservableMap, Object> getProperties() {
        return properties;
    }

    @Override
    public  void set(MapAccessor key, @Nullable T value) {
        key.set(properties, value);
    }

    /**
     * Gets a property value.
     *
     * @param  the value type
     * @param key the key
     * @return the value
     */
    @Override
    public @Nullable  T get(MapAccessor key) {
        return key.get(getProperties());
    }

    /**
     * Gets a nonnull property value.
     *
     * @param  the value type
     * @param key the key
     * @return the value
     */
    @Override
    public  T getNonNull(NonNullMapAccessor key) {
        T value = key.get(getProperties());
        return Objects.requireNonNull(value, "value");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy