/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Login.java
*
* Created on Dec 1, 2010, 12:05:35 PM
*/
package Gui;
import client.ChatClient;
import logic.*;
import common.ChatIF;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import client.User;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.io.Serializable;
/**
*
* @author Kipi12899
*/
public class Login extends javax.swing.JFrame {
private JLabel jLabel666 = null;
public static User user; // username
public static String password ; // password
public static String host; // Remove server IP address
public static ChatClient client;
private ChatIF ChatIF = null;
private static int error=0;
/** Creates new form Login */
public Login(User user,String host) {
this.user=user;
this.host = host; //To allow communication between two computers
initComponents();
}
public Login() {
initComponents();
}
public Login(String user1,String host, int error) {
this.user=user;
this.host = host; //To allow communication between two computers
this.error=error;
initComponents();
}
public void error(int error)
{
this.error=error;
}
/** 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() {
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("I-Book : Login");
setForeground(java.awt.Color.white);
setMinimumSize(new java.awt.Dimension(313, 390));
setResizable(false);
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});
getContentPane().setLayout(null);
jTextField1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
getContentPane().add(jTextField1);
jTextField1.setBounds(140, 100, 110, 18);
jPasswordField1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordField1ActionPerformed(evt);
}
});
getContentPane().add(jPasswordField1);
jPasswordField1.setBounds(140, 140, 110, 18);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel1.setText("Username:");
getContentPane().add(jLabel1);
jLabel1.setBounds(50, 100, 90, 17);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel2.setText("Password:");
getContentPane().add(jLabel2);
jLabel2.setBounds(50, 140, 80, 17);
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabel3.setText("New to I-Book?");
getContentPane().add(jLabel3);
jLabel3.setBounds(110, 280, 98, 20);
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Pictures/log-arrlog.gif"))); // NOI18N
jButton1.setText("Create new account ");
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(80, 310, 170, 30);
jButton2.setText("Login");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(120, 200, 80, 30);
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel7.setText("Host");
getContentPane().add(jLabel7);
jLabel7.setBounds(50, 60, 34, 14);
jTextField2.setText("localhost");
jTextField2.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
getContentPane().add(jTextField2);
jTextField2.setBounds(140, 60, 110, 20);
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Pictures/login.jpg"))); // NOI18N
getContentPane().add(jLabel4);
jLabel4.setBounds(0, 0, 340, 300);
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Pictures/login.jpg"))); // NOI18N
getContentPane().add(jLabel5);
jLabel5.setBounds(0, 300, 340, 70);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
AddUser s=new AddUser();
s.setVisible(true);
this.setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) //login button
{//GEN-FIRST:event_jButton2ActionPerformed
String[] data = new String[3]; // data sent to server
String[] data2 = new String[3]; // data sent to server
host = jTextField2.getText(); // set remote server IP
obj logdt = new obj();
logdt.setType("login");
logdt.setParam(0,jTextField1.getText());//user
logdt.setParam(1,jPasswordField1.getText());//Pass
int connected = 1;
try
{
client= new ChatClient(host,5555,ChatIF);
} // end try
catch(IOException exception)
{
JOptionPane.showMessageDialog(null,"Error: Can't setup connection!","Error",JOptionPane.CLOSED_OPTION);
connected = 0; // failed to connect
} // end catch
if(connected == 1)
client.handleMessageFromClientUI(logdt);
try {
Thread.sleep(1000);
} // end try
catch (InterruptedException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
} // end catch
if(user != null && error == 0) // if user exists
{
if(user.getPassword().equals(jPasswordField1.getText()) ) // if password currect
{
if(user.getOnline()==0) // first time login?
{
obj logdt2 = new obj();
logdt2.setType("Online");
logdt2.setParam(0,jTextField1.getText());//user
logdt2.setParam(1,jPasswordField1.getText());//Pass
client.handleMessageFromClientUI(logdt2);
if (user.getPriv()==0 || user.getPriv()==1 || user.getPriv()==2 || user.getPriv()==3)
{
MainWindow s=new MainWindow();
this.setVisible(false);
s.setLocationRelativeTo(null);
s.setVisible(true);
}
} // end first time login
else{ JOptionPane.showMessageDialog(null," " + user.getUser() + " already logged in","ERROR",JOptionPane.ERROR_MESSAGE);}
}// end password
else{ JOptionPane.showMessageDialog(null,"Wrong Password","ERROR",JOptionPane.ERROR_MESSAGE);}
}// end if user
else{ JOptionPane.showMessageDialog(null,"User name not exist","ERROR",JOptionPane.ERROR_MESSAGE);error=0;}
}//GEN-LAST:event_jButton2ActionPerformed
private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jPasswordField1ActionPerformed
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed
private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
// TODO add your handling code here:
}//GEN-LAST:event_formComponentResized
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration//GEN-END:variables
}