
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 Graph_line 
{
 
	private String					label;
	private ArrayList<LineObj>		lines;	
	
	public Graph_line() 
    { 
    	this("", new ArrayList<LineObj>()); 
  	}

    public Graph_line(String aLabel) 
    { 
    	this(aLabel, new ArrayList<LineObj>()); 
   	}

    public Graph_line(String aLabel, ArrayList<LineObj> initialLines) 
    {
        label = aLabel;
        lines = initialLines;
    }
    
    public void addLines(LineObj aLine) 
    {
    	lines.add(aLine);
    }
    
    public LineObj LineAt(double x, double y) 
    {
   	
    	int k = 30;
    	
        for (int i = 0;  i< lines.size() ; i++)
        {
            LineObj line = lines.get(i); 
         
            Rectangle net = new Rectangle(k,k);
    		
  		  	Line2D.Double aqw = new Line2D.Double(line.getX1() , line.getY1() , line.getX2() , line.getY2());
  		  
  		  	net.setFrameFromCenter(x, y, x+(k/2), y+(k/2) );
            if(aqw.intersects(net)) 
            {
            	return line;
            }
            
        }
        
        return null;
    }
  
    public ArrayList<LineObj> selectedLines() 
    {
        ArrayList<LineObj>   selected_lines = new ArrayList<LineObj>();
        for (LineObj item:  lines)
            if (item.isSelected()) selected_lines.add(item);
        return selected_lines;
    }
   
    public void effacer_tout() 
    {
    	lines.clear();
    	
    }
    
    public void deleteLine(LineObj l) 
    {
    	lines.remove(l); 
    }

    public ArrayList<LineObj> getLines() 
    { 
    	return lines; 
   	}
    
    /**********************************************************/
    public void saveTo(PrintWriter aFile) 
    {
        aFile.println(label);
        
        aFile.println(lines.size());
        for (LineObj item:  lines)
            item.saveTo(aFile);
    }
    
    public static Graph_line loadFrom(BufferedReader aFile) throws IOException 
    {
       
        Graph_line    aGraph = new Graph_line(aFile.readLine());

        int numLines = Integer.parseInt(aFile.readLine());
        for (int i=0; i<numLines; i++) 
        {
            LineObj tempLine = LineObj.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.addLines(tempLine); //.addEdge(start, end);
        }
        
        return aGraph;
    }
    
}

