KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > tutorial > TimeZoneBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.tutorial;
35
36 import javax.faces.component.UIComponent;
37 import javax.faces.context.FacesContext;
38 import javax.faces.event.ActionEvent;
39 import javax.faces.event.ValueChangeEvent;
40 import java.text.DateFormat JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Calendar JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.StringTokenizer JavaDoc;
46 import java.util.TimeZone JavaDoc;
47
48 /**
49  * Bean backing the Time Zone application. This bean controls time zone
50  * information during the session.
51  */

52 public class TimeZoneBean {
53
54     /**
55      * {@link DateFormat} used to display time.
56      */

57     private DateFormat JavaDoc currentFormat;
58     /**
59      * The default {@link TimeZone} for this host server.
60      */

61     private TimeZone JavaDoc serverTimeZone;
62     /**
63      * Active {@link TimeZone} displayed at top of UI. Changes when a time zone
64      * is selected by pressing one of six commandButtons in UI map.
65      */

66     private TimeZone JavaDoc selectedTimeZone;
67     /**
68      * Identifies a user across more than one page request and stores
69      * information about that user.
70      */

71
72     private ArrayList JavaDoc checkedTimeZoneList;
73     /**
74      * Constants designating a specific location in a time zone. This level of
75      * detail is required to extract daylight time properties.
76      */

77     private static final String JavaDoc GMT5DAYLIGHTLOCATION = "America/New_York";
78     private static final String JavaDoc GMT6DAYLIGHTLOCATION = "America/Chicago";
79     private static final String JavaDoc GMT7DAYLIGHTLOCATION = "America/Phoenix";
80     private static final String JavaDoc GMT8DAYLIGHTLOCATION = "America/Los_Angeles";
81     private static final String JavaDoc GMT9DAYLIGHTLOCATION = "America/Anchorage";
82     private static final String JavaDoc GMT10DAYLIGHTLOCATION = "Pacific/Honolulu";
83
84     /**
85      * Constructor initializes time zones and starts thread which updates the
86      * time.
87      */

88     public TimeZoneBean() {
89         init();
90     }
91
92     /**
93      * Initializes this TimeZoneBean's properties.
94      */

95     private void init() {
96         currentFormat = new SimpleDateFormat JavaDoc("EEE, HH:mm:ss");
97         serverTimeZone = TimeZone.getDefault();
98         selectedTimeZone = TimeZone.getTimeZone(
99                 "Etc/GMT+0"); // selected time zone set to UTC as default
100
checkedTimeZoneList = new ArrayList JavaDoc();
101     }
102
103     /**
104      * Gets selected time zone time. This is the time zone selected by one of
105      * six commandButtons from the map in the UI.
106      *
107      * @return selectedTimeZone time.
108      */

109
110     public String JavaDoc getSelectedTime() {
111         return getComputedTime(selectedTimeZone);
112     }
113
114     /**
115      * Gets selected time zone display name.
116      *
117      * @return selectedTimeZone display name.
118      */

119     public String JavaDoc getSelectedTimeZoneName() {
120         return displayNameTokenizer(selectedTimeZone.getDisplayName());
121     }
122
123     /**
124      * Gets server time.
125      *
126      * @return Server time.
127      */

128     public String JavaDoc getServerTime() {
129         long now = System.currentTimeMillis();
130         Calendar JavaDoc serverZoneCal = Calendar.getInstance(serverTimeZone);
131         serverZoneCal.setTimeInMillis(now);
132         return currentFormat.format(serverZoneCal.getTime());
133     }
134
135     /**
136      * Gets server time zone display name.
137      *
138      * @return Server time zone display name.
139      */

140     public String JavaDoc getServerTimeZoneName() {
141         return displayNameTokenizer(serverTimeZone.getDisplayName());
142     }
143
144     /**
145      * Gets ArrayList of <code>CheckedTimeZone</code> objects. This list is
146      * populated by selectBooleanCheckbox components in UI.
147      *
148      * @return ArrayList of CheckedTimeZone objects.
149      */

150     public ArrayList JavaDoc getCheckedTimeZoneList() {
151         return checkedTimeZoneList;
152     }
153
154     /**
155      * Extracts the first word from a TimeZone displayName.
156      *
157      * @param displayName A TimeZone displayName.
158      * @return String The first word from the TimeZone displayName.
159      */

160     public String JavaDoc displayNameTokenizer(String JavaDoc displayName) {
161         if (displayName == null) {
162             displayName = "";
163         } else {
164             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(displayName, " ");
165             if (tokens.hasMoreTokens()) {
166                 displayName = tokens.nextToken();
167             }
168         }
169         return displayName;
170     }
171
172     /**
173      * Calculates the current time in a specified time zone.
174      *
175      * @param zone The specified time zone.
176      * @return Time in the specified time zone.
177      */

178     protected String JavaDoc getComputedTime(TimeZone JavaDoc zone) {
179         String JavaDoc tmpTime = "No Time available";
180         long now = System.currentTimeMillis();
181
182         Calendar JavaDoc currentZoneCal = Calendar.getInstance(zone);
183         currentZoneCal.setTimeInMillis(now);
184         int shift = -1 * serverTimeZone.getRawOffset();
185
186         long calcMillis = zone.getRawOffset() + shift + now;
187         Calendar JavaDoc cal = Calendar.getInstance(zone);
188         cal.setTimeInMillis(calcMillis);
189         tmpTime = currentFormat.format(cal.getTime());
190
191         return tmpTime;
192     }
193
194     /**
195      * Extracts a {@link TimeZone} id from a component id, then selects a more
196      * specific id within that time zone. The specific locations used in this
197      * sample are saved in constants and were chosen based on the largest
198      * American city in the time zone.
199      *
200      * @param temp Component id containing time zone id.
201      * @return {@link TimeZone} id.
202      */

203     private String JavaDoc interpretID(String JavaDoc temp, String JavaDoc minus, String JavaDoc plus) {
204         String JavaDoc zoneId =
205                 "Etc/GMT+0"; // fallback is GMT time zone if no zoneid found.
206
if (temp.indexOf(minus) > 0) {
207             int transIndex = Integer.parseInt(
208                     temp.substring(temp.lastIndexOf(minus) + minus.length()));
209             zoneId = "Etc/GMT+" + transIndex;
210         } else {
211             if (temp.indexOf(plus) > 0) {
212                 int transIndex = Integer.parseInt(
213                         temp.substring(temp.lastIndexOf(plus) + plus.length()));
214                 zoneId = "Etc/GMT-" + transIndex;
215             }
216         }
217         // Choosing a specific {@link TimeZone} id within the larger {@link TimeZone}.
218
if (zoneId.endsWith("5")) {
219             zoneId = GMT5DAYLIGHTLOCATION;
220         } else if (zoneId.endsWith("6")) {
221             zoneId = GMT6DAYLIGHTLOCATION;
222         } else if (zoneId.endsWith("7")) {
223             zoneId = GMT7DAYLIGHTLOCATION;
224         } else if (zoneId.endsWith("8")) {
225             zoneId = GMT8DAYLIGHTLOCATION;
226         } else if (zoneId.endsWith("9")) {
227             zoneId = GMT9DAYLIGHTLOCATION;
228         } else if (zoneId.endsWith("10")) {
229             zoneId = GMT10DAYLIGHTLOCATION;
230         }
231         return zoneId;
232     }
233
234     /**
235      * Listens to client input from commandButtons in the UI map and sets the
236      * selected time zone.
237      *
238      * @param e ActionEvent.
239      */

240     public void listen(ActionEvent e) {
241         FacesContext context = FacesContext.getCurrentInstance();
242         String JavaDoc temp = e.getComponent().getClientId(context);
243         String JavaDoc zoneId = interpretID(temp, "GMTplus", "GMTminus");
244         selectedTimeZone = TimeZone.getTimeZone(zoneId);
245     }
246
247     /**
248      * Adds or removes a <code>CheckedTimeZone</code> to
249      * <code>checkedTimeZoneList</code> when a selectBooleanCheckbox
250      * ValueChangeEvent is fired from the UI.
251      *
252      * @param event ValueChangeEvent.
253      */

254     public void timeZoneChanged(ValueChangeEvent event) {
255         UIComponent comp = event.getComponent();
256         FacesContext context = FacesContext.getCurrentInstance();
257         String JavaDoc zoneId =
258                 interpretID(comp.getClientId(context), "Cplus", "Cminus");
259         TimeZone JavaDoc timeZone = TimeZone.getTimeZone(zoneId);
260         //Calendar is required to obtain a Date object to pass into inDaylightTime method.
261
Calendar JavaDoc cal = Calendar.getInstance(timeZone);
262
263         boolean newVal = ((Boolean JavaDoc) event.getNewValue()).booleanValue();
264         Iterator JavaDoc timeZones = checkedTimeZoneList.iterator();
265         while (timeZones.hasNext()) {
266             if (((CheckedTimeZone) timeZones.next()).getId()
267                     .equals(timeZone.getID())) {
268                 timeZones.remove();
269             }
270         }
271         if (newVal) {
272             CheckedTimeZone checkedTimeZone =
273                     new CheckedTimeZone(
274                             displayNameTokenizer(timeZone.getDisplayName()),
275                             zoneId,
276                             timeZone.useDaylightTime(),
277                             timeZone.inDaylightTime(cal.getTime()),
278                             this);
279             checkedTimeZoneList.add(checkedTimeZone);
280         }
281     }
282 }
283
284
Popular Tags