Package org.boco.seamUtility.converter

Source Code of org.boco.seamUtility.converter.DoubleConverter

/***************************************************************************
* Copyright (c) 2004 - 2008  Fabrizio Boco fabboco@users.sourceforge.net  *
*                                                                         *
*                                                                         *
*   This is free software; you can redistribute it and/or                 *
*   modify it under the terms of the GNU Library General Public           *
*   License (version 2.1) as published by the Free Software Foundation    *
*                                                                         *
*   This library  is distributed in the hope that it will be useful,      *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU Library General Public License for more details.                  *
*                                                                         *
*   You should have received a copy of the GNU Library General Public     *
*   License along with this library; see the file COPYING.LIB. If not,    *
*   write to the Free Software Foundation, Inc., 59 Temple Place,         *
*   Suite 330, Boston, MA  02111-1307, USA                                *
*                                                                         *
***************************************************************************/
/**
- $Header: /usr/local/cvslocalrepository/SeamUtility/src/org/boco/seamUtility/converter/DoubleConverter.java,v 1.4.4.3 2008/06/07 17:08:11 fab Exp $
- $Author: fab $
- $Revision: 1.4.4.3 $
- $Date: 2008/06/07 17:08:11 $
- $Log: DoubleConverter.java,v $
- Revision 1.4.4.3  2008/06/07 17:08:11  fab
- Correzione di tutti DummyConverter
-
- Revision 1.4.4.2  2008/04/22 06:14:35  fab
- Aggiornamento indirizzo di posta
-
- Revision 1.4.4.1  2008/04/19 11:14:41  fab
- Aggiornamento riferimenti licenza e documentazione
-
- Revision 1.4  2008/01/27 17:26:59  fab
- La stringa di formato viene letta dal file messages dell'applicazione per supportare l'internazionalizzazione e la configurazione completa
-
- Revision 1.3  2008/01/08 11:33:28  fab
- Fix per evitare che nella stampa PDF compaia null nei campi vuoti
-
- Revision 1.2  2007/11/27 17:55:16  bob
- Fix sui validatori
-
- Revision 1.1  2007/09/30 07:45:40  fab
- Nuova versione iniziale del 30/09/2007
-
- Revision 1.1  2006/11/28 09:37:58  dev
- Spostati tutti i converter in un nuovo package
-
- Revision 1.1  2006/07/29 07:25:19  dev
- Versione Iniziale
-
**/
package org.boco.seamUtility.converter;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.validator.ValidatorException;

import org.boco.seamUtility.messages.MessagesLoader;

public class DoubleConverter implements Converter, Serializable
{
  private static final long  serialVersionUID  = 6675907275085599225L;


  public DoubleConverter()
  {
    super();
  }

  public String getAsString(FacesContext facesContext, UIComponent component, Object obj)
  {
    if (obj == null)
      return "";

    String format = MessagesLoader.getMessageResourceString(facesContext.getApplication().getMessageBundle(), "Application_DoubleFormat", null, facesContext.getViewRoot().getLocale());   
    DecimalFormat  numberFormat = new DecimalFormat(format);   
   
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();

    dfs.setDecimalSeparator(MessagesLoader.getMessageResourceString(facesContext.getApplication().getMessageBundle(), "Application_DecimalSeparator", null, facesContext.getViewRoot().getLocale()).charAt(0));
    dfs.setGroupingSeparator(MessagesLoader.getMessageResourceString(facesContext.getApplication().getMessageBundle(), "Application_GroupingSeparator", null, facesContext.getViewRoot().getLocale()).charAt(0));

    numberFormat.setDecimalFormatSymbols(dfs);
   
    return numberFormat.format(obj);
  }

  public Object getAsObject(FacesContext facesContext, UIComponent component, String str) throws ConverterException
  {
    if (str == null || str.length() == 0)
    {
      return null;
    }

    try
    {
      String format = MessagesLoader.getMessageResourceString(facesContext.getApplication().getMessageBundle(), "Application_DoubleFormat", null, facesContext.getViewRoot().getLocale());   
      DecimalFormat  numberFormat = new DecimalFormat(format);   

      DecimalFormatSymbols dfs = new DecimalFormatSymbols();

      dfs.setDecimalSeparator(MessagesLoader.getMessageResourceString(facesContext.getApplication().getMessageBundle(), "Application_DecimalSeparator", null, facesContext.getViewRoot().getLocale()).charAt(0));
      dfs.setGroupingSeparator(MessagesLoader.getMessageResourceString(facesContext.getApplication().getMessageBundle(), "Application_GroupingSeparator", null, facesContext.getViewRoot().getLocale()).charAt(0));

      numberFormat.setDecimalFormatSymbols(dfs);
     
      return new Double(numberFormat.parse(str).doubleValue());
    }
    catch (ParseException e)
    {
      String summary = MessagesLoader.getMessageResourceString(facesContext.getApplication().getMessageBundle(), "error_NotDoubleSummary", null, facesContext.getViewRoot().getLocale());
      String detail = MessagesLoader.getMessageResourceString(facesContext.getApplication().getMessageBundle(), "error_NotDoubleDetail", null, facesContext.getViewRoot().getLocale());

      FacesMessage message = new FacesMessage();
      message.setSummary(summary);
      message.setDetail(detail);
      message.setSeverity(FacesMessage.SEVERITY_ERROR);
      throw new ConverterException(message);
    }
  }
}
TOP

Related Classes of org.boco.seamUtility.converter.DoubleConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.