use of com.vaadin.client.flow.StateTree in project flow by vaadin.
the class MapProperty method syncToServer.
/**
* Sets the value of this property and synchronizes the value to the server.
*
* @param newValue
* the new value to set.
*/
public void syncToServer(Object newValue) {
Object currentValue = hasValue() ? getValue() : null;
if (Objects.equals(newValue, currentValue)) {
// in case we are here with the same value that has been set from
// the server then we unlock client side updates already here via
// unmarking the server update flag. It allows another client side
// potential change for the same property being propagated to the
// server once the server value is set successfully (e.g. mutation
// the same property from its observer).
isServerUpdate = false;
}
if (!(Objects.equals(newValue, currentValue) && hasValue()) && !isServerUpdate) {
StateNode node = getMap().getNode();
StateTree tree = node.getTree();
if (tree.isActive(node)) {
doSetValue(newValue);
tree.sendNodePropertySyncToServer(this);
} else {
/*
* Fire an fake event to reset the property value back in the
* DOM element: we don't know how exactly set this property but
* it has to be set to the property value because of listener
* added to the property during binding.
*/
eventRouter.fireEvent(new MapPropertyChangeEvent(this, currentValue, currentValue));
// Flush is needed because we are out of normal lifecycle which
// call the flush() automatically.
Reactive.flush();
}
}
}
use of com.vaadin.client.flow.StateTree in project flow by vaadin.
the class GwtTemplateBinderTest method gwtSetUp.
@Override
protected void gwtSetUp() throws Exception {
super.gwtSetUp();
registry = new Registry() {
{
set(TemplateRegistry.class, new TemplateRegistry());
set(ConstantPool.class, new ConstantPool());
set(ExistingElementMap.class, new ExistingElementMap());
set(InitialPropertiesHandler.class, new InitialPropertiesHandler(this));
set(StateTree.class, new StateTree(this) {
@Override
public void sendTemplateEventToServer(StateNode node, String methodName, JsArray<?> argValues) {
serverMethods.put(methodName, argValues);
serverRpcNodes.put(methodName, node);
}
});
}
};
tree = registry.getStateTree();
/**
* This state node is ALWAYS a template !!!
*/
stateNode = new StateNode(0, tree) {
@Override
public boolean hasFeature(int id) {
if (id == NodeFeatures.TEMPLATE) {
return true;
}
return super.hasFeature(id);
}
};
// Use the most common model structure by default
JsonObject modelType = Json.createObject();
modelType.put(MODEL_KEY, "String");
setModelType(stateNode, modelType);
}
use of com.vaadin.client.flow.StateTree in project flow by vaadin.
the class AbstractTemplateStrategy method createAndBind.
/**
* Creates and binds a DOM node for the given state node and
* {@code templateId} using binder {@code context}.
*
* @param modelNode
* the state node with model data for which to create a DOM node,
* not <code>null</code>
* @param templateId
* template id
* @param context
* binder context
*
* @return the DOM node, not <code>null</code>
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected static Node createAndBind(StateNode modelNode, int templateId, TemplateBinderContext context) {
JsArray<AbstractTemplateStrategy> strategies = context.getStrategies(strategy -> strategy instanceof AbstractTemplateStrategy<?>);
StateTree tree = modelNode.getTree();
for (int i = 0; i < strategies.length(); i++) {
AbstractTemplateStrategy strategy = strategies.get(i);
if (strategy.isApplicable(tree, templateId)) {
Node domNode = strategy.create(tree, templateId);
strategy.bind(modelNode, domNode, templateId, context);
return domNode;
}
}
throw new IllegalArgumentException("Unsupported template type: " + getTemplateNode(tree, templateId).getType());
}
use of com.vaadin.client.flow.StateTree in project flow by vaadin.
the class ClientJsonCodecTest method decodeStateNode_node.
@Test
public void decodeStateNode_node() {
StateTree tree = new StateTree(null);
StateNode node = new StateNode(43, tree);
tree.registerNode(node);
JsElement element = new JsElement() {
};
node.setDomNode(element);
JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId()));
StateNode decoded = ClientJsonCodec.decodeStateNode(tree, json);
Assert.assertSame(node, decoded);
}
use of com.vaadin.client.flow.StateTree in project flow by vaadin.
the class NodeMapTest method getProperty_returnPropertyWhichAlwaysUpdatesTheValue.
@Test
public void getProperty_returnPropertyWhichAlwaysUpdatesTheValue() {
NodeMap map = new NodeMap(NodeFeatures.ELEMENT_PROPERTIES, new StateNode(0, new StateTree(null)));
MapProperty property = map.getProperty("innerHTML");
AtomicReference<MapPropertyChangeEvent> capture = new AtomicReference<>();
property.addChangeListener(capture::set);
property.setValue("foo");
Assert.assertNotNull(capture.get());
// reset
capture.set(null);
// set the same value again
property.setValue("foo");
Assert.assertNotNull(capture.get());
}
Aggregations