import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
import java.awt.*;

import java.awt.*;
import java.beans.DefaultPersistenceDelegate;
import java.beans.Encoder;
import java.beans.Statement;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

// MVC   modelisation  visualization  command

public class jdialog02 extends JDialog 
{
	private static JTable table;
	private JTree tree;
///	private static DefaultTableModel xxxmodel;
	private boolean close_Jdialog = false;
	private TreeModel model;
	private List<TreeNode[]> expanded;
	private JButton btn_load_tree;
	private JButton btn_save_tree;
	private JButton btn_load_table;
	private JButton btn_save_table;
	
	private JButton btn_analyse;
	
	
	private File file = new File("TableModel.xml");	
	private DefaultTableModel tableModel; 
	private JFileChooser myJFileChooser = new JFileChooser(new File(".")); 
	
//	private Class_plan intermediaire;   
	
	private ArrayList<String> tree_node = new ArrayList<String>();
	
	private Class_plan intermediaire = new Class_plan();
	
	private WriteXML WRITE_XML; 
	private ReadXML READ_XML; 
	
	
	public jdialog02(JFrame f, boolean modal) 
	{
       super(f, modal);
       

       WRITE_XML = new WriteXML();
       READ_XML = new ReadXML();
       
       
       // fermer la fenetre
       addWindowListener(new java.awt.event.WindowAdapter() 
       {
           public void windowClosing(java.awt.event.WindowEvent e) 
           {
               serialize();
        	   dispose();
           }
       });
       
       expanded = new ArrayList<TreeNode[]>();
       
       
       
       List<String>[] arrayOfList = new List[100];
       List<String> l1 = new ArrayList<String>();

       
      
                 
      l1.add("noeud");
      arrayOfList[0] = l1;
      l1.clear();

      l1.add("deux");
      l1.add("carma");
      arrayOfList[0] = l1;
      l1.clear();
       
      String stringToSplit = "str1-str2-str3";
      String[] tempArray;

    
      String delimiter = "-";

      tempArray = stringToSplit.split(delimiter);
       
       
      int n = 3; 
      
      // Here aList is an ArrayList of ArrayLists 
      ArrayList<ArrayList<Integer> > aList =    new ArrayList<ArrayList<Integer> >(n); 

      // Create n lists one by one and append to the  
      // master list (ArrayList of ArrayList) 
      ArrayList<Integer> a1 = new ArrayList<Integer>(); 
      a1.add(1); 
      a1.add(2); 
      aList.add(a1); 
      
      a1.clear();
       
       
      a1.add(88); 
      a1.add(253); 
      aList.add(a1); 
       
      
    
   // Create an ArrayList of strings.
      ArrayList<String> list = new ArrayList<>();
      list.add("cat");
      list.add("box");
      list.add("elephant");

      // Use toArray to copy ArrayList to string array.
      String[] array = new String[list.size()];
      array = list.toArray(array);

      list.clear();
      
      list.add("gggg");
      list.add("nada");
      list.add("cvgxbx");
      
      array = list.toArray(array);
      
      
      // Loop over the string elements in the array.
      for (String item : array) {
          System.out.println(item);
      }
     
      
      List<String> myArray = new ArrayList<String>();
      myArray.add("first");
      myArray.add("second");
       
       
      String valeurs[] = new String[10];
      valeurs[0] = "ggg";
      valeurs[1] = "pipo";
      valeurs[2] = "kiki";
      valeurs[3] = "alpha";
      
      String store[][] = new String[10][3];
      
      store[0][0] = "a";
      store[0][1] = "b";
      store[0][2] = "c";
      
      store[1][0] = "x";
      store[1][1] = "y";
      store[1][2] = "z";
      
      
      ArrayList<String> singleAddress = new ArrayList<String>();
      
      ArrayList<String> TABLE = new ArrayList<>();
      
      List<List<String>> addresses = new ArrayList<List<String>>();
      
      List<String>[] v = new List[100];
      
      
      
      TABLE.add("gggg");
      
      addresses.add(TABLE);
      
      addresses.add(list);
      
     
      
      singleAddress.add("17 Fake Street");
      singleAddress.add("Phoney town");
      singleAddress.add("Makebelieveland");

     // ArrayList<String[]> addresses = new ArrayList<String[]>();
      
    
      
      addresses.add(singleAddress);
      
      singleAddress.clear();
      
      singleAddress.add("aaa");
      singleAddress.add("4");
      singleAddress.add("paris");
      
      addresses.add(singleAddress);
      
       init();
       
       List listA = new ArrayList();

       listA.add("element 1");
       listA.add("element 2");
       listA.add("element 3");
       
     //  ArrayList<listA> xxx =new ArrayList<listA>();
       
       
       String stop = "0";
       
   /*    
       List<String> l1 = new ArrayList<String>();
		l1.add("1");
		l1.add("2");

		List<String> l2 = new ArrayList<String>();
		l2.add("3");
		l2.add("4");
		l2.add("5");

		List<String>[] arrayOfList = new List[100];
		arrayOfList[0] = l1;
		arrayOfList[1] = l2;

		
		
		
		
		
		  String[] arr = new String[l1.size()+ l2.size()]; 
		  
	        // ArrayList to Array Conversion 
	        for (int i =0; i < l1.size(); i++) 
	            arr[i] = l1.get(i); 
		
	        for (int i =0; i < l2.size(); i++) 
	            arr[i] = l2.get(i); 
		
		//ArrayList<l1> arrayOfList = new ArrayList<l1>();
		//arrayOfList.addAll(l1); 
		//arrayOfList.addAll(l2);
		
		/*
		for (int i = 0; i < arrayOfList.length; i++) {
			List<String> l = arrayOfList[i];
			System.out.println(l);
		}*/
       
     
	}
  
	private void init()
	{
	  	setResizable( false );
		setBounds(100, 100, 926, 550);
		setTitle("Test definition - debug ...");
		getContentPane().setLayout(null);
     	
		ImageIcon icon = null;
		icon = createImageIcon("resources/settings.png", "");
		setIconImage( icon.getImage() );
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(50, 39, 183, 194);
		getContentPane().add(scrollPane_2);
		
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Plan");
		
		
		
		tree = new JTree(root);
		//tree.setRowHeight(45);
		//tree = new JTree();
		
		tree.setComponentPopupMenu(new SelectionTreePopupMenu(intermediaire));
		
		scrollPane_2.setViewportView(tree);
		 
		tree.addMouseListener(new MouseAdapter() 
		{
		      public void mouseClicked(MouseEvent me) 
		      {
		    	  doMouseClicked(me);
		      }
	    });
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(294, 39, 532, 159);
		getContentPane().add(scrollPane);
		
		String[] columnNames =  {"Test", "Descrition" , "rien"};
	     
	    /*Object[][] data = {
							{null, null, null},
							{null, null, null},
							{null, null, null},
							{null, null, null},
							{null, null, null},
						  };
		*/
	    Object[][] data = {	{ "", "", ""}  };
	    
		tableModel = new DefaultTableModel(data, columnNames)
		{
	        @Override
	        public boolean isCellEditable(int row, int column)
	        {
	            // make read only fields except column 0,13,14
	            return column == 1 || column == 2 ;
	        }
	    };
		
		
		
	    table = new JTable(tableModel);
	    table.setRowHeight(30);
	    table.setFont(new Font("Serif", Font.BOLD, 20));

	    
	    table.addFocusListener(new FocusAdapter() 
	    {
	        @Override
	        public void focusLost(FocusEvent e) 
	        {
	            //if (e.getOppositeComponent().getParent() != table) 
	            //{
	               // loseCellFocus();
	            	System.out.println("Lost focus");
	            	
	            	
	            	String retour = table.getValueAt(0, 0).toString();
	            	
	            	//int row = table.getSelectedRow();
	            	String test_str = table.getModel().getValueAt(0, 0).toString();
	            	String descrition_str = table.getModel().getValueAt(0, 1).toString();
	            	String rien_str = table.getModel().getValueAt(0, 2).toString();
	            	
	            	System.out.println(test_str + " " + descrition_str + " " + rien_str );
	            	
	            	TestPlan obj = intermediaire.GetSequenceNamed(test_str);
	            	
	            	obj.setTextchamp1(descrition_str);
	            	obj.setTextchamp2(rien_str);
	            	
	           // }
	        }
	    });
	/*    
		JTable table = new JTable(); 
        Object[] columns = {"Id","First Name","Last Name","Age"};
        DefaultTableModel model = new DefaultTableModel();
        model.setColumnIdentifiers(columns);
        
        // set the model to the table
        table.setModel(model);
        
        table.setBackground(Color.LIGHT_GRAY);
        table.setForeground(Color.black);
        //Font font = new Font("",1,22);
        //table.setFont(font);
        //table.setRowHeight(30);
*/
	    
		scrollPane.setViewportView(table);

		/*******************/
		
		btn_load_tree  = new JButton("Load");
		btn_load_tree.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				System.out.println("Btn load  \n");
				deSerialize();
			}
		});
		btn_load_tree.setBounds(50, 257, 89, 23);
		getContentPane().add(btn_load_tree);
				
		btn_save_tree = new JButton("Save");
		btn_save_tree.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				System.out.println("Btn save  \n");
				serialize();
			}
		});
		btn_save_tree.setBounds(161, 257, 89, 23);
		getContentPane().add(btn_save_tree);
		
		/*******************/
		
		btn_load_table  = new JButton("Query");
		btn_load_table.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				System.out.println("Btn load table \n");
				
				TreeModel model = tree.getModel();
				
				String pipo = getTreeText(model, model.getRoot(), "");
				
				String[] parts = pipo.split(" ");
				
				System.out.println(getTreeText(model, model.getRoot(), ""));
				
				System.out.println("-------------------------------------");
				
				TreeNode root = (TreeNode) tree.getModel().getRoot();
			    visitAllNodes(root);
			    
			    System.out.println("-------------------------------------");
							    
			    traverse(tree);
				
			    System.out.println("-------------------------------------");
				
			    
			    explore_tree(model);
				
			    
			
			    
			    ArrayList<TestPlan>  objTree  = new ArrayList<TestPlan>();
			    objTree = intermediaire.getTreeChild();
                split02.myArea.append(" Nb tree : " + objTree.size() + "\n");

                for(int i = 0; i < objTree.size(); i++)
                {
                	
                	
                	String a = objTree.get(i).getType();
    				String val1 = objTree.get(i).getTextchamp1();
    	        	String val2 = objTree.get(i).getTextchamp2();
    	        	 
                	System.out.println(" TRACE: " + val1 + " " + val2);
                }

                
                
				//ArrayList<TestPlan>  list_tree_nodes  = new ArrayList<TestPlan>();
				//list_tree_nodes = intermediaire.getTreeElement();
                //split02.myArea.append(" Nb tree children  : " + list_tree_nodes.size() + "\n");
				
				////printDescendants(root);
				
                // https://www.javagists.com/java-tree-data-structure
                
				//loadTable(); 
			}
		});
		btn_load_table.setBounds(450, 257, 89, 23);
		getContentPane().add(btn_load_table);
				
		btn_save_table = new JButton("Save");
		btn_save_table.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				System.out.println("Btn save table  \n");
				//saveTable(file); 
			}
		});
		btn_save_table.setBounds(550, 257, 89, 23);
		getContentPane().add(btn_save_table);
		
		/*******************/
		
		
		btn_analyse  = new JButton(" ++++ ");
		btn_analyse.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				System.out.println("Btn analyse  \n");
				
				//TreeModel model = tree.getModel();
				//tableModel.setValueAt( "Zorro" , 0, 0);
				
				ArrayList<TestPlan>  list_tree_nodes  = new ArrayList<TestPlan>();
				list_tree_nodes = intermediaire.getTreeElement();
                split02.myArea.append(" Nb tree children  : " + list_tree_nodes.size() + "\n");

				
//				tableModel.addRow(new Object[]{null,null,null});
/*				
				TreeModel model = tree.getModel();
				System.out.println(getTreeText(model, model.getRoot(), ""));
				
				System.out.println("******************************");
				printDescendants(root);
				 
				 traverse(tree);
				 
				 if(tree_node.size() != 0)
				 {
					 for(int i = 0; i < tree_node.size() ; i++)
					 {
						 //if(tree_node.get(i).indexOf("--") != -1)
						 //{
							 tableModel.setValueAt( tree_node.get(i) , i, 0);
						 //}
					 }	 
				 }
				 else
				 {
			    	  String msg = String.format("ERROR: Node not find !.");
			          JOptionPane.showMessageDialog(tree, msg, "Message", JOptionPane.ERROR_MESSAGE);

				 }
*/					 
				// split02.myArea.append(" [ INFO ]: blabla " + "\n");
				
				//getModel()
				// https://imss-www.upmf-grenoble.fr/prevert/Prog/Java/swing/JTree.html
				// https://www.rgagnon.com/javadetails/java-0313.html    traverse 
				
			}
		});
		btn_analyse.setBounds(250, 257, 89, 23);
		getContentPane().add(btn_analyse);
		
		/*******************/
		
		JSeparator separator = new JSeparator();
		separator.setBounds(240, 302, 440, 2);
		getContentPane().add(separator);
	
		/*******************/
		
		JButton btn_ok = new JButton("OK");
		btn_ok.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				close_Jdialog = false;
				System.out.println("Btn Cancel  \n");
				dispose();
			}
		});
		btn_ok.setBounds(135, 391, 89, 23);
		getContentPane().add(btn_ok);
	
		/*******************/
		
		JButton btn_cancel = new JButton("Cancel");
		btn_cancel.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				close_Jdialog = false;
				System.out.println("Btn Cancel  \n");
				dispose();
			}
		});
		btn_cancel.setBounds(359, 391, 89, 23);
		getContentPane().add(btn_cancel);
		
		/*******************/
	}
	
	
	private  String getTreeText(TreeModel model, Object object, String indent) 
	{
	    String myRow = indent + object + "\n";
	    for (int i = 0; i < model.getChildCount(object); i++) {
	        myRow += getTreeText(model, model.getChild(object, i), indent + "  ");
	    }
	    return myRow;
	}
	
	private void saveStatus() 
	{
        //save the expanded Nodes:
        expanded = new ArrayList<TreeNode[]>();
        collectStatusInfo(model.getRoot());
	}

	private void restoreStatus() 
	{
        //expand the saved Nodes:
        for (int i = expanded.size() - 1; i > -1; i--) 
        {
        	TreeNode[] ar = expanded.get(i);
	        tree.expandPath(new TreePath(ar));
        }
	}

	private void collectStatusInfo(final Object o) 
	{
		int cc = model.getChildCount(o);
	    for (int i = 0; i < cc; i++) 
	    {
	    	DefaultMutableTreeNode child = (DefaultMutableTreeNode) model.getChild(o, i);
	        TreeNode[] ar = child.getPath();
	        if (!model.isLeaf(child) && tree.isExpanded(new TreePath(ar))) 
	        {
	        	expanded.add(ar);
	            collectStatusInfo(child);
	        }
        }
	}
	
	// https://wiki.byte-welt.net/wiki/JTree_Zustand_speichern
	public void serialize() 
	{
		try 
		{
			XMLEncoder o = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("tree.xml")));
	        o.writeObject(tree.getModel());
	        o.writeObject(expanded);
	        o.close();
        } 
		catch (IOException e) 
		{
			e.printStackTrace();
        }
	}

	@SuppressWarnings("unchecked")
	private void deSerialize() 
	{
        try 
        {
        	XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream("tree.xml")));
	        tree.setModel((TreeModel) d.readObject());
	        expanded = (List<TreeNode[]>) d.readObject();
	        d.close();
        } 
        catch (FileNotFoundException ex) 
        {
	    }
	}
    
	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;
		}
	}
	
	/******************************************/
	
	private void saveTable() 
	{
        if (myJFileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION ) 
        {
            saveTable(myJFileChooser.getSelectedFile());
        }
    }
     
    private void saveTable(File file) 
    {
        try 
        {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(tableModel.getDataVector());
            out.writeObject(getColumnNames());
            out.close();
        }
        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
    }
    
    private Vector<String> getColumnNames() 
    {
        Vector<String> columnNames = new Vector<String>();
        for (int i = 0; i < table.getColumnCount(); i++)
            columnNames.add(table.getColumnName(i) );
        return columnNames;
    }
    
    private void loadTable() 
	{
    	if (myJFileChooser.showOpenDialog(this) ==  JFileChooser.APPROVE_OPTION )
	            loadTable(myJFileChooser.getSelectedFile());
    }
	 
    private void loadTable(File file) 
	{
        try 
        {
        	ObjectInputStream in = new ObjectInputStream( new FileInputStream(file));
	        Vector rowData = (Vector)in.readObject();
	        Vector columnNames = (Vector)in.readObject();
	        tableModel.setDataVector(rowData, columnNames);
	        in.close();
        }
        catch (Exception ex) 
        {
	        ex.printStackTrace();
	    }
    }
	
	/******************************************/
	
    public  void printDescendants(TreeNode root) 
    {
        System.out.println(root);
        Enumeration children = root.children();
        if (children != null) 
        {
          while (children.hasMoreElements()) 
          {
            printDescendants((TreeNode) children.nextElement());
          }
        }
    }
    
    public static void visitAllNodes(TreeNode node) 
    {
        System.out.println(node);
        if (node.getChildCount() >= 0) {
          for (Enumeration e = node.children(); e.hasMoreElements();) {
            TreeNode n = (TreeNode) e.nextElement();
            visitAllNodes(n);
          }
        }
      }
    
    public void traverse(JTree tree) 
    { 
        TreeModel model = tree.getModel();
        if (model != null) 
        {
            Object root = model.getRoot();
            System.out.println(root.toString());
            node_hierarchie(model,root);    
        }
        else
        {	
           System.out.println("Tree is empty.");
        }   
    }
        
    public void node_hierarchie(TreeModel model, Object o)
    {
    	
    	tree_node.clear();
    	
        int  cc;
        cc = model.getChildCount(o);
        for( int i=0; i < cc; i++) 
        {
        	Object child = model.getChild(o, i );
        	if (model.isLeaf(child))
        	{	
        		System.out.println(child.toString());
        		tree_node.add(child.toString());
        	}
        	else 
        	{
        		System.out.print(child.toString()+"--");
        		tree_node.add(child.toString()+"--");
        		node_hierarchie(model,child); 
        	}	
           

            
        }
    } 
    
    public void explore_tree(TreeModel model)
    {
    	
    	
    	
    	 Object alpha = model.getRoot();
         String root_str = alpha.toString();
         System.out.println(root_str);
         
         int cpt = 0;
         
         //TreeModel model = tree.getModel();
         //Object alpha =  model.getRoot();
         
         //List<String>[] arrayOfList = new List[100];
         //List<String> l1 = new ArrayList<String>();

         //String valeurs[] = new String[10];
         
         String store[] = new String[10];
         
       
         
         
         for(int i = 0; i < ((DefaultMutableTreeNode) alpha).getChildCount() ; i++) 
         {
             DefaultMutableTreeNode child = (DefaultMutableTreeNode) ((DefaultMutableTreeNode) alpha).getChildAt(i);
             String noeud = child.getUserObject().toString();
             
             System.out.println(noeud);
             
         
             store[cpt] = noeud;
             cpt = cpt + 1; 
             
             for(int j = 0; j < child.getChildCount() ; j++) 
             {
                 DefaultMutableTreeNode aaachild = (DefaultMutableTreeNode) child.getChildAt(j);
                 String sub = aaachild.getUserObject().toString();
                 
                 TestPlan obj = intermediaire.GetSequenceNamed(sub);
 				 String val1 = obj.getTextchamp1();
 	        	 String val2 = obj.getTextchamp2();
 	        	 
 	        	 System.out.println(sub + "-" + val1 + "-" + val2);
 	        	 
 	        	 store[cpt] = sub + "-" + val1 + "-" + val2;
 	        	 cpt = cpt + 1; 
 	        	
             }
             
             
            
         }
         
         WRITE_XML.xxxWirteXML(store);
         
         WRITE_XML.aaaWirteXML();
         
         READ_XML.aaaReadXML();
         
        // aaa
        // un aze  qsd
        // bbb
        // deux pgh  145
        // trois tyg  bpio
         
         // Java   XML & JSON 
         
 /*        
         <Tree>
            <element category="node">
            	<name>blabla</name>
            	<val1>none</val1>
            	<val2>none</val2>
            </element>
            <element category="child">
               	<name>coco</name>
            	<val1>un</val1>
            	<val2>deux</val2>
            </element>
			<element category="child">
               	<name>trois</name>
            	<val1>123</val1>
            	<val2>aze</val2>
            </element>
            <element category="node">
            	<name>zarby</name>
            	<val1>none</val1>
            	<val2>none</val2>
            </element>
            <element category="child">
               	<name>alpha</name>
            	<val1>I2C</val1>
            	<val2>pipotron</val2>
            </element>
       	 </Tree>
*/         
    }
    
	public void doMouseClicked(MouseEvent me) 
	{
		TreePath TreePathObj = tree.getPathForLocation(me.getX(), me.getY());
		
		if(TreePathObj != null)
		{	
			Object pathString[] = TreePathObj.getPath();
		
			String value = "";
			String s = "";
			String str_concate = "";

			for (int i = 0; i < pathString.length ; i++) 
			{
				str_concate+=pathString[i] + ":";
			}	

			// nb d'ocuurence *
			int nb_occurence = str_concate.length() - str_concate.replace(":", "").length();
			
			System.out.println("----->  " + str_concate + " " + nb_occurence );   
			
			if(nb_occurence == 3)
			{
				// enlever le dernier caractere du path 
				String str_tmp = str_concate.substring(0, str_concate.length()-1); 
				// decoupe
				String[] parts = str_tmp.split(":");
				
				System.out.println(" Selection children " + parts[2] );   
				tableModel.setValueAt(parts[2], 0, 0);
				
				//System.out.println(" Selection children " + parts[1]);   
				TestPlan obj = intermediaire.GetSequenceNamed(parts[2]);
				String val1 = obj.getTextchamp1();
	        	String val2 = obj.getTextchamp2();
				tableModel.setValueAt(val1, 0, 1);
				tableModel.setValueAt(val2, 0, 2);
			}
			
			else
			{
				tableModel.setValueAt("", 0, 0);
				tableModel.setValueAt("", 0, 1);
				tableModel.setValueAt("", 0, 2);
			}
		
			//System.out.println(TreePathObj);   
			
			 Object[] row = new Object[3];
			 row[0] = "a";
	         row[1] = "b";
	         row[2] = "c";
	         
	         //tableModel.setValueAt("VALUE", 0, 0);
		}
		else
		{
			System.out.println(" Jtree area ");   
		}
         

 		//int nb = StringUtils.countMatches(Combien de a dans cette phrase ?, a);
 		
 		//int counter = value.split("\\", -1).length - 1;
 		
 		//int aaa = (int) value.chars().filter(num -> num == '\\').count();
         
       //tableModel.setValueAt("VALUE", 0, 0);
			//value = value.substring(0, value.length() - 1);
			//value = value.replaceFirst("Plan\\", ""); 
         
       //s = value.replace("\\", "");
			//s = s.replaceFirst("Plan", ""); 

			//value = value.replaceFirst("Plan\\", ""); 
		
			//value = s.substring(0, s.length()-1); //strNew is 'Hello World'
	}
  
}

class SelectionTreePopupMenu extends JPopupMenu 
{
	  protected TreePath path;

	  int NODE_MAXIMUM_LEVELS = 2;
	  
	 // Class_plan intermediaire = new Class_plan();

	  int level = 0;
	  int sublevel = 0;
	  
	  protected SelectionTreePopupMenu(Class_plan intermediaire) 
	  {
	    super();

	     // intermediaire.addSequence(new TestPlan());
	    
	    add("add child node").addActionListener(e -> 
	    {
	      JTree tree = (JTree) getInvoker();
	      TreePath path = tree.getSelectionPath();
	      //if (path.getPathCount() <= NODE_MAXIMUM_LEVELS) 
	      //{
		      if(path != null )
		      {
		    	  if (path.getPathCount() <= NODE_MAXIMUM_LEVELS) 
		    	  {
		    		  DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		    		  DefaultMutableTreeNode self = (DefaultMutableTreeNode) path.getLastPathComponent();
		    		  DefaultMutableTreeNode child = new DefaultMutableTreeNode("New child node");
		    		  self.add(child);
		    		  model.reload(self);
		  
		    		  Object pathString[] = path.getPath();
		    		  int n = pathString.length;
		    		  String type = "";
		    		  		    		  
		    		  switch(n) 
		    		  {
			             case 1 :
			            	 		type = "test";
			            	 		level = level + 1;
			            	 		System.out.println(" [Infos]:  level ---> " + type + " " + level);
			            	 		break;
			             case 2 :
			            	 		type = "subtest";
			            	 		sublevel = sublevel + 1;
			            	 		 System.out.println(" [Infos]:  ---> " + type + " " + sublevel);
			            	 		break;
			             default :
			            	 		System.out.println("Invalid choice");
		    		  }
		    			
		    		  intermediaire.addSequence(new TestPlan(type, level, "New child node", "", "") );
		    		  //System.out.println(" [Infos]:  ---> " + type + " " + level);
		    		  
		    	  }
		    	  else 
			      {
			    	  String message = String.format("ERROR: Maximum levels of %d exceeded.", NODE_MAXIMUM_LEVELS);
			          JOptionPane.showMessageDialog(tree, message, "add node", JOptionPane.ERROR_MESSAGE);
			      }
		    	  
		      }
		      else
		      {
		    	  String message = String.format("ERROR: Please , select tree node !. ");
		 	      JOptionPane.showMessageDialog(tree, message, "add child node", JOptionPane.ERROR_MESSAGE);
		      }
		      
	      //	graph_line.addLines( new LineObj((int)StartPoint.getX(), (int)StartPoint.getY(), (int)EndPoint.getX(), (int)EndPoint.getY() , Color.BLUE ) );
 		      
	      // or: model.insertNodeInto(child, self, self.getChildCount());
	      //}
	      //else 
	      //{
	    //	  String message = String.format("ERROR: Maximum levels of %d exceeded.", NODE_MAXIMUM_LEVELS);
	     //     JOptionPane.showMessageDialog(tree, message, "add node", JOptionPane.ERROR_MESSAGE);
	     // }
		      
	    });
	    
	    addSeparator();
	    
	    add("insert preceding sibling node").addActionListener(e -> 
	    {
	      JTree tree = (JTree) getInvoker();
	      DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
	      DefaultMutableTreeNode self = (DefaultMutableTreeNode) path.getLastPathComponent();
	      DefaultMutableTreeNode parent = (DefaultMutableTreeNode) self.getParent();
	      DefaultMutableTreeNode child = new DefaultMutableTreeNode("New preceding sibling");
	      int index = model.getIndexOfChild(parent, self);
	      parent.insert(child, index);
	      model.reload(parent);
	      // or: model.insertNodeInto(child, parent, index);
	    });
	    
	    add("insert following sibling node").addActionListener(e -> 
	    {
	      JTree tree = (JTree) getInvoker();
	      DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
	      DefaultMutableTreeNode self = (DefaultMutableTreeNode) path.getLastPathComponent();
	      DefaultMutableTreeNode parent = (DefaultMutableTreeNode) self.getParent();
	      DefaultMutableTreeNode child = new DefaultMutableTreeNode("New following sibling");
	      int index = model.getIndexOfChild(parent, self);
	      parent.insert(child, index + 1);
	      model.reload(parent);
	      // or: model.insertNodeInto(child, parent, index + 1);
	    });
	    
	    
	    add("Remove").addActionListener(e -> 
	    {
	    	  DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
		      if (!node.isRoot()) 
		      {
		        JTree tree = (JTree) getInvoker();
		        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		        model.removeNodeFromParent(node);
		      }
	    });
		    
	    add("Edit").addActionListener(e -> 
	    {
	    	JTextField textField = new JTextField(); //24
	    	Object node = path.getLastPathComponent();
		      
	    	DefaultMutableTreeNode leaf = (DefaultMutableTreeNode) node;
	    	textField.setText(leaf.getUserObject().toString());
	    	JTree tree = (JTree) getInvoker();
	        
	    	if( !textField.getText().equals("Plan"))
	    	{	
		    	int ret = JOptionPane.showConfirmDialog(tree, textField, "edit", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
		    	if (ret == JOptionPane.OK_OPTION) 
		    	{
		    		Optional.ofNullable(textField.getText())
		            .filter(str -> !str.trim().isEmpty())
		            .ifPresent(str -> {
		              DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		              String old_sequence = leaf.getUserObject().toString();
		              model.valueForPathChanged(path, str);

		              // mise à jour de la database
		             
		              TestPlan obj = intermediaire.GetSequenceNamed(old_sequence);
		              obj.setSequence(str); 
		              
		              String w = "123";
		              
		            });
		    	}
	    	}
	    	 else 
		     {
		    	  String message = String.format("ERROR: Plan level cannot be changed !.", NODE_MAXIMUM_LEVELS);
		          JOptionPane.showMessageDialog(tree, message, "Edit node", JOptionPane.ERROR_MESSAGE);
		      }
	    });
	    
	  }

	  @Override 
	  public void show(Component c, int x, int y) 
	  {
	    if (c instanceof JTree) 
	    {
	    	JTree tree = (JTree) c;
	    	path = tree.getPathForLocation(x, y);
	    	Optional.ofNullable(path).ifPresent(tp -> super.show(c, x, y));
	    }
	  }

}


//// Traversing Trees

// Set the selected Jtree Node to JTable


/*
public void printDescendants(TreeNode root) {
	  System.out.println(root);
	  Enumeration children = root.children();
	  if (children != null) {
	    while(children.hasMoreElements()) {
	      printDescendants((TreeNode)children.nextElement());
	    }
	  }
	}*/


/*
start at root
If is a leaf - Print node name and return
Print --- node name ----
If node has children - recurse with each child (starting at 2)
Print +++ Node name ++++


public static void print(DefaultMutableTreeNode aNode)
{
    String name = aNode.toString();
    int level= aNode.getLevel();
    String placement = "";
    while (level > 0)
    {
        placement += ">";
        level--;
    }
    if(aNode.isLeaf())
    {
        System.out.println(placement + name);
        return;
    }

    System.out.println(placement + "--- " + name + " ---");
    for(int i = 0 ; i < aNode.getChildCount() ; i++)
    {
        print((DefaultMutableTreeNode)aNode.getChildAt(i));
    }
    System.out.println(placement + "+++ " + name + " +++");
}
*/



// https://www.journaldev.com/1112/how-to-write-xml-file-in-java-dom-parser


//    https://stackoverflow.com/questions/22005632/converting-jtree-to-xml-in-java



/*

public String getNodeIndex(JTree tree, TreeNode node) {
    TreeNode root = (TreeNode) tree.getModel().getRoot();
    if (node == root) {
        return "";
    }
    TreeNode parent = node.getParent();
    if (parent == null) {
        return null;
    }
    String parentIndex= getNodeIndex(tree, parent);
    if (parentIndex == null) {
        return null;
    }
    return parentIndex+"."+parent.getIndex(node);
}

*/




/*
Get the root of the tree:

	 tree.getModel().getRoot();
	then get the number of children of this root node:

	 tree.getModel().getChildCount(rootNode)
	then go from 0 to the number of children and call

	tree.getModel().getChild(rootNode, i)

*/



// https://stackoverflow.com/questions/22005632/converting-jtree-to-xml-in-java