1 package de.orangecafe.amazonrcp.gui.commands;
2
3 import org.springframework.richclient.command.ActionCommand;
4 import org.springframework.richclient.application.ApplicationException;
5 import org.springframework.richclient.application.Application;
6 import org.springframework.richclient.application.event.LifecycleApplicationEvent;
7 import org.springframework.richclient.util.Assert;
8
9 import javax.swing.event.ListSelectionListener;
10 import javax.swing.event.ListSelectionEvent;
11 import javax.swing.*;
12 import java.util.List;
13 import java.util.prefs.Preferences;
14
15 import com.amazon.webservices.awsecommerceservice._2007_04_04.Item;
16 import com.amazon.webservices.awsecommerceservice._2007_04_04.Cart;
17 import de.orangecafe.amazonrcp.helper.SelectedItemListHolder;
18 import de.orangecafe.amazonrcp.helper.CartHolder;
19 import de.orangecafe.amazonrcp.service.IAmazonService;
20 import de.orangecafe.amazonrcp.service.ServiceException;
21 import de.orangecafe.amazonrcp.AmazonRCP;
22
23
24
25
26 public class AddToCartCommand extends ActionCommand implements ListSelectionListener {
27 private List<Item> _selectedItems;
28 private IAmazonService _service;
29
30 private Preferences _preferences = Preferences.userNodeForPackage(AmazonRCP.class);
31
32 public AddToCartCommand() {
33 super("addToCartCommand");
34 }
35
36 public void setSelectedItems(final SelectedItemListHolder holder) {
37 _selectedItems = holder.getSelectedItems();
38 }
39
40 public void setAmazonService(final IAmazonService service) {
41 _service = service;
42 }
43
44 protected void doExecuteCommand() {
45 String cartId = _preferences.get(CartHolder.KEY_CART_ID, null);
46 String cartHMAC = _preferences.get(CartHolder.KEY_CART_HMAC, null);
47
48 Assert.notNull(cartId);
49 Assert.notNull(cartHMAC);
50
51 try {
52 _service.addToCart(cartId, cartHMAC, _selectedItems);
53 } catch (ServiceException e) {
54 throw new ApplicationException(e);
55 }
56
57
58 Application.instance().getApplicationContext().publishEvent(new LifecycleApplicationEvent(LifecycleApplicationEvent.CREATED, Cart.class));
59 }
60
61 public void valueChanged(final ListSelectionEvent event) {
62 ListSelectionModel model = (ListSelectionModel) event.getSource();
63 this.setEnabled(!model.isSelectionEmpty());
64 }
65 }