Package DisplayProject

Source Code of DisplayProject.NumericDataFormatter

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject;

import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.text.NumberFormatter;

import Framework.DataValue;
import Framework.NumericData;
import Framework.TextData;

/**
* Base class for concrete formatters of NumericData instances. Provides
* several factory methods for well known numeric data formatters
*/
@SuppressWarnings("serial")
public abstract class NumericDataFormatter extends NumberFormatter
{
    public NumericDataFormatter(NumberFormat format)
    {
        super(format);

        setAllowsInvalid(true);
    }

    public String valueToString(Object value) throws ParseException {
      // TF:29/06/2008:Check to see if we're mapped to a nullable type
        if (value == null) {
          if (getValueClass() != null && DataValue.class.isAssignableFrom(getValueClass())) {
            return "N/A";
          }
          else {
            return "";
          }
        }
        else if (value instanceof Double) {
            return format((Double)value, (NumberFormat) getFormat());
        }
        else if (value instanceof Number) {
          double doubleValue = ((Number)value).doubleValue();
            return format(new Double(doubleValue), (NumberFormat) getFormat());
        }
        // TF:12/03/2009:DET-83: We need to be able to handle strings and other data values as well,
        // just in case the model and the mapped type don't match
        else if (value instanceof NumericData) {
          NumericData numericValue = (NumericData) value;
          if (numericValue.isNullable() && numericValue.isNull()) {
              return "N/A";
          }
          return format(numericValue, (NumberFormat) getFormat());
        }
        else if (value instanceof DataValue) {
            DataValue dataValue = (DataValue) value;
            if (dataValue.isNullable() && dataValue.isNull()) {
              return "N/A";
            }
            else {
              return dataValueToString((DataValue)value);
            }
        }
        else if (value instanceof String) {
            return (String)value;
        }

        return "";
    }

    protected String dataValueToString(DataValue value) {
        TextData buffer = new TextData();
        value.fillString(buffer);
        return buffer.toString();
    }


    @SuppressWarnings("unchecked")
  public Object stringToValue(String text) throws ParseException
    {
      // TF:9/3/08:This class is only used to format numeric data objects, so we know from it's
      // construction that the value class must be a subclass of data value
      Class<? extends DataValue> formatClass = (Class<? extends DataValue>)getValueClass();
        NumericData numericData = (NumericData) FormatterUtils.createDataValueInstance(formatClass);
        if (!FormatterUtils.isNullable(numericData, text))
        {
            // Check if the value is null, then map to 0.  This is what Forte did.  CraigM: 19/02/2008.
            if (text == null || text.length() == 0) {
                setNumericData(numericData, 0);
            }
            else {
                Number number = (Number) getFormat().parseObject(text);
                setNumericData(numericData, number);
            }
        }
        else
        {
            numericData.setIsNull(true);
        }
        return numericData;
    }

    public abstract void setNumericData(NumericData source, Number number);

    public abstract String format(NumericData source, NumberFormat format);

    public abstract String format(Double source, NumberFormat format);
    /**
     * Factory method that creates a formatter for use with IntegerDatas.
     * @param numberFormat the number format instance to use
     * @return formatter
     */
    public static NumericDataFormatter getIntegerDataInstance(NumberFormat numberFormat)
    {
        return new NumericDataFormatter(numberFormat)
        {
            public String format(NumericData source, NumberFormat format)
            {
                return format.format(new Integer(source.intValue()));
            }

            public void setNumericData(NumericData source, Number number)
            {
                source.setValue(number.intValue());
            }

            public String format(Double source, NumberFormat format) {
                return format.format(source);
            }

        };
    }

    /**
     * Factory method that creates a formatter for use with IntegerDatas with
     * a supplied number format instance.

     * @return formatter
     */
    public static NumericDataFormatter getIntegerDataInstance()
    {
        NumericDataFormatter formatter = getIntegerDataInstance(NumberFormat.getIntegerInstance());
        return formatter;
    }

    /**
     * Factory method that creates a formatter for use with Decimal Data.
     * @param numberFormat the number format instance to use
     * @return formatter
     */
    public static NumericDataFormatter getDecimalDataInstance(NumberFormat numberFormat)
    {
        return new NumericDataFormatter(numberFormat)
        {
            public String format(NumericData source, NumberFormat format)
            {
                return format.format(new Double(source.doubleValue()));
            }

            public void setNumericData(NumericData source, Number number)
            {
                source.setValue(number.doubleValue());
            }

            public String format(Double source, NumberFormat format) {
                return format.format(source);
            }

        };
    }

    /**
     * Factory method that creates a formatter for use with Decimal Data with
     * a supplied number format instance.

     * @return formatter
     */
    public static NumericDataFormatter getDecimalDataInstance()
    {
        NumericDataFormatter formatter = getDecimalDataInstance(NumberFormat.getNumberInstance());
        return formatter;
    }
}
TOP

Related Classes of DisplayProject.NumericDataFormatter

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.