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.Angle;
010    import edu.nrao.sss.util.StringUtil;
011    
012    import org.apache.log4j.Logger;
013    
014    /**
015     * This class converts a string that is a double followed by a space and then
016     * some units into a Angle and the other way around.
017     */
018    public class AngleConverter implements Converter
019    {
020            private static Logger log = Logger.getLogger(AngleConverter.class);
021      
022      private static final StringUtil FORMATTER = StringUtil.getInstance();
023    
024            public Object getAsObject(FacesContext context, UIComponent component, String value)
025            {
026                    log.debug("Trying to convert " + value + " to an object");
027                    
028                    Angle angle = null;
029                    if (value != null && value.length() > 0)
030                    {
031                            try
032                            {
033                                    angle = Angle.parse(value);
034                            }
035                            
036                            catch (IllegalArgumentException iae)
037                            {
038                                    throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Value '" + value + "' is not a valid Angle."), iae);
039                            }
040                    }
041                    
042                    return angle;
043            }
044    
045            public String getAsString(FacesContext context, UIComponent component, Object value)
046            {
047                    log.debug("Trying to convert " + value + " to a string");
048                    
049                    if (value == null)
050                            return "";
051    
052                    else if (value instanceof Angle)
053                    {
054                            Angle angle = (Angle)value;
055          
056          //Don't display the units if the value is infinity.
057          String s =
058            angle.isInfinite() ? FORMATTER.formatNoScientificNotation(angle.getValue())
059                               : angle.toString();
060    
061          log.debug("Returning value: " + s);
062          
063          return s;
064                    }
065    
066                    else
067                    {
068                            log.warn("Cannot Convert objects of type: " + value.getClass());
069                            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Cannot Convert objects of type: " + value.getClass()));
070                    }
071            }
072    }