/*
* File: CartInternalFrame.java
* Created on 23 / desembre / 2007, 19:21
*
* Copyright (C) 2008 M.Àngels Cerveró Abelló
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package client.ws.milanas.gui;
import client.ws.milanas.helpers.beans.Cart;
import client.ws.milanas.helpers.beans.CartItem;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
/**
*
* @author milana
*/
public class CartInternalFrame extends ClosableTab implements ActionListener
{
private JButton modify;
private JButton buy;
private JButton clean;
private Cart cart;
private ArrayList<JTextField> quantities;
private ResourceBundle messages;
/** Creates a new instance of CartInternalFrame */
public CartInternalFrame(Cart cart, int width, MilanasWSClientGUI parent, int indexTab, ResourceBundle messages)
{
super("See Cart", parent, indexTab);
int y = 10;
int x = 0;
this.cart = cart;
this.messages = messages;
ArrayList<CartItem> items = cart.getItems();
Font labelsFont = new Font("SansSerif", Font.BOLD, 12);
JDesktopPane panel = new JDesktopPane();
JScrollPane scroll = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Container content = getContentPane();
if(items.size() > 0)
{
quantities = new ArrayList<JTextField>();
modify = new JButton(messages.getString("button.modify"));
modify.addActionListener(this);
modify.setSize(width / 9, 30);
modify.setLocation(5 * width / 9, y);
panel.add(modify);
buy = new JButton(messages.getString("button.buy"));
buy.addActionListener(this);
buy.setSize(width / 9, 30);
buy.setLocation(6 * width / 9, y);
panel.add(buy);
clean = new JButton(messages.getString("button.clean"));
clean.addActionListener(this);
clean.setSize(width / 9, 30);
clean.setLocation(7 * width / 9, y);
panel.add(clean);
y = y + 40;
JLabel titleLab = new JLabel(messages.getString("label.search.title"));
titleLab.setFont(labelsFont);
titleLab.setSize(3 * width / 9, 30);
titleLab.setLocation(x, y);
panel.add(titleLab);
JLabel asinLab = new JLabel(messages.getString("label.asin"));
asinLab.setFont(labelsFont);
asinLab.setSize(3 * width / 9, 30);
asinLab.setLocation(3 * width / 9, y);
panel.add(asinLab);
JLabel unitPriceLab = new JLabel(messages.getString("label.unit.price"));
unitPriceLab.setFont(labelsFont);
unitPriceLab.setSize(width / 9, 30);
unitPriceLab.setLocation(6 * width / 9, y);
panel.add(unitPriceLab);
JLabel totalPriceLab = new JLabel(messages.getString("label.total.price"));
totalPriceLab.setFont(labelsFont);
totalPriceLab.setSize(width / 9, 30);
totalPriceLab.setLocation(7 * width / 9, y);
panel.add(totalPriceLab);
JLabel quantityLab = new JLabel(messages.getString("label.quantity"));
quantityLab.setSize(width / 9, 30);
quantityLab.setLocation(8 * width / 9, y);
panel.add(quantityLab);
y = y + 30;
JSeparator separator1 = new JSeparator();
separator1.setSize(width, 1);
separator1.setLocation(0, y);
panel.add(separator1);
y = y + 10;
for(CartItem item : items)
{
JLabel title = new JLabel(item.getItem().getTitle());
title.setSize(3 * width / 9, 30);
title.setLocation(x, y);
panel.add(title);
JLabel asin = new JLabel(item.getItem().getAsin());
asin.setSize(3 * width / 9, 30);
asin.setLocation(3 * width / 9, y);
panel.add(asin);
JLabel unitPrice = new JLabel(item.getUnitPrice());
unitPrice.setSize(width / 9, 30);
unitPrice.setLocation(6 * width / 9, y);
panel.add(unitPrice);
JLabel totalPrice = new JLabel(item.getTotalPrice());
totalPrice.setSize(width / 9, 30);
totalPrice.setLocation(7 * width / 9, y);
panel.add(totalPrice);
JTextField quantity = new JTextField(Integer.toString(item.getQuantity()));
quantity.setSize(50, 30);
quantity.setLocation(8 * width / 9, y);
quantities.add(quantity);
panel.add(quantity);
y = y + 30;
}
y = y + 10;
JSeparator separator2 = new JSeparator();
separator2.setSize(width, 1);
separator2.setLocation(0, y);
panel.add(separator2);
JLabel cartTotal = new JLabel(cart.getTotal());
cartTotal.setSize(width / 9, 30);
cartTotal.setLocation(7 * width / 9, y);
panel.add(cartTotal);
}
else
{
JLabel noItem = new JLabel(messages.getString("label.empty"));
noItem.setSize(3 * width / 9, 30);
noItem.setLocation(x, y);
panel.add(noItem);
y = y + 30;
JSeparator separator3 = new JSeparator();
separator3.setSize(width, 1);
separator3.setLocation(0, y);
panel.add(separator3);
}
panel.setPreferredSize(new Dimension (width, y));
content.add(scroll);
}
public void actionPerformed (ActionEvent event)
{
if (event.getSource ().getClass ().getName ().equals ("javax.swing.JButton"))
{
buttonTreatement((JButton) event.getSource());
}
}
private void buttonTreatement(JButton button)
{
int i, quantity = 0;
ArrayList<CartItem> items = null;
ArrayList<CartItem> modifiedItems = null;
CartItem item = null;
if(button == modify)
{
items = cart.getItems();
modifiedItems = new ArrayList<CartItem>();
for(i = 0; i < items.size(); i++)
{
item = new CartItem();
item.setOfferId(items.get(i).getCartItemId());
try
{
quantity = Integer.parseInt(quantities.get(i).getText());
}
catch(NumberFormatException nfe)
{
quantity = 0;
}
item.setQuantity(quantity);
modifiedItems.add(item);
}
parent.modifyCart(modifiedItems, tabIndex);
}
else if(button == buy)
{
try
{
Runtime.getRuntime().exec("firefox " + cart.getUrl());
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null, messages.getString("label.buy.dialog"),
messages.getString("label.buy.dialog.title"),
JOptionPane.ERROR_MESSAGE);
}
}
else if(button == clean)
{
parent.emtpyCart();
}
}
}