package admin;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
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 javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import main.ILanguage;
import database.DbwDBConnection;
public class UserPanel extends JFrame implements ILanguage {
/**
*
*/
private static final long serialVersionUID = 6494060304157569410L;
private int userLevel;
private JLabel login;
private JTextField loginField;
private JLabel password;
private JPasswordField passwordField;
private JLabel firstName;
private JTextField firstNameField;
private JLabel lastName;
private JTextField lastNameField;
private JLabel emailAdress;
private JTextField emailAdressField;
private JLabel phoneNumber;
private JTextField phoneNumberField;
private JCheckBox superUser;
private JCheckBox adminstratorUser;
private JCheckBox normalUser;
private ButtonGroup userLevelGroup;
private JLabel statut;
private JButton search;
private JButton createPassword;
private JButton delete;
private JButton modify;
private JButton subscribe;
private JButton close;
private JPanel panelUserInfo;
private JPanel panelButtons;
private JPanel panelGlobal;
private String userName;
private Container pane;
private DbwDBConnection dataBaseConnection;
public UserPanel(String userName,DbwDBConnection dataBaseConnection ){
this.userName=userName;
this.dataBaseConnection=dataBaseConnection;
this.userLevel=dataBaseConnection.getUserLevel(userName);
pane = this.getContentPane();
createComponents();
createPanels();
this.setVisible(true);
this.pack();
}
public void createComponents(){
this.login=new JLabel(LOGIN);
this.loginField=new JTextField();
this.password=new JLabel(PASSWORD);
this.passwordField= new JPasswordField();
this.firstName=new JLabel(FIRST_NAME);
this.firstNameField=new JTextField();
this.lastName=new JLabel(LAST_NAME);
this.lastNameField=new JTextField();
this.emailAdress=new JLabel(EMAIL_ADRESS);
this.emailAdressField=new JTextField();
this.phoneNumber=new JLabel(PHONE_NUMBER);
this.phoneNumberField=new JTextField();
this.superUser=new JCheckBox(SUPER_USER);
this.adminstratorUser=new JCheckBox(ADMINSTRATOR_USER);
this.normalUser=new JCheckBox(NORMAL_USER);
this.userLevelGroup = new ButtonGroup();
userLevelGroup.add(superUser);
userLevelGroup.add(adminstratorUser);
userLevelGroup.add(normalUser);
this.statut=new JLabel(STATUS);
this.search=new JButton(SEARCH);
search.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
search.setForeground(Color.BLUE);
}
public void mouseExited(MouseEvent arg0) {
search.setForeground(Color.BLACK);
}
public void mousePressed(MouseEvent arg0) {
search.setForeground(Color.GREEN);
}
});
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(dataBaseConnection.existedUser(loginField.getText())){
statut.setForeground(Color.BLACK);
statut.setText(USER_FOUND);
statut.repaint();
String result=dataBaseConnection.getUserInformation(loginField.getText());
String fname=result.split(SEPARATOR)[0];
String lname=result.split(SEPARATOR)[1];
String email=result.split(SEPARATOR)[2];
String phone=result.split(SEPARATOR)[3];
String level=result.split(SEPARATOR)[4];
firstNameField.setText(fname);
lastNameField.setText(lname);
emailAdressField.setText(email);
phoneNumberField.setText(phone);
if(level.equals("2"))
normalUser.setSelected(true);
else
if(level.equals("1"))
adminstratorUser.setSelected(true);
else
superUser.setSelected(true);
}
else{
statut.setForeground(Color.RED);
statut.setText(USER_NOT_FOUND);
statut.repaint();
}
}
});
this.delete=new JButton(DELETE_USER);
delete.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
delete.setForeground(Color.BLUE);
}
public void mouseExited(MouseEvent arg0) {
delete.setForeground(Color.BLACK);
}
public void mousePressed(MouseEvent arg0) {
delete.setForeground(Color.GREEN);
}
});
delete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(dataBaseConnection.existedUser(loginField.getText())){
if(dataBaseConnection.deleteUser(userName,loginField.getText())){
statut.setForeground(Color.BLACK);
statut.setText(USER_DELETED);
statut.repaint();
}
else
{
statut.setForeground(Color.RED);
statut.setText(USER_NOT_DELETED);
statut.repaint();
}
}
else{
statut.setForeground(Color.RED);
statut.setText(USER_NOT_FOUND);
statut.repaint();
}
}
});
this.createPassword=new JButton(CREATE_PASSWORD);
createPassword.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
createPassword.setForeground(Color.BLUE);
}
public void mouseExited(MouseEvent arg0) {
createPassword.setForeground(Color.BLACK);
}
public void mousePressed(MouseEvent arg0) {
createPassword.setForeground(Color.GREEN);
}
});
createPassword.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent ae){
if(dataBaseConnection.existedUser(loginField.getText())){
if(!passwordField.getText().equals("")){
if(dataBaseConnection.changePassword(userName,loginField.getText(),passwordField.getText())){
statut.setForeground(Color.BLACK);
statut.setText(PASSWORD_CREATED);
statut.repaint();
}
}
else
{
statut.setForeground(Color.RED);
statut.setText(ENTER_A_VALID_PASSWORD);
statut.repaint();
}
}
else{
statut.setForeground(Color.RED);
statut.setText(USER_NOT_FOUND);
statut.repaint();
}
}
});
this.modify=new JButton(MODIFY_USER_INFO);
modify.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
modify.setForeground(Color.BLUE);
}
public void mouseExited(MouseEvent arg0) {
modify.setForeground(Color.BLACK);
}
public void mousePressed(MouseEvent arg0) {
modify.setForeground(Color.GREEN);
}
});
modify.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
//TODO
}
});
this.subscribe=new JButton(SUBSCRIBE_USER);
subscribe.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
subscribe.setForeground(Color.BLUE);
}
public void mouseExited(MouseEvent arg0) {
subscribe.setForeground(Color.BLACK);
}
public void mousePressed(MouseEvent arg0) {
subscribe.setForeground(Color.GREEN);
}
});
subscribe.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent ae){
if(!dataBaseConnection.existedUser(loginField.getText())){
int level=2;
if(superUser.isSelected())
level=0;
else
if(adminstratorUser.isSelected())
level=1;
else
level=2;
if(loginField.getText().equals("")){
statut.setForeground(Color.RED);
statut.setText("please enter a login");
}
else
if(passwordField.getText().equals("")){
statut.setForeground(Color.RED);
statut.setText("please enter a valid password");
}
else
if(firstNameField.getText().equals("")){
statut.setForeground(Color.RED);
statut.setText("please enter your first name");
}
else
if(lastNameField.getText().equals("")){
statut.setForeground(Color.RED);
statut.setText("please enter your last name");
}
else
if(!superUser.isSelected() && !adminstratorUser.isSelected() && !normalUser.isSelected())
{
statut.setForeground(Color.RED);
statut.setText("please choose a user level");
}
else{
if(dataBaseConnection.createUser(userName, loginField.getText(), passwordField.getText(), firstNameField.getText(), lastNameField.getText().toUpperCase(), level, emailAdressField.getText(),phoneNumberField.getText()))
{statut.setForeground(Color.BLUE);
statut.setText("subscribe success");}
else
statut.setText("subscribe not success!!");
}
}
else
statut.setText("user exists already!!");
statut.repaint();
}
});
this.close=new JButton(CLOSE);
close.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
close.setForeground(Color.BLUE);
}
public void mouseExited(MouseEvent arg0) {
close.setForeground(Color.BLACK);
}
public void mousePressed(MouseEvent arg0) {
close.setForeground(Color.GREEN);
}
});
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
UserPanel.this.setVisible(false);
}
});
if(userLevel==1){
this.delete.setEnabled(false);
this.superUser.setEnabled(false);
this.adminstratorUser.setEnabled(false);
this.normalUser.setEnabled(true);
}
if(userLevel==2){
this.delete.setEnabled(false);
this.superUser.setEnabled(false);
this.adminstratorUser.setEnabled(false);
this.normalUser.setEnabled(false);
}
}
public void createPanels(){
this.panelUserInfo=new JPanel(new GridLayout(3,4));
panelUserInfo.add(login);
panelUserInfo.add(loginField);
panelUserInfo.add(password);
panelUserInfo.add(passwordField);
panelUserInfo.add(firstName);
panelUserInfo.add(firstNameField);
panelUserInfo.add(lastName);
panelUserInfo.add(lastNameField);
panelUserInfo.add(emailAdress);
panelUserInfo.add(emailAdressField);
panelUserInfo.add(phoneNumber);
panelUserInfo.add(phoneNumberField);
JPanel panelCheckBox = new JPanel(new FlowLayout());
panelCheckBox.add(superUser);
panelCheckBox.add(adminstratorUser);
panelCheckBox.add(normalUser);
panelButtons=new JPanel(new FlowLayout());
panelButtons.add(search);
panelButtons.add(subscribe);
panelButtons.add(delete);
panelButtons.add(createPassword);
panelButtons.add(modify);
panelButtons.add(close);
this.panelGlobal=new JPanel(new BorderLayout());
panelGlobal.add(panelUserInfo, BorderLayout.NORTH);
panelGlobal.add(panelCheckBox,BorderLayout.CENTER);
panelGlobal.add(panelButtons,BorderLayout.SOUTH);
panelGlobal.add(statut, BorderLayout.LINE_START);
pane.add(panelGlobal);
}
public static void main (String [] args){
DbwDBConnection dataBase= new DbwDBConnection();
//new UserPanel(0, dataBase);
}
}