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.math.Polynomial; 010 011 import org.apache.log4j.Logger; 012 013 /** 014 * This class allows JSF to automatically convert Polynomials to and from 015 * Strings. The conversion is done using Polynomial.toString() and 016 * Polynomial.parse(). 017 * 018 * @see Polynomial#parse 019 * @see Polynomial#toString 020 */ 021 public class PolynomialConverter implements Converter 022 { 023 private static final Logger log = Logger.getLogger(PolynomialConverter.class); 024 025 public Object getAsObject(FacesContext context, UIComponent component, String value) 026 { 027 try 028 { 029 return Polynomial.parse(value); 030 } 031 catch (IllegalArgumentException iae) 032 { 033 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Value '" + value + "' is not in a recognized format for a Polynomial."), iae); 034 } 035 } 036 037 public java.lang.String getAsString(FacesContext context, UIComponent component, Object value) 038 { 039 if (value == null) 040 return ""; 041 042 else if (value instanceof Polynomial) 043 return value.toString(); 044 045 else 046 { 047 log.warn("Cannot Convert objects of type: " + value.getClass()); 048 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Cannot Convert objects of type: " + value.getClass())); 049 } 050 } 051 }