<commit_before>
package org.cytoscape.internal.select;


import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.SwingUtilities;

import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.View;
import org.cytoscape.view.model.events.AboutToRemoveEdgeViewsEvent;
import org.cytoscape.view.model.events.AboutToRemoveEdgeViewsListener;
import org.cytoscape.view.model.events.AboutToRemoveNodeViewsEvent;
import org.cytoscape.view.model.events.AboutToRemoveNodeViewsListener;
import org.cytoscape.view.model.events.AddedEdgeViewsEvent;
import org.cytoscape.view.model.events.AddedEdgeViewsListener;
import org.cytoscape.view.model.events.AddedNodeViewsEvent;
import org.cytoscape.view.model.events.AddedNodeViewsListener;
import org.cytoscape.view.model.events.NetworkViewAboutToBeDestroyedEvent;
import org.cytoscape.view.model.events.NetworkViewAboutToBeDestroyedListener;
import org.cytoscape.view.model.events.NetworkViewAddedEvent;
import org.cytoscape.view.model.events.NetworkViewAddedListener;

public class RowViewTracker implements NetworkViewAddedListener, 
	AddedNodeViewsListener, AddedEdgeViewsListener, 
	AboutToRemoveNodeViewsListener, AboutToRemoveEdgeViewsListener,
	NetworkViewAboutToBeDestroyedListener {

	private final Map<CyNetworkView, Map<CyRow,View<?>>> rowViewMapsByNetworkView;
	private final Map<CyTable, Set<CyNetworkView>> networkViewsByTable;

	public RowViewTracker() {
		rowViewMapsByNetworkView = new IdentityHashMap<CyNetworkView, Map<CyRow,View<?>>>();
		networkViewsByTable = new IdentityHashMap<CyTable, Set<CyNetworkView>>();
	}

	public void handleEvent(final NetworkViewAddedEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				final CyNetworkView view = e.getNetworkView();
				final CyNetwork net = view.getModel(); 
				Map<CyRow, View<?>> rowViewMap = getRowViewMapInternal(view);

				for ( View<CyNode> nv : view.getNodeViews() )
					rowViewMap.put( net.getRow(nv.getModel()), nv);
		
				for ( View<CyEdge> ev : view.getEdgeViews() ) 
					rowViewMap.put( net.getRow(ev.getModel()), ev);
				
				addTables(view);
			}
		});
	}
	
	protected void addTables(CyNetworkView view) {
		CyNetwork network = view.getModel();
		addTable(view, network.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS));
		addTable(view, network.getTable(CyNode.class, CyNetwork.LOCAL_ATTRS));
		addTable(view, network.getTable(CyEdge.class, CyNetwork.LOCAL_ATTRS));
	}

	private void addTable(CyNetworkView view, CyTable table) {
		if (table == null) {
			return;
		}
		
		Set<CyNetworkView> views = networkViewsByTable.get(table);
		if (views == null) {
			views = new HashSet<CyNetworkView>();
			networkViewsByTable.put(table, views);
		}
		views.add(view);
	}

	protected void removeTables(CyNetworkView view) {
		CyNetwork network = view.getModel();
		removeTable(view, network.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS));
		removeTable(view, network.getTable(CyNode.class, CyNetwork.LOCAL_ATTRS));
		removeTable(view, network.getTable(CyEdge.class, CyNetwork.LOCAL_ATTRS));
	}
	
	private void removeTable(CyNetworkView view, CyTable table) {
		Set<CyNetworkView> views = networkViewsByTable.get(table);
		if (views == null) {
			return;
		}
		
		views.remove(view);
		if (views.size() == 0) {
			networkViewsByTable.remove(table);
		}
	}

	public void handleEvent(final AddedNodeViewsEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				final CyNetworkView view = e.getSource();
				final CyNetwork net = view.getModel(); 
				Map<CyRow, View<?>> rowViewMap = getRowViewMapInternal(view);
				
				for ( View<CyNode> v : e.getNodeViews()) 
					if (net.containsNode(v.getModel())) 
						rowViewMap.put( net.getRow(v.getModel()), v );
			}
		});
	}
	
	public void handleEvent(final AddedEdgeViewsEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				final CyNetworkView view = e.getSource();
				final CyNetwork net = view.getModel(); 
				Map<CyRow, View<?>> rowViewMap = getRowViewMapInternal(view);

				for ( View<CyEdge> v : e.getEdgeViews()) 
					if (net.containsEdge(v.getModel())) 
						rowViewMap.put( net.getRow(v.getModel()), v );
			}
		});
	}
	
	public void handleEvent(final AboutToRemoveNodeViewsEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Map<CyRow, View<?>> rowViewMap = rowViewMapsByNetworkView.get(e.getSource());
				if (rowViewMap == null) {
					return;
				}
				
				Collection<View<?>> values = rowViewMap.values();
				for ( View<CyNode> v : e.getPayloadCollection()) {
					values.remove(v);
				}
			}
		});
	}
	
	public void handleEvent(final AboutToRemoveEdgeViewsEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Map<CyRow, View<?>> rowViewMap = rowViewMapsByNetworkView.get(e.getSource());
				if (rowViewMap == null) {
					return;
				}
				
				Collection<View<?>> values = rowViewMap.values();
				for ( View<CyEdge> v : e.getPayloadCollection()) 
					values.remove(v);
			}
		});
	}
	
	@Override
	public void handleEvent(final NetworkViewAboutToBeDestroyedEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				rowViewMapsByNetworkView.remove(e.getSource());
			}
		});
	}
	
	private Map<CyRow, View<?>> getRowViewMapInternal(CyNetworkView view) {
		Map<CyRow, View<?>> rowViewMap = rowViewMapsByNetworkView.get(view);
		if (rowViewMap == null) {
			rowViewMap = new IdentityHashMap<CyRow, View<?>>();
			rowViewMapsByNetworkView.put(view, rowViewMap);
		}
		return rowViewMap;
	}

	public Map<CyRow,View<?>> getRowViewMap(CyNetworkView networkView) {
		return Collections.unmodifiableMap(rowViewMapsByNetworkView.get(networkView));  
	}

	public Collection<CyNetworkView> getAffectedNetworkViews(CyTable cyTable) {
		Set<CyNetworkView> views = networkViewsByTable.get(cyTable);
		if (views == null) {
			return Collections.emptySet();
		}<commit_msg>References #1551: Fixed cleanup code so it removes the network view instead of the network view manager<commit_after>
package org.cytoscape.internal.select;


import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.SwingUtilities;

import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.view.model.View;
import org.cytoscape.view.model.events.AboutToRemoveEdgeViewsEvent;
import org.cytoscape.view.model.events.AboutToRemoveEdgeViewsListener;
import org.cytoscape.view.model.events.AboutToRemoveNodeViewsEvent;
import org.cytoscape.view.model.events.AboutToRemoveNodeViewsListener;
import org.cytoscape.view.model.events.AddedEdgeViewsEvent;
import org.cytoscape.view.model.events.AddedEdgeViewsListener;
import org.cytoscape.view.model.events.AddedNodeViewsEvent;
import org.cytoscape.view.model.events.AddedNodeViewsListener;
import org.cytoscape.view.model.events.NetworkViewAboutToBeDestroyedEvent;
import org.cytoscape.view.model.events.NetworkViewAboutToBeDestroyedListener;
import org.cytoscape.view.model.events.NetworkViewAddedEvent;
import org.cytoscape.view.model.events.NetworkViewAddedListener;

public class RowViewTracker implements NetworkViewAddedListener, 
	AddedNodeViewsListener, AddedEdgeViewsListener, 
	AboutToRemoveNodeViewsListener, AboutToRemoveEdgeViewsListener,
	NetworkViewAboutToBeDestroyedListener {

	private final Map<CyNetworkView, Map<CyRow,View<?>>> rowViewMapsByNetworkView;
	private final Map<CyTable, Set<CyNetworkView>> networkViewsByTable;

	public RowViewTracker() {
		rowViewMapsByNetworkView = new IdentityHashMap<CyNetworkView, Map<CyRow,View<?>>>();
		networkViewsByTable = new IdentityHashMap<CyTable, Set<CyNetworkView>>();
	}

	public void handleEvent(final NetworkViewAddedEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				final CyNetworkView view = e.getNetworkView();
				final CyNetwork net = view.getModel(); 
				Map<CyRow, View<?>> rowViewMap = getRowViewMapInternal(view);

				for ( View<CyNode> nv : view.getNodeViews() )
					rowViewMap.put( net.getRow(nv.getModel()), nv);
		
				for ( View<CyEdge> ev : view.getEdgeViews() ) 
					rowViewMap.put( net.getRow(ev.getModel()), ev);
				
				addTables(view);
			}
		});
	}
	
	protected void addTables(CyNetworkView view) {
		CyNetwork network = view.getModel();
		addTable(view, network.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS));
		addTable(view, network.getTable(CyNode.class, CyNetwork.LOCAL_ATTRS));
		addTable(view, network.getTable(CyEdge.class, CyNetwork.LOCAL_ATTRS));
	}

	private void addTable(CyNetworkView view, CyTable table) {
		if (table == null) {
			return;
		}
		
		Set<CyNetworkView> views = networkViewsByTable.get(table);
		if (views == null) {
			views = new HashSet<CyNetworkView>();
			networkViewsByTable.put(table, views);
		}
		views.add(view);
	}

	protected void removeTables(CyNetworkView view) {
		CyNetwork network = view.getModel();
		removeTable(view, network.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS));
		removeTable(view, network.getTable(CyNode.class, CyNetwork.LOCAL_ATTRS));
		removeTable(view, network.getTable(CyEdge.class, CyNetwork.LOCAL_ATTRS));
	}
	
	private void removeTable(CyNetworkView view, CyTable table) {
		Set<CyNetworkView> views = networkViewsByTable.get(table);
		if (views == null) {
			return;
		}
		
		views.remove(view);
		if (views.size() == 0) {
			networkViewsByTable.remove(table);
		}
	}

	public void handleEvent(final AddedNodeViewsEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				final CyNetworkView view = e.getSource();
				final CyNetwork net = view.getModel(); 
				Map<CyRow, View<?>> rowViewMap = getRowViewMapInternal(view);
				
				for ( View<CyNode> v : e.getNodeViews()) 
					if (net.containsNode(v.getModel())) 
						rowViewMap.put( net.getRow(v.getModel()), v );
			}
		});
	}
	
	public void handleEvent(final AddedEdgeViewsEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				final CyNetworkView view = e.getSource();
				final CyNetwork net = view.getModel(); 
				Map<CyRow, View<?>> rowViewMap = getRowViewMapInternal(view);

				for ( View<CyEdge> v : e.getEdgeViews()) 
					if (net.containsEdge(v.getModel())) 
						rowViewMap.put( net.getRow(v.getModel()), v );
			}
		});
	}
	
	public void handleEvent(final AboutToRemoveNodeViewsEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Map<CyRow, View<?>> rowViewMap = rowViewMapsByNetworkView.get(e.getSource());
				if (rowViewMap == null) {
					return;
				}
				
				Collection<View<?>> values = rowViewMap.values();
				for ( View<CyNode> v : e.getPayloadCollection()) {
					values.remove(v);
				}
			}
		});
	}
	
	public void handleEvent(final AboutToRemoveEdgeViewsEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Map<CyRow, View<?>> rowViewMap = rowViewMapsByNetworkView.get(e.getSource());
				if (rowViewMap == null) {
					return;
				}
				
				Collection<View<?>> values = rowViewMap.values();
				for ( View<CyEdge> v : e.getPayloadCollection()) 
					values.remove(v);
			}
		});
	}
	
	@Override
	public void handleEvent(final NetworkViewAboutToBeDestroyedEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CyNetworkView view = e.getNetworkView();
				rowViewMapsByNetworkView.remove(view);
				removeTables(view);
			}
		});
	}
	
	private Map<CyRow, View<?>> getRowViewMapInternal(CyNetworkView view) {
		Map<CyRow, View<?>> rowViewMap = rowViewMapsByNetworkView.get(view);
		if (rowViewMap == null) {
			rowViewMap = new IdentityHashMap<CyRow, View<?>>();
			rowViewMapsByNetworkView.put(view, rowViewMap);
		}
		return rowViewMap;
	}

	public Map<CyRow,View<?>> getRowViewMap(CyNetworkView networkView) {
		return Collections.unmodifiableMap(rowViewMapsByNetworkView.get(networkView));  
	}

	public Collection<CyNetworkView> getAffectedNetworkViews(CyTable cyTable) {
		Set<CyNetworkView> views = networkViewsByTable.get(cyTable);
		if (views == null) {
			return Collections.emptySet();
		}
{'fraction_non_alphanumeric': 0.12088258471237194, 'fraction_numerical': 0.00047281323877068556, 'mean_word_length': 4.913793103448276, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3145056', 'n_tokens_mistral': 4760, 'n_tokens_neox': 4092, 'n_words': 738}