


/* Genomic Comparison Tool
   Master Thesis of computer science

   Author: Yue Lu
   Version: 1.1

*/



/* the program to draw the square and color or shape*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import java.io.*;
import java.lang.*;


public class DatabaseVisualDisplay extends JPanel
{

	//constructor
  public DatabaseVisualDisplay(String T_Name, Vector C_Data)
  {
	  Table_Name=T_Name;
	  Color_Data=C_Data;

	//execute the query to get the rowNumber of the query
	  String Query="select * from ["+Table_Name+"]";

	  DatabaseRowNumber Num=new DatabaseRowNumber(Query);

		//because the program display 100 genes per row
	  total=Num.getRowNumber();
          System.out.println("For some weird reason, it comes here???");
          System.out.println("The value of total number of genes to be displayed is " + total); 
	  //length=total/100;
          length=total/80;
          System.out.println("The display will display" +  length + " genes vertically");
	//in run time to decide whether the scrollbar will be show or not
	  this.setPreferredSize(new Dimension(1000, (length+1)*8+50));
  }


	//set the font
  public void setFonts(Graphics g)
   {
	  if (f != null) return;
      f = new Font("SansSerif", Font.BOLD, 2);
      fi = new Font("SansSerif",
         Font.BOLD + Font.ITALIC, 6);
      fm = g.getFontMetrics(f);
      fim = g.getFontMetrics(fi);
   }

	//paint the colors and shapes
   public void paintComponent(Graphics g)
   {
	  super.paintComponent(g);
        System.out.println("Inside paint component of DatabaseVisualDisplay TotalNumber");
        // am trying something here...
        //execute the query to get the rowNumber of the query
	  String Query="select * from ["+Table_Name+"]";

	  DatabaseRowNumber Num=new DatabaseRowNumber(Query);

		//because the program display 100 genes per row
	  total=Num.getRowNumber();
          
        //draw the number indicate the gene location in the database
	  //DrawNumber(10,g);
          DrawNumber(8,g);
	  //draw the Rectangles to represent the genes in the database
	  DrawRect(g);

	//get the row Data from the color shape table
	  Enumeration enum = Color_Data.elements();
	  while (enum.hasMoreElements())
	  {
	   	 Vector Data=(Vector)enum.nextElement();
	   	 Enumeration enum_Data = Data.elements();
	   	 int i=0;
	   	 while(enum_Data.hasMoreElements())
	     {
			 C_Data[i]=enum_Data.nextElement();
			 i++;
		 }


	   // get connection with database

	  try
      {
		  //get the Column_Name and value that use want to display
		 String Column_Name=C_Data[0].toString();
		 Object value2=C_Data[1];
		 Color color=(Color)C_Data[2];
		 Icon shape=(Icon)C_Data[3];

		 //get the shapes' name
		 String shapeString=shape.toString();
		 String s=shapeString.substring(0, 6);

		 if(color!=Color.white || !s.equals("blank.")){
                     System.out.println("Inside the If clause of 'color!=white....'");

			 if(value2==null)
                         {
                             System.out.println(" Value2 is null ");
				 query = "select ID, ["+Column_Name+"] from ["+Table_Name+"] where ["+Column_Name+"] is null";
			 }


			 else {

				 String string=new String();
                                 System.out.println("Inside the else part of things");
				 if(value2.getClass()==string.getClass())
				 {
                                        System.out.println("Executing the first query");

					query = "select ID, ["+Column_Name+"] from ["+Table_Name+"] where ["+Column_Name+"]="+"'"+value2+"'";
				 }

				 else
				 {
                                        System.out.println("Executing the second query");

					 query = "select ID, ["+Column_Name+"] from ["+Table_Name+"] where ["+Column_Name+"]="+value2;
				 }
			 }

			//get the database connection instance
			DatabaseConnection connection=DatabaseConnection.getInstance();
			 con=connection.getConnection();

			//get all the IDs that match with Column_Name and values
			 Statement stmt = con.createStatement();
                         System.out.println("The query being executed is" + query); 
			 rs = stmt.executeQuery(query);

			//iterate the ResultSet to draw the color and Shape for
			//each gene

			 while (rs.next())
			 {
				int proteinNum=rs.getInt(1);

				//fill the color on each gene
                              //  System.out.println("I am not sure wqhere the hell its going?"); 
				DrawColor(proteinNum,color,g);


				//draw shape on each gene
				//DrawShape(proteinNum, s, g);

			  }
			 stmt.close();
			 }
                  //rs.close();
	  }

		  catch (SQLException ex)
		  {
			  System.out.println ("10"+ex);

		  }

	   }

   }

   //draw the rectangle represent each gene
   public void DrawRect(Graphics g)
   {
	  int i, j, k;

	  for(i=0; i<length; i++)
	  {
	  	//for(j=0; j<100;j++)
                for(j=0; j<80;j++)
                {
		g.setColor(Color.black);
      		g.drawRect(80+j*12, 24+i*6, 12, 6);
                //g.drawRect(100+j*8, 24+i*8, 8, 8);
                }
	  }

	//get the rest of rectangle that are not enough for 80 genes
	  //int left=total%100;
          int left=total%80;
	  for(k=0; k<left; k++)
	  {
	  	//g.drawRect(100+k*8, 24+i*8, 8, 8);
                g.drawRect(80+k*12, 24+i*6, 12, 6);
          }
   }

	//draw the number for indicating the gene positions
   public void DrawNumber(int line, Graphics g)
   {
	  g.setColor(Color.black);
           System.out.println("The value of line is " + line);
		//draw horizontal numbers
	  for(int i=0; i<=line; i++)
	  {
	  	int j=i*10;
                String num=j + "";
		//int x=100+8*(j);
                int x=80+12*(j);
		//int y=10;
                int y=8;
		g.setFont(f);
		g.drawString(num, x, y);
	  }

	  //draw vertical Numbers
           System.out.println("The value of length is " + length);
           
	  for(int i=0; i<=length/10; i++)
          {
                int j=i*10;
	  	String num=j*100 + "";
		//int x=50;
                int x=50;
		//int y=30+8*(j);
                int y=24+8*(j);
		g.drawString(num, x, y);
	  }
   }


   //fill the color of the rectangle
   public void DrawColor(int proteinNum, Color Draw_Color, Graphics g)
   {
               // System.out.println("It is coming inside this DrawColor thingee somehow someone help mew!!!");
	  	g.setColor(Draw_Color);

	  	//set the mode of draw in order to make the color overlap
	  	g.setXORMode(Color.white);

	  	//fill the rectangles in a small size(not fully filled)
	  	//will make it visually nicer
	  	//g.fillRect(((proteinNum-1)%100)*8+101, ((proteinNum-1)/100)*8+1+24, 7, 7);
                g.fillRect(((proteinNum-1)%80)*12+81, ((proteinNum-1)/80)*6+25, 11, 5);
   }

	//fill the shape in the rectangles
	public void DrawShape(int proteinNum, String shape, Graphics g)
	{
		//Draw the shape which is shorter than the rectangle
		//will make it look better

		if(shape.equals("Shape1"))
		{
			g.setColor(Color.blue);
			g.setPaintMode();
			g.drawLine(((proteinNum-1)%100)*8+6+100, ((proteinNum-1)/100)*8+1+1+24,
						((proteinNum-1)%100)*8+1+1+100, ((proteinNum-1)/100)*8+6+24);
		}

		if(shape.equals("Shape2"))
		{
			g.setColor(Color.blue);
			g.setPaintMode();
			g.drawLine(((proteinNum-1)%100)*8+1+1+100, ((proteinNum-1)/100)*8+1+1+24,
						((proteinNum-1)%100)*8+6+100, ((proteinNum-1)/100)*8+6+24);
		}

		if(shape.equals("Shape4"))
		{
			g.setColor(Color.blue);
			g.setPaintMode();
			g.drawLine(((proteinNum-1)%100)*8+1+1+100, ((proteinNum-1)/100)*8+4+24,
						((proteinNum-1)%100)*8+6+100, ((proteinNum-1)/100)*8+4+24);
		}

		if(shape.equals("Shape3"))
		{
			g.setColor(Color.blue);
			g.setPaintMode();
			g.drawLine(((proteinNum-1)%100)*8+4+100, ((proteinNum-1)/100)*8+1+1+24,
						((proteinNum-1)%100)*8+4+100, ((proteinNum-1)/100)*8+6+24);
		}

	}




   private Font f;
   private Font fi;
   private FontMetrics fm;
   private FontMetrics fim;
   private int current = 0;
   private String Table_Name;
   private Vector Color_Data;
   private Object[] C_Data=new Object[4];
   private ResultSet rs;
   private int total;
   private int length;
   private Connection con;
   private String query;

}
