package jtable01;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;



public class code {

	private static JComboBox cmb1;
	
   public static void main(String[] args){
       
       // create JFrame and JTable
       JFrame frame = new JFrame();
       JTable table = new JTable(); 
       
       // create a table model and set a Column Identifiers to this model 
       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);
       
       // create JTextFields
       JTextField textId = new JTextField();
       JTextField textFname = new JTextField();
       JTextField textLname = new JTextField();
       JTextField textAge = new JTextField();
       
       // create JButtons
       JButton btnAdd = new JButton("Add");
       JButton btnDelete = new JButton("Delete");
       JButton btnUpdate = new JButton("Update");     
       
       textId.setBounds(20, 220, 100, 25);
       textFname.setBounds(20, 250, 100, 25);
       textLname.setBounds(20, 280, 100, 25);
       textAge.setBounds(20, 310, 100, 25);
       
       btnAdd.setBounds(150, 220, 100, 25);
       btnUpdate.setBounds(150, 265, 100, 25);
       btnDelete.setBounds(150, 310, 100, 25);
       
       // create JScrollPane
       JScrollPane pane = new JScrollPane(table);
       pane.setBounds(0, 0, 880, 200);
       
       frame.setLayout(null);
       
       frame.add(pane);
       
       // add JTextFields to the jframe
       frame.add(textId);
       frame.add(textFname);
       frame.add(textLname);
       frame.add(textAge);
   
       // add JButtons to the jframe
       frame.add(btnAdd);
       frame.add(btnDelete);
       frame.add(btnUpdate);
       
       // create an array of objects to set the row data
       Object[] row = new Object[4];
       
       
       JComboBox comboBox = new JComboBox();
       comboBox.addItem("Snowboarding");
       comboBox.addItem("Rowing");
       comboBox.addItem("Knitting");
       comboBox.addItem("Speed reading");
       comboBox.addItem("Pool");
       comboBox.addItem("None of the above");
       
       cmb1 = new JComboBox(new Object[]{"1.1", "1.0"});
       
       //use dropdown boxes
       String[] options = new String[]{"No", "Yes", "Only non-default"};
      // TableColumn column = TPTable.getColumnModel().getColumn(2);
      // column.setCellEditor(new MyJComboBoxEditor(options));

       //BorderPanel borderPanel3 =   new  BorderPanel("Panel 3");
       
//       JComboBox<String> ppComboBox;
//       DefaultComboBoxModel<String> cbm = new DefaultComboBoxModel<String>( new String[] 
//              { "person", "student", "employee" });
//       ppComboBox = new JComboBox<String>(cbm);

       // a voir pour le definition du model
       //http://www2.hawaii.edu/~takebaya/ics111/jtable_custom/jtable_custom.html
       
     //  OK premiere facon 
     //  table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(comboBox));
     
       
       // OK autre facon de l'écrire
       JComboBox combo = new JComboBox();
       combo.addItem("Names");
       combo.addItem("Antony");
       combo.addItem("Victor");
       combo.addItem("Ramkumar");
       TableColumn pipo = table.getColumnModel().getColumn(2);
       pipo.setCellEditor(new DefaultCellEditor(combo));

       // button add row
       btnAdd.addActionListener(new ActionListener(){

           @Override
           public void actionPerformed(ActionEvent e) {
            
               row[0] = textId.getText();
               row[1] = textFname.getText();
               //row[2] = textLname.getText();
               row[3] = textAge.getText();
               
               // add row to the model
               model.addRow(row);
           }
       });
       
       // button remove row
       btnDelete.addActionListener(new ActionListener(){

           @Override
           public void actionPerformed(ActionEvent e) {
           
               // i = the index of the selected row
               int i = table.getSelectedRow();
               if(i >= 0){
                   // remove a row from jtable
                   model.removeRow(i);
               }
               else{
                   System.out.println("Delete Error");
               }
           }
       });
       
       // get selected row data From table to textfields 
       table.addMouseListener(new MouseAdapter(){
       
       @Override
       public void mouseClicked(MouseEvent e){
           
           // i = the index of the selected row
           int i = table.getSelectedRow();
           
           textId.setText(model.getValueAt(i, 0).toString());
           textFname.setText(model.getValueAt(i, 1).toString());
           textLname.setText(model.getValueAt(i, 2).toString());
           textAge.setText(model.getValueAt(i, 3).toString());
       }
       });
       
       
       
       // button update row
       btnUpdate.addActionListener(new ActionListener(){

           @Override
           public void actionPerformed(ActionEvent e) 
           {
        	   // retourne le contenu de la cellule
        	   String val = (String) table.getModel().getValueAt(0, 2);
        	   
               // i = the index of the selected row
               int i = table.getSelectedRow();
               
               int nb_row = table.getRowCount();
               int nb8col = table.getColumnCount();
               
               String selectedData = null;
               int[] selectedRow = table.getSelectedRows();
               int[] selectedColumns = table.getSelectedColumns();

 /*              for (int i = 0; i < selectedRow.length; i++) {
                 for (int j = 0; j < selectedColumns.length; j++) {
                   selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
                 }
               }
*/               
               
               if(i >= 0) 
               {
                  model.setValueAt(textId.getText(), i, 0);
                  model.setValueAt(textFname.getText(), i, 1);
                  model.setValueAt(textLname.getText(), i, 2);
                  model.setValueAt(textAge.getText(), i, 3);
               }
               else{
                   System.out.println("Update Error");
               }
           }
       });
       
       frame.setSize(900,400);
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
       
   }
   
   
   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;
	}
   
   class MyJComboBoxEditor extends DefaultCellEditor {
	    public MyJComboBoxEditor(String[] items) {
	        super(new JComboBox(items));
	    }
	}

}

/*
comment remplir un Jtable a partir d'un array

JTable  booktable = new JTable();


String[] colName = { "Call No", "Name", "Category", "Author ",
        "Publisher", "From", "Price", "Remarks" ,"Edit/Delete"};
booktable.getTableHeader().setBackground(Color.WHITE);
booktable.getTableHeader().setForeground(Color.BLUE);
Font Tablefont = new Font("Book Details", Font.BOLD, 12);
booktable.getTableHeader().setFont(Tablefont);

List<Book> books = new ArrayList<Book>();
books = ServiceFactory.getBookService().findAllBook();

Object[][] object = new Object[100][100];
int i = 0;
if (books.size() != 0) {
    for (Book book : books) {
        object[i][0] = book.getCallNo();
        object[i][1] = book.getName();
        object[i][2] = book.getCategory_id().getName();
        object[i][3] = book.getAuthor();
        object[i][4] = book.getPublisher();
        object[i][5] = book.getFrom();
        object[i][6] = book.getPrice();
        object[i][7] = book.getRemark();
        object[i][8] = getEditDeleteButton();
        i++;
        booktable = new JTable(object, colName);
    }
}

*/