package event_manager.views.forms;
import event_manager.views.AppDialog;
import event_manager.controllers.StaffsController;
import event_manager.helpers.FormHelper;
import event_manager.helpers.ViewHelper;
import event_manager.listeners.InputBindingListener;
import event_manager.listeners.LoggingBindingListener;
import event_manager.models.Staff;
import event_manager.validators.EmailValidator;
import event_manager.validators.MinimumLengthValidator;
import event_manager.validators.PhoneNumberValidator;
import event_manager.views.View;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JTextField;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.BindingGroup;
/**
*
* @author uzzaldevkota
*/
public class CompanyConstantForm extends AppDialog {
private Staff staff = new Staff();
private String formTitle = "";
private BindingGroup bindingGroup;
private MinimumLengthValidator minimumLengthValidator;
private File profilePicture;
private final View view;
/**
* Creates new form StaffAppDialog
* @param view View
* @param modal boolean
* @param s Staff
*/
public CompanyConstantForm(View view, boolean modal, Staff s) {
super(view, modal);
this.view = view;
staff = s;
formTitle = (staff.get("id") == null ? "Create" : "Edit") + " Staff";
initComponents();
initBindings();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
}
/**
* Returns <code>Staff</code> being edited.
*
* @return <code>Staff</code> being edited.
*/
public Staff getStaff() {
return staff;
}
/**
* Sets client to edit.
*
* @param staff staff to return
*/
public void setStaff(Staff staff) {
Staff oldStaff = this.staff;
this.staff = staff;
firePropertyChange("staff", oldStaff, staff);
}
private void initBindings() {
bindingGroup = new BindingGroup();
minimumLengthValidator = new MinimumLengthValidator(5);
Binding binding = FormHelper.createBinding(this, "staff", "username", usernameField, "text");
binding.setValidator(minimumLengthValidator);
bindingGroup.addBinding(binding);
binding = FormHelper.createBinding(this, "staff", "password", passwordField, "text");
binding.setValidator(minimumLengthValidator);
bindingGroup.addBinding(binding);
binding = FormHelper.createBinding(this, "staff", "firstName", firstNameField, "text");
minimumLengthValidator.setLength(2);
binding.setValidator(minimumLengthValidator);
bindingGroup.addBinding(binding);
binding = FormHelper.createBinding(this, "staff", "lastName", lastNameField, "text");
binding.setValidator(minimumLengthValidator);
bindingGroup.addBinding(binding);
binding = FormHelper.createBinding(this, "staff", "phoneNo", phoneNumberField, "text");
binding.setValidator(new PhoneNumberValidator());
bindingGroup.addBinding(binding);
binding = FormHelper.createBinding(this, "staff", "email", emailField, "text");
binding.setValidator(new EmailValidator());
bindingGroup.addBinding(binding);
binding = FormHelper.createBinding(this, "staff", "joinedDate", joinedDatePicker, "date");
bindingGroup.addBinding(binding);
// Error when staff is new (createNew)
if(staff.get("is_admin") != null) {
binding = FormHelper.createBinding(this, "staff", "isAdmin", isAdminCheckBox, "selected");
bindingGroup.addBinding(binding);
}
binding = FormHelper.createBinding(this, "staff", "address", addressField, "text");
bindingGroup.addBinding(binding);
binding = FormHelper.createBinding(this, "staff", "profilePicturePath", profilePictureField, "text");
bindingGroup.addBinding(binding);
bindingGroup.bind();
for (Binding b : bindingGroup.getBindings()) {
if (b.getTargetObject() instanceof JTextField) {
b.addBindingListener(new InputBindingListener((JTextField)b.getTargetObject()));
}
}
bindingGroup.addBindingListener(new LoggingBindingListener(noticeLabel));
}
private void submit() {
for (Binding binding : bindingGroup.getBindings()) {
if(binding.save() != null) {
ViewHelper.showDialog("Please validate all fields");
return;
}
}
new StaffsController().save(view, staff);
}
/**
* 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 AppDialog Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
staffFormPanel = new javax.swing.JPanel();
usernameLabel = new javax.swing.JLabel();
usernameField = new javax.swing.JTextField();
passwordLabel = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
passwordField = new javax.swing.JPasswordField();
firstNameLabel = new javax.swing.JLabel();
firstNameField = new javax.swing.JTextField();
lastNameField = new javax.swing.JTextField();
lastNameLabel = new javax.swing.JLabel();
addressLabel = new javax.swing.JLabel();
addressField = new javax.swing.JTextField();
phoneNumberLabel = new javax.swing.JLabel();
phoneNumberField = new javax.swing.JTextField();
emailLabel = new javax.swing.JLabel();
emailField = new javax.swing.JTextField();
joinedDateLabel = new javax.swing.JLabel();
isAdminCheckBox = new javax.swing.JCheckBox();
submitButton = new javax.swing.JButton();
joinedDatePicker = new org.jdesktop.swingx.JXDatePicker();
profilePictureLabel = new javax.swing.JLabel();
profilePictureField = new javax.swing.JTextField();
browseButton = new javax.swing.JButton();
noticeLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
staffFormPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, formTitle, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Abadi MT Condensed Extra Bold", 0, 18))); // NOI18N
usernameLabel.setText("Username:");
passwordLabel.setText("Password:");
jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
firstNameLabel.setText("First Name:");
lastNameLabel.setText("Last Name:");
addressLabel.setText("Address:");
phoneNumberLabel.setText("Phone No:");
emailLabel.setText("Email:");
joinedDateLabel.setText("Joined Date:");
isAdminCheckBox.setText("Admin");
isAdminCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
isAdminCheckBoxActionPerformed(evt);
}
});
submitButton.setText("Submit");
submitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonActionPerformed(evt);
}
});
joinedDatePicker.setToolTipText("");
profilePictureLabel.setText("Profile Picture:");
browseButton.setText("Browse");
browseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout staffFormPanelLayout = new javax.swing.GroupLayout(staffFormPanel);
staffFormPanel.setLayout(staffFormPanelLayout);
staffFormPanelLayout.setHorizontalGroup(
staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(staffFormPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(staffFormPanelLayout.createSequentialGroup()
.addGroup(staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(phoneNumberField, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(firstNameField, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameLabel, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(firstNameLabel, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(phoneNumberLabel, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(joinedDateLabel, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameField, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(joinedDatePicker, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(isAdminCheckBox)
.addComponent(passwordLabel)
.addComponent(lastNameLabel)
.addComponent(emailLabel)
.addComponent(emailField, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
.addComponent(lastNameField)
.addComponent(passwordField)))
.addComponent(addressField)
.addGroup(staffFormPanelLayout.createSequentialGroup()
.addGroup(staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(submitButton)
.addComponent(profilePictureLabel)
.addComponent(addressLabel)
.addComponent(profilePictureField))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(browseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
staffFormPanelLayout.setVerticalGroup(
staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(staffFormPanelLayout.createSequentialGroup()
.addGroup(staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(staffFormPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(staffFormPanelLayout.createSequentialGroup()
.addComponent(usernameLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(firstNameLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(firstNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(phoneNumberLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(phoneNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(joinedDateLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(joinedDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(staffFormPanelLayout.createSequentialGroup()
.addComponent(passwordLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lastNameLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lastNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(emailLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34)
.addComponent(isAdminCheckBox)))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(addressLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(addressField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(profilePictureLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(profilePictureField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(browseButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
.addComponent(submitButton)
.addContainerGap())
);
noticeLabel.setText(" ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(staffFormPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(noticeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(staffFormPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(noticeLabel)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
submit();
}//GEN-LAST:event_submitButtonActionPerformed
private void isAdminCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_isAdminCheckBoxActionPerformed
// Weird :( due to error in binding for new staff :(
staff.setIsAdmin(isAdminCheckBox.isSelected());
}//GEN-LAST:event_isAdminCheckBoxActionPerformed
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
javax.swing.JFileChooser filechooser = new javax.swing.JFileChooser();
filechooser.setMultiSelectionEnabled(false);
int returnVal = filechooser.showDialog(this, "Choose a file");
if(returnVal != javax.swing.JFileChooser.CANCEL_OPTION) {
profilePicture = filechooser.getSelectedFile();
profilePictureField.setText(profilePicture.getPath());
}
}//GEN-LAST:event_browseButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField addressField;
private javax.swing.JLabel addressLabel;
private javax.swing.JButton browseButton;
private javax.swing.JTextField emailField;
private javax.swing.JLabel emailLabel;
private javax.swing.JTextField firstNameField;
private javax.swing.JLabel firstNameLabel;
private javax.swing.JCheckBox isAdminCheckBox;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JLabel joinedDateLabel;
private org.jdesktop.swingx.JXDatePicker joinedDatePicker;
private javax.swing.JTextField lastNameField;
private javax.swing.JLabel lastNameLabel;
private javax.swing.JLabel noticeLabel;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel passwordLabel;
private javax.swing.JTextField phoneNumberField;
private javax.swing.JLabel phoneNumberLabel;
private javax.swing.JTextField profilePictureField;
private javax.swing.JLabel profilePictureLabel;
private javax.swing.JPanel staffFormPanel;
private javax.swing.JButton submitButton;
private javax.swing.JTextField usernameField;
private javax.swing.JLabel usernameLabel;
// End of variables declaration//GEN-END:variables
}