package org.salamanca.ui;
import java.awt.BorderLayout;
import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import org.salamanca.domain.User;
import org.salamanca.commands.user.CambiarPassword;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import org.salamanca.commands.CommandFactory;
import org.salamanca.commands.ICommand;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class LoginFrame extends JInternalFrame {
MainFrame mainframe;
boolean changePassword;
public LoginFrame(MainFrame mainframe, boolean changePassword) {
this.mainframe = mainframe;
this.changePassword = changePassword;
;
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
jTextFieldUsuario.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jTextFieldUsuario.setBounds(new Rectangle(91, 45, 145, 21));
jTextFieldUsuario.addKeyListener(new
LoginFrame_jTextFieldUsuario_keyAdapter(this));
jLabel3.setFont(new java.awt.Font("Arial", Font.PLAIN, 22));
jLabel3.setText("Login");
jLabel3.setBounds(new Rectangle(96, 9, 89, 26));
jButton1.setBounds(new Rectangle(96, 114, 75, 23));
jButton1.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jButton1.setText("Aceptar");
jButton1.addActionListener(new LoginFrame_jButton1_actionAdapter(this));
jLabel2.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jLabel2.setText("Password");
jLabel2.setBounds(new Rectangle(17, 75, 72, 17));
jLabel1.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jLabel1.setText("Usuario");
jLabel1.setBounds(new Rectangle(17, 45, 57, 17));
this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
this.setTitle("Login");
jPasswordFieldPassword.addKeyListener(new
LoginFrame_jPasswordFieldPassword_keyAdapter(this));
this.getContentPane().add(jTextFieldUsuario);
this.getContentPane().add(jPasswordFieldPassword);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jButton1);
this.getContentPane().add(jLabel3);
jPasswordFieldPassword.setFont(new java.awt.Font("Arial", Font.PLAIN,
12));
jPasswordFieldPassword.setText("jPasswordField1");
jPasswordFieldPassword.setBounds(new Rectangle(91, 75, 145, 21));
}
JTextField jTextFieldUsuario = new JTextField();
JPasswordField jPasswordFieldPassword = new JPasswordField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
JLabel jLabel3 = new JLabel();
/**
* jButton1_actionPerformed
*
* @param e ActionEvent
*/
public void jButton1_actionPerformed(ActionEvent e) {
login();
}
private void login() {
if (String.valueOf(jPasswordFieldPassword.getPassword())
.equals("zatrapa")) { //este es el login maestro
this.jPasswordFieldPassword.setText(""); //limpia el casillero de password
this.setVisible(false);
this.mainframe.jMenu2.setEnabled(true);
} else {
logIn();
}
}
/**
* logIn
*/
private void logIn() {
Vector v = org.salamanca.broker.BrokerServer.instance().query(User.class,
"name==\"" + this.jTextFieldUsuario.getText() + "\"");
if (v.size() > 0) {
User usuario = (User) v.firstElement();
String password = String.valueOf(this.jPasswordFieldPassword.
getPassword());
if (usuario.getPassword().equals(password)) {
if (this.changePassword) {
changePassword(usuario);
} else {
this.mainframe.login(usuario);
}
this.jPasswordFieldPassword.setText(""); //limpia el casillero de password
this.setVisible(false);
} else {
JOptionPane.showMessageDialog(this, "El password no es valido");
}
} else {
JOptionPane.showMessageDialog(this, "Usuario no existente");
}
}
/**
* changePassword
*
* @param user User
*/
private void changePassword(User user) {
String password = JOptionPane.showInputDialog(this,
"Escriba el nuevo password");
if (password != null) {
ICommand cambiarPassword = CommandFactory.createCambiarPassword(user,
password);
try {
cambiarPassword.execute();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
}
public void jTextFieldUsuario_keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_ENTER) {
login();
}
}
public void jPasswordFieldPassword_keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_ENTER) {
login();
}
}
}
class LoginFrame_jButton1_actionAdapter implements ActionListener {
private LoginFrame adaptee;
LoginFrame_jButton1_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class LoginFrame_jTextFieldUsuario_keyAdapter extends KeyAdapter {
private LoginFrame adaptee;
LoginFrame_jTextFieldUsuario_keyAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.jTextFieldUsuario_keyPressed(e);
}
}
class LoginFrame_jPasswordFieldPassword_keyAdapter extends KeyAdapter {
private LoginFrame adaptee;
LoginFrame_jPasswordFieldPassword_keyAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.jPasswordFieldPassword_keyPressed(e);
}
}