001    package edu.nrao.sss.webapp.faces.converter;
002    
003    import edu.nrao.sss.measure.LinearVelocityUnits;
004    
005    import javax.faces.convert.Converter;
006    import javax.faces.convert.ConverterException;
007    import javax.faces.context.FacesContext;
008    import javax.faces.component.UIComponent;
009    import javax.faces.application.FacesMessage;
010    
011    import org.apache.log4j.Logger;
012    
013    public class LinearVelocityUnitsConverter implements Converter
014    {
015            private static Logger log = Logger.getLogger(BigDecimalConverter.class);
016            
017            public Object getAsObject(FacesContext context, UIComponent component, String value)
018            {
019                    log.debug("Trying to convert " + value + " to an object");
020                    
021                    LinearVelocityUnits u = null;
022                    if (value != null && value.length() > 0)
023                    {
024                            try
025                            {
026                                    u = LinearVelocityUnits.fromString(value);
027                            }
028                            
029                            catch (NumberFormatException nfe)
030                            {
031                                    throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Value '" + value + "' is not an angular velocity unit we recognize."), nfe);
032                            }
033                    }
034                    
035                    return u;
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 LinearVelocityUnits)
046                    {
047                            return ((LinearVelocityUnits)value).getSymbol();
048                    }
049    
050                    else
051                    {
052                            log.warn("Cannot Convert objects of type: " + value.getClass());
053                            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "LinearVelocityUnitsConverter cannot convert objects of type: " + value.getClass()));
054                    }
055            }
056    }