Search in sources :

Example 1 with StateTree

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

the class ClientDelegteHandlersTest method attach_noFeature.

@Test
public void attach_noFeature() {
    StateTree tree = new StateTree(new UI(), ElementChildrenList.class);
    StateNode stateNode = new StateNode(ClientDelegateHandlers.class);
    tree.getRootNode().getFeature(ElementChildrenList.class).add(stateNode);
    Assert.assertEquals(0, stateNode.getFeature(ClientDelegateHandlers.class).size());
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) StateNode(com.vaadin.flow.internal.StateNode) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Test(org.junit.Test)

Example 2 with StateTree

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

the class ClientDelegteHandlersTest method attach_noComponent.

@Test
public void attach_noComponent() {
    StateTree tree = new StateTree(new UI(), ElementChildrenList.class);
    StateNode stateNode = new StateNode(ComponentMapping.class, ClientDelegateHandlers.class);
    tree.getRootNode().getFeature(ElementChildrenList.class).add(stateNode);
    Assert.assertEquals(0, stateNode.getFeature(ClientDelegateHandlers.class).size());
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) StateNode(com.vaadin.flow.internal.StateNode) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Test(org.junit.Test)

Example 3 with StateTree

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

the class AttachExistingElementRpcHandler method handleNode.

@Override
protected Optional<Runnable> handleNode(StateNode node, JsonObject invocationJson) {
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_TAG_NAME);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_INDEX);
    int requestedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    int assignedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    String tag = invocationJson.getString(JsonConstants.RPC_ATTACH_TAG_NAME);
    int index = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_INDEX);
    AttachExistingElementFeature feature = node.getFeature(AttachExistingElementFeature.class);
    StateTree tree = (StateTree) node.getOwner();
    StateNode requestedNode = tree.getNodeById(requestedId);
    if (assignedId == -1) {
        // handle an error
        assert index == -1;
        ChildElementConsumer callback = feature.getCallback(requestedNode);
        assert callback != null;
        callback.onError(feature.getParent(requestedNode), tag, feature.getPreviousSibling(requestedNode));
        feature.unregister(requestedNode);
    } else {
        Element element = Element.get(tree.getNodeById(assignedId));
        attachElement(feature, element, index, tree.getNodeById(requestedId), requestedId == assignedId);
    }
    return Optional.empty();
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)

Example 4 with StateTree

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

the class MapSyncRpcHandler method tryConvert.

private Serializable tryConvert(Serializable value, StateNode context) {
    if (value instanceof JsonObject) {
        JsonObject json = (JsonObject) value;
        if (json.hasKey("nodeId")) {
            StateTree tree = (StateTree) context.getOwner();
            double id = json.getNumber("nodeId");
            StateNode stateNode = tree.getNodeById((int) id);
            return tryCopyStateNode(stateNode, json);
        }
    }
    return value;
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject)

Example 5 with StateTree

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

the class AttachTemplateChildRpcHandler method handleNode.

@Override
protected Optional<Runnable> handleNode(StateNode node, JsonObject invocationJson) {
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_ID);
    int requestedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    int assignedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    StateTree tree = (StateTree) node.getOwner();
    StateNode requestedNode = tree.getNodeById(requestedId);
    StateNode parent = tree.getNodeById(requestedId).getParent();
    JsonValue id = invocationJson.get(JsonConstants.RPC_ATTACH_ID);
    String tag = requestedNode.getFeature(ElementData.class).getTag();
    Logger logger = LoggerFactory.getLogger(AttachTemplateChildRpcHandler.class.getName());
    if (assignedId == -1) {
        logger.error("Attach existing element has failed because " + "the client-side element is not found");
        if (id instanceof JsonNull) {
            throw new IllegalStateException(String.format("The element with the tag name '%s' was " + "not found in the parent with id='%d'", tag, parent.getId()));
        } else {
            throw new IllegalStateException(String.format("The element with the tag name '%s' and id '%s' was " + "not found in the parent with id='%d'", tag, id.asString(), parent.getId()));
        }
    } else if (requestedId != assignedId) {
        logger.error("Attach existing element has failed because " + "the element has been already attached from the server side");
        if (id instanceof JsonNull) {
            throw new IllegalStateException(String.format("The element with the tag name '%s' is already " + "attached to the parent with id='%d'", tag, parent.getId()));
        } else {
            throw new IllegalStateException(String.format("The element with the tag name '%s' and id '%s' is " + "already attached to the parent with id='%d'", tag, id.asString(), parent.getId()));
        }
    } else {
        logger.error("Attach existing element request succeeded. " + "But the response about this is unexpected");
        // side should not respond
        throw new IllegalArgumentException("Unexpected successful attachment " + "response is received from the client-side. " + "Client side should not respond if everything is fine");
    }
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) StateNode(com.vaadin.flow.internal.StateNode) JsonValue(elemental.json.JsonValue) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) Logger(org.slf4j.Logger) JsonNull(elemental.json.JsonNull)

Aggregations

StateTree (com.vaadin.flow.internal.StateTree)32 Test (org.junit.Test)21 UI (com.vaadin.flow.component.UI)20 StateNode (com.vaadin.flow.internal.StateNode)18 JsonObject (elemental.json.JsonObject)10 Element (com.vaadin.flow.dom.Element)8 ArrayList (java.util.ArrayList)7 NodeChange (com.vaadin.flow.internal.change.NodeChange)6 UIInternals (com.vaadin.flow.component.internal.UIInternals)4 ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)4 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Component (com.vaadin.flow.component.Component)3 DomEvent (com.vaadin.flow.dom.DomEvent)3 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)3 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)3 JsonValue (elemental.json.JsonValue)3 NodeOwner (com.vaadin.flow.internal.NodeOwner)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2