Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 EST
file stats: LOC: 149   Methods: 12
NCLOC: 76   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NumberTranslator.java 100% 100% 100% 100%
coverage
 1    // Copyright 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.form.translator;
 16   
 17    import java.text.DecimalFormat;
 18    import java.text.DecimalFormatSymbols;
 19    import java.text.Format;
 20    import java.util.Locale;
 21   
 22    import org.apache.hivemind.util.PropertyUtils;
 23    import org.apache.tapestry.IMarkupWriter;
 24    import org.apache.tapestry.IRequestCycle;
 25    import org.apache.tapestry.TapestryUtils;
 26    import org.apache.tapestry.form.FormComponentContributorContext;
 27    import org.apache.tapestry.form.IFormComponent;
 28    import org.apache.tapestry.valid.ValidationConstraint;
 29    import org.apache.tapestry.valid.ValidationStrings;
 30   
 31    /**
 32    * A {@link java.text.DecimalFormat}-based {@link Translator} implementation.
 33    *
 34    * @author Paul Ferraro
 35    * @since 4.0
 36    */
 37    public class NumberTranslator extends FormatTranslator
 38    {
 39    private boolean _omitZero = true;
 40   
 41  11 public NumberTranslator()
 42    {
 43    }
 44   
 45  5 protected String formatObject(IFormComponent field, Locale locale, Object object)
 46    {
 47  5 Number number = (Number) object;
 48   
 49  5 if (_omitZero)
 50    {
 51  4 if (number.doubleValue() == 0)
 52   
 53  1 return "";
 54    }
 55   
 56  4 return super.formatObject(field, locale, object);
 57    }
 58   
 59    // Needed until HIVEMIND-134 fix is available
 60  3 public NumberTranslator(String initializer)
 61    {
 62  3 PropertyUtils.configureProperties(this, initializer);
 63    }
 64   
 65    /**
 66    * @see org.apache.tapestry.form.AbstractFormComponentContributor#defaultScript()
 67    */
 68  17 protected String defaultScript()
 69    {
 70  17 return "/org/apache/tapestry/form/translator/NumberTranslator.js";
 71    }
 72   
 73    /**
 74    * @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
 75    */
 76  14 protected String defaultPattern()
 77    {
 78  14 return "#";
 79    }
 80   
 81    /**
 82    * @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
 83    */
 84  9 protected Format getFormat(Locale locale)
 85    {
 86  9 return getDecimalFormat(locale);
 87    }
 88   
 89  14 public DecimalFormat getDecimalFormat(Locale locale)
 90    {
 91  14 return new DecimalFormat(getPattern(), new DecimalFormatSymbols(locale));
 92    }
 93   
 94    /**
 95    * @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
 96    */
 97  5 protected String getMessageKey()
 98    {
 99  5 return ValidationStrings.INVALID_NUMBER;
 100    }
 101   
 102    /**
 103    * @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale,
 104    * java.lang.String)
 105    */
 106  5 protected Object[] getMessageParameters(Locale locale, String label)
 107    {
 108  5 String pattern = getDecimalFormat(locale).toLocalizedPattern();
 109   
 110  5 return new Object[]
 111    { label, pattern };
 112    }
 113   
 114    /**
 115    * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter,
 116    * org.apache.tapestry.IRequestCycle, FormComponentContributorContext,
 117    * org.apache.tapestry.form.IFormComponent)
 118    */
 119  3 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 120    FormComponentContributorContext context, IFormComponent field)
 121    {
 122  3 super.renderContribution(writer, cycle, context, field);
 123   
 124  3 String message = TapestryUtils.enquote(buildMessage(context, field, getMessageKey()));
 125   
 126  3 context.addSubmitHandler("function(event) { Tapestry.validate_number(event, '"
 127    + field.getClientId() + "', " + message + "); }");
 128    }
 129   
 130    /**
 131    * @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
 132    */
 133  2 protected ValidationConstraint getConstraint()
 134    {
 135  2 return ValidationConstraint.NUMBER_FORMAT;
 136    }
 137   
 138    /**
 139    * If true (which is the default for the property), then values that are 0 are rendered to an
 140    * empty string, not "0" or "0.00". This is useful in most cases where the field is optional; it
 141    * allow the field to render blank when no value is present.
 142    */
 143   
 144  1 public void setOmitZero(boolean omitZero)
 145    {
 146  1 _omitZero = omitZero;
 147    }
 148   
 149    }