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.AdministradorFunciones; import com.cmsoftware.keyron.controlador.Log; import com.cmsoftware.keyron.modelo.Empleado; import java.awt.Color; import java.awt.Toolkit; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JDesktopPane; import javax.swing.JInternalFrame; import javax.swing.JOptionPane; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter; import org.json.JSONException; import org.json.JSONObject; /** * Ventana que muestra las opciones para agregar un nuevo empleado. * * @author Cristiam Mercado cristiammercadoj@gmail.com * @version 1.0 */ public class AgregarEmpleado extends javax.swing.JInternalFrame { private Empleado empleado = new Empleado(); /** * Constructor por defecto para la ventana AgregarEmpleado. */ public AgregarEmpleado() { initComponents(); ((AbstractDocument) cedula.getDocument()).setDocumentFilter(new FiltroNumerico()); ((AbstractDocument) telefono.getDocument()).setDocumentFilter(new FiltroNumerico()); cedula.requestFocus(); } /** * Comprueba si la cadena es un valor numrico o no. * * @param cadena Texto a comprobar si es numrico o no. * @return true si lo es, false en caso contrario. */ public boolean esNumero(String cadena) { cadena = cadena.trim(); try { Long.parseLong(cadena); return true; } catch (NumberFormatException e) { return false; } } /** * Comprueba si la cadena es un correo vlido o no. * * @param cadena Texto a comprobar si es un correo vlido o no. * @return true si lo es, false en caso contrario. */ public boolean esCorreo(String cadena) { String PATTERN_EMAIL = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; Pattern pattern = Pattern.compile(PATTERN_EMAIL); Matcher matcher = pattern.matcher(cadena); return matcher.matches(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { panel_titulo = new javax.swing.JPanel(); label_titulo = new javax.swing.JLabel(); label_logo = new javax.swing.JLabel(); panel_contenido = new javax.swing.JPanel(); cedula = new javax.swing.JTextField(); labelCedula = new javax.swing.JLabel(); labelNombre = new javax.swing.JLabel(); nombre = new javax.swing.JTextField(); labelApellido = new javax.swing.JLabel(); apellido = new javax.swing.JTextField(); labelDireccion = new javax.swing.JLabel(); direccion = new javax.swing.JTextField(); labelTelefono = new javax.swing.JLabel(); telefono = new javax.swing.JTextField(); labelEmail = new javax.swing.JLabel(); email = new javax.swing.JTextField(); labelNivel = new javax.swing.JLabel(); labelNivel1 = new javax.swing.JLabel(); guardar = new javax.swing.JButton(); cancelar = new javax.swing.JButton(); panelPie = new javax.swing.JPanel(); labelError = new javax.swing.JLabel(); setClosable(true); setForeground(java.awt.Color.white); setIconifiable(true); setTitle("Agregar Empleado"); setMaximumSize(new java.awt.Dimension(570, 438)); setMinimumSize(new java.awt.Dimension(570, 438)); setPreferredSize(new java.awt.Dimension(570, 438)); setRequestFocusEnabled(false); panel_titulo.setBackground(new java.awt.Color(119, 41, 83)); panel_titulo.setPreferredSize(new java.awt.Dimension(534, 47)); 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/encabezado_add_empleado.png"))); // NOI18N label_titulo.setText("Agregar Empleado"); label_logo.setIcon(new javax.swing.ImageIcon( getClass().getResource("/com/cmsoftware/keyron/recursos/iconoBarra.png"))); // NOI18N javax.swing.GroupLayout panel_tituloLayout = new javax.swing.GroupLayout(panel_titulo); panel_titulo.setLayout(panel_tituloLayout); panel_tituloLayout.setHorizontalGroup( panel_tituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel_tituloLayout.createSequentialGroup().addGap(30, 30, 30) .addComponent(label_titulo, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(label_logo).addGap(32, 32, 32))); panel_tituloLayout .setVerticalGroup(panel_tituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(label_titulo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_tituloLayout.createSequentialGroup() .addComponent(label_logo, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))); panel_contenido.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(119, 41, 83))); cedula.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); cedula.setToolTipText("Ingrese el nmero de Cdula"); cedula.setBorder(javax.swing.BorderFactory.createEtchedBorder()); cedula.setMargin(new java.awt.Insets(3, 3, 3, 3)); cedula.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { cedulaKeyTyped(evt); } }); labelCedula.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N labelCedula.setForeground(new java.awt.Color(90, 90, 90)); labelCedula.setText("Cdula:"); labelNombre.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N labelNombre.setForeground(new java.awt.Color(90, 90, 90)); labelNombre.setText("Nombres:"); nombre.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); nombre.setToolTipText("Ingrese los nombres"); nombre.setBorder(javax.swing.BorderFactory.createEtchedBorder()); nombre.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { nombreKeyTyped(evt); } }); labelApellido.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N labelApellido.setForeground(new java.awt.Color(90, 90, 90)); labelApellido.setText("Apellidos:"); apellido.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); apellido.setToolTipText("Ingrese los apellidos"); apellido.setBorder(javax.swing.BorderFactory.createEtchedBorder()); apellido.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { apellidoKeyTyped(evt); } }); labelDireccion.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N labelDireccion.setForeground(new java.awt.Color(90, 90, 90)); labelDireccion.setText("Direccin:"); direccion.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); direccion.setToolTipText("Ingrese la direccin"); direccion.setBorder(javax.swing.BorderFactory.createEtchedBorder()); direccion.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { direccionKeyTyped(evt); } }); labelTelefono.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N labelTelefono.setForeground(new java.awt.Color(90, 90, 90)); labelTelefono.setText("Telfono:"); telefono.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); telefono.setToolTipText("Ingrese nmero de telfono"); telefono.setBorder(javax.swing.BorderFactory.createEtchedBorder()); telefono.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { telefonoKeyTyped(evt); } }); labelEmail.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N labelEmail.setForeground(new java.awt.Color(90, 90, 90)); labelEmail.setText("E-mail:"); email.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); email.setToolTipText("Ingrese un E-mail vlido"); email.setBorder(javax.swing.BorderFactory.createEtchedBorder()); email.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { emailKeyTyped(evt); } }); labelNivel.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N labelNivel.setForeground(new java.awt.Color(90, 90, 90)); labelNivel.setText("Nivel:"); labelNivel1 .setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); labelNivel1.setForeground(new java.awt.Color(90, 90, 90)); labelNivel1.setText("EMPLEADO"); labelNivel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); guardar.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N guardar.setForeground(new java.awt.Color(89, 89, 89)); guardar.setText("Guardar"); guardar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); guardar.setMargin(new java.awt.Insets(6, 14, 6, 14)); guardar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { guardarActionPerformed(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); } }); javax.swing.GroupLayout panel_contenidoLayout = new javax.swing.GroupLayout(panel_contenido); panel_contenido.setLayout(panel_contenidoLayout); panel_contenidoLayout.setHorizontalGroup( panel_contenidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel_contenidoLayout.createSequentialGroup().addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_contenidoLayout.createSequentialGroup().addGap(330, 330, 330) .addComponent(guardar) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cancelar)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_contenidoLayout .createSequentialGroup().addGap(28, 28, 28) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel_contenidoLayout.createSequentialGroup() .addComponent(telefono, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(email)) .addComponent(direccion) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_contenidoLayout.createSequentialGroup().addGroup( panel_contenidoLayout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(nombre).addComponent(cedula) .addGroup(panel_contenidoLayout .createSequentialGroup() .addGroup(panel_contenidoLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent( labelDireccion, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelNombre, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelCedula, javax.swing.GroupLayout.Alignment.LEADING) .addGroup( javax.swing.GroupLayout.Alignment.LEADING, panel_contenidoLayout .createSequentialGroup() .addComponent( labelTelefono) .addGap(125, 125, 125) .addComponent( labelEmail))) .addGap(0, 0, Short.MAX_VALUE))) .addGap(18, 18, 18) .addGroup(panel_contenidoLayout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelNivel) .addGroup(panel_contenidoLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(labelApellido) .addComponent( labelNivel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(apellido, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))))))) .addGap(28, 28, 28))); panel_contenidoLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { cancelar, guardar }); panel_contenidoLayout.setVerticalGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel_contenidoLayout.createSequentialGroup().addContainerGap() .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(labelCedula).addComponent(labelNivel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(cedula).addComponent(labelNivel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(labelNombre).addComponent(labelApellido)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(apellido, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(labelDireccion) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(direccion, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel_contenidoLayout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelEmail).addComponent(labelTelefono)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(telefono, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(66, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_contenidoLayout .createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(guardar).addComponent(cancelar)) .addContainerGap())))); panelPie.setBackground(new java.awt.Color(119, 41, 83)); panelPie.setPreferredSize(new java.awt.Dimension(534, 47)); 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); labelError.setMaximumSize(new java.awt.Dimension(534, 40)); labelError.setMinimumSize(new java.awt.Dimension(534, 40)); labelError.setPreferredSize(new java.awt.Dimension(534, 40)); javax.swing.GroupLayout panelPieLayout = new javax.swing.GroupLayout(panelPie); panelPie.setLayout(panelPieLayout); panelPieLayout .setHorizontalGroup(panelPieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); panelPieLayout .setVerticalGroup(panelPieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelError, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panel_contenido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(panelPie, javax.swing.GroupLayout.DEFAULT_SIZE, 556, Short.MAX_VALUE) .addComponent(panel_titulo, javax.swing.GroupLayout.DEFAULT_SIZE, 556, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(panel_titulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0) .addComponent(panel_contenido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0) .addComponent(panelPie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void cedulaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cedulaKeyTyped labelError.setText(""); cedula.setBackground(new Color(255, 255, 255)); if (evt.getKeyChar() < '0' || evt.getKeyChar() > '9') { evt.consume(); Toolkit.getDefaultToolkit().beep(); } }//GEN-LAST:event_cedulaKeyTyped private void telefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_telefonoKeyTyped labelError.setText(""); telefono.setBackground(new Color(255, 255, 255)); if (evt.getKeyChar() == '\n') { guardarActionPerformed(null); } if (evt.getKeyChar() < '0' || evt.getKeyChar() > '9') { evt.consume(); Toolkit.getDefaultToolkit().beep(); } }//GEN-LAST:event_telefonoKeyTyped private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed if (validarCampos()) { String mensaje = "<html>"; mensaje += "<font size=4>Desea agregar el empleado con los siguientes datos?</font><br><br><hr><br>"; mensaje += "<font size=4><b>Cdula: </b>" + cedula.getText().trim().toLowerCase() + "</font><br>"; mensaje += "<font size=4><b>Nombre: </b>" + nombre.getText().trim().toLowerCase() + "</font><br>"; mensaje += "<font size=4><b>Apellido: </b>" + apellido.getText().trim().toLowerCase() + "</font><br>"; mensaje += "<font size=4><b>Direccin: </b>" + direccion.getText().trim().toLowerCase() + "</font><br>"; mensaje += "<font size=4><b>Telfono: </b>" + telefono.getText().trim().toLowerCase() + "</font><br>"; mensaje += "<font size=4><b>Correo: </b>" + email.getText().trim().toLowerCase() + "</font><br><br><hr><br>"; mensaje += "</html>"; int respuesta = JOptionPane.showConfirmDialog(null, mensaje, "Confirmar Operacin - Keyron", JOptionPane.OK_CANCEL_OPTION); if (respuesta == 0) { deshabilitarCampos(); guardarEmpleado(); } } }//GEN-LAST:event_guardarActionPerformed private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed this.dispose(); }//GEN-LAST:event_cancelarActionPerformed private void nombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nombreKeyTyped labelError.setText(""); nombre.setBackground(new Color(255, 255, 255)); if (evt.getKeyChar() == '\n') { guardarActionPerformed(null); } }//GEN-LAST:event_nombreKeyTyped private void apellidoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_apellidoKeyTyped labelError.setText(""); apellido.setBackground(new Color(255, 255, 255)); if (evt.getKeyChar() == '\n') { guardarActionPerformed(null); } }//GEN-LAST:event_apellidoKeyTyped private void direccionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_direccionKeyTyped labelError.setText(""); direccion.setBackground(new Color(255, 255, 255)); if (evt.getKeyChar() == '\n') { guardarActionPerformed(null); } }//GEN-LAST:event_direccionKeyTyped private void emailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailKeyTyped labelError.setText(""); email.setBackground(new Color(255, 255, 255)); if (evt.getKeyChar() == '\n') { guardarActionPerformed(null); } }//GEN-LAST:event_emailKeyTyped /** * Valida la informacin de los campos editados. * * @return true si cumple con todas las validaciones, o false en caso * contrario. */ private boolean validarCampos() { if (cedula.getText().trim().isEmpty()) { labelError.setText("Campo 'Cdula' vaco. Verifique."); cedula.requestFocus(); cedula.setBackground(new Color(253, 194, 194)); return false; } if (nombre.getText().trim().isEmpty()) { labelError.setText("Campo 'Nombres' vaco. Verifique."); nombre.requestFocus(); nombre.setBackground(new Color(253, 194, 194)); return false; } if (apellido.getText().trim().isEmpty()) { labelError.setText("Campo 'Apellidos' vaco. Verifique."); apellido.requestFocus(); apellido.setBackground(new Color(253, 194, 194)); return false; } if (direccion.getText().trim().isEmpty()) { labelError.setText("Campo 'Direccin' vaco. Verifique."); direccion.requestFocus(); direccion.setBackground(new Color(253, 194, 194)); return false; } if (telefono.getText().trim().isEmpty()) { labelError.setText("Campo 'Telfono' vaco. Verifique."); telefono.requestFocus(); telefono.setBackground(new Color(253, 194, 194)); return false; } if (email.getText().trim().isEmpty()) { labelError.setText("Campo 'E-mail' vaco. Verifique."); email.requestFocus(); email.setBackground(new Color(253, 194, 194)); return false; } else { if (!esCorreo(email.getText())) { labelError.setText("Campo 'E-mail' no vlido. Verifique."); email.requestFocus(); email.setBackground(new Color(253, 194, 194)); return false; } } return true; } /** * Inicializa los componentes necesarios para agregar un nuevo empleado. */ private void guardarEmpleado() { empleado.setCedula(cedula.getText()); empleado.setNombre(nombre.getText().trim().toLowerCase(Locale.ENGLISH)); empleado.setApellido(apellido.getText().trim().toLowerCase(Locale.ENGLISH)); empleado.setDireccion(direccion.getText().trim().toLowerCase(Locale.ENGLISH)); empleado.setTelefono(telefono.getText()); empleado.setCorreo(email.getText().trim().toLowerCase(Locale.ENGLISH)); empleado.setNivel("EMPLEADO"); empleado.setActivo("NO"); labelError.setText("Agregando empleado..."); labelError.setIcon( new javax.swing.ImageIcon(getClass().getResource("/com/cmsoftware/keyron/recursos/cargando.gif"))); new AgregarEmp().start(); } /** * Deshabilita todos los campos editables y botones. */ private void deshabilitarCampos() { cedula.setEnabled(false); nombre.setEnabled(false); apellido.setEnabled(false); direccion.setEnabled(false); telefono.setEnabled(false); email.setEnabled(false); guardar.setEnabled(false); cancelar.setEnabled(false); } /** * Habilita todos los campos editables y botones. */ private void habilitarCampos() { cedula.setEnabled(true); nombre.setEnabled(true); apellido.setEnabled(true); direccion.setEnabled(true); telefono.setEnabled(true); email.setEnabled(true); guardar.setEnabled(true); cancelar.setEnabled(true); } /** * Cierra la ventana. */ public void cerrarVentana() { this.dispose(); } /** * Actualiza la lista de empleados de dicha ventana si est abierta. */ public void actualizarListaEmpleado() { Thread t = new Thread(() -> { JDesktopPane escritorio = getDesktopPane(); 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(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField apellido; private javax.swing.JButton cancelar; private javax.swing.JTextField cedula; private javax.swing.JTextField direccion; private javax.swing.JTextField email; private javax.swing.JButton guardar; private javax.swing.JLabel labelApellido; private javax.swing.JLabel labelCedula; private javax.swing.JLabel labelDireccion; private javax.swing.JLabel labelEmail; private javax.swing.JLabel labelError; private javax.swing.JLabel labelNivel; private javax.swing.JLabel labelNivel1; private javax.swing.JLabel labelNombre; private javax.swing.JLabel labelTelefono; private javax.swing.JLabel label_logo; private javax.swing.JLabel label_titulo; private javax.swing.JTextField nombre; private javax.swing.JPanel panelPie; private javax.swing.JPanel panel_contenido; private javax.swing.JPanel panel_titulo; private javax.swing.JTextField telefono; // End of variables declaration//GEN-END:variables /** * Aplica un filtro nmerico, evitando copiar y pegar en los campos cadenas * de texto. */ private class FiltroNumerico extends DocumentFilter { @Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { if (esNumero(text)) { super.replace(fb, offset, length, text, attrs); } else { Toolkit.getDefaultToolkit().beep(); } } @Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { if (esNumero(string) || string.isEmpty()) { super.insertString(fb, offset, string, attr); } else { Toolkit.getDefaultToolkit().beep(); } } @Override public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException { super.remove(fb, offset, length); } } /** * Clase interna que inicializa el hilo para consultar el Web Service. * */ private class AgregarEmp extends Thread { @Override public void run() { try { AdministradorFunciones af = new AdministradorFunciones(); JSONObject json = af.agregarEmpleado(empleado); if (json != null) { int exito = json.getInt("exito"); int key_error = json.getInt("error"); if (key_error == 0) { actualizarListaEmpleado(); labelError.setText("Empleado Agregado!"); labelError.setIcon(null); JOptionPane.showMessageDialog(null, "El empleado fue agregado exitosamente", "Operacin Exitosa - Keyron", JOptionPane.INFORMATION_MESSAGE); cerrarVentana(); } else if (key_error == 16) { habilitarCampos(); labelError.setText("La cdula ingresada ya existe."); labelError.setIcon(null); cedula.requestFocus(); cedula.setBackground(new Color(253, 194, 194)); empleado = new Empleado(); } else if (key_error == 17) { habilitarCampos(); labelError.setText("El correo ingresado ya est registrado."); email.requestFocus(); labelError.setIcon(null); email.setBackground(new Color(253, 194, 194)); empleado = new Empleado(); } else if (key_error == 4) { habilitarCampos(); labelError.setText("No es posible ejecutar la accin."); labelError.setIcon(null); empleado = new Empleado(); } else { habilitarCampos(); labelError.setText("Error al procesar la solicitud."); labelError.setIcon(null); empleado = new Empleado(); } } else { habilitarCampos(); labelError.setText("Hubo un error al conectar a internet. Verifique su conexin."); labelError.setIcon(null); empleado = new Empleado(); } } catch (JSONException e) { habilitarCampos(); labelError.setIcon(null); Log.agregarError(e); empleado = new Empleado(); } } } }