TOC PREV NEXT INDEX



PDF
ICEfaces Online Reference




 


 




Creating the Backing JavaBean (TimeZoneBean.java)


The com.icesoft.faces.tutorial.TimeZoneBean class is 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.component.UIComponent;
 
import javax.faces.event.ActionEvent;
 
import java.text.DateFormat;
 
import java.text.SimpleDateFormat;
 
import java.util.ArrayList;
 
import java.util.Calendar;
 
import java.util.TimeZone;
 

 
/**
 
 * Bean backing the Time Zone application.
 
 * Also controls time zone information during the session.
 
 */
 

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

 
    /**
 
     * {@link DateFormat} used to display the server time.
 
     */
 
    private DateFormat serverFormat;
 

 
    /**
 
     * 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;
 

 
    /**
 
     * {@link DateFormat} used to display the selected time.
 
     */
 
    private DateFormat selectedFormat;
 

 
    /**
 
     * List of all possible {@link TimeZoneWrapper} objects,
 
     * which must mirror the map UI.
 
     */
 
    private ArrayList allTimeZoneList;
 

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

 
    /**
 
     * Initializes this TimeZoneBean's properties.
 
     */
 
    private void init() {
 
        serverTimeZone = TimeZone.getDefault();
 
        serverFormat = buildDateFormatForTimeZone( serverTimeZone );
 
        selectedTimeZone = TimeZone.getTimeZone("Etc/GMT+0"); 
 
        // selected time zone set to UTC as default
 
        selectedFormat = buildDateFormatForTimeZone( selectedTimeZone );
 

 
        // Entries in this list are hardcoded to match entries in
 
        //  the timezone web file, so no parameters can be changed.
 
        allTimeZoneList = new ArrayList( 6 );
 
        allTimeZoneList.add(new TimeZoneWrapper("Pacific/Honolulu",

                                                "GMTminus10"));
 
        allTimeZoneList.add(new TimeZoneWrapper("America/Anchorage",

                                                "GMTminus9"));
 
        allTimeZoneList.add(new TimeZoneWrapper("America/Los_Angeles",
 
                                                "GMTminus8"));
 
        allTimeZoneList.add(new TimeZoneWrapper("America/Phoenix",

                                                "GMTminus7"));
 
        allTimeZoneList.add(new TimeZoneWrapper("America/Chicago",

                                                "GMTminus6"));
 
        allTimeZoneList.add(new TimeZoneWrapper("America/New_York",

                                                "GMTminus5"));
 
    }
 

 
    /**
 
     * Gets server time.
 
     *
 
     * @return Server time.
 
     */
 
    public String getServerTime() {
 
        return formatCurrentTime( serverFormat );
 
    }
 

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

 
    /**
 
     * 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 formatCurrentTime( selectedFormat );
 
    }
 

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

 
    /**
 
     * Extracts the first word from a TimeZone displayName.
 
     *
 
     * @param displayName A TimeZone displayName.
 
     * @return String The first word from the TimeZone displayName.
 
     */
 
    public static String displayNameTokenizer(String displayName) {
 
        if( displayName == null ) {
 
            displayName = "";
 
        }
 
        else {
 
            int firstSpace = displayName.indexOf(' ');
 
            if (firstSpace != -1) {
 
                displayName = displayName.substring(0, firstSpace);
 
            }
 
        }
 
        return displayName;
 
    }
 

 
    public static DateFormat buildDateFormatForTimeZone(TimeZone timeZone) {
 
        SimpleDateFormat currentFormat = new SimpleDateFormat("EEE, HH:mm:ss");
 
        Calendar currentZoneCal = Calendar.getInstance( timeZone );
 
        currentFormat.setCalendar( currentZoneCal );
 
        currentFormat.setTimeZone( timeZone );
 
        return currentFormat;
 
    }
 

 
    public static String formatCurrentTime(DateFormat dateFormat) {
 
        Calendar cal = dateFormat.getCalendar();
 
        cal.setTimeInMillis( System.currentTimeMillis() );
 
        return dateFormat.format( cal.getTime() );
 
    }
 

 
    /**
 
     * Each TimeZoneWrapper has an id of a component in the UI
 
     *  that corresponds to its time zone.  By this, if an event comes
 
     *  from a component in the web page, then this will return the
 
     *  relevant TimeZoneWrapper.
 
     *
 
     * @param componentId Id of component in UI
 
     * @return TimeZoneWrapper
 
     */
 
    private TimeZoneWrapper getTimeZoneWrapperByComponentId(String componentId) 
{
 
        for(int i = 0; i < allTimeZoneList.size(); i++) {
 
            TimeZoneWrapper tzw = (TimeZoneWrapper) allTimeZoneList.get( i );
 
            if( tzw.isRelevantComponentId(componentId) )
 
                return tzw;
 
        }
 
        return null;
 
    }
 

 
    //
 
    // Implicit interfaces as defined by the callbacks in the web files
 
    //
 

 
    /**
 
     * Listens to client input from commandButtons in the UI map and sets the 
 
     * selected time zone.
 
     *
 
     * @param event ActionEvent.
 
     */
 
    public void listen(ActionEvent event) {
 
        UIComponent comp = event.getComponent();
 
        FacesContext context = FacesContext.getCurrentInstance();
 
        String componentId = comp.getClientId(context);
 
        TimeZoneWrapper tzw = getTimeZoneWrapperByComponentId( componentId );
 
        if( tzw != null ) {
 
            selectedTimeZone = TimeZone.getTimeZone( tzw.getId() );
 
            selectedFormat = buildDateFormatForTimeZone( selectedTimeZone );
 
        }
 
    }
 
} // End of TimeZoneBean class
 

 

TimeZoneBean stores the current state and time zone information, and also handles the actions generated by clicking the commandButtons. The listen(ActionEvent event) method in the bean takes the ID of the commandButton clicked, and uses the helper TimeZoneWrapper objects to determine which TimeZone ID should be used to instantiate the selectedTimeZone object. For clarity, the commandButton IDs represent the offset from Greenwich Mean Time (GMT) of their respective TimeZone objects. The IDs can be arbitrary, as long as they are all unique in the web application, and match between the web file and the Java event handler.



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

TOC PREV NEXT INDEX