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.JOptionPane; import org.json.JSONObject; /** * Ventana para cambiar clave de acceso. * * @author Cristiam Mercado cristiammercadoj@gmail.com * @version 1.0 */ public class CambiarClave extends javax.swing.JDialog { private boolean actualizando = false; /** * Constructor para esta clase. * * @param parent Frame padre. * @param modal deberia ser true. */ public CambiarClave(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); setLocationRelativeTo(null); } @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(); label_nueva_clave = new javax.swing.JLabel(); label_repetir_nueva_clave = new javax.swing.JLabel(); correo = new javax.swing.JTextField(); clave = new javax.swing.JPasswordField(); nuevaClave = new javax.swing.JPasswordField(); repetirNuevaClave = new javax.swing.JPasswordField(); cancelar = new javax.swing.JButton(); label_pass = 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(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(1, 13f)); label_correo.setForeground(new java.awt.Color(89, 89, 89)); label_correo.setText("Correo:"); label_clave .setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(1, 13f)); label_clave.setForeground(new java.awt.Color(89, 89, 89)); label_clave.setText("Clave:"); label_nueva_clave .setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(1, 13f)); label_nueva_clave.setForeground(new java.awt.Color(89, 89, 89)); label_nueva_clave.setText("Nueva clave:"); label_repetir_nueva_clave .setFont(com.cmsoftware.keyron.controlador.Optimizacion.getInstancia().getFuente().MyFont(1, 13f)); label_repetir_nueva_clave.setForeground(new java.awt.Color(89, 89, 89)); label_repetir_nueva_clave.setText("Repetir nueva 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); } }); nuevaClave.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N nuevaClave.setForeground(new java.awt.Color(89, 89, 89)); nuevaClave.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { nuevaClaveKeyReleased(evt); } public void keyTyped(java.awt.event.KeyEvent evt) { nuevaClaveKeyTyped(evt); } }); repetirNuevaClave.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N repetirNuevaClave.setForeground(new java.awt.Color(89, 89, 89)); repetirNuevaClave.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { repetirNuevaClaveKeyReleased(evt); } public void keyTyped(java.awt.event.KeyEvent evt) { repetirNuevaClaveKeyTyped(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); } }); label_pass.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N label_pass.setForeground(new java.awt.Color(255, 0, 0)); label_pass.setToolTipText(""); 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.TRAILING, false) .addGroup(panelDatosLayout.createSequentialGroup() .addComponent(label_pass, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(aceptar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cancelar)) .addGroup(panelDatosLayout.createSequentialGroup().addGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(label_repetir_nueva_clave, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE) .addComponent(label_nueva_clave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(label_clave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(label_correo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 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(nuevaClave) .addComponent(repetirNuevaClave)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); panelDatosLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { aceptar, cancelar }); panelDatosLayout.setVerticalGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelDatosLayout.createSequentialGroup().addContainerGap() .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)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(label_nueva_clave, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(nuevaClave, 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_repetir_nueva_clave, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(repetirNuevaClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(11, 11, 11) .addGroup(panelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(label_pass, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(panelDatosLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(aceptar).addComponent(cancelar))) .addContainerGap())); panelDatosLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] { clave, correo, nuevaClave, repetirNuevaClave }); 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("Cambiar Clave"); 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.actualizarClave(); }//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()) { actualizarClave(); } 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()) { actualizarClave(); } 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 repetirNuevaClaveKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_repetirNuevaClaveKeyReleased if (repetirNuevaClave.getPassword().length >= 6) { if (!String.copyValueOf(repetirNuevaClave.getPassword()) .equals(String.copyValueOf(nuevaClave.getPassword()))) { label_pass.setText("Las claves no coinciden."); } else { label_pass.setText(""); } } }//GEN-LAST:event_repetirNuevaClaveKeyReleased private void nuevaClaveKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nuevaClaveKeyReleased label_pass.setText(""); if (nuevaClave.getPassword().length < 6) { label_pass.setText("Clave muy corta. Mnimo 6 caracteres."); } }//GEN-LAST:event_nuevaClaveKeyReleased private void nuevaClaveKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nuevaClaveKeyTyped nuevaClave.setBackground(new Color(255, 255, 255)); if (KeyEvent.VK_ENTER == evt.getKeyChar()) { actualizarClave(); } else { labelError.setText(""); } }//GEN-LAST:event_nuevaClaveKeyTyped private void repetirNuevaClaveKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_repetirNuevaClaveKeyTyped repetirNuevaClave.setBackground(new Color(255, 255, 255)); if (KeyEvent.VK_ENTER == evt.getKeyChar()) { actualizarClave(); } else { labelError.setText(""); } }//GEN-LAST:event_repetirNuevaClaveKeyTyped private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing cerrar(); }//GEN-LAST:event_formWindowClosing /** * Cierra esta ventana. */ public void cerrar() { if (!actualizando) { dispose(); } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(() -> { CambiarClave dialog = new CambiarClave(new javax.swing.JFrame(), true); 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); nuevaClave.setEnabled(true); repetirNuevaClave.setEnabled(true); aceptar.setEnabled(true); cancelar.setEnabled(true); } /** * Desactiva todos los campos y botones.F */ public void desactivarCampos() { correo.setEnabled(false); clave.setEnabled(false); nuevaClave.setEnabled(false); repetirNuevaClave.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_nueva_clave; private javax.swing.JLabel label_pass; private javax.swing.JLabel label_repetir_nueva_clave; private javax.swing.JLabel label_titulo; private javax.swing.JPasswordField nuevaClave; private javax.swing.JPanel panelDatos; private javax.swing.JPanel panelFondo; private javax.swing.JPasswordField repetirNuevaClave; // End of variables declaration//GEN-END:variables /** * Verifica los campos rellenos y actualiza la clave del usuario. */ private void actualizarClave() { label_pass.setText(""); 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 if (String.copyValueOf(nuevaClave.getPassword()).trim().isEmpty() || String.copyValueOf(nuevaClave.getPassword()).trim().length() < 6) { label_pass.setText("Clave muy corta. Mnimo 6 caracteres."); nuevaClave.requestFocus(); nuevaClave.setBackground(new Color(253, 194, 194)); } else if (!String.copyValueOf(repetirNuevaClave.getPassword()) .equals(String.copyValueOf(nuevaClave.getPassword()))) { label_pass.setText("Las claves no coinciden."); repetirNuevaClave.requestFocus(); repetirNuevaClave.setBackground(new Color(253, 194, 194)); } else { correo.setEnabled(false); clave.setEnabled(false); nuevaClave.setEnabled(false); repetirNuevaClave.setEnabled(false); aceptar.setEnabled(false); cancelar.setEnabled(false); labelError.setText("Actualizando clave ..."); labelError.setIcon(new javax.swing.ImageIcon( getClass().getResource("/com/cmsoftware/keyron/recursos/cargando.gif"))); actualizando = true; new ActualizarClave().start(); } } /** * Clase privada que gestiona el cambio de clave. */ private class ActualizarClave extends Thread { @Override public void run() { try { UsuarioFunciones uf = new UsuarioFunciones(); JSONObject json = uf.cambiarClave(correo.getText(), String.copyValueOf(clave.getPassword()), String.copyValueOf(nuevaClave.getPassword())); if (json != null) { int key_error = json.getInt("error"); if (key_error == 0) { actualizando = false; labelError.setText(""); labelError.setIcon(null); JOptionPane.showMessageDialog(null, "Su clave fue cambiada exitosamente.", "Operacin Exitosa - Keyron", JOptionPane.INFORMATION_MESSAGE); cerrar(); } else if (key_error == 1) { activarCampos(); actualizando = 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(); actualizando = 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(); actualizando = false; labelError.setText("No se pudo ingresar al sistema. Intente nuevamente"); labelError.setIcon(null); } else if (key_error == 19) { activarCampos(); actualizando = 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 == 21) { activarCampos(); actualizando = false; labelError.setText("Su clave no pudo ser cambiada. Intente nuevamente"); nuevaClave.requestFocus(); nuevaClave.setBackground(new Color(253, 194, 194)); labelError.setIcon(null); } else { activarCampos(); actualizando = false; labelError.setText("Hubo un error al procesar la solicitud. Intente nuevamente"); labelError.setIcon(null); } } else { activarCampos(); actualizando = 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(); actualizando = false; labelError.setIcon(null); Log.agregarError(e); } } } }