01: package net.ar.webonswing.petstore;
02:
03: import java.util.*;
04:
05: import javax.swing.*;
06:
07: import net.ar.webonswing.petstore.helpers.*;
08: import net.ar.webonswing.petstore.model.*;
09: import net.ar.webonswing.swing.components.validators.*;
10:
11: public class CreditCardView extends JPanel {
12: protected CreditCard creditCard;
13: protected Binder binder = new Binder();
14: protected Collection validators = new Vector();
15: private boolean editing = true;
16:
17: public CreditCardView() {
18: init();
19: }
20:
21: public CreditCardView(CreditCard aCreditCard, boolean isEditing) {
22: editing = isEditing;
23: setCreditCard(aCreditCard);
24: init();
25: }
26:
27: protected void init() {
28: JComponent number = getTextComponent();
29: JComponent expiryDate = getTextComponent();
30: JComponent type = new JLabel();
31: if (editing)
32: type = new JComboBox(CreditCard.getTypes());
33:
34: binder.add(number, "number");
35: binder.add(expiryDate, "expiryDate");
36: binder.add(type, editing ? "selectedItem" : "text", "type");
37:
38: binder.addViewsToContainer(this );
39: binder.modelToView();
40:
41: if (editing) {
42: JValidator numberValidator = new JRequiredFieldValidator(
43: number, "You must enter a number");
44: JValidator expiryDateValidator = new JCompareValidator(
45: expiryDate, "*", "You must enter a valid date",
46: true, JCompareValidator.Operation.dataTypeCheck,
47: JCompareValidator.Type.DATE("MM-yy"));
48: JValidator typeValidator = new JRequiredFieldValidator(
49: type, "You must enter a card type");
50:
51: add(numberValidator).setName("numberValidator");
52: add(expiryDateValidator).setName("expiryDateValidator");
53: add(typeValidator).setName("typeValidator");
54:
55: validators = new Vector(Arrays
56: .asList(new JValidator[] { numberValidator,
57: expiryDateValidator, typeValidator }));
58: }
59: }
60:
61: protected JComponent getTextComponent() {
62: if (editing)
63: return new JTextField();
64: else
65: return new JLabel();
66: }
67:
68: public CreditCard getCreditCard() {
69: binder.viewToModel();
70: return creditCard;
71: }
72:
73: public void setCreditCard(CreditCard aCreditCard) {
74: this .creditCard = aCreditCard;
75: binder.setModel(aCreditCard);
76: binder.modelToView();
77: }
78:
79: public Collection getValidators() {
80: return validators;
81: }
82: }
|