Example usage for javax.swing.tree DefaultMutableTreeNode remove

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

Introduction

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

Prototype

public void remove(MutableTreeNode aChild) 

Source Link

Document

Removes aChild from this node's child array, giving it a null parent.

Usage

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

/**
 * Demote concept./*  ww  w.j  a v a 2  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  a2s .co  m*/
 */
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.pironet.tda.SunJDKParser.java

private void reNormalizeBlockingThreadTree(final MonitorMap mmap,
        final Map<String, DefaultMutableTreeNode> directChildMap) {
    Map<String, DefaultMutableTreeNode> allBlockingThreadsMap = new HashMap<>(directChildMap); // All threads that are blocking at least one other thread

    // First, re-normalize based on monitors to get our unique tree
    // Tree will be unique as long as there are no deadlocks aka monitor loops
    for (Iterator iter = mmap.iterOfKeys(); iter.hasNext();) {
        String monitor1 = (String) iter.next();
        Map<String, String>[] threads1 = mmap.getFromMonitorMap(monitor1);

        final DefaultMutableTreeNode thread1Node = allBlockingThreadsMap.get(monitor1);
        if (thread1Node == null) {
            continue;
        }/*  ww w. j a  v a  2s .co  m*/

        // Get information on the one thread holding this lock
        Iterator it = threads1[MonitorMap.LOCK_THREAD_POS].keySet().iterator();
        if (!it.hasNext()) {
            continue;
        }
        String threadLine1 = (String) it.next();

        for (Iterator iter2 = mmap.iterOfKeys(); iter2.hasNext();) {
            String monitor2 = (String) iter2.next();
            if (monitor1 == monitor2) {
                continue;
            }

            Map<String, String>[] threads2 = mmap.getFromMonitorMap(monitor2);
            if (threads2[MonitorMap.WAIT_THREAD_POS].containsKey(threadLine1)) {
                // Get the node of the thread that is holding this lock
                DefaultMutableTreeNode thread2Node = (DefaultMutableTreeNode) allBlockingThreadsMap
                        .get(monitor2);
                // Get the node of the monitor itself
                DefaultMutableTreeNode monitor2Node = (DefaultMutableTreeNode) thread2Node.getFirstChild();

                // If a redundant node for thread2 exists with no children, remove it
                // To compare, we have to remove "Thread - " from the front of display strings
                for (int i = 0; i < monitor2Node.getChildCount(); i++) {
                    DefaultMutableTreeNode child2 = (DefaultMutableTreeNode) monitor2Node.getChildAt(i);
                    if (child2.toString().substring(9).equals(threadLine1) && child2.getChildCount() == 0) {
                        monitor2Node.remove(i);
                        break;
                    }
                }

                // Thread1 is blocked by monitor2 held by thread2, so move thread1 under thread2
                monitor2Node.insert(thread1Node, 0);
                directChildMap.remove(monitor1);
                break;
            }
        }
    }

    allBlockingThreadsMap.clear();

    // Second, re-normalize top level based on threads for cases where one thread holds multiple monitors
    boolean changed;
    do {
        changed = false;
        for (final Object o : directChildMap.entrySet()) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) ((Map.Entry) o).getValue();
            if (checkForDuplicateThreadItem(directChildMap, node)) {
                changed = true;
                break;
            }
        }
    } while (changed);

    // Third, renormalize lower levels of the tree based on threads for cases where one thread holds multiple monitors
    for (final Object o : directChildMap.entrySet()) {
        renormalizeThreadDepth((DefaultMutableTreeNode) ((Map.Entry) o).getValue());
    }
}

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

/**
 * Down concept./*  w  ww.ja  v  a2s  .  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;
}

From source file:com.pironet.tda.SunJDKParser.java

private void renormalizeMonitorDepth(DefaultMutableTreeNode monitorNode, int index) {
    // First, remove all duplicates of the item at index "index"
    DefaultMutableTreeNode threadNode1 = (DefaultMutableTreeNode) monitorNode.getChildAt(index);
    ThreadInfo mi1 = (ThreadInfo) threadNode1.getUserObject();
    int i = index + 1;
    while (i < monitorNode.getChildCount()) {
        DefaultMutableTreeNode threadNode2 = (DefaultMutableTreeNode) monitorNode.getChildAt(i);
        ThreadInfo mi2 = (ThreadInfo) threadNode2.getUserObject();
        if (mi1.getName().equals(mi2.getName())) {
            if (threadNode2.getChildCount() > 0) {
                threadNode1.add((DefaultMutableTreeNode) threadNode2.getFirstChild());
                monitorNode.remove(i);
                continue;
            }//from   w w  w  . jav  a2s  .  c om
        }
        i++;
    }

    // Second, recurse into item "index"
    renormalizeThreadDepth(threadNode1);
}

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

/**
 * Up concept./*from  w w  w  . ja  va 2  s  .c o m*/
 */
public boolean conceptUp() {
    // move concept up in the tree
    DefaultMutableTreeNode node = getSelectedTreeNode();
    if (node != null) {
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
        if (parent != null) {
            DefaultMutableTreeNode previousSibling = node.getPreviousSibling();
            if (previousSibling != null) {
                // move it up in the model
                if (MindRaider.noteCustodian.up(MindRaider.outlineCustodian.getActiveOutlineResource(),
                        ((OutlineNode) parent).getUri(), ((OutlineNode) node).getUri())) {
                    int siblingIndex = parent.getIndex(previousSibling);
                    parent.remove(node);
                    parent.insert(node, siblingIndex);
                    treeTable.updateUI();
                    logger.debug(Messages.getString("NotebookOutlineJPanel.noMovedUp"));
                    return true;
                }
                // else it is the first concept in the sequence
            } else {
                logger.debug("No sibling!");
            }
        } else {
            logger.debug(Messages.getString("NotebookOutlineJPanel.noParent"));
        }
    } else {
        logger.debug(Messages.getString("NotebookOutlineJPanel.noNodeSelected"));
    }
    return false;
}

From source file:hr.fer.zemris.vhdllab.view.explorer.ProjectExplorerView.java

private void updateHierarchy(DefaultMutableTreeNode parentNode, Hierarchy hierarchy,
        HierarchyNode hierarchyNode) {/* w ww .  j  a  v a  2  s  .c  o  m*/
    Map<Object, DefaultMutableTreeNode> map = new HashMap<Object, DefaultMutableTreeNode>(
            parentNode.getChildCount());
    for (int i = 0; i < parentNode.getChildCount(); i++) {
        DefaultMutableTreeNode c = (DefaultMutableTreeNode) parentNode.getChildAt(i);
        map.put(c.getUserObject(), c);
    }

    Iterator<HierarchyNode> iterator = getHierarchyIterator(hierarchy, hierarchyNode);
    while (iterator.hasNext()) {
        HierarchyNode next = iterator.next();
        File file = next.getFile();
        DefaultMutableTreeNode nextParentNode;
        if (map.containsKey(file)) {
            nextParentNode = map.remove(file);
        } else {
            nextParentNode = (DefaultMutableTreeNode) insertNode(parentNode, file);
        }
        updateHierarchy(nextParentNode, hierarchy, next);
    }

    // model.nodesWereRemoved expected ordered indices
    Map<Integer, DefaultMutableTreeNode> sortedMap = new TreeMap<Integer, DefaultMutableTreeNode>();
    for (DefaultMutableTreeNode n : map.values()) {
        int index = parentNode.getIndex(n);
        sortedMap.put(Integer.valueOf(index), n);
    }

    // construct arguments for model.nodesWereRemoved
    int[] childIndices = new int[sortedMap.size()];
    Object[] removedChildren = new Object[sortedMap.size()];
    int i = 0;
    for (Entry<Integer, DefaultMutableTreeNode> entry : sortedMap.entrySet()) {
        childIndices[i] = entry.getKey();
        removedChildren[i] = entry.getValue();
        i++;
        parentNode.remove(entry.getValue());
    }
    model.nodesWereRemoved(parentNode, childIndices, removedChildren);
}

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./* w w w. ja v  a  2 s .  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()));
    }
}