/** * Case insensitive comparison of labels of two tree items. * * @param item1 First tree item. * @param item2 Second tree item. * @return Result of the comparison. */ private static int compare(Treenode item1, Treenode item2) { String label1 = item1.getLabel(); String label2 = item2.getLabel(); return label1 == label2 ? 0 : label1 == null ? -1 : label2 == null ? -1 : label1.compareToIgnoreCase(label2); }
@Override public boolean isMatch(Treenode item, String text) { String label = item.getLabel(); return label != null && label.toLowerCase().contains(text.toLowerCase()); }
/** * Search the entire tree for a tree item matching the specified label. * * @param tree Tree containing the item of interest. * @param label Label to match. * @param caseSensitive If true, match is case-sensitive. * @return The matching tree item, or null if not found. */ public static Treenode findNodeByLabel(Treeview tree, String label, boolean caseSensitive) { for (Treenode item : tree.getChildren(Treenode.class)) { if (caseSensitive ? label.equals(item.getLabel()) : label.equalsIgnoreCase(item.getLabel())) { return item; } } return null; }
/** * Returns the path of the specified tree node. This consists of the indexes or labels of this * and all parent nodes separated by a "\" character. * * @param item The node (tree item) whose path is to be returned. If this value is null, a zero * length string is returned. * @param useLabels If true, use the labels as identifiers; otherwise, use indexes. * @return The path of the node as described. */ public static String getPath(Treenode item, boolean useLabels) { StringBuilder sb = new StringBuilder(); boolean needsDelim = false; while (item != null) { if (needsDelim) { sb.insert(0, '\\'); } else { needsDelim = true; } sb.insert(0, useLabels ? item.getLabel() : item.getIndex()); item = (Treenode) item.getParent(); } return sb.toString(); }
/** * Perform a search. */ @EventHandler(value = "click", target = "btnSearch") private void onClick$btnSearch() { txtSearch.setFocus(true); lblNotFound.setVisible(false); String text = txtSearch.getValue(); tree.setSelectedNode(null); if (text != null && !text.isEmpty()) { text = text.toLowerCase(); iterator = iterator == null ? tree.iterator() : iterator; while (iterator.hasNext()) { Treenode node = iterator.next(); if (node.getLabel().toLowerCase().contains(text)) { node.makeVisible(); node.setSelected(true); return; } } iterator = null; lblNotFound.setVisible(true); } }
/** * Place the specified tree node in edit mode. In this mode, the node's label is replaced with a * text box containing the label's value. * * @param node Target tree node. */ private void editNodeStart(Treenode node) { txtLabel.setAttribute(ITEM_ATTR, node); if (node == null) { popLabel.close(); } else { String label = node.getLabel(); node.setAttribute(LABEL_ATTR, label); txtLabel.setValue(label); node.setLabel(null); popLabel.open(node, "left top", "left top"); txtLabel.setFocus(true); } }