Example usage for javax.swing.tree DefaultMutableTreeNode getPreviousSibling

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

Introduction

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

Prototype

public DefaultMutableTreeNode getPreviousSibling() 

Source Link

Document

Returns the previous sibling of this node in the parent's children array.

Usage

From source file:EditableTree.java

private MutableTreeNode getSibling(DefaultMutableTreeNode selNode) {
    MutableTreeNode sibling = (MutableTreeNode) selNode.getPreviousSibling();
    if (sibling == null) {
        sibling = (MutableTreeNode) selNode.getNextSibling();
    }/*from w  w w  . j  av a 2  s .  c  o m*/
    return sibling;
}

From source file:Main.java

public Main() {
    DefaultMutableTreeNode forums = new DefaultMutableTreeNode("B");
    forums.add(new DefaultMutableTreeNode("T"));
    DefaultMutableTreeNode articles = new DefaultMutableTreeNode("A");
    articles.add(new DefaultMutableTreeNode("A"));
    DefaultMutableTreeNode examples = new DefaultMutableTreeNode("E");
    examples.add(new DefaultMutableTreeNode("E"));

    rootNode.add(forums);/*w w  w  .ja v a2s.  c  o m*/
    rootNode.add(articles);
    rootNode.add(examples);

    m_tree.setEditable(true);
    m_tree.setSelectionRow(0);

    JScrollPane scrollPane = new JScrollPane(m_tree);
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    m_addButton = new JButton("Add Node");
    m_addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();

            if (selNode == null) {
                return;
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
            m_model.insertNodeInto(newNode, selNode, selNode.getChildCount());

            TreeNode[] nodes = m_model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            m_tree.scrollPathToVisible(path);

            m_tree.setSelectionPath(path);

            m_tree.startEditingAtPath(path);
        }

    });
    panel.add(m_addButton);
    getContentPane().add(panel, BorderLayout.SOUTH);
    m_delButton = new JButton("Delete Node");
    m_delButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            if (selNode == null) {
                return;
            }
            MutableTreeNode parent = (MutableTreeNode) (selNode.getParent());
            if (parent == null) {
                return;
            }
            MutableTreeNode toBeSelNode = (MutableTreeNode) selNode.getPreviousSibling();
            if (toBeSelNode == null) {
                toBeSelNode = (MutableTreeNode) selNode.getNextSibling();
            }
            if (toBeSelNode == null) {
                toBeSelNode = parent;
            }
            TreeNode[] nodes = m_model.getPathToRoot(toBeSelNode);
            TreePath path = new TreePath(nodes);
            m_tree.scrollPathToVisible(path);
            m_tree.setSelectionPath(path);
            m_model.removeNodeFromParent(selNode);
        }

    });
    panel.add(m_delButton);
    getContentPane().add(panel, BorderLayout.SOUTH);

    setSize(300, 400);
    setVisible(true);
}

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

/**
 * Up concept./*from   w  w  w.j a  v  a  2 s  .c  om*/
 */
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:GUI.MainWindow.java

private void doDelete() {

    DefaultTreeModel dtm = (DefaultTreeModel) VulnTree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) dtm.getRoot();
    TreePath[] paths = VulnTree.getSelectionPaths();

    if (paths == null) {
        return;/* w  w w .  j  av a  2  s  .  com*/
    }

    for (int i = 0; i < paths.length; i++) {
        TreePath path = paths[i];
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        if (i == 0) {
            // This is the first delete operation
            DefaultMutableTreeNode previous = (DefaultMutableTreeNode) node.getPreviousSibling();
            // Consider saving deleted vulns into a scratchpad file TODO
            //System.out.println("previous:" + previous);

            // If it is null here we have no nodes above it, we get the next sibling below
            if (previous == null) {
                previous = (DefaultMutableTreeNode) node.getNextSibling();
            }

            // If it is still null here there are no nodes in the tree. Point to the root. Avoids NullPointerException
            if (previous == null) {
                previous = root;
            }

            TreePath p = new TreePath(previous.getPath());
            VulnTree.setSelectionPath(p);
        }

        if (node.getParent() != null) {
            dtm.removeNodeFromParent(node);
        }
    }

    if (root.getChildCount() == 0) {
        clearGUI();
    }

}

From source file:org.apache.cayenne.modeler.ProjectTreeView.java

/**
 * Removes current node from the tree. Selects a new node adjacent to the currently
 * selected node instead./* www . j  a va2s. c o  m*/
 */
protected void removeNode(DefaultMutableTreeNode toBeRemoved) {

    // lookup for the new selected node
    Object selectedNode = null;

    TreePath selectionPath = getSelectionPath();
    if (selectionPath != null) {
        selectedNode = selectionPath.getLastPathComponent();
    }

    if (toBeRemoved == selectedNode) {

        // first search siblings
        DefaultMutableTreeNode newSelection = toBeRemoved.getNextSibling();
        if (newSelection == null) {
            newSelection = toBeRemoved.getPreviousSibling();

            // try parent
            if (newSelection == null) {
                newSelection = (DefaultMutableTreeNode) toBeRemoved.getParent();

                // search the whole tree
                if (newSelection == null) {

                    newSelection = toBeRemoved.getNextNode();
                    if (newSelection == null) {

                        newSelection = toBeRemoved.getPreviousNode();
                    }
                }
            }
        }

        showNode(newSelection);
    }

    // remove this node
    getProjectModel().removeNodeFromParent(toBeRemoved);
}