001: /**
002: * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Gregory Lapouchnian
022: * Patrick Smith
023: * --------------------------------------------------------------------------
024: * $Id: ShoppingCartPanel.java 7027 2005-07-08 14:00:45Z glapouch $
025: * --------------------------------------------------------------------------
026: */package olstore.client;
027:
028: import java.awt.Component;
029: import java.math.BigDecimal;
030: import java.util.ArrayList;
031: import java.util.List;
032:
033: import javax.swing.JPanel;
034:
035: /**
036: * A panel to display the contents of the shopping cart.
037: */
038: public class ShoppingCartPanel extends JPanel {
039:
040: /** The list of items in the shopping cart. */
041: private ArrayList items;
042:
043: /** A reference to the main client. */
044: private OlstoreSwingClient client;
045:
046: /**
047: * A panel to store and display all the items in the shopping cart.
048: *
049: * @param client the client application to which this cart belongs
050: */
051: public ShoppingCartPanel(OlstoreSwingClient client) {
052: items = new ArrayList();
053: this .client = client;
054: }
055:
056: /**
057: * Add a new item to the shopping cart.
058: * @param item the item to be added
059: */
060: public void addNewItemToCart(OrderItem item) {
061: items.add(item);
062: add(new OrderPanel(item));
063: updatePrice(Double.valueOf(item.getPrice()).doubleValue());
064: }
065:
066: /**
067: * Increase the quantity of the given item in the shopping cart by one.
068: * @param item the item whose quantity is to be increased
069: */
070: public void increaseQuantity(OrderItem item) {
071: // remove the corresponding OrderPanel from this component
072: Component[] orderPanels = getComponents();
073:
074: for (int i = 0; i < orderPanels.length; i++) {
075: OrderItem o = ((OrderPanel) orderPanels[i]).getOrderItem();
076:
077: if (o.equals(item)) {
078: o.setQuantity(o.getQuantity() + 1);
079:
080: // repaint this panel
081: ((OrderPanel) orderPanels[i]).updateQuantity();
082:
083: updatePrice(Double.valueOf(o.getPrice()).doubleValue());
084:
085: return;
086: }
087: }
088: }
089:
090: /**
091: * Add change to the total price displayed in the client.
092: * @param change the amount to add to the total (negative number to decrease
093: * the total)
094: */
095: public void updatePrice(double change) {
096: client.updatePrice(new BigDecimal(change));
097: }
098:
099: /**
100: * Check whether there are any items in the shopping cart.
101: * @return true is there are no items in the shopping cart, false otherwise
102: */
103: public boolean isEmpty() {
104: return items.isEmpty();
105: }
106:
107: /**
108: * Remove all items from the shopping cart.
109: */
110: public void removeAllItems() {
111: items = new ArrayList();
112: removeAll();
113:
114: client.setPrice(new BigDecimal(0));
115:
116: validate();
117: repaint();
118: }
119:
120: /**
121: * Remove the given item from the shopping cart.
122: * @param item the item to be removed from the shopping cart.
123: */
124: public void removeFromCart(OrderItem item) {
125: // remove the corresponding OrderPanel from this component
126: Component[] orderPanels = getComponents();
127:
128: for (int i = 0; i < orderPanels.length; i++) {
129: OrderItem o = ((OrderPanel) orderPanels[i]).getOrderItem();
130:
131: if (o.equals(item)) {
132: remove(orderPanels[i]);
133: updatePrice(-o.getTotal());
134: break;
135: }
136: }
137:
138: // remove the item from the list
139: items.remove(item);
140:
141: // repaint this panel
142: validate();
143: repaint();
144: }
145:
146: /**
147: * Get the contents fo the shopping cart.
148: * @return the array of all the OrderItems as an array of Objects
149: */
150: public Object[] getCartContents() {
151: return items.toArray();
152: }
153:
154: /**
155: * Get the items in the cart.
156: * @return a list of all the items in the cart
157: */
158: public List getItemsInCart() {
159: return items;
160: }
161:
162: /**
163: * Check whether the given order item is already in the cart.
164: * @param item the item that we need to check for
165: * @return true if the item is already in the shopping cart, false otherwise
166: */
167: public boolean isItemInCart(OrderItem item) {
168: return items.contains(item);
169: }
170: }
|