@NotNull private ConfigCheckedTreeNode insertBefore(CheckedTreeNode parent, TreeNode child, @NotNull List<MethodConfig> methodConfigs, @NotNull MethodConfig methodConfig, TreeNodeType nodeType) { ConfigCheckedTreeNode newNode = createNode(methodConfigs, methodConfig, nodeType); for (int i = 0; i < parent.getChildCount(); i++) { if (Objects.equals(parent.getChildAt(i).toString(), child.toString())) { model.insertNodeInto(newNode, parent, i); return newNode; } } throw new RuntimeException("Cannot insert new node"); }
@Nullable private ConfigCheckedTreeNode maybeInsertOrFind(CheckedTreeNode parent, @NotNull List<MethodConfig> methodConfigs, @NotNull MethodConfig methodConfig, TreeNodeType nodeType) { String name = getName(methodConfig, nodeType); if (name.compareTo(parent.getChildAt(0).toString()) < 0) { return insertBefore(parent, parent.getChildAt(0), methodConfigs, methodConfig, nodeType); } for (int i = 0; i < parent.getChildCount(); i++) { ConfigCheckedTreeNode child = (ConfigCheckedTreeNode) parent.getChildAt(i); TreeNode nextNode = parent.getChildAfter(child); if (nextNode != null) { if (name.compareTo(child.toString()) > 0 && name.compareTo(nextNode.toString()) < 0) { return insertBefore(parent, nextNode, methodConfigs, methodConfig, nodeType); } } if (Objects.equals(child.toString(), name)) { return child; } } return null; }
@NotNull private ConfigCheckedTreeNode insertNode(CheckedTreeNode parent, @NotNull List<MethodConfig> methodConfigs, @NotNull MethodConfig methodConfig, TreeNodeType nodeType) { if (parent.getChildCount() == 0) { return appendNode(parent, methodConfig, nodeType, methodConfigs); } String name = getName(methodConfig, nodeType); if (name.compareTo(parent.getChildAt(0).toString()) < 0) { return insertBefore(parent, parent.getChildAt(0), methodConfigs, methodConfig, nodeType); } for (int i = 0; i < parent.getChildCount(); i++) { ConfigCheckedTreeNode child = (ConfigCheckedTreeNode) parent.getChildAt(i); TreeNode nextNode = parent.getChildAfter(child); if (nextNode != null) { if (name.compareTo(child.toString()) > 0 && name.compareTo(nextNode.toString()) < 0) { return insertBefore(parent, nextNode, methodConfigs, methodConfig, nodeType); } } } return appendNode(parent, methodConfig, nodeType, methodConfigs); }
private boolean hasAnyCheckedLeaf(@NotNull CheckedTreeNode node) { if (node instanceof MethodTreeNode) { MethodConfig methodConfig = ((MethodTreeNode) node).getMethodConfig(); return methodConfig.isEnabled(); } int childCount = node.getChildCount(); for (int i = 0; i < childCount; i++) { if (hasAnyCheckedLeaf(((CheckedTreeNode) node.getChildAt(i)))) { return true; } } return false; }
private MethodTreeNode findNodeRecursively(@NotNull CheckedTreeNode node, MethodConfig methodConfig) { int len = node.getChildCount(); if (node instanceof MethodTreeNode) { if (Objects.equals(((MethodTreeNode) node).getMethodConfig().toString(), methodConfig.toString())) { return ((MethodTreeNode) node); } } for (int i = 0; i < len; i++) { MethodTreeNode res = findNodeRecursively((CheckedTreeNode) node.getChildAt(i), methodConfig); if (res != null) { return res; } } return null; }
private void setNodesChecked(@NotNull CheckedTreeNode node) { if (!node.isRoot() && !node.isLeaf()) { if (hasAnyCheckedLeaf(node)) { node.setChecked(true); } else { node.setChecked(false); } } int childCount = node.getChildCount(); for (int i = 0; i < childCount; i++) { setNodesChecked(((CheckedTreeNode) node.getChildAt(i))); } }