Example usage for javax.swing.tree DefaultMutableTreeNode removeFromParent

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

Introduction

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

Prototype

public void removeFromParent() 

Source Link

Document

Removes the subtree rooted at this node from the tree, giving this node a null parent.

Usage

From source file:lcmc.gui.ClusterBrowser.java

/** Updates common block devices. */
public void updateCommonBlockDevices() {
    if (commonBlockDevicesNode != null) {
        DefaultMutableTreeNode resource;
        final List<String> bd = cluster.getCommonBlockDevices();
        @SuppressWarnings("unchecked")
        final Enumeration<DefaultMutableTreeNode> e = commonBlockDevicesNode.children();
        final List<DefaultMutableTreeNode> nodesToRemove = new ArrayList<DefaultMutableTreeNode>();
        while (e.hasMoreElements()) {
            final DefaultMutableTreeNode node = e.nextElement();
            final Info cbdi = (Info) node.getUserObject();
            if (bd.contains(cbdi.getName())) {
                /* keeping */
                bd.remove(bd.indexOf(cbdi.getName()));
            } else {
                /* remove not existing block devices */
                cbdi.setNode(null);//from w  ww  .  j  ava  2s. c om
                nodesToRemove.add(node);
            }
        }

        /* remove nodes */
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                for (DefaultMutableTreeNode node : nodesToRemove) {
                    node.removeFromParent();
                }
            }
        });

        /* block devices */
        for (String device : bd) {
            /* add new block devices */
            resource = new DefaultMutableTreeNode(
                    new CommonBlockDevInfo(device, cluster.getHostBlockDevices(device), this));
            setNode(resource);
            addNode(commonBlockDevicesNode, resource);
        }
        if (!bd.isEmpty() || !nodesToRemove.isEmpty()) {
            reload(commonBlockDevicesNode, false);
            reloadAllComboBoxes(null);
        }
    }
}

From source file:lcmc.gui.ClusterBrowser.java

/** Updates VM nodes. */
public void updateVMS() {
    Tools.debug(this, "VM status update", 1);
    final Set<String> domainNames = new TreeSet<String>();
    for (final Host host : getClusterHosts()) {
        final VMSXML vxml = getVMSXML(host);
        if (vxml != null) {
            domainNames.addAll(vxml.getDomainNames());
        }/*from  w  w  w . j  a  v a  2s. com*/
    }
    final List<DefaultMutableTreeNode> nodesToRemove = new ArrayList<DefaultMutableTreeNode>();
    boolean nodeChanged = false;
    final List<VMSVirtualDomainInfo> currentVMSVDIs = new ArrayList<VMSVirtualDomainInfo>();

    mVMSUpdateLock.lock();
    if (vmsNode != null) {
        @SuppressWarnings("unchecked")
        final Enumeration<DefaultMutableTreeNode> ee = vmsNode.children();
        while (ee.hasMoreElements()) {
            final DefaultMutableTreeNode node = ee.nextElement();
            final VMSVirtualDomainInfo vmsvdi = (VMSVirtualDomainInfo) node.getUserObject();
            if (domainNames.contains(vmsvdi.toString())) {
                /* keeping */
                currentVMSVDIs.add(vmsvdi);
                domainNames.remove(vmsvdi.toString());
                vmsvdi.updateParameters(); /* update old */
            } else {
                if (!vmsvdi.getResource().isNew()) {
                    /* remove not existing vms */
                    vmsvdi.setNode(null);
                    nodesToRemove.add(node);
                    nodeChanged = true;
                }
            }
        }
    }

    /* remove nodes */
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            for (final DefaultMutableTreeNode node : nodesToRemove) {
                node.removeFromParent();
            }
        }
    });

    if (vmsNode == null) {
        mVMSUpdateLock.unlock();
        return;
    }
    for (final String domainName : domainNames) {
        @SuppressWarnings("unchecked")
        final Enumeration<DefaultMutableTreeNode> e = vmsNode.children();
        int i = 0;
        while (e.hasMoreElements()) {
            final DefaultMutableTreeNode node = e.nextElement();
            final VMSVirtualDomainInfo vmsvdi = (VMSVirtualDomainInfo) node.getUserObject();
            final String name = vmsvdi.getName();
            if (domainName != null && name != null && domainName.compareTo(vmsvdi.getName()) < 0) {
                break;
            }
            i++;
        }
        /* add new vms nodes */
        final VMSVirtualDomainInfo vmsvdi = new VMSVirtualDomainInfo(domainName, this);
        currentVMSVDIs.add(vmsvdi);
        final DefaultMutableTreeNode resource = new DefaultMutableTreeNode(vmsvdi);
        setNode(resource);
        vmsvdi.updateParameters();
        final int index = i;
        Tools.invokeAndWait(new Runnable() {
            public void run() {
                vmsNode.insert(resource, index);
            }
        });
        nodeChanged = true;
    }
    mVMSUpdateLock.unlock();
    if (nodeChanged) {
        reload(vmsNode, false);
    }
    for (final ServiceInfo si : getExistingServiceList(null)) {
        final VMSVirtualDomainInfo vmsvdi = si.connectWithVMS();
        if (vmsvdi != null) {
            /* keep the not connected ones.*/
            currentVMSVDIs.remove(vmsvdi);
        }
    }
    for (final VMSVirtualDomainInfo vmsvdi : currentVMSVDIs) {
        vmsvdi.setUsedByCRM(false);
    }
    final VMSInfo vmsi = (VMSInfo) vmsNode.getUserObject();
    if (vmsi != null) {
        vmsi.updateTable(VMSInfo.MAIN_TABLE);
    }
}

From source file:org.apache.syncope.ide.netbeans.view.ResourceExplorerTopComponent.java

private void leafRightClickAction(final MouseEvent evt, final DefaultMutableTreeNode node) {
    JPopupMenu menu = new JPopupMenu();
    JMenuItem deleteItem = new JMenuItem("Delete");
    menu.add(deleteItem);/*from www  .  j  a  v a2s.co  m*/

    deleteItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            int result = JOptionPane.showConfirmDialog(null, "Are you sure to delete the item?");
            if (result == JOptionPane.OK_OPTION) {
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
                boolean deleted;
                if (parent.getUserObject().equals(PluginConstants.MAIL_TEMPLATES)) {
                    deleted = mailTemplateManagerService.delete((String) node.getUserObject());
                } else {
                    deleted = reportTemplateManagerService.delete((String) node.getUserObject());
                }
                if (deleted) {
                    node.removeFromParent();
                    treeModel.reload(parent);
                } else {
                    JOptionPane.showMessageDialog(null, "Error while deleting new element", "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    });

    menu.show(evt.getComponent(), evt.getX(), evt.getY());
}

From source file:ropes.MainWindow.java

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    DefaultTreeModel model = (DefaultTreeModel) jTree_fileList.getModel();
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree_fileList.getLastSelectedPathComponent();
    //DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    // TreePath parent_node = null; 
    if (node != null) {
        // parent_node = new TreePath(node.getParent());
        node.removeFromParent();

    } else {//from  ww  w  .ja v a2  s  .c  o m
        JOptionPane.showMessageDialog(this, "Please select an item first", "Error", JOptionPane.ERROR_MESSAGE);
    }
    model.reload();

    //TODO expand jtree after removing of a node

}