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