Example usage for javax.swing.tree DefaultMutableTreeNode preorderEnumeration

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

Introduction

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

Prototype

public Enumeration<TreeNode> preorderEnumeration() 

Source Link

Document

Creates and returns an enumeration that traverses the subtree rooted at this node in preorder.

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);/*  w w w.ja  va  2  s  .  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:gdt.jgui.entity.JEntityDigestDisplay.java

private void setSubnodesNumbers(DefaultMutableTreeNode node) {
    try {/*w  w w . j av  a  2s .  c o  m*/
        Enumeration en = node.preorderEnumeration();
        DefaultMutableTreeNode child;
        String locator$;
        while (en.hasMoreElements()) {
            child = (DefaultMutableTreeNode) en.nextElement();
            locator$ = (String) child.getUserObject();
            locator$ = Locator.append(locator$, NODE_NUMBER, String.valueOf(nodeNumber++));
            child.setUserObject(locator$);
        }
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).severe(e.toString());
    }
}

From source file:gdt.jgui.entity.JEntityDigestDisplay.java

private void select() {
    if (selection$ == null)
        return;/*from w  w w. j  a  v a  2s. c  o m*/
    try {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
        Enumeration en = root.preorderEnumeration();
        DefaultMutableTreeNode node;
        // System.out.println("EntityDigestDisplay:select:selection="+selection$);
        Properties locator = Locator.toProperties(selection$);
        String selectionNumber$ = locator.getProperty(NODE_NUMBER);
        //          System.out.println("EntityDigestDisplay:select:selection number="+selectionNumber$);
        String nodeLocator$;
        String nodeNumber$;
        while (en.hasMoreElements()) {
            node = (DefaultMutableTreeNode) en.nextElement();
            nodeLocator$ = (String) node.getUserObject();
            locator = Locator.toProperties(nodeLocator$);
            nodeNumber$ = locator.getProperty(NODE_NUMBER);
            //            System.out.println("EntityDigestDisplay:select:node number="+nodeNumber$);
            if (selectionNumber$.equals(nodeNumber$)) {
                TreeNode[] nodes = ((DefaultTreeModel) tree.getModel()).getPathToRoot(node);
                TreePath tpath = new TreePath(nodes);
                tree.scrollPathToVisible(tpath);
                tree.setSelectionPath(tpath);
                break;
            }
        }
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).severe(e.toString());
    }
}

From source file:jeplus.JEPlusProject.java

/**
 * Get the total number of parameters in the parameter tree
 *
 * @return parameter count/* ww  w  .  ja v a 2 s  . c  o  m*/
 */
@JsonIgnore
public int getNumberOfParams() {
    DefaultMutableTreeNode ParaTree = this.getParamTree();
    if (ParaTree == null) {
        return 0;
    }
    Enumeration nodes = ParaTree.preorderEnumeration();
    int count = 0;
    while (nodes.hasMoreElements()) {
        nodes.nextElement();
        count++;
    }
    return count;
}

From source file:jeplus.JEPlusProject.java

/**
 * Get a list of search strings from the parameter tree of this project.
 *
 * @return/*from   w w w . j  ava  2s .  c o m*/
 */
@JsonIgnore
public String[] getSearchStrings() {
    DefaultMutableTreeNode ParaTree = this.getParamTree();
    if (ParaTree == null)
        return null;

    ArrayList<String> SearchStrings = new ArrayList<>();
    Enumeration nodes = ParaTree.preorderEnumeration();
    while (nodes.hasMoreElements()) {
        Object node = nodes.nextElement();
        String ss = ((ParameterItem) ((DefaultMutableTreeNode) node).getUserObject()).getSearchString();
        if (ss != null && ss.trim().length() > 0 && !SearchStrings.contains(ss)) {
            SearchStrings.add(ss);
        }
    }
    return SearchStrings.toArray(new String[0]);
}

From source file:gdt.jgui.entity.index.JIndexPanel.java

private void select(String selectedNodeKey$) {

    try {//from w w  w . j  ava2 s  . c om

        DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
        Enumeration en = root.preorderEnumeration();
        DefaultMutableTreeNode node;
        // System.out.println("EntityDigestDisplay:select:selection node="+selectedNodeKey$);
        String nodeLocator$;
        Properties locator;
        while (en.hasMoreElements()) {
            node = (DefaultMutableTreeNode) en.nextElement();
            nodeLocator$ = (String) node.getUserObject();
            locator = Locator.toProperties(nodeLocator$);
            //System.out.println("EntityDigestDisplay:select:node number="+nodeNumber$);
            if (selectedNodeKey$.equals(locator.getProperty(NODE_KEY))) {
                TreeNode[] nodes = ((DefaultTreeModel) tree.getModel()).getPathToRoot(node);
                TreePath tpath = new TreePath(nodes);
                tree.scrollPathToVisible(tpath);
                tree.setSelectionPath(tpath);
                break;
            }
        }
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).severe(e.toString());
    }

}

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

private List<GeneInfo> getSelectedGenes() {
    if (genes == null) {
        genes = new ArrayList<GeneInfo>();

        final DefaultMutableTreeNode root = (DefaultMutableTreeNode) chromosomeTree.getRoot();
        @SuppressWarnings("rawtypes")
        final Enumeration nodes = root.preorderEnumeration();
        nodes.nextElement();/*w  w  w . j a  v  a 2 s .c  om*/
        while (nodes.hasMoreElements()) {
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.nextElement();
            final ParameterOrGene userObj = (ParameterOrGene) node.getUserObject();
            if (userObj.isGene()) {
                convertGeneName(userObj);
                genes.add(userObj.getGeneInfo());
            }
        }
    }

    return genes;
}

From source file:eu.crisis_economics.abm.dashboard.Page_Parameters.java

private ParameterTree createParameterTreeFromParamSweepGUI() throws ModelInformationException {
    final ParameterTree result = new ParameterTree();

    final DefaultListModel listModel = (DefaultListModel) parameterList.getModel();
    for (int i = 0; i < listModel.getSize(); ++i) {
        final AvailableParameter param = (AvailableParameter) listModel.get(i);
        final AbstractParameterInfo<?> batchParameter = InfoConverter.parameterInfo2ParameterInfo(param.info);
        ParameterTreeUtils.addConstantParameterToParameterTree(batchParameter, result, currentModelHandler);
    }/*from  w  ww  . java2 s  .c  om*/

    for (final ParameterCombinationGUI pGUI : parameterTreeBranches) {
        final DefaultMutableTreeNode root = pGUI.combinationRoot;
        @SuppressWarnings("rawtypes")
        final Enumeration nodes = root.breadthFirstEnumeration();
        nodes.nextElement(); // root;
        while (nodes.hasMoreElements()) {
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.nextElement();
            final ParameterInATree userObj = (ParameterInATree) node.getUserObject();
            ParameterTreeUtils.filterConstants(userObj, result, currentModelHandler);
        }
    }

    for (final ParameterCombinationGUI pGUI : parameterTreeBranches) {
        final DefaultMutableTreeNode root = pGUI.combinationRoot;
        @SuppressWarnings("rawtypes")
        final Enumeration nodes = root.preorderEnumeration();
        nodes.nextElement(); // root
        ParameterNode parent = result.getRoot();
        while (nodes.hasMoreElements()) {
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.nextElement();
            final ParameterInATree userObj = (ParameterInATree) node.getUserObject();
            if (ParameterTreeUtils.isMutableParameter(userObj)) {
                final AbstractParameterInfo<?> batchParameter = InfoConverter
                        .parameterInfo2ParameterInfo(userObj.info);
                batchParameter.setRunNumber(1);
                final ParameterNode parameterNode = new ParameterNode(batchParameter);
                parent.add(parameterNode);
                parent = parameterNode;
            }
        }
    }

    return result;
}

From source file:com.monead.semantic.workbench.SemanticWorkbench.java

/**
 * Find a matching individual in the tree.
 * //  w  w  w .j a  v  a  2s. c  om
 * @param wrapper
 *          The individual to be matched
 * @param forward
 *          True to search forward, false to search backward
 */
private void findMatchingIndividual(Wrapper wrapper, boolean forward) {
    final DefaultTreeModel treeModel = (DefaultTreeModel) ontModelTree.getModel();
    final DefaultMutableTreeNode finalMatchAt;
    DefaultMutableTreeNode latestMatchAt = null;
    DefaultMutableTreeNode firstMatchAt = null;
    boolean foundClickedNode = false;
    boolean wrappedAroundBackward = false;

    if (wrapper != null && wrapper instanceof WrapperInstance) {
        final DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getRoot();
        @SuppressWarnings("unchecked")
        final Enumeration<DefaultMutableTreeNode> nodeEnumeration = node.preorderEnumeration();
        while (nodeEnumeration.hasMoreElements()) {
            final DefaultMutableTreeNode nextNode = nodeEnumeration.nextElement();
            if (nextNode.getUserObject() instanceof Wrapper) {
                final Wrapper nodeWrapper = (Wrapper) nextNode.getUserObject();
                if (wrapper.getUuid().equals(nodeWrapper.getUuid())) {
                    foundClickedNode = true;
                    if (forward) {
                        // If there is one past this location then
                        // use it, otherwise wrap back to top
                        latestMatchAt = null;
                    } else {
                        if (firstMatchAt != null || latestMatchAt != null) {
                            // Searching backward and have found a previous one
                            break;
                        }
                        wrappedAroundBackward = true;
                    }
                } else if (!wrapper.getUuid().equals(nodeWrapper.getUuid())
                        && wrapper.getClass().equals(wrapper.getClass())
                        && wrapper.getLocalName().equals(nodeWrapper.getLocalName())
                        && wrapper.getUri().equals(nodeWrapper.getUri())) {
                    if (firstMatchAt == null && !foundClickedNode) {
                        // First one found, keep it in case we wrap around
                        firstMatchAt = nextNode;
                        LOGGER.debug("Found first matching node: search UUID: " + wrapper.getUuid()
                                + " found UUID: " + nodeWrapper.getUuid());
                    } else {
                        // Keep track of latest one found
                        latestMatchAt = nextNode;
                        LOGGER.debug("Found a following matching node: search UUID: " + wrapper.getUuid()
                                + " found UUID: " + nodeWrapper.getUuid());
                        // If going forward then this is the next match
                        if (forward && foundClickedNode) {
                            break;
                        }
                    }
                }
            }
        }

        if ((!forward || foundClickedNode) && latestMatchAt != null) {
            finalMatchAt = latestMatchAt;
            if (forward) {
                setStatus("Next " + wrapper.getLocalName() + " found");
            } else {
                if (wrappedAroundBackward) {
                    setStatus("Wrapped to bottom of tree and found a " + wrapper.getLocalName());
                } else {
                    setStatus("Previous " + wrapper.getLocalName() + " found");
                }
            }
        } else if (firstMatchAt != null) {
            finalMatchAt = firstMatchAt;
            if (forward) {
                setStatus("Wrapped to top of tree and found a " + wrapper.getLocalName());
            } else {
                setStatus("Previous " + wrapper.getLocalName() + " found");
            }
        } else {
            finalMatchAt = null;
            setStatus(wrapper.getLocalName() + " could not be found elsewhere in the tree");
        }

        if (finalMatchAt != null) {
            ontModelTree.setExpandsSelectedPaths(true);

            // Scroll to the selected node
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ontModelTree.setSelectionPath(new TreePath(treeModel.getPathToRoot(finalMatchAt)));
                    ontModelTree.scrollPathToVisible(new TreePath(treeModel.getPathToRoot(finalMatchAt)));
                    final Rectangle visible = ontModelTree.getVisibleRect();
                    visible.x = 0;
                    ontModelTree.scrollRectToVisible(visible);
                }
            });
        }
    }
}

From source file:org.freeplane.core.resources.components.OptionPanel.java

@SuppressWarnings("unchecked")
/**//from w  w  w  . j a v a  2 s.  c  o  m
 * This is where the controls are added to the "controls" IProperty Vector
 * @param controlsTree This is the tree that gets built
 */
private void initControls(final DefaultMutableTreeNode controlsTree) {
    controls = new Vector<IPropertyControl>();
    for (final Enumeration<DefaultMutableTreeNode> i = controlsTree.preorderEnumeration(); i
            .hasMoreElements();) {
        final IPropertyControlCreator creator = (IPropertyControlCreator) i.nextElement().getUserObject();
        if (creator == null) {
            continue;
        }
        final IPropertyControl control = creator.createControl();
        controls.add(control);
    }
}