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