01: package net.ar.webonswing.petstore;
02:
03: import java.awt.event.*;
04: import java.util.*;
05:
06: import javax.swing.*;
07:
08: import net.ar.webonswing.*;
09: import net.ar.webonswing.petstore.helpers.*;
10: import net.ar.webonswing.petstore.model.*;
11: import net.ar.webonswing.swing.layouts.*;
12:
13: public class CartView extends JPanel {
14: protected Cart cart;
15:
16: public CartView(Cart aCart) {
17: cart = aCart;
18: init();
19: }
20:
21: public void init() {
22: removeAll();
23:
24: JPanel conditional = new JPanel();
25:
26: if (!cart.isEmpty()) {
27: final GenericTable table = new GenericTable(new String[] {
28: "itemId", "description", "unitCost", "quantity",
29: "subTotal", "remove" });
30:
31: for (Iterator i = cart.getCartItems().iterator(); i
32: .hasNext();) {
33: final CartItem cartItem = (CartItem) i.next();
34: final Item item = cartItem.getItem();
35:
36: JLabel itemId = new JLabel(item.getItemId());
37: itemId.addMouseListener(new ShowItemAction(item));
38:
39: JButton removeItem = new JButton(item.getItemId());
40: removeItem.addActionListener(new ActionListener() {
41: public void actionPerformed(ActionEvent aE) {
42: cart.removeItem(item);
43: init();
44: }
45: });
46:
47: JTextField quantityField = new JTextField(cartItem
48: .getQuantity()
49: + "") {
50: public void setText(String t) {
51: super .setText(t);
52:
53: if (cartItem != null)
54: cartItem.setQuantity(Integer.parseInt(t));
55: }
56: };
57:
58: table.addRow(new JComponent[] { itemId,
59: new JLabel(item.getDescription()),
60: new JLabel(item.getUnitCost() + ""),
61: quantityField,
62: new JLabel(cartItem.getTotal() + ""),
63: removeItem });
64: }
65:
66: JButton updateButton = new JButton("update");
67: updateButton.addActionListener(new ActionListener() {
68: public void actionPerformed(ActionEvent e) {
69: init();
70: }
71: });
72:
73: JButton checkoutButton = new JButton("Checkout");
74: checkoutButton.addActionListener(new ActionListener() {
75: public void actionPerformed(ActionEvent e) {
76: Checkout checkout = new Checkout();
77: checkout.setModal(true);
78: WosFramework.showChildWindow(CartView.this
79: .getTopLevelAncestor(), checkout);
80: }
81: });
82:
83: conditional.setName("cartActive");
84: conditional.add(table);
85: conditional.add(new JLabel(cart.getTotal() + "")).setName(
86: "total");
87: conditional.add(updateButton).setName("updateButton");
88: conditional.add(checkoutButton).setName("checkoutButton");
89: } else
90: conditional.setName("cartEmpty");
91:
92: add(conditional);
93: setLayout(new PropagateTemplateLayoutByName(ApplicationFrame
94: .getCurrentSkin()
95: .getCurrentTemplateFor("CartView.main"),
96: new TemplateFlowLayout(), true));
97: }
98: }
|