/*
 *
 * Copyright (C)   20 dec. 2003 Jean-Yves Beaujean 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,  59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.

 */
package jxtray.diag;



import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.print.Printable;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JPanel;

import java.awt.Graphics;

import java.util.Vector;

import java.sql.Connection;

import jxtray.Messages;

public class DiPanel extends JPanel implements MouseListener, MouseMotionListener, ActionListener, Printable
{

	private double zoom = DiConstants.ZOOM_NORMAL;
	private String title;
	private Connection connection;
	
	private Vector vTable = new Vector();
	
	/** Menu contextuel*/
	private DiPopupMenu diMenu;	
	
	public DiPanel(String title, Connection connection)
	{
		super();
		this.title = title;
		this.connection = connection;
		
		addMouseListener(this);
		addMouseMotionListener(this);
		
		diMenu = new DiPopupMenu(this);
		this.add(diMenu);
		this.setDoubleBuffered(true);
	}
	
	public void paintComponent(Graphics g)
	{
		DiTable table = null;
		super.paintComponent(g);	
		for ( int i = 0; i < vTable.size(); i++)
		{
			table = (DiTable) vTable.elementAt(i);
			table.paint(g);	 
		}
		
	}
	public void actionPerformed(ActionEvent e)
	{
		
			if ( e.getActionCommand().equals("zoomsupersmall"))
			{	
				setZoom(DiConstants.ZOOM_SUPERSMALL);
				this.repaint();
			}
			else if ( e.getActionCommand().equals("zoomsmall"))
			{
				setZoom(DiConstants.ZOOM_SMALL);
				this.repaint();
			}
			else if ( e.getActionCommand().equals("zoomnormal"))
			{
				setZoom(DiConstants.ZOOM_NORMAL); 
				this.repaint();
			}
			else if ( e.getActionCommand().equals("zoombig"))
			{
				setZoom(DiConstants.ZOOM_BIG);
				this.repaint();
			}		
			else if ( e.getActionCommand().equals("print"))
			{
					this.createPrintJob();
			}			
	}	
	
	public void mouseClicked(MouseEvent e) 
	{
	}
		
	public void mouseReleased(MouseEvent e) 
	{
			if ( e.getButton() == MouseEvent.BUTTON2 ||  e.getButton() == MouseEvent.BUTTON3 )
			{
					diMenu.show(this,e.getX(),e.getY());
			}
	}
	public void mousePressed(MouseEvent e)
	{
	}
	public void mouseDragged(MouseEvent e)
	{
	}
	public void mouseExited(MouseEvent e)
	{
	}
	public void mouseEntered(MouseEvent e)
	{
	}
	public void mouseMoved(MouseEvent e) 
	{
	}
	
	public void createPrintJob() 
	{

			PrinterJob job = PrinterJob.getPrinterJob();
			PageFormat pf = job.defaultPage();
			PageFormat pf2 = job.pageDialog(pf);
		
			if (pf2 != pf) 
			{
				job.setPrintable(this, pf2);
				if (job.printDialog()) 
				{
					try 
					{
						job.print();
					}
					catch (PrinterException e) 
					{
						Messages.error(e.getMessage());
					}
				}
			}
		}	
	
	
	public int print(Graphics g, PageFormat pf, int pageIndex) 
	{
			//Paper p = new Paper();
			//p.setImageableArea(0,0,850,600);
			//pf.setPaper(p);
	
			if (pageIndex != 0) 
			{
				return NO_SUCH_PAGE;
			}
	
			//Graphics2D g2 = (Graphics2D) g;
			double x = pf.getImageableX();
			double y = pf.getImageableY(); 
		
			double w = pf.getImageableWidth();
			double h = pf.getImageableHeight();
			// il faut dessiner sur g le contenu à imprimer
			this.paintComponent(g);
			//chart.draw(g2, new Rectangle2D.Double(x, y, w, h), null);

			return PAGE_EXISTS;
	}
	
	public void setZoom(double zoom)
	{
			this.zoom = zoom;
			DiTable table = null;
			for ( int i = 0; i < vTable.size(); i++)
			{
				table = (DiTable) vTable.elementAt(i);
				table.setZoom(zoom);
					 
			}
			
			this.repaint();
	}		
	
	
	/** name must be lib.tablename */ 
	public void addTable(String db, String table)
	{
		DiTable newTable = new DiTable(db+"."+table);
		
		newTable.setSize(100,100);
		/*System.out.println("Analyse colonnes de la  table"+ db +"."+table);
		try
		{
			ResultSet res1 = connection.getMetaData().getColumns(db,null,table,"texture");
			ResultSetMetaData rmtd1 = res1.getMetaData();
			int columncount = rmtd1.getColumnCount();
			while ( res1.next() ) 
			{
					String ligne = new String();
					for ( int i =1; i <= columncount; i++)
							ligne +="\t"+ rmtd1.getColumnName(i) + " = "+ res1.getString(i)+ " | ";
					System.out.println(ligne);
			}
		} 
		catch ( Exception e ) { System.err.println("DiPanel.addTable() : "+ e ); }*/
					
		addTable(newTable);
	}
	
	public void addTable(DiTable newTable)
	{
		vTable.add(newTable);
	}	
}
