KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > carstore > CarCustomizer


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 org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 import javax.faces.component.UIComponent;
46 import javax.faces.component.ValueHolder;
47 import javax.faces.context.FacesContext;
48 import javax.faces.convert.Converter;
49
50 import java.util.Enumeration JavaDoc;
51 import java.util.ResourceBundle JavaDoc;
52
53 /**
54  * <p>A helper class that customizes a CarBean for a set of options
55  * in a package.</p>
56  *
57  * <p>This class reads its settings from a Properties file</p>
58  */

59
60 public class CarCustomizer extends Object JavaDoc {
61
62     protected static final Log log = LogFactory.getLog(CarCustomizer.class);
63
64     //
65
// Relationship Instance Variables
66
//
67

68     private ResourceBundle JavaDoc bundle = null;
69
70
71     public CarCustomizer() {
72         this.init(CarStore.DEFAULT_PACKAGE_PROPERTIES);
73     }
74
75
76     public CarCustomizer(String JavaDoc bundleName) {
77         this.init(bundleName);
78     }
79
80
81     private void init(String JavaDoc bundleName) {
82         FacesContext context = FacesContext.getCurrentInstance();
83
84         if (log.isDebugEnabled()) {
85             log.debug("Loading bundle: " + bundleName + ".");
86         }
87         bundle = ResourceBundle.getBundle(bundleName);
88     }
89
90
91     private String JavaDoc buttonStyle = null;
92
93
94     public String JavaDoc getButtonStyle() {
95         return buttonStyle;
96     }
97
98
99     public void setButtonStyle(String JavaDoc newButtonStyle) {
100         buttonStyle = newButtonStyle;
101     }
102
103
104     public void customizeCar(CarBean toCustomize) {
105         FacesContext context = FacesContext.getCurrentInstance();
106         Enumeration JavaDoc keys = bundle.getKeys();
107         String JavaDoc
108             key = null,
109             disabledStr = null,
110             curSetting = null;
111         Boolean JavaDoc disabled = null;
112         UIComponent component = null;
113         Converter converter = null;
114         Object JavaDoc valueToSet = null;
115
116         while (keys.hasMoreElements()) {
117             key = (String JavaDoc) keys.nextElement();
118             // skip null and secondary keys.
119
if (key == null || -1 != key.indexOf("_")) {
120                 continue;
121             }
122             // skip null values
123
if (null == (curSetting = bundle.getString(key))) {
124                 continue;
125             }
126
127             // skip null components
128
if (null ==
129                 (component =
130                 (UIComponent) toCustomize.getComponents().get(key))) {
131                 continue;
132             }
133
134             // handle the disabled setting, if necessary
135
disabled = null;
136             try {
137                 if (null !=
138                     (disabledStr = bundle.getString(key + "_disabled"))) {
139                     disabled = Boolean.valueOf(disabledStr);
140                 }
141             } catch (Throwable JavaDoc e) {
142             }
143             if (null != disabled) {
144                 component.getAttributes().put("disabled", disabled);
145             }
146
147             // set the value
148
// If the component can and does have a converter
149
if (component instanceof ValueHolder &&
150                 (null != (converter =
151                 ((ValueHolder) component).getConverter()))) {
152                 valueToSet = converter.getAsObject(context, component,
153                                                    curSetting);
154             } else {
155                 valueToSet = curSetting;
156             }
157
158             if (component instanceof ValueHolder) {
159                 ((ValueHolder) component).setValue(valueToSet);
160             }
161         }
162     }
163 }
164     
165     
166     
167
Popular Tags