<commit_before>package am.ui.canvas2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;

import am.Utility;
import am.app.Core;
import am.app.mappingEngine.MatcherChangeListener;
import am.app.mappingEngine.MatchingTask;
import am.app.mappingEngine.MatchingTaskChangeEvent;
import am.app.ontology.Node;
import am.app.ontology.Ontology;
import am.app.ontology.OntologyChangeEvent;
import am.app.ontology.OntologyChangeListener;
import am.ui.Colors;
import am.ui.UICore;
import am.ui.VisualizationChangeEvent;
import am.ui.VisualizationChangeListener;
import am.ui.VisualizationPanel;
import am.ui.canvas2.graphical.GraphicalData.NodeType;
import am.ui.canvas2.graphical.MappingData;
import am.ui.canvas2.layouts.LegacyLayout;
import am.ui.canvas2.nodes.LegacyMapping;
import am.ui.canvas2.utility.Canvas2Edge;
import am.ui.canvas2.utility.Canvas2Layout;
import am.ui.canvas2.utility.Canvas2Vertex;
import am.ui.canvas2.utility.CanvasGraph;
import am.ui.canvas2.utility.GraphLocator;
import am.ui.canvas2.utility.GraphLocator.GraphType;
import am.utility.AppPreferences;


/**
 * The next version of the Canvas.
 * 
 * Sleeker, Faster, Easier to code for, and more Object Oriented
 * 
 * 
 * 	Requirements:
 * 		- Vertex objects that support multiple inheritance ( DO NOT EXTEND TREENODE! *facepalm* )
 * 		- FAST repaint(), i.e. only repaint the objects that are being currently viewed. (instead of the whole canvas everytime)
 * 		- NEW Views!
 * 			- leaf to leaf view
 * 		
 * 		- Order the Vertices so that mapping lines are shortest!
 * 		- Different ordering of vertices (alphabetical (asc/desc), by number of children, by "weight")
 * 
 * @author <NAME> <<EMAIL>>
 * @datestarted 11/3/2009
 *
 */

public class Canvas2 extends VisualizationPanel implements OntologyChangeListener, MatcherChangeListener, VisualizationChangeListener {

	private final Logger log = Logger.getLogger(Canvas2.class);
	
	private static final long serialVersionUID = 1544849142971067136L;


/*******************************************************************************
 **************************** CANVAS2 VARIABLES ********************************
 *******************************************************************************/
	
	// The list of the layout graphs for each of the ontologies.
	private ArrayList<CanvasGraph> graphs;  // holds all the ACTIVE graphs, those graphs that are drawn on the canvas
	
	private ArrayList<Canvas2Vertex> visibleVertices;  // everytime we paint the canvas, we keep a list of the visible nodes. (used in mouse movement functions)
	private ArrayList<Canvas2Edge>   visibleEdges;  // also keep a list of visible edges
	
	private Canvas2Layout layout; // the Canvas2Listener is the muscle of the operation, i.e. it does everything related to layout.
	
	public int Xpadding = 20;
	public int Ypadding = 20;


	private boolean painting = false;
	
	/* ************************************************* CONSTRUCTORS ********************************* */
	
	public Canvas2() { super(); initialize(null,null); }
	public Canvas2(JScrollPane s) { super(s); initialize(s,null); }
	public Canvas2(Canvas2Layout layout) { super(); initialize(null, layout); } // allows the use of a custom layout
	public Canvas2(JScrollPane s, Canvas2Layout layout) { super(s); initialize(s, layout); } // allows the use of a custom layout
	
	public Canvas2(JScrollPane s, ArrayList<CanvasGraph> sGraph) {
		this(s);
		this.graphs = sGraph;
		this.updateSize();
	}

	/** Shared method called from the constructors. */
	private void initialize(JScrollPane scrollPane, Canvas2Layout layout) {
		
		if( scrollPane != null ) setScrollPane(scrollPane);
		
		if( layout == null ) layout = new LegacyLayout(this);
		
		setCanvasLayout(layout);
		
		/* Setup the Listeners */
		setLayout(new BorderLayout());
		//addMouseWheelListener(layout);  // TODO: Add this later.  For now let the jscrollpane listen for mouse wheel events.
		Core.getInstance().addOntologyChangeListener(this);
		Core.getInstance().addMatcherChangeListener(this);
		UICore.getInstance().addVisualizationChangeListener(this);
	}
	
	public ArrayList<Canvas2Vertex> getVisibleVertices() { return visibleVertices; }
	public ArrayList<Canvas2Edge>   getVisibleEdges()    { return visibleEdges; }
	
	/**
	 * Ontology Change Listener methods 
	 */
	public synchronized void ontologyChanged(OntologyChangeEvent e) { 
		/**
		 *  This function gets called when an ontology is added or removed to/from the Core.
		 *  When this happens, we need to create the layout graph for the ontologies. 
		 */
		if( e.getEvent() == OntologyChangeEvent.EventType.ONTOLOGY_ADDED ) {
			// an ontology was added.
			layout.displayOntology(graphs,  e.getOntologyID() );
			updateSize();
			
			final Canvas2 canvas = this;
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					canvas.repaint();
				}
			});
			
		} else if( e.getEvent() == OntologyChangeEvent.EventType.ONTOLOGY_REMOVED ) {
			layout.removeOntology( graphs, e.getOntologyID() );
			updateSize();
			
			final Canvas2 canvas = this;
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					canvas.repaint();
				}
			});
		}
		
	}
	
	/**
	 * This function is called from within OntoTreeBuilder, when the OntoTreeBuilder first gets its hands on an ontology.
	 * 
	 * We had to do this in order to unify all the actions done when an ontology is loaded.  Otherwise, actions that get
	 * done when an ontology is loaded are called outside of the "Loading Ontology" progress display.
	 */
	@Override
	public void buildLayoutGraphs(Ontology ontology) {
		// build the graph and put it in the repository, because we are not necessarily going to display it now
		List<CanvasGraph> gr = layout.buildGlobalGraph(ontology);
		Iterator<CanvasGraph> graphIter = gr.iterator();
		while( graphIter.hasNext() ) { graphIter.next().setVisible(false); } // initially the graphs should be invisible.  
																			 // they will be displayed when displayOntology() is called.
		graphs.addAll( gr );  // add them to our list of graphs.
	}
	
	
	public ArrayList<CanvasGraph> getGraphs() { return graphs; }
	
	// return a matcher graph with the given id (used in deleting mappings)
	public CanvasGraph getMatcherGraph( int matcherID ) {<commit_msg>Ontologies are correctly shown again.<commit_after>package am.ui.canvas2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;

import am.Utility;
import am.app.Core;
import am.app.mappingEngine.MatcherChangeListener;
import am.app.mappingEngine.MatchingTask;
import am.app.mappingEngine.MatchingTaskChangeEvent;
import am.app.ontology.Node;
import am.app.ontology.Ontology;
import am.app.ontology.OntologyChangeEvent;
import am.app.ontology.OntologyChangeListener;
import am.ui.Colors;
import am.ui.UICore;
import am.ui.VisualizationChangeEvent;
import am.ui.VisualizationChangeListener;
import am.ui.VisualizationPanel;
import am.ui.canvas2.graphical.GraphicalData.NodeType;
import am.ui.canvas2.graphical.MappingData;
import am.ui.canvas2.layouts.LegacyLayout;
import am.ui.canvas2.nodes.LegacyMapping;
import am.ui.canvas2.utility.Canvas2Edge;
import am.ui.canvas2.utility.Canvas2Layout;
import am.ui.canvas2.utility.Canvas2Vertex;
import am.ui.canvas2.utility.CanvasGraph;
import am.ui.canvas2.utility.GraphLocator;
import am.ui.canvas2.utility.GraphLocator.GraphType;
import am.utility.AppPreferences;


/**
 * The next version of the Canvas.
 * 
 * Sleeker, Faster, Easier to code for, and more Object Oriented
 * 
 * 
 * 	Requirements:
 * 		- Vertex objects that support multiple inheritance ( DO NOT EXTEND TREENODE! *facepalm* )
 * 		- FAST repaint(), i.e. only repaint the objects that are being currently viewed. (instead of the whole canvas everytime)
 * 		- NEW Views!
 * 			- leaf to leaf view
 * 		
 * 		- Order the Vertices so that mapping lines are shortest!
 * 		- Different ordering of vertices (alphabetical (asc/desc), by number of children, by "weight")
 * 
 * @author <NAME> <<EMAIL>>
 * @datestarted 11/3/2009
 *
 */

public class Canvas2 extends VisualizationPanel implements OntologyChangeListener, MatcherChangeListener, VisualizationChangeListener {

	private final Logger log = Logger.getLogger(Canvas2.class);
	
	private static final long serialVersionUID = 1544849142971067136L;


/*******************************************************************************
 **************************** CANVAS2 VARIABLES ********************************
 *******************************************************************************/
	
	// The list of the layout graphs for each of the ontologies.
	private ArrayList<CanvasGraph> graphs;  // holds all the ACTIVE graphs, those graphs that are drawn on the canvas
	
	private ArrayList<Canvas2Vertex> visibleVertices;  // everytime we paint the canvas, we keep a list of the visible nodes. (used in mouse movement functions)
	private ArrayList<Canvas2Edge>   visibleEdges;  // also keep a list of visible edges
	
	private Canvas2Layout layout; // the Canvas2Listener is the muscle of the operation, i.e. it does everything related to layout.
	
	public int Xpadding = 20;
	public int Ypadding = 20;


	private boolean painting = false;
	
	/* ************************************************* CONSTRUCTORS ********************************* */
	
	public Canvas2() { super(); initialize(null,null); }
	public Canvas2(JScrollPane s) { super(s); initialize(s,null); }
	public Canvas2(Canvas2Layout layout) { super(); initialize(null, layout); } // allows the use of a custom layout
	public Canvas2(JScrollPane s, Canvas2Layout layout) { super(s); initialize(s, layout); } // allows the use of a custom layout
	
	public Canvas2(JScrollPane s, ArrayList<CanvasGraph> sGraph) {
		this(s);
		this.graphs = sGraph;
		this.updateSize();
	}

	/** Shared method called from the constructors. */
	private void initialize(JScrollPane scrollPane, Canvas2Layout layout) {
		
		if( scrollPane != null ) setScrollPane(scrollPane);
		
		if( layout == null ) layout = new LegacyLayout(this);
		
		setCanvasLayout(layout);
		
		/* Setup the Listeners */
		setLayout(new BorderLayout());
		//addMouseWheelListener(layout);  // TODO: Add this later.  For now let the jscrollpane listen for mouse wheel events.
		Core.getInstance().addOntologyChangeListener(this);
		Core.getInstance().addMatcherChangeListener(this);
		UICore.getInstance().addVisualizationChangeListener(this);
	}
	
	public ArrayList<Canvas2Vertex> getVisibleVertices() { return visibleVertices; }
	public ArrayList<Canvas2Edge>   getVisibleEdges()    { return visibleEdges; }
	
	/**
	 * Ontology Change Listener methods 
	 */
	public synchronized void ontologyChanged(OntologyChangeEvent e) { 
		/**
		 *  This function gets called when an ontology is added or removed to/from the Core.
		 *  When this happens, we need to create the layout graph for the ontologies. 
		 */
		if( e.getEvent() == OntologyChangeEvent.EventType.ONTOLOGY_ADDED ) {
			// we have to build the layout graphs.
			Ontology o = Core.getInstance().getOntologyByID(e.getOntologyID());
			buildLayoutGraphs(o);
			
			// display the ontology graphs
			layout.displayOntology(graphs,  e.getOntologyID() );
			updateSize();
			
			final Canvas2 canvas = this;
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					canvas.repaint();
				}
			});
			
		} else if( e.getEvent() == OntologyChangeEvent.EventType.ONTOLOGY_REMOVED ) {
			layout.removeOntology( graphs, e.getOntologyID() );
			updateSize();
			
			final Canvas2 canvas = this;
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					canvas.repaint();
				}
			});
		}
		
	}
	
	/**
	 * Builds the entire visual graph for one ontology.
	 */
	@Override
	public void buildLayoutGraphs(Ontology ontology) {
		// build the graph and put it in the repository, because we are not necessarily going to display it now
		List<CanvasGraph> gr = layout.buildGlobalGraph(ontology);
		Iterator<CanvasGraph> graphIter = gr.iterator();
		while( graphIter.hasNext() ) { graphIter.next().setVisible(false); } // initially the graphs should be invisible.  
																			 // they will be displayed when displayOntology() is called.
		graphs.addAll( gr );  // add them to our list of graphs.
	}
	
	
	public ArrayList<CanvasGraph> getGraphs() { return graphs; }
	
	// return a matcher graph with the given id (used in deleting mappings)
	public CanvasGraph getMatcherGraph( int matcherID ) {
{'fraction_non_alphanumeric': 0.13843685537474215, 'fraction_numerical': 0.009779203911681566, 'mean_word_length': 4.36915504511895, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '2407633', 'n_tokens_mistral': 4186, 'n_tokens_neox': 3740, 'n_words': 1119}