use of org.cytoscape.group.events.GroupCollapsedEvent 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();
}
use of org.cytoscape.group.events.GroupCollapsedEvent 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));
}
use of org.cytoscape.group.events.GroupCollapsedEvent in project cytoscape-impl by cytoscape.
the class GroupViewCollapseHandler method handleEvent.
@Override
public void handleEvent(GroupAddedEvent e) {
getServices();
CyGroup group = e.getGroup();
GroupViewType groupViewType = cyGroupSettings.getGroupViewType(group);
try {
// of the group visually immediately
if (groupViewType.equals(GroupViewType.COMPOUND) || groupViewType.equals(GroupViewType.SINGLENODE)) {
Set<CyNetwork> networkSet = group.getNetworkSet();
if (networkSet == null || networkSet.size() == 0)
return;
for (CyNetwork network : networkSet) {
((CyGroupImpl) group).setGroupNodeShown(network, true);
GroupCollapsedEvent ev = new GroupCollapsedEvent(group, network, false);
handleEvent(ev);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return;
}
use of org.cytoscape.group.events.GroupCollapsedEvent in project cytoscape-impl by cytoscape.
the class GroupViewCollapseHandler method handleEvent.
@Override
public void handleEvent(GroupCollapsedEvent e) {
getServices();
CyNetwork network = e.getNetwork();
CyGroup group = e.getSource();
final Collection<CyNetworkView> views = cyNetworkViewManager.getNetworkViews(network);
CyNetworkView view = null;
if (views.size() == 0) {
return;
}
for (CyNetworkView v : views) {
if (v.getRendererId().equals("org.cytoscape.ding")) {
view = v;
}
}
if (view == null)
return;
CyRootNetwork rootNetwork = group.getRootNetwork();
GroupViewType groupViewType = cyGroupSettings.getGroupViewType(group);
if (e.collapsed()) {
// System.out.println("Got collapse event for "+group);
// Get the location to move the group node to
Dimension d = ViewUtils.getLocation(network, group);
if (d != null) {
// Move it.
ViewUtils.moveNode(view, group.getGroupNode(), d);
}
View<CyNode> nView = view.getNodeView(group.getGroupNode());
if (cyGroupSettings.getUseNestedNetworks(group)) {
// Now, if we're displaying the nested network, create it....
CyNetwork nn = group.getGroupNetwork();
CyNetworkView nnView = null;
// If we've already registered the network, don't do it again
if (!cyNetworkManager.networkExists(nn.getSUID())) {
cyNetworkManager.addNetwork(nn, false);
nnView = cyNetworkViewFactory.createNetworkView(nn);
cyNetworkViewManager.addNetworkView(nnView, false);
// Apply our current style to the nested network
VisualStyle style = cyStyleManager.getVisualStyle(view);
cyStyleManager.setVisualStyle(style, nnView);
style.apply(nnView);
} else if (cyNetworkViewManager.viewExists(nn)) {
nnView = cyNetworkViewManager.getNetworkViews(nn).iterator().next();
}
if (nnView != null) {
ViewUtils.moveNodes(group, nnView, d);
nnView.updateView();
// Allow the nested network image to be displayed
nView.clearValueLock(BasicVisualLexicon.NODE_NESTED_NETWORK_IMAGE_VISIBLE);
}
} else {
// Make sure the nested network image is not displayed
nView.setLockedValue(BasicVisualLexicon.NODE_NESTED_NETWORK_IMAGE_VISIBLE, Boolean.FALSE);
}
// If we were showing this as a compound node, we need to restyle
if (groupViewType.equals(GroupViewType.COMPOUND)) {
deActivateCompoundNode(group, view);
}
styleGroupNode(group, view, views);
} else {
// System.out.println("Got expand event for "+group);
CyNode groupNode = group.getGroupNode();
// Get the location of the group node before it went away
Dimension center = ViewUtils.getLocation(network, group);
if (center != null)
ViewUtils.moveNodes(group, view, center);
// If we're asked to, show the group node
if (!groupViewType.equals(GroupViewType.NONE)) {
CySubNetwork subnet = (CySubNetwork) network;
// Add the node back
subnet.addNode(group.getGroupNode());
// Add the group nodes's edges back
List<CyEdge> groupNodeEdges = rootNetwork.getAdjacentEdgeList(groupNode, CyEdge.Type.ANY);
for (CyEdge edge : groupNodeEdges) {
CyRow row = rootNetwork.getRow(edge, CyNetwork.HIDDEN_ATTRS);
// Only add non-meta edges
if (row != null && (!row.isSet(CyGroupImpl.ISMETA_EDGE_ATTR) || !row.get(CyGroupImpl.ISMETA_EDGE_ATTR, Boolean.class))) {
subnet.addEdge(edge);
} else if (subnet.containsEdge(edge) && row != null && row.isSet(CyGroupImpl.ISMETA_EDGE_ATTR) && row.get(CyGroupImpl.ISMETA_EDGE_ATTR, Boolean.class)) {
// Edge is a meta edge, but is still in the network. Remove it
subnet.removeEdges(Collections.singletonList(edge));
}
}
if (groupViewType.equals(GroupViewType.SHOWGROUPNODE)) {
// If this is the first time, we need to add our member
// edges in.
addMemberEdges(group, network);
// Style our member edges
styleGroupNode(group, view, views);
}
// Flush events so that the view hears about it
final CyEventHelper cyEventHelper = cyGroupManager.getService(CyEventHelper.class);
cyEventHelper.flushPayloadEvents();
// Now, call ourselves as if we had been collapsed
handleEvent(new GroupCollapsedEvent(group, network, true));
if (groupViewType.equals(GroupViewType.COMPOUND) || groupViewType.equals(GroupViewType.SINGLENODE)) {
// May be redundant, but just to make sure
((CyGroupImpl) group).setGroupNodeShown(network, true);
activateCompoundNode(group, view);
}
cyEventHelper.flushPayloadEvents();
view.updateView();
return;
}
final List<CyNode> nodeList = group.getNodeList();
// TODO: turn off stupid nested network thing
for (CyNode node : nodeList) {
if (!network.containsNode(node))
continue;
View<CyNode> nView = view.getNodeView(node);
if (node.getNetworkPointer() != null && cyGroupManager.isGroup(node, network)) {
if (!cyGroupSettings.getUseNestedNetworks(cyGroupManager.getGroup(node, network))) {
nView.setLockedValue(BasicVisualLexicon.NODE_NESTED_NETWORK_IMAGE_VISIBLE, Boolean.FALSE);
}
}
}
// Apply visual property to added graph elements
ViewUtils.applyStyle(nodeList, views, cyStyleManager);
ViewUtils.applyStyle(group.getInternalEdgeList(), views, cyStyleManager);
ViewUtils.applyStyle(group.getExternalEdgeList(), views, cyStyleManager);
}
view.updateView();
}
Aggregations