//Nathan Viniconis
//CSE269
//The database of the Cell Program!!!!!

import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

import java.io.*;


public class GUIdata {
   	
   	 //membrane 0, molecule 1, receptor 2
   	 //erase 20, customCM 40, customNM 41, customMito 42
   	int curTYPE=1; 
   	int curPAGE=1;
   	int curBUTTON=1;
   	int count = 1;
   	mainGUI main;
   	boolean mainset = false;
   	boolean multiple = false;
   	
   	Molecule tempMol = new Molecule();
   	Receptor tempRec = new Receptor();
   	Membrane tempMem = new Membrane();

//For Saving...
	news authInfo;
//ID Stuff
	int numMem = 0;
	int numRec = 0;
	int numMol = 0;
	String cellMem = new String("Cell Membrane");
	String nucMem = new String("Nuclear Membrane");
   	

//Author Data
   	String AuthorName = new String("defaultName");
   	String AuthorOrg = new String("defaultOrg");
   	String AuthorTitle = new String("defaultTitle");
   	String AuthorZip = new String("90210");
   	String AuthorState = new String("defaultState");
   	String AuthorCity = new String("defaultCity");
   	String AuthorStreet = new String("defaultStreet");
   	String AuthorEmail = new String("defaultEmail");

//Date Data
	String DateDay = new String("31");
	String DateMonth = new String("7");
	String DateYear = new String("1981");

//Description
	String Description = new String("defaultDescription");   	

//Objects	
	Molecule[] mols = new Molecule[1000];
	int moleculeCount = 0;
	Receptor[] recs = new Receptor[1000];
	int receptorCount = 0;
	Membrane[] mems = new Membrane[1000];
	int membraneCount = 0;
	
//Background
	String BackID;
	int BackWidth;
	int BackHeight;
	
	String DefaultDest = "0001";
	
	public void setDefDest(String in)	{
		DefaultDest = in;
		if(mainset)	{
			main.ToolStuff();
		}
	}
	public String getDefDest()	{
		return(DefaultDest);
	}
	
	public void setMain(mainGUI in)	{
		main = in;
		mainset = true;
	}
	
    public void setAuthInfo(news AuthInfo)	{
    	authInfo = AuthInfo;
    }
    public news getAuthInfo()	{
    	return(authInfo);
    }
    public boolean getMultiple()	{
    	return(multiple);
    }
    public void setMultiple(boolean mult)	{
    	multiple = mult;
    }
    public void setNumMol(int in)	{
    	numMol = in;
    }
    public void setNumRec(int in)	{
    	numRec = in;
    }
    public void setNumMem(int in)	{
    	numMem = in;
    }
    public int getNumMol()	{
    	return(numMol);
    }
    public int getNumRec()	{
    	return(numRec);
    }
    public int getNumMem()	{
    	return(numMem);
    }
    public Membrane getCM()	{
    	return(mems[0]);
    }
    public Membrane getNM()	{
    	return(mems[1]);
    }
    	
   	public int getPAGE()	{
   		return(curPAGE);
   	}
   	public int getTYPE()	{
   		return(curTYPE);
   	}
   	public int getBUTTON()	{
   		return(curBUTTON);
   	}
   	public void setPAGE(int page)	{
   		curPAGE = page;
   	}
   	public void setTYPE(int type)	{
   		curTYPE = type;
   	}
   	public void setBUTTON(int button)	{
   		curBUTTON = button;
   	}
  	public Molecule getTempMol()	{
  		return(tempMol);
  	}
  	public Receptor getTempRec()	{
  		return(tempRec);
  	}
  	public Membrane getTempMem()	{
  		return(tempMem);
  	}
  	public void setTempMol(Molecule in)	{
  		tempMol = in;
  	}
  	public void setTempRec(Receptor in)	{
  		tempRec = in;
  	}
  	public void setTempMem(Membrane in)	{
  		tempMem = in;
  	}
  	public void setCount(int in)	{
  		count = in;
  	}
  	public int getCount()	{
  		return(count);
  	}
  	  //Used for returning the Number of whatever type of thingy
 	public Molecule getMol(int num)	{
 		return(mols[num]);
 	}
 	public Receptor getRec(int num)	{
 		return(recs[num]);
 	}
 	public Membrane getMem(int num)	{
 		return(mems[num]);
 	}
 	public void setMol(int num, Molecule mol)	{
 		mols[num]=mol;
 	}
 	public void setMem(int num, Membrane mem)	{
 		mems[num] = mem;
 	}
 	public void setRec(int num, Receptor rec)	{
 		recs[num] = rec;
 	}
 	
 	//Information Extraction
	public int getMemCount()	{
		return(membraneCount);
	}
	public int getMolCount()	{
		return(moleculeCount);
	}
	public int getRecCount()	{
		return(receptorCount);
	}
	public void setMemCount(int in)	{
		membraneCount = in;
	}
	public void setMolCount(int in)	{
		moleculeCount = in;
	}
	public void setRecCount(int in)	{
		receptorCount = in;
	}
	
	
	//***********************AUTHOR DATA***********************    	
   	public void setName(String name)	{
   		AuthorName = name;
   	}	
   	public void setOrg(String org)	{
   		AuthorOrg = org;
   	}
   	public void setTitle(String title)	{
   		AuthorTitle = title;
   	}
   	public void setZip(String zip)	{
   		AuthorZip = zip;
   	}
   	public void setState(String state)	{
   		AuthorState = state;
   	}
   	public void setCity(String city)	{
   		AuthorCity = city;
   	}
   	public void setStreet(String street)	{
   		AuthorStreet = street;
   	}
   	public void setEmail(String email)	{
   		AuthorEmail = email;
   	}
   	public String getName()	{
   		return(AuthorName);
   	}	
   	public String getOrg()	{
   		return(AuthorOrg);
   	}
   	public String getTitle()	{
   		return(AuthorTitle);
   	}
   	public String getZip()	{
   		return(AuthorZip);
   	}
   	public String getState()	{
   		return(AuthorState);
   	}
   	public String getCity()	{
   		return(AuthorCity);
   	}
   	public String getStreet()	{
   		return(AuthorStreet);
   	}
   	public String getEmail()	{
   		return(AuthorEmail);
   	}
   	
//***********************DATE DATA***********************    	
   	public void setDay(String day)	{
   		DateDay = day;
   	}	
   	public void setMonth(String month)	{
   		DateMonth = month;
   	}
   	public void setYear(String year)	{
   		DateYear = year;
   	}   	
   	public String getDay()	{
   		return(DateDay);
   	}
   	public String getMonth()	{
   		return(DateMonth);
   	}
   	public String getYear()	{
   		return(DateYear);
   	}
   	
//************************BACKGROUND DATA*****************
	public void setBID(String ID)	{
   		BackID = ID;
   	}	
   	public String getBID()	{
   		return(BackID);
   	}
   	public void setBwidth(int width)	{
   		BackWidth = width;
   	}
   	public int getBwidth()	{
   		return(BackWidth);
   	}
   	public void setBheight(int height)	{
   		BackHeight = height;
   	}   
   	public int getBheight()	{
   		return(BackHeight);
   	}
//********8
//**********
//********8
   	public GUIdata() {
    }
    
    public void Clear()	{
    	if(membraneCount > 0)	{
    		membraneCount = 0;
    		numMem = 0;
   		}
   		moleculeCount = 0;
   		numMol = 0;
   		receptorCount = 0;
   		numRec = 0;
   		
 	}
 	
 	public void ChangeColor(Color inC, String Desc)	{
 		int i=0;
 		while(i<getMolCount())	{
 			if(getMol(i).getDescription() == Desc)	{
 				tempMol.setColorC(inC);
 				getMol(i).setColorC(inC);
 			}
 			i++;
 		}
 		i=0;
 		while(i<getRecCount())	{
 			if(getRec(i).getDescription() == Desc)	{
 				tempRec.setColorC(inC);
 				getRec(i).setColorC(inC);
 			}
 			i++;
 		}
 		i=0;
 		while(i<getMemCount())	{
 			if(getMem(i).getDescription() == Desc)	{
 				tempMem.setColorC(inC);
 				getMem(i).setColorC(inC);
 			}
 			i++;
 		}
 		main.ToolStuff();
 	}
  	
    public String returnAction()	{
    	String temp = new String();
    	
    	if(curTYPE == 0)	{
    		temp = "Place Membrane";
    	}
    	if(curTYPE == 1)	{
    		temp = "Place Molecule";
    	}
    	if(curTYPE == 2)	{
    		temp = "Place Receptor";
    	}
    	if(curTYPE == 20)	{
    		temp = "Erase";
    	}
    	if(curTYPE == 40)	{
    		temp = "Custom CelMembrane";
    	}
    	if(curTYPE == 41)	{
    		temp = "Custom NucMembrane";
    	}
    	if(curTYPE == 42)	{
    		temp = "Custom Mitochondria";
    	}
    	if(curTYPE == 50)	{
    		temp = "Erase Area";
    	}
    	if(curTYPE == 60)	{
    		temp = "Default Destination";
    	}
    	return(temp);
    }
 
//************************KEEP TRACK OF CURRENT SELECTED TYPE*********
	public void setcurSelect(int i, int j, int k)	{
		
		tempMol = new Molecule();
		tempRec = new Receptor();
		tempMem = new Membrane();
		int ct=0;
		boolean fd = false;
		count = 1;
		//i=type j=page k=button
		curTYPE = i;
		curPAGE = j;
		curBUTTON = k;
		if(i==0)	{
			if(j==1)	{
				if(k==1)	{
					tempMem.setRadius(215);
					tempMem.setColor("99ff00");
					tempMem.setDescription("Cellular Cytoplasm");
				}
				if(k==2)	{
					tempMem.setRadius(100);
					tempMem.setColor("ff3399");
					tempMem.setDescription("Nuclear Cytoplasm");
				}
				if(k==3)	{
					tempMem.setRadius(45);
					tempMem.setColor("cccc99");
					tempMem.setDescription("Mitochondria A Cytoplasm");
				}
				if(k==4)	{
					tempMem.setRadius(40);
					tempMem.setColor("ffff99");
					tempMem.setDescription("Mitochondria B Cytoplasm");
				}
			}
			if(j==2)	{
				if(k==1)	{
					tempMem.setRadius(70);
					tempMem.setColor("3399ff");
					tempMem.setDescription("Mitochondria C Cytoplasm");
				}
				if(k==2)	{
					tempMem.setRadius(90);
					tempMem.setColor("ffcccc");
					tempMem.setDescription("Mitochondria D Cytoplasm");
				}
				if(k==3)	{
					tempMem.setRadius(60);
					tempMem.setColor("00ccff");
					tempMem.setDescription("Mitochondria E Cytoplasm");
				}
				if(k==4)	{
					tempMem.setRadius(30);
					tempMem.setColor("cc9999");
					tempMem.setDescription("Mitochondria F Cytoplasm");
				}
			}
			ct = 0;
			fd = false;
			while(ct < getMemCount()	&& !fd)	{
				if(tempMem.getDescription().compareTo(getMem(ct).getDescription()) == 0)	{
					tempMem.setColorC(getMem(ct).getColorC());
					fd = true;
				}
				ct++;
			}
			fd = false;
		}
		if(i==1)	{
			tempMol.setDestination(getDefDest());
			if(j==1)	{
				if(k==1)	{
					tempMol.setColor("FF0000");
					tempMol.setMoleculeType("red");
					tempMol.setDescription("Mol-1-Alpha-Uno-A");
					//add red molecule
				}
				if(k==2)	{
					count = 4;
					tempMol.setColor("FF0000");
					tempMol.setMoleculeType("red");
					tempMol.setDescription("Mol-1-Alpha-Uno-A");
					//add 4 red molecules
				}
				if(k==3)	{
					tempMol.setColor("0000FF");
					tempMol.setMoleculeType("blue");
					tempMol.setDescription("Mol-2-Beta-Dos-B");
					//add blue molecule
				}
				if(k==4)	{
					tempMol.setColor("0000FF");
					tempMol.setMoleculeType("blue");
					tempMol.setDescription("Mol-2-Beta-Dos-B");
					count = 4;
					//add 4 blue molecules
				}
			}
			if(j==2)	{
				if(k==1)	{
					tempMol.setColor("FFFF00");
					tempMol.setMoleculeType("yellow");
					tempMol.setDescription("Mol-3-Chi-Tres-C");
				}
				if(k==2)	{
					count = 4;
					tempMol.setColor("FFFF00");
					tempMol.setMoleculeType("yellow");
					tempMol.setDescription("Mol-3-Chi-Tres-C");
					//add 4 yellow molecules
				}
				if(k==3)	{
					tempMol.setColor("006600");
					tempMol.setMoleculeType("green");
					tempMol.setDescription("Mol-4-Delta-Quatro-D");
					//add green molecule
				}
				if(k==4)	{
					tempMol.setColor("006600");
					tempMol.setMoleculeType("green");
					tempMol.setDescription("Mol-4-Delta-Quatro-D");
					count = 4;
					//add 4 green molecules
				}
			}
			ct = 0;
			fd = false;
			while(ct < getMolCount()	&& !fd)	{
				if(tempMol.getDescription().compareTo(getMol(ct).getDescription()) == 0)	{
					tempMol.setColorC(getMol(ct).getColorC());
					fd = true;
				}
				ct++;
			}
			fd = false;
		}
		if(i==2)	{
			if(j==1)	{
				if(k==1)	{
					tempRec.setColor("CC33FF");
					tempRec.setDescription("Rec-HDL-c 194");
				}
				if(k==2)	{
					tempRec.setColor("FF9900");
					tempRec.setDescription("LDL-c 80");
				}
				if(k==3)	{
					tempRec.setColor("33CC99");
					tempRec.setDescription("Apo B-specific LDL");
				}
				if(k==4)	{
					tempRec.setColor("FFFF33");
					tempRec.setDescription("47 kDa SREBP");
				}
			}
			ct = 0;
			fd = false;
			while(ct < getRecCount()	&& !fd)	{
				if(tempRec.getDescription().compareTo(getRec(ct).getDescription()) == 0)	{
					tempRec.setColorC(getRec(ct).getColorC());
					fd = true;
				}
				ct++;
			}
			fd = false;
		}
		
		if(count != 1)	{
			multiple = true;
		}
		else
			multiple = false;
	}
    
//************************ADD OBJECT TO CELLMODEL*********************    
    public void Add(Molecule co)	{
		//sets ID for current Molecule
    	if(numMol < 10)	{ 			co.setID("100" + numMol);	}
    	else if(numMol < 100)	{ 	co.setID("10"  + numMol);	}
    	else if(numMol < 1000)	{ 	co.setID("1"+numMol);		}
    	else						co.setID( "" + (1000+numMol));
    	co.setType("molecule");
   		mols[moleculeCount] = co;
    	moleculeCount++;
    	numMol++;
	}
	public void Add(Membrane co)	{
		//sets ID for current Membrane
	  if(membraneCount==0)	{
	  	AddCM(co);
	  }
	  else if(membraneCount == 1)	{
	  	AddNM(co);
	  }
	  else	{
		if(numMem < 8)	{ 			co.setID("000" + (numMem));	}
    	else if(numMem < 98)	{ 	co.setID("00" + (numMem));	}
    	else if(numMem < 998)	{ 	co.setID("0"+(numMem));		}
    	else						co.setID("" + (numMem));
    	co.setType("membrane");
    	mems[membraneCount] = co;
    	membraneCount++;
    	numMem++;
      }
	}
	public void Add(Receptor co)	{
		//sets ID for current Receptor
		if(numRec < 10)	{ 			co.setID("200" + numRec);	}
    	else if(numRec < 100)	{ 	co.setID("20" + numRec);	}
    	else if(numRec < 1000)	{ 	co.setID("2"+numRec);		}
    	else						co.setID("" + (2000+numRec));
    	co.setType("receptor");
    	recs[receptorCount] = co;
    	receptorCount++;
    	numRec++;
	}
	//adding/changing the cell membrane
	public void AddCM(Membrane co)	{
		// if already exists
		if(membraneCount > 0)	{
			co.setType("membrane");
			co.setID("0000");
			mems[0] = co;
		}
		else	{
		  //create a whole new one(this gets rid of the "pass by reference"
		  // error.
			mems[0] = new Membrane();
			mems[0].setID("0000");
			mems[0].setType("membrane");
			mems[0].setColor(co.getColor());
			mems[0].setRadius((int)co.getRadius());
			mems[0].setDescription(co.getDescription());
			mems[0].setX(co.getX());
			mems[0].setY(co.getY());
			membraneCount++;
			numMem++;
		}
	}
	//Same with Nuclear Membrane
	public void AddNM(Membrane co)	{
//		co.setType("membrane");
		if(membraneCount > 1)	{
			co.setType("membrane");
			co.setID("0001");
			mems[1] = co;
		}
		else if	(membraneCount != 0)	{
			mems[1] = new Membrane();
			mems[1].setID("0001");
			mems[1].setColor(co.getColor());
			mems[1].setRadius((int)co.getRadius());
			mems[1].setDescription(co.getDescription());
			mems[1].setX(co.getX());
			mems[1].setY(co.getY());
			membraneCount++;
			numMem++;
		}
	}
	
	   //Checks to see if a certain element exists
    public boolean ifExists(Molecule mol)	{
    	int curM = 0;
    	
    	while(curM < moleculeCount)	{
    		if(mols[curM] == mol)	{
    			return(true);
    		}
    		curM++;
    	}
    	return(false);
    }
    //Checks to see if a certain element exists
    public boolean ifExists(Receptor rec)	{
    	int curR = 0;
    	
    	while(curR < receptorCount)	{
    		if(recs[curR] == rec)	{
    			return(true);
    		}
    		curR++;
    	}
    	return(false);
    }
    //Checks to see if a certain element exists
    public boolean ifExists(Membrane mem)	{
    	int curM = 0;
    	
    	while(curM < membraneCount)	{
    		if(mems[curM] == mem)	{
    			return(true);
    		}
    		curM++;
    	}
    	return(false);
    }
    //Erases a certain data element
    public boolean Remove(Molecule mol)	{
    	int cur = 0;
    	int temp = 0;
    	Molecule tempMol = new Molecule();
    	
    	if(ifExists(mol))	{
    		while(mol != mols[cur] )	{
    			cur++;
    		}
    		while(cur < moleculeCount-1)	{
    			mols[cur] = mols[cur+1];
    			cur++;
    		}
    		moleculeCount--;
    		return(true);
    	}
    	return(false);
    }
        //Erases a certain data element
    public boolean Remove(Receptor rec)	{
    	int cur = 0;
    	int temp = 0;
    	Receptor tempRec = new Receptor();
    	
    	if(ifExists(rec))	{
    		while(rec != recs[cur] )	{
    			cur++;
    		}
    		while(cur < receptorCount-1)	{
    			recs[cur] = recs[cur+1];
    			cur++;
    		}
    		receptorCount--;
    		return(true);
    	}
    	return(false);
    }
    
    public boolean Remove(Membrane mem)	{
    	int cur = 0;
    	int temp = 0;
    	Membrane tempMem = new Membrane();
    	
    	if(ifExists(mem))	{
    		while(mem != mems[cur] )	{
    			cur++;
    		}
    		while(cur < membraneCount-1)	{
    			mems[cur] = mems[cur+1];
    			cur++;
    		}
    		membraneCount--;
    		return(true);
    	}
    	return(false);
    }
    
   //get a Molecule from the database
    public Molecule getMolXY(int tX, int tY)	{
    	Molecule damn = new Molecule();
		boolean MolFound = false;
    	int curMol = 0;
    	while(curMol < moleculeCount && MolFound == false)	{
    		
    		if(mols[curMol].containsPoint(tX,tY))	{
    			MolFound = true;
    			return(mols[curMol]);
    		}
    		curMol++;
    	}
    	return(damn);
    }
      //get a Membrane from the database
    public Membrane getMemXY(int tX, int tY)	{
    	Membrane damn = new Membrane();
    	boolean MemFound = false;
    	int curMem = membraneCount-1;
    	while(curMem >= 0 && MemFound == false)	{
    		
    		if(mems[curMem].containsPoint(tX,tY))	{
    			MemFound = true;
    			return(mems[curMem]);
    		}
    		curMem--;
    	}
    	return(damn);
    }
      //get a Receptor from the database
    public Receptor getRecXY(int tX, int tY)	{
    	Receptor damn = new Receptor();
    	boolean RecFound = false;
    	int curRec = 0;
    	while(curRec < receptorCount && RecFound == false)	{
    		
    		if(recs[curRec].containsPoint(tX,tY))	{
    			RecFound = true;
    			return(recs[curRec]);
    		}
    		curRec++;
    	}
    	return(damn);
    }
    
    public Receptor getRecBondedTo(String bon)	{
    	Receptor damn = new Receptor();
    	boolean RecFound = false;
    	int curRec = 0;
    	while(curRec < receptorCount && RecFound == false)	{
    		if(recs[curRec].getBondedTo() == bon)	{
    			RecFound = true;
    			return(recs[curRec]);
    		}
    		curRec++;
    	}
    	return(damn);
    }
    	
    
    //get "type" given XY coords
    public String getTypeXY(int tX, int tY)	{
    	//First Check Molecules (since they're on top)
    	int curMol = 0;
    	int curRec = 0;
    	int curMem = 0;
    	boolean MolFound = false;
    	boolean RecFound = false;
    	boolean MemFound = false;
    	while(curMol < moleculeCount && MolFound == false)	{
    		
    		if(mols[curMol].containsPoint(tX,tY))	{
    			MolFound = true;
    			return(mols[curMol].getType());
    		}
    		curMol++;
    	}
    	if(!MolFound)	{
    		while(curRec < receptorCount && RecFound == false)	{
    			
    			if(recs[curRec].containsPoint(tX,tY))	{
    				RecFound = true;
    				return(recs[curRec].getType());
    			}
    			curRec++;
    		}
    	}
    	if(!MolFound && !RecFound)	{
    		
    		while(curMem < membraneCount && MemFound == false)	{
    				
    			if(mems[curMem].containsPoint(tX,tY))	{
					MemFound = true;
					return(mems[curMem].getType());
				}
				curMem++;
			}
		}
		//Not Found
		return("**");
	}
						    	
   
    //get "ID" given XY coords
    public String getIDXY(int tX, int tY)	{
    	//First Check Molecules (since they're on top)
     	int curMol = 0;
    	int curRec = 0;
    	int curMem = 0;
    	boolean MolFound = false;
    	boolean RecFound = false;
    	boolean MemFound = false;
    	while(curMol < moleculeCount && MolFound == false)	{
    		
    		if(mols[curMol].containsPoint(tX,tY))	{
    			MolFound = true;
    			return(mols[curMol].getID());
    		}
    		curMol++;
    	}
    	if(!MolFound)	{
    		while(curRec < receptorCount && RecFound == false)	{
    			
    			if(recs[curRec].containsPoint(tX,tY))	{
    				RecFound = true;
    				return(recs[curRec].getID());
    			}
    			curRec++;
    		}
    	}
    	if(!MolFound && !RecFound)	{
    		curMem = membraneCount-1;
    		while(curMem >= 0 && MemFound == false)	{
    				
    			if(mems[curMem].containsPoint(tX,tY))	{
					MemFound = true;
					return(mems[curMem].getID());
				}
				curMem--;
			}
		}
		// Not Found
		return("**");
	}
	
	public String ReturnTag(int X, int Y)	{
		boolean found = false;
		Membrane tMem = new Membrane();
		int i=0;
		while(i < moleculeCount && found == false)	{
			if(mols[i].containsPoint(X,Y))	{
				found = true;
				return(mols[i].getType());
			}
			i++;
		}
		i=0;
		while(i < receptorCount && found == false)	{
			if(recs[i].containsPoint(X,Y))	{
				found = true;
				return(recs[i].getType());
			}
			i++;
		}
		i=membraneCount-1;
		while(i >= 0 && found == false)	{
			if(mems[i].containsPoint(X,Y))	{
				tMem.setX(mems[i].getX());
				tMem.setY(mems[i].getY());
				tMem.setRadius((int)mems[i].getRadius()-2);
				if(tMem.containsPoint(X,Y))	{
					if(i==0){
						return("Cellular CytoPlasm");
					}
					else if(i==1)	{
						return("Nuclear CytoPlasm");
					}
					else {
						return("Mito CytoPlasm");
					}
				}
				else{
					if(i==0)	{
						return("Cell Membrane");
					}
					else if(i==1)	{
						return("Nuclear Membrane");
					}
					else 
						return("Mito Membrane");
				}
			}
			i--;
		}
		return("**");
	}
		
	public String getParentID(Molecule M)	{
		int i = membraneCount-1;
		boolean found = false;
		
		while(i>=0 && !found)	{
			if(getMem(i).containsPoint(M.getX(),M.getY()))	{
				found = true;
				return(getMem(i).getID());
			}
			i--;
		}
		if(found==false)	{
			return("NULL");
		}
		return("XXXX");
	}
	
	public String getParentID(Receptor R)	{
		int i = membraneCount-1;
		boolean found = false;
		
		while(i>=0 && !found)	{
			if(getMem(i).containsPoint(R.getX(),R.getY()))	{
				found = true;
				return(getMem(i).getID());
			}
			i--;
		}
		if(found==false)	{
			return("NULL");
		}
		return("XXXX");
	}
					
		
  
   	public void SAVE()	{
   		int i,j,k;
   		System.out.print("Making DTD\n");		
 		 try {
			BufferedWriter out = new BufferedWriter(new FileWriter("model2.dtd"));
//******* begin generic info  
  
 		    out.write("<?xml version='1.0' encoding='us-ascii'?>\n\n");
    		out.write("<!--  Team 2A, Nate, Pete, Ken, Slav -->\n\n");

			out.write("<!ELEMENT model (author, date, description, membrane*, molecule*, receptor*, model-background)>\n");
			out.write("<!ATTLIST model\n");
			out.write("				id CDATA #REQUIRED\n>\n");
			out.write("<!ELEMENT author (author-name,address,author-organization,author-title,author-email)>\n");
			out.write("<!ELEMENT author-name (#PCDATA)>\n");
			out.write("<!ELEMENT author-organization (#PCDATA)>\n");
			out.write("<!ELEMENT author-title (#PCDATA)>\n");
			out.write("<!ELEMENT author-email (#PCDATA)>\n");
			out.write("<!ELEMENT address (street,city,state,zip-code)>\n");
			out.write("<!ELEMENT street (#PCDATA)> \n");
			out.write("<!ELEMENT city (#PCDATA)>\n");
			out.write("<!ELEMENT state (#PCDATA)> \n");
			out.write("<!ELEMENT zip-code (#PCDATA)>\n");
			out.write("<!ELEMENT date (day,month,year)> \n");
			out.write("<!ELEMENT day (#PCDATA)>\n");
			out.write("<!ELEMENT month (#PCDATA)>\n");
			out.write("<!ELEMENT year (#PCDATA)>\n");
			out.write("<!ELEMENT description (#PCDATA)> \n");
			out.write("<!ELEMENT x (#PCDATA)>\n");
			out.write("<!ELEMENT y (#PCDATA)>\n");
			out.write("<!ELEMENT width (#PCDATA)>\n");
			out.write("<!ELEMENT height (#PCDATA)>\n");
			out.write("<!ELEMENT destination (#PCDATA)>\n");
			out.write("<!ELEMENT color (#PCDATA)>\n");
			out.write("<!-- all colors are in hexadecimal format -->\n\n");
			out.write("<!-- parent_id is the id of the object that contains this object -->\n\n");
			out.write("<!-- x, y refer to the top left point -->\n\n");
			out.write("<!-- membrane types: cell, nucleus, other -->\n");			
			out.write("<!ELEMENT membrane (x, y, width, height, description, color)>\n");
			out.write("<!ATTLIST membrane\n");
			out.write("            id        CDATA  #REQUIRED\n");
			out.write("            parent_id CDATA  #REQUIRED\n");
			out.write("            type      CDATA  #REQUIRED\n");
			out.write(">\n");
			out.write("<!-- molecule types: red, green, blue, yellow -->\n");
			out.write("<!ELEMENT molecule (x, y, width, height, description, color, destination)>\n");
			out.write("<!ATTLIST molecule\n");
			out.write("            id        CDATA  #REQUIRED\n");
			out.write("            parent_id CDATA  #REQUIRED\n");
			out.write("            type      CDATA  #REQUIRED\n");
			out.write(">\n");
			out.write("<!ELEMENT receptor (x, y, width, height, description, color)>\n");
			out.write("<!ATTLIST receptor\n");
			out.write("            id        CDATA  #REQUIRED\n");
			out.write("            parent_id CDATA  #REQUIRED\n");
			out.write("            type      CDATA  #REQUIRED\n");
			out.write(">\n");
			out.write("\n");
			out.write("<!ELEMENT model-background (width, height, color)>\n");
			out.close();
			 } 
			 catch (IOException dfgdf) {}
		System.out.print("DTD Made\n");


//************************ DTD MADE, XML TIME *****************************
		System.out.print("XML Time\n");
		try {
	
			BufferedWriter out = new BufferedWriter(new FileWriter(authInfo.getXMLname()));
//****Generic Info
			out.write("<?xml version='1.0' encoding='utf-8'?>\n");
			out.write("<!-- Team 2A, NaTe PeTe KeN SlaV -->\n\n");
			out.write("<!DOCTYPE model SYSTEM \"model2.dtd\" >\n\n");
			out.write("<model id='0000'>\n\n");
//****Begin Author
			out.write("  <!-- Author of this XML file. -->\n");
			out.write("<author>\n");
			out.write("    <author-name>"+authInfo.getFirstName()+" "+authInfo.getLastName()+"</author-name>\n");
			out.write("    <address>\n");
			out.write("      <street>"+authInfo.getStreet()+"</street>\n");
			out.write("      <city>"+authInfo.getCity()+"</city>\n");
			out.write("      <state>"+authInfo.getStateCB()+"</state>\n");
			out.write("      <zip-code>"+authInfo.getZip()+"</zip-code>\n");
			out.write("    </address>\n");
			out.write("    <author-organization>"+authInfo.getOrg()+"</author-organization>\n");
			out.write("    <author-title>"+authInfo.getTitle()+"</author-title>\n");
			out.write("    <author-email>"+authInfo.getEmail()+"</author-email>    \n");
			out.write("  </author>\n\n");
//****Begin Date
			out.write("  <!-- Date XML file was created. -->\n");
			out.write("  <date> \n");
			out.write("    <day>"+authInfo.getDay()+"</day>\n");
			int month;
			if(authInfo.getMonth() == "January")	{
				month = 1;
			}
			else if(authInfo.getMonth() == "February")	{
				month = 2;
			}
			else if(authInfo.getMonth() == "March")	{
				month = 3;
			}
			else if(authInfo.getMonth() == "April")	{
				month = 4;
			}
			else if(authInfo.getMonth() == "May")	{
				month = 5;
			}
			else if(authInfo.getMonth() == "June")	{
				month = 6;
			}
			else if(authInfo.getMonth() == "July")	{
				month = 7;
			}
			else if(authInfo.getMonth() == "August")	{
				month = 8;
			}
			else if(authInfo.getMonth() == "September")	{
				month = 9;
			}
			else if(authInfo.getMonth() == "October")	{
				month = 10;
			}
			else if(authInfo.getMonth() == "November")	{
				month = 11;
			}
			else if(authInfo.getMonth() == "December")	{
				month = 12;
			}
			else {
				month = 3;
			}
			out.write("    <month>"+month+"</month>\n");
			out.write("    <year>"+authInfo.getYear()+"</year>\n");
			out.write("  </date>\n\n");
//****Begin Description
			out.write("  <!-- Description of this XML file. -->\n");
			out.write("  <description>\n");
			out.write("    "+authInfo.getDescription()+"\n");
			out.write("  </description> \n\n");




//****Begin Membranes****
			i = membraneCount-1;
			String typeID = new String();
			String parentID;
			int X,Y,width;
			while(i>=0)	{
			//Get Type
				if(i==0)	{
					typeID = "cell";
				}
				else if(i==1)	{
					typeID = "nucleus";
				}
				else 	{
					typeID = "other";
				}
			//Get ID
				parentID = "0000";
			//Get XY
				
				X=(int)mems[i].getX() - (int)mems[i].getRadius();
				Y=(int)mems[i].getY() - (int)mems[i].getRadius();
				width = (int)mems[i].getRadius()*2;
					
				
				out.write("  <!-- A membrane. -->\n");
				out.write("  <membrane id = '"+mems[i].getID()+"' parent_id = '"+parentID+"' type = '"+typeID+"'>\n");
				out.write("    <x>"+X+"</x>\n");
				out.write("    <y>"+Y+"</y>\n");
				out.write("    <width>"+width+"</width>\n");
				out.write("    <height>"+width+"</height>\n");
				out.write("    <description>"+mems[i].getDescription()+"</description>\n");
				out.write("    <color>"+mems[i].getHexColor()+"</color>  \n");
				out.write("  </membrane>\n\n");
				i--;
			}

//******  Molecules
			i=0;

			while(i<moleculeCount)	{
		
			//Get Type
				if(getMol(i).getDescription().compareTo("Mol-1-Alpha-Uno-A") == 0)	{
					typeID = "red";
				}
				else if(getMol(i).getDescription().compareTo("Mol-2-Beta-Dos-B") == 0)	{
					typeID = "blue";
				}
				else if(getMol(i).getDescription().compareTo("Mol-3-Chi-Tres-C") == 0)	{
					typeID = "yellow";
				}
				else
					typeID = "green";
				
			//Get ID
				parentID = getParentID(getMol(i));
			//Get XY
				
				X=(int)mols[i].getX() - (int)mols[i].getRadius();
				Y=(int)mols[i].getY() - (int)mols[i].getRadius();
				width = (int)mols[i].getRadius()*2;
					
				
				out.write("  <!-- A molecule. -->\n");
				out.write("  <molecule id = '"+mols[i].getID()+"' parent_id = '"+parentID+"' type = '"+typeID+"'>\n");
				out.write("    <x>"+X+"</x>\n");
				out.write("    <y>"+Y+"</y>\n");
				out.write("    <width>"+width+"</width>\n");
				out.write("    <height>"+width+"</height>\n");
				out.write("    <description>"+mols[i].getDescription()+"</description>\n");
				out.write("    <color>"+mols[i].getHexColor()+"</color>  \n");
				out.write("    <destination>"+mols[i].getDestination()+"</destination>\n");
				out.write("  </molecule>\n\n");
				i++;
			}
			
	//****** Receptors
			i=0;
			while(i<receptorCount)	{
		
			//Get Type
				typeID = getRec(i).getType();	
			//Get ID
				parentID = getRec(i).getBondedTo();
			//Get XY
				
				X=(int)recs[i].getX() - (int)recs[i].getRadius();
				Y=(int)recs[i].getY() - (int)recs[i].getRadius();
				width = (int)recs[i].getRadius()*2;
					
				
				out.write("  <!-- A receptor. -->\n");
				out.write("  <receptor id = '"+recs[i].getID()+"' parent_id = '"+parentID+"' type = '"+typeID+"'>\n");
				out.write("    <x>"+X+"</x>\n");
				out.write("    <y>"+Y+"</y>\n");
				out.write("    <width>"+width+"</width>\n");
				out.write("    <height>"+width+"</height>\n");
				out.write("    <description>"+recs[i].getDescription()+"</description>\n");
				out.write("    <color>"+recs[i].getHexColor()+"</color>  \n");
				out.write("  </receptor>\n\n");
				i++;
			}
	
		
//****Begin Background
			out.write("  <model-background>\n");
			out.write("      <width>500</width>\n");
			out.write("      <height>500</height>\n");
			out.write("      <color>FCFCFC</color>\n");
			out.write("  </model-background> \n");
			out.write("\n");
			out.write("</model>\n");	
			out.close();
  		} 
  		catch (IOException dfgdf) {System.out.print("Error in XML");}
	}
}