Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.form.translator;
 
 import  org.apache.hivemind.util.PropertyUtils;
 
A java.text.DecimalFormat-based Translator implementation.

Author(s):
Paul Ferraro
Since:
4.0
 
 public class NumberTranslator extends FormatTranslator
 {
     private boolean _omitZero = false;
 
     public NumberTranslator()
     {
     }
 
     //TODO: Needed until HIVEMIND-134 fix is available
     public NumberTranslator(String initializer)
     {
         PropertyUtils.configureProperties(thisinitializer);
     }
 
     protected String formatObject(IFormComponent fieldLocale localeObject object)
     {
         Number number = (Numberobject;
 
         if ()
         {
             if (number.doubleValue() == 0)
                 return "";
         }
 
         return super.formatObject(fieldlocaleobject);
     }
 
     protected Object getValueForEmptyInput()
     {
         return  ? null : new Double(0);
     }

    

See also:
org.apache.tapestry.form.translator.FormatTranslator.defaultPattern()
 
     protected String defaultPattern()
     {
         return "#";
     }

    

See also:
org.apache.tapestry.form.translator.FormatTranslator.getFormat(java.util.Locale)
 
     protected Format getFormat(Locale locale)
     {
         return getDecimalFormat(locale);
     }
 
     public DecimalFormat getDecimalFormat(Locale locale)
     {
         return new DecimalFormat(getPattern(), new DecimalFormatSymbols(locale));
     }

    

See also:
org.apache.tapestry.form.translator.FormatTranslator.getMessageKey()
 
     protected String getMessageKey()
     {
         return .;
     }

    

See also:
org.apache.tapestry.form.translator.AbstractTranslator.getMessageParameters(java.util.Locale, java.lang.String)
    protected Object[] getMessageParameters(Locale localeString label)
    {
        String pattern = getDecimalFormat(locale).toLocalizedPattern();
        return new Object[] { labelpattern };
    }

    

See also:
org.apache.tapestry.form.FormComponentContributor.renderContribution(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, FormComponentContributorContext, org.apache.tapestry.form.IFormComponent)
    public void renderContribution(IMarkupWriter writerIRequestCycle cycle,
                                   FormComponentContributorContext contextIFormComponent field)
    {
        super.renderContribution(writercyclecontextfield);
        String message = buildMessage(contextfieldgetMessageKey());
        JSONObject profile = context.getProfile();
        if (!profile.has(.)) {
            profile.put(.new JSONObject());
        }
        JSONObject cons = profile.getJSONObject(.);
        DecimalFormat format = getDecimalFormat(context.getLocale());
        String grouping = "";
        if (format.isGroupingUsed()) {
            grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator());
            grouping += ",groupSize:" + format.getGroupingSize();
        } else {
            grouping += ",separator:\"\"";
        }
        cons.accumulate(field.getClientId(),
                        new JSONLiteral("[tapestry.form.validation.isReal,null,{"
                                        + "places:" + format.getMaximumFractionDigits() + ","
                                        + "decimal:"
                                        + JSONObject.quote(format.getDecimalFormatSymbols().getDecimalSeparator())
                                        + grouping
                                        + "}]"));
        accumulateProfileProperty(fieldprofile.message);
    }

    

See also:
org.apache.tapestry.form.translator.FormatTranslator.getConstraint()
    {
        return .;
    }

    
If true (which is the default for the property), then values that are 0 are rendered to an empty string, not "0" or "0.00". This is useful in most cases where the field is optional; it allows the field to render blank when no value is present.

Parameters:
omitZero Whether or not to omit zero.
    public void setOmitZero(boolean omitZero)
    {
         = omitZero;
    }
    public boolean isOmitZero()
    {
        return ;
    }
New to GrepCode? Check out our FAQ X