Coverage Report - org.apache.tapestry.form.translator.NumberTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberTranslator
97% 
100% 
1.385
 
 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 org.apache.hivemind.util.PropertyUtils;
 18  
 import org.apache.tapestry.IMarkupWriter;
 19  
 import org.apache.tapestry.IRequestCycle;
 20  
 import org.apache.tapestry.form.FormComponentContributorContext;
 21  
 import org.apache.tapestry.form.IFormComponent;
 22  
 import org.apache.tapestry.json.JSONLiteral;
 23  
 import org.apache.tapestry.json.JSONObject;
 24  
 import org.apache.tapestry.valid.ValidationConstants;
 25  
 import org.apache.tapestry.valid.ValidationConstraint;
 26  
 import org.apache.tapestry.valid.ValidationStrings;
 27  
 
 28  
 import java.text.DecimalFormat;
 29  
 import java.text.DecimalFormatSymbols;
 30  
 import java.text.Format;
 31  
 import java.util.Locale;
 32  
 
 33  
 /**
 34  
  * A {@link java.text.DecimalFormat}-based {@link Translator} implementation.
 35  
  *
 36  
  * @author Paul Ferraro
 37  
  * @since 4.0
 38  
  */
 39  
 public class NumberTranslator extends FormatTranslator
 40  
 {
 41  17
     private boolean _omitZero = false;
 42  
 
 43  
     public NumberTranslator()
 44  13
     {
 45  13
     }
 46  
 
 47  
     //TODO: Needed until HIVEMIND-134 fix is available
 48  
     public NumberTranslator(String initializer)
 49  4
     {
 50  4
         PropertyUtils.configureProperties(this, initializer);
 51  4
     }
 52  
 
 53  
     protected String formatObject(IFormComponent field, Locale locale, Object object)
 54  
     {
 55  6
         Number number = (Number) object;
 56  
 
 57  6
         if (_omitZero)
 58  
         {
 59  1
             if (number.doubleValue() == 0)
 60  1
                 return "";
 61  
         }
 62  
 
 63  5
         return super.formatObject(field, locale, object);
 64  
     }
 65  
 
 66  
     protected Object getValueForEmptyInput()
 67  
     {
 68  2
         return _omitZero ? null : new Double(0);
 69  
     }
 70  
 
 71  
     /**
 72  
      * @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
 73  
      */
 74  
     protected String defaultPattern()
 75  
     {
 76  17
         return "#";
 77  
     }
 78  
 
 79  
     /**
 80  
      * @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
 81  
      */
 82  
     protected Format getFormat(Locale locale)
 83  
     {
 84  10
         return getDecimalFormat(locale);
 85  
     }
 86  
 
 87  
     public DecimalFormat getDecimalFormat(Locale locale)
 88  
     {
 89  20
         return new DecimalFormat(getPattern(), new DecimalFormatSymbols(locale));
 90  
     }
 91  
 
 92  
     /**
 93  
      * @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
 94  
      */
 95  
     protected String getMessageKey()
 96  
     {
 97  6
         return ValidationStrings.INVALID_NUMBER;
 98  
     }
 99  
 
 100  
     /**
 101  
      * @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale,
 102  
      *      java.lang.String)
 103  
      */
 104  
     protected Object[] getMessageParameters(Locale locale, String label)
 105  
     {
 106  6
         String pattern = getDecimalFormat(locale).toLocalizedPattern();
 107  
 
 108  6
         return new Object[] { label, pattern };
 109  
     }
 110  
 
 111  
     /**
 112  
      * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter,
 113  
      *      org.apache.tapestry.IRequestCycle, FormComponentContributorContext,
 114  
      *      org.apache.tapestry.form.IFormComponent)
 115  
      */
 116  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 117  
                                    FormComponentContributorContext context, IFormComponent field)
 118  
     {
 119  4
         super.renderContribution(writer, cycle, context, field);
 120  
 
 121  4
         String message = buildMessage(context, field, getMessageKey());
 122  
 
 123  4
         JSONObject profile = context.getProfile();
 124  4
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 125  4
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 126  
         }
 127  4
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 128  
 
 129  4
         context.addInitializationScript(field, "dojo.require(\"dojo.i18n.number\");");
 130  
 
 131  4
         DecimalFormat format = getDecimalFormat(context.getLocale());
 132  
 
 133  4
         String grouping = "";
 134  4
         if (format.isGroupingUsed()) {
 135  
 
 136  1
             grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator());
 137  1
             grouping += ",groupSize:" + format.getGroupingSize();
 138  
         } else {
 139  
 
 140  3
             grouping += ",separator:\"\"";
 141  
         }
 142  
 
 143  4
         cons.accumulate(field.getClientId(),
 144  
                         new JSONLiteral("[dojo.i18n.number.isReal,null,{"
 145  
                                         + "places:" + format.getMaximumFractionDigits() + ","
 146  
                                         + "decimal:"
 147  
                                         + JSONObject.quote(format.getDecimalFormatSymbols().getDecimalSeparator())
 148  
                                         + grouping
 149  
                                         + "}]"));
 150  
 
 151  4
         accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, message);
 152  4
     }
 153  
 
 154  
     /**
 155  
      * @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
 156  
      */
 157  
     protected ValidationConstraint getConstraint()
 158  
     {
 159  2
         return ValidationConstraint.NUMBER_FORMAT;
 160  
     }
 161  
 
 162  
     /**
 163  
      * If true (which is the default for the property), then values that are 0 are rendered to an
 164  
      * empty string, not "0" or "0.00". This is useful in most cases where the field is optional; it
 165  
      * allows the field to render blank when no value is present.
 166  
      *
 167  
      * @param omitZero
 168  
      *          Whether or not to omit zero.
 169  
      */
 170  
 
 171  
     public void setOmitZero(boolean omitZero)
 172  
     {
 173  3
         _omitZero = omitZero;
 174  3
     }
 175  
 
 176  
     public boolean isOmitZero()
 177  
     {
 178  0
         return _omitZero;
 179  
     }
 180  
 }