TOC PREV NEXT INDEX




Creating the Backing JavaBean (TimeZoneBean.java)


The com.icesoft.faces.tutorial.TimeZoneBean.java file is the implementation of the backing bean for the timezone.jsp page. The bean stores the current state of the selections and all the time zone information.

The code for the TimeZoneBean.java class is as follows:

package com.icesoft.tutorial;
 

 
import javax.faces.context.FacesContext;
 
import javax.faces.event.ActionEvent;
 
import java.text.DateFormat;
 
import java.text.SimpleDateFormat;
 
import java.util.Calendar;
 
import java.util.StringTokenizer;
 
import java.util.TimeZone;
 

 
/**
 
* Bean backing the Time Zone application. Controls time zone information during
 
* the session.
 
*/
 
public class TimeZoneBean {
 

 
/**
 
* {@link DateFormat} used to display time.
 
*/
 
	private DateFormat currentFormat;
 

 
/**
 
* The default {@link TimeZone} for this host server.
 
*/
 
	private TimeZone serverTimeZone;
 

 
/**
 
* Active {@link TimeZone} displayed at top of UI. Changes when a time zone
 
* is selected by pressing one of six commandButtons in UI map.
 
*/
 
	private TimeZone selectedTimeZone;
 

 
/**
 
* Constants designating a specific location in a time zone. This level of
 
* detail is required to extract daylight time properties.
 
*/
 
private static final String GMT5DAYLIGHTLOCATION = "America/New_York";
 
private static final String GMT6DAYLIGHTLOCATION = "America/Chicago";
 
private static final String GMT7DAYLIGHTLOCATION = "America/Phoenix";
 
private static final String GMT8DAYLIGHTLOCATION = "America/Los_Angeles";
 
private static final String GMT9DAYLIGHTLOCATION = "America/Anchorage";
 
private static final String GMT10DAYLIGHTLOCATION = "Pacific/Honolulu";
 

 
/**
 
* Constructor, initializes time zones.
 
*/
 
public TimeZoneBean() {
 
	init();
 
}
 

 
/**
 
* Initializes this TimeZoneBean's properties.
 
*/
 
private void init() {
 
	currentFormat = new SimpleDateFormat("EEE, HH:mm:ss");
 
	serverTimeZone = TimeZone.getDefault();
 

 
	// selected time zone set to UTC as default
 
	selectedTimeZone = TimeZone.getTimeZone("Etc/GMT+0");
 
}
 

 
/**
 
* Gets selected time zone time. This is the time zone selected by one of
 
* six commandButtons from the map in the UI.
 
*
 
* @return selectedTimeZone time.
 
*/
 
public String getSelectedTime() {
 
	return getComputedTime(selectedTimeZone);
 
}
 

 
/**
 
* Gets selected time zone display name.
 
*
 
* @return selectedTimeZone display name.
 
*/
 
public String getSelectedTimeZoneName() {
 
	return displayNameTokenizer(selectedTimeZone.getDisplayName());
 
}
 

 
/**
 
* Gets server time.
 
*
 
* @return Server time.
 
*/
 
public String getServerTime() {
 
	long now = System.currentTimeMillis();
 
	Calendar serverZoneCal = Calendar.getInstance(serverTimeZone);
 
	serverZoneCal.setTimeInMillis(now);
 
	return currentFormat.format(serverZoneCal.getTime());
 
}
 

 
/**
 
* Gets server time zone display name.
 
*
 
* @return Server time zone display name.
 
*/
 
public String getServerTimeZoneName() {
 
	return displayNameTokenizer(serverTimeZone.getDisplayName());
 
}
 

 
/**
 
* Extracts the first word from a TimeZone displayName.
 
*
 
* @param String A TimeZone displayName.
 
* @return String The first word from the TimeZone displayName.
 
*/   
 
public String displayNameTokenizer(String displayName) {
 
	if(displayName==null){
 
		displayName = "";
 
	} else {
 
		StringTokenizer tokens = new StringTokenizer(displayName," ");
 
		if (tokens.hasMoreTokens()) {
 
			displayName = tokens.nextToken();
 
		}
 
	}
 
	return displayName;		
 
}
 

 
/**
 
* Calculates the current time in a specified time zone.
 
*
 
* @param zone The specified time zone.
 
* @return Time in the specified time zone.
 
*/
 
protected String getComputedTime(TimeZone zone) {
 
	String tmpTime = "No Time available";
 
	long now = System.currentTimeMillis();
 

 
	Calendar currentZoneCal = Calendar.getInstance(zone);
 
	currentZoneCal.setTimeInMillis(now);
 
	int shift = -1 * serverTimeZone.getRawOffset();
 

 
	long calcMillis = zone.getRawOffset() + shift + now;
 
	Calendar cal = Calendar.getInstance(zone);
 
	cal.setTimeInMillis(calcMillis);
 
	tmpTime = currentFormat.format(cal.getTime());
 

 
	return tmpTime;
 
}
 

 
/**
 
* Extracts a {@link TimeZone} id from a component id, then selects a more
 
* specific id within that time zone. The specific locations used in this
 
* sample are saved in constants and were chosen based on the largest
 
* American city in the time zone.
 
*
 
* @param temp Component id containing time zone id.
 
* @return {@link TimeZone} id.
 
*/
 
private String interpretID(String temp, String minus, String plus) {
 
	// fallback is GMT time zone if no zoneid found.
 
	String zoneId = "Etc/GMT+0";
 
	if (temp.indexOf(minus) > 0) {
 
		int transIndex = Integer.parseInt(
 
				temp.substring(temp.lastIndexOf(minus) + minus.length()));
 
		zoneId = "Etc/GMT+" + transIndex;
 
	} else {
 
		if (temp.indexOf(plus) > 0) {
 
			int transIndex = Integer.parseInt(
 
					temp.substring(temp.lastIndexOf(plus) + plus.length()));
 
			zoneId = "Etc/GMT-" + transIndex;
 
		}
 
	}
 

 
	// Choosing a specific {@link TimeZone}
 
	// id within the larger {@link TimeZone}.
 
	if (zoneId.endsWith("5")) {
 
		zoneId = GMT5DAYLIGHTLOCATION;
 
	} else if (zoneId.endsWith("6")) {
 
		zoneId = GMT6DAYLIGHTLOCATION;
 
	} else if (zoneId.endsWith("7")) {
 
		zoneId = GMT7DAYLIGHTLOCATION;
 
	} else if (zoneId.endsWith("8")) {
 
		zoneId = GMT8DAYLIGHTLOCATION;
 
	} else if (zoneId.endsWith("9")) {
 
		zoneId = GMT9DAYLIGHTLOCATION;
 
	} else if (zoneId.endsWith("10")) {
 
		zoneId = GMT10DAYLIGHTLOCATION;
 
}
 
	return zoneId;
 
}
 

 
/**
 
* Listens to client input from commandButtons in the UI map and sets the
 
* selected time zone.
 
*
 
* @param e ActionEvent.
 
*/
 
	public void listen(ActionEvent e) {
 
		FacesContext context = FacesContext.getCurrentInstance();
 
		String temp = e.getComponent().getClientId(context);
 
		String zoneId = interpretID(temp, "GMTplus", "GMTminus");
 
		selectedTimeZone = TimeZone.getTimeZone(zoneId);
 
	}
 
}
 

Besides storing the current state and the time zone information, the TimeZoneBean also handles the actions generated by clicking the commandButtons. The listen(ActionEvent e) method in the bean takes the id of the commandButton clicked and passes it into the method interpretId(). This method takes the component id and returns a TimeZone id that can be used to instantiate a TimeZone object. The commandButton ids represent a number relative to Greenwich Mean Time (GMT).



Copyright 2005. ICEsoft Technologies, Inc.
http://www.icesoft.com

TOC PREV NEXT INDEX