View Javadoc

1   /*
2    * $Id: PaddingFormatter.java,v 1.2 2008/06/05 13:50:41 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.validator.impl;
5   
6   import javax.faces.component.UIComponent;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  import org.rcfaces.core.component.capability.IMaxTextLengthCapability;
11  import org.rcfaces.core.internal.lang.StringAppender;
12  import org.rcfaces.core.validator.IClientValidatorContext;
13  import org.rcfaces.core.validator.IFormatterTask;
14  
15  /**
16   * 
17   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
18   * @version $Revision: 1.2 $ $Date: 2008/06/05 13:50:41 $
19   */
20  public class PaddingFormatter extends AbstractClientValidatorTask implements
21          IFormatterTask {
22      private static final String REVISION = "$Revision: 1.2 $";
23  
24      private static final Log LOG = LogFactory.getLog(PaddingFormatter.class);
25  
26      public String applyFormatter(IClientValidatorContext context, String value) {
27  
28          if (value == null || value.length() == 0) {
29              return value;
30          }
31  
32          UIComponent component = context.getComponentRenderContext()
33                  .getComponent();
34          if ((component instanceof IMaxTextLengthCapability) == false) {
35              return value;
36          }
37  
38          int maxTextLength = ((IMaxTextLengthCapability) component)
39                  .getMaxTextLength();
40          if (maxTextLength < 1) {
41              return value;
42          }
43  
44          String padderString = getParameter(context, "padder.value", " ");
45          boolean leftSide = getBoolParameter(context, "padder.leftSide", true);
46          boolean modifyInput = getBoolParameter(context, "padder.modifyInput",
47                  true);
48  
49          int length = maxTextLength - value.length();
50          if (length <= 0) {
51              return value;
52          }
53  
54          StringAppender sa = new StringAppender(maxTextLength);
55  
56          if (leftSide == false) {
57              sa.append(value);
58          }
59  
60          sa.append(padderString.charAt(0), length);
61  
62          if (leftSide) {
63              sa.append(value);
64          }
65  
66          if (modifyInput) {
67              context.setInputValue(value);
68          }
69  
70          return sa.toString();
71      }
72  }