Search in sources :

Example 1 with NodeFeature

use of com.vaadin.flow.internal.nodefeature.NodeFeature in project flow by vaadin.

the class StateNode method addFeature.

private void addFeature(Class<? extends NodeFeature> featureType) {
    if (!features.containsKey(featureType)) {
        NodeFeature feature = NodeFeatureRegistry.create(featureType, this);
        features.put(featureType, feature);
    }
}
Also used : NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature)

Example 2 with NodeFeature

use of com.vaadin.flow.internal.nodefeature.NodeFeature in project flow by vaadin.

the class StateNode method collectChanges.

/**
 * Collects all changes made to this node since the last time
 * {@link #collectChanges(Consumer)} has been called. If the node is
 * recently attached, then the reported changes will be relative to a newly
 * created node.
 *
 * @param collector
 *            a consumer accepting node changes
 */
public void collectChanges(Consumer<NodeChange> collector) {
    boolean isAttached = isAttached();
    if (isAttached != wasAttached) {
        if (isAttached) {
            collector.accept(new NodeAttachChange(this));
            // Make all changes show up as if the node was recently attached
            clearChanges();
            getFeatures().values().forEach(NodeFeature::generateChangesFromEmpty);
        } else {
            collector.accept(new NodeDetachChange(this));
        }
        wasAttached = isAttached;
    }
    if (!isAttached()) {
        return;
    }
    if (isInactive()) {
        if (!isInactiveSelf) {
            /*
                 * We are here if: the node itself is not inactive but it has
                 * some ascendant which is inactive.
                 *
                 * In this case we send only some subset of changes (not from
                 * all the features). But we should send changes for all
                 * remaining features. Normally it automatically happens if the
                 * node becomes "visible". But if it was visible with some
                 * invisible parent then only the parent becomes dirty (when
                 * it's set visible) and this child will never participate in
                 * collection of changes since it's not marked as dirty.
                 *
                 * So here such node (which is active itself but its ascendant
                 * is inactive) we mark as dirty again to be able to collect its
                 * changes later on when its ascendant becomes active.
                 */
            getOwner().markAsDirty(this);
        }
        if (isInitialChanges) {
            // send only required (reported) features updates
            Stream<NodeFeature> initialFeatures = Stream.concat(getFeatures().entrySet().stream().filter(entry -> isReportedFeature(entry.getKey())).map(Entry::getValue), getDisalowFeatures());
            doCollectChanges(collector, initialFeatures);
        } else {
            doCollectChanges(collector, getDisalowFeatures());
        }
    } else {
        doCollectChanges(collector, getFeatures().values().stream());
    }
}
Also used : NodeDetachChange(com.vaadin.flow.internal.change.NodeDetachChange) NodeAttachChange(com.vaadin.flow.internal.change.NodeAttachChange) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature)

Example 3 with NodeFeature

use of com.vaadin.flow.internal.nodefeature.NodeFeature in project flow by vaadin.

the class TemplateElementStateProviderTest method requiredNodeFeatures.

@Test
@SuppressWarnings("unchecked")
public void requiredNodeFeatures() {
    Class<? extends NodeFeature>[] requiredFeatures = new Class[] { TemplateOverridesMap.class };
    TemplateElementStateProvider provider = (TemplateElementStateProvider) createElement("<div></div").getStateProvider();
    // Test that a node with all required features is accepted
    Assert.assertTrue(provider.supports(new StateNode(requiredFeatures)));
    // Test that removing any feature makes it non-accepted
    for (int i = 0; i < requiredFeatures.length; i++) {
        List<Class<? extends NodeFeature>> list = new ArrayList<>(Arrays.asList(requiredFeatures));
        list.remove(i);
        Assert.assertFalse(provider.supports(new StateNode(list.toArray(new Class[0]))));
    }
}
Also used : TemplateOverridesMap(com.vaadin.flow.internal.nodefeature.TemplateOverridesMap) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature) StateNode(com.vaadin.flow.internal.StateNode) ArrayList(java.util.ArrayList) TemplateElementStateProvider(com.vaadin.flow.dom.impl.TemplateElementStateProvider) Test(org.junit.Test)

Example 4 with NodeFeature

use of com.vaadin.flow.internal.nodefeature.NodeFeature in project flow by vaadin.

the class StateTreeTest method testSerializable.

@Test
public void testSerializable() {
    @SuppressWarnings("unchecked") Class<? extends NodeFeature>[] features = new Class[] { ElementChildrenList.class, ElementData.class, ElementAttributeMap.class, ElementPropertyMap.class };
    StateTree tree = new StateTree(new UI(), features);
    StateNode root = tree.getRootNode();
    root.getFeature(ElementData.class).setTag("body");
    StateNode child = new StateNode(features);
    root.getFeature(ElementChildrenList.class).add(0, child);
    child.getFeature(ElementData.class).setTag(Tag.DIV);
    byte[] serialized = SerializationUtils.serialize(tree);
    StateTree d1 = (StateTree) SerializationUtils.deserialize(serialized);
    Assert.assertNotNull(d1);
}
Also used : UI(com.vaadin.flow.component.UI) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) ElementAttributeMap(com.vaadin.flow.internal.nodefeature.ElementAttributeMap) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Aggregations

NodeFeature (com.vaadin.flow.internal.nodefeature.NodeFeature)4 Test (org.junit.Test)2 UI (com.vaadin.flow.component.UI)1 TemplateElementStateProvider (com.vaadin.flow.dom.impl.TemplateElementStateProvider)1 StateNode (com.vaadin.flow.internal.StateNode)1 NodeAttachChange (com.vaadin.flow.internal.change.NodeAttachChange)1 NodeDetachChange (com.vaadin.flow.internal.change.NodeDetachChange)1 ElementAttributeMap (com.vaadin.flow.internal.nodefeature.ElementAttributeMap)1 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)1 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)1 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)1 TemplateOverridesMap (com.vaadin.flow.internal.nodefeature.TemplateOverridesMap)1 ArrayList (java.util.ArrayList)1