package org.cytoscape.view.model.internal;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.events.SetCurrentNetworkViewEvent;
import org.cytoscape.application.events.SetCurrentNetworkViewListener;
import org.cytoscape.event.CyEventHelper;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.events.RowsSetEvent;
import org.cytoscape.model.events.RowsSetListener;
import org.cytoscape.service.util.CyServiceRegistrar;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.events.SelectedNodesAndEdgesEvent;


/**
 * Fires an event whenever the selection in the current network view changes.
 * @author mkucera
 */
public class SelectionMediator implements RowsSetListener, SetCurrentNetworkViewListener {

	private final CyApplicationManager applicationManager;
	private final CyEventHelper eventHelper;
	
	public SelectionMediator(CyServiceRegistrar serviceRegistrar) {
		this.applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
		this.eventHelper = serviceRegistrar.getService(CyEventHelper.class);
	}
	
	@Override
	public void handleEvent(RowsSetEvent e) {
		if(!e.containsColumn(CyNetwork.SELECTED)) {
			return;
		}
		CyNetworkView networkView = applicationManager.getCurrentNetworkView();
		if(networkView != null) {
			CyNetwork network = networkView.getModel();
			if(e.getSource() == network.getDefaultEdgeTable() || e.getSource() == network.getDefaultNodeTable()) {
				eventHelper.fireEvent(new SelectedNodesAndEdgesEvent(networkView));
			}
		}
	}

	@Override
	public void handleEvent(SetCurrentNetworkViewEvent e) {
		CyNetworkView networkView = e.getNetworkView();
		eventHelper.fireEvent(new SelectedNodesAndEdgesEvent(networkView));
	}

}

@@ -666,27 +666,8 @@
 es.%0A
- * @author mkucera%0A
  */%0A
@@ -779,310 +779,152 @@
 l Cy
-ApplicationManager applicationManager;%0A%09private final CyEventHelper eventHelper;%0A%09%0A%09public SelectionMediator(CyServiceRegistrar serviceRegistrar) %7B%0A%09%09this.applicationManager = serviceRegistrar.getService(CyApplicationManager.class);%0A%09%09this.eventHelper = serviceRegistrar.getService(CyEventHelper.class)
+ServiceRegistrar serviceRegistrar;%0A%09%0A%09public SelectionMediator(CyServiceRegistrar serviceRegistrar) %7B%0A%09%09this.serviceRegistrar = serviceRegistrar
 ;%0A%09%7D
@@ -1041,16 +1041,117 @@
 rn;%0A%09%09%7D%0A
+%09%09CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);%0A
 %09%09CyNetw
@@ -1397,16 +1397,98 @@
 le()) %7B%0A
+%09%09%09%09CyEventHelper eventHelper = serviceRegistrar.getService(CyEventHelper.class);%0A
 %09%09%09%09even
@@ -1682,16 +1682,126 @@
 View();%0A
+%09%09if(networkView != null) %7B%0A%09%09%09CyEventHelper eventHelper = serviceRegistrar.getService(CyEventHelper.class);%0A%09
 %09%09eventH
@@ -1862,14 +1862,18 @@
 View));%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

Fix broken integration tests.