Java Code Examples for javax.swing.tree.TreePath#pathByAddingChild()

The following examples show how to use javax.swing.tree.TreePath#pathByAddingChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckTreeSelectionModel.java    From osp with GNU General Public License v3.0 18 votes vote down vote up
/**
 * Unselects the ancestor of a path and selects ancestor's descendants
 * other than those for which the path is a descendant.
 *
 * @param path the path
 */
private void unselectAncestor(TreePath path) {
  // find selected ancestor 
  Stack<TreePath> stack = new Stack<TreePath>();
  stack.push(path);
  TreePath ancestor = path.getParentPath();
  while((ancestor!=null)&&!isPathSelected(ancestor)) {
    stack.push(ancestor);
    ancestor = ancestor.getParentPath();
  }
  if(ancestor==null) { // no selected ancestors!
    return;
  }
  stack.push(ancestor);
  // for each path in the stack, unselect it and select all child nodes
  while(!stack.isEmpty()) {
    TreePath next = stack.pop();
    super.removeSelectionPaths(new TreePath[] {next});
    if(stack.isEmpty()) {
      return;
    }
    Object node = next.getLastPathComponent();
    int childCount = model.getChildCount(node);
    for(int i = 0; i<childCount; i++) {
      Object child = model.getChild(node, i);
      super.addSelectionPaths(new TreePath[] {next.pathByAddingChild(child)});
    }
  }
}
 
Example 2
Source File: DProperties.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void expandTwoLevels(TreePath treePath) {
	if (treePath.getPathCount() > 2) {
		return;
	}

	TreeNode node = (TreeNode) treePath.getLastPathComponent();

	if (node.getChildCount() >= 0) {
		for (Enumeration<?> enumChildren = node.children(); enumChildren.hasMoreElements();) {
			TreeNode subNode = (TreeNode) enumChildren.nextElement();
			TreePath path = treePath.pathByAddingChild(subNode);
			expandTwoLevels(path);
		}
	}

	jtrProperties.expandPath(treePath);
}
 
Example 3
Source File: FileNavigationPane.java    From java-disassembler with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
void treeDfs(final TreePath parent, Consumer<TreePath> onPreOrder, Consumer<TreePath> onPostOrder) {
    if (onPreOrder != null)
        onPreOrder.accept(parent);

    final TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (final Enumeration e = node.children(); e.hasMoreElements(); ) {
            final TreeNode n = (TreeNode) e.nextElement();
            final TreePath path = parent.pathByAddingChild(n);
            treeDfs(path, onPreOrder, onPostOrder);
        }
    }

    if (onPostOrder != null)
        onPostOrder.accept(parent);
}
 
Example 4
Source File: TDA.java    From tda with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * expand or collapse all nodes of the specified tree
 * @param tree the tree to expand all/collapse all
 * @param parent the parent to start with
 * @param expand expand=true, collapse=false
 */
private void expandAll(JTree catTree, TreePath parent, boolean expand) {
    // Traverse children
    TreeNode node = (TreeNode)parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration e=node.children(); e.hasMoreElements(); ) {
            TreeNode n = (TreeNode)e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAll(catTree, path, expand);
        }
    }

    if(parent.getPathCount() > 1) {
        // Expansion or collapse must be done bottom-up
        if (expand) {
            catTree.expandPath(parent);
        } else {
            catTree.collapsePath(parent);
        }
    }
}
 
Example 5
Source File: GTreeExpandNodeToDepthTask.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static void expandPath(JTree tree, TreePath treePath, int currentDepth,
		TaskMonitor monitor) throws CancelledException {

	if (currentDepth <= 0) {
		return;
	}

	GTreeNode treeNode = (GTreeNode) treePath.getLastPathComponent();
	TreeModel treeModel = tree.getModel();
	int childCount = treeModel.getChildCount(treeNode);

	if (childCount > 0) {
		for (int i = 0; i < childCount; i++) {
			monitor.checkCanceled();

			GTreeNode n = (GTreeNode) treeModel.getChild(treeNode, i);
			TreePath path = treePath.pathByAddingChild(n);

			expandPath(tree, path, currentDepth - 1, monitor);
		}
	}

	tree.expandPath(treePath);
}
 
Example 6
Source File: ProfilerTreeTable.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void collapseChildren(TreePath tpath) {
        if (tree != null) try {
            markExpansionTransaction();
            
            if (tpath == null || tree.isCollapsed(tpath)) return;
            
            TreeModel tmodel = tree.getModel();
            Object selected = tpath.getLastPathComponent();
            
            int nchildren = tmodel.getChildCount(selected);
            for (int i = 0; i < nchildren; i++) {
                TreePath tp = tpath.pathByAddingChild(tmodel.getChild(selected, i));
                tree.collapsePath(tp);
//                tree.resetExpandedState(tp);
            }
        
        } finally {
            clearExpansionTransaction();
        }
    }
 
Example 7
Source File: DSWorkbenchSelectionFrame.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
private TreePath find2(JTree tree, TreePath parent, Village pNode, int depth) {
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    DefaultMutableTreeNode o = (DefaultMutableTreeNode) node;

    // If equal, go down the branch
    if (o.getUserObject().equals(pNode)) {
        // If at end, return match
        return parent;
    } else {
        // Traverse children
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                TreePath result = find2(tree, path, pNode, depth + 1);
                // Found a match
                if (result != null) {
                    return result;
                }
            }
        }
    }
    // No match at this branch
    return null;
}
 
Example 8
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void collapseChildren(TreePath tpath) {
        if (tree != null) try {
            markExpansionTransaction();
            
            if (tpath == null || tree.isCollapsed(tpath)) return;
            
            TreeModel tmodel = tree.getModel();
            Object selected = tpath.getLastPathComponent();
            
            int nchildren = tmodel.getChildCount(selected);
            for (int i = 0; i < nchildren; i++) {
                TreePath tp = tpath.pathByAddingChild(tmodel.getChild(selected, i));
                tree.collapsePath(tp);
//                tree.resetExpandedState(tp);
            }
        
        } finally {
            clearExpansionTransaction();
        }
    }
 
Example 9
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
TreePath getNextPath(TreePath path, boolean down) {
    TreeModel _model = model.treeModel;
    TreeNode node = (TreeNode)path.getLastPathComponent();
    if (down && _model.getChildCount(node) > 0)
        return path.pathByAddingChild(_model.getChild(node, 0));

    TreePath parentPath = path.getParentPath();
    if (!down && parentPath == null)
        return path.pathByAddingChild(_model.getChild(node, 0));
    
    TreeNode parent = (TreeNode)parentPath.getLastPathComponent();
    int idx = _model.getIndexOfChild(parent, node) + 1;

    if (_model.getChildCount(parent) > idx)
        return parentPath.pathByAddingChild(_model.getChild(parent, idx));

    if (!down && parentPath.getParentPath() == null) {
        return parentPath.pathByAddingChild(_model.getChild(parent, 0));
    } else {
        return getNextPath(parentPath, false);
    }
}
 
Example 10
Source File: JCheckBoxTree.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
/**
 * iterate through every child and do the action
 *
 * @param parent
 * @param expand
 */
private void expandAll(TreePath parent, boolean expand) {
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (getChildCount(node) >= 0) {
        for (int i = 0; i < getChildCount(node); i++) {
            TreePath path = parent.pathByAddingChild(getChildAt(node, i));
            expandAll(path, expand);
        }
    }
    if (expand) {
        expandPath(parent);
    } else {
        collapsePath(parent);
    }

}
 
Example 11
Source File: UIUtil.java    From MVPManager with MIT License 6 votes vote down vote up
private static void expandAll(JTree tree, TreePath parent, boolean expand) {
    // Traverse children
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements(); ) {
            TreeNode n = (TreeNode) e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAll(tree, path, expand);
        }
    }

    // Expansion or collapse must be done bottom-up
    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}
 
Example 12
Source File: JTreeTable.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void treeNodesChanged(TreeModelEvent e)
{
	TreePath parentPath = e.getTreePath();
	int leadingRow = Integer.MAX_VALUE;
	int trailingRow = -1;
	if (e.getChildren() != null)
	{
		for (Object node : e.getChildren())
		{
			TreePath childPath = parentPath.pathByAddingChild(node);
			int row = tree.getRowForPath(childPath);
			leadingRow = Math.min(leadingRow, row);
			trailingRow = Math.max(trailingRow, row);
		}
	}
	fireTableRowsUpdated(leadingRow, trailingRow);
}
 
Example 13
Source File: ElementTreePanel.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a TreePath to the element at <code>position</code>.
 */
protected TreePath getPathForIndex(int position, Object root,
        Element rootElement) {
    TreePath path = new TreePath(root);
    Element child = rootElement.getElement(rootElement.getElementIndex(
            position));

    path = path.pathByAddingChild(rootElement);
    path = path.pathByAddingChild(child);
    while (!child.isLeaf()) {
        child = child.getElement(child.getElementIndex(position));
        path = path.pathByAddingChild(child);
    }
    return path;
}
 
Example 14
Source File: Preferences.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
static TreePath str2path(String[] path, int[] idxs, TreeModel model) {
   Object    node  = model.getRoot();
   TreePath  path_ = new TreePath(node);
 pathloop:
   for(int i = 0; i < path.length; i++) {
Object n = node;
int    c = model.getChildCount(node);

if(c == 0) return null;

// first we try with child @ idx.
if(idxs[i] < 0)  idxs[i] = 0;
if(idxs[i] >= c) idxs[i] = c - 1;

node = model.getChild(n, idxs[i]);
if(node.toString().equals(path[i])) {
  path_ = path_.pathByAddingChild(node);
  continue pathloop;
}
// failed -> loop over all childs
// we start the search in a neighborhood of idxs[i]
int stop = (c / 2) + 1;
for(int j = 0; j < stop; j++) {
  node = model.getChild(n, (idxs[i] + j) % c);
  if(node.toString().equals(path[i])) {
    path_ = path_.pathByAddingChild(node);
    continue pathloop;
  }
  node = model.getChild(n, Math.abs(idxs[i] - j) % c);
  if(node.toString().equals(path[i])) {
    path_ = path_.pathByAddingChild(node);
    continue pathloop;
  }
} 
return null;
   }
   return path_;
 }
 
Example 15
Source File: JTreeJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private TreePath getPath(JTree tree, String path) {
    String[] tokens = path.substring(1).split("(?<!\\\\)/");
    TreeModel treeModel = tree.getModel();
    if (treeModel == null) {
        throw new RuntimeException("Could not find model for tree");
    }
    Object rootNode = treeModel.getRoot();
    int start = tree.isRootVisible() ? 1 : 0;
    TreePath treePath = new TreePath(rootNode);
    StringBuilder searchedPath = new StringBuilder();
    if (tree.isRootVisible()) {
        String rootNodeText = unescapeSpecialCharacters(tokens[0]);
        searchedPath.append("/" + rootNodeText);
        assertTrue("JTree does not have a root node!", rootNode != null);
        assertTrue("JTree root node does not match: Expected </" + getPathText(tree, treePath) + "> Actual: <"
                + searchedPath.toString() + ">", searchedPath.toString().equals("/" + getPathText(tree, treePath)));
    }
    for (int i = start; i < tokens.length; i++) {
        String childText = unescapeSpecialCharacters(tokens[i]);
        searchedPath.append("/" + childText);
        boolean matched = false;
        tree.expandPath(treePath);
        for (int j = 0; j < treeModel.getChildCount(treePath.getLastPathComponent()); j++) {
            Object child = treeModel.getChild(treePath.getLastPathComponent(), j);
            TreePath childPath = treePath.pathByAddingChild(child);
            if (childText.equals(getPathText(tree, childPath))) {
                treePath = childPath;
                matched = true;
                break;
            }
        }
        if (!matched) {
            return null;
        }
    }
    return treePath;
}
 
Example 16
Source File: RepositoryTreeModel.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns the tree path to the entry, or the first parent, which is visible in the tree
 *
 * @param entry
 * 		the entry
 * @return the tree path to the entry or it's first visible parent
 */
TreePath getPathTo(Entry entry) {
	// get parent if only folder are allowed
	if (onlyFolders && entry != null && !(entry instanceof Folder)) {
		entry = entry.getContainingFolder();
	}
	TreePath path = new TreePath(root);
	// quick exit for null / zero children
	if (entry == null) {
		return path;
	}
	Deque<Entry> entries = new ArrayDeque<>();
	for (Entry parent = entry; parent != null; parent = parent.getContainingFolder()) {
		entries.addFirst(parent);
	}
	// Check if writable repositories are allowed
	if (onlyWriteableRepositories && entries.getFirst().isReadOnly()) {
		return path;
	}
	// build path
	for (Entry child : entries) {
		if (!checkElements.test(child)) {
			return path;
		}
		path = path.pathByAddingChild(child);
	}
	return path;
}
 
Example 17
Source File: JTreeView.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
private TreePath shortestCommonPath(TreePath p1, TreePath p2) {
   TreePath result = new TreePath(p1.getPathComponent(0));
   for(int i = 0; i < Math.min(p1.getPathCount(), p2.getPathCount()); i++)
     if(!p1.getPathComponent(i).equals(p2.getPathComponent(i)))
break;
     else
result = result.pathByAddingChild(p1.getPathComponent(i));
   return result;
 }
 
Example 18
Source File: TreeTableModelAdapter.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private TreePath getCurrentPath(TreePath oldPath) {
    if (oldPath == null || oldPath.getPathCount() < 1) return null;
    if (!treeTableModel.getRoot().equals(oldPath.getPathComponent(0))) return null;
    
    TreePath p = getRootPath();
    Object[] op = oldPath.getPath();
    CCTNode n = (CCTNode)treeTableModel.getRoot();
    
    for (int i = 1; i < op.length; i++) {
        // #241115
        CCTNode[] children = n.getChildren();
        if (children == null) return null;
        
        CCTNode nn = null;
        
        for (CCTNode c : children)
            if (c.equals(op[i])) {
                nn = c;
                break;
            }
        
        if (nn == null) return null;
        
        n = nn;
        p = p.pathByAddingChild(n);
    }
    
    return p;
}
 
Example 19
Source File: NavigatorContent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void selectTreeNode(int offset) {
    TreeNodeAdapter root = (TreeNodeAdapter)tree.getModel().getRoot();
    
    int from = root.getStart();
    int to = root.getEnd();
    // sanity check:
    if (offset < from || offset >= to) {
        // retain the selection path as it is
        return;
    }
    TreePath p = new TreePath(root);
    boolean cont = true;
    OUT: while (cont) {
        cont = false;
        Enumeration chE = root.children();
        while (chE.hasMoreElements()) {
            TreeNodeAdapter ch = (TreeNodeAdapter)chE.nextElement();
            if (offset < ch.getStart()) {
                break OUT;
            }
            if (offset < ch.getEnd()) {
                root = ch;
                p = p.pathByAddingChild(ch);
                cont = true;
                break;
            }
        }
    };
    tree.scrollPathToVisible(p);
    tree.setSelectionPath(p);
}
 
Example 20
Source File: InspectorTreeUI.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public @NotNull
TreePath getLastExpandedDescendant(TreePath path) {
  while (tree.isExpanded(path)) {
    final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
    if (node.isLeaf()) {
      break;
    }
    path = path.pathByAddingChild(node.getLastChild());
  }
  return path;
}