KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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

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

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

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

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

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