View Javadoc

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          super();
15      }
16  
17      public WOInput(String aName, NSDictionary assocs, WOElement template) {
18          super(aName, assocs, template);
19      }
20  
21      protected abstract String inputType();
22      protected abstract Object value(WOContext c);
23  
24      protected NSMutableArray additionalAttributes() {
25          return new NSMutableArray(new Object[]{
26              "disabled", "type", "value", "name"
27          });
28      }
29  
30      public String inputName(WOContext c) {
31          String x = (String)valueForProperty("name", c.component());
32          if (x != null)
33              return x;
34          return c.elementID();
35      }
36  
37      protected boolean disabled(WOContext c) {
38          return booleanForProperty("disabled", c.component());
39      }
40  
41      protected void appendExtras(WOResponse r, WOContext c) {
42      }
43  
44      public void appendToResponse(WOResponse r, WOContext c) {
45          r.appendContentString("<INPUT TYPE=\"");
46          r.appendContentString(inputType());
47          r.appendContentString("\" NAME=\"");
48          r.appendContentString(inputName(c));
49          r.appendContentString("\" VALUE=\"");
50          r.appendContentString(value(c).toString());
51          r.appendContentString("\"");
52          String moreFields = additionalHTMLProperties(c.component(), additionalAttributes());
53          if (moreFields != null && moreFields.length() > 0)
54              r.appendContentString(moreFields);
55          appendExtras(r, c);
56          if (disabled(c)) {
57              r.appendContentString(" DISABLED");
58          }
59          r.appendContentString(">");
60      }
61  
62      public void takeValuesFromRequest(WORequest r, WOContext c) {
63          if (disabled(c))
64              return;
65          Object val = r.formValueForKey(inputName(c));
66          WOAssociation va = (WOAssociation)associations.objectForKey("value");
67          if (val != null && va != null && va.isValueSettable())
68              setValueForProperty("value", val, c.component());
69      }
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  		String pattern = (String)valueForProperty("numberformat", c);
81  		if (pattern != null) {
82  			DecimalFormat fmt = new DecimalFormat(pattern);
83  			try {
84  				return fmt.parse(value.toString());
85  			} catch (ParseException e) {
86  				return value;
87  			}
88  		}
89  		//Format the value in case of date
90  		pattern = (String)valueForProperty("dateformat", c);
91  		if (pattern != null) {
92  			SimpleDateFormat fmt = new SimpleDateFormat(pattern);
93  			try {
94  				return fmt.parse(value.toString());
95  			} catch (ParseException e) {
96  				return value;
97  			}
98  		}
99  		return value;
100 	}
101 
102 }