Example usage for javax.swing.tree TreeModel getRoot

List of usage examples for javax.swing.tree TreeModel getRoot

Introduction

In this page you can find the example usage for javax.swing.tree TreeModel getRoot.

Prototype

public Object getRoot();

Source Link

Document

Returns the root of the tree.

Usage

From source file:dotaSoundEditor.Controls.EditorPanel.java

/**
 * @param oldTree The tree that was previously in use
 * @param scriptFilePath A File object pointing to or containing a script.
 * @return The merged tree./* w  w  w.jav  a2s. c  o m*/
 */
TreeModel mergeNewChanges(TreeModel oldTree, File scriptFilePath) {
    //Look for any modified wavestrings. Save their nodes, and note their indices.
    //Parse in updated script tree, replace nodes at indices with saved nodes.   
    //Return merged tree
    System.out.println("Running a merge operation!");

    TreeNode oldRoot = (TreeNode) oldTree.getRoot();
    ArrayList<DefaultMutableTreeNode> savedNodeList = new ArrayList<>();
    for (Enumeration e = ((DefaultMutableTreeNode) oldRoot).depthFirstEnumeration(); e.hasMoreElements()
            && oldRoot != null;) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        if (node.getUserObject().toString().contains("custom\\")
                || node.getUserObject().toString().contains("custom/")) {
            savedNodeList.add(node);
        }
    }
    ScriptParser parser = new ScriptParser(scriptFilePath);
    TreeModel newTree = parser.getTreeModel();
    TreeNode newRoot = (TreeNode) newTree.getRoot();
    for (DefaultMutableTreeNode savedNode : savedNodeList) {
        int rndwaveIndex = -1;
        int childIndex = -1;
        int parentIndex = -1;
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) savedNode.getParent();
        if (parent.getUserObject().toString().contains("rndwave")) {
            rndwaveIndex = parent.getParent().getIndex(parent);
            childIndex = parent.getIndex(savedNode);
            parent = (DefaultMutableTreeNode) parent.getParent();
            parentIndex = parent.getParent().getIndex(parent);
        } else {
            parentIndex = parent.getParent().getIndex(parent);
            childIndex = parent.getIndex(savedNode);
        }

        TreeNode newParentNode = newRoot.getChildAt(parentIndex);
        TreeNode newChildNode;
        if (rndwaveIndex != -1) {
            newChildNode = newParentNode.getChildAt(rndwaveIndex);
            newChildNode = newChildNode.getChildAt(childIndex);
        } else {
            newChildNode = newParentNode.getChildAt(childIndex);
        }
        newChildNode = savedNode;
    }
    return newTree;
}

From source file:org.adl.sequencer.impl.ADLSequencer.java

/**
 * Displays the values of the <code>ADLTOC</code> objects that constitute
 * table of contents. This method is used for diagnostic purposes.
 * // w ww  .  j  ava2 s  .  c  o  m
 * @param iOldTOC
 *            A List of <code>ADLTOC</code> objects describing the
 *            'first pass' TOC.
 * 
 * @param oNewTOC
 *            A List of <code>ADLTOC</code> objects describing the
 *            'final pass' TOC.
 * 
 * @return The set of valid activity IDs for 'Choice' navigation requests.
 */
private Hashtable<String, ActivityNode> getChoiceSet(TreeModel treeModel) {
    Hashtable<String, ActivityNode> set = null;
    String lastLeaf = null;

    if (treeModel != null) {
        ActivityNode tempNode = null;
        set = new Hashtable<String, ActivityNode>();

        ActivityNode rootNode = (ActivityNode) treeModel.getRoot();

        if (rootNode != null) {
            @SuppressWarnings("unchecked")
            Enumeration<ActivityNode> breadthFirst = rootNode.breadthFirstEnumeration();

            List<ActivityNode> bfList = Collections.list(breadthFirst);

            // Traverse the breadth-first search backwards
            for (int i = bfList.size() - 1; i > 0; i--) {
                tempNode = bfList.get(i);

                if (tempNode.getDepth() == -1) {
                    if (tempNode.isSelectable()) {
                        set.put(tempNode.getActivity().getID(), tempNode);
                    }
                } else if (!tempNode.isHidden()) {
                    set.put(tempNode.getActivity().getID(), tempNode);
                }

                if (lastLeaf == null) {
                    if (tempNode.isLeaf() && tempNode.isEnabled()) {
                        lastLeaf = tempNode.getActivity().getID();
                    }
                }

            }
        }
    }

    if (lastLeaf != null) {
        if (_Debug) {
            System.out.println("  ::--> Setting last leaf --> " + lastLeaf);
        }

        mSeqTree.setLastLeaf(lastLeaf);
    }

    // If there are no items in the set, there is no TOC.
    if (set != null && set.isEmpty()) {
        set = null;
    }

    // TODO: JLR -- think we might be able to live without this... 9/10/2007

    // If there is only one item in the set, it must be the root -- remove
    // it
    // If there is only one item in the set, it is the parent of a
    // choiceExit == false cluster, it cannot be selected -- no TOC
    /*if (oNewTOC.size() == 1) {
       ADLTOC temp = (ADLTOC) oNewTOC.get(0);
            
       if (!temp.mIsEnabled) {
    if (_Debug) {
       System.out.println("  ::--> Clearing single non-enabled "
             + " activity");
    }
            
    oNewTOC.remove(0);
       } else if (!temp.mLeaf) {
    if (_Debug) {
       System.out.println("  ::--> Clearing root activity");
    }
            
    oNewTOC.remove(0);
       }
    }*/

    return set;
}

From source file:org.apache.jetspeed.portlets.site.PortalSiteManager.java

private DefaultMutableTreeNode getTreeNodeByPath(String path) {
    PortalTree tree = (PortalTree) getPage().get("siteTree");
    TreeModel treeModel = tree.getModelObject();
    List<DefaultMutableTreeNode> treeNodeList = new ArrayList<DefaultMutableTreeNode>();
    findTreeNodeByPath((DefaultMutableTreeNode) treeModel.getRoot(), path, treeNodeList, 1);
    return (treeNodeList.isEmpty() ? null : treeNodeList.get(0));
}

From source file:org.apache.jetspeed.portlets.site.PortalSiteManager.java

private DefaultMutableTreeNode getSelectedNode() {
    PortalTree tree = (PortalTree) getPage().get("siteTree");
    Collection selectedNodes = tree.getTreeState().getSelectedNodes();
    DefaultMutableTreeNode treeNode = null;

    if (selectedNodes != null) {
        Iterator selectedNode = selectedNodes.iterator();
        while (selectedNode.hasNext()) {
            treeNode = (DefaultMutableTreeNode) selectedNode.next();
            break;
        }//ww  w  . j a  va2 s. c o  m
    }

    if (treeNode == null) {
        TreeModel treeModel = tree.getModelObject();

        if (treeModel != null && treeModel.getRoot() != null) {
            treeNode = (DefaultMutableTreeNode) treeModel.getRoot();
        }
    }

    if (treeNode == null) {
        treeNode = new DefaultMutableTreeNode(new SiteTreeNode("root", "/", FileType.Folder));
    }

    return treeNode;
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.models.tree.TreeModelPropertyEditor.java

@Override
protected String getText(Property property) throws Exception {
    Object value = property.getValue();
    if (value instanceof TreeModel) {
        TreeModel model = (TreeModel) value;
        TreeNode rootNode = (TreeNode) model.getRoot();
        ////  w  w w . j  av a  2s .c  o  m
        StringBuffer buffer = new StringBuffer();
        getText_appendNodes(buffer, rootNode, "");
        return buffer.toString();
    }
    // no string presentation
    return null;
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.models.tree.TreeModelPropertyEditor.java

/**
 * @return the text to display as tooltip, or <code>null</code> if {@link Property} has invalid
 *         value.//from   www .j a v a2  s . c o m
 */
private String getTooltip(Property property) throws Exception {
    Object value = property.getValue();
    if (value instanceof TreeModel) {
        TreeModel model = (TreeModel) value;
        TreeNode rootNode = (TreeNode) model.getRoot();
        //
        StringBuffer buffer = new StringBuffer();
        getTooltip_appendNodes(buffer, rootNode, "");
        return buffer.toString();
    }
    // no tooltip
    return null;
}

From source file:org.eclipse.wb.tests.designer.swing.model.component.JTreeTest.java

public void test_JTree_parsing() throws Exception {
    ContainerInfo panel = parseContainer("import javax.swing.tree.*;", "public class Test extends JPanel {",
            "  public Test() {", "    JTree tree = new JTree();", "    add(tree);",
            "    tree.setModel(new DefaultTreeModel(", "      new DefaultMutableTreeNode('(root)') {",
            "        {", "          DefaultMutableTreeNode node1 = new DefaultMutableTreeNode('1');",
            "            DefaultMutableTreeNode node2 = new DefaultMutableTreeNode('11');",
            "            node1.add(node2);", "          add(node1);",
            "          node1 = new DefaultMutableTreeNode('2');",
            "            node1.add(new DefaultMutableTreeNode('21'));", "          add(node1);", "        }",
            "      }", "    ));", "  }", "}");
    panel.refresh();/*w  w w . j  a  v a 2 s.  c om*/
    //
    ComponentInfo treeInfo = panel.getChildrenComponents().get(0);
    JTree treeObject = (JTree) treeInfo.getObject();
    // validate model
    {
        TreeModel model = treeObject.getModel();
        assertNotNull(model);
        DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) model.getRoot();
        {
            assertEquals("(root)", rootNode.getUserObject());
            assertEquals(2, rootNode.getChildCount());
        }
        {
            DefaultMutableTreeNode node_1 = (DefaultMutableTreeNode) rootNode.getChildAt(0);
            assertEquals("1", node_1.getUserObject());
            assertEquals(1, node_1.getChildCount());
            {
                DefaultMutableTreeNode node_2 = (DefaultMutableTreeNode) node_1.getChildAt(0);
                assertEquals("11", node_2.getUserObject());
                assertEquals(0, node_2.getChildCount());
            }
        }
        {
            DefaultMutableTreeNode node_1 = (DefaultMutableTreeNode) rootNode.getChildAt(1);
            assertEquals("2", node_1.getUserObject());
            assertEquals(1, node_1.getChildCount());
            {
                DefaultMutableTreeNode node_2 = (DefaultMutableTreeNode) node_1.getChildAt(0);
                assertEquals("21", node_2.getUserObject());
                assertEquals(0, node_2.getChildCount());
            }
        }
    }
    // check "model" property
    {
        Property modelProperty = treeInfo.getPropertyByTitle("model");
        PropertyEditor modelEditor = modelProperty.getEditor();
        // text
        {
            String text = (String) ReflectionUtils.invokeMethod2(modelEditor, "getText", Property.class,
                    modelProperty);
            assertEquals("(root), +1, ++11, +2, ++21", text);
        }
        // tooltip
        {
            String tooltip = getPropertyTooltipText(modelEditor, modelProperty);
            assertEquals(StringUtils.join(
                    new String[] { "(root)", "    1", "        11", "    2", "        21" }, "\n"), tooltip);
            // position
            PropertyTooltipProvider provider = modelEditor.getAdapter(PropertyTooltipProvider.class);
            assertSame(PropertyTooltipProvider.BELOW, provider.getTooltipPosition());
        }
    }
}

From source file:org.kepler.gui.SimpleLibrarySearcher.java

/**
 * Search the tree model and add any components that match any of the
 * supplied KeplerLSIDs to the results. This method does not match
 * EntityLibraries but only checks leaf nodes that are ComponentEntities.
 * //  w  w w  . j  a  v a  2  s.com
 * @param lsids
 * @param model
 */
private void findLiids(Vector<Integer> liids, TreeModel model) {
    if (isDebugging)
        log.debug("findLsids(" + liids + " " + model.getRoot() + "");

    Object o = model.getRoot();

    // start from the root
    _pathStack = new Stack<Object>();

    _pathStack.push(o);

    for (int i = 0; i < model.getChildCount(o); i++) {
        Object child = model.getChild(o, i);
        if (child instanceof NamedObj) {
            NamedObj nobjChild = (NamedObj) child;
            String name = nobjChild.getName();
            if (name.startsWith("_"))
                continue;

            _pathStack.push(child);

            if (nobjChild instanceof EntityLibrary) {
                findLiids(liids, (EntityLibrary) nobjChild);
            } else if (nobjChild instanceof ComponentEntity) {
                checkLiid((ComponentEntity) nobjChild, liids);
            }
        }
    }
}

From source file:org.kepler.objectmanager.cache.LocalRepositoryManager.java

/**
 * Refresh the folder model for the specified local repository.
 * //from   ww  w  . ja  v a  2 s. c  om
 * @param repo
 */
public void refreshFolderModelForRepo(LocalRepository repo) {
    if (isDebugging)
        log.debug("refreshFolderModelForRepo(" + repo.toString() + ")");

    String repoName = getLocalRepositories().get(repo);
    if (repoName == null) {
        log.warn("Error: not a local repository: " + repo);
        return;
    }

    TreeModel tm = getFolderModel(repoName);
    if (tm == null) {
        tm = new DefaultTreeModel(new DefaultMutableTreeNode(repo));
        _folderModel.put(repoName, tm);
    }

    DefaultMutableTreeNode root = (DefaultMutableTreeNode) tm.getRoot();
    root.removeAllChildren();

    for (File dir : repo.getDirectories()) {
        findKarsRecursive(dir, 20, root);
    }
}

From source file:org.kepler.objectmanager.cache.LocalRepositoryManager.java

/**
 * Given the File object for a folder in a local repository, return the
 * corresponding DefaultMutableTreeNode object from the Folder model.
 * //  www.  j  a va  2s.  c  om
 * @param folder
 * @return
 */
public DefaultMutableTreeNode getFolderModelNode(File folder) {
    String folderStr = folder.toString();
    for (LocalRepository repo : getLocalRepositories().keySet()) {
        for (File repoRootDir : repo.getDirectories()) {
            if (folderStr.equals(repoRootDir.toString())) {
                return (DefaultMutableTreeNode) getFolderModel(getLocalRepositories().get(repo)).getRoot();
            } else if (folderStr.startsWith(repoRootDir.toString())) {
                String remainder = folderStr.substring(repoRootDir.toString().length());
                if (remainder.startsWith(File.separator)) {
                    remainder = remainder.substring(1);
                }
                StringTokenizer st = new StringTokenizer(remainder, File.separator);

                TreeModel tm = getFolderModel(getLocalRepositories().get(repo));
                DefaultMutableTreeNode root = (DefaultMutableTreeNode) tm.getRoot();
                String dir = null;
                File current = repoRootDir;
                int count = st.countTokens();
                while (st.hasMoreTokens()) {
                    dir = st.nextToken();
                    current = new File(current.toString(), dir);
                    DefaultMutableTreeNode dmtn = checkChildren(root, current);
                    if (dmtn == null) {
                        return null;
                    }
                    if (count == 1) {
                        return dmtn;
                    } else {
                        root = dmtn;
                    }
                    count--;
                }
            }
        }
    }

    return null;
}