

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.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.io.FileInputStream;
import java.io.IOException;
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.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

public class split01 extends JFrame 
{

  public static Connection connection;
  private JTabbedPane tabbedPane =new JTabbedPane();
      
  public static JTextArea myArea=new JTextArea(1,1);
	  
  private JScrollPane myControlArea=new JScrollPane(myArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
	            ,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	
	    
  StatusPanel stPanel = new StatusPanel();
  
  //private JPanel qqq= new JPanel();  
	  
  private draw qqq= new draw();  
	  
  private JSplitPane mySplitePane;
  private JPanel[] myTables=new JPanel[5];
  private JScrollPane[] mySP=new JScrollPane[5];
  private myToolbar myToolbar ;
  /////////////// private myJMenu myJM=new myJMenu();
  public static String CompName="Casper";
  public static String Port="6666";
	
  ///     private myStart start=new myStart();
	
  // 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   apropos;
  
  //private  JPanel topPanel = new JPanel();         
  
	    
   public split01() 
    {
         
      setTitle("GUI ");
      setSize(800,700);
      setLocation(200,0);
      
     // topPanel = new JPanel();         
     // topPanel.setBackground(Color.WHITE);
      
      Container cp = getContentPane();
      
      cp.add(stPanel,BorderLayout.SOUTH);
      
      MyMousePositionsListener mPosAdpt =  new MyMousePositionsListener(stPanel);
      qqq.addMouseMotionListener(mPosAdpt); // qqq
    		  
    		  MyMouseListener MAdpt = new MyMouseListener(stPanel);
    		  qqq.addMouseListener(MAdpt);  /// qqq
       
     // myTables[0]=new VisitorTable(connection);
     // myTables[1]=new BookTable( connection);
     // myTables[2]=new AuthorTable(connection);
     // myTables[3]=new PublisherTable(connection);
     // myTables[4]=new BookStoreTable(connection);
       
      mySP[0]=new JScrollPane(myTables[0],JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        
      mySP[1]=new JScrollPane(myTables[1],JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      mySP[2]=new JScrollPane(myTables[2],JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      mySP[3]=new JScrollPane(myTables[3],JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      mySP[4]=new JScrollPane(myTables[4],JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
       
      myToolbar =new myToolbar(tabbedPane,this,connection);
       
      tabbedPane.addTab("Vistor", mySP[0]);
        
      tabbedPane.addTab("Book", mySP[1]);
      tabbedPane.addTab("Author", mySP[2]);
      tabbedPane.addTab("Publisher", mySP[3]);
      tabbedPane.addTab("Book Store", mySP[4]);
       
       tabbedPane.addChangeListener(new ChangeListener() {
           public void stateChanged(ChangeEvent e) {
                switch(tabbedPane.getSelectedIndex())
                  {
                     // case 0:((VisitorTable)(myTables[0])).ChangeModel() ;break;      
                      //case 1:((BookTable)(myTables[1])).ChangeModel();break;
                      //case 2:((AuthorTable)(myTables[2])).ChangeModel();break;
                      //case 3:((PublisherTable)(myTables[3])).ChangeModel();break;
                      //case 4:((BookStoreTable)(myTables[4])).ChangeModel();break;
                       
                      //default :break;
                  }
                
           }
       });
       
       
       
      //mySplitePane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,tabbedPane,myControlArea);
       
       myArea.setEditable(false); 
       
      mySplitePane=new JSplitPane(JSplitPane.VERTICAL_SPLIT, qqq ,myControlArea);
      
      mySplitePane.setResizeWeight(0.85);
      mySplitePane.setEnabled( false );
      
      cp.add(myToolbar,BorderLayout.NORTH);
      cp.add(mySplitePane); 
        
     /// qqq.setBackground(Color.BLUE);
         
      addWindowListener(new WindowListener() {
          public void windowActivated(WindowEvent e) {
          }
          public void windowClosed(WindowEvent e) {
          }
          public void windowClosing(WindowEvent e) {
        	  split01.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) {
          }
      }); 
       
     
        //.dispose();
   /////////////     setJMenuBar(myJM);
      
      
      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("?");
      menuBar.add(menu2);

      apropos = new JMenuItem("A propos");
      //apropos.addActionListener(this);
      menu2.add(apropos);
      
      setJMenuBar(menuBar);
      
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      setSize( 1000, 800 );
      setVisible( true );
      
      
      
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
       
       
    }
      
  
    public static void main(String[] args)  
    {
        new split01();
    }
  
}


class topPanel extends JPanel
{
	
    public topPanel()
    {
    	split01.myArea.setText("[ INFO ]: Init GUI : OK" + "\n");
    	
    	DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    	Date date = new Date();
    	dateFormat.format(date);
   	
    	split01.myArea.append(dateFormat.format(date) + "\n");
    	
    	repaint();
    }
    
    @Override
	public void paint(Graphics g) 
	{
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(Color.RED);
		g2d.fillOval(0, 0, 30, 30);
		g2d.drawOval(0, 50, 30, 30);		
		g2d.fillRect(50, 0, 30, 30);
		g2d.drawRect(50, 50, 30, 30);

		g2d.draw(new Ellipse2D.Double(0, 100, 30, 30));
	
	}

}
/********************************************************/

class draw extends JPanel
{
	
    public draw()
    {
    	split01.myArea.setText("[ INFO ]: Init GUI : OK" + "\n");
    	
    	DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    	Date date = new Date();
    	dateFormat.format(date);
   	
    	split01.myArea.append(dateFormat.format(date) + "\n");
    	
    	setBackground(Color.YELLOW);
    	
    	repaint();
    	
    	
    	  
    	
    	
    }
	
	@Override
	public void paint(Graphics g) 
	{
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(Color.RED);
		g2d.fillOval(0, 0, 30, 30);
		g2d.drawOval(0, 50, 30, 30);		
		g2d.fillRect(50, 0, 30, 30);
		g2d.drawRect(50, 50, 30, 30);

		g2d.draw(new Ellipse2D.Double(0, 100, 30, 30));
	
	}
}

/********************************************************/
 
class myToolbar extends JToolBar
{
	
	//private JButton AddBut ;
	private JButton search_btn ;
	private JButton rectangle_btn ;
	private JButton text_btn ;
	private JButton line_btn ;
	private JButton delete_btn ;
	
	//private JButton BorrowBut ;
	//private JButton ReturnBut ;
	//private JButton ReportBut ;
	//private JButton MasterBut ;
	
	//private  Icon AddImag; 
	private  Icon search_img;
	private  Icon rectangle_img;
	private  Icon text_img;
	private  Icon line_img;
	private  Icon delete_img;
	
	
	//private  Icon BorrowImag;
	//private  Icon ReturnImag;
	//private  Icon ReportImag;
	//private  Icon MasterImag;
	
    public myToolbar(final JTabbedPane Tabbed,final JFrame myMother,final Connection conn)
    {

    	//  setLayout(null);
     	
    	//Toolkit tk = Toolkit.getDefaultToolkit();
    	
    	//AddImag=new ImageIcon("icons/Add.png");
        
        //URL imageURL=getClass().getClassLoader().getResource("images/" + iconName);
        
        //ImageIcon icon = new ImageIcon("book.jpg");
        
        //setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")))

    	
    	
        Image img = null;
        
        try 
        {
        	 img =	ImageIO.read(new FileInputStream("d:/search.png"));
		} 
        catch (IOException e1) 
        {
			e1.printStackTrace();
		}
        search_img=new ImageIcon(img);
        
     
        
        try 
        {
        	 img =	ImageIO.read(new FileInputStream("d:/rectangle.png"));
		} 
        catch (IOException e1) 
        {
			e1.printStackTrace();
		}
        rectangle_img =new ImageIcon(img);

        
        try 
        {
        	 img =	ImageIO.read(new FileInputStream("d:/text.png"));
		} 
        catch (IOException e1) 
        {
			e1.printStackTrace();
		}
        text_img =new ImageIcon(img);
        
        
        try 
        {
        	 img =	ImageIO.read(new FileInputStream("d:/arrow.png"));
		} 
        catch (IOException e1) 
        {
			e1.printStackTrace();
		}
        line_img =new ImageIcon(img);
        
        
      //  text_imag = new ImageIcon("icons/Remove.png");
      //  BorrowImag=new ImageIcon("icons/Borrow.png");
      //  ReturnImag=new ImageIcon("icons/Return.png");
      //  ReportImag=new ImageIcon("icons/Report.png");
      //  MasterImag=new ImageIcon("icons/Master.png"); 

        
        
        //AddBut=new JButton("Add", AddImag);
        //SearchBut.setPreferredSize(new Dimension(140, 40));
        
        search_btn = new JButton("Search",search_img);
        rectangle_btn =new JButton("Rectangle", rectangle_img);
        text_btn =new JButton("Text", text_img);
        line_btn =new JButton("Line", line_img);
/*        
        ModifyBut=new JButton("Rectangle",ModifyImag);
        RemoveBut=new JButton("Remove",RemoveImag);
        BorrowBut=new JButton("Borrow",BorrowImag);
        ReturnBut=new JButton("Return",ReturnImag);
        ReportBut=new JButton("Report",ReportImag);
        MasterBut=new JButton("Master",MasterImag);
        AddBut.setAlignmentY(CENTER_ALIGNMENT);
        search_btn.setAlignmentY(CENTER_ALIGNMENT);
        ModifyBut.setAlignmentY(CENTER_ALIGNMENT);
        RemoveBut.setAlignmentY(CENTER_ALIGNMENT);
        ReturnBut.setAlignmentY(CENTER_ALIGNMENT);
        ReportBut.setAlignmentY(CENTER_ALIGNMENT);
        MasterBut.setAlignmentY(CENTER_ALIGNMENT);
  */      
        
        //add(AddBut);
        add(search_btn);
        add(rectangle_btn);
        add(text_btn);
        add(line_btn);
        // add(RemoveBut);
       // add(BorrowBut);
       // add(ReturnBut);
       // add(ReportBut);
       // add(MasterBut);
       

        search_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                switch(Tabbed.getSelectedIndex())
                {
                    //case 0:(new VisitorSearch(conn,myMother)).show();break;      
                    //case 1:(new BookSearch(conn,myMother)).show();break;
                    //case 2:(new AuthorSearch(conn,myMother)).show();break;
                    //case 3:(new PublisherSearch(conn,myMother)).show();break;
                    //case 4:(new BookStoreSearch(conn,myMother)).show();break;
                     
                    default :break;
                }
            }
        });
        
        
         
 
    }
     
     
 
  
}

class myStart extends JDialog
{
    myStart()
    {
         
        setUndecorated(true);
      //  setIgnoreRepaint(true);
        setResizable(false);
        setSize(150,150);
        setLocation(400,300);
         
    }
      
    private Image Imgstart=new ImageIcon("icons/back.png").getImage();
    public void paint(Graphics g) 
    {
        g.drawImage(Imgstart,0,0,this);
    }
}

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

class StatusPanel extends JPanel 
{ 

	private JLabel posText = new JLabel("position:"); 
	private JLabel XCoord = new JLabel("0", JLabel.RIGHT); 
	private JLabel YCoord = new JLabel("0", JLabel.RIGHT); 
	private JLabel countText = new JLabel("no. of clicks"); 
	private JLabel counts = new JLabel("0", JLabel.RIGHT); 
	private JLabel dummy1 = new JLabel(); 
	private JLabel inOutText = new JLabel("mouse is in comp."); 
	private JLabel inOut = new JLabel("no", JLabel.RIGHT); 
	private JLabel dummy2 = new JLabel(); 
	private int clickCount = 0;


	public StatusPanel() 
	{
		this.setLayout(new GridLayout(3,3)); 
		this.add(posText); 
		this.add(XCoord); 
		this.add(YCoord); 
		this.add(countText); 
		this.add(counts); 
		this.add(dummy1); 
		this.add(inOutText); 
		this.add(inOut); 
		this.add(dummy2); 
	} 
		
	public void setCoordinates(int x, int y) 
	{ 
		XCoord.setText(Integer.toString(x)); 
		YCoord.setText(Integer.toString(y)); 
	} 

	public void setInOut(String str) 
	{ 
		inOut.setText(str); 
	} 

	public void incrementClickCount()
	{
		clickCount++;
		counts.setText(Integer.toString(clickCount));
	}

}

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

class MyMousePositionsListener implements MouseMotionListener 
{ 
	private StatusPanel statusPane; 

	public MyMousePositionsListener(StatusPanel s) 
	{ 
		statusPane = s;
	} 

	public void mouseMoved(MouseEvent evt) 
	{ 
		statusPane.setCoordinates(evt.getX(),evt.getY()); 
	} 

	public void mouseDragged(MouseEvent evt) 
	{ 
	} 
}

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

class MyMouseListener implements MouseListener
{
	private StatusPanel statusPane;

	public MyMouseListener(StatusPanel s)
	{
		statusPane = s;
	}

	public void mouseEntered(MouseEvent e)
	{
		statusPane.setInOut("yes");
	}

	public void mouseExited(MouseEvent e)
	{
		statusPane.setInOut("no");
		statusPane.setCoordinates(-1,-1);
	}

	public void mouseClicked(MouseEvent e)
	{
		statusPane.incrementClickCount();
	}

	public void mousePressed(MouseEvent e)
	{
	}

	public void mouseReleased(MouseEvent e)
	{
	}

 }

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