package chronoDivers;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicButtonUI;

import chrono.ChronoFrame;
import chrono.ChronoPanel;
import chrono.Main;

public class TabButton  extends JButton  implements MouseListener
{
	private static final long serialVersionUID = 1L;
	private ButtonTabComponent comp;
  
	public TabButton(ButtonTabComponent comp)
	{
		int size = 17;
		this.comp = comp;
		setPreferredSize(new Dimension(size, size));
    
		setUI(new BasicButtonUI());
    
		setContentAreaFilled(false);
    
		setFocusable(false);
		setBorder(BorderFactory.createEtchedBorder());
		setBorderPainted(false);
    
		addMouseListener(this);
		setRolloverEnabled(true);
	}
  
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g.create();
		if (getModel().isPressed()) 
		{
			g2.translate(1, 1);
		}
    
		g2.setStroke(new BasicStroke(2.0F));
		g2.setColor(new Color(50, 50, 50));
    
		if (getModel().isRollover()) 
		{
			g2.setColor(Color.RED);
		}
		int delta = 6;
		g2.drawLine(delta, delta, getWidth() - delta, getHeight() - delta);
		g2.drawLine(getWidth() - delta, delta, delta, getHeight() - delta);
		g2.dispose();
  }
  
  public void mouseClicked(MouseEvent arg0) {}
  
  public void mouseEntered(MouseEvent arg0) {}
  
  public void mouseExited(MouseEvent arg0) {}
  
  public void mousePressed(MouseEvent arg0)
  {
	  int i = ChronoFrame.tabs.indexOfTabComponent(this.comp);
	  if (i != -1) 
	  {
	      if (((ChronoPanel)ChronoFrame.chrono.get(i)).modifToSave)
	      {
	    	  int option = JOptionPane.showConfirmDialog(null, "Save chronogram before closing ?", "Closing file", 1, 3);
	    	  if ((option != 2) && (option != -1))
	    	  {
	    		  if (option == 0) 
	    		  {
	    			  Main.window.save(false, i);
	    		  }
	    		  close(i);
	    	  }
	      }
	      else
	      {
	    	  close(i);
	      }
	  }
  }
  
  public void close(int i)
  {
	  ChronoFrame.tabs.remove(i);
	  ChronoFrame.chrono.remove(i);
  }
  
  public void mouseReleased(MouseEvent arg0) {}
  
}
