/*
* Copyright (C) 2015 Allsoft
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package br.com.allsoft.avros.interfaces;
import br.com.allsoft.avros.dao.UsuarioDAO;
import br.com.allsoft.avros.modelo.Usuario;
import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Luana
*/
public class FrmLogin extends javax.swing.JFrame {
//Variáveis
public static Usuario usuario = new Usuario();
String logAntes = "";
int erros = 0;
/**
* Creates new form frmLogin
*/
public FrmLogin() {
initComponents();
}
/**
* 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() {
lblLogo = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtLogin = new javax.swing.JTextField();
btnLogin = new javax.swing.JButton();
txtSenha = new javax.swing.JPasswordField();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Avros - Login");
setBackground(new java.awt.Color(255, 255, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setIconImage(getIconImage());
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
lblLogo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/allsoft/avros/img/logopequeno.png"))); // NOI18N
jLabel3.setFont(ClsEstilo.labelFonte);
jLabel3.setForeground(ClsEstilo.labelCor);
jLabel3.setText("Senha");
jLabel2.setFont(ClsEstilo.labelFonte);
jLabel2.setForeground(ClsEstilo.labelCor);
jLabel2.setText("Usuário");
txtLogin.setFont(ClsEstilo.labelFonte);
txtLogin.setForeground(ClsEstilo.textoInputCor);
this.getRootPane().setDefaultButton(btnLogin);
btnLogin.setFont(ClsEstilo.botaoFonte);
btnLogin.setForeground(ClsEstilo.botaoCor);
btnLogin.setText("Login");
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
txtSenha.setFont(ClsEstilo.labelFonte);
txtSenha.setForeground(ClsEstilo.textoInputCor);
jLabel4.setBackground(ClsEstilo.formbg);
jLabel4.setFont(ClsEstilo.linkFonte);
jLabel4.setForeground(ClsEstilo.linkCor);
jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel4.setText("Esqueci meu usuário/senha.");
jLabel4.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
jLabel4.setOpaque(true);
jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel4MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lblLogo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(98, 98, 98))
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jLabel3)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(75, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblLogo)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jLabel2))
.addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jLabel3))
.addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(25, 25, 25)
.addComponent(btnLogin)
.addGap(18, 18, 18)
.addComponent(jLabel4)
.addContainerGap(27, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
String login = txtLogin.getText();
char[] senha = txtSenha.getPassword();
try {
usuario = UsuarioDAO.login(login, senha);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Verifique sua conexão com o banco de dados.", "Erro", JOptionPane.ERROR_MESSAGE);
Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
return;
}
if (usuario.getId() > 0) {
this.dispose();
new FrmPrincipal().setVisible(true);
} else {
if (login.equalsIgnoreCase(logAntes)) {
erros += 1;
}
logAntes = login;
if (erros > 2) {
try {
UsuarioDAO.uusuarioAtivo(false, login);
JOptionPane.showMessageDialog(this, "O usuário " + login + " foi bloqueado por conta do excesso de erros. Contate um administrador do sistema para desbloquear.", "Usuário bloqueado", JOptionPane.ERROR_MESSAGE);
} catch (SQLException ex) {
Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
return;
}
} else {
JOptionPane.showMessageDialog(this, "Nome de usuário ou senha inválidos.", "Tente outra vez", JOptionPane.ERROR_MESSAGE);
}
}
}//GEN-LAST:event_btnLoginActionPerformed
private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
Container a = this.getContentPane();
a.setBackground(ClsEstilo.formbg);
URL url = this.getClass().getResource("/br/com/allsoft/avros/img/logo.png");
Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
this.setIconImage(imagemTitulo);
}//GEN-LAST:event_formWindowOpened
private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseClicked
JOptionPane.showMessageDialog(this, "Para recuperar a senha, contate o administrador de seu sistema.");
}//GEN-LAST:event_jLabel4MouseClicked
/**
* @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(FrmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new FrmLogin().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnLogin;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel lblLogo;
private javax.swing.JTextField txtLogin;
private javax.swing.JPasswordField txtSenha;
// End of variables declaration//GEN-END:variables
}