/** * Returns a {@link Set} of the selected options. If no options have been selected, then it returns an empty list. * <p> * As getValue calls {@link #getData()} for the currently selected options, it usually expects getData to return * null (for no selection) or a {@link List} of selected options. If the data returned by getData is not null and is * not a List, then setData will either (1) if the data is an array, convert the array to a List or (2) create a * List and add the data as the selected option. * </p> * <p> * getValue will verify the selected option/s are valid. If a selected option does not exist, then it will throw an * {@link IllegalArgumentException}. * </p> * * @return the selected options in the given UI context. */ @Override public Set<String> getValue() { // Convert data to a set (if necessary) Set<String> data = TreeItemUtil.convertDataToSet(getData()); if (data == null || data.isEmpty()) { return Collections.EMPTY_SET; } // TODO Similar to select lists, consider validating the selected items return data; }
@Test public void testConvertTreeToJsonToTree() { TreeItemIdNode tree = MockTreeItemData.TEST_MULTI_TREE; // Tree to JSON String json = TreeItemUtil.convertTreeToJson(tree); // JSON to Tree TreeItemIdNode tree2 = TreeItemUtil.convertJsonToTree(json); // Check tree the same Assert.assertTrue("Tree should be the same after conversion", TreeItemUtil.isTreeSame(tree, tree2)); }
@Test public void testCopyTree() { TreeItemIdNode copy = TreeItemUtil.copyTreeNode(MockTreeItemData.TEST_MULTI_TREE); Assert.assertTrue("Copy of tree should be the same", TreeItemUtil.isTreeSame(MockTreeItemData.TEST_MULTI_TREE, copy)); }
@Test public void testConvertJsonToTreeToJson() { // JSON to Tree TreeItemIdNode tree = TreeItemUtil.convertJsonToTree(MockTreeItemData.TEST_MULTI_JSON); // Tree back to JSON String json = TreeItemUtil.convertTreeToJson(tree); Assert.assertEquals("JSON should be the same after conversion", MockTreeItemData.TEST_MULTI_JSON, json); }
/** * @param json the json representing a custom tree structure */ public void setCustomTree(final String json) { TreeItemIdNode root = TreeItemUtil.convertJsonToTree(json); setCustomTree(root); }
/** * * @param tree1 the first tree to compare * @param tree2 the second tree to compare * @return true if the trees match */ public static boolean isTreeSame(final TreeItemIdNode tree1, final TreeItemIdNode tree2) { // Check IDs match if (!Util.equals(tree1.getItemId(), tree2.getItemId())) { return false; } // Check have same number of children if (tree1.getChildren().size() != tree2.getChildren().size()) { return false; } // Check child IDs match for (int i = 0; i < tree1.getChildren().size(); i++) { if (!isTreeSame(tree1.getChildren().get(i), tree2.getChildren().get(i))) { return false; } } return true; }
newTree = TreeItemUtil.convertJsonToTree(json); } catch (Exception e) { LOG.warn("Could not parse JSON for shuffle tree items. " + e.getMessage()); boolean changed = !TreeItemUtil.isTreeSame(newTree, currentTree);
/** * {@inheritDoc} */ @Override protected void initialiseComponentModel() { super.initialiseComponentModel(); // Copy the custom tree (if set) to allow the nodes to be updated per user TreeItemIdNode custom = getCustomTree(); if (custom != null) { TreeItemIdNode copy = TreeItemUtil.copyTreeNode(custom); setCustomTree(copy); } }
@Test public void testConvertTreeToJsonMulti() { String json = TreeItemUtil.convertTreeToJson(MockTreeItemData.TEST_MULTI_TREE); Assert.assertEquals("Invalid json returned for multiple rows", MockTreeItemData.TEST_MULTI_JSON, json); }
/** * @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); }
/** * @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; }
/** * {@inheritDoc} */ @Override public String getItemId(final List<Integer> row) { return TreeItemUtil.rowIndexListToString(row); }
/** * Get the example which is selected in the tree. * * @return an example data object. */ public final ExampleData getSelectedExampleData() { Set<String> allSelectedItems = getSelectedRows(); if (allSelectedItems == null || allSelectedItems.isEmpty()) { return null; } for (String selectedItem : allSelectedItems) { // Only interested in the first selected item as it is a single select list. List<Integer> rowIndex = TreeItemUtil.rowIndexStringToList(selectedItem); return getTreeModel().getExampleData(rowIndex); } return null; }
@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()); }
@Test public void testIsSameBasicTree() { Assert.assertTrue("Tree is same - Basic tree", TreeItemUtil.isTreeSame(MockTreeItemData.TEST_BASIC_TREE, MockTreeItemData.TEST_BASIC_TREE)); }
/** * @param node the node to copy * @return a copy of the node */ public static TreeItemIdNode copyTreeNode(final TreeItemIdNode node) { TreeItemIdNode copy = new TreeItemIdNode(node.getItemId()); copy.setHasChildren(node.hasChildren()); for (TreeItemIdNode childItem : node.getChildren()) { TreeItemIdNode childCopy = copyTreeNode(childItem); copy.addChild(childCopy); } return copy; }
@Test public void testConvertTreeToJsonBasic() { String json = TreeItemUtil.convertTreeToJson(MockTreeItemData.TEST_BASIC_TREE); Assert.assertEquals("Invalid json returned for basic tree", MockTreeItemData.TEST_BASIC_JSON, json); }
/** * @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); } } } }
/** * 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); } } }
@Test public void testConvertToSetWithSet() { Set<String> data = new HashSet<>(); data.add("A"); data.add("B"); Assert.assertEquals("Convert set value should be equal", CONVERT_RESULT, TreeItemUtil.convertDataToSet(data)); }