Example usage for javax.swing.tree DefaultTreeModel getRoot

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

Introduction

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

Prototype

public Object getRoot() 

Source Link

Document

Returns the root of the tree.

Usage

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

/**
 * Find a matching individual in the tree.
 * //from  www. j  a v  a 2  s  .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:src.gui.ItSIMPLE.java

public void updateCloseProjectParallelTree(DefaultTreeModel treeModel, int index) {

    //Close the projects in the plan simulation problem tree
    ItTreeNode treeRoot = (ItTreeNode) treeModel.getRoot();
    treeModel.removeNodeFromParent((ItTreeNode) treeRoot.getChildAt(index));

}

From source file:src.gui.ItSIMPLE.java

public void updateDeleteDomainProjectParallelTree(DefaultTreeModel treeModel, ItTreeNode domain,
        ItTreeNode project) {//  ww  w .  ja  va  2 s  .c o m

    ItTreeNode problemsTreeProject = (ItTreeNode) ((ItTreeNode) treeModel.getRoot())
            .getChildAt(treeRoot.getIndex(project));

    ItTreeNode problemsTreeDomain = null;
    // look for the domain in problems plan tree
    for (int i = 0; i < problemsTreeProject.getChildCount(); i++) {
        ItTreeNode child = (ItTreeNode) problemsTreeProject.getChildAt(i);
        if (child.getData() == domain.getData()) {
            problemsTreeDomain = child;
            break;
        }
    }

    if (problemsTreeDomain != null) {
        treeModel.removeNodeFromParent(problemsTreeDomain);
    }
}

From source file:src.gui.ItSIMPLE.java

/**
    * This method should be called every time the user changes
    * the names of elements in the project tree.
    *///from w ww.j a va  2 s. c  om
private void updateTreeChanges(DefaultTreeModel treeModel) {
    //1. get tree root
    ItTreeNode problemsPlanTreeRoot = (ItTreeNode) treeModel.getRoot();

    //1.1 for each project...
    for (int projectIndex = 0; projectIndex < problemsPlanTreeRoot.getChildCount(); projectIndex++) {
        ItTreeNode project = (ItTreeNode) problemsPlanTreeRoot.getChildAt(projectIndex);

        //1.2 update project name
        String projectName = project.getData().getChildText("name");
        if (!projectName.equals(project.getUserObject())) {
            project.setUserObject(projectName);
            treeModel.nodeChanged(project);
        }

        //Check if it is a UML project
        if (project.getData().getName().equals("project")) {
            //1.3 for each domain in project...
            for (int domainIndex = 0; domainIndex < project.getChildCount(); domainIndex++) {
                ItTreeNode domain = (ItTreeNode) project.getChildAt(domainIndex);

                // 1.4 update domain name
                String domainName = domain.getData().getChildText("name");
                if (!domainName.equals(domain.getUserObject())) {
                    domain.setUserObject(domainName);
                    treeModel.nodeChanged(domain);
                }

                //1.5 for each problem in domain...
                for (int problemIndex = 0; problemIndex < domain.getChildCount(); problemIndex++) {
                    ItTreeNode problem = (ItTreeNode) domain.getChildAt(problemIndex);

                    //1.6 update problem name
                    String problemName = problem.getData().getChildText("name");
                    if (!problemName.equals(problem.getUserObject())) {
                        problem.setUserObject(problemName);
                        treeModel.nodeChanged(problem);
                    }
                }
            }

        }
        //Check if it is a PDDL project
        else if (project.getData().getName().equals("pddlproject")) {
            //1.3 for each problem instance in project...
            for (int problemIndex = 0; problemIndex < project.getChildCount(); problemIndex++) {
                ItTreeNode problem = (ItTreeNode) project.getChildAt(problemIndex);

                // 1.4 update domain name
                String domainName = problem.getData().getChildText("name");
                if (!domainName.equals(problem.getUserObject())) {
                    problem.setUserObject(domainName);
                    treeModel.nodeChanged(problem);
                }

            }

        }

    }

}

From source file:src.gui.ItSIMPLE.java

public void updateNewDomainProjectParallelTree(DefaultTreeModel treeModel, Element domain,
        ItTreeNode selectedNode) {/*from w  w w  .  j a va 2s. co  m*/

    ItTreeNode problemsTreeProjectNode = (ItTreeNode) ((ItTreeNode) treeModel.getRoot())
            .getChildAt(treeRoot.getIndex(selectedNode));

    ItTreeNode problemsTreeDomainNode = new ItTreeNode(domain.getChildText("name"), domain, null, null);
    problemsTreeDomainNode.setIcon(new ImageIcon("resources/images/domain.png"));

    treeModel.insertNodeInto(problemsTreeDomainNode, problemsTreeProjectNode,
            problemsTreeProjectNode.getChildCount());

    List<?> problems = domain.getChild("planningProblems").getChildren("problem");
    for (Iterator<?> iterator = problems.iterator(); iterator.hasNext();) {
        Element problem = (Element) iterator.next();
        ItTreeNode planProblemNode = new ItTreeNode(problem.getChildText("name"), problem, null, null);
        planProblemNode.setIcon(new ImageIcon("resources/images/planningProblem.png"));
        treeModel.insertNodeInto(planProblemNode, problemsTreeDomainNode,
                problemsTreeDomainNode.getChildCount());
    }
}

From source file:src.gui.ItSIMPLE.java

public void updateDuplicateProblemProjectParallelTree(DefaultTreeModel treeModel, Element problem,
        ItTreeNode selectedNode, ItTreeNode project) {

    ItTreeNode problemsTreeProject = (ItTreeNode) ((ItTreeNode) treeModel.getRoot())
            .getChildAt(treeRoot.getIndex(project));

    ItTreeNode problemsTreeDomain = null;
    // look for the domain in problems plan tree
    for (int i = 0; i < problemsTreeProject.getChildCount(); i++) {
        ItTreeNode child = (ItTreeNode) problemsTreeProject.getChildAt(i);
        if (child.getData() == selectedNode.getData().getParentElement().getParentElement()) {
            problemsTreeDomain = child;//from ww  w . ja  v  a 2s.  co  m
            break;
        }
    }
    if (problemsTreeDomain != null) {
        ItTreeNode problemsTreeProblem = new ItTreeNode(problem.getChildText("name"), problem, null, null);

        problemsTreeProblem.setIcon(new ImageIcon("resources/images/planningProblem.png"));

        treeModel.insertNodeInto(problemsTreeProblem, problemsTreeDomain, problemsTreeDomain.getChildCount());
    }

}

From source file:src.gui.ItSIMPLE.java

public void updateNewProblemProjectParallelTree(DefaultTreeModel treeModel, Element problem,
        ItTreeNode selectedNode, ItTreeNode project) {

    ItTreeNode problemsTreeProject = (ItTreeNode) ((ItTreeNode) treeModel.getRoot())
            .getChildAt(treeRoot.getIndex(project));
    //selectedNode.getParent()

    ItTreeNode problemsTreeDomain = null;
    // look for the domain in problems plan tree
    for (int i = 0; i < problemsTreeProject.getChildCount(); i++) {
        ItTreeNode child = (ItTreeNode) problemsTreeProject.getChildAt(i);
        if (child.getData() == selectedNode.getData()) {
            problemsTreeDomain = child;//w w w  .  j ava2 s  .  co  m
            break;
        }
    }

    if (problemsTreeDomain != null) {
        ItTreeNode problemsTreeProblem = new ItTreeNode(problem.getChildText("name"), problem, null, null);

        problemsTreeProblem.setIcon(new ImageIcon("resources/images/planningProblem.png"));

        treeModel.insertNodeInto(problemsTreeProblem, problemsTreeDomain, problemsTreeDomain.getChildCount());
    }

}

From source file:src.gui.ItSIMPLE.java

public void updateDeleteProblemProjectParallelTree(DefaultTreeModel treeModel, ItTreeNode domain,
        ItTreeNode problem, ItTreeNode project) {

    ItTreeNode problemsTreeProject = (ItTreeNode) ((ItTreeNode) treeModel.getRoot())
            .getChildAt(treeRoot.getIndex(project));

    ItTreeNode problemsTreeDomain = null;
    // look for the domain in problems plan tree
    for (int i = 0; i < problemsTreeProject.getChildCount(); i++) {
        ItTreeNode child = (ItTreeNode) problemsTreeProject.getChildAt(i);
        if (child.getData() == domain.getData()) {
            problemsTreeDomain = child;//from   ww  w.ja va 2 s.  co  m
            break;
        }
    }

    if (problemsTreeDomain != null) {

        ItTreeNode problemsTreeProblem = null;
        // look for the problem in problems plan tree
        for (int i = 0; i < problemsTreeDomain.getChildCount(); i++) {
            ItTreeNode child = (ItTreeNode) problemsTreeDomain.getChildAt(i);
            if (child.getData() == problem.getData()) {
                problemsTreeProblem = child;
                break;
            }
        }

        treeModel.removeNodeFromParent(problemsTreeProblem);
    }
}

From source file:src.gui.ItSIMPLE.java

/**
 * This method updates any parallel tree (e.g., planning, analysis) when a new project is opened or created
 * @param doc/*from w  w w . j a v  a  2  s  .  co  m*/
 * @param xmlRoot
 * @param newProjectNode 
 */
public void updateNewProjectParallelTree(DefaultTreeModel treeModel, JTree tree, Document doc, Element xmlRoot,
        ItTreeNode newProjectNode) {

    // plan simulation problem tree
    ItTreeNode problemsPlanTreeRoot = (ItTreeNode) treeModel.getRoot();
    //opening
    //ItTreeNode planProjectNode = (ItTreeNode)newProjectNode.clone();
    //new project
    //ItTreeNode planProjectNode = new ItTreeNode(xmlRoot.getChildText("name"), xmlRoot, null, null);
    ItTreeNode planProjectNode = newProjectNode;
    planProjectNode.setIcon(new ImageIcon("resources/images/project.png"));
    treeModel.insertNodeInto(planProjectNode, problemsPlanTreeRoot, problemsPlanTreeRoot.getChildCount());
    //check if this is a UML project
    if (xmlRoot.getName().equals("project")) {
        List<?> domains = doc.getRootElement().getChild("diagrams").getChild("planningDomains")
                .getChildren("domain");
        for (Iterator<?> iter = domains.iterator(); iter.hasNext();) {
            Element domain = (Element) iter.next();
            ItTreeNode planDomainNode = new ItTreeNode(domain.getChildText("name"), domain, null, null);
            planDomainNode.setIcon(new ImageIcon("resources/images/domain.png"));
            treeModel.insertNodeInto(planDomainNode, planProjectNode, planProjectNode.getChildCount());
            List<?> problems = domain.getChild("planningProblems").getChildren("problem");
            for (Iterator<?> iterator = problems.iterator(); iterator.hasNext();) {
                Element problem = (Element) iterator.next();
                ItTreeNode planProblemNode = new ItTreeNode(problem.getChildText("name"), problem, null, null);
                planProblemNode.setIcon(new ImageIcon("resources/images/planningProblem.png"));
                treeModel.insertNodeInto(planProblemNode, planDomainNode, planDomainNode.getChildCount());
            }
        }

    }
    //check if this is a PDDL project
    else if (xmlRoot.getName().equals("pddlproject")) {

        List<?> problems = doc.getRootElement().getChild("problemInstances").getChildren("pddlproblem");
        for (Iterator<?> iter = problems.iterator(); iter.hasNext();) {
            Element problem = (Element) iter.next();
            ItTreeNode planpddlProblemNode = new ItTreeNode(problem.getChildText("name"), problem, null, null);
            planpddlProblemNode.setIcon(new ImageIcon("resources/images/domain.png"));
            treeModel.insertNodeInto(planpddlProblemNode, planProjectNode, planProjectNode.getChildCount());
        }
    }
    tree.expandRow(0);
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositMain.java

private void setSortBy() {
    DefaultTreeModel model = (DefaultTreeModel) treeFileSystem.getModel();
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) model.getRoot();
    if (node.getUserObject() instanceof FileSystemObject) {
        FileSystemObject fso = (FileSystemObject) node.getUserObject();
        fso.setSortBy(getCurrentSortBy());
        theAppProperties.getApplicationData().getPersonalSettings().setSortFilesBy(getCurrentSortBy());
        depositPresenter.addFileSystemRoot(fso, false, false, theCurrentDirectory);
    }/*from  w w  w  . ja  v  a  2s.  c  o  m*/
}