Search in sources :

Example 1 with NodeOwner

use of com.vaadin.flow.internal.NodeOwner in project flow by vaadin.

the class AbstractComponentDataGenerator method registerRenderedComponent.

/**
 * Appends the component to the container and registers it for future use
 * during the lifecycle of the generator.
 *
 * @param itemKey
 *            the key of the model item
 * @param component
 *            the component to be attached to the container
 */
protected void registerRenderedComponent(String itemKey, Component component) {
    Element element = component.getElement();
    getContainer().appendChild(element);
    NodeOwner owner = getContainer().getNode().getOwner();
    UI containerUi = null;
    if (owner instanceof StateTree) {
        containerUi = ((StateTree) owner).getUI();
    }
    if (containerUi != null && component.getUI().isPresent() && containerUi != component.getUI().get()) {
        throw new IllegalStateException("The component '" + component.getClass() + "' is already attached to a UI instance which differs " + "from the conainer's UI instance. It means that the component instance is " + "reused instead being produced every time on 'createComponent' call." + " Check whether the component instance is a singleton or has inappropriate Spring scope.");
    }
    renderedComponents.put(itemKey, component);
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) NodeOwner(com.vaadin.flow.internal.NodeOwner) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element)

Example 2 with NodeOwner

use of com.vaadin.flow.internal.NodeOwner in project flow by vaadin.

the class DomEvent method extractElement.

static Element extractElement(JsonObject eventData, Element source, String key, boolean lookUnderUI) {
    assert key.startsWith(JsonConstants.MAP_STATE_NODE_EVENT_DATA);
    if (!eventData.hasKey(key)) {
        return null;
    }
    final JsonValue reportedStateNodeId = eventData.get(key);
    if (reportedStateNodeId == null) {
        return null;
    }
    int id = (int) reportedStateNodeId.asNumber();
    if (id == -1) {
        return null;
    }
    AtomicReference<Element> matchingNode = new AtomicReference<>();
    final Consumer<StateNode> visitor = node -> {
        if (node.getId() == id) {
            matchingNode.set(Element.get(node));
        }
    };
    // first look under event source
    source.getNode().visitNodeTree(visitor);
    if (lookUnderUI && matchingNode.get() == null) {
        // widen search to look under UI too
        final NodeOwner owner = source.getNode().getOwner();
        if (owner instanceof StateTree) {
            ((StateTree) owner).getRootNode().visitNodeTree(visitor);
        }
    }
    final Element mappedElementOrNull = matchingNode.get();
    // prevent spoofing invisible elements by sending bad state node ids
    if (mappedElementOrNull != null && !mappedElementOrNull.isVisible()) {
        return null;
    }
    return mappedElementOrNull;
}
Also used : Objects(java.util.Objects) Consumer(java.util.function.Consumer) NodeOwner(com.vaadin.flow.internal.NodeOwner) JsonValue(elemental.json.JsonValue) StateNode(com.vaadin.flow.internal.StateNode) JsonConstants(com.vaadin.flow.shared.JsonConstants) Optional(java.util.Optional) StateTree(com.vaadin.flow.internal.StateTree) JsonObject(elemental.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventObject(java.util.EventObject) StateTree(com.vaadin.flow.internal.StateTree) NodeOwner(com.vaadin.flow.internal.NodeOwner) JsonValue(elemental.json.JsonValue) StateNode(com.vaadin.flow.internal.StateNode) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 3 with NodeOwner

use of com.vaadin.flow.internal.NodeOwner in project flow by vaadin.

the class DefaultBindingExceptionHandler method getUI.

private UI getUI(Element element) {
    NodeOwner owner = element.getNode().getOwner();
    UI ui = null;
    if (element.getNode().isAttached()) {
        ui = ((StateTree) owner).getUI();
    }
    if (ui == null) {
        ui = UI.getCurrent();
    }
    return ui;
}
Also used : NodeOwner(com.vaadin.flow.internal.NodeOwner) UI(com.vaadin.flow.component.UI)

Aggregations

NodeOwner (com.vaadin.flow.internal.NodeOwner)3 UI (com.vaadin.flow.component.UI)2 StateTree (com.vaadin.flow.internal.StateTree)2 Element (com.vaadin.flow.dom.Element)1 StateNode (com.vaadin.flow.internal.StateNode)1 JsonConstants (com.vaadin.flow.shared.JsonConstants)1 JsonObject (elemental.json.JsonObject)1 JsonValue (elemental.json.JsonValue)1 EventObject (java.util.EventObject)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (java.util.function.Consumer)1