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.modelo.AdminDAO; import com.cmsoftware.keyron.controlador.AdministradorFunciones; import com.cmsoftware.keyron.modelo.Empleado; import com.cmsoftware.keyron.controlador.Log; import java.awt.Color; import java.awt.Toolkit; 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.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter; import org.json.JSONObject; /** * Ver detalladamente la informacin del empleado, adems de ofrecer otras * opciones. * * @author Cristiam Mercado cristiammercadoj@gmail.com * @version 1.0 */ public class EditarEmpleado extends javax.swing.JInternalFrame { private Empleado empleado; private InformacionEmpleado vd; /** * Constructor para la clase EditarEmpleado. * * @param c Cdula del empleado. */ public EditarEmpleado(String c) { initComponents(); cedula.setEnabled(false); cedula.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2)); nivel.setEnabled(false); nivel.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2)); nombre.requestFocus(); if (c.length() > 0) { empleado = new AdminDAO().getEmpleado(c); this.setInformacion(empleado); } else { } } /** * Modifica la informacin del empleado en los campos. * * @param empleado Instancia del empleado a editar. */ private void setInformacion(Empleado empleado) { cedula.setText(empleado.getCedula()); nombre.setText(empleado.getNombre()); apellido.setText(empleado.getApellido()); direccion.setText(empleado.getDireccion()); telefono.setText(empleado.getTelefono()); email.setText(empleado.getCorreo()); } @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(); 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(); nivel = new javax.swing.JLabel(); guardar = new javax.swing.JButton(); cancelar = new javax.swing.JButton(); restaurar = new javax.swing.JButton(); cedula = new javax.swing.JLabel(); panel_pie = new javax.swing.JPanel(); labelError = new javax.swing.JLabel(); setClosable(true); setIconifiable(true); setTitle("Editar Empleado"); setMaximumSize(new java.awt.Dimension(580, 490)); setMinimumSize(new java.awt.Dimension(580, 490)); setNormalBounds(new java.awt.Rectangle(0, 0, 91, 0)); setPreferredSize(new java.awt.Dimension(580, 490)); panel_titulo.setBackground(new java.awt.Color(119, 41, 83)); 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_edit_empleado.png"))); // NOI18N label_titulo.setText("Editar Informacin de 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(21, 21, 21) .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(25, 25, 25))); 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.setBackground(new java.awt.Color(246, 246, 246)); 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:"); nivel.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); nivel.setForeground(new java.awt.Color(90, 90, 90)); nivel.setText("EMPLEADO"); nivel.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); } }); restaurar.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N restaurar.setForeground(new java.awt.Color(89, 89, 89)); restaurar.setText("Restaurar"); restaurar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); restaurar.setMargin(new java.awt.Insets(6, 14, 6, 14)); restaurar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { restaurarActionPerformed(evt); } }); cedula.setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(0, 14f)); cedula.setForeground(new java.awt.Color(90, 90, 90)); cedula.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 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().addGap(27, 27, 27) .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(panel_contenidoLayout.createSequentialGroup() .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel_contenidoLayout.createSequentialGroup() .addComponent(labelTelefono).addGap(125, 125, 125) .addComponent(labelEmail)) .addGroup(panel_contenidoLayout.createSequentialGroup() .addComponent(guardar) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(restaurar))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 165, Short.MAX_VALUE) .addComponent(cancelar)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_contenidoLayout.createSequentialGroup().addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(nombre) .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)) .addGap(0, 0, Short.MAX_VALUE)) .addComponent( cedula, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panel_contenidoLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelNivel) .addGroup(panel_contenidoLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(labelApellido) .addComponent(nivel, 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(49, 49, 49))); panel_contenidoLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { guardar, restaurar }); panel_contenidoLayout.setVerticalGroup( panel_contenidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel_contenidoLayout.createSequentialGroup().addGap(19, 19, 19) .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.BASELINE) .addComponent(nivel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cedula, 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) .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)) .addGap(26, 26, 26) .addGroup(panel_contenidoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cancelar).addComponent(guardar).addComponent(restaurar)) .addContainerGap(33, Short.MAX_VALUE))); panel_contenidoLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] { cancelar, guardar, restaurar }); panel_pie.setBackground(new java.awt.Color(119, 41, 83)); labelError.setForeground(new java.awt.Color(255, 255, 255)); labelError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); javax.swing.GroupLayout panel_pieLayout = new javax.swing.GroupLayout(panel_pie); panel_pie.setLayout(panel_pieLayout); panel_pieLayout .setHorizontalGroup(panel_pieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); panel_pieLayout .setVerticalGroup(panel_pieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_pieLayout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent( labelError, javax.swing.GroupLayout.PREFERRED_SIZE, 47, 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(panel_contenido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(panel_pie, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(panel_titulo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(0, 0, 0).addComponent(panel_pie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); pack(); }// </editor-fold>//GEN-END:initComponents 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 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 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 private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed if (validarCampos()) { deshabilitarCampos(); guardarEmpleado(); } else { } }//GEN-LAST:event_guardarActionPerformed private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed cerrarVentana(); }//GEN-LAST:event_cancelarActionPerformed private void restaurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restaurarActionPerformed setInformacion(empleado); nombre.requestFocus(); }//GEN-LAST:event_restaurarActionPerformed /** * Crea el hilo que guarda las modificaciones de un nuevo empleado. */ public void guardarEmpleado() { Empleado e = new Empleado(cedula.getText().trim().toLowerCase(), nombre.getText().trim().toLowerCase(), apellido.getText().trim().toLowerCase(), direccion.getText().trim().toLowerCase(), telefono.getText().trim().toLowerCase(), email.getText().trim().toLowerCase(), "EMPLEADO", empleado.getFechaRegistro(), empleado.getActivo(), empleado.getFechaActivacion(), empleado.getFechaDesactivacion()); labelError.setText("Actualizando informacin de empleado..."); labelError.setIcon( new javax.swing.ImageIcon(getClass().getResource("/com/cmsoftware/keyron/recursos/cargando.gif"))); new ActualizarEmp(e).start(); } /** * 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; } } if (validarNuevosCambios()) { labelError.setText("No hay datos modificados en la informacin del empleado."); return false; } return true; } /** * Valida si en realidad se le hicieron cambios a la informacin del * empleado o no. Es til para evitar el trfico innecesario con el web * service. * * @return true si no tiene cambios, false en caso contrario. */ private boolean validarNuevosCambios() { boolean sinCambios = true; if (!empleado.getCedula().equalsIgnoreCase(cedula.getText().trim())) { sinCambios = false; } if (!empleado.getNombre().equalsIgnoreCase(nombre.getText().trim())) { sinCambios = false; } if (!empleado.getApellido().equalsIgnoreCase(apellido.getText().trim())) { sinCambios = false; } if (!empleado.getDireccion().equalsIgnoreCase(direccion.getText().trim())) { sinCambios = false; } if (!empleado.getTelefono().equalsIgnoreCase(telefono.getText().trim())) { sinCambios = false; } if (!empleado.getCorreo().equalsIgnoreCase(email.getText().trim())) { sinCambios = false; } return sinCambios; } /** * Valida si es un correo vlido. * * @param cadena Texto con correo a verificar. * @return true si es un correo vlido, o 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(); } /** * Deshabilita los campos y algunos botones para edicion. */ private void deshabilitarCampos() { nombre.setEnabled(false); apellido.setEnabled(false); direccion.setEnabled(false); telefono.setEnabled(false); email.setEnabled(false); guardar.setEnabled(false); restaurar.setEnabled(false); cancelar.setEnabled(false); } /** * Habilita los campos y algunos botones para edicion. */ private void habilitarCampos() { nombre.setEnabled(true); apellido.setEnabled(true); direccion.setEnabled(true); telefono.setEnabled(true); email.setEnabled(true); guardar.setEnabled(true); restaurar.setEnabled(true); cancelar.setEnabled(true); } /** * Cierra la ventana actual. */ private void cerrarVentana() { this.dispose(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField apellido; private javax.swing.JButton cancelar; private javax.swing.JLabel 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 labelNombre; private javax.swing.JLabel labelTelefono; private javax.swing.JLabel label_logo; private javax.swing.JLabel label_titulo; private javax.swing.JLabel nivel; private javax.swing.JTextField nombre; private javax.swing.JPanel panel_contenido; private javax.swing.JPanel panel_pie; private javax.swing.JPanel panel_titulo; private javax.swing.JButton restaurar; private javax.swing.JTextField telefono; // End of variables declaration//GEN-END:variables /** * 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(); } /** * 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; } } /** * Establece filtros especiales para los campos numricos. */ 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.insertString(fb, offset, text, attrs); } else { Toolkit.getDefaultToolkit().beep(); } } @Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { if (esNumero(string)) { 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); //To change body of generated methods, choose Tools | Templates. } } /** * Clase interna que inicializa el hilo para consultar el Web Service. * * @author Cristiam Mercado. */ private class ActualizarEmp extends Thread { private final Empleado em; /** * Constructor para este hilo. * * @param e Datos del empleado a actualizar. */ private ActualizarEmp(Empleado e) { this.em = e; } @Override public void run() { try { AdministradorFunciones af = new AdministradorFunciones(); JSONObject json = af.actualizarEmpleado(em); if (json != null) { int exito = json.getInt("exito"); int key_error = json.getInt("error"); if (key_error == 0) { JDesktopPane escritorio = getDesktopPane(); new AdminDAO().actualizarEmpleado(em); vd = new InformacionEmpleado(em.getCedula()); escritorio.add(vd); vd.setLocation(vd.getParent().getWidth() / 2 - vd.getWidth() / 2, vd.getParent().getHeight() / 2 - vd.getHeight() / 2 - 10); labelError.setText(""); labelError.setIcon(null); actualizarListaEmpleado(); JOptionPane.showMessageDialog(null, "El empleado fue editado exitosamente", "Operacin Exitosa - Keyron", JOptionPane.INFORMATION_MESSAGE); vd.setVisible(true); cerrarVentana(); } else if (key_error == 17) { habilitarCampos(); labelError.setText("El correo ingresado ya est registrado."); labelError.setIcon(null); nombre.requestFocus(); } else if (key_error == 20) { habilitarCampos(); labelError.setText("La cdula del empleado a actualizar no existe."); labelError.setIcon(null); nombre.requestFocus(); } else if (key_error == 4) { habilitarCampos(); labelError.setText("No es posible ejecutar la accin."); labelError.setIcon(null); } else { habilitarCampos(); labelError.setText("Error al procesar la solicitud."); labelError.setIcon(null); } } else { habilitarCampos(); labelError.setText("Hubo un error al conectar a internet. Verifique su conexin."); labelError.setIcon(null); } } catch (Exception e) { Log.agregarError(e); habilitarCampos(); labelError.setIcon(null); System.err.println("ERROR VerDetalle:756 -> " + e.toString()); } } } }