KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > proxy > ScriptProxy


1 /*
2  * Copyright 2005 Joe Walker
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 package org.directwebremoting.proxy;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.directwebremoting.ScriptBuffer;
24 import org.directwebremoting.ScriptSession;
25
26 /**
27  * Class to help people send scripts to collections of browsers.
28  * ScriptProxy also is the base class for the Java implementations of Util
29  * and Scriptaculous.Effect.
30  * @author Joe Walker [joe at getahead dot ltd dot uk]
31  */

32 public class ScriptProxy
33 {
34     /**
35      * Http thread constructor
36      */

37     public ScriptProxy()
38     {
39     }
40
41     /**
42      * Http thread constructor
43      * @param scriptSession The browser to alter
44      */

45     public ScriptProxy(ScriptSession scriptSession)
46     {
47         scriptSessions.add(scriptSession);
48     }
49
50     /**
51      * Non-http thread constructor
52      * @param scriptSessions The browsers to alter
53      */

54     public ScriptProxy(Collection JavaDoc scriptSessions)
55     {
56         this.scriptSessions.addAll(scriptSessions);
57     }
58
59     /**
60      * @param scriptSession
61      */

62     public void addScriptSession(ScriptSession scriptSession)
63     {
64         scriptSessions.add(scriptSession);
65     }
66
67     /**
68      * @param addScriptSessions
69      */

70     public void addScriptSessions(Collection JavaDoc addScriptSessions)
71     {
72         scriptSessions.addAll(addScriptSessions);
73     }
74
75     /**
76      * Call a named function with no parameters.
77      * @param funcName The name of the function to call
78      */

79     public void addFunctionCall(String JavaDoc funcName)
80     {
81         ScriptBuffer script = new ScriptBuffer();
82         script.appendScript(funcName)
83               .appendScript("();");
84         addScript(script);
85     }
86
87     /**
88      * Call a named function with one parameter.
89      * @param funcName The name of the function to call
90      * @param param1 The first parameter to the above function
91      */

92     public void addFunctionCall(String JavaDoc funcName, Object JavaDoc param1)
93     {
94         ScriptBuffer script = new ScriptBuffer();
95         script.appendScript(funcName)
96               .appendScript("(")
97               .appendData(param1)
98               .appendScript(");");
99         addScript(script);
100     }
101
102     /**
103      * Call a named function with one parameter.
104      * @param funcName The name of the function to call
105      * @param param1 The first parameter to the above function
106      * @param param2 The second parameter to the above function
107      */

108     public void addFunctionCall(String JavaDoc funcName, Object JavaDoc param1, Object JavaDoc param2)
109     {
110         ScriptBuffer script = new ScriptBuffer();
111         script.appendScript(funcName)
112               .appendScript("(")
113               .appendData(param1)
114               .appendScript(",")
115               .appendData(param2)
116               .appendScript(");");
117         addScript(script);
118     }
119
120     /**
121      * Call a named function with one parameter.
122      * @param funcName The name of the function to call
123      * @param param1 The first parameter to the above function
124      * @param param2 The second parameter to the above function
125      * @param param3 The third parameter to the above function
126      */

127     public void addFunctionCall(String JavaDoc funcName, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3)
128     {
129         ScriptBuffer script = new ScriptBuffer();
130         script.appendScript(funcName)
131               .appendScript("(")
132               .appendData(param1)
133               .appendScript(",")
134               .appendData(param2)
135               .appendScript(",")
136               .appendData(param3)
137               .appendScript(");");
138         addScript(script);
139     }
140
141     /**
142      * Call a named function with one parameter.
143      * @param funcName The name of the function to call
144      * @param param1 The first parameter to the above function
145      * @param param2 The second parameter to the above function
146      * @param param3 The third parameter to the above function
147      * @param param4 The fouth parameter to the above function
148      */

149     public void addFunctionCall(String JavaDoc funcName, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4)
150     {
151         ScriptBuffer script = new ScriptBuffer();
152         script.appendScript(funcName)
153               .appendScript("(")
154               .appendData(param1)
155               .appendScript(",")
156               .appendData(param2)
157               .appendScript(",")
158               .appendData(param3)
159               .appendScript(",")
160               .appendData(param4)
161               .appendScript(");");
162         addScript(script);
163     }
164
165     /**
166      * Call a named function with one parameter.
167      * @param funcName The name of the function to call
168      * @param param1 The first parameter to the above function
169      * @param param2 The second parameter to the above function
170      * @param param3 The third parameter to the above function
171      * @param param4 The fourth parameter to the above function
172      * @param param5 The fifth parameter to the above function
173      */

174     public void addFunctionCall(String JavaDoc funcName, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4, Object JavaDoc param5)
175     {
176         ScriptBuffer script = new ScriptBuffer();
177         script.appendScript(funcName)
178               .appendScript("(")
179               .appendData(param1)
180               .appendScript(",")
181               .appendData(param2)
182               .appendScript(",")
183               .appendData(param3)
184               .appendScript(",")
185               .appendData(param4)
186               .appendScript(",")
187               .appendData(param5)
188               .appendScript(");");
189         addScript(script);
190     }
191
192     /**
193      * Utility to add the given script to all known browsers.
194      * @param script The Javascript to send to the browsers
195      */

196     public void addScript(ScriptBuffer script)
197     {
198         for (Iterator JavaDoc it = scriptSessions.iterator(); it.hasNext();)
199         {
200             ScriptSession scriptSession = (ScriptSession) it.next();
201             scriptSession.addScript(script);
202         }
203     }
204
205     /**
206      * The browsers that we affect.
207      */

208     private final List JavaDoc scriptSessions = new ArrayList JavaDoc();
209 }
210
Popular Tags