
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JMenu;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.event.MouseAdapter;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.CubicCurve2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class Graph02 
{
    private String            label;
    private ArrayList<Text>	  texts;	
    
    public Graph02() 
    { 
    	this("", new ArrayList<Text>()); 
  	}

    public Graph02(String aLabel) 
    { 
    	this(aLabel, new ArrayList<Text>()); 
   	}

    public Graph02(String aLabel, ArrayList<Text> initialNodes) 
    {
        label = aLabel;
        texts = initialNodes;
    }
    
    
    public ArrayList<Text> getTexts() 
    { 
    	return texts; 
   	}

    public void addText(Text aText) 
    {
        texts.add(aText);
    }
   
    public Text textAt(double x, double y) 
    {
    	
        for (int i = 0;  i< texts.size() ; i++)
        {
            Text item = texts.get(i);
         
            int xmin = item.getX();
   		 	int ymin = item.getY();  ;
   		 	int xmax = xmin + item.getWidth();
   		 	int ymax = ymin + item.getHeight();
   		 
   		 	if(x >= xmin && x <= xmax && y >= ymin && y <= ymax  )
   		 	{
   		 		System.out.println("Text pressed.. "); 
   		 		return item;
   		 	}
            
        }
        
        return null;
    }
    
    public ArrayList<Text> selectedTexts() 
    {
        ArrayList<Text>   selected_text = new ArrayList<Text>();
        for (Text item:  texts)
            if (item.isSelected()) selected_text.add(item);
        return selected_text;
    }
    

    public void deleteText(Text aText) 
    {
    	texts.remove(aText); 
    }

    
    public void draw(Graphics g) 
    {
      
    }
    
    
    public void effacer_tout() 
    {
        texts.clear();
    }
    
    public void saveTo(PrintWriter aFile) 
    {
        aFile.println(label);

        // Output the text
        aFile.println(texts.size());
        for (Text item:  texts)
            item.saveTo(aFile);
    }
    
    public static Graph02 loadFrom(BufferedReader aFile) throws IOException 
    {
        Graph02    aGraph = new Graph02(aFile.readLine());

        int numText = Integer.parseInt(aFile.readLine());
        for (int i=0; i<numText; i++)
            aGraph.addText(Text.loadFrom(aFile));

       
        
        return aGraph;
    }
    
    
    public Rectangle2D getBounds(Graphics2D g2, String text)
    {
    	double width = 0;
    	double height = 0;
    	int GAP = 3;
       
    	Font oldFont = g2.getFont();
    	Font font = oldFont;
       
    	g2.setFont(font);
    	FontRenderContext frc = g2.getFontRenderContext();
    	StringTokenizer tokenizer = new StringTokenizer(text, "\n");
       
    	int nlines =  text.split("[\n|\r]").length;
       
    	String[] ligne = text.split("[\\n|\\r]");
       
    	for(int i =0; i < nlines; i++)
    	{
       	 	Rectangle2D b = font.getStringBounds(ligne[i], frc);
            width = Math.max(width, b.getWidth() + 2 * GAP);
            height += b.getHeight();
    	}
       
    	
    	 Font aaafont = new Font("Serif", Font.ITALIC, 14);
    	FontMetrics metrics  = new FontMetrics(aaafont) {};
    	
    	int testHeight = metrics.getHeight();
    	
    	//int testHeight = getFontMetrics(font).getHeight();
    	height = testHeight + height;
       
    	//System.out.println(" Bounds : " + width + " " + height + " "  + testHeight + " - " + nlines); 
       
    	return new Rectangle2D.Double(0, 0, width, height);
    }
    
    private Rectangle getStringBounds(Graphics g, String str, int x, int y)
    {
   	 	FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
   	 	GlyphVector gv = g.getFont().createGlyphVector(frc, str);
   	 	return gv.getPixelBounds(null, x, y);
    }

	public void drawString(Graphics g, String text, int x, int y) 
	{
        for (String line : text.split("\n"))
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
    }
 
}

