Example usage for javax.swing.tree DefaultTreeModel insertNodeInto

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

Introduction

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

Prototype

public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) 

Source Link

Document

Invoked this to insert newChild at location index in parents children.

Usage

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  w  w  w . j a va 2 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: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);//from  www . j av a  2  s . co  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);
}

From source file:org.fseek.simon.swing.util.TreeUtil.java

private static void fakeNode(LinkTreeNode node, DefaultTreeModel model) {
    Debug.println("Faking node: " + node.getUserObject().toString());
    deleteAllChilds(node, model, null);/*from   w w  w. ja  va2  s. c om*/
    if (Thread.interrupted()) {
        return;
    }
    model.insertNodeInto(new DefaultMutableTreeNode(), node, 0);
}

From source file:org.fseek.simon.swing.util.TreeUtil.java

private static void addFiles(File[] files, final LinkTreeNode root, final DefaultTreeModel model,
        boolean showFiles, boolean showHidden) {
    for (int i = 0; i < files.length; i++) {
        if (Thread.interrupted()) {
            break;
        }/*from   w  ww. j  a  v a 2 s  .  co m*/
        File f = files[i];
        if (f.isDirectory() || showFiles == true) {
            final LinkTreeNode childNode = new DefaultLinkTreeNode(f, new IconChangedListener() {
                @Override
                public void iconChanged(final LinkTreeNode node, Icon newIcon) {
                    EventQueue.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            model.nodeChanged(node);
                        }
                    });
                }
            });
            final int index = i;
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    model.insertNodeInto(childNode, root, index);
                }
            });
            AddChildsThread create = AddChildsThread.create(model, childNode, false, showFiles, showHidden,
                    true);
            if (create.isAlive() && create.isFake() == false) {
                create.interrupt();
                try {
                    create.join();
                    create = AddChildsThread.create(model, childNode, false, showFiles, showHidden, true);
                    create.start();
                } catch (InterruptedException ex) {
                    return;
                }
            } else if (create.isAlive() == false) {
                create.start();
            }
        }
    }
}

From source file:org.kuali.test.ui.components.repositorytree.RepositoryTree.java

private void addRepositoryNodes(DefaultMutableTreeNode pnode, List repositoryObjects) {
    if (pnode != null) {
        DefaultTreeModel model = getModel();
        for (Object repositoryObject : repositoryObjects) {
            model.insertNodeInto(new RepositoryNode(configuration, repositoryObject), pnode,
                    pnode.getChildCount());
        }//from  w ww  .j  ava2  s.c o  m
    }
}

From source file:org.mbari.aved.ui.classifier.knowledgebase.ConceptTree.java

/**
 * Sets the parent node of the currently selected node to be the node
 * representing the <code>Concept</code> of the specified name.
 *
 * @param  newParentName   The name of the <code>Concept</code> for which the currently selected
 *  node is to become a child./*from  w ww.ja va 2s. c  o m*/
 */
public void updateTreeNodeParent(String newParentName) {

    // Get the node being moved
    DefaultMutableTreeNode conceptNode = (DefaultMutableTreeNode) getSelectionPath().getLastPathComponent();
    String conceptNodeName = ((TreeConcept) conceptNode.getUserObject()).getName();
    DefaultTreeModel treeModel = (DefaultTreeModel) getModel();

    // Remove node from current parent node and update structure
    DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) conceptNode.getParent();

    parentNode.remove(conceptNode);
    treeModel.nodeStructureChanged(parentNode);

    // Get the new parent node
    DefaultMutableTreeNode newParentNode = expandDownToNode(newParentName);
    TreeConcept treeConcept = (TreeConcept) newParentNode.getUserObject();
    boolean parentNeededExpanding = treeConcept.lazyExpand(newParentNode);

    // Branch on whether parent needed expanding:
    // - The parent node needed to be expanded. The call to lazyExpand()
    // updates the parent node's children so we don't need to explicitly add
    // the new child node. Find and select the new child node.
    // - The parent node is already expanded, so insert the new child node in
    // the appropriate slot and select the new child node.
    if (parentNeededExpanding) {
        Enumeration children = newParentNode.children();

        while (children.hasMoreElements()) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) children.nextElement();
            String nodeName = ((TreeConcept) node.getUserObject()).getName();

            if (nodeName.equals(conceptNodeName)) {
                setSelectionPath(new TreePath(node.getPath()));

                break;
            }
        }
    } else {

        // Insert the node at the appropriate point in the new parent node.
        int insertPosition = 0;
        Enumeration children = newParentNode.children();

        while (children.hasMoreElements()) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) children.nextElement();
            String nodeName = ((TreeConcept) node.getUserObject()).getName();

            if (0 < nodeName.compareTo(conceptNodeName)) {
                break;
            } else {
                insertPosition++;
            }
        }

        treeModel.insertNodeInto(conceptNode, newParentNode, insertPosition);
        setSelectionPath(new TreePath(conceptNode.getPath()));
    }
}

From source file:org.openmicroscopy.shoola.agents.treeviewer.browser.BrowserUI.java

/**
 * Creates an experimenter node hosting the passed experimenter.
 * /*from  w ww .  jav a 2s. c  o  m*/
 * @param exp   The experimenter to add.
 * @param groupNode The node to attach the experimenter to.
 * @return See above.
 */
private TreeImageSet createExperimenterNode(ExperimenterData exp, TreeImageDisplay groupNode) {
    DefaultTreeModel tm = (DefaultTreeModel) treeDisplay.getModel();
    TreeImageSet node = new TreeImageSet(exp);
    switch (model.getBrowserType()) {
    case Browser.IMAGES_EXPLORER:
        createTimeElements(node);
        break;
    case Browser.FILES_EXPLORER:
        createFileElements(node);
        break;
    case Browser.TAGS_EXPLORER:
        createTagsElements(node);
        break;
    default:
        buildEmptyNode(node);
    }
    //TreeImageDisplay root = getTreeRoot();
    //root.addChildDisplay(node);
    //tm.insertNodeInto(node, root, root.getChildCount());
    groupNode.addChildDisplay(node);
    tm.insertNodeInto(node, groupNode, groupNode.getChildCount());
    return node;
}

From source file:org.openmicroscopy.shoola.agents.treeviewer.browser.BrowserUI.java

/**
 * Creates folders hosting <code>FileAnnotation</code>s.
 * /*w w w . j a v a  2  s . c o  m*/
 * @param parent The parent of the elements.
 */
private void createFileElements(TreeImageSet parent) {
    DefaultTreeModel tm = (DefaultTreeModel) treeDisplay.getModel();
    TreeFileSet node;
    for (int i = 0; i < VALUES.length; i++) {
        node = new TreeFileSet(VALUES[i]);
        buildEmptyNode(node);
        node.setNumberItems(-1);
        parent.addChildDisplay(node);
        tm.insertNodeInto(node, parent, parent.getChildCount());
    }
}

From source file:org.openmicroscopy.shoola.agents.treeviewer.browser.BrowserUI.java

/**
 * Creates the smart folders added to the passed node.
 * /*w  ww.j a  v  a2  s  .c  o  m*/
 * @param parent The parent of the smart folder.
 */
private void createTagsElements(TreeImageDisplay parent) {
    if (model.getDisplayMode() == TreeViewer.GROUP_DISPLAY)
        return;
    DefaultTreeModel tm = (DefaultTreeModel) treeDisplay.getModel();
    TreeFileSet node = new TreeFileSet(TreeFileSet.TAG);
    buildEmptyNode(node);
    node.setNumberItems(-1);
    parent.addChildDisplay(node);
    tm.insertNodeInto(node, parent, parent.getChildCount());
}

From source file:org.openmicroscopy.shoola.agents.treeviewer.browser.BrowserUI.java

/**
 * Creates and returns a {@link TreeImageTimeSet}.
 * //from  w  w  w .jav  a  2s  .  c om
 * @param index    One of the following constants: 
 *                {@link TreeImageTimeSet#YEAR} or 
 *                {@link TreeImageTimeSet#WEEK}
 * @param parent   The parent of the new node.
 * @param empty      Pass <code>true</code> to add an empty node,
 *                <code>false</code> otherwise.
 * @return See above.
 */
private TreeImageTimeSet createTimeNode(int index, TreeImageSet parent, boolean empty) {
    DefaultTreeModel tm = (DefaultTreeModel) treeDisplay.getModel();
    TreeImageTimeSet date = new TreeImageTimeSet(index);
    if (empty)
        buildEmptyNode(date);
    parent.addChildDisplay(date);
    tm.insertNodeInto(date, parent, parent.getChildCount());
    return date;
}