/*
* Userform.java
*
* Created on Oct 19, 2009, 4:56:48 PM
*/
/**
* Appfrica Labs Uganda Ltd Copyrigth @since 2009
* @version 2
*/
/**
* This class is responsible for creating user forms; the users have
* different categories: caller, operator and administrator categories
* The class will adjust according to a category ID stored in the database
*/
package questionbox;
import javax.swing.JOptionPane;
import qbox.model.*;
/**
*
* @author Victor Miclovich
*/
public class UserForm extends javax.swing.JFrame {
private int UsertypeID;
private User usr;
private void intializelabel()
{
btnSearch.setText("Search");
lblUsername.setText("Username");
lblpassword.setText("Password");
lblemail.setText("Email");
lblfirstname.setText("First name");
lbllastname.setText("Last name");
lblPhone.setText("Phone");
rbMale.setText("Male");
rbFemale.setText("Female");
lblGender.setText("Gender");
btnSaveContinue.setText("Save & Continue");
btnNext.setText("Next >>");
}
/**
* Creates new form Userform
* @param usertypeid
*/
public UserForm(int usertypeid) {
initComponents();
intializelabel();
setLocationRelativeTo(null);
setResizable(false);
//Setting the type of user being handled
UsertypeID = usertypeid;
if (UsertypeID != 0) {
switch (UsertypeID) {
case 1:
lblSearch.setText("Search Operator by Username");
this.setTitle("Add/Update Operator Form");
lblAddUser.setText("OR Add Operator");
btnNext.setText("Cancel");
break;
case 2:
lblSearch.setText("Search Admin by Username");
this.setTitle("Add/Update Admin Form");
lblAddUser.setText("OR Add Admin");
break;
case 3:
lblSearch.setText("Search Expert by Username");
this.setTitle("Add/Update Expert Form");
lblAddUser.setText("OR Add Expert");
break;
case 4:
lblSearch.setText("Search Caller by Username");
this.setTitle("Add/Update Caller Form");
lblAddUser.setText("OR Add Caller");
lblpassword.setVisible(false);
txtPassword.setVisible(false);
break;
}
}
}
/** 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() {
GpGender = new javax.swing.ButtonGroup();
plnMain = new javax.swing.JPanel();
btnSearch = new javax.swing.JButton();
txtSearch = new javax.swing.JTextField();
lblSearch = new javax.swing.JLabel();
pnlUserfields = new javax.swing.JPanel();
txtUsername = new javax.swing.JTextField();
lblUsername = new javax.swing.JLabel();
lblpassword = new javax.swing.JLabel();
txtEmail = new javax.swing.JTextField();
txtFirstname = new javax.swing.JTextField();
txtLastname = new javax.swing.JTextField();
lblemail = new javax.swing.JLabel();
lblfirstname = new javax.swing.JLabel();
lbllastname = new javax.swing.JLabel();
txtPassword = new javax.swing.JPasswordField();
rbMale = new javax.swing.JRadioButton();
rbFemale = new javax.swing.JRadioButton();
lblGender = new javax.swing.JLabel();
lblPhone = new javax.swing.JLabel();
txtPhone = new javax.swing.JTextField();
lblAddUser = new javax.swing.JLabel();
btnSaveContinue = new javax.swing.JButton();
btnNext = new javax.swing.JButton();
jSeparator1 = new javax.swing.JSeparator();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setName("Form"); // NOI18N
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
plnMain.setName("plnMain"); // NOI18N
btnSearch.setName("btnSearch"); // NOI18N
btnSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSearchActionPerformed(evt);
}
});
txtSearch.setName("txtSearch"); // NOI18N
lblSearch.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
lblSearch.setName("lblSearch"); // NOI18N
pnlUserfields.setName("pnlUserfields"); // NOI18N
txtUsername.setName("txtUsername"); // NOI18N
lblUsername.setName("lblUsername"); // NOI18N
lblpassword.setName("lblpassword"); // NOI18N
txtEmail.setName("txtEmail"); // NOI18N
txtFirstname.setName("txtFirstname"); // NOI18N
txtLastname.setName("txtLastname"); // NOI18N
lblemail.setName("lblemail"); // NOI18N
lblfirstname.setName("lblfirstname"); // NOI18N
lbllastname.setName("lbllastname"); // NOI18N
txtPassword.setName("txtPassword"); // NOI18N
GpGender.add(rbMale);
rbMale.setName("rbMale"); // NOI18N
GpGender.add(rbFemale);
rbFemale.setName("rbFemale"); // NOI18N
lblGender.setName("lblGender"); // NOI18N
lblPhone.setName("lblPhone"); // NOI18N
txtPhone.setName("txtPhone"); // NOI18N
txtPhone.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtPhoneActionPerformed(evt);
}
});
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(questionbox.QuestionBoxApp.class).getContext().getResourceMap(UserForm.class);
lblAddUser.setText(resourceMap.getString("lblAddUser.text")); // NOI18N
lblAddUser.setName("lblAddUser"); // NOI18N
javax.swing.GroupLayout pnlUserfieldsLayout = new javax.swing.GroupLayout(pnlUserfields);
pnlUserfields.setLayout(pnlUserfieldsLayout);
pnlUserfieldsLayout.setHorizontalGroup(
pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlUserfieldsLayout.createSequentialGroup()
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlUserfieldsLayout.createSequentialGroup()
.addGap(55, 55, 55)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbllastname)
.addComponent(lblfirstname)
.addComponent(lblemail)
.addComponent(lblpassword)
.addComponent(lblGender)
.addComponent(lblPhone))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtEmail)
.addComponent(txtFirstname)
.addComponent(txtLastname)
.addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
.addComponent(txtPassword)
.addGroup(pnlUserfieldsLayout.createSequentialGroup()
.addComponent(rbMale)
.addGap(18, 18, 18)
.addComponent(rbFemale))
.addGroup(pnlUserfieldsLayout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(txtPhone))))
.addGroup(pnlUserfieldsLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblAddUser, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(38, Short.MAX_VALUE))
);
pnlUserfieldsLayout.setVerticalGroup(
pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlUserfieldsLayout.createSequentialGroup()
.addComponent(lblAddUser, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlUserfieldsLayout.createSequentialGroup()
.addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblpassword))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblemail))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtFirstname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblfirstname))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtLastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbllastname)))
.addComponent(lblUsername))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(rbMale)
.addComponent(rbFemale))
.addComponent(lblGender))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(pnlUserfieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblPhone))
.addContainerGap())
);
lblAddUser.getAccessibleContext().setAccessibleName(resourceMap.getString("jLabel1.AccessibleContext.accessibleName")); // NOI18N
btnSaveContinue.setName("btnSaveContinue"); // NOI18N
btnSaveContinue.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveContinueActionPerformed(evt);
}
});
btnNext.setName("btnNext"); // NOI18N
btnNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNextActionPerformed(evt);
}
});
jSeparator1.setName("jSeparator1"); // NOI18N
javax.swing.GroupLayout plnMainLayout = new javax.swing.GroupLayout(plnMain);
plnMain.setLayout(plnMainLayout);
plnMainLayout.setHorizontalGroup(
plnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, plnMainLayout.createSequentialGroup()
.addContainerGap(90, Short.MAX_VALUE)
.addComponent(btnSaveContinue, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(149, 149, 149)
.addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(82, 82, 82))
.addGroup(plnMainLayout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(pnlUserfields, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(128, Short.MAX_VALUE))
.addGroup(plnMainLayout.createSequentialGroup()
.addGap(185, 185, 185)
.addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(89, Short.MAX_VALUE))
.addGroup(plnMainLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, plnMainLayout.createSequentialGroup()
.addContainerGap(26, Short.MAX_VALUE)
.addComponent(lblSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(358, 358, 358))
);
plnMainLayout.setVerticalGroup(
plnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(plnMainLayout.createSequentialGroup()
.addContainerGap()
.addGroup(plnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(plnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(5, 5, 5)
.addComponent(pnlUserfields, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addGroup(plnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnSaveContinue)
.addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(19, Short.MAX_VALUE))
);
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()
.addComponent(plnMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(plnMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
// ur handling code here:
// lblSearch.setText("Search");
}//GEN-LAST:event_formWindowActivated
private void txtPhoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPhoneActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtPhoneActionPerformed
/**
* Search for a user by user name from the database.
* @param evt
*/
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
usr = new User();
try {
usr = User.getUserByUsername(txtSearch.getText());
if (usr != null && usr.getTypeid() == UsertypeID) {
txtUsername.setText(usr.getUsername());
txtPassword.setText(usr.getPassword());
txtFirstname.setText(usr.getFname());
txtLastname.setText(usr.getLname());
txtEmail.setText(usr.getEmail());
// gender search
if (usr.getGender().equals("M")) {
rbMale.setSelected(true); // male button set to true
} else if (usr.getGender().equals("F")) {
rbFemale.setSelected(true); // set female radio button to true
}
txtPhone.setText(usr.getPhone()); // populate phone field
} else {
JOptionPane.showMessageDialog(this, "User not found!\n", "Search Results", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
System.out.println( e.getMessage());
JOptionPane.showMessageDialog(this, "Search failed!\n" , "Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_btnSearchActionPerformed
/**
* Saving to database
* @param evt
*/
private void btnSaveContinueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveContinueActionPerformed
try {
if (usr != null && usr.getUserid() != 0) //update the caller.
{
usr.setUsername(txtUsername.getText());
usr.setEmail(txtEmail.getText());
usr.setFname(txtFirstname.getText());
usr.setLname(txtLastname.getText());
usr.setPhone(txtPhone.getText());
if (rbMale.isEnabled()) {
usr.setGender("M");
} else if (rbFemale.isEnabled()) {
usr.setGender("F");
}
usr.UpdateUser();
} else //Create a new caller.
{
usr = new User();
usr.setUsername(txtUsername.getText());
usr.setEmail(txtEmail.getText());
usr.setTypeid(this.UsertypeID);
usr.setFname(txtFirstname.getText());
usr.setLname(txtLastname.getText());
usr.setPhone(txtPhone.getText());
if (rbMale.isEnabled()) {
usr.setGender("M");
} else if (rbFemale.isEnabled()) {
usr.setGender("F");
}
usr.AddUser();
}
JOptionPane.showMessageDialog(this, "User was added or updated successfully!\n", "Message Report", JOptionPane.ERROR_MESSAGE);
if (StaticMain.ticket != null)
{
StaticMain.ticket.setCallerid(usr.getUserid());
StaticMain.search = new Search();
StaticMain.search.show();
}
this.dispose();
} catch (Exception e) {
System.out.println( e.getMessage());
JOptionPane.showMessageDialog(this, "Update/Insert failed!\n" , "Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_btnSaveContinueActionPerformed
/**
* Going to the next event
* @param evt
*/
private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
if (StaticMain.ticket != null)
{
StaticMain.ticket.setCallerid(this.UsertypeID);
StaticMain.ticket.setOperatorid(StaticMain.loggedinuser.getUserid());
StaticMain.ticket.setCallerid(usr.getUserid());
StaticMain.search = new Search();
StaticMain.search.show();
}
this.dispose();
}//GEN-LAST:event_btnNextActionPerformed
/**
* Close window
* @param evt
*/
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
this.dispose();
}//GEN-LAST:event_formWindowClosing
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
UserForm usrForm = new UserForm(1);
// usrForm.
usrForm.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup GpGender;
private javax.swing.JButton btnNext;
private javax.swing.JButton btnSaveContinue;
private javax.swing.JButton btnSearch;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JLabel lblAddUser;
private javax.swing.JLabel lblGender;
private javax.swing.JLabel lblPhone;
private javax.swing.JLabel lblSearch;
private javax.swing.JLabel lblUsername;
private javax.swing.JLabel lblemail;
private javax.swing.JLabel lblfirstname;
private javax.swing.JLabel lbllastname;
private javax.swing.JLabel lblpassword;
private javax.swing.JPanel plnMain;
private javax.swing.JPanel pnlUserfields;
private javax.swing.JRadioButton rbFemale;
private javax.swing.JRadioButton rbMale;
private javax.swing.JTextField txtEmail;
private javax.swing.JTextField txtFirstname;
private javax.swing.JTextField txtLastname;
private javax.swing.JPasswordField txtPassword;
private javax.swing.JTextField txtPhone;
private javax.swing.JTextField txtSearch;
private javax.swing.JTextField txtUsername;
// End of variables declaration//GEN-END:variables
}