Java Code Examples for com.vaadin.flow.internal.StateNode#getFeature()

The following examples show how to use com.vaadin.flow.internal.StateNode#getFeature() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PolymerTemplateTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void updateOneOfModelValues() {
    String message = "message";
    TestPolymerTemplate template = new TestPolymerTemplate();
    ModelClass model = template.getModel();
    StateNode stateNode = template.getStateNode();

    model.setMessage(message);

    assertEquals(message, model.getMessage());
    assertNull(model.getTitle());

    Map<String, Object> expectedState = new HashMap<>();
    expectedState.put("message", message);
    expectedState.put("title", null);

    ElementPropertyMap modelMap = stateNode
            .getFeature(ElementPropertyMap.class);
    modelMap.getPropertyNames().forEach(key -> {
        assertTrue(expectedState.containsKey(key));
        assertEquals(expectedState.get(key), modelMap.getProperty(key));
    });
}
 
Example 2
Source File: MapSyncRpcHandler.java    From flow with Apache License 2.0 6 votes vote down vote up
private Serializable tryCopyStateNode(StateNode node,
        JsonObject properties) {
    if (node == null) {
        return properties;
    }

    // Copy only if the request is for a node inside a list
    if (isInList(node)) {
        StateNode copy = new StateNode(node);
        ElementPropertyMap originalProperties = node
                .getFeature(ElementPropertyMap.class);
        ElementPropertyMap copyProperties = copy
                .getFeature(ElementPropertyMap.class);
        originalProperties.getPropertyNames()
                .forEach(property -> copyProperties.setProperty(property,
                        originalProperties.getProperty(property)));
        return copy;
    }
    if (isProperty(node)) {
        return node;
    }
    return properties;
}
 
Example 3
Source File: PolymerTemplateTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void stateNodeIsInitialised() {
    TestPolymerTemplate template = new TestPolymerTemplate();
    StateNode stateNode = template.getStateNode();

    Map<String, Object> expectedState = new HashMap<>();
    expectedState.put("message", null);
    expectedState.put("title", null);

    assertTrue(stateNode.hasFeature(ElementPropertyMap.class));
    ElementPropertyMap modelMap = stateNode
            .getFeature(ElementPropertyMap.class);
    modelMap.getPropertyNames().forEach(key -> {
        assertTrue(expectedState.containsKey(key));
        assertEquals(expectedState.get(key), modelMap.getProperty(key));
    });
}
 
Example 4
Source File: MapSyncRpcHandlerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void syncJSON_jsonIsPropertyValueOfStateNode_propertySetToNode()
        throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);

    StateNode node = element.getNode();

    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node
            .getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);

    ElementPropertyMap modelMap = propertyMap.resolveModelMap("foo");
    // fake StateNode has been created for the model
    StateNode model = modelMap.getNode();
    modelMap.setProperty("bar", "baz");

    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", model.getId());

    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);

    Serializable testPropertyValue = propertyMap.getProperty("foo");

    Assert.assertTrue(testPropertyValue instanceof StateNode);

    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertSame(model, newNode);
}
 
Example 5
Source File: NodeValueEmptyRequiredFeatureTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    node = new StateNode(Arrays.asList(BasicTypeValue.class)) {
        @Override
        public boolean isAttached() {
            return true;
        }
    };
    nodeValue = node.getFeature(BasicTypeValue.class);
}
 
Example 6
Source File: VisibilityDataTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void setVisible() {
    StateNode node = new StateNode(ElementData.class);
    ElementData data = node.getFeature(ElementData.class);

    Assert.assertNull(data.get(NodeProperties.VISIBLE));
    Assert.assertTrue(data.isVisible());

    data.put(NodeProperties.VISIBLE, true);
    Assert.assertTrue(data.isVisible());

    data.put(NodeProperties.VISIBLE, false);
    Assert.assertFalse(data.isVisible());
}
 
Example 7
Source File: NodeListEmptyRequiredFeatureTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    node = new StateNode(Arrays.asList(ElementChildrenList.class)) {
        @Override
        public boolean isAttached() {
            return true;
        }
    };
    nodeList = node.getFeature(ElementChildrenList.class);
}
 
Example 8
Source File: NodeMapEmptyRequiredFeatureTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    node = new StateNode(Arrays.asList(ElementPropertyMap.class)) {
        @Override
        public boolean isAttached() {
            return true;
        }
    };
    nodeMap = node.getFeature(ElementPropertyMap.class);
}
 
Example 9
Source File: ElementTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private void assertClientStyleKey(String sentToClient,
        String setUsingStyleApi) {
    Element element = ElementFactory.createDiv();
    StateNode stateNode = element.getNode();
    ElementStylePropertyMap map = stateNode
            .getFeature(ElementStylePropertyMap.class);

    Style style = element.getStyle();
    style.set(setUsingStyleApi, "foo");
    Assert.assertEquals("foo", style.get(setUsingStyleApi));
    Assert.assertEquals(sentToClient, map.getPropertyNames().toArray()[0]);
    Assert.assertEquals("foo", map.getProperty(sentToClient));

}
 
Example 10
Source File: MapSyncRpcHandlerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void noSyncPropertiesFeature_noExplicitAllow_throws() {
    StateNode noSyncProperties = new StateNode(ElementPropertyMap.class);

    ElementPropertyMap map = noSyncProperties
            .getFeature(ElementPropertyMap.class);

    new MapSyncRpcHandler().handleNode(noSyncProperties,
            createSyncPropertyInvocation(noSyncProperties, TEST_PROPERTY,
                    NEW_VALUE));

    Assert.assertEquals(NEW_VALUE, map.getProperty(TEST_PROPERTY));
}
 
Example 11
Source File: AbstractBasicModelType.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public void createInitialValue(StateNode node, String property) {
    ElementPropertyMap feature = node.getFeature(ElementPropertyMap.class);
    if (!feature.hasProperty(property)) {
        feature.setProperty(property,
                (Serializable) modelToApplication(null));
    }
}
 
Example 12
Source File: BasicElementStateProvider.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(StateNode node, NodeVisitor visitor) {
    Element element = Element.get(node);
    ElementData data = node.getFeature(ElementData.class);
    JsonValue payload = data.getPayload();

    boolean visitDescendants;
    if (payload instanceof JsonObject) {
        JsonObject object = (JsonObject) payload;
        String type = object.getString(NodeProperties.TYPE);
        if (NodeProperties.IN_MEMORY_CHILD.equals(type)) {
            visitDescendants = visitor
                    .visit(NodeVisitor.ElementType.VIRTUAL, element);
        } else if (NodeProperties.INJECT_BY_ID.equals(type)
                || NodeProperties.TEMPLATE_IN_TEMPLATE.equals(type)) {
            visitDescendants = visitor.visit(
                    NodeVisitor.ElementType.VIRTUAL_ATTACHED, element);
        } else {
            throw new IllegalStateException(
                    "Unexpected payload type : " + type);
        }
    } else if (payload == null) {
        visitDescendants = visitor.visit(NodeVisitor.ElementType.REGULAR,
                element);
    } else {
        throw new IllegalStateException(
                "Unexpected payload in element data : " + payload.toJson());
    }

    if (visitDescendants) {
        visitDescendants(element, visitor);

        element.getShadowRoot().ifPresent(root -> root.accept(visitor));
    }
}
 
Example 13
Source File: BasicElementStateProvider.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public DomListenerRegistration addEventListener(StateNode node,
        String eventType, DomEventListener listener) {
    ElementListenerMap listeners = node
            .getFeature(ElementListenerMap.class);

    return listeners.add(eventType, listener);
}
 
Example 14
Source File: WebComponentConfigurationRegistry.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a partial copy of the element sub-tree, with the given
 * {@code rootElement} as the root element of the created tree. The copy
 * cares only about the HTML structure of the element and by-passes state
 * information where possible. The copying is done on element-level: tags,
 * attributes, and contents.
 * <p>
 * This is used create copies from elements which should be moved from
 * document head to each embedded web component on the page.
 * <p>
 * Copies the following
 * {@link com.vaadin.flow.internal.nodefeature.NodeFeature}:
 * <ul>
 * <li>{@link com.vaadin.flow.internal.nodefeature.ElementData}</li>
 * </ul>
 *
 * @param rootElement
 *            element to copy and make the root node of the new element tree
 * @return copy of the given {@code rootElement} with copied child hierarchy
 * @see com.vaadin.flow.dom.ElementUtil#fromJsoup(org.jsoup.nodes.Node) for
 *      the source of the elements being copied
 */
private static Element copyElementTree(Element rootElement) {
    // exception case for text node
    if (rootElement.getNode().hasFeature(TextNodeMap.class)) {
        return Element.createText(rootElement.getText());
    }

    StateNode copyNode = new StateNode(rootElement.getNode());
    // copy ElementData
    ElementData originalData = rootElement.getNode()
            .getFeature(ElementData.class);
    ElementData copyData = copyNode.getFeature(ElementData.class);
    copyData.setTag(originalData.getTag());
    copyData.setPayload(originalData.getPayload());
    copyData.setVisible(originalData.isVisible());

    Element copyElement = Element.get(copyNode);

    // copy relevant attributes
    rootElement.getAttributeNames().forEach(name -> copyElement
            .setAttribute(name, rootElement.getAttribute(name)));
    rootElement.getChildren().forEach(
            child -> copyElement.appendChild(copyElementTree(child)));

    // Element created from the copied StateNode
    return copyElement;
}
 
Example 15
Source File: MapSyncRpcHandlerTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test
public void syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy()
        throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);

    StateNode node = element.getNode();

    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node
            .getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);
    ModelList modelList = propertyMap.resolveModelList("foo");
    // fake StateNode has been created for the model
    StateNode item = new StateNode(ElementPropertyMap.class);
    modelList.add(item);
    item.getFeature(ElementPropertyMap.class).setProperty("bar", "baz");

    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", item.getId());

    // send sync request
    sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, json);

    // Now the model node should be copied and available as the
    // TEST_PROPERTY value
    Serializable testPropertyValue = propertyMap.getProperty(TEST_PROPERTY);

    Assert.assertTrue(testPropertyValue instanceof StateNode);

    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertNotEquals(item.getId(), newNode.getId());

    Assert.assertEquals("baz", newNode.getFeature(ElementPropertyMap.class)
            .getProperty("bar"));
}
 
Example 16
Source File: AbstractNodeFeatureTest.java    From flow with Apache License 2.0 4 votes vote down vote up
public static <T extends NodeFeature> T createFeature(
        Class<T> featureType) {
    StateNode node = new StateNode(featureType);

    return node.getFeature(featureType);
}
 
Example 17
Source File: AbstractNodeStateProvider.java    From flow with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the children data feature for the given node and asserts it is
 * non-null.
 *
 * @param node
 *            the node
 * @return the children feature
 */
private static ElementChildrenList getChildrenFeature(StateNode node) {
    return node.getFeature(ElementChildrenList.class);
}
 
Example 18
Source File: BasicElementStateProvider.java    From flow with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the property feature for the given node and asserts it is non-null.
 *
 * @param node
 *            the node
 * @return the property feature
 */
private static ElementPropertyMap getPropertyFeature(StateNode node) {
    return node.getFeature(ElementPropertyMap.class);
}
 
Example 19
Source File: BasicElementStateProvider.java    From flow with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the element attribute feature for the given node and asserts it is
 * non-null.
 *
 * @param node
 *            the node
 * @return the attribute feature
 */
private static ElementAttributeMap getAttributeFeature(StateNode node) {
    return node.getFeature(ElementAttributeMap.class);
}
 
Example 20
Source File: BasicElementStateProvider.java    From flow with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the element data feature for the given node and asserts it is
 * non-null.
 *
 * @param node
 *            the node
 * @return the data feature
 */
private static ElementData getDataFeature(StateNode node) {
    return node.getFeature(ElementData.class);
}