Example usage for javax.swing.tree DefaultMutableTreeNode getNextSibling

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

Introduction

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

Prototype

public DefaultMutableTreeNode getNextSibling() 

Source Link

Document

Returns the next 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();
    }/* w  ww. jav  a2s .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);/*  ww w  . jav a  2s. com*/
    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

/**
 * Down concept./*from   w w  w .ja  va2 s  .co  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: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 ww.  j a v  a  2  s.co  m
    }

    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./*from w  w w. j a va 2s  . c om*/
 */
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);
}

From source file:streamme.visuals.Main.java

public void playNodes() {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree_files.getLastSelectedPathComponent();
    if (node != null && node.isLeaf()) {
        DataNode dn = (DataNode) node.getUserObject();
        if (!dn.isFolder()) {
            PlaylistManager pm = PlaylistManager.get();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
            pm.clearPlaylist(-1);/* w  w w  . ja v  a2  s .c o  m*/

            // Get Path:
            javax.swing.tree.TreeNode[] tree = parent.getPath();
            String path = StreamMe.OPTIONS.getOutputPath();
            for (int i = 1; i < tree.length; i++) {
                path += "\\" + ((DataNode) ((DefaultMutableTreeNode) tree[i]).getUserObject()).getName();
            }

            Enumeration ch = parent.children();
            DefaultMutableTreeNode child = null;
            while (ch.hasMoreElements()) {
                child = (DefaultMutableTreeNode) ch.nextElement();
                if (child.isLeaf())
                    break;
            }
            int songIdx = 0, i = 0;
            while (child != null) {
                DataNode childnode = (DataNode) child.getUserObject();

                if (child == node) {
                    songIdx = i;
                }
                pm.addToPlaylist(-1, childnode.toFileLink(path + "\\" + childnode.getName()));
                child = child.getNextSibling();
                i++;
            }
            playSong(pm.getDefaultModelIdx(), songIdx);
        }
    }
}