Search in sources :

Example 1 with GroupAboutToCollapseEvent

use of org.cytoscape.group.events.GroupAboutToCollapseEvent in project cytoscape-impl by cytoscape.

the class CyGroupImpl method collapse.

/**
 * @see org.cytoscape.group.CyGroup#collapse()
 */
@Override
public void collapse(CyNetwork net) {
    CySubNetwork subnet;
    CyNetwork groupNet;
    synchronized (lock) {
        // printGroup();
        if (isCollapsed(net))
            // Already collapsed
            return;
        if (!networkSet.contains(net))
            // We're not in that network
            return;
        subnet = (CySubNetwork) net;
        groupNet = groupNode.getNetworkPointer();
        // First collapse any children that are groups
        for (CyNode node : getNodeList()) {
            // not in our network, it might have been collapsed by another group
            if (mgr.isGroup(node, net)) {
                final CyGroup gn = mgr.getGroup(node, net);
                if (!gn.isCollapsed(net)) {
                    // Yes, collapse it
                    gn.collapse(net);
                }
            }
        }
    }
    // Now collapse ourselves
    cyEventHelper.fireEvent(new GroupAboutToCollapseEvent(CyGroupImpl.this, net, true));
    collapsing = true;
    // Since we're going to hide our nodes and edges, we probably shouldn't
    // force these to redraw while we're collapsing.  It causes a significant
    // performance hit if we do.
    cyEventHelper.silenceEventSource(net.getDefaultNodeTable());
    cyEventHelper.silenceEventSource(net.getDefaultEdgeTable());
    synchronized (lock) {
        // Deselect all of the nodes
        for (CyNode node : getNodeList()) {
            if (net.containsNode(node))
                net.getRow(node).set(CyNetwork.SELECTED, Boolean.FALSE);
        }
        // Deselect all of our internal edges
        for (CyEdge edge : getInternalEdgeList()) {
            if (net.containsEdge(edge))
                net.getRow(edge).set(CyNetwork.SELECTED, Boolean.FALSE);
        }
        // Deselect all of our member edges
        for (CyEdge edge : memberEdges) {
            if (net.containsEdge(edge))
                net.getRow(edge).set(CyNetwork.SELECTED, Boolean.FALSE);
        }
    }
    // Make sure to restore any of our existing metaEdges
    for (CyEdge e : metaEdges.values()) {
        rootNetwork.restoreEdge(e);
    }
    List<CyNode> nodes;
    synchronized (lock) {
        // Deselect all of our external edges, and check for any possible
        // collapsed partner groups
        ListIterator<CyEdge> iterator = (new ArrayList<CyEdge>(externalEdges)).listIterator();
        while (iterator.hasNext()) {
            CyEdge edge = iterator.next();
            if (net.containsEdge(edge)) {
                net.getRow(edge).set(CyNetwork.SELECTED, Boolean.FALSE);
            } else if (!externalEdgeProcessed.contains(edge)) {
                // See if a partner node got collapsed
                CyNode node = getPartner(edge);
                if (!net.containsNode(node) && !mgr.getGroupsForNode(node).isEmpty()) {
                    for (CyGroup group : mgr.getGroupsForNode(node)) {
                        if (!group.equals(this)) {
                            // Create our meta-edge
                            CyEdge metaEdge = null;
                            metaEdge = createMetaEdge(edge, group.getGroupNode(), groupNode);
                            addMetaEdge(edge, metaEdge);
                            // Get the reciprocal edges
                            addPartnerMetaEdges(net, edge, group, metaEdge);
                            // Get the external edges and make them part of our external
                            ListIterator<CyEdge> edgeIterator = (new ArrayList<CyEdge>(group.getExternalEdgeList())).listIterator();
                            while (edgeIterator.hasNext()) {
                                CyEdge partnerEdge = edgeIterator.next();
                                CyEdge partnerMetaEdge = null;
                                if (groupNet.containsNode(partnerEdge.getSource())) {
                                    partnerMetaEdge = createMetaEdge(partnerEdge, partnerEdge.getTarget(), groupNode);
                                } else if (groupNet.containsNode(partnerEdge.getTarget())) {
                                    partnerMetaEdge = createMetaEdge(partnerEdge, partnerEdge.getSource(), groupNode);
                                }
                                if (partnerMetaEdge != null) {
                                    ((CyGroupImpl) group).addExternalEdge(partnerMetaEdge);
                                }
                            }
                        }
                    }
                }
                externalEdgeProcessed.add(edge);
            }
        }
        cyEventHelper.unsilenceEventSource(net.getDefaultNodeTable());
        cyEventHelper.unsilenceEventSource(net.getDefaultEdgeTable());
        // Only collapse nodes that are actually in our
        // network.  This checks for nodes that are in
        // multiple groups.
        nodes = new ArrayList<CyNode>();
        for (CyNode node : getNodeList()) {
            if (net.containsNode(node)) {
                nodes.add(node);
            } else {
                // This node may be in multiple groups, ours and at least one
                // other, or it has been removed.  We need to add a meta-edge
                // to that node in case we expand it, and potentially remove
                // a meta-edge that might not apply
                List<CyGroup> otherGroups = mgr.getGroupsForNode(node);
                // Make sure someone didn't remove it
                if (otherGroups == null || otherGroups.size() == 0)
                    continue;
                for (CyGroup group : otherGroups) {
                    if (!((CyGroupImpl) group).equals(this)) {
                        // Find any internal edges that point to this node
                        for (CyEdge edge : groupNet.getAdjacentEdgeList(node, CyEdge.Type.ANY)) {
                            // If this node has an internal edge, add a meta-edge to it
                            CyEdge metaEdge = createMetaEdge(edge, node, groupNode);
                            ((CyGroupImpl) group).addExternalEdge(metaEdge);
                        // TODO: Need to deal with unnecessary meta-edge
                        }
                    }
                }
            }
        }
        // System.out.println(nodes.size()+" nodes to collapse: "+nodes);
        collapsedNodes.put(net.getSUID(), nodes);
    }
    // We flush our events to renderers can process any edge
    // creation events before we start removing things...
    cyEventHelper.flushPayloadEvents();
    // Remove all of the nodes from the target network:
    // But first, Save their locked visual properties values...
    final CyNetworkViewManager netViewMgr = mgr.getService(CyNetworkViewManager.class);
    final Collection<CyNetworkView> netViewList = netViewMgr.getNetworkViews(subnet);
    for (CyNode n : nodes) {
        lvpMgr.saveLockedValues(n, netViewList);
        saveLocalAttributes(net, n);
        for (CyEdge e : subnet.getAdjacentEdgeList(n, CyEdge.Type.ANY)) {
            lvpMgr.saveLockedValues(e, netViewList);
            saveLocalAttributes(net, e);
        }
    }
    // causes external edges to be removed as well
    subnet.removeNodes(nodes);
    // Make sure the external edges don't get auto-deleted
    for (CyEdge edge : externalEdges) {
        subnet.getRootNetwork().restoreEdge(edge);
    }
    final Set<CyIdentifiable> addedElements = new HashSet<CyIdentifiable>();
    synchronized (lock) {
        if (net.containsNode(groupNode)) {
            // System.out.println("Network contains group node!");
            nodeShownSet.add(net.getSUID());
        }
        // memberEdges.size()+" and nodeShownSet = "+nodeShownSet.contains(net.getSUID()));
        if (memberEdges.size() == 0 && !nodeShownSet.contains(net.getSUID())) {
            subnet.addNode(groupNode);
            addedElements.add(groupNode);
            for (CyEdge e : groupNodeEdges) {
                // I have no idea why this would be necessary, but it is....
                if (subnet.containsEdge(e)) {
                    // Save edge's locked visual properties values before they are removed
                    lvpMgr.saveLockedValues(e, netViewList);
                    saveLocalAttributes(net, e);
                    subnet.removeEdges(Collections.singletonList(e));
                    // But don't loose track of it...
                    rootNetwork.restoreEdge(e);
                }
                if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                    subnet.addEdge(e);
                    addedElements.add(e);
                }
            }
        }
        // Add the meta-edges
        for (CyEdge e : getMetaEdgeList()) {
            // The same meta-edge might represent multiple edges
            if (subnet.containsEdge(e)) {
                continue;
            }
            if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                subnet.addEdge(e);
                addedElements.add(e);
                nameMetaEdge(e, e.getSource(), e.getTarget());
                copyEdgeName(subnet, e);
            }
        }
        collapseSet.add(net.getSUID());
        // Update attributes
        updateCountAttributes(net);
    }
    // Make sure the view "hears" about all of the changes...
    cyEventHelper.flushPayloadEvents();
    // Restore locked visual property values of added nodes/edges
    lvpMgr.setLockedValues(netViewList, addedElements);
    restoreLocalAttributes(net, addedElements);
    // OK, all done
    collapsing = false;
    cyEventHelper.fireEvent(new GroupCollapsedEvent(CyGroupImpl.this, net, true));
// System.out.println("collapsed "+this.toString()+" in net "+net.toString()+": isCollapsed = "+isCollapsed(net));
// printGroup();
}
Also used : CyNetworkViewManager(org.cytoscape.view.model.CyNetworkViewManager) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) ListIterator(java.util.ListIterator) CyEdge(org.cytoscape.model.CyEdge) CyGroup(org.cytoscape.group.CyGroup) GroupAboutToCollapseEvent(org.cytoscape.group.events.GroupAboutToCollapseEvent) CyNode(org.cytoscape.model.CyNode) GroupCollapsedEvent(org.cytoscape.group.events.GroupCollapsedEvent) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) CyIdentifiable(org.cytoscape.model.CyIdentifiable) HashSet(java.util.HashSet)

Example 2 with GroupAboutToCollapseEvent

use of org.cytoscape.group.events.GroupAboutToCollapseEvent in project cytoscape-impl by cytoscape.

the class CyGroupImpl method expand.

/**
 * @see org.cytoscape.group.CyGroup#expand()
 */
@Override
public void expand(CyNetwork net) {
    synchronized (lock) {
        if (!isCollapsed(net))
            // Already expanded
            return;
        if (!networkSet.contains(net))
            // We're not in that network
            return;
    }
    cyEventHelper.fireEvent(new GroupAboutToCollapseEvent(CyGroupImpl.this, net, false));
    expanding = true;
    CySubNetwork subnet = (CySubNetwork) net;
    List<CyNode> nodes;
    synchronized (lock) {
        // Get the list of nodes we collapsed in this net
        nodes = collapsedNodes.get(net.getSUID());
        // initialized.  Handle this special case here
        if (nodes == null) {
            nodes = getNodeList();
        }
    }
    boolean groupNodeShown = nodeShownSet.contains(net.getSUID());
    // Make sure the group node isn't selected
    if (!groupNodeShown && net.containsNode(groupNode)) {
        cyEventHelper.silenceEventSource(net.getDefaultNodeTable());
        net.getRow(groupNode).set(CyNetwork.SELECTED, Boolean.FALSE);
        cyEventHelper.unsilenceEventSource(net.getDefaultNodeTable());
    }
    // Expand it.
    final CyNetworkViewManager netViewMgr = mgr.getService(CyNetworkViewManager.class);
    final Collection<CyNetworkView> netViewList = netViewMgr.getNetworkViews(net);
    final Set<CyIdentifiable> addedElements = new HashSet<CyIdentifiable>();
    synchronized (lock) {
        // the group node did not go away.
        if (memberEdges.size() == 0 && !groupNodeShown) {
            // First, get all of the group node's edges
            List<CyEdge> groupEdges = subnet.getAdjacentEdgeList(groupNode, CyEdge.Type.ANY);
            // Save group node's locked visual properties values before they are removed
            lvpMgr.saveLockedValues(groupNode, netViewList);
            saveLocalAttributes(net, groupNode);
            for (CyEdge e : groupEdges) {
                lvpMgr.saveLockedValues(e, netViewList);
                saveLocalAttributes(net, e);
            }
            groupNodeEdges = new HashSet<CyEdge>();
            // Now, see which of these groupEdges aren't meta-edges
            for (CyEdge edge : groupEdges) {
                if (!isMeta(edge))
                    groupNodeEdges.add(edge);
            }
            // Remove the group node, if we're supposed to
            if (!nodeShownSet.contains(subnet.getSUID())) {
                List<CyEdge> groupNodeEdges = subnet.getAdjacentEdgeList(groupNode, CyEdge.Type.ANY);
                subnet.removeNodes(Collections.singletonList(groupNode));
                rootNetwork.restoreNode(groupNode);
                for (CyEdge e : groupNodeEdges) rootNetwork.restoreEdge(e);
            }
        }
        // Add all of the member nodes and edges in
        for (CyNode n : nodes) {
            subnet.addNode(n);
            addedElements.add(n);
        }
    }
    // Make sure everyone knows about all of our changes!
    cyEventHelper.flushPayloadEvents();
    synchronized (lock) {
        // Add all of the interior edges in
        for (CyEdge e : getInternalEdgeList()) {
            if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                subnet.addEdge(e);
                addedElements.add(e);
            }
        }
        // Add all of the exterior edges in
        for (CyEdge e : getExternalEdgeList()) {
            // collapsed group
            if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                subnet.addEdge(e);
                addedElements.add(e);
            }
        }
        // our nodes to collapsed groups.  Add those in
        for (CyEdge e : getMetaEdgeList()) {
            if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                subnet.addEdge(e);
                addedElements.add(e);
            }
        }
        // Add all of the member edges in
        for (CyEdge e : memberEdges) {
            subnet.addEdge(e);
            addedElements.add(e);
        }
    }
    // Make sure everyone knows about all of our changes!
    cyEventHelper.flushPayloadEvents();
    // Restore locked visual property values of added nodes/edges
    lvpMgr.setLockedValues(netViewList, addedElements);
    restoreLocalAttributes(net, addedElements);
    synchronized (lock) {
        collapseSet.remove(net.getSUID());
        collapsedNodes.remove(net.getSUID());
    }
    // Finish up
    expanding = false;
    cyEventHelper.fireEvent(new GroupCollapsedEvent(CyGroupImpl.this, net, false));
}
Also used : CyNetworkViewManager(org.cytoscape.view.model.CyNetworkViewManager) CyEdge(org.cytoscape.model.CyEdge) GroupAboutToCollapseEvent(org.cytoscape.group.events.GroupAboutToCollapseEvent) CyNode(org.cytoscape.model.CyNode) GroupCollapsedEvent(org.cytoscape.group.events.GroupCollapsedEvent) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) CyIdentifiable(org.cytoscape.model.CyIdentifiable) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)2 GroupAboutToCollapseEvent (org.cytoscape.group.events.GroupAboutToCollapseEvent)2 GroupCollapsedEvent (org.cytoscape.group.events.GroupCollapsedEvent)2 CyEdge (org.cytoscape.model.CyEdge)2 CyIdentifiable (org.cytoscape.model.CyIdentifiable)2 CyNode (org.cytoscape.model.CyNode)2 CySubNetwork (org.cytoscape.model.subnetwork.CySubNetwork)2 CyNetworkView (org.cytoscape.view.model.CyNetworkView)2 CyNetworkViewManager (org.cytoscape.view.model.CyNetworkViewManager)2 ArrayList (java.util.ArrayList)1 ListIterator (java.util.ListIterator)1 CyGroup (org.cytoscape.group.CyGroup)1 CyNetwork (org.cytoscape.model.CyNetwork)1