View Javadoc

1   
2   package net.wotonomy.web;
3   
4   import net.wotonomy.foundation.NSArray;
5   import net.wotonomy.foundation.NSDictionary;
6   import net.wotonomy.foundation.NSMutableArray;
7   
8   public class WOCheckBox extends WOInput {
9   
10      protected boolean checked = false;
11  
12      public WOCheckBox() {
13          super();
14      }
15  
16      public WOCheckBox(String aName, NSDictionary assocs, WOElement template) {
17          super(aName, assocs, template);
18      }
19  
20      protected String inputType() {
21          return "CHECKBOX";
22      }
23  
24      protected Object value(WOContext c) {
25          Object val = null;
26          boolean checked = false;
27          if (associations.objectForKey("value") != null) {
28              val = valueForProperty("value", c.component());
29              Object sel = valueForProperty("selection", c.component());
30              if (sel != null && val != null && sel.equals(val))
31                  checked = true;
32          }
33          if (val == null) {
34              val = c.elementID();
35          }
36          return val;
37      }
38  
39      protected void appendExtras(WOResponse r, WOContext c) {
40          checked |= booleanForProperty("checked", c.component());
41          if (checked)
42              r.appendContentString(" CHECKED");
43      }
44  
45      protected NSMutableArray additionalAttributes() {
46          NSMutableArray a = super.additionalAttributes();
47          a.addObject("checked");
48          a.addObject("selection");
49          return a;
50      }
51  
52      public void appendToResponse(WOResponse r, WOContext c) {
53          checked = false;
54          super.appendToResponse(r, c);
55      }
56  
57      public void takeValuesFromRequest(WORequest r, WOContext c) {
58          if (disabled(c))
59              return;
60          NSArray values = r.formValuesForKey(inputName(c));
61          Object val = valueForProperty("value", c.component());
62          if (val == null)
63              val = c.elementID();
64          java.util.Enumeration enumerator = values.objectEnumerator();
65          checked = false;
66          while (enumerator.hasMoreElements()) {
67              Object nextval = enumerator.nextElement();
68              if (nextval.equals(val))
69                  checked = true;
70          }
71          if (associations.objectForKey("value") != null && associations.objectForKey("selection") != null) {
72              if (checked)
73                  setValueForProperty("selection", val, c.component());
74              else if (valueForProperty("selection", c.component()) != null)
75                  setValueForProperty("selection", null, c.component());
76          }
77          if (associations.objectForKey("checked") != null)
78              setValueForProperty("checked", checked ? Boolean.TRUE : Boolean.FALSE, c.component());
79      }
80  
81  }