import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

/** Esta clase implementará un panel que mostrara una tabla con los semestres y sus ramos.
 */
public class Tabla extends JPanel {
    
	private static JTable table;
	
	public Tabla(){
		
	}
	
    /** Constructor de la clase
     */
    public Tabla(final Object[][] rowData, final String[] columnNames, final boolean b) {
    	super(new GridLayout(1,0));
        
        TableModel dataModel = new AbstractTableModel() {
        	public int getColumnCount() { return columnNames.length; } 
        	public int getRowCount() { return rowData.length;} 
        	public Object getValueAt(int row, int col) {return rowData[row][col];} 
        	public String getColumnName(int column) {return (String)columnNames[column];} 
        	public Class getColumnClass(int c) {return getValueAt(0, c).getClass();} 
        	public boolean isCellEditable(int row, int col) {return false;} 
        	public void setValueAt(Object aValue, int row, int column) { rowData[row][column] = aValue; } 
        }; 
        
        DefaultListSelectionModel rm = new DefaultListSelectionModel();

        DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() { 
        	public void setValue(Object value) { 
        		if (value instanceof Celda) { 
        			Celda c = (Celda) value; 
        			if (b){
        				setBackground(c.getColorFondo());
        				setForeground(c.getColorText());
        			}
        			else{
        				setBackground(Color.white);
        				setForeground(Color.black);
        			}
        			setText(c.toString()); 
        		} else { 
        			super.setValue(value); 
        		} 
	 	    } 
	    };
        
        table = new JTable(dataModel);
        table.setRowSelectionAllowed(false);
        table.setColumnSelectionAllowed(false);
        table.setCellSelectionEnabled(b);
        table.setSelectionModel(rm);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setDragEnabled(false);
        table.setPreferredScrollableViewportSize(new Dimension(600, 120));
        
        for (int i=0; i<table.getColumnCount(); i++){
        	String j = table.getColumnName(i);
        	TableColumn tc =(TableColumn)table.getColumn(j);
        	tc.setCellRenderer(colorRenderer);
        }
    	
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
        setOpaque(true);
        setSize(600,145);
        if (b)
        	rm.addListSelectionListener(new Seleccion(table));
    }
    
    /** Setea los parametros de la celda seleccionada
     * @param b booleano caracteristico de futura celda.
     */
    public void setCelda(boolean b){
    	int x = table.getSelectedColumn();
		int y = table.getSelectedRow();
		Celda c = ((Celda)table.getValueAt(y,x));
		c.setPasado(b);
		table.setValueAt(c,y,x);
		table.repaint();
    }
    
    private class Seleccion implements ListSelectionListener{
    	JTable table;
        private static final long serialVersionUID=5;    	

      	public Seleccion(JTable table){
    		this.table = table;
    	}
    	
       	public void valueChanged(ListSelectionEvent e){
    		int x = table.getSelectedColumn();
    		int y = table.getSelectedRow();
    		try{
    			ColumnaRamo.actualizarRamo((Celda)table.getValueAt(y,x));
    		}
    		catch(NullPointerException ev){
    			ColumnaRamo.desactualizarRamo();
    		}
    	}
    }

    private static final long serialVersionUID=4;
}
