Coverage Report - net.wotonomy.web.WOInput
 
Classes in this File Line Coverage Branch Coverage Complexity
WOInput
0% 
0% 
2.364
 
 1  
 
 2  
 package net.wotonomy.web;
 3  
 
 4  
 import java.text.DecimalFormat;
 5  
 import java.text.ParseException;
 6  
 import java.text.SimpleDateFormat;
 7  
 
 8  
 import net.wotonomy.foundation.NSDictionary;
 9  
 import net.wotonomy.foundation.NSMutableArray;
 10  
 
 11  
 public abstract class WOInput extends WODynamicElement {
 12  
 
 13  
     public WOInput() {
 14  0
         super();
 15  0
     }
 16  
 
 17  
     public WOInput(String aName, NSDictionary assocs, WOElement template) {
 18  0
         super(aName, assocs, template);
 19  0
     }
 20  
 
 21  
     protected abstract String inputType();
 22  
     protected abstract Object value(WOContext c);
 23  
 
 24  
     protected NSMutableArray additionalAttributes() {
 25  0
         return new NSMutableArray(new Object[]{
 26  0
             "disabled", "type", "value", "name"
 27  
         });
 28  
     }
 29  
 
 30  
     public String inputName(WOContext c) {
 31  0
         String x = (String)valueForProperty("name", c.component());
 32  0
         if (x != null)
 33  0
             return x;
 34  0
         return c.elementID();
 35  
     }
 36  
 
 37  
     protected boolean disabled(WOContext c) {
 38  0
         return booleanForProperty("disabled", c.component());
 39  
     }
 40  
 
 41  
     protected void appendExtras(WOResponse r, WOContext c) {
 42  0
     }
 43  
 
 44  
     public void appendToResponse(WOResponse r, WOContext c) {
 45  0
         r.appendContentString("<INPUT TYPE=\"");
 46  0
         r.appendContentString(inputType());
 47  0
         r.appendContentString("\" NAME=\"");
 48  0
         r.appendContentString(inputName(c));
 49  0
         r.appendContentString("\" VALUE=\"");
 50  0
         r.appendContentString(value(c).toString());
 51  0
         r.appendContentString("\"");
 52  0
         String moreFields = additionalHTMLProperties(c.component(), additionalAttributes());
 53  0
         if (moreFields != null && moreFields.length() > 0)
 54  0
             r.appendContentString(moreFields);
 55  0
         appendExtras(r, c);
 56  0
         if (disabled(c)) {
 57  0
             r.appendContentString(" DISABLED");
 58  
         }
 59  0
         r.appendContentString(">");
 60  0
     }
 61  
 
 62  
     public void takeValuesFromRequest(WORequest r, WOContext c) {
 63  0
         if (disabled(c))
 64  0
             return;
 65  0
         Object val = r.formValueForKey(inputName(c));
 66  0
         WOAssociation va = (WOAssociation)associations.objectForKey("value");
 67  0
         if (val != null && va != null && va.isValueSettable())
 68  0
             setValueForProperty("value", val, c.component());
 69  0
     }
 70  
 
 71  
         /** Formats a value as a date or number. Checks for
 72  
          * numberformat or dateformat associations; if one of them
 73  
          * exists, the value is formatter using the specified pattern.
 74  
          * @param value The value to format.
 75  
          * @return The original object, or a date or number if the
 76  
          * receiver has a numberformat or dateformat association.
 77  
          */
 78  
         protected Object formattedValue(Object value, WOComponent c) {
 79  
                 //Format the value in case of number
 80  0
                 String pattern = (String)valueForProperty("numberformat", c);
 81  0
                 if (pattern != null) {
 82  0
                         DecimalFormat fmt = new DecimalFormat(pattern);
 83  
                         try {
 84  0
                                 return fmt.parse(value.toString());
 85  0
                         } catch (ParseException e) {
 86  0
                                 return value;
 87  
                         }
 88  
                 }
 89  
                 //Format the value in case of date
 90  0
                 pattern = (String)valueForProperty("dateformat", c);
 91  0
                 if (pattern != null) {
 92  0
                         SimpleDateFormat fmt = new SimpleDateFormat(pattern);
 93  
                         try {
 94  0
                                 return fmt.parse(value.toString());
 95  0
                         } catch (ParseException e) {
 96  0
                                 return value;
 97  
                         }
 98  
                 }
 99  0
                 return value;
 100  
         }
 101  
 
 102  
 }