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.FrequencyRange; 010 011 import org.apache.log4j.Logger; 012 013 /** 014 * Note that this converter assumes that the low and high units are the same and 015 * does not allow for negative frequencies because the Frequency class does not! 016 */ 017 public class FrequencyRangeConverter implements Converter 018 { 019 private static Logger log = Logger.getLogger(FrequencyRangeConverter.class); 020 021 public Object getAsObject(FacesContext context, UIComponent component, String value) 022 { 023 log.debug("Trying to convert " + value + " to an object"); 024 025 FrequencyRange fr = null; 026 if (value != null && value.length() > 0) 027 { 028 try 029 { 030 fr = FrequencyRange.parse(value); 031 } 032 catch(IllegalArgumentException iae) 033 { 034 log.warn("Could not convert the value '" + value + "'"); 035 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Can not convert value '" + value + "' to a Frequency Range")); 036 } 037 } 038 039 log.debug("Converter returning: " + fr.toString()); 040 return fr; 041 } 042 043 public String getAsString(FacesContext context, UIComponent component, Object value) 044 { 045 log.debug("Trying to convert " + value + " to a string"); 046 047 if (value == null) 048 return ""; 049 050 else if (value instanceof FrequencyRange) 051 { 052 return value.toString(); 053 } 054 055 else 056 { 057 log.warn("Cannot Convert objects of type: " + value.getClass()); 058 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Cannot Convert objects of type: " + value.getClass())); 059 } 060 } 061 }