Search in sources :

Example 1 with TreeItemIdNode

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

the class TreeItemUtil_Test method testConvertJsonToTreeToJson.

@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);
}
Also used : TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) Test(org.junit.Test)

Example 2 with TreeItemIdNode

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

the class TreeItemUtil_Test method testConvertTreeToJsonToTree.

@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));
}
Also used : TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) Test(org.junit.Test)

Example 3 with TreeItemIdNode

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

the class WTreeRenderer method handlePaintCustom.

/**
 * Paint the custom tree layout.
 *
 * @param tree the WTree to render
 * @param xml the XML string builder
 */
protected void handlePaintCustom(final WTree tree, final XmlStringBuilder xml) {
    TreeItemModel model = tree.getTreeModel();
    TreeItemIdNode root = tree.getCustomTree();
    Set<String> selectedRows = new HashSet(tree.getSelectedRows());
    Set<String> expandedRows = new HashSet(tree.getExpandedRows());
    WTree.ExpandMode mode = tree.getExpandMode();
    // Process root nodes
    for (TreeItemIdNode node : root.getChildren()) {
        paintCustomItem(tree, mode, model, node, xml, selectedRows, expandedRows);
    }
}
Also used : WTree(com.github.bordertech.wcomponents.WTree) TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) TreeItemModel(com.github.bordertech.wcomponents.TreeItemModel) HashSet(java.util.HashSet)

Example 4 with TreeItemIdNode

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

the class WTreeRenderer method handleOpenItemRequest.

/**
 * Paint the item that was on the open request.
 *
 * @param tree the WTree to render
 * @param xml the XML string builder
 * @param itemId the item id to open
 */
protected void handleOpenItemRequest(final WTree tree, final XmlStringBuilder xml, final String itemId) {
    TreeItemModel model = tree.getTreeModel();
    Set<String> selectedRows = new HashSet(tree.getSelectedRows());
    WTree.ExpandMode mode = tree.getExpandMode();
    // Only want to render the children of the "open item" so only include the open item id in the expanded rows
    Set<String> expandedRows = new HashSet();
    expandedRows.add(itemId);
    if (tree.getCustomTree() == null) {
        List<Integer> rowIndex = tree.getExpandedItemIdIndexMap().get(itemId);
        paintItem(tree, mode, model, rowIndex, xml, selectedRows, expandedRows);
    } else {
        TreeItemIdNode node = tree.getCustomIdNodeMap().get(itemId);
        paintCustomItem(tree, mode, model, node, xml, selectedRows, expandedRows);
    }
}
Also used : WTree(com.github.bordertech.wcomponents.WTree) TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) TreeItemModel(com.github.bordertech.wcomponents.TreeItemModel) HashSet(java.util.HashSet)

Example 5 with TreeItemIdNode

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

the class WTreeRenderer method paintCustomItem.

/**
 * Iterate of over the nodes to render the custom layout of the tree items.
 *
 * @param tree the WTree to render
 * @param mode the expand mode
 * @param model the tree model
 * @param node the current node in the custom tree layout
 * @param xml the XML string builder
 * @param selectedRows the set of selected rows
 * @param expandedRows the set of expanded rows
 */
protected void paintCustomItem(final WTree tree, final WTree.ExpandMode mode, final TreeItemModel model, final TreeItemIdNode node, final XmlStringBuilder xml, final Set<String> selectedRows, final Set<String> expandedRows) {
    String itemId = node.getItemId();
    List<Integer> rowIndex = tree.getRowIndexForCustomItemId(itemId);
    boolean selected = selectedRows.remove(itemId);
    boolean expandable = node.hasChildren();
    boolean expanded = expandedRows.remove(itemId);
    TreeItemImage image = model.getItemImage(rowIndex);
    String url = null;
    if (image != null) {
        url = tree.getItemImageUrl(image, itemId);
    }
    xml.appendTagOpen("ui:treeitem");
    xml.appendAttribute("id", tree.getItemIdPrefix() + itemId);
    xml.appendAttribute("label", model.getItemLabel(rowIndex));
    xml.appendOptionalUrlAttribute("imageUrl", url);
    xml.appendOptionalAttribute("selected", selected, "true");
    xml.appendOptionalAttribute("expandable", expandable, "true");
    xml.appendOptionalAttribute("open", expandable && expanded, "true");
    xml.appendClose();
    // Paint child items
    if (expandable && (mode == WTree.ExpandMode.CLIENT || expanded)) {
        for (TreeItemIdNode childNode : node.getChildren()) {
            paintCustomItem(tree, mode, model, childNode, xml, selectedRows, expandedRows);
        }
    }
    xml.appendEndTag("ui:treeitem");
}
Also used : TreeItemIdNode(com.github.bordertech.wcomponents.TreeItemIdNode) TreeItemImage(com.github.bordertech.wcomponents.TreeItemImage)

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