Search in sources :

Example 1 with NodeFeature

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

the class StateNode method getMap.

/**
 * Gets the node map with the given id. Creates a new map if one doesn't
 * already exist.
 *
 * @param id
 *            the id of the map
 * @return the map with the given id
 */
public NodeMap getMap(int id) {
    Double key = Double.valueOf(id);
    NodeFeature feature = features.get(key);
    if (feature == null) {
        feature = new NodeMap(id, this);
        features.set(key, feature);
    }
    assert feature instanceof NodeMap;
    return (NodeMap) feature;
}
Also used : NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 2 with NodeFeature

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

the class StateNode method getList.

/**
 * Gets the node list with the given id. Creates a new node list if one
 * doesn't already exist.
 *
 * @param id
 *            the id of the list
 * @return the list with the given id
 */
public NodeList getList(int id) {
    Double key = Double.valueOf(id);
    NodeFeature feature = features.get(key);
    if (feature == null) {
        feature = new NodeList(id, this);
        features.set(key, feature);
    }
    assert feature instanceof NodeList;
    return (NodeList) feature;
}
Also used : NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) NodeList(com.vaadin.client.flow.nodefeature.NodeList)

Example 3 with NodeFeature

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

the class StateNodeTest method testGetListFeature.

@Test
public void testGetListFeature() {
    NodeList list = node.getList(1);
    Assert.assertEquals(1, list.getId());
    List<NodeFeature> features = collectFeatures();
    Assert.assertEquals(Arrays.asList(list), features);
    NodeList anotherList = node.getList(1);
    Assert.assertSame(anotherList, list);
    Assert.assertEquals(features, collectFeatures());
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) Test(org.junit.Test)

Example 4 with NodeFeature

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

the class StateNodeTest method testGetMapFeature.

@Test
public void testGetMapFeature() {
    NodeMap map = node.getMap(1);
    Assert.assertEquals(1, map.getId());
    List<NodeFeature> features = collectFeatures();
    Assert.assertEquals(Arrays.asList(map), features);
    NodeMap anotherMap = node.getMap(1);
    Assert.assertSame(anotherMap, map);
}
Also used : NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) Test(org.junit.Test)

Example 5 with NodeFeature

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

the class PolymerUtils method convertToJson.

/**
 * Makes an attempt to convert an object into json.
 *
 * @param object
 *            the object to convert to json
 * @return json from object, {@code null} for null
 */
public static JsonValue convertToJson(Object object) {
    if (object instanceof StateNode) {
        StateNode node = (StateNode) object;
        NodeFeature feature = null;
        if (node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
            feature = node.getMap(NodeFeatures.ELEMENT_PROPERTIES);
        } else if (node.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
            feature = node.getList(NodeFeatures.TEMPLATE_MODELLIST);
        } else if (node.hasFeature(NodeFeatures.BASIC_TYPE_VALUE)) {
            return convertToJson(node.getMap(NodeFeatures.BASIC_TYPE_VALUE).getProperty(NodeProperties.VALUE));
        }
        assert feature != null : "Don't know how to convert node without map or list features";
        JsonValue convert = feature.convert(PolymerUtils::convertToJson);
        if (convert instanceof JsonObject && !((JsonObject) convert).hasKey("nodeId")) {
            ((JsonObject) convert).put("nodeId", node.getId());
        }
        return convert;
    } else if (object instanceof MapProperty) {
        MapProperty property = (MapProperty) object;
        if (property.getMap().getId() == NodeFeatures.BASIC_TYPE_VALUE) {
            return convertToJson(property.getValue());
        } else {
            JsonObject convertedObject = Json.createObject();
            convertedObject.put(property.getName(), convertToJson(property.getValue()));
            return convertedObject;
        }
    } else {
        return WidgetUtil.crazyJsoCast(object);
    }
}
Also used : NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) StateNode(com.vaadin.client.flow.StateNode) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Aggregations

NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)5 NodeList (com.vaadin.client.flow.nodefeature.NodeList)2 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)2 Test (org.junit.Test)2 StateNode (com.vaadin.client.flow.StateNode)1 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)1 JsonObject (elemental.json.JsonObject)1 JsonValue (elemental.json.JsonValue)1