Package DisplayProject

Source Code of DisplayProject.NumericFormatter

/*
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.ParseException;
import java.text.ParsePosition;

import javax.swing.JFormattedTextField;
import javax.swing.text.DocumentFilter;
import javax.swing.text.NavigationFilter;
import javax.swing.text.NumberFormatter;

import Framework.DataValue;
import Framework.DecimalData;
import Framework.NullAwareNumberFormat;
import Framework.NumericData;

/**
* This class can be used to format numeric data in much the same way that forte formatted numeric data,
* allowing for positive, negative, zero and null templates, with similar insert and removal rules.
*/
@SuppressWarnings("serial")
public class NumericFormatter extends NumberFormatter {
  private FormattedNumericDocumentFilter docFilter;
  private FormattedNumericNavigationFilter navFilter;
  private NullAwareNumberFormat formatter;
  private String originalFormat;

  public NumericFormatter(JFormattedTextField field, String pFormat, Class<?> valueClass) {
    super();
    this.originalFormat = pFormat;
        this.formatter = new NullAwareNumberFormat(pFormat);
      this.docFilter = new FormattedNumericDocumentFilter(formatter, field);           
        this.navFilter = new FormattedNumericNavigationFilter(field);
        this.setValueClass(valueClass);
  }

  @Override
  protected NavigationFilter getNavigationFilter() {
    return navFilter;
  }
  @Override
  protected DocumentFilter getDocumentFilter() {
    return docFilter;
  }
 
  public String getOriginalFormat() {
    return originalFormat;
  }
 
  @Override
  public Object stringToValue(String text) throws ParseException {
    Object result;
    // It is possible to have a blank mask in forte, allowing "" instead of 0 for instance. Thus
    // we need to cater for this case.
    if ("".equals(text)) {
          ParsePosition parsePosition = new ParsePosition(0);
          result = formatter.parse(text, parsePosition);
          if (parsePosition.getErrorIndex() >= 0) {
              throw new ParseException("Unparseable number: \"\"",
                                       parsePosition.getErrorIndex());
          }
    }
    else {
      result = formatter.parse(text);
    }
    return convertValueToValueClass(result);
  }
  @Override
  public String valueToString(Object value) throws ParseException {
    return formatter.format(value);
  }
 
  /**
   * Convert the passed value to a value that is compatible with the value class
   * associated with this type
   * @param value
   * @return
   */
  @SuppressWarnings("unchecked")
  private Object convertValueToValueClass(Object value) {
    Class<?> valueClass = getValueClass();
    if (value == null) {
      return null;
    }
    if (valueClass != null && value.getClass().equals(valueClass)) {
      return value;
    }
        if (valueClass != null && (value instanceof Number)) {
            if (valueClass == Integer.class) {
              return Integer.valueOf(((Number)value).intValue());
            }
            else if (valueClass == Long.class) {
              return Long.valueOf(((Number)value).longValue());
            }
            else if (valueClass == Float.class) {
                return new Float(((Number)value).floatValue());
            }
            else if (valueClass == Double.class) {
                return new Double(((Number)value).doubleValue());
            }
            else if (valueClass == Byte.class) {
              return Byte.valueOf(((Number)value).byteValue());
            }
            else if (valueClass == Short.class) {
                return new Short(((Number)value).shortValue());
            }
            else if (NumericData.class.isAssignableFrom(valueClass)) {
              Class<? extends NumericData>numericValueClass = (Class<? extends NumericData>)valueClass;
            try {
              // TF:10/4/08:Added in the scale of decimaldatas if necessary
              Object currentValue = this.docFilter.field.getValue();
          DataValue dataValue = FormatterUtils.createDataValueInstance(numericValueClass);
          if (currentValue instanceof DecimalData) {
            DecimalData originalValue = (DecimalData)currentValue;
            if (!originalValue.isNull() && dataValue instanceof DecimalData) {
              ((DecimalData)dataValue).setScale(originalValue.getScale());
            }
          }
          // TF:29 sept. 2008:Changed this to use the generic method to prevent numerics being
          // converted to strings, and hence losing international formatting characters
          FormatterUtils.setDataValue(dataValue, value);
          return dataValue;
        }
        catch (ParseException e) {
          // This shouldn't happen. Just return the original object
          return value;
        }
            }
        }
        return value;

  }
}
TOP

Related Classes of DisplayProject.NumericFormatter

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.