Package menu

Source Code of menu.ChangePassword

package menu;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
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.sql.SQLException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import database.DbwDBConnection;

public class ChangePassword extends JFrame implements IChangePassword {
  /**
   *
   */
  private static final long serialVersionUID = 6689847672683975702L;
  private String       userName;
  private JLabel       oldPassword;
  private  JPasswordField  oldPasswordField;
  private JLabel      newPassword;
  private  JPasswordField  newPasswordField;
  private JLabel      retypeNewPassword;
  private JPasswordField  retypeNewPasswordField;
  private JButton      changePassword;
  private JButton      exit;
  private JLabel      statut;
  private JPanel      globalPanel;
  private Container     pane;
  private DbwDBConnection databaseConnection;
  protected Object button = new JButton();

  public ChangePassword(String userName) throws SQLException{
    super(TITLE_CHANGE_PASSWORD_FRAME);
    pane = this.getContentPane();
    this.userName=userName;
    this.databaseConnection= new DbwDBConnection();
   
    createComponents();
    createPanels();
    this.setVisible(true);
   
    this.pack();
  }

  public void createComponents(){
    this.oldPassword= new JLabel(OLD_PASWORD);
    this.oldPassword.setPreferredSize(new Dimension(155,25));
    this.oldPasswordField= new JPasswordField(25);
    //this.oldPasswordField.setPreferredSize(new Dimension(25,25));

    this.newPassword= new JLabel(NEW_PASSWORD);
    this.newPassword.setPreferredSize(new Dimension(155,25));
    this.newPasswordField=new JPasswordField(25);
    //this.newPasswordField.setPreferredSize(new Dimension(25,25));
   
    this.retypeNewPassword= new JLabel(RETYPE_NEW_PASSWORD);
    this.retypeNewPassword.setPreferredSize(new Dimension(155,25));
    this.retypeNewPasswordField=new JPasswordField(25);
    //this.retypeNewPasswordField.setPreferredSize(new Dimension(25,25));

    this.exit=new JButton(EXIT);
    exit.setPreferredSize(new Dimension(100,30));
   
    this.changeButtonColor(exit);
    exit.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
        ChangePassword.this.setVisible(false);

      }
    });
    this.changePassword=new JButton(CHANGE_PASSWORD);
    changePassword.setPreferredSize(new Dimension(200,30));
    this.changeButtonColor(changePassword);
    this.changePassword.addActionListener(new ActionListener(){
      @SuppressWarnings("deprecation")
      public void actionPerformed(ActionEvent arg0) {
        if (!databaseConnection.checkPassword(userName, oldPasswordField.getText())){
          statut.setForeground(Color.RED);
          statut.setText(WRONG_OLD_PASSWORD);
          statut.repaint();
        }
        else{


          if(!newPasswordField.getText().equals(retypeNewPasswordField.getText())){
            statut.setForeground(Color.RED);
            statut.setText(WRONG_NEW_PASSWORD);
            statut.repaint();
          }
          else
          {
            databaseConnection.changePassword(userName,userName, newPasswordField.getText());
            statut.setForeground(Color.BLUE);
            statut.setText(CHANGE_PASSWORD_SUCCESS);
            statut.repaint();

          }

        }
      }

    });
    Date date=new Date();
    this.statut=new JLabel(date.toString());



  }
  public void createPanels(){
   
    JPanel panelOldPassword=new JPanel(new FlowLayout(FlowLayout.CENTER));
    panelOldPassword.add(oldPassword);
    panelOldPassword.add(oldPasswordField);
   
   
    JPanel panelNewPassword=new JPanel(new FlowLayout(FlowLayout.CENTER));
    panelNewPassword.add(newPassword);
    panelNewPassword.add(newPasswordField);
   
    JPanel panelRetypeNewPassword=new JPanel(new FlowLayout(FlowLayout.CENTER));
    panelRetypeNewPassword.add(retypeNewPassword);
    panelRetypeNewPassword.add(retypeNewPasswordField);
   
   
    JPanel panelButtons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    panelButtons.add(changePassword);
    panelButtons.add(exit);
   
    globalPanel=new JPanel(new GridLayout(5,1));
    globalPanel.add(panelOldPassword);
    globalPanel.add(panelNewPassword);
    globalPanel.add(panelRetypeNewPassword);
    globalPanel.add(panelButtons);
    globalPanel.add(statut);
   
   
    /*globalPanel=new JPanel(new GridLayout(5,2));
    globalPanel.add(oldPassword);
    globalPanel.add(oldPasswordField);
    globalPanel.add(newPassword);
    globalPanel.add(newPasswordField);
    globalPanel.add(retypeNewPassword);
    globalPanel.add(retypeNewPasswordField);
    globalPanel.add(changePassword);
    globalPanel.add(exit);
    globalPanel.add(statut);*/
   
    pane.add(globalPanel);
   
  }
  public void changeButtonColor(JButton button){

    ((Component) this.button).addMouseListener(new MouseAdapter(){
      public void mouseEntered(MouseEvent e) {
        ((JComponent) ChangePassword.this.button).setForeground(Color.BLUE);
      }
      public void mouseExited(MouseEvent arg0) {
        ((JComponent) ChangePassword.this.button).setForeground(Color.BLACK);

      }
      public void mousePressed(MouseEvent arg0) {
        ((JComponent) ChangePassword.this.button).setForeground(Color.GREEN)
      }
    });

  }
 
  public static void main (String [] args){
  try {
    new ChangePassword("dupont");
  } catch (SQLException e) {
    // TODO Auto-generated catch block
    System.out.println("No internet connection found!!");
    e.printStackTrace();
  }
   
   
  }
}
TOP

Related Classes of menu.ChangePassword

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.