001 package edu.nrao.sss.webapp.faces.converter; 002 003 import javax.faces.convert.Converter; 004 import javax.faces.convert.ConverterException; 005 import javax.faces.context.FacesContext; 006 import javax.faces.component.UIComponent; 007 import javax.faces.application.FacesMessage; 008 009 import edu.nrao.sss.measure.TimeDuration; 010 011 import org.apache.log4j.Logger; 012 013 /** 014 * This class converts a string that is a double followed by a space and then 015 * some units into a TimeDuration and the other way around. 016 */ 017 public class TimeDurationConverter implements Converter 018 { 019 private static Logger log = Logger.getLogger(TimeDurationConverter.class); 020 021 public Object getAsObject(FacesContext context, UIComponent component, String value) 022 { 023 if (value == null) 024 return null; 025 026 log.debug("Trying to convert " + value + " to an object"); 027 028 try 029 { 030 return TimeDuration.parse(value); 031 } 032 catch(IllegalArgumentException iae) 033 { 034 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Can not convert value '" + value + "' into a Time Duration")); 035 } 036 } 037 038 public String getAsString(FacesContext context, UIComponent component, Object value) 039 { 040 log.debug("Trying to convert " + value + " to a string"); 041 042 if (value == null) 043 return ""; 044 045 else if (value instanceof TimeDuration) 046 { 047 return ((TimeDuration)value).toStringHms(1, 3); 048 } 049 050 else 051 { 052 log.warn("Cannot Convert objects of type: " + value.getClass()); 053 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Cannot Convert objects of type: " + value.getClass())); 054 } 055 } 056 }