Example usage for javax.swing.tree DefaultMutableTreeNode breadthFirstEnumeration

List of usage examples for javax.swing.tree DefaultMutableTreeNode breadthFirstEnumeration

Introduction

In this page you can find the example usage for javax.swing.tree DefaultMutableTreeNode breadthFirstEnumeration.

Prototype

public Enumeration<TreeNode> breadthFirstEnumeration() 

Source Link

Document

Creates and returns an enumeration that traverses the subtree rooted at this node in breadth-first order.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Traverse Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTree tree = new JTree();
    tree.setRootVisible(true);//from   w w  w . j a va  2s .  c  om
    TreeModel model = tree.getModel();
    Object rootObject = model.getRoot();
    if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) {
        DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject;
        printDescendents(r);
        Enumeration breadth = r.breadthFirstEnumeration();
        Enumeration depth = r.depthFirstEnumeration();
        Enumeration preOrder = r.preorderEnumeration();
        printEnumeration(breadth, "Breadth");
        printEnumeration(depth, "Depth");
        printEnumeration(preOrder, "Pre");
    }

    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            TreePath path = treeSource.getSelectionPath();
            System.out.println(path);
            System.out.println(path.getPath());
            System.out.println(path.getParentPath());
            System.out.println(((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject());
            System.out.println(path.getPathCount());
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 400);
    frame.setVisible(true);
}

From source file:Main.java

public static void expandTree(JTree tree) {
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
    @SuppressWarnings("unchecked")
    Enumeration<DefaultMutableTreeNode> e = root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        if (node.isLeaf())
            continue;
        int row = tree.getRowForPath(new TreePath(node.getPath()));
        tree.expandRow(row);// w  w  w. ja va2s .c om
    }
}

From source file:Main.java

public static JComponent makeUI() {
    JTree tree = new JTree();
    TreeModel model = tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    Enumeration e = root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        Object o = node.getUserObject();
        if (o instanceof String) {
            node.setUserObject(new CheckBoxNode((String) o, false));
        }// w  w w . ja  va 2 s .  c  o  m
    }
    tree.setEditable(true);
    tree.setCellRenderer(new CheckBoxNodeRenderer());
    tree.setCellEditor(new CheckBoxNodeEditor());
    tree.expandRow(0);
    return new JScrollPane(tree);
}

From source file:gov.nij.er.ui.RecordTreeModel.java

/**
 * Get the IDs of the records represented by the specified path
 * @param selectedPath the path to search
 * @return the ids of the records at that path
 *//* ww  w.  jav a  2  s. co m*/
public Set<String> getRecordIdsForPath(TreePath selectedPath) {
    Set<String> ret = new HashSet<String>();

    Object[] pathArray = selectedPath.getPath();
    if (pathArray.length > 1) {

        DefaultMutableTreeNode recordNode = (DefaultMutableTreeNode) pathArray[1];
        @SuppressWarnings("unchecked")
        Enumeration<TreeNode> descendants = recordNode.breadthFirstEnumeration();

        while (descendants.hasMoreElements()) {
            TreeNode node = descendants.nextElement();
            if (node instanceof IdNode) {
                ret.add(((IdNode) node).getId());
            }
        }
    }
    LOG.debug("Record ids for path: " + ret);
    return ret;
}

From source file:com.sec.ose.osi.ui.frm.main.identification.JTreeAllFiles.java

private TreePath getTreePathByFilePathString(DefaultMutableTreeNode rootNode, String selectedFilePath) {
    TreePath tp = null;//from   w  w  w. j a v a 2  s.  c  om
    if (selectedFilePath != null) {
        DefaultMutableTreeNode node = null;
        Enumeration<?> enumer = rootNode.breadthFirstEnumeration();
        String sFilePath = "";
        DefaultTreeModel m_model = new DefaultTreeModel(rootNode);
        TreeNode[] nodes = null;
        while (enumer.hasMoreElements()) {
            node = (DefaultMutableTreeNode) enumer.nextElement();
            if (node.getUserObject().equals("/"))
                continue;
            sFilePath = ((FileNodeInfo) node.getUserObject()).getFilePath();
            if (sFilePath.equals(selectedFilePath)) {
                nodes = m_model.getPathToRoot(node);
                tp = new TreePath(nodes);
                break;
            }
        }
    }
    return tp;
}

From source file:com.openbravo.pos.admin.RolesViewTree.java

public DefaultMutableTreeNode searchNode(String nodeStr, DefaultMutableTreeNode tNode) {
    DefaultMutableTreeNode node = null;
    Enumeration e = tNode.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        node = (DefaultMutableTreeNode) e.nextElement();
        if (nodeStr.equals(node.getUserObject().toString())) {
            return node;
        }/*w  w w. j a  v  a  2s.  c om*/
    }
    return null;
}

From source file:org.kuali.maven.plugins.externals.MojoHelper.java

public Map<String, DefaultMutableTreeNode> getGavMap(DefaultMutableTreeNode node) {
    Enumeration<?> e = node.breadthFirstEnumeration();
    Map<String, DefaultMutableTreeNode> map = new HashMap<String, DefaultMutableTreeNode>();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode element = (DefaultMutableTreeNode) e.nextElement();
        Project project = (Project) element.getUserObject();
        GAV gav = project.getGav();/*from w  ww  .  jav  a  2s  .c om*/
        String gavId = toString(gav);
        map.put(gavId, element);
    }
    return map;
}

From source file:org.kuali.maven.plugins.externals.MojoHelper.java

protected DefaultMutableTreeNode findNode(DefaultMutableTreeNode node, String artifactId) {
    Enumeration<?> e = node.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode element = (DefaultMutableTreeNode) e.nextElement();
        Project project = (Project) element.getUserObject();
        GAV gav = project.getGav();//  w w  w  .j  a va  2  s .c  om
        if (gav.getArtifactId().equals(artifactId)) {
            return element;
        }
    }
    throw new IllegalStateException("Unable to locate " + artifactId);
}

From source file:org.kuali.maven.plugins.externals.MojoHelper.java

public void validateParents(DefaultMutableTreeNode node, Map<String, DefaultMutableTreeNode> map) {
    Enumeration<?> e = node.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode element = (DefaultMutableTreeNode) e.nextElement();
        if (element.isRoot()) {
            continue;
        }/*from   ww  w . j av a2 s . c  o  m*/
        Project project = (Project) element.getUserObject();
        GAV parentGav = project.getParent();
        String parentGavId = toString(parentGav);
        DefaultMutableTreeNode parent = map.get(parentGavId);
        if (parent == null) {
            throw new IllegalStateException(parentGavId + " could not be located");
        }
    }
}

From source file:ai.aitia.meme.paramsweep.intellisweepPlugin.JgapGAPlugin.java

private int calculateNumberOfGenes() {
    int count = 0;

    final DefaultMutableTreeNode root = (DefaultMutableTreeNode) chromosomeTree.getRoot();
    @SuppressWarnings("rawtypes")
    final Enumeration nodes = root.breadthFirstEnumeration();
    nodes.nextElement();/*  ww  w. j  a v a 2s .  co  m*/

    while (nodes.hasMoreElements()) {
        final DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.nextElement();
        final ParameterOrGene userObj = (ParameterOrGene) node.getUserObject();
        if (userObj.isGene())
            count++;
    }

    return count;
}