package User;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import Expense.ExpensePane;
import Inventory.InventoryPane;
import Login.Button;
import Login.DBHandler;
import Login.MainFrame;
import Login.User;
import Sales.SalePane;
public class UserPane extends JPanel implements ActionListener, MouseListener, MouseMotionListener{
private JPanel parent;
private CardLayout cards;
private Button add, update, remove;
private BufferedImage bg, panel1, panel2, panel3;
private Button salesBtn, inBtn, expBtn, usBtn, logBtn, userAdd, userUpd, userRem;
private Timer time;
private int curPanel, selUser, listSelIndex;
private DBHandler db;
private String userName, password, retype, error;
private ArrayList<User> users;
private JComboBox userBox;
private JTextField nameBox;
private JPasswordField passBox, retypeBox;
private boolean started, checkPass, clicked, clickHere;
private JFrame frame;
private JLabel typeLabel, userLabel, passLabel, retypeLabel;
private JList list;
private JScrollPane scrollPane, scrollPane2;
private DefaultListModel listModel;
public UserPane(DBHandler db, JPanel parent, CardLayout cards, BufferedImage bg, JFrame frame){
this.db=db;
this.parent=parent;
this.cards = cards;
this.setLayout(null);
this.frame=frame;
password="";
error="";
retype="";
clicked=false;
this.db=db;
started =false;
users = new ArrayList<User>();
curPanel=0;
add = new Button("images/us1a.png", "images/us1.png", "images/us1.png");
update = new Button("images/us2.png", "images/us2a.png", "images/us2.png");
remove = new Button("images/us3.png", "images/us3a.png", "images/us3.png");
typeLabel = new JLabel("User Type :");
userLabel = new JLabel("Username :");
passLabel = new JLabel("Password :");
retypeLabel = new JLabel("Retype Password :");
Font font = new Font("Calibri", Font.PLAIN, 18);
String[] modes = {"Owner", "Manager", "Bookkeeper"};
userBox = new JComboBox(modes);
nameBox =new JTextField();
passBox = new JPasswordField();
retypeBox = new JPasswordField();
typeLabel.setFont(font);
userLabel.setFont(font);
passLabel.setFont(font);
retypeLabel.setFont(font);
typeLabel.setBounds(235, 205, 150, 50);
userLabel.setBounds(235, 255, 150, 50);
passLabel.setBounds(235, 305, 150, 50);
retypeLabel.setBounds(235, 355, 150, 50);
userBox.setBounds(450, 220, 150, 20);
nameBox.setBounds(450, 265, 150, 28);
passBox.setBounds(450, 315, 150, 28);
retypeBox.setBounds(450, 365, 150, 28);
showButtons();
userAdd.setCurImg(0);
salesBtn.setCurImg(0);
inBtn.setCurImg(0);
expBtn.setCurImg(0);
usBtn.setCurImg(0);
logBtn.setCurImg(0);
inBtn.setCurImg(0);
clickHere=false;
this.addMouseListener(this);
this.addMouseMotionListener(this);
selUser=0;
userBox.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent key) {
// TODO Auto-generated method stub
if(key.getSource()==userBox){
selUser = userBox.getSelectedIndex();
}
}
}
);
nameBox.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent key) {
// TODO Auto-generated method stub
userName=nameBox.getText();
getUsers();
}
}
);
passBox.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent key) {
// TODO Auto-generated method stub
password=passBox.getText();
}
}
);
retypeBox.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent key) {
// TODO Auto-generated method stub
retype=retypeBox.getText();
}
}
);
list=new JList();
list.addListSelectionListener(
new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent l) {
// TODO Auto-generated method stub
listSelIndex = list.getSelectedIndex();
}
}
);
KL kl = new KL();
nameBox.addKeyListener(kl);
passBox.addKeyListener(kl);
retypeBox.addKeyListener(kl);
this.add(userBox);
this.add(typeLabel);
this.add(userLabel);
this.add(passLabel);
this.add(retypeLabel);
this.add(nameBox);
this.add(passBox);
this.add(retypeBox);
time = new Timer(300, this);
}
public void showButtons(){
try {
salesBtn = new Button("images/b1.png", "images/b1a.png", "images/b1.png");
inBtn = new Button("images/b2.png", "images/b2a.png", "images/b2.png");
expBtn = new Button("images/b3.png", "images/b3a.png", "images/b3.png");
usBtn = new Button("images/b5a.png", "images/b5.png", "images/b5.png");
logBtn = new Button("images/b7.png", "images/b7a.png", "images/b7.png");
panel1 = ImageIO.read(new File("images/adduser.png"));
panel2 = ImageIO.read(new File("images/updateuser.png"));
panel3= ImageIO.read(new File("images/removeuser.png"));
userAdd = new Button("images/usadd.png", "images/usadda.png", "images/usadd.png");
userUpd=new Button("images/usupd.png", "images/usupda.png", "images/usupd.png");
userRem=new Button("images/usrem.png", "images/usrema.png", "images/usrem.png");
bg=ImageIO.read(new File("images/main2.png"));
} catch (IOException e) {
System.out.print("Image not found");
e.printStackTrace();
}
}
public void showAddComponents(boolean b){
userBox.setVisible(b);
typeLabel.setVisible(b);
userLabel.setVisible(b);
passLabel.setVisible(b);
retypeLabel.setVisible(b);
nameBox.setVisible(b);
passBox.setVisible(b);
retypeBox.setVisible(b);
}
public boolean checkUserName(String username){
for(int i=0; i<users.size();i++){
if(users.get(i).getUserName().equalsIgnoreCase(username)&&!users.get(i).getUserName().equals("")){
return true;}
}
return false;
}
public boolean checkPassword(String password, String retype){
if(password.equals(retype))
return true;
else
return false;
}
public void updateList(){
if(listModel==null)
listModel= new DefaultListModel();
else
listModel.clear();
getUsers();
for(User x: users)
listModel.addElement(x.getUserName());
Font font2 = new Font("Calibri", Font.PLAIN, 16);
list.setModel(listModel);
list.setFont(font2);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
}
@Override
public void actionPerformed(ActionEvent m) {
// TODO Auto-generated method stub
repaint();
switch(curPanel){
case 0: add.setCurImg(0);
break;
case 1: update.setCurImg(1);
break;
case 2: remove.setCurImg(1);
}
if(curPanel==1){
if(list.getSelectedIndex()!=-1){
if(!userBox.hasFocus()&&!passBox.hasFocus()&&!retypeBox.hasFocus()){
if(users.get(listSelIndex).getUsertype().equals("Owner"))
userBox.setSelectedIndex(0);
if(users.get(listSelIndex).getUsertype().equals("Manager"))
userBox.setSelectedIndex(1);
if(users.get(listSelIndex).getUsertype().equals("Bookkeeper"))
userBox.setSelectedIndex(2);
}
nameBox.setText(users.get(listSelIndex).getUserName());
nameBox.setEditable(false);
}
}
if(curPanel==2){
if(list.getSelectedIndex()!=-1){
nameBox.setText(users.get(listSelIndex).getUserName());
}
}
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
time.start();
g2D.drawImage(bg, 0, 0, null);
g2D.drawImage(add.getCurImg(), 10, 130, null);
g2D.drawImage(update.getCurImg(), 10, 250, null);
g2D.drawImage(remove.getCurImg(), 10, 370, null);
g2D.drawImage(usBtn.getCurImg(), 497, 8, null);
g2D.drawImage(salesBtn.getCurImg(), 145, 8, null);
g2D.drawImage(inBtn.getCurImg(), 269, 8, null);
g2D.drawImage(expBtn.getCurImg(), 383, 8, null);
g2D.drawImage(logBtn.getCurImg(), 611, 8, null);
Font font = new Font("Calibri", Font.PLAIN, 20);
g2D.setColor(Color.RED);
g2D.setFont(font);
if(curPanel==0){
g2D.drawImage(panel1, 120, 105, null);
if(started){
userName =nameBox.getText();
}
if(started&&this.checkUserName(userName)){
error = "*Error: Username already exists";
}
else if(checkPass&&!this.checkPassword(password, retype))
error="*Error: Password mismatch";
else if(checkPass&&password.equals("")&&retype.equals(""))
error="*Error: Password must not be empty";
else
error="";
g2D.drawString(error, 250, 205);
g2D.drawImage(userAdd.getCurImg(), 350, 425, null);
}
if(curPanel==1){
g2D.drawImage(panel2, 120, 105, null);
if(checkPass&&!this.checkPassword(password, retype))
error="*Error: Password mismatch";
else if(clicked&&list.getSelectedIndex()==-1)
error="*Error: Choose a username";
else if(clicked&&password.equals("")&&retype.equals(""))
error="*Error: Password cannot be empty";
else
error="";
g2D.drawImage(userUpd.getCurImg(), 463, 426, null);
g2D.drawString(error, 398, 205);
g2D.setColor(Color.BLACK);
g2D.drawString("Not There?", 145, 465);
g2D.setColor(Color.BLUE);
if(clickHere)
g2D.setFont(new Font("Calibri", Font.ITALIC, 16));
else
g2D.setFont(new Font("Calibri", Font.PLAIN, 16));
g2D.drawString("Click Here", 245, 465);
}
if(curPanel==2){
g2D.drawImage(panel3, 120, 105, null);
g2D.drawImage(userRem.getCurImg(), 378, 425, null);
if(clicked&&list.getSelectedIndex()!=-1){
updateList();
clicked=false;
}
}
}
public ArrayList<User> getUsers(){
users = new ArrayList<User>();
ResultSet RS;
try {
RS=db.MySQLSelect("SELECT * from UserAccount");
while(RS.next()){
User temp = new User("", "", "");
temp.setUserName(RS.getString("username"));
temp.setPassword(RS.getString("password"));
temp.setUserType(RS.getString("usertype"));
users.add(temp);
}
RS.close();
}catch(Exception ex){
}
return users;
}
@Override
public void mouseClicked(MouseEvent m) {
// TODO Auto-generated method stub
if (m.getX() >= 151&& m.getX()<=229&&m.getY()>=8&&m.getY()<=86)//within the boundaries of sales btn
cards.show(parent, "sales");
if (m.getX() >= 275&& m.getX()<=354&&m.getY()>=7&&m.getY()<=88){//within the boundaries of inventory btn
cards.show(parent, "inventory");
}
else if (m.getX() >= 390&& m.getX()<=469&&m.getY()>=8&&m.getY()<=86){//within the boundaries of exp btn
cards.show(parent, "expense");
}
if (m.getX() >= 245&& m.getX()<=311&&m.getY()>=454&&m.getY()<=466){//within the boundaries of click here link
nameBox.setEditable(true);
error = "";
started=false;
checkPass=false;
selUser = 0;
userName="";
password = "";
retype = "";
error = "";
nameBox.setText("");
passBox.setText("");
retypeBox.setText("");
nameBox.requestFocus();
update.setCurImg(0);
remove.setCurImg(0);
curPanel=0;
nameBox.setEditable(true);
typeLabel.setBounds(235, 205, 150, 50);
userLabel.setBounds(235, 255, 150, 50);
passLabel.setBounds(235, 305, 150, 50);
retypeLabel.setBounds(235, 355, 150, 50);
userBox.setBounds(450, 220, 150, 20);
nameBox.setBounds(450, 265, 150, 28);
passBox.setBounds(450, 315, 150, 28);
retypeBox.setBounds(450, 365, 150, 28);
showAddComponents(true);
if(scrollPane!=null)
scrollPane.setVisible(false);
if(scrollPane2!=null)
scrollPane2.setVisible(false);
}
if (m.getX() >= 17&& m.getX()<=95&&m.getY()>=129&&m.getY()<=208){//within the boundaries of add btn
//reset values
nameBox.setEditable(true);
error = "";
started=false;
checkPass=false;
selUser = 0;
userName="";
password = "";
retype = "";
error = "";
nameBox.setText("");
passBox.setText("");
retypeBox.setText("");
nameBox.requestFocus();
update.setCurImg(0);
remove.setCurImg(0);
curPanel=0;
nameBox.setEditable(true);
typeLabel.setBounds(235, 205, 150, 50);
userLabel.setBounds(235, 255, 150, 50);
passLabel.setBounds(235, 305, 150, 50);
retypeLabel.setBounds(235, 355, 150, 50);
userBox.setBounds(450, 220, 150, 20);
nameBox.setBounds(450, 265, 150, 28);
passBox.setBounds(450, 315, 150, 28);
retypeBox.setBounds(450, 365, 150, 28);
showAddComponents(true);
if(scrollPane!=null)
scrollPane.setVisible(false);
if(scrollPane2!=null)
scrollPane2.setVisible(false);
}
if (m.getX() >= 350&& m.getX()<=497&&m.getY()>=427&&m.getY()<=466&&curPanel==0){//within the boundaries of add user btn
getUsers();
selUser = userBox.getSelectedIndex();
userName=nameBox.getText();
password=passBox.getText();
retype=retypeBox.getText();
started=true;
if(!this.checkUserName(userName)&&this.checkPassword(password, retype)&&!
retypeBox.getText().equals("")&&!passBox.getText().equals("")&&!nameBox.getText().equals("")){
String userType="";
switch (selUser){
case 2: userType="BookKeeper";
break;
case 0: userType="Owner";
break;
case 1: userType = "Manager";
}
db.MySQLInsert("INSERT into UserAccount values('"+userName+"','"+password+"','"+userType+"');");
}
}
if (m.getX() >= 17&& m.getX()<=95&&m.getY()>=249&&m.getY()<=331){//within the boundaries of update btn
error = "";
clicked=false;
checkPass=false;
error = "";
add.setCurImg(1);
remove.setCurImg(0);
curPanel=1;
showAddComponents(false);
nameBox.setEditable(false);
if(scrollPane2!=null)
scrollPane2.setVisible(false);
updateList();
scrollPane= new JScrollPane(list);
scrollPane.setBounds(145, 185, 175, 260);
typeLabel.setBounds(399, 205, 150, 50);
userLabel.setBounds(399, 255, 150, 50);
passLabel.setBounds(399, 305, 150, 50);
retypeLabel.setBounds(399, 355, 150, 50);
userBox.setBounds(553, 220, 150, 20);
nameBox.setBounds(553, 265, 150, 28);
passBox.setBounds(553, 315, 150, 28);
retypeBox.setBounds(553, 365, 150, 28);
//reset values
selUser = 0;
userName ="";
password = "";
retype = "";
nameBox.setText("");
passBox.setText("");
retypeBox.setText("");
passBox.requestFocus();
showAddComponents(true);
this.add(scrollPane);
}
if (m.getX() >= 462&& m.getX()<=610&&m.getY()>=428&&m.getY()<=468&&curPanel==1){//within the boundaries of update user btn
getUsers();
selUser = userBox.getSelectedIndex();
userName=nameBox.getText();
password=passBox.getText();
retype=retypeBox.getText();
clicked=true;
checkPass=true;
if(list.getSelectedIndex()!=-1&&this.checkPassword(password, retype)&&!
retypeBox.getText().equals("")&&!passBox.getText().equals("")&&!nameBox.getText().equals("")){
String userType="";
switch (selUser){
case 2: userType="BookKeeper";
break;
case 0: userType="Owner";
break;
case 1: userType = "Manager";
}
try{
db.MySQLUpdate("UPDATE UserAccount SET Username='"+userName+"',Password='"+password
+"',UserType='"+userType+"' WHERE Username='"+userName+"';");
}catch(SQLException e) {
// TODO Auto-generated catch block
error="Modification did not occur";
}
getUsers();
}
}
if (m.getX() >= 16&& m.getX()<=97&&m.getY()>=369&&m.getY()<=450){//within the boundaries of remove btn
error = "";
clicked=false;
curPanel=2;
clicked=false;
add.setCurImg(1);
update.setCurImg(0);
showAddComponents(false);
if(scrollPane!=null)
scrollPane.setVisible(false);
updateList();
scrollPane2 = new JScrollPane(list);
scrollPane2.setBounds(346, 218, 200, 170);
scrollPane2.setVisible(true);
this.add(scrollPane2);
}
if (m.getX() >= 378&& m.getX()<=523&&m.getY()>=425&&m.getY()<=468&&curPanel==2){//within the boundaries of remove user btn
error ="";
if(!scrollPane2.hasFocus()){
error ="*Error: Choose a username";
}
if(list.getSelectedIndex()<=users.size()&&list.getSelectedIndex()>=0&&list.getSelectedIndex()!=-1){
clicked=false;
try {
db.MySQLUpdate("DELETE FROM UserAccount where Username='"+users.get(list.getSelectedIndex()).getUserName()+"';");
clicked=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
error = "Modification did not occur";
}
}
}
if (m.getX() >= 617&& m.getX()<=698&&m.getY()>=9&&m.getY()<=85){//within the boundaries of log out btn
final JFrame frame2 = new JFrame("Confirm Action");
JLabel label = new JLabel();
JLabel label2 = new JLabel("Are you sure you want to log out?");
JButton yes = new JButton("Yes");
JButton no = new JButton("No");
yes.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
frame.dispose();
frame2.dispose();
new MainFrame();
}
}
);
yes.setIcon(new ImageIcon("Images/yes.png"));
no.setIcon(new ImageIcon("Images/no.png"));
no.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
frame2.dispose();
}
}
);
label2.setFont(new Font(Font.SERIF, Font.PLAIN, 17));
JPanel panel = new JPanel();
ImageIcon confirm;
confirm= new ImageIcon("Images/confirmaction.png");
label.setIcon(confirm);
panel.add(label);
panel.add(label2);
panel.add(yes);
panel.add(no);
label.setBounds(26, 20, 120, 120);
label2.setBounds(145, 40, 250, 30);
yes.setBounds(160, 105, 80, yes.getPreferredSize().height);
no.setBounds(260, 105, 80, no.getPreferredSize().height+2);
panel.setLayout(null);
frame2.add(panel);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.setSize(400, 200);
frame2.setLocationRelativeTo(null);
}
}
@Override
public void mouseEntered(MouseEvent m) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent m) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent m) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent m) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent m) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent m) {
// TODO Auto-generated method stub
if (m.getX() >= 151&& m.getX()<=229&&m.getY()>=8&&m.getY()<=86)//within the boundaries of sales btn
salesBtn.setCurImg(1);
else if (m.getX() >= 275&& m.getX()<=354&&m.getY()>=7&&m.getY()<=88)//within the boundaries of inventory btn
inBtn.setCurImg(1);
else if (m.getX() >= 390&& m.getX()<=469&&m.getY()>=8&&m.getY()<=86)//within the boundaries of exp btn
expBtn.setCurImg(1);
/*
else if (m.getX() >= 504&& m.getX()<=581&&m.getY()>=7&&m.getY()<=85)//within the boundaries of us btn
usBtn.setCurImg(1);
*/
else if (m.getX() >= 17&& m.getX()<=95&&m.getY()>=129&&m.getY()<=208)//within the boundaries of add btn
add.setCurImg(0);
else if (m.getX() >= 17&& m.getX()<=95&&m.getY()>=249&&m.getY()<=331)//within the boundaries of update btn
update.setCurImg(1);
else if (m.getX() >= 16&& m.getX()<=97&&m.getY()>=369&&m.getY()<=450)//within the boundaries of remove btn
remove.setCurImg(1);
else if (m.getX() >= 350&& m.getX()<=497&&m.getY()>=427&&m.getY()<=466&&curPanel==0)//within the boundaries of add user btn
userAdd.setCurImg(1);
else if (m.getX() >= 462&& m.getX()<=610&&m.getY()>=428&&m.getY()<=468&&curPanel==1)//within the boundaries of update user btn
userUpd.setCurImg(1);
else if (m.getX() >= 378&& m.getX()<=523&&m.getY()>=425&&m.getY()<=468&&curPanel==2)//within the boundaries of remove user btn
userRem.setCurImg(1);
else if (m.getX() >= 245&& m.getX()<=311&&m.getY()>=454&&m.getY()<=466)//within the boundaries of click here link
clickHere=true;
else{
clickHere=false;
salesBtn.setCurImg(0);
inBtn.setCurImg(0);
expBtn.setCurImg(0);
logBtn.setCurImg(0);
add.setCurImg(1);
update.setCurImg(0);
remove.setCurImg(0);
userAdd.setCurImg(0);
userUpd.setCurImg(0);
userRem.setCurImg(0);
}
if (m.getX() >= 617&& m.getX()<=698&&m.getY()>=9&&m.getY()<=85)//within the boundaries of log out btn
logBtn.setCurImg(1);
}
class KL implements KeyListener{
@Override
public void keyPressed(KeyEvent key) {
// TODO Auto-generated method stub
selUser = userBox.getSelectedIndex();
getUsers();
if (key.getSource()==nameBox&&(key.getKeyCode()==KeyEvent.VK_ENTER||key.getKeyCode()==KeyEvent.VK_TAB)){
passBox.requestFocus();
started=true;
checkPass=false;
}
else if(key.getSource()==nameBox){
started=true;
checkPass=false;
}
else if(key.getSource()==passBox&&(key.getKeyCode()==KeyEvent.VK_ENTER||key.getKeyCode()==KeyEvent.VK_TAB)){
retypeBox.requestFocus();
}
else if(key.getSource()==retypeBox&&key.getKeyCode()==KeyEvent.VK_ENTER){
checkPass=true;
started=false;
selUser = userBox.getSelectedIndex();
userName=nameBox.getText();
password=passBox.getText();
retype=retypeBox.getText();
started=true;
if(!checkUserName(userName)&&checkPassword(password, retype)&&!
retypeBox.getText().equals("")&&!passBox.getText().equals("")&&!nameBox.getText().equals("")){
String userType="";
switch (selUser){
case 2: userType="BookKeeper";
break;
case 0: userType="Owner";
break;
case 1: userType = "Manager";
}
db.MySQLInsert("INSERT into UserAccount values('"+userName+"','"+password+"','"+userType+"');");
}
}
}
@Override
public void keyReleased(KeyEvent key) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent key) {
// TODO Auto-generated method stub
}
}
}