Search in sources :

Example 1 with AboutToRemoveNodesEvent

use of org.cytoscape.model.events.AboutToRemoveNodesEvent in project cytoscape-impl by cytoscape.

the class CySubNetworkImpl method removeNodes.

@Override
public boolean removeNodes(final Collection<CyNode> nodes) {
    if (nodes == null || nodes.isEmpty())
        return false;
    eventHelper.fireEvent(new AboutToRemoveNodesEvent(this, nodes));
    CyTable nodeHiddenTable = getTable(CyNode.class, CyNetwork.HIDDEN_ATTRS);
    CyTable nodeDefaultTable = getTable(CyNode.class, CyNetwork.DEFAULT_ATTRS);
    CyTable edgeHiddenTable = getTable(CyEdge.class, CyNetwork.HIDDEN_ATTRS);
    CyTable edgeDefaultTable = getTable(CyEdge.class, CyNetwork.DEFAULT_ATTRS);
    List<Long> nodeSuids = new ArrayList<>();
    Set<Long> edgeSuids = new HashSet<>();
    for (CyNode node : nodes) {
        if (this.containsNode(node)) {
            Long nodeSuid = node.getSUID();
            if (nodeDefaultTable.rowExists(nodeSuid))
                nodeSuids.add(nodeSuid);
            for (CyEdge edge : getAdjacentEdgeIterable(node, Type.ANY)) {
                Long edgeSuid = edge.getSUID();
                if (edgeDefaultTable.rowExists(edgeSuid)) {
                    edgeSuids.add(edgeSuid);
                }
            }
        }
    }
    boolean ret = removeNodesInternal(nodes);
    nodeHiddenTable.deleteRows(nodeSuids);
    nodeDefaultTable.deleteRows(nodeSuids);
    edgeHiddenTable.deleteRows(edgeSuids);
    edgeDefaultTable.deleteRows(edgeSuids);
    if (ret) {
        parent.subnetworkNodesRemoved(nodes);
    }
    if (ret) {
        eventHelper.fireEvent(new RemovedNodesEvent(this));
    }
    return ret;
}
Also used : CyTable(org.cytoscape.model.CyTable) ArrayList(java.util.ArrayList) CyNode(org.cytoscape.model.CyNode) RemovedNodesEvent(org.cytoscape.model.events.RemovedNodesEvent) CyEdge(org.cytoscape.model.CyEdge) AboutToRemoveNodesEvent(org.cytoscape.model.events.AboutToRemoveNodesEvent) HashSet(java.util.HashSet)

Example 2 with AboutToRemoveNodesEvent

use of org.cytoscape.model.events.AboutToRemoveNodesEvent in project cytoscape-impl by cytoscape.

the class CySubNetworkTest method testRemoveNodeEvents.

// 
// A bunch of tests to validate that the proper events are fired
// 
@Test
public void testRemoveNodeEvents() {
    CySubNetwork sub = root.addSubNetwork();
    CyNode nx1 = sub.addNode();
    CyNode nx2 = sub.addNode();
    CyNode nx3 = sub.addNode();
    CyNode nx4 = sub.addNode();
    List<CyNode> nodes = new ArrayList<CyNode>();
    nodes.add(nx1);
    nodes.add(nx2);
    nodes.add(nx3);
    nodes.add(nx4);
    sub.removeNodes(nodes);
    int aboutToRemoveInd = -1;
    int removedInd = -1;
    int ind = 0;
    for (Object event : deh.getAllLastEvents()) {
        if (event instanceof AboutToRemoveNodesEvent)
            aboutToRemoveInd = ind;
        else if (event instanceof RemovedNodesEvent)
            removedInd = ind;
        ind++;
    }
    // verify that we found the events in the list
    assertTrue(aboutToRemoveInd >= 0);
    assertTrue(removedInd >= 0);
    // verify that the about to remove event came first
    assertTrue(aboutToRemoveInd < removedInd);
}
Also used : ArrayList(java.util.ArrayList) CyNode(org.cytoscape.model.CyNode) RemovedNodesEvent(org.cytoscape.model.events.RemovedNodesEvent) AboutToRemoveNodesEvent(org.cytoscape.model.events.AboutToRemoveNodesEvent) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 CyNode (org.cytoscape.model.CyNode)2 AboutToRemoveNodesEvent (org.cytoscape.model.events.AboutToRemoveNodesEvent)2 RemovedNodesEvent (org.cytoscape.model.events.RemovedNodesEvent)2 HashSet (java.util.HashSet)1 CyEdge (org.cytoscape.model.CyEdge)1 CyTable (org.cytoscape.model.CyTable)1 Test (org.junit.Test)1