/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package club.ui;
import club.beans.AdministratorBean;
import club.beans.ConfigBean;
import club.data.DataAdmin;
import club.data.DataConfig;
import javax.swing.JOptionPane;
/**
*
* @author tuan2
*/
public class Login extends javax.swing.JFrame {
/**
* Creates new form Login
*/
public Login() {
this.setLocation(500, 300);
initComponents();
this.getRootPane().setDefaultButton(btn_Login);
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtAccount = new javax.swing.JTextField();
btn_Login = new javax.swing.JButton();
btn_Exit = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
txtPassword = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Staff Login");
setResizable(false);
jLabel1.setText("Account :");
jLabel2.setText("Passrword :");
txtAccount.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
txtAccountFocusGained(evt);
}
});
btn_Login.setText("Login");
btn_Login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_LoginActionPerformed(evt);
}
});
btn_Exit.setText("Exit");
btn_Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_ExitActionPerformed(evt);
}
});
jLabel3.setText("Please enter your account and password.");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtAccount, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 4, Short.MAX_VALUE)))
.addGap(16, 16, 16))
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(btn_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_Exit, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtAccount, txtPassword});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtAccount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn_Exit, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
.addComponent(btn_Login, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btn_ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ExitActionPerformed
System.exit(0);
}//GEN-LAST:event_btn_ExitActionPerformed
private void btn_LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_LoginActionPerformed
String acc = txtAccount.getText();
String pass = txtPassword.getText();
if (acc == null || pass == null) {
JOptionPane.showMessageDialog(rootPane, "Please input account and password!", "Error!", JOptionPane.ERROR_MESSAGE);
} else {
AdministratorBean auth = DataAdmin.getAuthentication(acc);
if (auth != null && pass.equals(auth.getPassword())) {
String name = auth.getFullname();
//JOptionPane.showMessageDialog(rootPane, "Hello "+name, "Error!", JOptionPane.ERROR_MESSAGE);
DataConfig dataConfig = new DataConfig();
ConfigBean config = dataConfig.readConfigFile();
config.setAdminAccount(acc);
config.setAdminID(auth.getAdminID());
if (dataConfig.writeConfigFile(config)){
new MainFrame();
} else{
JOptionPane.showMessageDialog(rootPane, "Error! Click OK to exit application", "Error!", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
this.dispose();
} else {
JOptionPane.showMessageDialog(rootPane, "Incorrect account or password!", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
}//GEN-LAST:event_btn_LoginActionPerformed
private void txtAccountFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtAccountFocusGained
// TODO add your handling code here:
txtAccount.selectAll();
}//GEN-LAST:event_txtAccountFocusGained
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btn_Exit;
private javax.swing.JButton btn_Login;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField txtAccount;
private javax.swing.JPasswordField txtPassword;
// End of variables declaration//GEN-END:variables
}