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);
}
}
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());
}
}
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]))));
}
}
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);
}
Aggregations