/*
 * jProyecto1.java
 *
 * Created on 7 de julio de 2007, 11:49
 */

package proyecto1;

import java.sql.*;
import java.lang.Object;
import java.util.ArrayList;
import java.util.Date;
import java.lang.Integer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.lang.Integer;
import java.awt.event.InvocationEvent;
import java.awt.EventQueue;
import java.awt.*;
import javax.swing.JFrame;

/**
 *
 * @author  Simón Campos Rivera
 */
public class jProyecto1 extends javax.swing.JFrame {
    
    /** Creates new form jProyecto1 */
    public jProyecto1() throws  Exception{
        initComponents();
        mdbc = new MyDBConnection();//Genera un nuevo objeto para la conexion.
        if(mdbc.init()){
            Connection conn = mdbc.getMyConnection();
            stmt = conn.createStatement();
        }
        else{
            System.out.println("NO CONECTO");
         }
         
        TablasParametros();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_Semana = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jNombre = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jAsignatura = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jNombreCurso = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jHoras = new javax.swing.JTextField();
        jListaAsignaturas = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jPedir = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTabla_datos = new javax.swing.JTable();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jCampo1 = new javax.swing.JTextField();
        jCampo2 = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Horario Inteligente");
        setResizable(false);
        jPanel1.setLayout(null);

        jTable_Semana.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Hora", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable_Semana.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_SemanaMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(jTable_Semana);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(70, 170, 580, 120);

        jButton1.setText("GUARDAR");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });

        jPanel1.add(jButton1);
        jButton1.setBounds(470, 70, 180, 60);

        jPanel1.add(jNombre);
        jNombre.setBounds(160, 60, 250, 20);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("NOMBRE:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(70, 60, 70, 15);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("ASIGNATURA:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(70, 90, 80, 20);

        jPanel1.add(jAsignatura);
        jAsignatura.setBounds(160, 90, 160, 20);

        jTabbedPane1.addTab("HORARIO", jPanel1);

        jPanel2.setLayout(null);

        jNombreCurso.setFont(new java.awt.Font("Tahoma", 0, 18));
        jPanel2.add(jNombreCurso);
        jNombreCurso.setBounds(40, 90, 120, 50);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 20));
        jLabel3.setText(" CURSO");
        jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.add(jLabel3);
        jLabel3.setBounds(40, 50, 100, 30);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText(" ASIGANTURA");
        jLabel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.add(jLabel4);
        jLabel4.setBounds(280, 60, 90, 30);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText(" N\u00ba HORAS");
        jLabel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.add(jLabel5);
        jLabel5.setBounds(280, 100, 90, 30);

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jButton2.setText("AGREGAR");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton2MousePressed(evt);
            }
        });

        jPanel2.add(jButton2);
        jButton2.setBounds(520, 320, 160, 60);

        jPanel2.add(jHoras);
        jHoras.setBounds(380, 100, 80, 30);

        jListaAsignaturas.setFont(new java.awt.Font("Tahoma", 0, 14));
        jListaAsignaturas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Matematicas", "Castellano", "Historia", "Geografia", "Ingles", "Comprension del Medio", "Educacion Fisica" }));
        jPanel2.add(jListaAsignaturas);
        jListaAsignaturas.setBounds(380, 60, 250, 25);

        jTabbedPane1.addTab("HORAS SEMANAL", jPanel2);

        jPanel3.setLayout(null);

        jPedir.setFont(new java.awt.Font("Tahoma", 1, 14));
        jPedir.setText("OBTENER DATOS");
        jPedir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPedirMousePressed(evt);
            }
        });

        jPanel3.add(jPedir);
        jPedir.setBounds(480, 320, 210, 60);

        jScrollPane2.setAutoscrolls(true);
        jTabla_datos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Indice", "NOMBRE", "ASIGNATURA", "DIA", "BLOQUE"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTabla_datos);

        jPanel3.add(jScrollPane2);
        jScrollPane2.setBounds(40, 30, 460, 230);

        jComboBox1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "nombre", "asignatura", "dia", "bloque" }));
        jPanel3.add(jComboBox1);
        jComboBox1.setBounds(40, 300, 150, 20);

        jLabel6.setText("CRITERIO DE SELECCION 1");
        jPanel3.add(jLabel6);
        jLabel6.setBounds(40, 280, 160, 20);

        jLabel7.setText("CRITERIO DE SELECCION 2");
        jPanel3.add(jLabel7);
        jLabel7.setBounds(280, 280, 160, 20);

        jComboBox2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "nombre", "asignatura", "dia", "bloque" }));
        jPanel3.add(jComboBox2);
        jComboBox2.setBounds(280, 300, 150, 20);

        jCampo1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel3.add(jCampo1);
        jCampo1.setBounds(40, 340, 160, 30);

        jCampo2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel3.add(jCampo2);
        jCampo2.setBounds(280, 340, 160, 30);

        jTabbedPane1.addTab("DATOS", jPanel3);

        jTabbedPane1.getAccessibleContext().setAccessibleName("HORARIO");

        jMenu1.setText("Archivo");
        jMenuItem1.setText("Item");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Menu");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Ayuda");
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 715, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 433, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jPedirMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPedirMousePressed
        int x = 0;
        
        for(int i = 0; i < jTabla_datos.getRowCount(); i++ )
            for(int j = 1; j < jTabla_datos.getColumnCount(); j++)
            {
            jTabla_datos.setValueAt(null, i, j);
            }
        
        if(((String)jComboBox2.getModel().getSelectedItem()).compareTo("") == 0){
            try {                
                ResultSet rs = stmt.executeQuery("SELECT * FROM profesor WHERE " + (String)jComboBox1.getModel().getSelectedItem() + " = '" + jCampo1.getText() + "';");
                while(rs.next()){
                    jTabla_datos.setValueAt(rs.getString("nombre"), x, 1);
                    jTabla_datos.setValueAt(rs.getString("asignatura"), x, 2);
                    jTabla_datos.setValueAt(rs.getString("dia"), x, 3);
                    jTabla_datos.setValueAt(rs.getString("bloque"), x, 4);
                    x++;
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        else{
            try {                
                ResultSet rs = stmt.executeQuery("SELECT * FROM profesor WHERE " + (String)jComboBox1.getModel().getSelectedItem() + " = '" + jCampo1.getText() + "'" +
                        " && " + (String)jComboBox2.getModel().getSelectedItem() + " = '" + jCampo2.getText() + "';");
                while(rs.next()){
                    jTabla_datos.setValueAt(rs.getString("nombre"), x, 1);
                    jTabla_datos.setValueAt(rs.getString("asignatura"), x, 2);
                    jTabla_datos.setValueAt(rs.getString("dia"), x, 3);
                    jTabla_datos.setValueAt(rs.getString("bloque"), x, 4);
                    x++;
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_jPedirMousePressed

    
    private void jTable_SemanaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_SemanaMouseClicked
        // TODO add your handling code here:
        int cell_X = 0;
        int cell_Y = 0;
        cell_X = jTable_Semana.columnAtPoint(evt.getPoint());
        cell_Y = jTable_Semana.rowAtPoint(evt.getPoint());
        if(cell_X > 0){
            if(jTable_Semana.getValueAt(cell_Y, cell_X) == "X"){
                jTable_Semana.setValueAt("",cell_Y, cell_X);
            }
            else{
                jTable_Semana.setValueAt("X", cell_Y, cell_X);
            }
        }
    }//GEN-LAST:event_jTable_SemanaMouseClicked

    private void jButton2MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MousePressed
        
        ResultSet rs;  
        if(!(jHoras.getText().compareTo("") == 0) && !(jNombreCurso.getText().compareTo("") == 0))
            try {
                stmt.executeUpdate("INSERT INTO asignaturas VALUES ('" + (String)jListaAsignaturas.getModel().getSelectedItem() +
                        "','" + jHoras.getText()+ "','" + jNombreCurso.getText() + "')");
                
                jHoras.setText("");
                jNombreCurso.setText("");
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        else
            System.out.println("No paso nada");
        
        
    }//GEN-LAST:event_jButton2MousePressed

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
        // TODO add your handling code here:
        Integer ii = new Integer(0);
        
        if(!(jNombre.getText().compareTo("") == 0) && !(jAsignatura.getText().compareTo("") == 0))
        {
            for(int x = 0; x < jTable_Semana.getRowCount(); x++)
                for(int y = 0; y < jTable_Semana.getColumnCount(); y++)
                    if(jTable_Semana.getValueAt(y,x) == "X"){
                        System.out.println(jTable_Semana.getColumnName(x));
                    
                        try {
                            stmt.executeUpdate("INSERT INTO profesor VALUES ('" + jNombre.getText()+"','" + jAsignatura.getText() +
                                    "','" + jTable_Semana.getColumnName(x) + "','" + ii.toString(y + 1) + "')");
            
                        } catch (SQLException ex) {
                            ex.printStackTrace();   
                        }
                    }
            
            for(int i = 0; i < jTable_Semana.getRowCount(); i++ )
                for(int j = 1; j < jTable_Semana.getColumnCount(); j++)
                    jTable_Semana.setValueAt(null, i, j); 
            
            jNombre.setText("");
            jAsignatura.setText("");
            
        }
    }//GEN-LAST:event_jButton1MousePressed
    
    private void TablasParametros(){
        
        jTable_Semana.getTableHeader().setReorderingAllowed(false);
        jTable_Semana.setValueAt("08:00 - 10:00",0,0);
        jTable_Semana.setValueAt("10:00 - 12:00",1,0);
        jTable_Semana.setValueAt("12:00 - 14:00",2,0);
        jTable_Semana.setValueAt("14:00 - 16:00",3,0);
        jTable_Semana.setValueAt("16:00 - 18:00",4,0);
        jTable_Semana.setValueAt("18:00 - 20:00",5,0);
        
        TableColumn columna  = null;
        TableColumn columna2 = null;
        
        //Columna Hora.
         columna = jTable_Semana.getColumnModel().getColumn(0);
         
         columna.setPreferredWidth(60);
         columna.setResizable(false);
         
        //Columna Lunes
         columna = jTable_Semana.getColumnModel().getColumn(1);
         columna.setPreferredWidth(80);
         columna.setResizable(false);
        //Columna Martes.
         columna = jTable_Semana.getColumnModel().getColumn(2);
         columna.setPreferredWidth(80);
         columna.setResizable(false);
        //Columna Miercoles.
         columna = jTable_Semana.getColumnModel().getColumn(3);
         columna.setPreferredWidth(80);
         columna.setResizable(false);
        //Columna Jueves.
         columna = jTable_Semana.getColumnModel().getColumn(4);
         columna.setPreferredWidth(80);
         columna.setResizable(false);
        //Columna Viernes. 
         columna = jTable_Semana.getColumnModel().getColumn(5);
         columna.setPreferredWidth(80);
         columna.setResizable(false);
         
         //TABLA DE DATOS. FINAL.
        jTabla_datos.getTableHeader().setReorderingAllowed(false);
        
        columna2 = jTabla_datos.getColumnModel().getColumn(0);
        columna2.setPreferredWidth(17);
        columna2.setResizable(false);
        
        columna2 = jTabla_datos.getColumnModel().getColumn(1);
        columna2.setPreferredWidth(128);
        columna2.setResizable(false);

        columna2 = jTabla_datos.getColumnModel().getColumn(2);
        columna2.setPreferredWidth(80);
        columna2.setResizable(false);

        columna2 = jTabla_datos.getColumnModel().getColumn(3);
        columna2.setPreferredWidth(60);
        columna2.setResizable(false);
        
        columna2 = jTabla_datos.getColumnModel().getColumn(4);
        columna2.setPreferredWidth(60);
        columna2.setResizable(false);
        
        for(int x = 0 ; x < jTabla_datos.getRowCount(); x++ )
            jTabla_datos.setValueAt(x + 1,x,0);
      }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new jProyecto1().setVisible(true);
                    
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
    
    private MyDBConnection mdbc;
    private java.sql.Statement stmt;
    private ArrayList profeDia;
    private ArrayList profebloque;
          
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jAsignatura;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jCampo1;
    private javax.swing.JTextField jCampo2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JTextField jHoras;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JComboBox jListaAsignaturas;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JTextField jNombre;
    private javax.swing.JTextField jNombreCurso;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton jPedir;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTabla_datos;
    private javax.swing.JTable jTable_Semana;
    // End of variables declaration//GEN-END:variables


    
}
