1 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 ; 43 import java.util.Collections ; 44 import java.util.Hashtable ; 45 import java.util.Iterator ; 46 import java.util.Random ; 47 48 53 public class CartBean { 54 private ArrayList purchasedList = new ArrayList (0); 56 private StoreTable storeTable = new StoreTable(); 57 58 private Random random = new Random (System.currentTimeMillis()); 60 61 private Effect cartEffect; 63 64 67 public CartBean() { 68 loadDefaults(); 70 } 71 72 77 public ArrayList getStoreTableAsList() { 78 return (Collections.list(storeTable.elements())); 79 } 80 81 86 public ArrayList getPurchasedList() { 87 return (purchasedList); 88 } 89 90 95 public Hashtable getStoreTable() { 96 return (storeTable); 97 } 98 99 104 public String getTotalPriceFormatted() { 105 double price = 0.0; 106 107 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 120 public int getPurchasedCount() { 121 Iterator 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 136 public String getItemString() { 137 if (getPurchasedCount() == 1) { 138 return ("item"); 139 } 140 return ("items"); 141 } 142 143 148 public void setPurchasedList(ArrayList purchasedList) { 149 this.purchasedList = purchasedList; 150 } 151 152 157 public void setStoreTable(StoreTable storeTable) { 158 this.storeTable = storeTable; 159 } 160 161 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 190 public void cartListener(DragEvent event) { 191 if (event.getEventType() == DndEvent.HOVER_START) { 192 String targetId = event.getTargetClientId(); 193 if ((targetId != null) && 194 (targetId.indexOf("cartDropTarget") != -1)) { 195 cartEffect = new Highlight(); 196 197 } 198 } 199 if (event.getEventType() == DndEvent.DROPPED) { 201 String targetId = event.getTargetClientId(); 202 if ((targetId != null) && 203 (targetId.indexOf("cartDropTarget") != -1)) { 204 String value = ((HtmlPanelGroup) event.getComponent()) 205 .getDragValue().toString(); 206 207 CartItem dragged = storeTable 209 .getItem(value.substring(0, value.length() - 1)); 210 211 if (targetId.endsWith("cartDropTarget")) { 213 if (storeTable.purchaseItem(dragged)) { 215 addToPurchases(dragged); 216 } 217 218 } 219 } 220 } 221 } 222 223 229 private void addToPurchases(CartItem dragged) { 230 if (purchasedList.contains(dragged)) { 231 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 246 public void removeFromPurchases(CartItem dragged) { 247 dragged.increaseQuantity(); 248 249 if (dragged.getPurchasedQuantity() == 0) { 251 purchasedList.remove(dragged); 252 } 253 } 254 255 260 public Effect getCartEffect() { 261 return cartEffect; 262 } 263 264 269 public void setCartEffect(Effect cartEffect) { 270 this.cartEffect = cartEffect; 271 } 272 } | Popular Tags |