Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.dmrr.asistenciasx; import au.com.bytecode.opencsv.CSVReader; import static com.dmrr.asistenciasx.SIIAUConnector.getFecha; import java.awt.Dimension; import java.io.ByteArrayInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.text.html.HTMLEditorKit; import org.apache.commons.lang.StringEscapeUtils; import org.eclipse.persistence.config.QueryHints; import org.eclipse.persistence.config.ResultType; import org.jdesktop.beansbinding.AutoBinding; import org.jdesktop.beansbinding.ELProperty; import org.jdesktop.swingbinding.JTableBinding; import org.jdesktop.swingbinding.SwingBindings; import org.jsoup.Jsoup; import org.jsoup.nodes.Attribute; import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; /** * * @author diego */ public class Horarios extends javax.swing.JFrame { /** * Creates new form ReasignarCursos */ public Horarios() { this.setExtendedState(JFrame.MAXIMIZED_BOTH); initComponents(); } Main parent; Horarios(Main aThis) { this(); parent = aThis; } /** * 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTableHorarios = new javax.swing.JTable(); jButtonReasignar = new javax.swing.JButton(); jButtonObtener = new javax.swing.JButton(); jTextFieldFiltro = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jTableHorarios .setModel(new javax.swing.table.DefaultTableModel(new Object[][] { {}, {}, {}, {} }, new String[] { })); jScrollPane1.setViewportView(jTableHorarios); jButtonReasignar.setText("Reasignar a otro profesor"); jButtonReasignar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonReasignarActionPerformed(evt); } }); jButtonObtener.setText("Obtener"); jButtonObtener.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonObtenerActionPerformed(evt); } }); jButton1.setText("Horarios de profesor en SIIAU"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(jButton1) .addGap(18, 18, 18).addComponent(jButtonReasignar)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jTextFieldFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonObtener))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonObtener).addComponent(jTextFieldFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE) .addGap(9, 9, 9) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonReasignar).addComponent(jButton1)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents FiltroDeTabla filtro; private void jButtonObtenerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonObtenerActionPerformed loadHorarios(); filtro = new FiltroDeTabla(jTableHorarios, jTextFieldFiltro); }//GEN-LAST:event_jButtonObtenerActionPerformed HorariosReasignarPantallaListaDeProfesores ventanaReasignarCurso; private void jButtonReasignarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonReasignarActionPerformed int x = jTableHorarios.getSelectedRow(); if (x == -1) { JOptionPane.showMessageDialog(this, "Seleccione un profesor primero", "Datos incompletos", JOptionPane.WARNING_MESSAGE); return; } Integer idCurso = Integer.parseInt((String) jTableHorarios.getValueAt(jTableHorarios.getSelectedRow(), 0)); String nombreProfesor = (String) jTableHorarios.getValueAt(jTableHorarios.getSelectedRow(), 2); ventanaReasignarCurso = new HorariosReasignarPantallaListaDeProfesores(idCurso, nombreProfesor, this); ventanaReasignarCurso.setVisible(true); }//GEN-LAST:event_jButtonReasignarActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed try { int x = jTableHorarios.getSelectedRow(); if (x == -1) { JOptionPane.showMessageDialog(this, "Seleccione un profesor primero", "Datos incompletos", JOptionPane.WARNING_MESSAGE); return; } Integer idProfesor = Integer .parseInt((String) jTableHorarios.getValueAt(jTableHorarios.getSelectedRow(), 1)); JPasswordField pf = new JPasswordField(); String nip = ""; int okCxl = JOptionPane.showConfirmDialog(null, pf, "Introduzca el NIP del jefe del departamento", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (okCxl == JOptionPane.OK_OPTION) { nip = new String(pf.getPassword()); } else { return; } org.jsoup.Connection.Response respuesta = Jsoup .connect("http://siiauescolar.siiau.udg.mx/wus/gupprincipal.valida_inicio") .data("p_codigo_c", "2225255", "p_clave_c", nip).method(org.jsoup.Connection.Method.POST) .timeout(0).execute(); Document login = respuesta.parse(); String sessionId = respuesta.cookie(getFecha() + "SIIAUSESION"); String sessionId2 = respuesta.cookie(getFecha() + "SIIAUUDG"); Document listaHorarios = Jsoup.connect("http://siiauescolar.siiau.udg.mx/wse/sspsecc.consulta_oferta") .data("ciclop", "201510", "cup", "J", "deptop", "", "codprofp", "" + idProfesor, "ordenp", "0", "mostrarp", "1000", "tipop", "T", "secp", "A", "regp", "T") .userAgent("Mozilla").cookie(getFecha() + "SIIAUSESION", sessionId) .cookie(getFecha() + "SIIAUUDG", sessionId2).timeout(0).post(); Elements tabla = listaHorarios.select("body"); tabla.select("style").remove(); Elements font = tabla.select("font"); font.removeAttr("size"); System.out.println(tabla.html()); JEditorPane jEditorPane = new JEditorPane(); jEditorPane.setEditable(false); HTMLEditorKit kit = new HTMLEditorKit(); jEditorPane.setEditorKit(kit); javax.swing.text.Document doc = kit.createDefaultDocument(); jEditorPane.setDocument(doc); jEditorPane.setText(tabla.html()); JOptionPane.showMessageDialog(null, jEditorPane); } catch (IOException ex) { Logger.getLogger(Horarios.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton1ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Horarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Horarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Horarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Horarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Horarios().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButtonObtener; private javax.swing.JButton jButtonReasignar; private javax.swing.JScrollPane jScrollPane1; public javax.swing.JTable jTableHorarios; public javax.swing.JTextField jTextFieldFiltro; // End of variables declaration//GEN-END:variables private javax.persistence.EntityManager em; private javax.persistence.Query registrosQuery; private org.jdesktop.beansbinding.BindingGroup bindingGroup; private List<Map> listaDeRegistros; void loadHorarios() { bindingGroup = new org.jdesktop.beansbinding.BindingGroup(); try { em = javax.persistence.Persistence .createEntityManagerFactory("asistenciasx?zeroDateTimeBehavior=convertToNullPU") .createEntityManager(); } catch (Exception e) { JOptionPane.showMessageDialog(rootPane, "Cheque la conexion a la base de datos."); } registrosQuery = em.createNativeQuery("SELECT * FROM asistenciasx.horarios"); registrosQuery.setHint(QueryHints.RESULT_TYPE, ResultType.Map); listaDeRegistros = registrosQuery.getResultList(); JTableBinding jTableBinding; jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, listaDeRegistros, jTableHorarios); JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${idcurso}")); columnBinding.setColumnName("NRC"); columnBinding.setColumnClass(String.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${idprofesor}")); columnBinding.setColumnName("IDProfesor"); columnBinding.setColumnClass(String.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${Profesor}")); columnBinding.setColumnName("Profesor"); columnBinding.setColumnClass(String.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${Materia}")); columnBinding.setColumnName("Materia"); columnBinding.setColumnClass(String.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${horaEntrada}")); columnBinding.setColumnName("Entrada"); columnBinding.setColumnClass(String.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${horaSalida}")); columnBinding.setColumnName("Salida"); columnBinding.setColumnClass(String.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${lun}")); columnBinding.setColumnName("L"); columnBinding.setColumnClass(Boolean.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${mar}")); columnBinding.setColumnName("M"); columnBinding.setColumnClass(Boolean.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${mie}")); columnBinding.setColumnName("M"); columnBinding.setColumnClass(Boolean.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${jue}")); columnBinding.setColumnName("J"); columnBinding.setColumnClass(Boolean.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${vie}")); columnBinding.setColumnName("V"); columnBinding.setColumnClass(Boolean.class); columnBinding = jTableBinding.addColumnBinding(ELProperty.create("${sab}")); columnBinding.setColumnName("S"); columnBinding.setColumnClass(Boolean.class); bindingGroup.addBinding(jTableBinding); bindingGroup.bind(); jTableHorarios.getColumnModel().getColumn(0).setPreferredWidth(150); jTableHorarios.getColumnModel().getColumn(2).setPreferredWidth(250); jTableHorarios.getColumnModel().getColumn(3).setPreferredWidth(350); } }