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:org.artifactory.webapp.wicket.actionable.tree.ActionableItemsTree.java

private void selectPath(DefaultTreeSelection defaultSelection) {
    if (defaultSelection == null) {
        return;//from   w  ww  .j a va 2s .  co m
    }

    String treePath = defaultSelection.getDefaultSelectionTreePath();
    if (StringUtils.isNotBlank(treePath)) {
        try {
            // now build all the nodes on the way to the destination path and
            // expand only the nodes to the destination path
            DefaultTreeModel treeModel = getTreeModel();
            ActionableItemTreeNode parentNode = (ActionableItemTreeNode) treeModel.getRoot();
            String remainingPath = treePath;
            ActionableItemTreeNode currentNode = null;
            while (PathUtils.hasText(remainingPath)) {

                // get deepest node for the path (will also take care of compacted paths)
                currentNode = defaultSelection.getNodeAt(parentNode, remainingPath);
                if (currentNode == parentNode) {
                    throw new ItemNotFoundRuntimeException(format("Child node %s not found under %s",
                            remainingPath, parentNode.getUserObject().getDisplayName()));
                }

                ActionableItem userObject = currentNode.getUserObject();
                if (userObject instanceof HierarchicActionableItem
                        && !(userObject instanceof ZipFileActionableItem)) {
                    // the node found is hierarchical, meaning it can have children
                    // so we get and create all the current node children
                    List<? extends ActionableItem> folderChildren = itemsProvider
                            .getChildren((HierarchicActionableItem) userObject);
                    setChildren(currentNode, folderChildren);
                    getTreeState().expandNode(currentNode);
                    parentNode = currentNode;
                }

                // subtract the resolved path from the remaining path
                // we are currently relying on the display name as there is
                // no better way to know if the node was compacted or not
                String displayName = userObject.getDisplayName();
                remainingPath = remainingPath.substring(displayName.length());
                // just make sure we don't have '/' at the beginning
                remainingPath = PathUtils.trimLeadingSlashes(remainingPath);
            }

            // everything went well and we have the destination node. now select it
            selectNode(currentNode);

        } catch (Exception e) {
            String message = "Unable to find path: " + treePath;
            error(message);
            log.error(message, e);
            getTreeState().collapseAll();
        }
    }
}

From source file:org.artifactory.webapp.wicket.actionable.tree.ActionableItemsTree.java

/**
 * @param item Item to look for/*from w  w  w.ja  va  2s .  c  om*/
 * @return Tree node containing this item as the user object, null if not found
 */
public ActionableItemTreeNode searchForNodeByItem(ActionableItem item) {
    DefaultTreeModel model = getTreeModel();
    ActionableItemTreeNode root = (ActionableItemTreeNode) model.getRoot();
    return searchForNodeByItem(root, item);
}

From source file:org.artifactory.webapp.wicket.actionable.tree.ActionableItemsTree.java

public TreeNode getPrevTreeNode(TreeNode node) {
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
    if (parent == null) {
        return null;
    }//from  w ww . j a va2  s.  c  o m

    TreeNode prevNode = parent.getChildBefore(node);
    if (prevNode != null) {
        ITreeState state = getTreeState();
        node = prevNode;
        while (!node.isLeaf() && node.getAllowsChildren() && state.isNodeExpanded(node)) {
            node = node.getChildAt(node.getChildCount() - 1);
        }
        return node;
    }

    DefaultTreeModel treeModel = getTreeModel();
    if (parent == treeModel.getRoot()) {
        return null;
    }
    return parent;
}

From source file:org.codinjutsu.tools.jenkins.view.BrowserPanel.java

public void setSortedByStatus(boolean selected) {
    sortedByBuildStatus = selected;/*  w w w  . jav  a 2s  .co  m*/
    final DefaultTreeModel model = (DefaultTreeModel) jobTree.getModel();
    if (selected) {
        TreeUtil.sort(model, sortByStatusComparator);
    } else {
        TreeUtil.sort(model, sortByNameComparator);
    }

    GuiUtil.runInSwingThread(new Runnable() {
        @Override
        public void run() {
            model.nodeStructureChanged((TreeNode) model.getRoot());
        }
    });
}

From source file:org.codinjutsu.tools.jenkins.view.BrowserPanel.java

private void updateJobNode(Job job) {
    final DefaultTreeModel model = (DefaultTreeModel) jobTree.getModel();
    final Object modelRoot = model.getRoot();
    final int childCount = model.getChildCount(modelRoot);
    for (int i = 0; i < childCount; ++i) {
        Object child = model.getChild(modelRoot, i);
        if (child instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) child;
            if (childNode.getUserObject() == job) {
                model.nodeChanged(childNode);
                break;
            }/*from  w w  w  .j a va2s  .com*/
        }
    }
}

From source file:org.codinjutsu.tools.jenkins.view.BrowserPanel.java

public void handleEmptyConfiguration() {
    JenkinsWidget.getInstance(project).updateStatusIcon(BuildStatusAggregator.EMPTY); //FIXME could be handled elsehwere
    DefaultTreeModel model = (DefaultTreeModel) jobTree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    root.removeAllChildren();//from   w w  w.  j  ava2 s  .  co  m
    model.nodeStructureChanged(root);
    jobTree.setRootVisible(false);

    jenkins.update(Jenkins.byDefault());

    currentSelectedView = null;
    setJobsUnavailable();
}

From source file:org.codinjutsu.tools.jenkins.view.BrowserPanel.java

private void fillJobTree(final BuildStatusVisitor buildStatusVisitor) {
    final List<Job> jobList = jenkins.getJobs();
    if (jobList.isEmpty()) {
        return;//from  ww  w.  j a  v a 2s .c  om
    }

    DefaultTreeModel model = (DefaultTreeModel) jobTree.getModel();
    final DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) model.getRoot();
    rootNode.removeAllChildren();

    for (Job job : jobList) {
        DefaultMutableTreeNode jobNode = new DefaultMutableTreeNode(job);
        rootNode.add(jobNode);
        visit(job, buildStatusVisitor);
    }

    watch();

    setSortedByStatus(sortedByBuildStatus);

    jobTree.setRootVisible(true);
}

From source file:org.codinjutsu.tools.mongo.view.MongoEditionPanel.java

public void addKey(String key, Object value) {

    List<TreeNode> node = new LinkedList<TreeNode>();
    JsonTreeNode treeNode = new JsonTreeNode(MongoKeyValueDescriptor.createDescriptor(key, value));

    if (value instanceof DBObject) {
        JsonTreeModel.processDbObject(treeNode, (DBObject) value);
    }//from  w  ww .  j  a  v a 2 s.  c  o  m

    node.add(treeNode);

    DefaultTreeModel treeModel = (DefaultTreeModel) editTableView.getTree().getModel();
    JsonTreeNode parentNode = getParentNode();
    if (parentNode == null) {
        parentNode = (JsonTreeNode) treeModel.getRoot();
    }
    TreeUtil.addChildrenTo(parentNode, node);
    treeModel.reload(parentNode);
}

From source file:org.codinjutsu.tools.nosql.mongo.view.MongoEditionPanel.java

public void addKey(String key, Object value) {

    List<TreeNode> node = new LinkedList<>();
    NoSqlTreeNode treeNode = new NoSqlTreeNode(MongoKeyValueDescriptor.createDescriptor(key, value));

    if (value instanceof Document || value instanceof List) {
        JsonTreeModel.processDocument(treeNode, value);
    }/*from  ww  w.  j  av a  2  s. co  m*/

    node.add(treeNode);

    DefaultTreeModel treeModel = (DefaultTreeModel) editTableView.getTree().getModel();
    NoSqlTreeNode parentNode = getParentNode();
    if (parentNode == null) {
        parentNode = (NoSqlTreeNode) treeModel.getRoot();
    }
    TreeUtil.addChildrenTo(parentNode, node);
    treeModel.reload(parentNode);
}

From source file:org.feistymeow.dragdrop.dragdrop_tree_test.java

public dragdrop_tree_test(String startPath) {
    super("dragdrop_test");

    // create the tree, configure it to show our hashtable nodes, and put it in
    // a scroll pane.
    larch = new DraggableDroppableTree(startPath);
    DefaultTreeModel treeModel = (DefaultTreeModel) larch.getModel();
    larch.setCellRenderer(new CustomCellRenderer());
    TreeSelectionModel selmod = new DefaultTreeSelectionModel();
    selmod.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    larch.setSelectionModel(selmod);//w  ww . ja  v a 2  s . c  o m
    larch.addTreeSelectionListener(this);
    JScrollPane listScrollPane = new JScrollPane(larch);
    // get the files that live in the specified directory.
    String dirName = startPath + "/"; // make sure we think of it as a
                                      // directory.
    String filelist[] = new File(dirName).list();
    MutableTreeNode root_node = (MutableTreeNode) treeModel.getRoot();
    if (root_node == null) {
        logger.error("something is not right about tree.  has null root.");
        System.exit(1);
    }
    // load up the tree with the files in the directory they passed.
    for (int i = 0; i < filelist.length; i++) {
        String thisFileSt = dirName + filelist[i];
        File thisFile = new File(thisFileSt);
        // skip directories for now.
        if (thisFile.isDirectory())
            continue;
        // skip dot files.
        if (filelist[i].startsWith("."))
            continue;
        try {
            // need to trap exceptions from the URI/URL functions.
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(makeNode(thisFile.getName(),
                    thisFile.toURI().toURL().toString(), thisFile.getAbsolutePath()));
            treeModel.insertNodeInto(newNode, root_node, root_node.getChildCount());
        } catch (java.net.MalformedURLException e) {
            logger.warn("caught an exception while trying to process path: " + thisFile.getAbsolutePath());
        }
    }

    // set our status bar to have the current path info.
    fileName = new JTextField(50);
    // select the root.
    larch.setSelectionPath(larch.getPathForRow(0));

    // pop out all the nodes.
    larch.expandAll();

    // Create a panel that uses FlowLayout (the default).
    JPanel buttonPane = new JPanel();
    buttonPane.add(fileName);

    Container contentPane = getContentPane();
    contentPane.add(listScrollPane, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.NORTH);
}