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.Distance;
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 Declination and the other way around.
017     */
018    public class DistanceConverter implements Converter
019    {
020            private static Logger log = Logger.getLogger(DistanceConverter.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                    Distance dist = null;
029                    if (value != null && value.length() > 0)
030                    {
031                            try
032                            {
033                                    //Since the units were taken out for Infinity, put them back to parse correctly.
034                                    if (value.equals("Infinity"))
035                                            dist = Distance.parse(value + "km");
036    
037                                    else
038                                            dist = Distance.parse(value);
039                            }
040                            
041                            catch (IllegalArgumentException iae)
042                            {
043                                    throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Value '" + value + "' is not in a recognized Distance format."), iae);
044                            }
045                    }
046                    
047                    return dist;
048            }
049    
050            public String getAsString(FacesContext context, UIComponent component, Object value)
051            {
052                    log.debug("Trying to convert " + value + " to a string");
053                    
054                    if (value == null)
055                            return "";
056    
057                    else if (value instanceof Distance)
058                    {
059                            Distance dist = (Distance)value;
060                            
061          //Don't display the units if the value is infinity.
062          String s =
063            dist.isInfinite() ? FORMATTER.formatNoScientificNotation(dist.getValue())
064                              : dist.toString();
065    
066                            log.debug("Returning value: " + s);
067                            
068                            return s;
069                    }
070    
071                    else
072                    {
073                            log.warn("Can not Convert objects of type: " + value.getClass());
074                            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Can not Convert objects of type: " + value.getClass()));
075                    }
076            }
077    }