
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.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

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.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import javax.swing.JTable;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


// http://tecfa.unige.ch/guides/tie/pdf/files/java-swing.pdf
// http://myflex.org/yf/java/slides/Unit13.pdf    

// MVC  JTABLE

public class DialogPreferences extends JDialog 
{
	
	private JTextField champ_state_name;
	private boolean close_Jdialog = false;
	private Node node;
	private String state_name;
	private ArrayList copy_node;
	
	private Color current;
	
	private Color state_newColor;
	private Color text_newColor;
	
	private Color current_state_Color;
	private Color current_text_Color;
	
	private JPanel panel_state_color;
	private JPanel panel_text_color;

	private Object obj[][];
	private String store_datas[][];
	
	public DialogPreferences(JFrame f, boolean modal) // , ArrayList xxxxcopy_node)  // passage par initialise et pas par le constructeur
	{
       super(f, modal);
             
       
       this.node = node;
       //this.copy_node = copy_node;
       
       panel_state_color = new JPanel();
       panel_text_color = new JPanel();
       
       // decalage par rapport au frame
       Point location = f.getLocation();
       setLocation(location.x + 250, location.y + 250);

       // fermer la fenetre
       addWindowListener(new java.awt.event.WindowAdapter() 
       {
           public void windowClosing(java.awt.event.WindowEvent e) 
           {
        	   dispose();
           }
       });
       
       init();
   
	}

	// envoi les datas
	public void initialise( Object [] o)
	{
		
	}
 
	// retourne les datas
	public Object [] getDonnees()
	{
		return null;
	
	}
  
	private void init()
	{
	  	setResizable( false );
	  	setBounds(100, 100, 600, 460);
		getContentPane().setLayout(null);
	    	 
     	
		ImageIcon icon = null;
		icon = createImageIcon("resources/settings.png", "");
		setIconImage( icon.getImage() );
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Settings state parameters ");
		
		
		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);
        
        // Change A JTable Background Color, Font Size, Font Color, Row Height
        table.setBackground(Color.LIGHT_GRAY);
        table.setForeground(Color.black);
        Font font = new Font("",1,22);
        table.setFont(font);
        table.setRowHeight(30);
		
        JScrollPane pane = new JScrollPane(table);
        pane.setBounds(34, 24, 525, 180);
        //pane.setBounds(30, 40, 200, 300);
        getContentPane().add(pane);
	/*	
		String data[][]={ {"101","Amit","670000"},    
                {"102","Jai","780000"},    
                {"101","Sachin","700000"}};    
		String column[]={"ID","NAME","SALARY"};         

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(30, 40, 200, 300);
		getContentPane().add(scrollPane);

		// DefaultTableModel model = new DefaultTableModel();
		// JTable table = new JTable(model);
		//    DefaultTableModel model = new DefaultTableModel();
		//    JTable table = new JTable(model);
		//    model.addRow(new Object[]{"c1", "c2"});
		 
		 
		JTable jt=new JTable(data,column);
		scrollPane.setViewportView(jt);
		*/
        
        
        JComboBox combo = new JComboBox();
        combo.addItem("No");
        combo.addItem("Yes");
        combo.addItem("blabla");
        combo.addItem("Taratata");
        
        table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(combo));
        
        
        Object[] row = new Object[4];
        
		JButton btn_addrow = new JButton("New button");
		btn_addrow.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				System.out.println("Btn add row ");
				
				  row[0] = "a";
	                row[1] = "b";
	               // row[2] = "c";
	                row[3] = "d";
	                
	                // add row to the model
	                model.addRow(row);
				
			
				
			}
		});
		btn_addrow.setBounds(65, 242, 89, 23);
		getContentPane().add(btn_addrow);
		
		JButton btn_remove = new JButton("Remove");
		btn_remove.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
	               int i = table.getSelectedRow();
	               if(i >= 0)
	               {
	                   // remove a row from jtable
	                   model.removeRow(i);
	               }
				
			}
		});
		btn_remove.setBounds(65, 276, 89, 23);
		getContentPane().add(btn_remove);
		
		JButton btn_test = new JButton("Test");
		btn_test.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				System.out.println("Btn test ");
				
				  row[0] = "a";
	                row[1] = "b";
	               // row[2] = "c";
	                row[3] = "d";
	                
	                // add row to the model
	                model.addRow(row);
				
				
			}
		});
		btn_test.setBounds(65, 310, 89, 23);
		getContentPane().add(btn_test);
		
		
		/*******************/
		
		JSeparator separator = new JSeparator();
		separator.setBounds(1, 384, 600, 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) 
			{
				Object[][] tableData = getTableData (table);
				
				model.setRowCount(0);
				
				int len = tableData.length;
				
				for (int i = 0 ; i < len ; i++)
				{
					System.out.println("Contenu :" + tableData[i][0] + " " + tableData[i][1] + "  " +  tableData[i][2] + "  " + tableData[i][3] );
					
					row[0] = tableData[i][0];
	                row[1] = tableData[i][1];
		            row[2] = tableData[i][2];
		            row[3] = tableData[i][3];
		            model.addRow(row);
				}
				
				
				close_Jdialog = false;
				System.out.println("Btn Cancel  \n");
				//dispose();
				
				
			}
		});
		btn_cancel.setBounds(359, 391, 89, 23);
		getContentPane().add(btn_cancel);
		
		/*******************/
	}
	
	public Object[][] getTableData (JTable table) 
	{
		TableModel dtm = table.getModel();
		int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
		
		Object[][] tableData = new Object[nRow][nCol];
		
		for (int i = 0 ; i < nRow ; i++)
		{	
			for (int j = 0 ; j < nCol ; j++)
		    {
				tableData[i][j] = dtm.getValueAt(i,j);
		    }
		}    
		
		return tableData;
	}
    
	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;
		}
	}
  
}