Package gui

Source Code of gui.LoginDlg

/*
* LoginDlg.java
*
* Created on 15. Januar 2008, 15:39
*/
package gui;

import data.User;
import db.Datenbank;
import java.net.InetAddress;
import javax.swing.JOptionPane;

/**
*
* @author  Kevin
*/
public class LoginDlg extends javax.swing.JDialog
{
  private boolean isOk = false;
  private User user = null;
  private User newUser = null;

  public boolean isOk()
  {
    return isOk;
  }

  public User getUser()
  {
    return user;
  }

  public User getNewUser()
  {
    return newUser;
  }

  /** Creates new form LoginDlg */
  public LoginDlg(java.awt.Frame parent, boolean modal)
  {
    super(parent, modal);
    initComponents();
    this.getRootPane().setDefaultButton(btLogin);
  }

  /** 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.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents()
  {
    java.awt.GridBagConstraints gridBagConstraints;

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    tfNickname = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    pfPasswort = new javax.swing.JPasswordField();
    jPanel2 = new javax.swing.JPanel();
    jPanel3 = new javax.swing.JPanel();
    btLogin = new javax.swing.JButton();
    btAbbrechen = new javax.swing.JButton();
    btRegistrieren = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Login");
    setResizable(false);

    jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
    jPanel1.setLayout(new java.awt.GridBagLayout());

    jLabel1.setText("Nickname: ");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 10);
    jPanel1.add(jLabel1, gridBagConstraints);

    tfNickname.setColumns(20);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
    jPanel1.add(tfNickname, gridBagConstraints);

    jLabel2.setText("Passwort: ");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 10);
    jPanel1.add(jLabel2, 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, 0, 0, 0);
    jPanel1.add(pfPasswort, gridBagConstraints);

    getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

    jPanel3.setLayout(new java.awt.GridLayout(1, 0, 5, 0));

    btLogin.setText("Login");
    btLogin.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        onLogin(evt);
      }
    });
    jPanel3.add(btLogin);

    btAbbrechen.setText("Abbrechen");
    btAbbrechen.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        onAbbrechen(evt);
      }
    });
    jPanel3.add(btAbbrechen);

    btRegistrieren.setText("Registrieren");
    btRegistrieren.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        onRegistrieren(evt);
      }
    });
    jPanel3.add(btRegistrieren);

    jPanel2.add(jPanel3);

    getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

    pack();
  }// </editor-fold>//GEN-END:initComponents
  private void onAbbrechen(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onAbbrechen
    dispose();
  }//GEN-LAST:event_onAbbrechen

  private void onRegistrieren(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onRegistrieren
    RegistrierungDlg dlg = new RegistrierungDlg(this, true);
    dlg.setVisible(true);
    if (dlg.isOk())
    {
      newUser = dlg.getUser();
      isOk = true;
      dispose();
    }
  }//GEN-LAST:event_onRegistrieren

  private void onLogin(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onLogin
    try
    {
      InetAddress inetAddress = InetAddress.getLocalHost();
      String ip = inetAddress.getHostAddress().toString();
      //nun mit dem Server connecten und ihm die Logindaten senden!
      user = Datenbank.getInstance().getLoggedUser(tfNickname.getText(),
        User.toHash(new String(pfPasswort.getPassword())));
      newUser = new User(user.getNick(), user.getNachname(), user.getVorname(),
        user.getKlasse(), user.getPwHash(), user.getKoop(), user.getNKoop(),
        user.getPunkteG(), user.getPunkte5(), user.getPunkte10(),
        user.getPunkte20(), user.getPunkte50(), user.getOnline(), ip);
      isOk = true;
      dispose();
    }
    catch (Exception ex)
    {
      JOptionPane.showMessageDialog(this, "Kein Benutzer vorhanden. Falsches " +
        "Passwort?", "Fehler", JOptionPane.ERROR_MESSAGE);
    }
  }//GEN-LAST:event_onLogin

  /**
   * @param args the command line arguments
   */
  public static void main(String args[])
  {
    java.awt.EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        LoginDlg dialog = new LoginDlg(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter()
        {
          public void windowClosing(java.awt.event.WindowEvent e)
          {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btAbbrechen;
  private javax.swing.JButton btLogin;
  private javax.swing.JButton btRegistrieren;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JPanel jPanel3;
  private javax.swing.JPasswordField pfPasswort;
  private javax.swing.JTextField tfNickname;
  // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of gui.LoginDlg

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.