1 16 17 package org.apache.cocoon.woody.flow.javascript; 18 import org.apache.cocoon.woody.formmodel.AggregateField; 19 import org.apache.cocoon.woody.formmodel.BooleanField; 20 import org.apache.cocoon.woody.formmodel.Field; 21 import org.apache.cocoon.woody.formmodel.MultiValueField; 22 import org.apache.cocoon.woody.formmodel.Output; 23 import org.apache.cocoon.woody.formmodel.Repeater; 24 import org.apache.cocoon.woody.formmodel.Widget; 25 import org.mozilla.javascript.Context; 26 import org.mozilla.javascript.NativeArray; 27 import org.mozilla.javascript.Scriptable; 28 import org.mozilla.javascript.ScriptableObject; 29 import org.mozilla.javascript.Undefined; 30 import org.mozilla.javascript.Wrapper; 31 32 36 public class ScriptableWidget extends ScriptableObject { 37 38 Widget delegate; 39 40 public String getClassName() { 41 return "Widget"; 42 } 43 44 public ScriptableWidget() { 45 } 46 47 public ScriptableWidget(Object widget) { 48 this.delegate = (Widget)unwrap(widget); 49 } 50 51 private Object unwrap(Object obj) { 52 if (obj == Undefined.instance) { 53 return null; 54 } 55 if (obj instanceof Wrapper) { 56 return ((Wrapper)obj).unwrap(); 57 } 58 return obj; 59 } 60 61 private ScriptableWidget wrap(Widget w) { 62 ScriptableWidget result = new ScriptableWidget(w); 63 result.setPrototype(getClassPrototype(this, "Widget")); 64 result.setParentScope(getParentScope()); 65 return result; 66 } 67 68 public boolean has(String id, Scriptable start) { 69 if (delegate instanceof Repeater) { 70 if (id.equals("length")) { 71 return true; 72 } 73 } else if (delegate instanceof MultiValueField) { 74 if (id.equals("length")) { 75 return true; 76 } 77 } else if (delegate != null) { 78 Widget sub = delegate.getWidget(id); 79 if (sub != null) { 80 return true; 81 } 82 } 83 return super.has(id, start); 84 } 85 86 public boolean has(int index, Scriptable start) { 87 if (delegate instanceof Repeater) { 88 Repeater repeater = (Repeater)delegate; 89 return index >= 0 && index < repeater.getSize(); 90 } 91 if (delegate instanceof MultiValueField) { 92 Object [] values = (Object [])delegate.getValue(); 93 return index >= 0 && index < values.length; 94 } 95 return super.has(index, start); 96 } 97 98 public Object get(String id, Scriptable start) { 99 if (delegate instanceof Repeater) { 100 if (id.equals("length")) { 101 Repeater repeater = (Repeater)delegate; 102 return new Integer (repeater.getSize()); 103 } 104 } else if (delegate instanceof MultiValueField) { 105 if (id.equals("length")) { 106 Object [] values = (Object [])delegate.getValue(); 107 return new Integer (values.length); 108 } 109 } else if (delegate != null) { 110 Widget sub = delegate.getWidget(id); 111 if (sub != null) { 112 if (sub instanceof Field || 113 sub instanceof BooleanField || 114 sub instanceof AggregateField || 115 sub instanceof Output) { 116 return sub.getValue(); 117 } 118 return wrap(sub); 119 } 120 } 121 return super.get(id, start); 122 } 123 124 public Object get(int index, Scriptable start) { 125 if (delegate instanceof Repeater) { 126 Repeater repeater = (Repeater)delegate; 127 if (index >= 0) { 128 while (index >= repeater.getSize()) { 129 repeater.addRow(); 130 } 131 return wrap(repeater.getRow(index)); 132 } 133 } 134 if (delegate instanceof MultiValueField) { 135 Object [] values = (Object [])delegate.getValue(); 136 if (index >= 0 && index < values.length) { 137 return values[index]; 138 } else { 139 return NOT_FOUND; 140 } 141 } 142 return super.get(index, start); 143 } 144 145 public void delete(String id) { 146 super.delete(id); 147 } 148 149 public void delete(int index) { 150 if (delegate instanceof Repeater) { 151 Repeater repeater = (Repeater)delegate; 152 if (index >= 0 && index < repeater.getSize()) { 153 repeater.removeRow(index); 154 return; 155 } 156 } else if (delegate instanceof MultiValueField) { 157 MultiValueField field = (MultiValueField)delegate; 158 Object [] values = (Object [])field.getValue(); 159 if (values != null && values.length > index) { 160 Object [] newValues = new Object [values.length-1]; 161 int i; 162 for (i = 0; i < index; i++) { 163 newValues[i] = values[i]; 164 } 165 i++; 166 for (;i < values.length;i++) { 167 newValues[i-1] = values[i]; 168 } 169 field.setValues(newValues); 170 } 171 return; 172 } 173 super.delete(index); 174 } 175 176 public void put(String id, Scriptable start, Object value) { 177 if (delegate instanceof Repeater) { 178 if (id.equals("length")) { 179 int len = (int)Context.toNumber(value); 180 Repeater repeater = (Repeater)delegate; 181 int size = repeater.getSize(); 182 if (size > len) { 183 while (repeater.getSize() > len) { 184 repeater.removeRow(repeater.getSize() -1); 185 } 186 } else { 187 for (int i = size; i < len; ++i) { 188 repeater.addRow(); 189 } 190 } 191 } 192 } else if (delegate != null) { 193 Widget sub = delegate.getWidget(id); 194 if (sub instanceof Field) { 195 Field field = (Field)sub; 196 value = unwrap(value); 197 if (value instanceof Double ) { 198 Class typeClass = 200 field.getFieldDefinition().getDatatype().getTypeClass(); 201 if (typeClass == long.class || typeClass == Long .class) { 202 value = new Long (((Number )value).longValue()); 203 } else if (typeClass == int.class || typeClass == Integer .class) { 204 value = new Integer (((Number )value).intValue()); 205 } else if (typeClass == float.class || typeClass == Float .class) { 206 value = new Float (((Number )value).floatValue()); 207 } else if (typeClass == short.class || typeClass == Short .class) { 208 value = new Short (((Number )value).shortValue()); 209 } 210 } 211 field.setValue(value); 212 return; 213 } else if (sub instanceof BooleanField) { 214 BooleanField field = (BooleanField)sub; 215 value = unwrap(value); 216 field.setValue(value); 217 } else if (sub instanceof Output) { 218 Output field = (Output)sub; 219 value = unwrap(value); 220 field.setValue(value); 221 } else if (sub instanceof Repeater) { 222 Repeater repeater = (Repeater)sub; 223 if (value instanceof NativeArray) { 224 NativeArray arr = (NativeArray)value; 225 Object length = getProperty(arr, "length"); 226 int len = ((Number )length).intValue(); 227 for (int i = repeater.getSize(); i >= len; --i) { 228 repeater.removeRow(i); 229 } 230 for (int i = 0; i < len; i++) { 231 Object elemValue = getProperty(arr, i); 232 if (elemValue instanceof Scriptable) { 233 Scriptable s = (Scriptable)elemValue; 234 Object [] ids = s.getIds(); 235 ScriptableWidget wid = wrap(repeater.getRow(i)); 236 for (int j = 0; j < ids.length; j++) { 237 String idStr = ids[j].toString(); 238 wid.put(idStr, wid, getProperty(s, idStr)); 239 } 240 } 241 } 242 return; 243 } 244 } else if (sub instanceof MultiValueField) { 245 MultiValueField field = (MultiValueField)sub; 246 Object [] values = null; 247 if (value instanceof NativeArray) { 248 NativeArray arr = (NativeArray)value; 249 Object length = getProperty(arr, "length"); 250 int len = ((Number )length).intValue(); 251 values = new Object [len]; 252 for (int i = 0; i < len; i++) { 253 Object elemValue = getProperty(arr, i); 254 values[i] = unwrap(elemValue); 255 } 256 } else if (value instanceof Object []) { 257 values = (Object [])value; 258 } 259 field.setValues(values); 260 } else { 261 if (value instanceof Scriptable) { 262 Scriptable s = (Scriptable)value; 263 Object [] ids = s.getIds(); 264 ScriptableWidget wid = wrap(sub); 265 for (int j = 0; j < ids.length; j++) { 266 String idStr = ids[j].toString(); 267 wid.put(idStr, wid, getProperty(s, idStr)); 268 } 269 return; 270 } 271 } 272 } 273 super.put(id, start, value); 274 } 275 276 public String jsGet_id() { 277 return delegate.getId(); 278 } 279 280 public Scriptable jsGet_parent() { 281 if (delegate != null) { 282 return wrap(delegate.getParent()); 283 } 284 return Undefined.instance; 285 } 286 287 public boolean jsFunction_equals(Object other) { 288 if (other instanceof ScriptableWidget) { 289 ScriptableWidget otherWidget = (ScriptableWidget)other; 290 return delegate.equals(otherWidget.delegate); 291 } 292 return false; 293 } 294 295 public void jsFunction_remove(int index) { 296 delete(index); 297 } 298 299 } 300
| Popular Tags
|