private void checkForEmptyAndDuplicatedNames( MyNode rootNode, String prefix, String title, Class<? extends NamedConfigurable> configurableClass, boolean recursively) throws ConfigurationException { final Set<String> names = new HashSet<String>(); for (int i = 0; i < rootNode.getChildCount(); i++) { final MyNode node = (MyNode) rootNode.getChildAt(i); final NamedConfigurable scopeConfigurable = node.getConfigurable(); if (configurableClass.isInstance(scopeConfigurable)) { final String name = scopeConfigurable.getDisplayName(); if (name.trim().length() == 0) { selectNodeInTree(node); throw new ConfigurationException("Name should contain non-space characters"); } if (names.contains(name)) { final NamedConfigurable selectedConfigurable = getSelectedConfigurable(); if (selectedConfigurable == null || !Comparing.strEqual(selectedConfigurable.getDisplayName(), name)) { selectNodeInTree(node); } throw new ConfigurationException( CommonBundle.message("smth.already.exist.error.message", prefix, name), title); } names.add(name); } if (recursively) { checkForEmptyAndDuplicatedNames(node, prefix, title, configurableClass, true); } } }
@Nullable public NamedConfigurable getSelectedConfigurable() { final TreePath selectionPath = myTree.getSelectionPath(); if (selectionPath != null) { MyNode node = (MyNode) selectionPath.getLastPathComponent(); final NamedConfigurable configurable = node.getConfigurable(); LOG.assertTrue(configurable != null, "already disposed"); return configurable; } return null; }
private static String getNodePathString(final MyNode node) { StringBuilder path = new StringBuilder(); MyNode current = node; while (current != null) { final Object userObject = current.getUserObject(); if (!(userObject instanceof NamedConfigurable)) break; final String displayName = current.getDisplayName(); if (StringUtil.isEmptyOrSpaces(displayName)) break; if (path.length() > 0) { path.append('|'); } path.append(displayName); final TreeNode parent = current.getParent(); if (!(parent instanceof MyNode)) break; current = (MyNode) parent; } return path.toString(); }
protected void clearChildren() { TreeUtil.traverseDepth( myRoot, new TreeUtil.Traverse() { public boolean accept(Object node) { if (node instanceof MyNode) { final MyNode treeNode = ((MyNode) node); treeNode.getConfigurable().disposeUIResources(); if (!(treeNode instanceof MyRootNode)) { treeNode.setUserObject(null); } } return true; } }); myRoot.removeAllChildren(); }
public void reset() { loadComponentState(); myHasDeletedItems = false; ((DefaultTreeModel) myTree.getModel()).reload(); // myTree.requestFocus(); myState.getProportions().restoreSplitterProportions(myWholePanel); final Enumeration enumeration = myRoot.breadthFirstEnumeration(); boolean selected = false; while (enumeration.hasMoreElements()) { final MyNode node = (MyNode) enumeration.nextElement(); if (node instanceof MyRootNode) continue; final String path = getNodePathString(node); if (!selected && Comparing.strEqual(path, myState.getLastEditedConfigurable())) { TreeUtil.selectInTree(node, false, myTree); selected = true; } } if (!selected) { TreeUtil.selectFirstNode(myTree); } updateSelectionFromTree(); }
protected void removePaths(final TreePath... paths) { MyNode parentNode = null; int idx = -1; for (TreePath path : paths) { final MyNode node = (MyNode) path.getLastPathComponent(); final NamedConfigurable namedConfigurable = node.getConfigurable(); final Object editableObject = namedConfigurable.getEditableObject(); parentNode = (MyNode) node.getParent(); idx = parentNode.getIndex(node); ((DefaultTreeModel) myTree.getModel()).removeNodeFromParent(node); myHasDeletedItems |= wasObjectStored(editableObject); fireItemsChangeListener(editableObject); onItemDeleted(editableObject); namedConfigurable.disposeUIResources(); } if (paths.length > 0) { if (parentNode != null && idx != -1) { DefaultMutableTreeNode toSelect = null; if (idx < parentNode.getChildCount()) { toSelect = (DefaultMutableTreeNode) parentNode.getChildAt(idx); } else { if (idx > 0 && parentNode.getChildCount() > 0) { if (idx - 1 < parentNode.getChildCount()) { toSelect = (DefaultMutableTreeNode) parentNode.getChildAt(idx - 1); } else { toSelect = (DefaultMutableTreeNode) parentNode.getFirstChild(); } } else { if (parentNode.isRoot() && myTree.isRootVisible()) { toSelect = parentNode; } else if (parentNode.getChildCount() > 0) { toSelect = (DefaultMutableTreeNode) parentNode.getFirstChild(); } } } if (toSelect != null) { TreeUtil.selectInTree(toSelect, true, myTree); } } else { TreeUtil.selectFirstNode(myTree); } } }
protected void setSelectedNode(@Nullable MyNode node) { if (node != null) { myState.setLastEditedConfigurable(getNodePathString(node)); } updateSelection(node != null ? node.getConfigurable() : null); }