KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > carstore > CarStore


1 /*
2  * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistribution in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials
14  * provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any
21  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
25  * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
26  * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
27  * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
28  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
29  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
30  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
31  * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that this software is not designed, licensed or
35  * intended for use in the design, construction, operation or
36  * maintenance of any nuclear facility.
37  */

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 JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.Locale JavaDoc;
52 import java.util.Map JavaDoc;
53
54 /**
55  * <p>This is the main bean for the application. It maintains a
56  * <code>Map</code> of {@link CarBean} instances, keyed by model name,
57  * and a <code>Map</code> of {@link CarCustomizer} instances, keyed by
58  * package name. The <code>CarBean</code> instances in the model
59  * <code>Map</code> are accessed from several pages, as described
60  * below.</p>
61  *
62  * <p>Several pages in the application use this bean as the target of
63  * method reference and value reference expressions.</p>
64  *
65  * <ul>
66  *
67  * <li><p>The "chooseLocale" page uses <code>actionListener</code>
68  * attributes to point to the {@link #chooseLocaleFromMap} and {@link
69  * #chooseLocaleFromLink} methods.</p></li>
70  *
71  * <li><p>The "storeFront" page uses value binding expressions to pull
72  * information about four of the known car models in the store.</p></li>
73  *
74  * <li><p>The "carDetail" page uses value binding expressions to pull
75  * information about the currently chosen model. It also uses the
76  * <code>action</code> attribute to convey the user's package
77  * choices.</p></li>
78  *
79  * <li><p>The "confirmChoices" page uses value binding expressions to
80  * pull the user's choices from the currently chosen model.</p></li>
81  *
82  * </ul>
83  */

84
85 public class CarStore extends Object JavaDoc {
86
87     protected static final Log log = LogFactory.getLog(CarStore.class);
88
89     static final String JavaDoc CARSTORE_PREFIX = "carstore";
90
91     static final String JavaDoc DEFAULT_MODEL = "Jalopy";
92
93     static final String JavaDoc DEFAULT_PACKAGE = "Custom";
94
95     static final String JavaDoc DEFAULT_MODEL_PROPERTIES = CARSTORE_PREFIX +
96         ".bundles." + DEFAULT_MODEL;
97
98     static final String JavaDoc DEFAULT_PACKAGE_PROPERTIES = CARSTORE_PREFIX +
99         ".bundles." + DEFAULT_PACKAGE;
100
101     //
102
// Relationship Instance Variables
103
//
104

105     /**
106      * <p>The locales to be selected for each hotspot, keyed by the
107      * alternate text for that area.</p>
108      */

109     private Map JavaDoc locales = null;
110
111     /**
112      * <p>The currently selected car model.</p>
113      */

114
115     private String JavaDoc currentModelName = DEFAULT_MODEL;
116
117     /**
118      * <p>The car models we offer.</p>
119      *
120      * <p>Keys: Strings that happen to correspond to the name of the
121      * packages.</p>
122      *
123      * <p>Values: CarBean instances</p>
124      */

125     private Map JavaDoc carModels = null;
126
127     /**
128      * <p>Keys: Strings that happen to correspond to the name of the
129      * Properties file for the car (without the package prefix).</p>
130      *
131      * <p>Values: CarBeanCustomizer instances</p>
132      */

133
134     private Map JavaDoc 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 JavaDoc();
143         locales.put("NAmerica", Locale.ENGLISH);
144         locales.put("SAmerica", new Locale JavaDoc("es", "es"));
145         locales.put("Germany", Locale.GERMAN);
146         locales.put("France", Locale.FRENCH);
147     }
148
149     //
150
// ActionListener handlers
151
//
152

153     public void chooseLocaleFromMap(ActionEvent actionEvent) {
154         AreaSelectedEvent event = (AreaSelectedEvent) actionEvent;
155         String JavaDoc current = event.getMapComponent().getCurrent();
156         FacesContext context = FacesContext.getCurrentInstance();
157         context.getViewRoot().setLocale((Locale JavaDoc) locales.get(current));
158         resetMaps();
159     }
160
161
162     public void chooseLocaleFromLink(ActionEvent event) {
163         String JavaDoc current = event.getComponent().getId();
164         FacesContext context = FacesContext.getCurrentInstance();
165         context.getViewRoot().setLocale((Locale JavaDoc) 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 JavaDoc packageName = event.getComponent().getId();
184         choosePackage(packageName);
185     }
186
187
188     public void choosePackage(String JavaDoc packageName) {
189         CarCustomizer packageCustomizer =
190             (CarCustomizer) carCustomizers.get(packageName);
191         packageCustomizer.customizeCar(getCurrentModel());
192         getCurrentModel().getCurrentPrice();
193
194         // HERE IS WHERE WE UPDATE THE BUTTON STYLE!
195
String JavaDoc curName;
196         Iterator JavaDoc iter = carCustomizers.keySet().iterator();
197         // go through all the available packages and set the button
198
// style accordingly.
199
while (iter.hasNext()) {
200             curName = (String JavaDoc) 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     //
211
// action handlers
212
//
213

214     public String JavaDoc storeFrontJalopyPressed() {
215         setCurrentModelName("Jalopy");
216         return "carDetail";
217     }
218
219
220     public String JavaDoc storeFrontRoadsterPressed() {
221         setCurrentModelName("Roadster");
222         return "carDetail";
223     }
224
225
226     public String JavaDoc storeFrontLuxuryPressed() {
227         setCurrentModelName("Luxury");
228         return "carDetail";
229     }
230
231
232     public String JavaDoc storeFrontSUVPressed() {
233         setCurrentModelName("SUV");
234         return "carDetail";
235     }
236
237
238     public String JavaDoc buyCurrentCar() {
239         getCurrentModel().getCurrentPrice();
240         return "confirmChoices";
241     }
242
243     //
244
// Accessors
245
//
246

247     public CarBean getCurrentModel() {
248         CarBean result = (CarBean) carModels.get(getCurrentModelName());
249         return result;
250     }
251
252
253     public Map JavaDoc getModels() {
254         if (null == carModels) {
255             carModels = new HashMap JavaDoc();
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 JavaDoc getCustomizers() {
274         getModels();
275         if (null == carCustomizers) {
276             carCustomizers = new HashMap JavaDoc();
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     //
299
// private methods
300
//
301

302     private String JavaDoc getCurrentModelName() {
303         return currentModelName;
304     }
305
306
307     private void setCurrentModelName(String JavaDoc newName) {
308         currentModelName = newName;
309     }
310
311     // package private util methods
312

313     static Class JavaDoc loadClass(String JavaDoc name,
314                            Object JavaDoc fallbackClass) throws ClassNotFoundException JavaDoc {
315         ClassLoader JavaDoc loader = getCurrentLoader(fallbackClass);
316         return loader.loadClass(name);
317     }
318
319
320     static ClassLoader JavaDoc getCurrentLoader(Object JavaDoc fallbackClass) {
321         ClassLoader JavaDoc 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