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.border.LineBorder;

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.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;


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

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

// MVC  JTABLE

public class test extends JDialog 
{
	
	private boolean close_Jdialog = false;
	
	
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	
	public test(JFrame f, boolean modal) 
	{
       super(f, modal);
       
       //Point location = f.getLocation();
       //setLocation(location.x + 250, location.y + 750);

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

	// envoi les datas
	public void initialise( Object [] o)
	{
		
	}
 
	// retourne les datas
	public Object [] getDonnees()
	{
		return null;
	
	}
  
	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;
		}
	}
	
	
	/******************************************/
	
	class TextAreaRenderer extends JScrollPane implements TableCellRenderer
	{
	   JTextArea textarea;
	  
	   public TextAreaRenderer() 
	   {
	      textarea = new JTextArea();
	      textarea.setLineWrap(true);
	      textarea.setWrapStyleWord(true);
	      //textarea.setBorder(new TitledBorder("This is a JTextArea"));
	      getViewport().add(textarea);
	   }
	  
	   public Component getTableCellRendererComponent(JTable table, Object value,
	                                  boolean isSelected, boolean hasFocus,
	                                  int row, int column)
	   {
	      if (isSelected) {
	         setForeground(table.getSelectionForeground());
	         setBackground(table.getSelectionBackground());
	         textarea.setForeground(table.getSelectionForeground());
	         textarea.setBackground(table.getSelectionBackground());
	      } else {
	         setForeground(table.getForeground());
	         setBackground(table.getBackground());
	         textarea.setForeground(table.getForeground());
	         textarea.setBackground(table.getBackground());
	      }
	  
	      textarea.setText((String) value);
	      textarea.setCaretPosition(0);
	      return this;
	   }
	}
	  
	class TextAreaEditor extends DefaultCellEditor {
	   protected JScrollPane scrollpane;
	   protected JTextArea textarea;
	  
	   public TextAreaEditor() {
	      super(new JCheckBox());
	      scrollpane = new JScrollPane();
	      textarea = new JTextArea(); 
	      textarea.setLineWrap(true);
	      textarea.setWrapStyleWord(true);
	      //textarea.setBorder(new TitledBorder("This is a JTextArea"));
	      scrollpane.getViewport().add(textarea);
	   }
	  
	   public Component getTableCellEditorComponent(JTable table, Object value,
	                                   boolean isSelected, int row, int column) {
	      textarea.setText((String) value);
	  
	      return scrollpane;
	   }
	  
	   public Object getCellEditorValue() {
	      return textarea.getText();
	   }
	} 

	/*******************************************************/
	// https://coderanch.com/t/340609/java/JTable-Custom-Cell-Renderer-JTextArea
	
	// http://esus.com/embedding-a-jtextarea-in-a-jtable-cell/
	 private void ccc()
	    {
		 
		 setResizable( false );
		  	setBounds(100, 100, 600, 460);
			getContentPane().setLayout(null);
	     	
			ImageIcon icon = null;
			icon = createImageIcon("resources/test.png", "");
			setIconImage( icon.getImage() );
			
			setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
			setTitle("blabaTest definition ");
/*	 OK
			JTable table = new JTable(); 
	        Object[] columns = {"Id","First Name","Last Name","Age"};
	        DefaultTableModel model = new DefaultTableModel();
	        model.setColumnIdentifiers(columns);
	        table.setModel(model);
		    
	        TableColumnModel colModel = table.getColumnModel();
	        TableColumn col = colModel.getColumn(1);
	        col.setCellRenderer(new CustomRenderer());
	        col.setCellEditor(new CustomEditor());
	        table.setRowHeight(50);
	        table.setPreferredScrollableViewportSize(table.getPreferredSize());
 */       

			
/*	OK		
			DefaultTableModel dtm = new DefaultTableModel(); 
			
			dtm.setDataVector(new Object[][]{{ "JTextArea1", "This is a testnon long linesn" },
                 { "JTextArea2", "Hello, world!" }},
					 new Object[]{ "String","JTextArea"});
  
			JTable table = new JTable(dtm);
			table.getColumn("JTextArea").setCellRenderer(new TextAreaRenderer());
			table.getColumn("JTextArea").setCellEditor(new TextAreaEditor());
			table.setRowHeight(80);			
*/	        
			
			
			DefaultTableModel model = new DefaultTableModel(); 
			
			//model.setDataVector(new Object[][]{  },	 new Object[]{ "String","JTextArea"});
			model.setDataVector(new Object[][]{  },	 new Object[]{ "Un","Deux", "Trois", "Quatre"});
			
			JTable table = new JTable(model);
			table.getColumn("Deux").setCellRenderer(new TextAreaRenderer());
			table.getColumn("Deux").setCellEditor(new TextAreaEditor());
			table.setRowHeight(80);			
	        
			table.getColumnModel().getColumn(0).setPreferredWidth(27);
			table.getColumnModel().getColumn(1).setPreferredWidth(120);
			table.getColumnModel().getColumn(2).setPreferredWidth(100);
			table.getColumnModel().getColumn(3).setPreferredWidth(90);
			
			/*
			TableColumn column = null;
			for (int i = 0; i < 3; i++) {
			    column = table.getColumnModel().getColumn(i);
			    if (i == 2) {
			        column.setPreferredWidth(100); //third column is bigger
			    } else {
			        column.setPreferredWidth(50);
			    }
			}*/
			
	        JScrollPane pane = new JScrollPane(table);
	        pane.setBounds(34, 24, 525, 180);
	        getContentPane().add(pane);
	        
	        Object[] col = new Object[4];
	        
	        /*******************/
	        
	        JButton btn_addrow = new JButton("Add");
			btn_addrow.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					System.out.println("Btn add row ");
					//col[0] = "aaa";
		            //col[1] = "bbb";
		            //col[2] = "ccc";
		            //col[2] = "ddd";
		            model.addRow(col);  
				}
			});
			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) 
				{
					System.out.println("Btn remove row ");
		            int i = table.getSelectedRow();
		            if(i >= 0)
		            {
		            	 model.removeRow(i);
		            }
					
				}
			});
			btn_remove.setBounds(65, 276, 89, 23);
			getContentPane().add(btn_remove);
	        
	        /*******************/
			
			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) 
				{
					close_Jdialog = false;
					System.out.println("Btn Cancel  \n");
					//dispose();
					
					
				}
			});
			btn_cancel.setBounds(359, 391, 89, 23);
			getContentPane().add(btn_cancel);
	
	    }
	  
	 
	    
}
