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.NSKeyValueCodingAdditions;
7   
8   public class WOPopUpButton extends WOInput {
9   
10      public WOPopUpButton() {
11          super();
12      }
13  
14      public WOPopUpButton(String aName, NSDictionary assocs, WOElement template) {
15          super(aName, assocs, template);
16      }
17  
18      protected String inputType() {
19          return "SELECT";
20      }
21  
22      protected int inputSize() {
23          return 1;
24      }
25  
26      protected NSArray list(WOContext c) {
27          NSArray l = (NSArray)valueForProperty("list", c.component());
28          if (l == null)
29              l = NSArray.EmptyArray;
30          return l;
31      }
32  
33      protected void setItem(Object v, WOContext c) {
34          if (associations.objectForKey("item") == null)
35              return;
36          setValueForProperty("item", v, c.component());
37      }
38      protected Object item(WOContext c) {
39          return valueForProperty("item", c.component());
40      }
41  
42      protected void setSelection(Object v, WOContext c) {
43          if (associations.objectForKey("selection") == null)
44              return;
45          setValueForProperty("selection", v, c.component());
46      }
47      protected Object selection(WOContext c) {
48          return valueForProperty("selection", c.component());
49      }
50  
51      public Object value(WOContext c) {
52          return null;
53      }
54  
55      public void appendToResponse(WOResponse r, WOContext c) {
56          r.appendContentString("<SELECT NAME=\"");
57          r.appendContentString(inputName(c));
58          r.appendContentString("\" SIZE=");
59          r.appendContentString(Integer.toString(inputSize()));
60          r.appendContentString(">");
61          java.util.Enumeration numerador = list(c).objectEnumerator();
62          String displayKey = stringForProperty("displayString", c.component());
63          String valueKey = stringForProperty("value", c.component());
64          Object sel = selection(c);
65          if (sel == null)
66              sel = item(c);
67          int pos = 0;
68          while (numerador.hasMoreElements()) {
69              Object item = numerador.nextElement();
70              setItem(item, c);
71              r.appendContentString("<OPTION ");
72              //Append the "SELECTED" attribute if it's the selected item
73              if (sel != null && item.equals(sel))
74                  r.appendContentString("SELECTED ");
75              r.appendContentString("VALUE=\"");
76              //Append the value
77              if (valueKey != null && item instanceof NSKeyValueCodingAdditions) {
78                  Object val = ((NSKeyValueCodingAdditions)item).valueForKeyPath(valueKey);
79                  if (val == null)
80                      val = "null";
81                  r.appendContentString(val.toString());
82              } else
83                  r.appendContentString(Integer.toString(pos));
84              r.appendContentString("\">");
85              //Append display string
86              if (displayKey != null && item instanceof NSKeyValueCodingAdditions) {
87                  Object ds = ((NSKeyValueCodingAdditions)item).valueForKeyPath(displayKey);
88                  if (ds == null)
89                      ds = "";
90                  r.appendContentString(ds.toString());
91              } else
92                  r.appendContentString(item.toString());
93              r.appendContentString("\n");
94              pos++;
95          }
96          r.appendContentString("</SELECT>");
97      }
98  
99      protected void select(Object v, WOContext c) {
100         if (associations.objectForKey("selection") != null) {
101             setSelection(v, c);
102             return;
103         }
104         if (associations.objectForKey("item") != null) {
105             setItem(v,c);
106         }
107     }
108     public void takeValuesFromRequest(WORequest r, WOContext c) {
109         Object val = r.formValueForKey(inputName(c));
110         if (val == null)
111             return;
112         NSArray list = list(c);
113         String valueKey = stringForProperty("value", c.component());
114         //If no value binding, just get the index
115         if (valueKey == null) {
116             int pos = Integer.parseInt(val.toString());
117             val = list.objectAtIndex(pos);
118             select(val, c);
119             return;
120         }
121         //If value binding is present, lookup the value
122         java.util.Enumeration numerador = list.objectEnumerator();
123         while (numerador.hasMoreElements()) {
124             Object o = numerador.nextElement();
125             if (o instanceof NSKeyValueCodingAdditions) {
126                 Object x = ((NSKeyValueCodingAdditions)o).valueForKeyPath(valueKey);
127                 if (x != null && x.equals(val)) {
128                     select(o, c);
129                     return;
130                 }
131             }
132         }
133     }
134 
135 }