Java Code Examples for javax.swing.JTree#expandPath()

The following examples show how to use javax.swing.JTree#expandPath() . 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: 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 2
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static void restoreExpandedNodes(JTree tree, UIState uiState) {
        try {
            tree.putClientProperty(UIUtils.PROP_EXPANSION_TRANSACTION, Boolean.TRUE);
            Enumeration<TreePath> paths = uiState.getExpandedPaths();
            if (paths != null) while (paths.hasMoreElements()) {
                TreePath tp = paths.nextElement();
//                System.err.println(">>> Restoring expanded " + tp);
                tree.expandPath(getSimilarPath(tp, tree.getModel()));
            }
        } catch (Exception e) {
            System.err.println(">>> Exception in ProfilerTreeTable.restoreExpandedNodes: " + e.getMessage());
            e.printStackTrace();
        } finally {
            tree.putClientProperty(UIUtils.PROP_EXPANSION_TRANSACTION, null);
        }
    }
 
Example 3
Source File: TreeUtil.java    From Zettelkasten with GNU General Public License v3.0 6 votes vote down vote up
private static void expandAllTrees(TreePath parent, JTree tree) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            DefaultMutableTreeNode n = (DefaultMutableTreeNode) e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAllTrees(path, tree);
        }
    }
    // retrieve treenode user object
    TreeUserObject userObject = (TreeUserObject) node.getUserObject();
    // check whether deepest level is reached.
    if ((expandLevel != -1 && node.getLevel() < expandLevel) || -1 == expandLevel) {
        // check whether treenode-id is in the list of collapsed items
        if (userObject != null && collapsedNodes.contains(userObject.getId())) {
            // if yes, collapse treenode
            tree.collapsePath(parent);
        } else {
            // else expand it
            tree.expandPath(parent);
        }
    } else {
        tree.collapsePath(parent);
    }
}
 
Example 4
Source File: JCompositeRowsSelectorPanel.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
private 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 5
Source File: ProfilerTreeTable.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
static void restoreExpandedNodes(JTree tree, UIState uiState) {
        try {
            tree.putClientProperty(UIUtils.PROP_EXPANSION_TRANSACTION, Boolean.TRUE);
            Enumeration<TreePath> paths = uiState.getExpandedPaths();
            if (paths != null) while (paths.hasMoreElements()) {
                TreePath tp = paths.nextElement();
//                System.err.println(">>> Restoring expanded " + tp);
                tree.expandPath(getSimilarPath(tp, tree.getModel()));
            }
        } catch (Exception e) {
            System.err.println(">>> Exception in ProfilerTreeTable.restoreExpandedNodes: " + e.getMessage());
            e.printStackTrace();
        } finally {
            tree.putClientProperty(UIUtils.PROP_EXPANSION_TRANSACTION, null);
        }
    }
 
Example 6
Source File: View.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
private static void expandTreeNodesRecursive(JTree tree, TreePath parent, boolean expand) {
    TreeModel model = tree.getModel();

    Object node = parent.getLastPathComponent();
    int childCount = model.getChildCount(node);
    for (int j = 0; j < childCount; j++) {
        Object child = model.getChild(node, j);
        TreePath path = parent.pathByAddingChild(child);
        expandTreeNodesRecursive(tree, path, expand);
    }

    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}
 
Example 7
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 8
Source File: ProductLayerAssistantPage.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Component createPageComponent() {
    ProductTreeModel model = createTreeModel();
    tree = new JTree(model);
    tree.setEditable(false);
    tree.setShowsRootHandles(true);
    tree.setRootVisible(false);
    tree.setCellRenderer(new ProductNodeTreeCellRenderer());
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    tree.getSelectionModel().addTreeSelectionListener(new ProductNodeSelectionListener());

    List<CompatibleNodeList> nodeLists = model.compatibleNodeLists;
    for (CompatibleNodeList nodeList : nodeLists) {
        tree.expandPath(new TreePath(new Object[]{nodeLists, nodeList}));
    }

    JPanel panel = new JPanel(new BorderLayout(4, 4));
    panel.setBorder(new EmptyBorder(4, 4, 4, 4));
    panel.add(new JLabel("Compatible bands and tie-point grids:"), BorderLayout.NORTH);
    panel.add(new JScrollPane(tree), BorderLayout.CENTER);
    return panel;
}
 
Example 9
Source File: TreeUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
public static void collapseAllFromNode(JTree tree, DBBrowserNode node) {
    TreePath pathToNode = new TreePath(node);
    collapseAll(tree, pathToNode);
    if (!tree.isRootVisible()) {
        tree.expandPath(pathToNode);
    }
}
 
Example 10
Source File: EntityTreePanel.java    From Robot-Overlord-App with GNU General Public License v2.0 5 votes vote down vote up
/**
    * list all entities in the world.  Double click an item to get its panel.
    * See https://docs.oracle.com/javase/7/docs/api/javax/swing/JTree.html
    */
public void updateEntityTree() {
	// list all objects in scene
    DefaultMutableTreeNode newTop = createTreeNodes(ro);
	JTree newTree = new JTree(newTop);

    newTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    newTree.setShowsRootHandles(true);
    newTree.addTreeSelectionListener(this);
	//tree.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

	if(oldTree!=null) {
		// preserve the original expansions
		ArrayList<TreePath> expanded = new ArrayList<TreePath>();
		for(int i=0;i<oldTree.getRowCount();++i) {
			if(oldTree.isExpanded(i)) {
				expanded.add(oldTree.getPathForRow(i));
			}
		}
		// restore the expanded paths
		for(TreePath p : expanded) {
			newTree.expandPath(p);
		}
		// restore the selected paths
		TreePath[] paths = oldTree.getSelectionPaths();
		newTree.setSelectionPaths(paths);
	}
	
	if(!newTree.equals(oldTree)) {
		scroll.setViewportView(newTree);
		oldTree=newTree;
		oldTop =newTop;
	}
}
 
Example 11
Source File: Preferences.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
void expandAll(JTree tree) {
     for(int i = 0; i < expandedPaths.length; i++)
if(!expanded[i]) {
  TreePath path = str2path(expandedPaths[i], expandedPathIdxs[i], tree.getModel());
  if(path != null) {
    expanded[i] = true;
    tree.expandPath(path);
  }
}
   }
 
Example 12
Source File: DViewCertificate.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void expandTree(JTree tree, TreePath parent) {
	TreeNode node = (TreeNode) parent.getLastPathComponent();
	if (node.getChildCount() >= 0) {
		for (Enumeration<?> enumNodes = node.children(); enumNodes.hasMoreElements();) {
			TreeNode subNode = (TreeNode) enumNodes.nextElement();
			TreePath path = parent.pathByAddingChild(subNode);
			expandTree(tree, path);
		}
	}

	tree.expandPath(parent);
}
 
Example 13
Source File: TreeUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
/**
 * Expand all the previously remembered expanded paths.
 */
public static void setExpandedPaths(JTree tree, TreePath[] expandedPaths) {
    if (expandedPaths == null) {
        return;
    }
    for (int i = 0; i < expandedPaths.length; ++i) {
        TreePath oldPath = expandedPaths[i];
        TreePath newPath = searchPath(tree.getModel(), oldPath);
        if (newPath != null) {
            tree.expandPath(newPath);
        }
    }
}
 
Example 14
Source File: TreeUtil.java    From Zettelkasten with GNU General Public License v3.0 5 votes vote down vote up
private static void expandAllTrees(TreePath parent, boolean expand, JTree tree) {
    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);
            expandAllTrees(path, expand, tree);
        }
    }
    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}
 
Example 15
Source File: TreeUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
/**
 * Expand a tree node and all its child nodes recursively.
 *
 * @param tree The tree whose nodes to expand.
 * @param path Path to the node to start at.
 */
public static void expandAll(JTree tree, TreePath path) {
    Object node = path.getLastPathComponent();
    TreeModel model = tree.getModel();
    if (model.isLeaf(node)) {
        return;
    }
    tree.expandPath(path);
    int num = model.getChildCount(node);
    for (int i = 0; i < num; i++) {
        expandAll(tree, path.pathByAddingChild(model.getChild(node, i)));
    }
}
 
Example 16
Source File: TreeUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param tree com.sun.java.swing.JTree
 * @param start com.sun.java.swing.tree.DefaultMutableTreeNode
 */
public static void expandTree(JTree tree, TreeNode start, int level) {
    for (Enumeration children = start.children(); children.hasMoreElements();) {
        DefaultMutableTreeNode dtm = (DefaultMutableTreeNode) children.nextElement();
        //System.out.println(dtm.getUserObject()+" "+dtm.getDepth());
        if (!dtm.isLeaf() && dtm.getLevel() <= level) {
            //
            TreePath tp = new TreePath(dtm.getPath());
            tree.expandPath(tp);
            //
            expandTree(tree, dtm, level);
        }
    }
    return;
}
 
Example 17
Source File: GUIBrowser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void expandAll(JTree tree, TreePath path) {
    tree.expandPath(path);
    TreeModel model = tree.getModel();
    Object lastComponent = path.getLastPathComponent();
    for (int i = 0; i < model.getChildCount(lastComponent); i++) {
        expandAll(tree,
                path.pathByAddingChild(model.getChild(lastComponent, i)));
    }
}
 
Example 18
Source File: JFrameRTStats.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
public static void expandAll(JTree tree, TreePath parent)
{
    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);
        }
    }
    tree.expandPath(parent);
}
 
Example 19
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 20
Source File: View.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
private static TreePath expandTreeNode(JTree tree, List<String> pathAsStringList) {
    TreePath tp = getTreePathByPathStrings(tree, pathAsStringList);
    tree.expandPath(tp);
    return tp;
}