1 38 39 40 package org.apache.cocoon.faces.samples.carstore; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 import org.apache.cocoon.faces.samples.components.components.AreaSelectedEvent; 46 47 import javax.faces.context.FacesContext; 48 import javax.faces.event.ActionEvent; 49 50 import java.util.HashMap ; 51 import java.util.Iterator ; 52 import java.util.Locale ; 53 import java.util.Map ; 54 55 85 86 public class CarStore extends Object { 87 88 protected static final Log log = LogFactory.getLog(CarStore.class); 89 90 static final String CARSTORE_PREFIX = "org.apache.cocoon.faces.samples.carstore"; 91 92 static final String DEFAULT_MODEL = "Jalopy"; 93 94 static final String DEFAULT_PACKAGE = "Custom"; 95 96 static final String DEFAULT_MODEL_PROPERTIES = CARSTORE_PREFIX + 97 ".bundles." + DEFAULT_MODEL; 98 99 static final String DEFAULT_PACKAGE_PROPERTIES = CARSTORE_PREFIX + 100 ".bundles." + DEFAULT_PACKAGE; 101 102 106 110 private Map locales = null; 111 112 115 116 private String currentModelName = DEFAULT_MODEL; 117 118 126 private Map carModels = null; 127 128 134 135 private Map carCustomizers = null; 136 137 138 public CarStore() { 139 if (log.isDebugEnabled()) { 140 log.debug("Creating main CarStore bean"); 141 log.debug("Populating locale map"); 142 } 143 locales = new HashMap (); 144 locales.put("NAmerica", Locale.ENGLISH); 145 locales.put("SAmerica", new Locale ("es", "es")); 146 locales.put("Germany", Locale.GERMAN); 147 locales.put("France", Locale.FRENCH); 148 } 149 150 154 public void chooseLocaleFromMap(ActionEvent actionEvent) { 155 AreaSelectedEvent event = (AreaSelectedEvent) actionEvent; 156 String current = event.getMapComponent().getCurrent(); 157 FacesContext context = FacesContext.getCurrentInstance(); 158 context.getViewRoot().setLocale((Locale ) locales.get(current)); 159 resetMaps(); 160 } 161 162 163 public void chooseLocaleFromLink(ActionEvent event) { 164 String current = event.getComponent().getId(); 165 FacesContext context = FacesContext.getCurrentInstance(); 166 context.getViewRoot().setLocale((Locale ) locales.get(current)); 167 resetMaps(); 168 } 169 170 171 private void resetMaps() { 172 if (null != carModels) { 173 carModels.clear(); 174 carModels = null; 175 } 176 if (null != carCustomizers) { 177 carCustomizers.clear(); 178 carCustomizers = null; 179 } 180 } 181 182 183 public void choosePackage(ActionEvent event) { 184 String packageName = event.getComponent().getId(); 185 choosePackage(packageName); 186 } 187 188 189 public void choosePackage(String packageName) { 190 CarCustomizer packageCustomizer = 191 (CarCustomizer) carCustomizers.get(packageName); 192 packageCustomizer.customizeCar(getCurrentModel()); 193 getCurrentModel().getCurrentPrice(); 194 195 String curName; 197 Iterator iter = carCustomizers.keySet().iterator(); 198 while (iter.hasNext()) { 201 curName = (String ) iter.next(); 202 packageCustomizer = (CarCustomizer) carCustomizers.get(curName); 203 if (curName.equals(packageName)) { 204 packageCustomizer.setButtonStyle("package-selected"); 205 } else { 206 packageCustomizer.setButtonStyle("package-unselected"); 207 } 208 } 209 } 210 211 215 public String storeFrontJalopyPressed() { 216 setCurrentModelName("Jalopy"); 217 return "carDetail"; 218 } 219 220 221 public String storeFrontRoadsterPressed() { 222 setCurrentModelName("Roadster"); 223 return "carDetail"; 224 } 225 226 227 public String storeFrontLuxuryPressed() { 228 setCurrentModelName("Luxury"); 229 return "carDetail"; 230 } 231 232 233 public String storeFrontSUVPressed() { 234 setCurrentModelName("SUV"); 235 return "carDetail"; 236 } 237 238 239 public String buyCurrentCar() { 240 getCurrentModel().getCurrentPrice(); 241 return "confirmChoices"; 242 } 243 244 248 public CarBean getCurrentModel() { 249 CarBean result = (CarBean) carModels.get(getCurrentModelName()); 250 return result; 251 } 252 253 254 public Map getModels() { 255 if (null == carModels) { 256 carModels = new HashMap (); 257 if (log.isDebugEnabled()) { 258 log.debug("Populating carModel map"); 259 } 260 carModels.put(DEFAULT_MODEL, 261 new CarBean(DEFAULT_MODEL_PROPERTIES)); 262 carModels.put("Roadster", 263 new CarBean(CARSTORE_PREFIX + ".bundles.Roadster")); 264 carModels.put("Luxury", new CarBean(CARSTORE_PREFIX + 265 ".bundles.Luxury")); 266 carModels.put("SUV", new CarBean(CARSTORE_PREFIX + 267 ".bundles.SUV")); 268 } 269 270 return carModels; 271 } 272 273 274 public Map getCustomizers() { 275 getModels(); 276 if (null == carCustomizers) { 277 carCustomizers = new HashMap (); 278 if (log.isDebugEnabled()) { 279 log.debug("Populating carCustomizers map"); 280 } 281 carCustomizers.put("Custom", new CarCustomizer(CARSTORE_PREFIX + 282 ".bundles.Custom")); 283 carCustomizers.put("Standard", 284 new CarCustomizer(CARSTORE_PREFIX + 285 ".bundles.Standard")); 286 carCustomizers.put("Performance", 287 new CarCustomizer(CARSTORE_PREFIX + 288 ".bundles.Performance")); 289 carCustomizers.put("Deluxe", 290 new CarCustomizer(CARSTORE_PREFIX + 291 ".bundles.Deluxe")); 292 choosePackage("Custom"); 293 } 294 return carCustomizers; 295 } 296 297 298 299 303 private String getCurrentModelName() { 304 return currentModelName; 305 } 306 307 308 private void setCurrentModelName(String newName) { 309 currentModelName = newName; 310 } 311 312 314 static Class loadClass(String name, 315 Object fallbackClass) throws ClassNotFoundException { 316 ClassLoader loader = getCurrentLoader(fallbackClass); 317 return loader.loadClass(name); 318 } 319 320 321 static ClassLoader getCurrentLoader(Object fallbackClass) { 322 ClassLoader loader = 323 Thread.currentThread().getContextClassLoader(); 324 if (loader == null) { 325 loader = fallbackClass.getClass().getClassLoader(); 326 } 327 return loader; 328 } 329 330 331 } 332 | Popular Tags |