KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > flow > javascript > ScriptableWidget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
33  * @version $Id: ScriptableWidget.java 54079 2004-10-08 13:30:28Z vgritsenko $
34  *
35  */

36 public class ScriptableWidget extends ScriptableObject {
37
38     Widget delegate;
39
40     public String JavaDoc getClassName() {
41         return "Widget";
42     }
43
44     public ScriptableWidget() {
45     }
46
47     public ScriptableWidget(Object JavaDoc widget) {
48         this.delegate = (Widget)unwrap(widget);
49     }
50
51     private Object JavaDoc unwrap(Object JavaDoc 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 JavaDoc 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 JavaDoc[] values = (Object JavaDoc[])delegate.getValue();
93             return index >= 0 && index < values.length;
94         }
95         return super.has(index, start);
96     }
97
98     public Object JavaDoc get(String JavaDoc id, Scriptable start) {
99         if (delegate instanceof Repeater) {
100             if (id.equals("length")) {
101                 Repeater repeater = (Repeater)delegate;
102                 return new Integer JavaDoc(repeater.getSize());
103             }
104         } else if (delegate instanceof MultiValueField) {
105             if (id.equals("length")) {
106                 Object JavaDoc[] values = (Object JavaDoc[])delegate.getValue();
107                 return new Integer JavaDoc(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 JavaDoc 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 JavaDoc[] values = (Object JavaDoc[])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 JavaDoc 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 JavaDoc[] values = (Object JavaDoc[])field.getValue();
159             if (values != null && values.length > index) {
160                 Object JavaDoc[] newValues = new Object JavaDoc[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 JavaDoc id, Scriptable start, Object JavaDoc 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 JavaDoc) {
198                     // make woody accept a JS Number
199
Class JavaDoc typeClass =
200                         field.getFieldDefinition().getDatatype().getTypeClass();
201                     if (typeClass == long.class || typeClass == Long JavaDoc.class) {
202                         value = new Long JavaDoc(((Number JavaDoc)value).longValue());
203                     } else if (typeClass == int.class || typeClass == Integer JavaDoc.class) {
204                         value = new Integer JavaDoc(((Number JavaDoc)value).intValue());
205                     } else if (typeClass == float.class || typeClass == Float JavaDoc.class) {
206                         value = new Float JavaDoc(((Number JavaDoc)value).floatValue());
207                     } else if (typeClass == short.class || typeClass == Short JavaDoc.class) {
208                         value = new Short JavaDoc(((Number JavaDoc)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 JavaDoc length = getProperty(arr, "length");
226                     int len = ((Number JavaDoc)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 JavaDoc elemValue = getProperty(arr, i);
232                         if (elemValue instanceof Scriptable) {
233                             Scriptable s = (Scriptable)elemValue;
234                             Object JavaDoc[] ids = s.getIds();
235                             ScriptableWidget wid = wrap(repeater.getRow(i));
236                             for (int j = 0; j < ids.length; j++) {
237                                 String JavaDoc 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 JavaDoc[] values = null;
247                 if (value instanceof NativeArray) {
248                     NativeArray arr = (NativeArray)value;
249                     Object JavaDoc length = getProperty(arr, "length");
250                     int len = ((Number JavaDoc)length).intValue();
251                     values = new Object JavaDoc[len];
252                     for (int i = 0; i < len; i++) {
253                         Object JavaDoc elemValue = getProperty(arr, i);
254                         values[i] = unwrap(elemValue);
255                     }
256                 } else if (value instanceof Object JavaDoc[]) {
257                     values = (Object JavaDoc[])value;
258                 }
259                 field.setValues(values);
260             } else {
261                 if (value instanceof Scriptable) {
262                     Scriptable s = (Scriptable)value;
263                     Object JavaDoc[] ids = s.getIds();
264                     ScriptableWidget wid = wrap(sub);
265                     for (int j = 0; j < ids.length; j++) {
266                         String JavaDoc 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 JavaDoc 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 JavaDoc 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
Free Books   Free Magazines  
Popular Tags