1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.struts.flow.core.javascript.fom;
17
18 import java.util.Iterator;
19 import java.util.List;
20
21 import org.apache.struts.flow.core.Factory;
22
23 import org.apache.struts.flow.core.ContinuationsManager;
24 import org.apache.struts.flow.core.WebContinuation;
25 import org.mozilla.javascript.Context;
26 import org.mozilla.javascript.Function;
27 import org.mozilla.javascript.NativeArray;
28 import org.mozilla.javascript.Scriptable;
29 import org.mozilla.javascript.ScriptableObject;
30 import org.mozilla.javascript.Undefined;
31 import org.mozilla.javascript.Wrapper;
32 import org.mozilla.javascript.continuations.Continuation;
33
34 /***
35 *
36 * @version CVS $Id: FOM_WebContinuation.java 292158 2005-09-28 10:24:51Z sylvain $
37 */
38 public class FOM_WebContinuation extends ScriptableObject {
39
40 WebContinuation wk;
41
42
43 static class UserObject {
44 boolean isBookmark;
45 PageLocalScopeImpl pageLocal;
46 }
47
48 static private boolean isBookmark(WebContinuation wk) {
49 UserObject userObj = (UserObject)wk.getUserObject();
50 if (userObj == null) {
51 return false;
52 }
53 return userObj.isBookmark;
54 }
55
56 public FOM_WebContinuation() {
57 this(null);
58 }
59
60
61 public FOM_WebContinuation(WebContinuation wk) {
62 this.wk = wk;
63 }
64
65
66
67
68 public static Object jsConstructor(Context cx, Object[] args,
69 Function ctorObj,
70 boolean inNewExpr)
71 throws Exception {
72 FOM_WebContinuation result = null;
73 if (args.length < 1) {
74
75 }
76 Continuation c = (Continuation)unwrap(args[0]);
77 FOM_WebContinuation parent = null;
78 if (args.length > 1) {
79 parent = (FOM_WebContinuation)args[1];
80 }
81 int timeToLive = 0;
82 if (args.length > 2) {
83 timeToLive =
84 (int)org.mozilla.javascript.Context.toNumber(args[2]);
85 }
86 WebContinuation wk;
87 Scriptable scope = getTopLevelScope(c);
88 FOM_Flow cocoon = (FOM_Flow)getProperty(scope, "flow");
89 ContinuationsManager contMgr = Factory.getContinuationsManager();
90 wk = contMgr.createWebContinuation(c,
91 (parent == null ? null : parent.getWebContinuation()),
92 timeToLive,
93 cocoon.getInterpreterId(),
94 null,
95 cocoon.getWebContext());
96 result = new FOM_WebContinuation(wk);
97 result.setParentScope(getTopLevelScope(scope));
98 result.setPrototype(getClassPrototype(scope, result.getClassName()));
99 return result;
100 }
101
102 public String getClassName() {
103 return "FOM_WebContinuation";
104 }
105
106 public Object jsFunction_getAttribute(String name) {
107 return org.mozilla.javascript.Context.javaToJS(
108 wk.getAttribute(name),
109 getParentScope());
110 }
111
112 public void jsFunction_setAttribute(String name, Object value) {
113 wk.setAttribute(name, unwrap(value));
114 }
115
116 public void jsFunction_removeAttribute(String name) {
117 wk.removeAttribute(name);
118 }
119
120 public Object jsFunction_getAttributeNames() {
121 return org.mozilla.javascript.Context.javaToJS(
122 wk.getAttributeNames(),
123 getParentScope());
124 }
125
126 public String jsGet_id() {
127 return wk.getId();
128 }
129
130
131 public Continuation jsGet_continuation() {
132 return (Continuation)wk.getContinuation();
133 }
134
135 public FOM_WebContinuation jsFunction_getParent() {
136 WebContinuation parent = wk.getParentContinuation();
137 if (parent == null) {
138 return null;
139 }
140
141 FOM_WebContinuation pwk = new FOM_WebContinuation(parent);
142 pwk.setParentScope(getParentScope());
143 pwk.setPrototype(getClassPrototype(getParentScope(),
144 pwk.getClassName()));
145 return pwk;
146 }
147
148 public NativeArray jsFunction_getChildren() throws Exception {
149 List list = wk.getChildren();
150 NativeArray arr =
151 (NativeArray)org.mozilla.javascript.Context.getCurrentContext().newObject(getParentScope(),
152 "Array",
153 new Object[]{new Integer(list.size())});
154 Iterator iter = list.iterator();
155 for (int i = 0; iter.hasNext(); i++) {
156 WebContinuation child = (WebContinuation)iter.next();
157 FOM_WebContinuation cwk = new FOM_WebContinuation(child);
158 cwk.setParentScope(getParentScope());
159 cwk.setPrototype(getClassPrototype(getParentScope(),
160 cwk.getClassName()));
161 arr.put(i, arr, cwk);
162 }
163 return arr;
164 }
165
166 public void jsFunction_invalidate() throws Exception {
167 ContinuationsManager contMgr = null;
168 FOM_Flow cocoon =
169 (FOM_Flow)getProperty(getTopLevelScope(this), "flow");
170 contMgr = Factory.getContinuationsManager();
171 contMgr.invalidateWebContinuation(wk, cocoon.getWebContext());
172 }
173
174 public void jsFunction_display() {
175 wk.display();
176 }
177
178 public WebContinuation getWebContinuation() {
179 return wk;
180 }
181
182 private static Object unwrap(Object obj) {
183 if (obj instanceof Wrapper) {
184 obj = ((Wrapper)obj).unwrap();
185 } else if (obj == Undefined.instance) {
186 obj = null;
187 }
188 return obj;
189 }
190
191 PageLocalScopeImpl getPageLocal() {
192 UserObject userObj = (UserObject)wk.getUserObject();
193 if (userObj == null) return null;
194 return userObj.pageLocal;
195 }
196
197 void setPageLocal(PageLocalScopeImpl pageLocal) {
198 UserObject userObj = (UserObject)wk.getUserObject();
199 if (userObj == null) {
200 userObj = new UserObject();
201 wk.setUserObject(userObj);
202 }
203 userObj.pageLocal = pageLocal;
204 }
205
206 public void jsFunction_setBookmark(boolean value) {
207 UserObject userObj = (UserObject)wk.getUserObject();
208 if (userObj == null) {
209 userObj = new UserObject();
210 wk.setUserObject(userObj);
211 }
212 userObj.isBookmark = value;
213 }
214
215 public boolean jsGet_bookmark() {
216 return isBookmark(wk);
217 }
218
219 public boolean jsFunction_isBookmark() {
220 return isBookmark(wk);
221 }
222
223 public FOM_WebContinuation jsGet_previousBookmark() {
224 WebContinuation c = wk.getParentContinuation();
225 if (c == null) {
226 return null;
227 }
228
229
230
231
232 if (!isBookmark(wk) && isBookmark(c)) {
233 c = c.getParentContinuation();
234 }
235 while (c != null && !isBookmark(c)) {
236 c = c.getParentContinuation();
237 }
238 if (c == null) {
239 return null;
240 }
241
242 FOM_WebContinuation pwk = new FOM_WebContinuation(c);
243 pwk.setParentScope(getParentScope());
244 pwk.setPrototype(getClassPrototype(getParentScope(), pwk.getClassName()));
245 return pwk;
246 }
247
248 /***
249 * Return text representation of the WebContinuation.
250 */
251 public String toString() {
252 return "WC" + wk.getId();
253 }
254 }