Coverage Report - org.apache.tapestry.form.translator.NumberTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberTranslator
0%
0/37
0%
0/10
1.462
 
 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  0
     private boolean _omitZero = false;
 42  
 
 43  
     public NumberTranslator()
 44  0
     {
 45  0
     }
 46  
 
 47  
     //TODO: Needed until HIVEMIND-134 fix is available
 48  
     public NumberTranslator(String initializer)
 49  0
     {
 50  0
         PropertyUtils.configureProperties(this, initializer);
 51  0
     }
 52  
 
 53  
     protected String formatObject(IFormComponent field, Locale locale, Object object)
 54  
     {
 55  0
         Number number = (Number) object;
 56  
 
 57  0
         if (_omitZero)
 58  
         {
 59  0
             if (number.doubleValue() == 0)
 60  0
                 return "";
 61  
         }
 62  
 
 63  0
         return super.formatObject(field, locale, object);
 64  
     }
 65  
 
 66  
     protected Object getValueForEmptyInput()
 67  
     {
 68  0
         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  0
         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  0
         return getDecimalFormat(locale);
 85  
     }
 86  
 
 87  
     public DecimalFormat getDecimalFormat(Locale locale)
 88  
     {
 89  0
         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  0
         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  0
         String pattern = getDecimalFormat(locale).toLocalizedPattern();
 107  
 
 108  0
         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  0
         super.renderContribution(writer, cycle, context, field);
 120  
 
 121  0
         String message = buildMessage(context, field, getMessageKey());
 122  
 
 123  0
         JSONObject profile = context.getProfile();
 124  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 125  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 126  
         }
 127  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 128  
 
 129  0
         DecimalFormat format = getDecimalFormat(context.getLocale());
 130  
 
 131  0
         String grouping = "";
 132  0
         if (format.isGroupingUsed()) {
 133  
 
 134  0
             grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator());
 135  0
             grouping += ",groupSize:" + format.getGroupingSize();
 136  
         } else {
 137  
 
 138  0
             grouping += ",separator:\"\"";
 139  
         }
 140  
 
 141  0
         cons.accumulate(field.getClientId(),
 142  
                         new JSONLiteral("[tapestry.form.validation.isReal,null,{"
 143  
                                         + "places:" + format.getMaximumFractionDigits() + ","
 144  
                                         + "decimal:"
 145  
                                         + JSONObject.quote(format.getDecimalFormatSymbols().getDecimalSeparator())
 146  
                                         + grouping
 147  
                                         + "}]"));
 148  
 
 149  0
         accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, message);
 150  0
     }
 151  
 
 152  
     /**
 153  
      * @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
 154  
      */
 155  
     protected ValidationConstraint getConstraint()
 156  
     {
 157  0
         return ValidationConstraint.NUMBER_FORMAT;
 158  
     }
 159  
 
 160  
     /**
 161  
      * If true (which is the default for the property), then values that are 0 are rendered to an
 162  
      * empty string, not "0" or "0.00". This is useful in most cases where the field is optional; it
 163  
      * allows the field to render blank when no value is present.
 164  
      *
 165  
      * @param omitZero
 166  
      *          Whether or not to omit zero.
 167  
      */
 168  
 
 169  
     public void setOmitZero(boolean omitZero)
 170  
     {
 171  0
         _omitZero = omitZero;
 172  0
     }
 173  
 
 174  
     public boolean isOmitZero()
 175  
     {
 176  0
         return _omitZero;
 177  
     }
 178  
 }