

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;

  
  
 // https://www.codemiles.com/Tagging.php?mode=search&tag=%22Java+Swing%22

// probléme focus avec keylistener ????
// https://forum.hardware.fr/hfr/Programmation/Java/probleme-avec-addkeylistener-sujet_69352_1.htm

// http://munier.perso.univ-pau.fr/temp/I6/GeneralitesGUI1.pdf  pour faire un GUI

public class Frame extends JFrame implements ActionListener ,KeyListener
{
	
  // declaration du menu
  private JMenuBar    menuBar = new JMenuBar(); 
  private JMenuItem   nouveau;
  private JMenuItem   ouvrir;
  private JMenuItem   sauver;
  private JMenuItem   exporter;
  private JMenuItem   imprimer;
  private JMenuItem   quitter;
  private JMenuItem   export_JPEG;
  private JMenuItem   export_PNG;
  private JMenuItem   export_CLIPBOARD;
  
  private JMenuItem   undo;
  private JMenuItem   redo;
  
  
  private JMenuItem   apropos;
  
  public static JTextArea myArea=new JTextArea(1,1);
  
  private JScrollPane myControlArea=new JScrollPane(myArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  ,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	  
  private JSplitPane mySplitePane;
  private static Toolbar myToolbar  = new Toolbar();
  
  private static TopPanel09 panel = new TopPanel09();  

  private static StatusPanel  stPanel = new StatusPanel();
  
  static JFrame jframe;

  private static ClasseTransfert transfert = new ClasseTransfert();
  
  public Frame() 
  {
	  super();
         
	  setTitle("Debug Frame ---- FSM GUI by Patoche  2018  \u00a9"); // AVEC LE COPYRIGHT
      //setSize(800,700);
      //setLocation(200,0);
      //
      //Container cp = getContentPane();
      
      //cp.add(stPanel,BorderLayout.SOUTH);
      
       
      //myArea.setEditable(false); 
       
      //mySplitePane=new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel ,myControlArea);
      
      //mySplitePane.setResizeWeight(0.85);
      //mySplitePane.setEnabled( false );
      
      //cp.add(myToolbar,BorderLayout.NORTH);
      //cp.add(mySplitePane); 
      
	  JTextArea console = new JTextArea();
  	  for (int i=0; i<3; i++)
  			 console.append("Hello, World!\n");
  	 	
  	  JSplitPane splitPane =	 new JSplitPane(JSplitPane.VERTICAL_SPLIT , new JScrollPane(new TopPanel09()) 	,  new JScrollPane(console));  
      
  	 splitPane.setResizeWeight(0.90);
  	 splitPane.setEnabled( false );
   	   
   	  add(splitPane);
   	    
      setPreferredSize(new Dimension(800, 600));
      pack();
      
      
      
      setVisible(true);
      requestFocusInWindow();
      addKeyListener(this);
      
      ImageIcon icon = null;
      
      icon = createImageIcon("resources/diagram.png", "");
      
      this.setIconImage(icon.getImage());
      
      //toFront();
      //requestFocus();
      //setAlwaysOnTop(true); 
        
      /*   
      addWindowListener(new WindowListener() {
          public void windowActivated(WindowEvent e) {
          }
          public void windowClosed(WindowEvent e) {
          }
          public void windowClosing(WindowEvent e) {
        	  split02.myArea.append("Connection Closed !!!\n")     ;
                
          }
          public void windowDeactivated(WindowEvent e) {
          }
          public void windowDeiconified(WindowEvent e) {
          }
          public void windowIconified(WindowEvent e) {
          }
          public void windowOpened(WindowEvent e) {
          }
      }); */
      
      
      /******/
      JMenuBar menuBar = new JMenuBar();

      JMenu menu = new JMenu("File");
      menuBar.add(menu);

      nouveau = new JMenuItem("Nouveau");
      nouveau.addActionListener(this);
      menu.add(nouveau);
              
      ouvrir = new JMenuItem("Ouvrir");
      ouvrir.addActionListener(this);
      menu.add(ouvrir);
              
      sauver = new JMenuItem("Sauver");
      sauver.addActionListener(this);
      menu.add(sauver);
      
      JMenu submenu = new JMenu("Exporter");
      menu.add(submenu);
              
      export_JPEG =  new JMenuItem("JPEG");
      export_JPEG.addActionListener(this);
      submenu.add(export_JPEG);
              
             
      export_PNG =  new JMenuItem("PNG");
      export_PNG.addActionListener(this);
      submenu.add(export_PNG);
      
      export_CLIPBOARD =  new JMenuItem("CLIPBOARD");
      export_CLIPBOARD.addActionListener(this);
      submenu.add(export_CLIPBOARD);
              
      imprimer = new JMenuItem("Imprimer");
      imprimer.addActionListener(this);
      menu.add(imprimer);

      menu.addSeparator();
               
      quitter = new JMenuItem("Quitter");
      quitter.addActionListener(this);
      menu.add(quitter);
              
      /******************************/
      
      JMenu menu2 = new JMenu("Edit");
      menuBar.add(menu2);

      undo = new JMenuItem("Undo");
      undo.addActionListener(this);
      menu2.add(undo);

      redo = new JMenuItem("Redo");
      redo.addActionListener(this);
      menu2.add(redo);
      
      /******************************/
      
      JMenu menu3 = new JMenu("?");
      menuBar.add(menu3);

      apropos = new JMenuItem("A propos");
      apropos.addActionListener(this);
      menu3.add(apropos);
      
      /******************************/
      
      setJMenuBar(menuBar);
      
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
       
    }
      
  	public ImageIcon createImageIcon(String path,  String description) 
  	{
  		URL imgURL = getClass().getResource(path);

  		if (imgURL != null)
  		{
  			return new ImageIcon(imgURL, description);
  		} 
  		else 
  		{
  			System.err.println("Couldn't find file: " + path);
  			return null;
  		}
  	}
  
   public static void mouse_compter_click()
   {
	   stPanel.incrementClickCount();
   }
   
   public static void mouse_position(int x, int y)
   {
	   stPanel.setCoordinates(x, y);
   }
   
   public static void mouse_exit_panel()
   {
	   stPanel.setInOut("no");
	   stPanel.setCoordinates(-1,-1);
   }
   
   public static void mouse_inside()
   {
		stPanel.setInOut("yes");
   }

   public static String return_action_selected()
   {
	   return(myToolbar.get_action_selected() );
	
   }

   public static void stop_action_selected()
   {
	   myToolbar.set_action_selected("");
	
   }
   
   public static void transfert_set(String val)
   {
	   
	   transfert.setData(val);
   }
   
   public static String transfert_get()
   {
	   
	   return( transfert.getData() );
   }
   
   
   
   
   
   public static String gloup()
   {
	   
	   return( transfert.get_Btn_clicked() );
	   
   }
   
   public void frame_focus()
   {
	   
	  // jframe.setFocusable(true);
	   requestFocus();
	   setVisible(true);
	      requestFocusInWindow();
	   
   }
   
/**********************************************************/

   @Override
   public void actionPerformed(ActionEvent e) 
   {
	   if(e.getSource()== nouveau)
       {
           System.out.println("nouveau");
           panel.effacer_tout();
       }
       
       if(e.getSource()== ouvrir)
       {
           panel.ouvrir_fichier();
       }
       
       if(e.getSource()== sauver)
       {
          panel.sauver_fichier();
       }
       
       if(e.getSource()== export_JPEG)
       {
          panel.export_image("jpeg");
       }
       
       if(e.getSource()== export_PNG)
       {
          panel.export_image("png");
       }
       
       if(e.getSource()== export_CLIPBOARD)
       {
          panel.clipboard();
       }
       
       if(e.getSource()== imprimer)
       {
          panel.imprimer_fichier();
       }
       
       if(e.getSource()== apropos)
       {
           AboutDialog dialog_box = new AboutDialog(this);
           dialog_box.setVisible(true);
       }
       
       if(e.getSource()==quitter)
       {
           // System.out.println("blabla");
           // System.exit(0);
           quitter_appli();
       }
   }

 /**********************************************************/
   
   @Override
   public void keyPressed(KeyEvent e) 
   {
   }

   @Override
   public void keyTyped(KeyEvent e) 
   {
   }
   
   @Override
   public void keyReleased(KeyEvent e) 
   {

	   // supprimer
	   if (e.getKeyCode() == KeyEvent.VK_DELETE) 
	   {
		   System.out.println("Delete key Released");
		   panel.effacer_selection();
	   }
    
	   // query
	   if (e.getKeyCode() == KeyEvent.VK_Q) 
	   {
		   //panel.edition_node();
		   System.out.println("Query key");
		   panel.query();
	   }

	   if (e.getKeyCode() == KeyEvent.VK_U) 
	   {
		   // seulement si un seul node est en mode selection
		   System.out.println("Unselect all");
		   panel.unselect_all();
	   }

	   if (e.getKeyCode() == KeyEvent.VK_T) 
	   {
		   System.out.println("Add text");
	   }
     
	   if (e.getKeyCode() == KeyEvent.VK_M) 
	   {
		   panel.showPopup();
	   }
   }
 
   /**********************************************************/
   
   public void quitter_appli()
   {
        int confirmed = JOptionPane.showConfirmDialog(null, "Exit Program?","EXIT confirmation",JOptionPane.YES_NO_OPTION);
   
       if(confirmed == JOptionPane.YES_OPTION)
       {
           dispose();
       }
        else 
       {
           setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
       }
   }

}

/********************************************************/
