package main;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import user.User;
import database.DbwDBConnection;
public class SigningIn extends JFrame implements ISiginingIn, ILanguage, ISettings {
/**
*
*/
private static final long serialVersionUID = 2985279922226679169L;
private JLabel login;
private JTextField loginTextField;
private JLabel passwordLabel;
private JPasswordField passwordTextField;
private JButton connection;
private JButton cancel;
private JLabel statut;
private String userName;
private String password;
private DbwDBConnection databaseConnection;
private JCheckBox jcbSavePassword;
private String savedPassword;
private String savedLogin;
public SigningIn(){
super(TITLE);
this.setSize(300,170);
//this.setSize(400,270);
this.setLocation(350, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
int retour = JOptionPane.showConfirmDialog(SigningIn.this,EXIT_MESSAGE, EXIT_CONFIRMATION,JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if (retour == JOptionPane.YES_OPTION){
System.exit(0);}
}
});
File fileExt= new File("/home/imad/Bureau/password.txt");
if (fileExt.exists()){
try {
FileInputStream intExt = new FileInputStream(fileExt);
DataInputStream dis=new DataInputStream(intExt);
savedLogin=dis.readUTF();
savedPassword=dis.readUTF();
System.out.println(savedPassword);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
savedPassword="";
savedLogin="";}
createComponents();
createPanel();
this.setVisible(true);
}
public void createComponents(){
this.login= new JLabel(LOGIN);
this.login.setPreferredSize(new Dimension(155,25));
this.loginTextField= new JTextField(savedLogin);
this.loginTextField.setPreferredSize(new Dimension(155,25));
this.passwordLabel= new JLabel(PASSWORD);
this.passwordLabel.setPreferredSize(new Dimension(155,25));
this.passwordTextField= new JPasswordField(savedPassword);
this.passwordTextField.setPreferredSize(new Dimension(155,25));
this.statut=new JLabel(STATUS);
databaseConnection= new DbwDBConnection();
this.cancel=new JButton(CANCEL);
cancel.setPreferredSize(new Dimension(100,30));
cancel.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
cancel.setForeground(BUTTON_MOUSE_ENTERED);
}
public void mouseExited(MouseEvent arg0) {
cancel.setForeground(BUTTON_MOUSE_EXITED);
}
public void mousePressed(MouseEvent arg0) {
cancel.setForeground(BUTTON_MOUSE_PRESSED);
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int retour = JOptionPane.showConfirmDialog(SigningIn.this,EXIT_MESSAGE, EXIT_CONFIRMATION,JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if (retour == JOptionPane.YES_OPTION){
System.exit(0);}
}
});
this.connection = new JButton(CONNECT);
connection.setPreferredSize(new Dimension(100,30));
connection.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
connection.setForeground(BUTTON_MOUSE_ENTERED);
}
public void mouseExited(MouseEvent arg0) {
connection.setForeground(BUTTON_MOUSE_EXITED);
}
public void mousePressed(MouseEvent arg0) {
connection.setForeground(BUTTON_MOUSE_PRESSED);
}
});
connection.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent aeCheckUser){
userName=SigningIn.this.loginTextField.getText();
password=SigningIn.this.passwordTextField.getText();
if(!databaseConnection.existedUser(userName)){
SigningIn.this.statut.setForeground(Color.RED);
SigningIn.this.statut.setText(USER_NOT_FOUND);
}
else
{
if(!databaseConnection.checkPassword(userName, password)){
SigningIn.this.statut.setForeground(Color.RED);
SigningIn.this.statut.setText(WRONG_PASSWORD);
}
else{
//SigningIn.this.statut.setForeground(Color.BLUE);
//SigningIn.this.statut.setText(CONNECTING);
if(!databaseConnection.whoIsOnline().contains(userName)){
statut.setForeground(Color.BLUE);
statut.setText(CONNECTING);
new User(userName, databaseConnection.getUserInformation(userName).split(SEPARATOR)[0]+" "+databaseConnection.getUserInformation(userName).split(SEPARATOR)[1]);
databaseConnection.connectUser(userName);
if(jcbSavePassword.isSelected()){
File fileExt= new File("/home/imad/Bureau/password.txt");
try {
FileOutputStream outExt = new FileOutputStream(fileExt);
DataOutputStream dos=new DataOutputStream(outExt);
//BufferedOutputStream bosExt = new BufferedOutputStream(outExt);
//PrintWriter writerExt = new PrintWriter(bosExt);
//writerExt.write(passwordTextField.getText());
//writerExt.close();
try {
dos.writeUTF(loginTextField.getText());
dos.writeUTF(passwordTextField.getText());
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Sauvegarde");
}
SigningIn.this.setVisible(false);
}
else{
SigningIn.this.statut.setForeground(Color.RED);
SigningIn.this.statut.setText(MULTY_CONNECTION);
}
}
}
statut.repaint();
}});
this.jcbSavePassword=new JCheckBox("save password");
}
public void createPanel(){
JPanel panelUserInfo=new JPanel(new GridLayout(2,2));
panelUserInfo.add(login);
panelUserInfo.add(loginTextField);
panelUserInfo.add(passwordLabel);
panelUserInfo.add(passwordTextField);
JPanel panelTop=new JPanel(new BorderLayout());
panelTop.add(panelUserInfo, BorderLayout.NORTH);
panelTop.add(jcbSavePassword,BorderLayout.SOUTH);
JPanel panelButtons =new JPanel(new FlowLayout());
//panelButtons.add(jcbSavePassword);
panelButtons.add(connection);
panelButtons.add(cancel);
this.setLayout(new BorderLayout());
this.add(panelTop,BorderLayout.NORTH);
this.add(panelButtons,BorderLayout.CENTER);
this.add(statut, BorderLayout.SOUTH);
//pane.add(panelTop);
}
public static void main (String [] args){
new SigningIn();
}
}