package event_manager.views.forms;
import event_manager.views.AppDialog;
import event_manager.controllers.ClientsController;
import event_manager.helpers.FormHelper;
import event_manager.helpers.ValidationHelper;
import event_manager.listeners.InputBindingListener;
import event_manager.listeners.LoggingBindingListener;
import event_manager.models.Client;
import event_manager.validators.MinimumLengthValidator;
import event_manager.views.View;
import javax.swing.JFrame;
import javax.swing.JTextField;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.BindingGroup;
/**
*
* @author uzzaldevkota
*/
public class ClientForm extends AppDialog {
private Client client = new Client();
private String formTitle = "";
private BindingGroup bindingGroup;
private MinimumLengthValidator minimumLengthValidator;
private final View view;
/**
* Creates new form StaffAppDialogOld
* @param view View
* @param modal boolean
* @param client Client
*/
public ClientForm(View view, boolean modal, Client client) {
super(view, modal);
this.view = view;
this.client = client;
formTitle = (client.get("id") == null ? "Create" : "Edit") + " Client";
initComponents();
initBindings();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
}
/**
* Returns <code>Client</code> being edited.
*
* @return <code>Client</code> being edited.
*/
public Client getClient() {
return client;
}
/**
* Sets client to edit.
*
* @param client client to return
*/
public void setClient(Client client) {
Client oldClient = this.client;
this.client = client;
firePropertyChange("client", oldClient, client);
}
private void initBindings() {
bindingGroup = new BindingGroup();
minimumLengthValidator = new MinimumLengthValidator(5);
Binding binding = FormHelper.createBinding(this, "client", "name", nameField, "text");
binding.setValidator(minimumLengthValidator);
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() {
if(ValidationHelper.validate(bindingGroup) && new ClientsController().save(view, client)) {
dispose();
}
}
/**
* 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 AppDialogOld 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();
nameField = new javax.swing.JTextField();
submitButton = new javax.swing.JButton();
noticeLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_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("Name:");
submitButton.setText("Submit");
submitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonActionPerformed(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)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(usernameLabel)
.addComponent(submitButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
staffFormPanelLayout.setVerticalGroup(
staffFormPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(staffFormPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(usernameLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(submitButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
noticeLabel.setText(" ");
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.LEADING)
.addComponent(staffFormPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(noticeLabel, javax.swing.GroupLayout.Alignment.TRAILING, 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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(noticeLabel))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
submit();
}//GEN-LAST:event_submitButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField nameField;
private javax.swing.JLabel noticeLabel;
private javax.swing.JPanel staffFormPanel;
private javax.swing.JButton submitButton;
private javax.swing.JLabel usernameLabel;
// End of variables declaration//GEN-END:variables
}