1 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 ; 41 import java.text.SimpleDateFormat ; 42 import java.util.ArrayList ; 43 import java.util.Calendar ; 44 import java.util.Iterator ; 45 import java.util.StringTokenizer ; 46 import java.util.TimeZone ; 47 48 52 public class TimeZoneBean { 53 54 57 private DateFormat currentFormat; 58 61 private TimeZone serverTimeZone; 62 66 private TimeZone selectedTimeZone; 67 71 72 private ArrayList checkedTimeZoneList; 73 77 private static final String GMT5DAYLIGHTLOCATION = "America/New_York"; 78 private static final String GMT6DAYLIGHTLOCATION = "America/Chicago"; 79 private static final String GMT7DAYLIGHTLOCATION = "America/Phoenix"; 80 private static final String GMT8DAYLIGHTLOCATION = "America/Los_Angeles"; 81 private static final String GMT9DAYLIGHTLOCATION = "America/Anchorage"; 82 private static final String GMT10DAYLIGHTLOCATION = "Pacific/Honolulu"; 83 84 88 public TimeZoneBean() { 89 init(); 90 } 91 92 95 private void init() { 96 currentFormat = new SimpleDateFormat ("EEE, HH:mm:ss"); 97 serverTimeZone = TimeZone.getDefault(); 98 selectedTimeZone = TimeZone.getTimeZone( 99 "Etc/GMT+0"); checkedTimeZoneList = new ArrayList (); 101 } 102 103 109 110 public String getSelectedTime() { 111 return getComputedTime(selectedTimeZone); 112 } 113 114 119 public String getSelectedTimeZoneName() { 120 return displayNameTokenizer(selectedTimeZone.getDisplayName()); 121 } 122 123 128 public String getServerTime() { 129 long now = System.currentTimeMillis(); 130 Calendar serverZoneCal = Calendar.getInstance(serverTimeZone); 131 serverZoneCal.setTimeInMillis(now); 132 return currentFormat.format(serverZoneCal.getTime()); 133 } 134 135 140 public String getServerTimeZoneName() { 141 return displayNameTokenizer(serverTimeZone.getDisplayName()); 142 } 143 144 150 public ArrayList getCheckedTimeZoneList() { 151 return checkedTimeZoneList; 152 } 153 154 160 public String displayNameTokenizer(String displayName) { 161 if (displayName == null) { 162 displayName = ""; 163 } else { 164 StringTokenizer tokens = new StringTokenizer (displayName, " "); 165 if (tokens.hasMoreTokens()) { 166 displayName = tokens.nextToken(); 167 } 168 } 169 return displayName; 170 } 171 172 178 protected String getComputedTime(TimeZone zone) { 179 String tmpTime = "No Time available"; 180 long now = System.currentTimeMillis(); 181 182 Calendar currentZoneCal = Calendar.getInstance(zone); 183 currentZoneCal.setTimeInMillis(now); 184 int shift = -1 * serverTimeZone.getRawOffset(); 185 186 long calcMillis = zone.getRawOffset() + shift + now; 187 Calendar cal = Calendar.getInstance(zone); 188 cal.setTimeInMillis(calcMillis); 189 tmpTime = currentFormat.format(cal.getTime()); 190 191 return tmpTime; 192 } 193 194 203 private String interpretID(String temp, String minus, String plus) { 204 String zoneId = 205 "Etc/GMT+0"; 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 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 240 public void listen(ActionEvent e) { 241 FacesContext context = FacesContext.getCurrentInstance(); 242 String temp = e.getComponent().getClientId(context); 243 String zoneId = interpretID(temp, "GMTplus", "GMTminus"); 244 selectedTimeZone = TimeZone.getTimeZone(zoneId); 245 } 246 247 254 public void timeZoneChanged(ValueChangeEvent event) { 255 UIComponent comp = event.getComponent(); 256 FacesContext context = FacesContext.getCurrentInstance(); 257 String zoneId = 258 interpretID(comp.getClientId(context), "Cplus", "Cminus"); 259 TimeZone timeZone = TimeZone.getTimeZone(zoneId); 260 Calendar cal = Calendar.getInstance(timeZone); 262 263 boolean newVal = ((Boolean ) event.getNewValue()).booleanValue(); 264 Iterator 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 |