Java tutorial
/* * Copyright 2014 Cristiam Mercado. * * Esta obra est licenciada bajo la Licencia: * Creative Commons Atribucin-NoComercial-SinDerivar 4.0 Internacional. * * Para ver una copia de esta licencia, visita: * http://creativecommons.org/licenses/by-nc-nd/4.0/. */ package com.cmsoftware.keyron.vista.admin; import com.cmsoftware.keyron.controlador.Log; import com.cmsoftware.keyron.controlador.UsuarioFunciones; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.JDesktopPane; import javax.swing.JInternalFrame; import javax.swing.JOptionPane; import org.json.JSONObject; /** * Ventana para activar un empleado. * * @author Cristiam Mercado cristiammercadoj@gmail.com * @version 1.0 */ public class ActivarEmpleado extends javax.swing.JDialog { private boolean activando = false; private final String cedulaEmpleado; private final JDesktopPane escritorio; /** * Constructor para esta clase. * * @param parent Frame padre. * @param modal deberia ser true. * @param cedulaEmpleado Cdula del empleado a activar. * @param escritorio Escritorio en donde se muestran todas las ventanas. */ public ActivarEmpleado(java.awt.Frame parent, boolean modal, String cedulaEmpleado, JDesktopPane escritorio) { super(parent, modal); this.cedulaEmpleado = cedulaEmpleado; this.escritorio = escritorio; initComponents(); setLocationRelativeTo(null); } public String getCedulaEmpleado() { return cedulaEmpleado; } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { panelFondo = new javax.swing.JPanel(); labelError = new javax.swing.JLabel(); panelDatos = new javax.swing.JPanel(); aceptar = new javax.swing.JButton(); label_correo = new javax.swing.JLabel(); label_clave = new javax.swing.JLabel(); correo = new javax.swing.JTextField(); clave = new javax.swing.JPasswordField(); cancelar = new javax.swing.JButton(); separador = new javax.swing.JLabel(); mensajeAcceso = new javax.swing.JLabel(); label_titulo = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Cambiar Clave"); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); panelFondo.setBackground(new java.awt.Color(119, 41, 83)); labelError.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N labelError.setForeground(new java.awt.Color(255, 255, 255)); labelError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); aceptar.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N aceptar.setForeground(new java.awt.Color(89, 89, 89)); aceptar.setText("Aceptar"); aceptar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); aceptar.setMargin(new java.awt.Insets(6, 14, 6, 14)); aceptar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { aceptarActionPerformed(evt); } }); label_correo.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N label_correo.setForeground(new java.awt.Color(89, 89, 89)); label_correo.setText("Correo:"); label_clave.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N label_clave.setForeground(new java.awt.Color(89, 89, 89)); label_clave.setText("Clave:"); correo.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); correo.setForeground(new java.awt.Color(89, 89, 89)); correo.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { correoKeyTyped(evt); } }); clave.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N clave.setForeground(new java.awt.Color(89, 89, 89)); clave.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { claveKeyTyped(evt); } }); cancelar.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N cancelar.setForeground(new java.awt.Color(89, 89, 89)); cancelar.setText("Cancelar"); cancelar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); cancelar.setMargin(new java.awt.Insets(6, 14, 6, 14)); cancelar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelarActionPerformed(evt); } }); separador.setForeground(new java.awt.Color(119, 41, 83)); separador.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); separador.setText("__________________________________________________________________"); mensajeAcceso.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N mensajeAcceso.setForeground(new java.awt.Color(89, 89, 89)); mensajeAcceso.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); mensajeAcceso.setText("Para confirmar la activacin, por favor confirme su sesin"); javax.swing.GroupLayout panelDatosLayout = new javax.swing.GroupLayout(panelDatos); panelDatos.setLayout(panelDatosLayout); panelDatosLayout.setHorizontalGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelDatosLayout.createSequentialGroup().addContainerGap().addGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(mensajeAcceso, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE) .addGroup(panelDatosLayout.createSequentialGroup().addGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(panelDatosLayout.createSequentialGroup().addComponent(aceptar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cancelar)) .addGroup(panelDatosLayout.createSequentialGroup().addGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(label_clave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(label_correo, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panelDatosLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(correo, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE) .addComponent(clave)))) .addComponent(separador, javax.swing.GroupLayout.PREFERRED_SIZE, 447, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); panelDatosLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { aceptar, cancelar }); panelDatosLayout.setVerticalGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelDatosLayout.createSequentialGroup().addGap(20, 20, 20).addComponent(mensajeAcceso) .addGap(11, 11, 11).addComponent(separador).addGap(18, 18, 18) .addGroup(panelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(label_correo, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(correo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(label_clave, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(clave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(panelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(aceptar).addComponent(cancelar)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); panelDatosLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] { clave, correo }); label_titulo .setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(1, 16f)); label_titulo.setForeground(new java.awt.Color(255, 255, 255)); label_titulo.setIcon(new javax.swing.ImageIcon( getClass().getResource("/com/cmsoftware/keyron/recursos/cambiar_clave.png"))); // NOI18N label_titulo.setText("Activar Empleado"); javax.swing.GroupLayout panelFondoLayout = new javax.swing.GroupLayout(panelFondo); panelFondo.setLayout(panelFondoLayout); panelFondoLayout.setHorizontalGroup(panelFondoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(label_titulo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE) .addComponent(panelDatos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); panelFondoLayout.setVerticalGroup(panelFondoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelFondoLayout.createSequentialGroup() .addComponent(label_titulo, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE) .addComponent(panelDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0).addComponent(labelError, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panelFondo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( panelFondo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents private void aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarActionPerformed this.activarEmpleado(); }//GEN-LAST:event_aceptarActionPerformed private void correoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_correoKeyTyped correo.setBackground(new Color(255, 255, 255)); if (KeyEvent.VK_ENTER == evt.getKeyChar()) { activarEmpleado(); } else { labelError.setText(""); } }//GEN-LAST:event_correoKeyTyped private void claveKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_claveKeyTyped clave.setBackground(new Color(255, 255, 255)); if (KeyEvent.VK_ENTER == evt.getKeyChar()) { activarEmpleado(); } else { labelError.setText(""); } }//GEN-LAST:event_claveKeyTyped private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed this.dispose(); }//GEN-LAST:event_cancelarActionPerformed private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing cerrar(); }//GEN-LAST:event_formWindowClosing /** * Cierra esta ventana. */ public void cerrar() { if (!activando) { dispose(); } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(() -> { ActivarEmpleado dialog = new ActivarEmpleado(new javax.swing.JFrame(), true, "", new javax.swing.JDesktopPane()); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); }); } /** * Activa todos los campos y botones. */ public void activarCampos() { correo.setEnabled(true); clave.setEnabled(true); aceptar.setEnabled(true); cancelar.setEnabled(true); } /** * Desactiva todos los campos y botones.F */ public void desactivarCampos() { correo.setEnabled(false); clave.setEnabled(false); aceptar.setEnabled(false); cancelar.setEnabled(false); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton aceptar; private javax.swing.JButton cancelar; private javax.swing.JPasswordField clave; private javax.swing.JTextField correo; private javax.swing.JLabel labelError; private javax.swing.JLabel label_clave; private javax.swing.JLabel label_correo; private javax.swing.JLabel label_titulo; private javax.swing.JLabel mensajeAcceso; private javax.swing.JPanel panelDatos; private javax.swing.JPanel panelFondo; private javax.swing.JLabel separador; // End of variables declaration//GEN-END:variables /** * Verifica los campos rellenos y actualiza la clave del usuario. */ private void activarEmpleado() { labelError.setText(""); if (correo.getText().trim().isEmpty() || correo.getText().trim().length() < 5) { labelError.setText("El campo 'Correo' no es vlido"); correo.requestFocus(); correo.setBackground(new Color(253, 194, 194)); } else if (String.copyValueOf(clave.getPassword()).trim().isEmpty() || String.copyValueOf(clave.getPassword()).trim().length() < 6) { labelError.setText("El campo 'Clave' no es vlido"); clave.requestFocus(); clave.setBackground(new Color(253, 194, 194)); } else { correo.setEnabled(false); clave.setEnabled(false); aceptar.setEnabled(false); cancelar.setEnabled(false); labelError.setText("Activando empleado ..."); labelError.setIcon(new javax.swing.ImageIcon( getClass().getResource("/com/cmsoftware/keyron/recursos/cargando.gif"))); activando = true; new ActivaEmpleado().start(); } } /** * Actualiza la lista de empleados de dicha ventana si est abierta. */ public void actualizarListaEmpleado() { Thread t = new Thread(() -> { JInternalFrame[] ventanas = escritorio.getAllFrames(); for (int i = 0; i < ventanas.length; i++) { if ("com.cmsoftware.keyron.vista.admin.ListarEmpleado".equals(ventanas[i].getClass().getName())) { ListarEmpleado l = (ListarEmpleado) ventanas[i]; l.actualizarModeloTabla(0, null); break; } } }); t.start(); } /** * Clase privada que gestiona la activacion de un empleado. */ private class ActivaEmpleado extends Thread { @Override public void run() { try { UsuarioFunciones uf = new UsuarioFunciones(); JSONObject json = uf.activarUsuario(correo.getText(), String.copyValueOf(clave.getPassword()), getCedulaEmpleado()); if (json != null) { int key_error = json.getInt("error"); if (key_error == 0) { activando = false; labelError.setText(""); labelError.setIcon(null); actualizarListaEmpleado(); JOptionPane.showMessageDialog(null, "El empleado fue activado exitosamente", "Operacin Exitosa - Keyron", JOptionPane.INFORMATION_MESSAGE); cerrar(); } else if (key_error == 1) { activarCampos(); activando = false; labelError.setText("El correo electrnico no existe"); labelError.setIcon(null); correo.requestFocus(); correo.setBackground(new Color(253, 194, 194)); } else if (key_error == 2) { activarCampos(); activando = false; labelError.setText("Clave incorrecta"); clave.requestFocus(); clave.setBackground(new Color(253, 194, 194)); labelError.setIcon(null); } else if (key_error == 3 || key_error == 4) { activarCampos(); activando = false; labelError.setText("No se pudo ingresar al sistema. Intente nuevamente"); labelError.setIcon(null); } else if (key_error == 19) { activarCampos(); activando = false; labelError.setText("Esta cuenta no se encuentra activa"); correo.requestFocus(); correo.setBackground(new Color(253, 194, 194)); labelError.setIcon(null); } else if (key_error == 22) { activarCampos(); activando = false; labelError.setText("No se pudo activar el empleado. Intente nuevamente"); labelError.setIcon(null); } else { activarCampos(); activando = false; labelError.setText("Hubo un error al procesar la solicitud. Intente nuevamente"); labelError.setIcon(null); } } else { activarCampos(); activando = false; labelError.setText("Hubo un error al conectar. Revise su conexin a Internet"); labelError.setIcon(null); } } catch (Exception e) { labelError.setText("Se produjo un error al actualizar la informacin. Intente nuevamente"); activarCampos(); activando = false; labelError.setIcon(null); Log.agregarError(e); } } } }