001: package net.ar.webonswing.petstore;
002:
003: import java.util.*;
004:
005: import javax.swing.*;
006:
007: import net.ar.webonswing.petstore.helpers.*;
008: import net.ar.webonswing.petstore.model.*;
009: import net.ar.webonswing.swing.components.validators.*;
010:
011: public class CustomerView extends JPanel {
012: protected Customer customer;
013:
014: protected JTextField firstName = new JTextField();
015: protected JTextField lastName = new JTextField();
016: protected JTextField email = new JTextField();
017: protected JTextField telephone = new JTextField();
018: protected JComponent username = new JTextField();
019: protected JPasswordField password = new JPasswordField();
020: protected JPasswordField repeatPassword = new JPasswordField();
021:
022: protected AddressView address = new AddressView();
023: protected CreditCardView creditCard = new CreditCardView();
024:
025: protected Binder binder = new Binder();
026:
027: protected Collection validators;
028:
029: public CustomerView() {
030: init();
031: }
032:
033: public CustomerView(Customer aCustomer, boolean isEditing) {
034: if (isEditing)
035: username.setEnabled(false);
036:
037: init();
038: setCustomer(aCustomer);
039: }
040:
041: protected void init() {
042: binder.add(firstName, "firstname");
043: binder.add(lastName, "lastname");
044: binder.add(email, "email");
045: binder.add(telephone, "telephone");
046: binder.add(creditCard, "creditCard", "creditCard");
047: binder.add(address, "address", "address");
048: binder.add(username, "username");
049: binder.add(password, "password");
050:
051: binder.addViewsToContainer(this );
052: binder.modelToView();
053:
054: JRegularExpressionValidator emailValidator = new JRegularExpressionValidator(
055: email, "*", "Email is incorrect", true,
056: "^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil)$");
057: JRegularExpressionValidator telephoneValidator = new JRegularExpressionValidator(
058: telephone,
059: "*",
060: "Phone number must be in form: (XXX) XXX-XXXX",
061: true,
062: "(^x\\s*[0-9]{5}$)|(^(\\([1-9][0-9]{2}\\)\\s)?[1-9][0-9]{2}-[0-9]{4}(\\sx\\s*[0-9]{5})?$)");
063: JValidator usernameValidator = new JRequiredFieldValidator(
064: username);
065: JValidator passwordValidator = new JRegularExpressionValidator(
066: password, "*",
067: "Password must include one of these (!@#$%^&*+;:)",
068: true, ".*[!@#$%^&*+;:].*");
069: JValidator repeatPasswordValidator = new JCompareValidator(
070: repeatPassword, "*", "Password fields dont match",
071: true, password);
072:
073: add(emailValidator).setName("emailValidator");
074: add(telephoneValidator).setName("telephoneValidator");
075: add(usernameValidator).setName("usernameValidator");
076: add(passwordValidator).setName("passwordValidator");
077: add(repeatPasswordValidator).setName("repeatPasswordValidator");
078: add(repeatPassword).setName("repeatPassword");
079:
080: validators = new Vector(Arrays.asList(new JValidator[] {
081: emailValidator, telephoneValidator, usernameValidator,
082: passwordValidator, repeatPasswordValidator }));
083: validators.addAll(creditCard.getValidators());
084: }
085:
086: public Customer getCustomer() {
087: binder.viewToModel();
088: return customer;
089: }
090:
091: public void setCustomer(Customer aCustomer) {
092: this .customer = aCustomer;
093: binder.setModel(aCustomer);
094: binder.modelToView();
095: }
096:
097: public Collection getValidators() {
098: return validators;
099: }
100:
101: public Binder getBinder() {
102: return binder;
103: }
104: }
|