Example usage for javax.swing.tree DefaultMutableTreeNode getParent

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

Introduction

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

Prototype

public TreeNode getParent() 

Source Link

Document

Returns this node's parent or null if this node has no parent.

Usage

From source file:Main.java

public static boolean isChildNode(DefaultMutableTreeNode parent, DefaultMutableTreeNode child) {
    if (parent == child)
        return false;
    DefaultMutableTreeNode node = child;
    while ((node = (DefaultMutableTreeNode) node.getParent()) != null) {
        if (node.getUserObject() == parent.getUserObject())
            return true;
    }/*from   ww w.j  a  v  a2 s. c o  m*/
    return false;
}

From source file:EditableTree.java

public void removeNode(DefaultMutableTreeNode selNode) {
    if (selNode == null) {
        return;//from www .ja v  a  2s . com
    }
    MutableTreeNode parent = (MutableTreeNode) (selNode.getParent());
    if (parent == null) {
        return;
    }
    MutableTreeNode toBeSelNode = getSibling(selNode);
    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);
}

From source file:Main.java

public void removeCurrentNode() {
    TreePath currentSelection = tree.getSelectionPath();
    if (currentSelection != null) {
        DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection.getLastPathComponent());
        MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
        if (parent != null) {
            treeModel.removeNodeFromParent(currentNode);
            return;
        }//from  www  .java  2  s .c  om
    }
}

From source file:DynamicTreeDemo.java

/** Remove the currently selected node. */
public void removeCurrentNode() {
    TreePath currentSelection = tree.getSelectionPath();
    if (currentSelection != null) {
        DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection.getLastPathComponent());
        MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
        if (parent != null) {
            treeModel.removeNodeFromParent(currentNode);
            return;
        }//w  w w.j a va 2 s.  co  m
    }

    // Either there was no selection, or the root was selected.
    toolkit.beep();
}

From source file:TreeEditTest.java

/**
 * Makes the buttons to add a sibling, add a child, and delete a node.
 *///from w w  w.  j a  v  a  2 s  .c  om
public void makeButtons() {
    JPanel panel = new JPanel();
    JButton addSiblingButton = new JButton("Add Sibling");
    addSiblingButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (selectedNode == null)
                return;

            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();

            if (parent == null)
                return;

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");

            int selectedIndex = parent.getIndex(selectedNode);
            model.insertNodeInto(newNode, parent, selectedIndex + 1);

            // now display new node

            TreeNode[] nodes = model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            tree.scrollPathToVisible(path);
        }
    });
    panel.add(addSiblingButton);

    JButton addChildButton = new JButton("Add Child");
    addChildButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (selectedNode == null)
                return;

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
            model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());

            // now display new node

            TreeNode[] nodes = model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            tree.scrollPathToVisible(path);
        }
    });
    panel.add(addChildButton);

    JButton deleteButton = new JButton("Delete");
    deleteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (selectedNode != null && selectedNode.getParent() != null)
                model.removeNodeFromParent(selectedNode);
        }
    });
    panel.add(deleteButton);
    add(panel, BorderLayout.SOUTH);
}

From source file:Main.java

public void actionPerformed(ActionEvent ae) {
    DefaultMutableTreeNode dmtn, node;

    TreePath path = this.getSelectionPath();
    dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
    if (ae.getActionCommand().equals("insert")) {
        node = new DefaultMutableTreeNode("children");
        dmtn.add(node);/*from www.  j av  a  2  s.c o m*/
        ((DefaultTreeModel) this.getModel()).nodeStructureChanged((TreeNode) dmtn);
    }
    if (ae.getActionCommand().equals("remove")) {
        node = (DefaultMutableTreeNode) dmtn.getParent();
        int nodeIndex = node.getIndex(dmtn);
        dmtn.removeAllChildren();
        node.remove(nodeIndex);
        ((DefaultTreeModel) this.getModel()).nodeStructureChanged((TreeNode) dmtn);
    }
}

From source file:dotaSoundEditor.Controls.ItemPanel.java

private void currentTreeSelectionChanged(TreeSelectionEvent evt) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) currentTree.getLastSelectedPathComponent();
    if (node == null) {
        return;//from w  w  w  .ja v  a2s .c  o m
    }
    //get highest parent of selected node
    while (node.getParent() != null && !node.getParent().equals(node.getRoot())) {
        node = (DefaultMutableTreeNode) node.getParent();
    }

    NamedItem clickedItem = new NamedItem(node.getUserObject().toString(), this.getCurrentScriptString());
    fillImageFrame(clickedItem);
    itemLabel.setText("Item: " + clickedItem.getFriendlyName());

}

From source file:be.ugent.maf.cellmissy.gui.controller.load.generic.DragAndDropController.java

/**
 * Given an imaging node, find the upper dataset
 *
 * @param draggedNode//from ww  w  .  j  a v a  2  s.c om
 * @return
 */
private Algorithm findAlgorithm(DefaultMutableTreeNode draggedNode) {
    ObservableList<Algorithm> algorithmsBindingList = genericImagedPlateController.getAlgorithmsBindingList();
    Algorithm foundAlgorithm = null;
    DefaultMutableTreeNode algoNode = (DefaultMutableTreeNode) draggedNode.getParent().getParent();
    for (Algorithm algorithm : algorithmsBindingList) {
        if (algorithm.getAlgorithmName().equals(algoNode.toString())) {
            foundAlgorithm = algorithm;
        }
    }
    return foundAlgorithm;
}

From source file:be.ugent.maf.cellmissy.gui.controller.load.generic.DragAndDropController.java

/**
 * Given the node being dragged,/*ww  w  .  j a v a  2  s .  c o  m*/
 *
 * @param node
 */
private void setCombinationImagingTypeAlgorithm(DefaultMutableTreeNode draggedNode) {
    // look for imaging type selected
    ObservableList<ImagingType> imagingTypesBindingList = genericImagedPlateController
            .getImagingTypesBindingList();
    for (ImagingType imagingType : imagingTypesBindingList) {
        if (imagingType.getName().equals(draggedNode.getParent().toString())) {
            // imaging type that was selected
            currentImagingType = imagingType;
            // look for associated algorithm
            currentAlgorithm = findAlgorithm(draggedNode);
        }
    }
}

From source file:dotaSoundEditor.Controls.EditorPanel.java

/**
 * @param oldTree The tree that was previously in use
 * @param scriptFilePath A File object pointing to or containing a script.
 * @return The merged tree./*from w  w w .ja va2  s. c o m*/
 */
TreeModel mergeNewChanges(TreeModel oldTree, File scriptFilePath) {
    //Look for any modified wavestrings. Save their nodes, and note their indices.
    //Parse in updated script tree, replace nodes at indices with saved nodes.   
    //Return merged tree
    System.out.println("Running a merge operation!");

    TreeNode oldRoot = (TreeNode) oldTree.getRoot();
    ArrayList<DefaultMutableTreeNode> savedNodeList = new ArrayList<>();
    for (Enumeration e = ((DefaultMutableTreeNode) oldRoot).depthFirstEnumeration(); e.hasMoreElements()
            && oldRoot != null;) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        if (node.getUserObject().toString().contains("custom\\")
                || node.getUserObject().toString().contains("custom/")) {
            savedNodeList.add(node);
        }
    }
    ScriptParser parser = new ScriptParser(scriptFilePath);
    TreeModel newTree = parser.getTreeModel();
    TreeNode newRoot = (TreeNode) newTree.getRoot();
    for (DefaultMutableTreeNode savedNode : savedNodeList) {
        int rndwaveIndex = -1;
        int childIndex = -1;
        int parentIndex = -1;
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) savedNode.getParent();
        if (parent.getUserObject().toString().contains("rndwave")) {
            rndwaveIndex = parent.getParent().getIndex(parent);
            childIndex = parent.getIndex(savedNode);
            parent = (DefaultMutableTreeNode) parent.getParent();
            parentIndex = parent.getParent().getIndex(parent);
        } else {
            parentIndex = parent.getParent().getIndex(parent);
            childIndex = parent.getIndex(savedNode);
        }

        TreeNode newParentNode = newRoot.getChildAt(parentIndex);
        TreeNode newChildNode;
        if (rndwaveIndex != -1) {
            newChildNode = newParentNode.getChildAt(rndwaveIndex);
            newChildNode = newChildNode.getChildAt(childIndex);
        } else {
            newChildNode = newParentNode.getChildAt(childIndex);
        }
        newChildNode = savedNode;
    }
    return newTree;
}