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

org.fxmisc.wellbehaved.event.Nodes Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.fxmisc.wellbehaved.event;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javafx.collections.MapChangeListener;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;

import org.fxmisc.wellbehaved.event.InputMap.HandlerConsumer;

public class Nodes {

    private static final String P_INPUTMAP = "org.fxmisc.wellbehaved.event.inputmap";
    private static final String P_HANDLERS = "org.fxmisc.wellbehaved.event.handlers";

    public static void addInputMap(Node node, InputMap im) {
        init(node);
        setInputMap(node, InputMap.sequence(im, getInputMap(node)));
    }

    public static void addFallbackInputMap(Node node, InputMap im) {
        init(node);
        setInputMap(node, InputMap.sequence(getInputMap(node), im));
    }

    public static void removeInputMap(Node node, InputMap im) {
        setInputMap(node, getInputMap(node).without(im));
    }

    static InputMap getInputMap(Node node) {
        init(node);
        return (InputMap) node.getProperties().get(P_INPUTMAP);
    }

    private static void setInputMap(Node node, InputMap im) {
        node.getProperties().put(P_INPUTMAP, im);
    }

    private static void init(Node node) {
        if(node.getProperties().get(P_INPUTMAP) == null) {

            node.getProperties().put(P_INPUTMAP, InputMap.empty());
            node.getProperties().put(P_HANDLERS, new ArrayList>());

            MapChangeListener listener = ch -> {
                if(!P_INPUTMAP.equals(ch.getKey())) {
                    return;
                }

                getHandlers(node).forEach(entry -> {
                    node.removeEventHandler((EventType) entry.getKey(), (EventHandler) entry.getValue());
                });

                getHandlers(node).clear();

                InputMap inputMap = (InputMap) ch.getValueAdded();
                inputMap.forEachEventType(new HandlerConsumer() {

                    @Override
                    public  void accept(
                            EventType t, InputHandler h) {
                        node.addEventHandler(t, h);
                        getHandlers(node).add(new SimpleEntry<>(t, h));
                    }});
            };
            node.getProperties().addListener(listener);
        }
    }

    private static List, EventHandler>> getHandlers(Node node) {
        return (List, EventHandler>>) node.getProperties().get(P_HANDLERS);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy