KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > openajax > beans > TimeZoneBean


1 package com.icesoft.openajax.beans;
2
3 import com.icesoft.faces.async.render.IntervalRenderer;
4 import com.icesoft.faces.async.render.RenderManager;
5 import com.icesoft.faces.async.render.Renderable;
6 import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
7 import com.icesoft.faces.webapp.xmlhttp.RenderingException;
8 import javax.faces.component.UIComponent;
9 import javax.faces.context.FacesContext;
10 import javax.faces.event.ActionEvent;
11 import javax.faces.event.ValueChangeEvent;
12 import java.text.DateFormat JavaDoc;
13 import java.text.SimpleDateFormat JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Calendar JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.TimeZone JavaDoc;
18
19 /**
20  * Bean backing the Time Zone application. This bean uses the RenderManager to
21  * update state at a specified interval. Also controls time zone information
22  * during the session.
23  */

24 public class TimeZoneBean implements Renderable
25 {
26     /**
27      * The default {@link TimeZone} for this host server.
28      */

29     private TimeZone JavaDoc serverTimeZone;
30
31     /**
32      * {@link DateFormat} used to display the server time.
33      */

34     private DateFormat JavaDoc serverFormat;
35
36     /**
37      * Active {@link TimeZone} displayed at top of UI. Changes when a time zone
38      * is selected by pressing one of six commandButtons in UI map.
39      */

40     private TimeZone JavaDoc selectedTimeZone;
41
42     /**
43      * {@link DateFormat} used to display the selected time.
44      */

45     private DateFormat JavaDoc selectedFormat;
46
47     /**
48      * List of all possible {@link TimeZoneWrapper} objects, which must mirror
49      * the map and checkbox UIs.
50      */

51     private ArrayList JavaDoc allTimeZoneList;
52
53     /**
54      * List of checked {@link TimeZoneWrapper} objects, which are shown at the
55      * bottom table UI. Changes when a time zone is selected or deselected by
56      * checking one of six check boxes underneath map UI.
57      */

58     private ArrayList JavaDoc checkedTimeZoneList;
59
60     /**
61      * Time interval, in milliseconds, between renders.
62      */

63     private final int renderInterval = 1000;
64
65     /**
66      * The state associated with the current user that can be used for
67      * server-initiated render calls.
68      */

69     private PersistentFacesState state;
70
71     /**
72      * A named render group that can be shared by all TimeZoneBeans for
73      * server-initiated render calls. Setting the interval determines the
74      * frequency of the render call.
75      */

76     private IntervalRenderer clock;
77
78     /**
79      * a table mapping the checkbox id's to the checkbox states
80      */

81     private Hashtable JavaDoc checkboxStates;
82     
83     /**
84      * display the open ajax conformance dialog or not
85      */

86     private boolean dialogRendered = true;
87
88     /**
89      * Constructor initializes time zones.
90      */

91     public TimeZoneBean() {
92         init();
93     }
94
95     /**
96      * Initializes this TimeZoneBean's properties.
97      */

98     private void init() {
99         serverTimeZone = TimeZone.getDefault();
100         serverFormat = buildDateFormatForTimeZone(serverTimeZone);
101         selectedTimeZone = TimeZone.getTimeZone(
102                 "Etc/GMT+0"); // selected time zone set to UTC as default
103
selectedFormat = buildDateFormatForTimeZone(selectedTimeZone);
104
105         // Entries in this list are hardcoded to match entries in
106
// the timezone web file, so no parameters can be changed.
107
allTimeZoneList = new ArrayList JavaDoc(6);
108         allTimeZoneList.add(new TimeZoneWrapper(
109                 "Pacific/Honolulu", "GMTminus10", "Cminus10"));
110         allTimeZoneList.add(new TimeZoneWrapper(
111                 "America/Anchorage", "GMTminus9", "Cminus9"));
112         allTimeZoneList.add(new TimeZoneWrapper(
113                 "America/Los_Angeles", "GMTminus8", "Cminus8"));
114         allTimeZoneList.add(new TimeZoneWrapper(
115                 "America/Phoenix", "GMTminus7", "Cminus7"));
116         allTimeZoneList.add(new TimeZoneWrapper(
117                 "America/Chicago", "GMTminus6", "Cminus6"));
118         allTimeZoneList.add(new TimeZoneWrapper(
119                 "America/New_York", "GMTminus5", "Cminus5"));
120
121         checkedTimeZoneList = new ArrayList JavaDoc();
122
123         state = PersistentFacesState.getInstance();
124
125         checkboxStates = new Hashtable JavaDoc(6);
126         checkboxStates.put("Cminus10", "false");
127         checkboxStates.put("Cminus9", "false");
128         checkboxStates.put("Cminus8", "false");
129         checkboxStates.put("Cminus7", "false");
130         checkboxStates.put("Cminus6", "false");
131         checkboxStates.put("Cminus5", "false");
132     }
133
134     /**
135      * Gets server time.
136      *
137      * @return Server time.
138      */

139     public String JavaDoc getServerTime() {
140         return formatCurrentTime(serverFormat);
141     }
142
143     /**
144      * Gets server time zone display name.
145      *
146      * @return Server time zone display name.
147      */

148     public String JavaDoc getServerTimeZoneName() {
149         return displayNameTokenizer(serverTimeZone.getDisplayName());
150     }
151
152     /**
153      * Gets selected time zone time. This is the time zone selected by one of
154      * six commandButtons from the map in the UI.
155      *
156      * @return selectedTimeZone time.
157      */

158     public String JavaDoc getSelectedTime() {
159         return formatCurrentTime(selectedFormat);
160     }
161
162     /**
163      * Gets selected time zone display name.
164      *
165      * @return selectedTimeZone display name.
166      */

167     public String JavaDoc getSelectedTimeZoneName() {
168         synchronized (TimeZone JavaDoc.class) {
169             return displayNameTokenizer(selectedTimeZone.getDisplayName());
170         }
171     }
172
173     /**
174      * Gets ArrayList of currently active <code>TimeZoneWrapper</code> objects.
175      * This list is populated by selectBooleanCheckbox components in UI.
176      *
177      * @return ArrayList of TimeZoneWrapper objects.
178      */

179     public ArrayList JavaDoc getCheckedTimeZoneList() {
180         return checkedTimeZoneList;
181     }
182     
183     public boolean isDialogRendered() {
184         return dialogRendered;
185     }
186     
187     public void setDialogRendered(boolean dialogRendered) {
188         this.dialogRendered = dialogRendered;
189     }
190     
191     public String JavaDoc closeDialog() { dialogRendered = false; return "closeDialog"; }
192
193     /**
194      * Extracts the first word from a TimeZone displayName.
195      *
196      * @param displayName A TimeZone displayName.
197      * @return String The first word from the TimeZone displayName.
198      */

199     public static String JavaDoc displayNameTokenizer(String JavaDoc displayName) {
200         if (displayName == null) {
201             displayName = "";
202         } else {
203             int firstSpace = displayName.indexOf(' ');
204             if (firstSpace != -1) {
205                 displayName = displayName.substring(0, firstSpace);
206             }
207         }
208         return displayName;
209     }
210
211     public static DateFormat JavaDoc buildDateFormatForTimeZone(TimeZone JavaDoc timeZone) {
212         SimpleDateFormat JavaDoc currentFormat = new SimpleDateFormat JavaDoc("EEE, HH:mm:ss");
213         Calendar JavaDoc currentZoneCal = Calendar.getInstance(timeZone);
214         currentFormat.setCalendar(currentZoneCal);
215         currentFormat.setTimeZone(timeZone);
216         return currentFormat;
217     }
218
219     public static String JavaDoc formatCurrentTime(DateFormat JavaDoc dateFormat) {
220         Calendar JavaDoc cal = dateFormat.getCalendar();
221         cal.setTimeInMillis(System.currentTimeMillis());
222         return dateFormat.format(cal.getTime());
223     }
224
225     /**
226      * Each TimeZoneWrapper has one or more ids of components in the UI that
227      * correspond to its time zone. By this, if an event comes from a component
228      * in the web page, then this will return the relevant TimeZoneWrapper.
229      *
230      * @param componentId Id of component in UI
231      * @return TimeZoneWrapper
232      */

233     private TimeZoneWrapper getTimeZoneWrapperByComponentId(
234             String JavaDoc componentId) {
235         for (int i = 0; i < allTimeZoneList.size(); i++) {
236             TimeZoneWrapper tzw = (TimeZoneWrapper) allTimeZoneList.get(i);
237             if (tzw.isRelevantComponentId(componentId)) {
238                 return tzw;
239             }
240         }
241         return null;
242     }
243
244     /**
245      * Used to create, setup, and start an IntervalRenderer from the passed
246      * renderManager This is used in conjunction with faces-config.xml to allow
247      * the same single render manager to be set in all TimeZoneBeans
248      *
249      * @param renderManager RenderManager to get the IntervalRenderer from
250      */

251     public void setRenderManager(RenderManager renderManager) {
252         clock = renderManager.getIntervalRenderer("clock");
253         clock.setInterval(renderInterval);
254         clock.add(this);
255         clock.requestRender();
256     }
257
258     /**
259      * Gets RenderManager
260      *
261      * @return RenderManager null
262      */

263     public RenderManager getRenderManager() {
264         return null;
265     }
266
267     //
268
// Renderable interface
269
//
270

271     /**
272      * Gets the current instance of PersistentFacesState
273      *
274      * @return PersistentFacesState state
275      */

276     public PersistentFacesState getState() {
277         return state;
278     }
279
280     /**
281      * Callback to inform us that there was an Exception while rendering
282      *
283      * @param renderingException
284      */

285     public void renderingException(RenderingException renderingException) {
286         if (clock != null) {
287             clock.remove(this);
288             clock = null;
289         }
290     }
291
292     //
293
// Implicit interfaces as defined by the callbacks in the web files
294
//
295

296     /**
297      * Listens to client input from commandButtons in the UI map and sets the
298      * selected time zone.
299      *
300      * @param event ActionEvent.
301      */

302     public void listen(ActionEvent event) {
303         UIComponent comp = event.getComponent();
304         FacesContext context = FacesContext.getCurrentInstance();
305         String JavaDoc componentId = comp.getClientId(context);
306         TimeZoneWrapper tzw = getTimeZoneWrapperByComponentId(componentId);
307         if (tzw != null) {
308             selectedTimeZone = TimeZone.getTimeZone(tzw.getId());
309             selectedFormat = buildDateFormatForTimeZone(selectedTimeZone);
310         }
311     }
312
313     /**
314      * Adds or removes a <code>TimeZoneWrapper</code> to
315      * <code>checkedTimeZoneList</code> when a selectBooleanCheckbox
316      * ValueChangeEvent is fired from the UI.
317      *
318      * @param event ValueChangeEvent.
319      */

320     public void timeZoneChanged(ValueChangeEvent event) {
321         UIComponent comp = event.getComponent();
322         FacesContext context = FacesContext.getCurrentInstance();
323         String JavaDoc componentId = comp.getClientId(context);
324         TimeZoneWrapper tzw = getTimeZoneWrapperByComponentId(componentId);
325         if (tzw != null) {
326             boolean checked = ((Boolean JavaDoc) event.getNewValue()).booleanValue();
327             // If checkbox is checked, then add tzw to checkedTimeZoneList
328
if (checked) {
329                 if (!checkedTimeZoneList.contains(tzw)) {
330                     checkedTimeZoneList.add(tzw);
331                 }
332             }
333             // Otherwise, if checkbox is unchecked, then remove tzw from checkedTimeZoneList
334
else {
335                 checkedTimeZoneList.remove(tzw);
336             }
337             checkboxStates.put(tzw.getCheckboxId(), checked ? "true" : "false");
338         }
339     }
340
341     /**
342      * Gets the table of checkbox states.
343      *
344      * @return a table mapping the checkbox id's to the checkbox states
345      */

346     public Hashtable JavaDoc getCheckboxStates() {
347         return checkboxStates;
348     }
349
350     /**
351      * Sets the table of checkbox states.
352      *
353      * @param checkboxStates a table mapping the checkbox id's to the checkbox
354      * states
355      */

356     public void setCheckboxStates(Hashtable JavaDoc checkboxStates) {
357         this.checkboxStates = checkboxStates;
358     }
359 }
360
Popular Tags