KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > dragDrop > CartBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.icefaces.samples.showcase.components.dragDrop;
35
36 import com.icesoft.faces.component.dragdrop.DndEvent;
37 import com.icesoft.faces.component.dragdrop.DragEvent;
38 import com.icesoft.faces.component.ext.HtmlPanelGroup;
39 import com.icesoft.faces.context.effects.Effect;
40 import com.icesoft.faces.context.effects.Highlight;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.Hashtable JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Random JavaDoc;
47
48 /**
49  * <p>The CartBean class is used as a bean for each user to maintain their store
50  * and cart list and available items in the Drag and Drop store demo. It also
51  * handles the store's drag and drop functionality via listeners.</p>
52  */

53 public class CartBean {
54     // store and cart data structures
55
private ArrayList JavaDoc purchasedList = new ArrayList JavaDoc(0);
56     private StoreTable storeTable = new StoreTable();
57
58     // random prices
59
private Random JavaDoc random = new Random JavaDoc(System.currentTimeMillis());
60
61     // effects
62
private Effect cartEffect;
63
64     /**
65      * Loads the default set of store objects with random prices.
66      */

67     public CartBean() {
68         // instantiate the store objects
69
loadDefaults();
70     }
71
72     /**
73      * Gets a list representation of the Store.
74      *
75      * @return list representation of the store
76      */

77     public ArrayList JavaDoc getStoreTableAsList() {
78         return (Collections.list(storeTable.elements()));
79     }
80
81     /**
82      * Gets the list of purchases.
83      *
84      * @return the list of purchases
85      */

86     public ArrayList JavaDoc getPurchasedList() {
87         return (purchasedList);
88     }
89
90     /**
91      * Gets the store table.
92      *
93      * @return the store table
94      */

95     public Hashtable JavaDoc getStoreTable() {
96         return (storeTable);
97     }
98
99     /**
100      * Gets the total price formatted correctly as currency.
101      *
102      * @return the total price
103      */

104     public String JavaDoc getTotalPriceFormatted() {
105         double price = 0.0;
106
107         // Loop through the item list and calculate the total price
108
for (int i = 0; i < purchasedList.size(); i++) {
109             price += ((CartItem) purchasedList.get(i)).getProductTotalDouble();
110         }
111
112         return (StoreTable.CURRENCY_FORMAT.format(price));
113     }
114
115     /**
116      * Determines amount of purchased items.
117      *
118      * @return the number purchased.
119      */

120     public int getPurchasedCount() {
121         Iterator JavaDoc i = purchasedList.iterator();
122         int count = 0;
123
124         while (i.hasNext()) {
125             count += ((CartItem) i.next()).getPurchasedQuantity();
126         }
127
128         return (count);
129     }
130
131     /**
132      * Determines if there are plural items.
133      *
134      * @return correct grammar for item/items
135      */

136     public String JavaDoc getItemString() {
137         if (getPurchasedCount() == 1) {
138             return ("item");
139         }
140         return ("items");
141     }
142
143     /**
144      * Sets the list of purchases.
145      *
146      * @param purchasedList the list of purchases
147      */

148     public void setPurchasedList(ArrayList JavaDoc purchasedList) {
149         this.purchasedList = purchasedList;
150     }
151
152     /**
153      * Sets the Store data.
154      *
155      * @param storeTable the store data
156      */

157     public void setStoreTable(StoreTable storeTable) {
158         this.storeTable = storeTable;
159     }
160
161     /**
162      * Generates a list of default items in the store.
163      */

164     protected void loadDefaults() {
165         storeTable.addItem(new CartItem(this, CartItem.ICE_BERG, Math.floor(
166                 (1 + random.nextDouble() * random.nextInt(19)) * 100) / 100, 1 +
167                                                                              random.nextInt(
168                                                                                      59)));
169         storeTable.addItem(new CartItem(this, CartItem.ICE_SAILER, Math.floor(
170                 (1 + random.nextDouble() * random.nextInt(19)) * 100) / 100, 1 +
171                                                                              random.nextInt(
172                                                                                      59)));
173         storeTable.addItem(new CartItem(this, CartItem.ICE_CASTLE, Math.floor(
174                 (1 + random.nextDouble() * random.nextInt(19)) * 100) / 100, 1 +
175                                                                              random.nextInt(
176                                                                                      59)));
177         storeTable.addItem(new CartItem(this, CartItem.ICE_BREAKER, Math.floor(
178                 (1 + random.nextDouble() * random.nextInt(19)) * 100) / 100, 1 +
179                                                                              random.nextInt(
180                                                                                      59)));
181     }
182
183     /**
184      * Method used to handle the drag and drop events from the page An item can
185      * be dragged from the store to the cart to purchase it.
186      *
187      * @param DragEvent event fired
188      * @return void
189      */

190     public void cartListener(DragEvent event) {
191         if (event.getEventType() == DndEvent.HOVER_START) {
192             String JavaDoc targetId = event.getTargetClientId();
193             if ((targetId != null) &&
194                 (targetId.indexOf("cartDropTarget") != -1)) {
195                 cartEffect = new Highlight();
196
197             }
198         }
199         // only deal with DROPPED event types
200
if (event.getEventType() == DndEvent.DROPPED) {
201             String JavaDoc targetId = event.getTargetClientId();
202             if ((targetId != null) &&
203                 (targetId.indexOf("cartDropTarget") != -1)) {
204                 String JavaDoc value = ((HtmlPanelGroup) event.getComponent())
205                         .getDragValue().toString();
206
207                 // get the actual dropped item from the store table
208
CartItem dragged = storeTable
209                         .getItem(value.substring(0, value.length() - 1));
210
211                 // ensure the dropped target was the cart
212
if (targetId.endsWith("cartDropTarget")) {
213                     // attempt to purchase the item (which checks quantity), and add it if valid
214
if (storeTable.purchaseItem(dragged)) {
215                         addToPurchases(dragged);
216                     }
217
218                 }
219             }
220         }
221     }
222
223     /**
224      * Increases the purchased quantity of an item by 1. Adds the item to the
225      * cart list if it was not already there.
226      *
227      * @param dragged
228      */

229     private void addToPurchases(CartItem dragged) {
230         if (purchasedList.contains(dragged)) {
231             //duplicate
232
int i = purchasedList.indexOf(dragged);
233             CartItem existing = (CartItem) purchasedList.get(i);
234             existing.setEffect(new Highlight());
235         } else {
236             purchasedList.add(dragged);
237         }
238     }
239
240     /**
241      * Reduces the purchased quantity of an item by 1. Removes the item from the
242      * cart list if the quantity reaches 0.
243      *
244      * @param dragged
245      */

246     public void removeFromPurchases(CartItem dragged) {
247         dragged.increaseQuantity();
248
249         // if it is the last one remaining, it is removed from the display
250
if (dragged.getPurchasedQuantity() == 0) {
251             purchasedList.remove(dragged);
252         }
253     }
254
255     /**
256      * Gets effect for shopping cart.
257      *
258      * @return the effect
259      */

260     public Effect getCartEffect() {
261         return cartEffect;
262     }
263
264     /**
265      * Sets effect for shopping cart.
266      *
267      * @param cartEffect the effect
268      */

269     public void setCartEffect(Effect cartEffect) {
270         this.cartEffect = cartEffect;
271     }
272 }
Popular Tags