Example usage for javax.swing.tree DefaultMutableTreeNode insert

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

Introduction

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

Prototype

public void insert(MutableTreeNode newChild, int childIndex) 

Source Link

Document

Removes newChild from its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at index childIndex.

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode mercury = new DefaultMutableTreeNode("Mercury");
    root.insert(mercury, 0);
    DefaultMutableTreeNode venus = new DefaultMutableTreeNode("Venus");
    root.insert(venus, 1);//from ww w . j a v  a2s.  c om
    DefaultMutableTreeNode mars = new DefaultMutableTreeNode("Mars");
    root.insert(mars, 2);
    JTree tree = new JTree(root);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

private static DefaultTreeModel getTreeModel() {
    MutableTreeNode root = new DefaultMutableTreeNode("1");

    DefaultMutableTreeNode cover = new DefaultMutableTreeNode("A");
    cover.insert(new DefaultMutableTreeNode("P"), 0);
    cover.insert(new DefaultMutableTreeNode("S"), 0);
    root.insert(cover, 0);//ww  w . j  ava2  s .c  o m

    DefaultMutableTreeNode base = new DefaultMutableTreeNode("B");
    base.insert(new DefaultMutableTreeNode("S"), 0);
    base.insert(new DefaultMutableTreeNode("H"), 0);
    root.insert(base, 0);

    DefaultTreeModel model = new DefaultTreeModel(root);
    return model;
}

From source file:Main.java

/**
 * Method by Adrian: [//from   www .jav a  2 s  .co  m
 * <a href="http://stackoverflow.com/a/15704264/5620200">StackOverflow</a> ]
 * & Mike: [ <a href=
 * "http://stackoverflow.com/questions/1542170/arranging-nodes-in-a-jtree">
 * StackOverflow</a> ]
 * 
 * @param node
 * @return
 */
@SuppressWarnings("unchecked")
public static DefaultMutableTreeNode sort(DefaultMutableTreeNode node) {
    List<DefaultMutableTreeNode> children = Collections.list(node.children());
    List<String> orgCnames = new ArrayList<String>();
    List<String> cNames = new ArrayList<String>();
    DefaultMutableTreeNode temParent = new DefaultMutableTreeNode();
    for (DefaultMutableTreeNode child : children) {
        DefaultMutableTreeNode ch = (DefaultMutableTreeNode) child;
        temParent.insert(ch, 0);
        String uppser = ch.toString().toUpperCase();
        // Not dependent on package name, so if duplicates are found
        // they will later on be confused. Adding this is of
        // very little consequence and fixes the issue.
        if (cNames.contains(uppser)) {
            uppser += "$COPY";
        }
        cNames.add(uppser);
        orgCnames.add(uppser);
        if (!child.isLeaf()) {
            sort(child);
        }
    }
    Collections.sort(cNames);
    for (String name : cNames) {
        int indx = orgCnames.indexOf(name);
        int insertIndex = node.getChildCount();
        node.insert(children.get(indx), insertIndex);
    }
    // Fixing folder placement
    for (int i = 0; i < node.getChildCount() - 1; i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
        for (int j = i + 1; j <= node.getChildCount() - 1; j++) {
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) node.getChildAt(j);
            if (!prevNode.isLeaf() && child.isLeaf()) {
                node.insert(child, j);
                node.insert(prevNode, i);
            }
        }
    }
    return node;
}

From source file:OAT.ui.util.UiUtil.java

public static DefaultMutableTreeNode addChildNode(DefaultMutableTreeNode parent, Object child) {
    HashSet hashSet = new HashSet();
    DefaultMutableTreeNode childNode;

    for (int i = 0; i < parent.getChildCount(); i++) {
        hashSet.add(parent.getChildAt(i).toString());
    }/*from  ww w.  j a  va 2 s . c  o m*/

    if (hashSet.add(child.toString())) {
        childNode = new DefaultMutableTreeNode(child);
        parent.insert(childNode, parent.getChildCount());
    } else {
        int i = 0;
        do {
            childNode = (DefaultMutableTreeNode) parent.getChildAt(i++);
            //System.out.println("childNode: " + childNode);
        } while (!childNode.toString().equals(child.toString()));
    }

    return childNode;
}

From source file:Main.java

public static DefaultMutableTreeNode sortTreeNode(DefaultMutableTreeNode root) {
    if (root == null)
        return root;
    int count = root.getChildCount();
    if (count <= 0)
        return root;
    for (int i = 0; i < count; i++) {
        for (int j = count - 1; j > i; j--) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j);
            String nt = node.getUserObject().toString();
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1);
            String np = prevNode.getUserObject().toString();
            if (nt.compareToIgnoreCase(np) < 0) {
                root.insert(node, j - 1);
                root.insert(prevNode, j);
            }// ww  w.  jav a2s . co  m
        }
        sortTreeNode((DefaultMutableTreeNode) root.getChildAt(i));
    }

    for (int i = 0; i < count; i++) {
        for (int j = count - 1; j > i; j--) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j);
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1);
            if (prevNode.isLeaf() && !node.isLeaf()) {
                root.insert(node, j - 1);
                root.insert(prevNode, j);
            }
        }
    }

    return root;
}

From source file:com.openbravo.pos.admin.RolesViewTree.java

public static DefaultMutableTreeNode sortTree(DefaultMutableTreeNode root) {
    for (int i = 0; i < root.getChildCount() - 1; i++) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
        String nt = node.getUserObject().toString();
        for (int j = i + 1; j <= root.getChildCount() - 1; j++) {
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j);
            String np = prevNode.getUserObject().toString();
            if (nt.compareToIgnoreCase(np) > 0) {
                root.insert(node, j);
                root.insert(prevNode, i);
            }//from w  ww.j  a v a  2s .  co m
        }
        if (node.getChildCount() > 0) {
            node = sortTree(node);
        }
    }
    return root;
}

From source file:Main.java

private void buildTreeFromString(final DefaultTreeModel model, final String str) {
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    String[] strings = str.split("/");
    DefaultMutableTreeNode node = root;
    for (String s : strings) {
        int index = childIndex(node, s);
        if (index < 0) {
            DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(s);
            node.insert(newChild, node.getChildCount());
            node = newChild;//  ww  w .j a  v a 2 s  . c o m
        } else {
            node = (DefaultMutableTreeNode) node.getChildAt(index);
        }
    }
}

From source file:com.emental.mindraider.ui.outline.OutlineJPanel.java

/**
 * Demote concept./*from   w w w.jav  a2 s  .  c o m*/
 */
public void conceptDemote() {
    // current node becomes the first child of the previous sibling, if node
    // has index 0, then do nothing
    DefaultMutableTreeNode node = getSelectedTreeNode();
    if (node != null) {
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
        if (parent != null) {
            DefaultMutableTreeNode previous = (DefaultMutableTreeNode) parent.getChildBefore(node);
            if (previous != null) {
                parent.remove(node);
                previous.insert(node, previous.getChildCount());
                treeTable.updateUI();

                logger.debug(Messages.getString("NotebookOutlineJPanel.conceptDemoted"));

                MindRaider.noteCustodian.demote(MindRaider.outlineCustodian.getActiveOutlineResource(),
                        ((OutlineNode) parent).getUri(), ((OutlineNode) node).getUri());

                setSelectedTreeNodeConcept(((OutlineNode) node).getUri());
            } else {
                logger.debug(Messages.getString("NotebookOutlineJPanel.isTheFirstChild"));
            }
        } else {
            logger.debug(Messages.getString("NotebookOutlineJPanel.noParent"));
        }
    } else {
        logger.debug(Messages.getString("NotebookOutlineJPanel.noNodeSelected!"));
    }
}

From source file:com.emental.mindraider.ui.outline.OutlineJPanel.java

/**
 * Promote concept.//from w ww. j av  a  2 s  .c om
 */
public void conceptPromote() {
    // imagine the row and move it to the level up (left) in this row
    // node must be stored as parent's parent
    DefaultMutableTreeNode node = getSelectedTreeNode();
    if (node != null) {
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
        if (parent != null) {
            DefaultMutableTreeNode pparent = (DefaultMutableTreeNode) parent.getParent();
            if (pparent != null) {
                // now put it right behind previous parent
                int parentIndex = pparent.getIndex(parent);

                parent.remove(node);
                pparent.insert(node, parentIndex + 1);
                treeTable.updateUI();
                logger.debug(Messages.getString("NotebookOutlineJPanel.noNodePromoted"));

                MindRaider.noteCustodian.promote(MindRaider.outlineCustodian.getActiveOutlineResource(),
                        ((OutlineNode) pparent).getUri(), ((OutlineNode) parent).getUri(),
                        ((OutlineNode) node).getUri());
            } else {
                logger.debug(Messages.getString("NotebookOutlineJPanel.noParentsParent"));
            }
        } else {
            logger.debug(Messages.getString("NotebookOutlineJPanel.noParent"));
        }
    } else {
        logger.debug(Messages.getString("NotebookOutlineJPanel.noNodeSelected"));
    }
}

From source file:com.emental.mindraider.ui.outline.OutlineJPanel.java

/**
 * Down concept.//from  w  w  w  . j a  va2  s . c  o m
 */
public boolean conceptDown() {
    // move concept down in the tree
    DefaultMutableTreeNode node = getSelectedTreeNode();
    if (node != null) {
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
        if (parent != null) {
            DefaultMutableTreeNode nextSibling = node.getNextSibling();
            if (nextSibling != null) {
                if (MindRaider.noteCustodian.down(MindRaider.outlineCustodian.getActiveOutlineResource(),
                        ((OutlineNode) parent).getUri(), ((OutlineNode) node).getUri())) {
                    int siblingIndex = parent.getIndex(nextSibling);
                    parent.remove(node);
                    parent.insert(node, siblingIndex);
                    treeTable.updateUI();
                    logger.debug("Node moved down!");
                    return true;
                }
                // else node the last in the sequence
            } else {
                logger.debug("No sibling!");
            }
        } else {
            logger.debug(Messages.getString("NotebookOutlineJPanel.noParent"));
        }
    } else {
        logger.debug(Messages.getString("NotebookOutlineJPanel.noNodeSelected"));
    }
    return false;
}