Search in sources :

Example 6 with TreeItemIdNode

use of com.github.bordertech.wcomponents.TreeItemIdNode in project wcomponents by BorderTech.

the class TreeItemUtil method convertJsonToTree.

/**
 * @param jsonString the string of JSON to convert to a custom tree of nodes
 * @return the custom tree structure of item ids
 */
public static TreeItemIdNode convertJsonToTree(final String jsonString) {
    TreeItemIdNode root = new TreeItemIdNode(null);
    if (Util.empty(jsonString)) {
        return root;
    }
    JsonParser parser = new JsonParser();
    JsonObject json = parser.parse(jsonString).getAsJsonObject();
    JsonArray children = json.getAsJsonArray("root");
    if (children != null) {
        for (int i = 0; i < children.size(); i++) {
            JsonObject child = children.get(i).getAsJsonObject();
            processJsonToTree(root, child);
        }
    }
    return root;
}
Also used : JsonArray(com.google.gson.JsonArray) TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 7 with TreeItemIdNode

use of com.github.bordertech.wcomponents.TreeItemIdNode in project wcomponents by BorderTech.

the class TreeItemUtil method processTreeToJson.

/**
 * @param jsonArray the JSON array holding the node
 * @param node the node being processed
 */
private static void processTreeToJson(final JsonArray jsonArray, final TreeItemIdNode node) {
    // Add node
    JsonObject jsonNode = new JsonObject();
    jsonNode.addProperty("id", node.getItemId());
    jsonArray.add(jsonNode);
    // Process children
    if (node.hasChildren()) {
        if (node.getChildren().isEmpty()) {
            jsonNode.addProperty("expandable", Boolean.TRUE);
        } else {
            JsonArray itemArray = new JsonArray();
            jsonNode.add("items", itemArray);
            for (TreeItemIdNode child : node.getChildren()) {
                processTreeToJson(itemArray, child);
            }
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) JsonObject(com.google.gson.JsonObject)

Example 8 with TreeItemIdNode

use of com.github.bordertech.wcomponents.TreeItemIdNode in project wcomponents by BorderTech.

the class TreeItemUtil method processJsonToTree.

/**
 * Iterate over the JSON objects to create the tree structure.
 *
 * @param parentNode the parent node
 * @param json the current JSON object
 */
private static void processJsonToTree(final TreeItemIdNode parentNode, final JsonObject json) {
    String id = json.getAsJsonPrimitive("id").getAsString();
    JsonPrimitive expandableJson = json.getAsJsonPrimitive("expandable");
    TreeItemIdNode node = new TreeItemIdNode(id);
    if (expandableJson != null && expandableJson.getAsBoolean()) {
        node.setHasChildren(true);
    }
    parentNode.addChild(node);
    JsonArray children = json.getAsJsonArray("items");
    if (children != null) {
        for (int i = 0; i < children.size(); i++) {
            JsonObject child = children.get(i).getAsJsonObject();
            processJsonToTree(node, child);
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) JsonObject(com.google.gson.JsonObject)

Example 9 with TreeItemIdNode

use of com.github.bordertech.wcomponents.TreeItemIdNode in project wcomponents by BorderTech.

the class TreeItemUtil method convertTreeToJson.

/**
 * @param root the root tree item node
 * @return the tree item node as a JSON string
 */
public static String convertTreeToJson(final TreeItemIdNode root) {
    JsonObject json = new JsonObject();
    if (root.hasChildren()) {
        JsonArray rootArray = new JsonArray();
        json.add("root", rootArray);
        // Process nodes
        for (TreeItemIdNode child : root.getChildren()) {
            processTreeToJson(rootArray, child);
        }
    }
    Gson gson = new Gson();
    return gson.toJson(json);
}
Also used : JsonArray(com.google.gson.JsonArray) TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 10 with TreeItemIdNode

use of com.github.bordertech.wcomponents.TreeItemIdNode in project wcomponents by BorderTech.

the class TreeItemUtil_Test method testConvertJsonToTreeEmptyJson.

@Test
public void testConvertJsonToTreeEmptyJson() {
    TreeItemIdNode root = TreeItemUtil.convertJsonToTree("{}");
    Assert.assertNotNull("No json - Root node not returned", root);
    Assert.assertTrue("No json - Root should have no children", root.getChildren().isEmpty());
}
Also used : TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) Test(org.junit.Test)

Aggregations

TreeItemIdNode (com.github.bordertech.wcomponents.TreeItemIdNode)17 Test (org.junit.Test)8 JsonArray (com.google.gson.JsonArray)4 JsonObject (com.google.gson.JsonObject)4 WTree (com.github.bordertech.wcomponents.WTree)3 TreeItemModel (com.github.bordertech.wcomponents.TreeItemModel)2 HashSet (java.util.HashSet)2 TreeItemImage (com.github.bordertech.wcomponents.TreeItemImage)1 Gson (com.google.gson.Gson)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1