import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
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.AdjustmentEvent;
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.SwingUtilities;
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 split02 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   preferences;
  private JMenuItem   test_menu;
  
  
  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 TopPanel15 panel = new TopPanel15();  
  
  private JScrollPane area=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  ,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	

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

  private static ClasseTransfert transfert = new ClasseTransfert();
  
  private  JFrame frame;
  
  public split02() 
  {
	  super();
         
	  //https://www.developpez.net/forums/d500366/java/interfaces-graphiques-java/awt-swing/probleme-jscrollpane-panel-paint/
	  
	  setTitle("FSM GUI by Patoche  2018  \u00a9"); // AVEC LE COPYRIGHT
      //setSize(800,700);
      setSize(1200,1000);

      setLocation(200,0);
      
      
      frame= new JFrame();	
      
      Container cp = getContentPane();
      
      cp.add(stPanel,BorderLayout.SOUTH);
      
      /////myToolbar =new Toolbar();
       
      myArea.setEditable(false); 
       
    //  area.setPreferredSize(new Dimension(1000, 1000));   
      
      mySplitePane=new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(panel) ,myControlArea);
      
     // area.getHorizontalScrollBar().addComponentListener(this);
      
      //panel.setPreferredSize(new Dimension(2400, 2000));
      
      
   //   Dimension listSize = new Dimension(2000, 2000);
  //	 area.setSize(listSize);
      
      mySplitePane.setResizeWeight(0.85);
      mySplitePane.setEnabled( false );
      
      cp.add(myToolbar,BorderLayout.NORTH);
      cp.add(mySplitePane); 
      
      
      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);
      
      preferences = new JMenuItem("Preferences");
      preferences.addActionListener(this);
      menu2.add(preferences);
      
      test_menu = new JMenuItem("Test"); 
      test_menu.addActionListener(this);
      menu2.add(test_menu);
      
      
      /******************************/
      
      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 );
    //  setSize( 1000, 800 );
      setVisible( true );
      
    
       
    }
      
  
  public void adjustmentValueChanged(AdjustmentEvent e) {
		repaint();
	
	}
  
  	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()==quitter)
       {
           // System.out.println("blabla");
           // System.exit(0);
           quitter_appli();
       }
       
	   ////////////

       if(e.getSource()== preferences)
       {
           //AboutDialog dialog_box = new AboutDialog(this);
           //dialog_box.setVisible(true);
    	   System.out.println("display preferences");
    	   
    		//new Dialog_preferences(this);
    		
    		
//    		Dialog_preferences dlg = new Dialog_preferences(this);

    	   /*
    	   Dialog_preferences dlg = new Dialog_preferences();
    		
    		
             String[] results = dlg.run();
             
             if (results[0] != null) 
             {
            	 System.out.println("Valeur: " + results[0] + "  " + results[1]);
             }
    	   
    	  // panel.set_preferences();
 */            
             
         	MonDialogue md1 = new MonDialogue(this,   "Préférences",        true);

         	md1.pack();

         	int i = 5;
         	String nom = "patoche";
         	md1.initialise(new Object[]{i, nom});
         	md1.show();
         	Object [] retour = md1.getDonnees();
         	i = (Integer)retour[0];
         	nom = (String)retour[1];
         	System.out.println("Valeur de retour de la Jdialog preferences: " + i + "  " + nom);
             
       }
       
       if(e.getSource() == test_menu)
       {
/*    	     
    	    MyDialog dialog = new MyDialog(this, true);
    	    dialog.setLocationRelativeTo(this);
    	    dialog.setModal(true);
    	    dialog.setVisible(true); 
*/    	   
    	   
    	   //JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
    	   
    	   //Component component = this;
    	   
    	   //JFrame f1 = (JFrame) SwingUtilities.windowForComponent(component);
    	   
    	    //Dialog01 dialog = new Dialog01(frame, true);
    	    //Jtable01 dialog = new Jtable01(frame, true);
    	    //jdialog011 dialog = new jdialog011(frame, true);
   	    	//jtabpane03 dialog = new jtabpane03(frame, true);
   	    	jfileviewer02 dialog = new jfileviewer02(frame, true);
   	    	
    	   // test dialog = new test(frame, true);

    	    dialog.setLocationRelativeTo(this);
         	dialog.setModal(true);
         	dialog.setVisible(true);
    	     
 /*   	   
    	   java.awt.Container c = this.getParent();  
	    	 while (!(c instanceof javax.swing.JFrame) && (c!=null)) 
	    	 {  
	    	         c = c.getParent();  
	    	 }  
	    	 if (c!=null) 
	    	 { 
	    		System.out.println("Test menu");
	    	    JFrame frame_parent =(javax.swing.JFrame) c;  
    	   
    	   MyDialog dialog = new MyDialog(this);
    	   dialog.setModal(true);
    	   dialog.setVisible(true); 
	    	 }
	    	 
*/	    	 
    	   //DialogExample ido = new DialogExample();  
    	   /*
    	   java.awt.Container c = this.getParent();  
	    	 while (!(c instanceof javax.swing.JFrame) && (c!=null)) 
	    	 {  
	    	         c = c.getParent();  
	    	 }  
	    	 if (c!=null) 
	    	 { 
	    		System.out.println("Test menu");
	    	    JFrame frame_parent =(javax.swing.JFrame) c;  
	    	 	Test_dialog dialog_windows = new Test_dialog(frame_parent, true ) ; 
	    	 	dialog_windows.setVisible(true);
	    	 	
	    	 }*/
       }	   
       
       
	   ////////////
       if(e.getSource()== apropos)
       {
           AboutDialog dialog_box = new AboutDialog(this);
           dialog_box.setVisible(true);
       }
       
      
   }

 /**********************************************************/
   
   public  Frame getFrame() 
   { // récupération de la frame de l'applet

        Component component=this;

        do{

           component=component.getParent();

        } while(!(component instanceof Frame));

        return (Frame)component;

     }

   
   @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();
	   }
	   
	   if(e.getKeyCode() == KeyEvent. VK_ESCAPE)
	   {
		   System.out.println("Escape key");
	   }
	   
	   if(e.getKeyCode() == KeyEvent.VK_SPACE)
	   {
		   System.out.println("Space key");
	   }
   }
 
   /**********************************************************/
   
   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);
       }
   }

}

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

	class dia  extends JDialog {

		public dia() {
			// MyDialog
			setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			setTitle("About Us");
			setBounds(100, 100, 359, 174);
			getContentPane().setLayout(null);
			
			// Label
			JLabel label = new JLabel("ThaiCreate.Com Version 1.0");
			label.setBounds(86, 37, 175, 29);
			getContentPane().add(label);	
			
		}
	}
