Search in sources :

Example 41 with StateNode

use of com.vaadin.flow.internal.StateNode 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 42 with StateNode

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

the class HierarchicalCommunicatorTest method setUp.

@Before
public void setUp() {
    ui = Mockito.mock(UI.class);
    uiInternals = Mockito.mock(UIInternals.class);
    stateTree = Mockito.mock(StateTree.class);
    Mockito.when(ui.getInternals()).thenReturn(uiInternals);
    Mockito.when(uiInternals.getStateTree()).thenReturn(stateTree);
    treeData = new TreeData<>();
    treeData.addItems(null, ROOT);
    treeData.addItems(ROOT, FOLDER);
    treeData.addItems(FOLDER, LEAF);
    dataProvider = new TreeDataProvider<>(treeData);
    stateNode = Mockito.mock(StateNode.class);
    Mockito.when(stateNode.hasFeature(Mockito.any())).thenReturn(true);
    ComponentMapping mapping = Mockito.mock(ComponentMapping.class);
    Mockito.when(stateNode.getFeatureIfInitialized(ComponentMapping.class)).thenReturn(java.util.Optional.ofNullable(mapping));
    Mockito.when(mapping.getComponent()).thenReturn(java.util.Optional.of(new TestComponent()));
    communicator = new HierarchicalDataCommunicator<>(Mockito.mock(CompositeDataGenerator.class), arrayUpdater, json -> {
    }, stateNode, () -> null);
    communicator.setDataProvider(dataProvider, null);
}
Also used : Component(com.vaadin.flow.component.Component) JsonArray(elemental.json.JsonArray) HashMap(java.util.HashMap) StateTree(com.vaadin.flow.internal.StateTree) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) ArrayList(java.util.ArrayList) JsonValue(elemental.json.JsonValue) Tag(com.vaadin.flow.component.Tag) HierarchicalUpdate(com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) UI(com.vaadin.flow.component.UI) Before(org.junit.Before) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test) CompositeDataGenerator(com.vaadin.flow.data.provider.CompositeDataGenerator) Serializable(java.io.Serializable) Mockito(org.mockito.Mockito) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) JsonObject(elemental.json.JsonObject) UIInternals(com.vaadin.flow.component.internal.UIInternals) Assert(org.junit.Assert) ComponentMapping(com.vaadin.flow.internal.nodefeature.ComponentMapping) Assert.assertEquals(org.junit.Assert.assertEquals) StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) StateNode(com.vaadin.flow.internal.StateNode) UIInternals(com.vaadin.flow.component.internal.UIInternals) ComponentMapping(com.vaadin.flow.internal.nodefeature.ComponentMapping) Before(org.junit.Before)

Example 43 with StateNode

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

the class BasicElementStateProvider method createStateNode.

/**
 * Creates a compatible element state node using the given {@code tag}.
 *
 * @param tag
 *            the tag to use for the element
 * @return a initialized and compatible state node
 */
public static StateNode createStateNode(String tag) {
    assert ElementUtil.isValidTagName(tag) : "Invalid tag name " + tag;
    StateNode node = new StateNode(Collections.singletonList(ElementData.class), features);
    node.getFeature(ElementData.class).setTag(tag);
    return node;
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ElementData(com.vaadin.flow.internal.nodefeature.ElementData)

Example 44 with StateNode

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

the class ShadowRootStateProvider method createShadowRootNode.

/**
 * Create a new shadow root node for the given element {@code node}.
 *
 * @param node
 *            the node to create the shadow root for
 * @return the shadow root node
 */
public StateNode createShadowRootNode(StateNode node) {
    StateNode shadowRoot = new StateNode(getProviderFeatures());
    node.getFeature(ShadowRootData.class).setShadowRoot(shadowRoot);
    return shadowRoot;
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ShadowRootData(com.vaadin.flow.internal.nodefeature.ShadowRootData)

Example 45 with StateNode

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

the class MapPutChange method populateJson.

@Override
protected void populateJson(JsonObject json, ConstantPool constantPool) {
    // Set the type and key before calling super to make the keys appear in
    // a more logical order
    json.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_PUT);
    json.put(JsonConstants.CHANGE_MAP_KEY, key);
    super.populateJson(json, constantPool);
    if (value instanceof StateNode) {
        StateNode node = (StateNode) value;
        json.put(JsonConstants.CHANGE_PUT_NODE_VALUE, Json.create(node.getId()));
    } else {
        json.put(JsonConstants.CHANGE_PUT_VALUE, JsonCodec.encodeWithConstantPool(value, constantPool));
    }
}
Also used : StateNode(com.vaadin.flow.internal.StateNode)

Aggregations

StateNode (com.vaadin.flow.internal.StateNode)196 Test (org.junit.Test)122 Element (com.vaadin.flow.dom.Element)32 JsonObject (elemental.json.JsonObject)24 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)22 UI (com.vaadin.flow.component.UI)19 StateTree (com.vaadin.flow.internal.StateTree)18 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)18 ArrayList (java.util.ArrayList)18 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)17 Serializable (java.io.Serializable)17 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)16 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)10 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)9 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)9 VirtualChildrenList (com.vaadin.flow.internal.nodefeature.VirtualChildrenList)9 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)9 Bean (com.vaadin.flow.templatemodel.Bean)9 HashMap (java.util.HashMap)9 Collectors (java.util.stream.Collectors)9