package gafetes; /* * LoginDialogAdqal.java * * Created on 30 de diciembre de 2004, 5:50 */ /** * * @author maritza */ import javax.swing.JOptionPane; import javax.swing.JFrame; import javax.swing.InputMap; import javax.swing.ActionMap; import javax.swing.KeyStroke; import javax.swing.JComponent; import javax.swing.AbstractAction; import java.awt.event.KeyEvent; import java.awt.event.ActionEvent; import gafetes.beans.Usuario; import java.sql.*; public class LoginDialogAdqal extends javax.swing.JDialog { /** Creates new form LoginDialogAdqal */ public LoginDialogAdqal() { super(); setTitle("Validaci�n de usuario"); initComponents(); dialog = this; configurarBotones(); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); } public LoginDialogAdqal( javax.swing.JFrame frame, boolean bol, Usuario user, boolean modo ) { super(frame, bol); setTitle("Validaci�n de usuario"); initComponents(); dialog = this; this.user = user; this.modo = modo; configurarBotones(modo); } public LoginDialogAdqal( javax.swing.JFrame frame, boolean bol ) { super( frame, bol); setTitle("Validaci�n de usuario"); initComponents(); dialog = this; configurarBotones(); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); } /** 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. */ private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; northPanel = new javax.swing.JPanel(); eastPanel = new javax.swing.JPanel(); westPanel = new javax.swing.JPanel(); southPanel = new javax.swing.JPanel(); okButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); centerPanel = new javax.swing.JPanel(); imagePanel = new javax.swing.JPanel(); jPanel8 = new javax.swing.JPanel(); jPanel9 = new javax.swing.JPanel(); jPanel10 = new javax.swing.JPanel(); jPanel11 = new javax.swing.JPanel(); loginPanel = new javax.swing.JPanel(); userField = new javax.swing.JTextField(); userLabel = new javax.swing.JLabel(); passwordLabel = new javax.swing.JLabel(); passwordField = new javax.swing.JPasswordField(); getContentPane().add(northPanel, java.awt.BorderLayout.NORTH); getContentPane().add(eastPanel, java.awt.BorderLayout.EAST); getContentPane().add(westPanel, java.awt.BorderLayout.WEST); okButton.setText("Aceptar"); southPanel.add(okButton); cancelButton.setText("Cancelar"); southPanel.add(cancelButton); getContentPane().add(southPanel, java.awt.BorderLayout.SOUTH); centerPanel.setLayout(new java.awt.GridBagLayout()); centerPanel.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED)); centerPanel.setPreferredSize(new java.awt.Dimension(340, 140)); imagePanel.setLayout(new java.awt.BorderLayout()); imagePanel.add(jPanel8, java.awt.BorderLayout.NORTH); imagePanel.add(jPanel9, java.awt.BorderLayout.EAST); imagePanel.add(jPanel10, java.awt.BorderLayout.SOUTH); imagePanel.add(jPanel11, java.awt.BorderLayout.WEST); javax.swing.JLabel jLabel1 = new javax.swing.JLabel(); jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("images/password.png"))); imagePanel.add(jLabel1); jLabel1.setBounds(10, 0, 50, 50); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; centerPanel.add(imagePanel, gridBagConstraints); loginPanel.setLayout(new java.awt.GridBagLayout()); userField.setColumns(15); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); loginPanel.add(userField, gridBagConstraints); userLabel.setText("Usuario"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); loginPanel.add(userLabel, gridBagConstraints); passwordLabel.setText("Clave"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); loginPanel.add(passwordLabel, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); loginPanel.add(passwordField, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; centerPanel.add(loginPanel, gridBagConstraints); getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER); pack(); }//GEN-END:initComponents /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { new LoginDialogAdqal().show(); } private void configurarBotones() { InputMap im = passwordField.getInputMap(JComponent.WHEN_FOCUSED); im.put(KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ), "enterAction"); ActionMap am = passwordField.getActionMap(); am.put("enterAction", new AbstractAction("enterAction") { public void actionPerformed(ActionEvent evt) { okAction(); } }); InputMap im2 = passwordField.getInputMap(JComponent.WHEN_FOCUSED); im2.put(KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), "cancelAction"); ActionMap am2 = passwordField.getActionMap(); am2.put("cancelAction", new AbstractAction("cancelAction") { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); InputMap im4 = userField.getInputMap(JComponent.WHEN_FOCUSED); im4.put(KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ), "enterAction"); ActionMap am4 = userField.getActionMap(); am4.put("enterAction", new AbstractAction("enterAction") { public void actionPerformed(ActionEvent evt) { okAction(); } }); InputMap im3 = userField.getInputMap(JComponent.WHEN_FOCUSED); im3.put(KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), "cancelAction"); ActionMap am3 = userField.getActionMap(); am3.put("cancelAction", new AbstractAction("cancelAction") { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } }); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okAction(); } }); } private void configurarBotones(boolean modo) { InputMap im = passwordField.getInputMap(JComponent.WHEN_FOCUSED); im.put(KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ), "enterAction"); ActionMap am = passwordField.getActionMap(); am.put("enterAction", new AbstractAction("enterAction") { public void actionPerformed(ActionEvent evt) { user.setNombre(userField.getText().trim()); user.setPassword(passwordField.getText().trim()); okAction(); } }); InputMap im2 = passwordField.getInputMap(JComponent.WHEN_FOCUSED); im2.put(KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), "cancelAction"); ActionMap am2 = passwordField.getActionMap(); am2.put("cancelAction", new AbstractAction("cancelAction") { public void actionPerformed(ActionEvent evt) { user = null; dialog.dispose(); } }); InputMap im3 = userField.getInputMap(JComponent.WHEN_FOCUSED); im3.put(KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), "cancelAction"); ActionMap am3 = userField.getActionMap(); am3.put("cancelAction", new AbstractAction("cancelAction") { public void actionPerformed(ActionEvent evt) { user = null; dialog.dispose(); } }); InputMap im4 = userField.getInputMap(JComponent.WHEN_FOCUSED); im4.put(KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ), "enterAction"); ActionMap am4 = userField.getActionMap(); am4.put("enterAction", new AbstractAction("enterAction") { public void actionPerformed(ActionEvent evt) { user.setNombre(userField.getText().trim()); user.setPassword(passwordField.getText().trim()); okAction(); } }); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { user = null; dialog.dispose(); } }); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { user.setNombre(userField.getText().trim()); user.setPassword(passwordField.getText().trim()); //dialog.dispose(); okAction(); } }); } public void okAction() { boolean go = validateUser(); if( go ) { dialog.dispose(); } else if( tries > 2 ) { JOptionPane.showMessageDialog( new javax.swing.JFrame(), "3 Intentos maximo", "", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } else JOptionPane.showMessageDialog( Application.getparentFrame(), "Compruebe su nombre de usuario y contrase�a", "Usuario inexistente", JOptionPane.INFORMATION_MESSAGE); } private boolean validateUser() { boolean go = false; String nombre = userField.getText().trim(); char [] s = passwordField.getPassword(); String password = new String(s); //'pepe' or 1=1 String sSQL = " SELECT * FROM " + gafetes.db.GlobalSettings.GetPrefix()+"USUARIOS WHERE USERNAME = ?" +" AND PASSWORD = ?" ; /*' or password!='*/ PreparedStatement pStmt = db.getPreparedStatement( sSQL ); try{ pStmt.setString(1, nombre); pStmt.setString(2, password); java.sql.ResultSet rs = pStmt.executeQuery(); if( rs != null ) { if( rs.next() ) { String name = rs.getString("Username"); String passwd = rs.getString("password"); boolean esAdministrador = rs.getBoolean("ADMINISTRADOR"); int id = rs.getInt("id"); Usuario user = new Usuario(); user.setNombre(name); user.setPassword(passwd); user.setAdmin(esAdministrador); user.setId(id); Application.setUsuario(user); go = true; }else tries++; } } catch( SQLException ex ) { db.tratarExcepcionSQL( ex ); } return go; } public Usuario getUser() { return user; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel centerPanel; private javax.swing.JPanel eastPanel; private javax.swing.JPanel imagePanel; private javax.swing.JButton okButton; private javax.swing.JButton cancelButton; private javax.swing.JPanel jPanel10; private javax.swing.JPanel jPanel11; private javax.swing.JPanel jPanel8; private javax.swing.JPanel jPanel9; private javax.swing.JPanel loginPanel; private javax.swing.JPanel northPanel; private javax.swing.JPasswordField passwordField; private javax.swing.JLabel passwordLabel; private javax.swing.JPanel southPanel; private javax.swing.JTextField userField; private javax.swing.JLabel userLabel; private javax.swing.JPanel westPanel; // End of variables declaration//GEN-END:variables private int tries = 0; //DBMySQL db = new DBMySQL(); private javax.swing.JDialog dialog; Usuario user = new Usuario(); boolean modo = false; public static gafetes.db.MSSQL db = new gafetes.db.MSSQL(); }