001: package net.ar.webonswing.petstore;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.util.*;
006: import java.util.List;
007:
008: import javax.swing.*;
009:
010: import net.ar.webonswing.*;
011: import net.ar.webonswing.petstore.contributors.*;
012: import net.ar.webonswing.petstore.model.*;
013: import net.ar.webonswing.swing.components.validators.*;
014:
015: public class AccountView extends ApplicationFrame implements
016: ActionListener {
017: protected Customer accountOwner = new Customer();
018: private Collection validators;
019: protected JGroupValidator groupValidator;
020: protected CustomerView customerView;
021: private boolean editingCustomer = false;
022:
023: public AccountView() {
024: WosFramework.assignContributor(this ,
025: new HibernateObjectPersistenceContributor(
026: "accountOwner"));
027: }
028:
029: public AccountView(Customer aCustomer) {
030: this (aCustomer, true);
031: }
032:
033: public AccountView(Customer aCustomer, boolean isEditingCustomer) {
034: this ();
035: setAccountOwner(aCustomer);
036: editingCustomer = isEditingCustomer;
037: init();
038: }
039:
040: protected JComponent getBody() {
041: customerView = new CustomerView(accountOwner, editingCustomer);
042: customerView.setLayout(getCurrentSkin()
043: .getCurrentPropagateTemplateFor("CustomerView"));
044: groupValidator = new JGroupValidator();
045: JButton saveButton = new JButton("save");
046: saveButton.addActionListener(this );
047:
048: groupValidator.addValidators(customerView.getValidators());
049: groupValidator.addShooter(saveButton);
050:
051: JValidator registrationValidator = new JValidator() {
052: public boolean isRemoteValidation() {
053: return false;
054: }
055:
056: protected boolean performValidation() {
057: customerView.getBinder().viewToModel();
058:
059: if (!editingCustomer) {
060: List customers = getDao().getCustomers(
061: accountOwner.getUsername());
062: if (!customers.isEmpty())
063: return false;
064: }
065:
066: getDao().saveOrUpdate(accountOwner);
067:
068: return true;
069: }
070: };
071:
072: groupValidator.addValidator(registrationValidator);
073:
074: JPanel panel = new JPanel();
075: panel.add(customerView).setName("customerView");
076: panel.add(groupValidator).setName("groupValidator");
077: panel.add(saveButton).setName("button");
078:
079: panel.setLayout(getCurrentSkin()
080: .getCurrentPropagateTemplateFor("Register.main"));
081:
082: return panel;
083: }
084:
085: public Collection getValidators() {
086: return validators;
087: }
088:
089: public void actionPerformed(ActionEvent e) {
090: if (groupValidator.doValidation()) {
091: Window nextWindow = null;
092:
093: if (editingCustomer)
094: nextWindow = new Home();
095: else
096: nextWindow = new LoginPage();
097:
098: WosFramework.showChildWindow(this , nextWindow);
099: }
100: }
101:
102: public boolean isEditingCustomer() {
103: return editingCustomer;
104: }
105:
106: public void setEditingCustomer(boolean aEditingCustomer) {
107: editingCustomer = aEditingCustomer;
108: }
109:
110: public Customer getAccountOwner() {
111: return accountOwner;
112: }
113:
114: public void setAccountOwner(Customer aAccountOwner) {
115: accountOwner = aAccountOwner;
116: editingCustomer = true;
117: setCurrentCustomer(aAccountOwner);
118: }
119: }
|