<commit_before>// PathVisio,
// a tool for data visualization and analysis using Biological Pathways
// Copyright 2006-2007 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
//  
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
//
package org.pathvisio.model;

import java.io.File;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.jdom.Document;
import org.jdom.Element;
import org.pathvisio.debug.Logger;
import org.pathvisio.model.GraphLink.GraphRefContainer;

/**
* This class is the model for pathway data. It is responsible for
* storing all information necessary for maintaining, loading and saving
* pathway data.
* 
* Pathway contains multiple PathwayElements. Pathway is guaranteed
* to always have exactly one object of the type MAPPINFO and exactly
* one object of the type INFOBOX.
*/
public class Pathway implements PathwayListener
{		
	private boolean changed = true;
	/**
	   The "changed" flag tracks if the Pathway has been changed since
	   the file was opened or last saved. New pathways start changed.
	 */
	public boolean hasChanged() { return changed; }
	
	/**
	   clearChangedFlag should be called after when the current
	   pathway is known to be the same as the one on disk. This
	   happens when you just opened it, or when you just saved it.
	*/
	private void clearChangedFlag()
	{
		if (changed)
		{
			changed = false;
			fireStatusFlagEvent (new StatusFlagEvent (changed));
			//System.out.println ("Changed flag is cleared");
		}		
	}
	
	/**
	   To be called after each edit operation
	*/
	private void markChanged()
	{
		if (!changed)
		{
			changed = true;
			fireStatusFlagEvent (new StatusFlagEvent (changed));
			//System.out.println ("Changed flag is set");
		}
	}
	
	/**
	 * factor to convert screen cordinates used in GenMAPP to pixel cordinates
	 * NOTE: maybe it is better to adapt gpml to store cordinates as pixels and
	 * divide the GenMAPP cordinates by this factor on conversion
	 * 
	 * @deprecated
	 */
	final public static int OLD_GMMLZOOM = 15;
		
	/**
	 * List of contained dataObjects
	 */
	private List<PathwayElement> dataObjects = new ArrayList<PathwayElement>();
	
	/**
	 * Getter for dataobjects contained. There is no setter, you
	 * have to add dataobjects individually
	 * @return List of dataObjects contained in this pathway
	 */
	public List<PathwayElement> getDataObjects() 
	{
		return dataObjects;
	}
	
	/**
	 * Get a pathway element by it's GraphId
	 * @param graphId The graphId of the element
	 * @return The pathway element with the given id, or null when no element was found
	 */
	public PathwayElement getElementById(String graphId) {
		//TODO: dataobject should be stored in a hashmap, with the graphId as key!
		for(PathwayElement e : dataObjects) {
			if(graphId.equals(e.getGraphId())) {
				return e;
			}
		}
		return null;
	}
	
	/**
	 * Takes the Xref of all DataNodes in this pathway
	 * and returns them as a List.
	 * 
	 * returns an empty arraylist if there are no datanodes in
	 * this pathway.
	 */
	public List<Xref> getDataNodeXrefs()
	{
		List<Xref> result = new ArrayList<Xref>();
		for (PathwayElement e : dataObjects)
		{
			if (e.getObjectType() == ObjectType.DATANODE)
			{
				result.add(e.getXref());
			}
		}
		return result;
	}
	
	private PathwayElement mappInfo = null;
	private PathwayElement infoBox = null;
	private PathwayElement biopax = null;
	
	/**
	 * get the one and only MappInfo object.
	 * 
	 * @return a PathwayElement with ObjectType set to mappinfo.
	 */
	public PathwayElement getMappInfo()
	{
		return mappInfo;
	}
	
	/**
	 * get the one and only InfoBox object.
	 * 
	 * @return a PathwayElement with ObjectType set to mappinfo.
	 */
	public PathwayElement getInfoBox()
	{
		return infoBox;
	}

	/**
	   note: may return null.
	 */
	public PathwayElement getBiopax()
	{
		return biopax;
	}
	
	public void createBiopax()
	{
		biopax = new PathwayElement(ObjectType.BIOPAX);
		this.add(biopax);
	}

	/**
	 * Add a PathwayElement to this Pathway.
	 * takes care of setting parent and removing from possible previous
	 * parent. 
	 * 
	 * fires PathwayEvent.ADDED event <i>after</i> addition of the object
	 * 
	 * @param o The object to add
	 */
	public void add (PathwayElement o)
	{
		assert (o != null);
		// There can be only one mappInfo object, so if we're trying to add it, remove the old one.
		if (o.getObjectType() == ObjectType.MAPPINFO && o != mappInfo)
		{
			if(mappInfo != null) {
				replaceUnique (mappInfo, o);
				mappInfo = o;
				return;
			}
			mappInfo = o;
		}
		// There can be only one InfoBox object, so if we're trying to add it, remove the old one.
		if (o.getObjectType() == ObjectType.INFOBOX && o != infoBox)
		{
			if(infoBox != null) {
				replaceUnique (infoBox, o);
				infoBox = o;
				return;
			}
			infoBox = o;
		}
		// There can be zero or one Biopax object, so if we're trying to add it, remove the old one.
		if(o.getObjectType() == ObjectType.BIOPAX && o != biopax)
		{
			if(biopax != null) {
				replaceUnique (biopax, o);
				biopax = o;
				return;
			}
			biopax = o;
		}
		if (o.getParent() == this) return; // trying to re-add the same object
		forceAddObject(o);
	}
	<commit_msg>fixed #298<commit_after>// PathVisio,
// a tool for data visualization and analysis using Biological Pathways
// Copyright 2006-2007 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
//  
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
//
package org.pathvisio.model;

import java.io.File;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.jdom.Document;
import org.jdom.Element;
import org.pathvisio.debug.Logger;
import org.pathvisio.model.GraphLink.GraphRefContainer;

/**
* This class is the model for pathway data. It is responsible for
* storing all information necessary for maintaining, loading and saving
* pathway data.
* 
* Pathway contains multiple PathwayElements. Pathway is guaranteed
* to always have exactly one object of the type MAPPINFO and exactly
* one object of the type INFOBOX.
*/
public class Pathway implements PathwayListener
{		
	private boolean changed = true;
	/**
	   The "changed" flag tracks if the Pathway has been changed since
	   the file was opened or last saved. New pathways start changed.
	 */
	public boolean hasChanged() { return changed; }
	
	/**
	   clearChangedFlag should be called after when the current
	   pathway is known to be the same as the one on disk. This
	   happens when you just opened it, or when you just saved it.
	*/
	private void clearChangedFlag()
	{
		if (changed)
		{
			changed = false;
			fireStatusFlagEvent (new StatusFlagEvent (changed));
			//System.out.println ("Changed flag is cleared");
		}		
	}
	
	/**
	   To be called after each edit operation
	*/
	private void markChanged()
	{
		if (!changed)
		{
			changed = true;
			fireStatusFlagEvent (new StatusFlagEvent (changed));
			//System.out.println ("Changed flag is set");
		}
	}
	
	/**
	 * factor to convert screen cordinates used in GenMAPP to pixel cordinates
	 * NOTE: maybe it is better to adapt gpml to store cordinates as pixels and
	 * divide the GenMAPP cordinates by this factor on conversion
	 * 
	 * @deprecated
	 */
	final public static int OLD_GMMLZOOM = 15;
		
	/**
	 * List of contained dataObjects
	 */
	private List<PathwayElement> dataObjects = new ArrayList<PathwayElement>();
	
	/**
	 * Getter for dataobjects contained. There is no setter, you
	 * have to add dataobjects individually
	 * @return List of dataObjects contained in this pathway
	 */
	public List<PathwayElement> getDataObjects() 
	{
		return dataObjects;
	}
	
	/**
	 * Get a pathway element by it's GraphId
	 * @param graphId The graphId of the element
	 * @return The pathway element with the given id, or null when no element was found
	 */
	public PathwayElement getElementById(String graphId) {
		//TODO: dataobject should be stored in a hashmap, with the graphId as key!
		for(PathwayElement e : dataObjects) {
			if(graphId.equals(e.getGraphId())) {
				return e;
			}
		}
		return null;
	}
	
	/**
	 * Takes the Xref of all DataNodes in this pathway
	 * and returns them as a List.
	 * 
	 * returns an empty arraylist if there are no datanodes in
	 * this pathway.
	 */
	public List<Xref> getDataNodeXrefs()
	{
		List<Xref> result = new ArrayList<Xref>();
		for (PathwayElement e : dataObjects)
		{
			if (e.getObjectType() == ObjectType.DATANODE)
			{
				result.add(e.getXref());
			}
		}
		return result;
	}
	
	private PathwayElement mappInfo = null;
	private PathwayElement infoBox = null;
	private PathwayElement biopax = null;
	private PathwayElement legend = null;
	
	/**
	 * get the one and only MappInfo object.
	 * 
	 * @return a PathwayElement with ObjectType set to mappinfo.
	 */
	public PathwayElement getMappInfo()
	{
		return mappInfo;
	}
	
	/**
	 * get the one and only InfoBox object.
	 * 
	 * @return a PathwayElement with ObjectType set to mappinfo.
	 */
	public PathwayElement getInfoBox()
	{
		return infoBox;
	}

	/**
	   note: may return null.
	 */
	public PathwayElement getBiopax()
	{
		return biopax;
	}
	
	public void createBiopax()
	{
		biopax = new PathwayElement(ObjectType.BIOPAX);
		this.add(biopax);
	}

	/**
	 * Add a PathwayElement to this Pathway.
	 * takes care of setting parent and removing from possible previous
	 * parent. 
	 * 
	 * fires PathwayEvent.ADDED event <i>after</i> addition of the object
	 * 
	 * @param o The object to add
	 */
	public void add (PathwayElement o)
	{
		assert (o != null);
		// There can be only one mappInfo object, so if we're trying to add it, remove the old one.
		if (o.getObjectType() == ObjectType.MAPPINFO && o != mappInfo)
		{
			if(mappInfo != null) {
				replaceUnique (mappInfo, o);
				mappInfo = o;
				return;
			}
			mappInfo = o;
		}
		// There can be only one InfoBox object, so if we're trying to add it, remove the old one.
		if (o.getObjectType() == ObjectType.INFOBOX && o != infoBox)
		{
			if(infoBox != null) {
				replaceUnique (infoBox, o);
				infoBox = o;
				return;
			}
			infoBox = o;
		}
		// There can be zero or one Biopax object, so if we're trying to add it, remove the old one.
		if(o.getObjectType() == ObjectType.BIOPAX && o != biopax)
		{
			if(biopax != null) {
				replaceUnique (biopax, o);
				biopax = o;
				return;
			}
			biopax = o;
		}
		// There can be only one Legend object, so if we're trying to add it, remove the old one.
		if (o.getObjectType() == ObjectType.LEGEND && o != legend)
		{
			if(legend != null) {
				replaceUnique (legend, o);
				legend = o;
				return;
			}
			legend = o;
		}
		if (o.getParent() == this) return; // trying to re-add the same object
		forceAddObject(o);
	}
	
{'fraction_non_alphanumeric': 0.09526982011992005, 'fraction_numerical': 0.002581612258494337, 'mean_word_length': 3.2048319327731094, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3618735', 'n_tokens_mistral': 4115, 'n_tokens_neox': 3653, 'n_words': 1386}