
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 Graph03 
{
 
	private String    			        label;
	private ArrayList<RectangleObj>		rects;	
	    
    public Graph03() 
    { 
    	this("", new ArrayList<RectangleObj>()); 
  	}

    public Graph03(String aLabel) 
    { 
    	this(aLabel, new ArrayList<RectangleObj>()); 
   	}

    public Graph03(String aLabel, ArrayList<RectangleObj> initialNodes) 
    {
        label = aLabel;
        rects = initialNodes;
    }
    
    public void addRect(RectangleObj aRect) 
    {
    	rects.add(aRect);
    }
    
    public RectangleObj rectangleAt(double x, double y) 
    {
   	
        for (int i = 0;  i< rects.size() ; i++)
        {
            RectangleObj item = rects.get(i); 
            int xmin = item.getX1() - 20;
   		 	int ymin = item.getY1() - 20;
   		 	int xmax = xmin + item.getWidth() + 20;
   		 	int ymax = ymin + item.getHeight() +20;
   		 
   		 	if(x >= xmin && x <= xmax && y >= ymin && y <= ymax  )
   		 	{
   		 		System.out.println("Rectangle pressed.. "); 
   		 		return item;
   		 	}
        }
        
        return null;
    }
    
    public ArrayList<RectangleObj> selectedRects() 
    {
        ArrayList<RectangleObj>   selected_rects = new ArrayList<RectangleObj>();
        for (RectangleObj item:  rects)
            if (item.isSelected()) selected_rects.add(item);
        return selected_rects;
    }
   
    public void effacer_tout() 
    {
        rects.clear();
    }
    
    public void deleteRect(RectangleObj r) 
    {
    	rects.remove(r); 
    }

    public ArrayList<RectangleObj> getRects() 
    { 
    	return rects; 
   	}
    
    public void saveTo(PrintWriter aFile) 
    {
        aFile.println(label);

        // Output the rectangle
        aFile.println(rects.size());
        for (RectangleObj item:  rects)
            item.saveTo(aFile);
    }
    
    public static Graph03 loadFrom(BufferedReader aFile) throws IOException 
    {
       
        Graph03    aGraph = new Graph03(aFile.readLine());

        int numRects = Integer.parseInt(aFile.readLine());
        for (int i=0; i<numRects; i++) 
        {
            RectangleObj tempRect = RectangleObj.loadFrom(aFile);
          //  Node start = aGraph.nodeAt(tempEdge.getStartNode().getLocation().getX(),  tempEdge.getStartNode().getLocation().getY());
          //  Node end = aGraph.nodeAt(tempEdge.getEndNode().getLocation().getX(),  tempEdge.getEndNode().getLocation().getY());
            aGraph.addRect(tempRect); //.addEdge(start, end);
        }
        
        return aGraph;
    }
    
}

