001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.proxy;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.List;
021:
022: import org.directwebremoting.ScriptBuffer;
023: import org.directwebremoting.ScriptSession;
024:
025: /**
026: * Class to help people send scripts to collections of browsers.
027: * ScriptProxy also is the base class for the Java implementations of GI, Util
028: * and Script.aculo.us.Effect.
029: * @author Joe Walker [joe at getahead dot ltd dot uk]
030: */
031: public class ScriptProxy {
032: /**
033: * Http thread constructor
034: */
035: public ScriptProxy() {
036: }
037:
038: /**
039: * Http thread constructor
040: * @param scriptSession The browser to alter
041: */
042: public ScriptProxy(ScriptSession scriptSession) {
043: scriptSessions.add(scriptSession);
044: }
045:
046: /**
047: * Non-http thread constructor
048: * @param scriptSessions The browsers to alter
049: */
050: public ScriptProxy(Collection<ScriptSession> scriptSessions) {
051: this .scriptSessions.addAll(scriptSessions);
052: }
053:
054: /**
055: * @param scriptSession The script session to add to the list
056: */
057: public void addScriptSession(ScriptSession scriptSession) {
058: scriptSessions.add(scriptSession);
059: }
060:
061: /**
062: * @param addScriptSessions The script sessions to add to the list
063: */
064: public void addScriptSessions(
065: Collection<ScriptSession> addScriptSessions) {
066: scriptSessions.addAll(addScriptSessions);
067: }
068:
069: /**
070: * Call a named function with no parameters.
071: * @param funcName The name of the function to call
072: */
073: public void addFunctionCall(String funcName) {
074: ScriptBuffer script = new ScriptBuffer();
075: script.appendScript(funcName).appendScript("();");
076: addScript(script);
077: }
078:
079: /**
080: * Call a named function with one parameter.
081: * @param funcName The name of the function to call
082: * @param param1 The first parameter to the above function
083: */
084: public void addFunctionCall(String funcName, Object param1) {
085: ScriptBuffer script = new ScriptBuffer();
086: script.appendScript(funcName).appendScript("(").appendData(
087: param1).appendScript(");");
088: addScript(script);
089: }
090:
091: /**
092: * Call a named function with one parameter.
093: * @param funcName The name of the function to call
094: * @param param1 The first parameter to the above function
095: * @param param2 The second parameter to the above function
096: */
097: public void addFunctionCall(String funcName, Object param1,
098: Object param2) {
099: ScriptBuffer script = new ScriptBuffer();
100: script.appendScript(funcName).appendScript("(").appendData(
101: param1).appendScript(",").appendData(param2)
102: .appendScript(");");
103: addScript(script);
104: }
105:
106: /**
107: * Call a named function with one parameter.
108: * @param funcName The name of the function to call
109: * @param param1 The first parameter to the above function
110: * @param param2 The second parameter to the above function
111: * @param param3 The third parameter to the above function
112: */
113: public void addFunctionCall(String funcName, Object param1,
114: Object param2, Object param3) {
115: ScriptBuffer script = new ScriptBuffer();
116: script.appendScript(funcName).appendScript("(").appendData(
117: param1).appendScript(",").appendData(param2)
118: .appendScript(",").appendData(param3)
119: .appendScript(");");
120: addScript(script);
121: }
122:
123: /**
124: * Call a named function with one parameter.
125: * @param funcName The name of the function to call
126: * @param param1 The first parameter to the above function
127: * @param param2 The second parameter to the above function
128: * @param param3 The third parameter to the above function
129: * @param param4 The fourth parameter to the above function
130: */
131: public void addFunctionCall(String funcName, Object param1,
132: Object param2, Object param3, Object param4) {
133: ScriptBuffer script = new ScriptBuffer();
134: script.appendScript(funcName).appendScript("(").appendData(
135: param1).appendScript(",").appendData(param2)
136: .appendScript(",").appendData(param3).appendScript(",")
137: .appendData(param4).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 fourth parameter to the above function
148: * @param param5 The fifth parameter to the above function
149: */
150: public void addFunctionCall(String funcName, Object param1,
151: Object param2, Object param3, Object param4, Object param5) {
152: ScriptBuffer script = new ScriptBuffer();
153: script.appendScript(funcName).appendScript("(").appendData(
154: param1).appendScript(",").appendData(param2)
155: .appendScript(",").appendData(param3).appendScript(",")
156: .appendData(param4).appendScript(",")
157: .appendData(param5).appendScript(");");
158: addScript(script);
159: }
160:
161: /**
162: * Call a named function with one parameter.
163: * @param funcName The name of the function to call
164: * @param param1 The first parameter to the above function
165: * @param param2 The second parameter to the above function
166: * @param param3 The third parameter to the above function
167: * @param param4 The fourth parameter to the above function
168: * @param param5 The fifth parameter to the above function
169: * @param param6 The sixth parameter to the above function
170: */
171: public void addFunctionCall(String funcName, Object param1,
172: Object param2, Object param3, Object param4, Object param5,
173: Object param6) {
174: ScriptBuffer script = new ScriptBuffer();
175: script.appendScript(funcName).appendScript("(").appendData(
176: param1).appendScript(",").appendData(param2)
177: .appendScript(",").appendData(param3).appendScript(",")
178: .appendData(param4).appendScript(",")
179: .appendData(param5).appendScript(",")
180: .appendData(param6).appendScript(");");
181: addScript(script);
182: }
183:
184: /**
185: * Call a named function with one parameter.
186: * @param funcName The name of the function to call
187: * @param param1 The first parameter to the above function
188: * @param param2 The second parameter to the above function
189: * @param param3 The third parameter to the above function
190: * @param param4 The fourth parameter to the above function
191: * @param param5 The fifth parameter to the above function
192: * @param param6 The sixth parameter to the above function
193: * @param param7 The seventh parameter to the above function
194: */
195: public void addFunctionCall(String funcName, Object param1,
196: Object param2, Object param3, Object param4, Object param5,
197: Object param6, Object param7) {
198: ScriptBuffer script = new ScriptBuffer();
199: script.appendScript(funcName).appendScript("(").appendData(
200: param1).appendScript(",").appendData(param2)
201: .appendScript(",").appendData(param3).appendScript(",")
202: .appendData(param4).appendScript(",")
203: .appendData(param5).appendScript(",")
204: .appendData(param6).appendScript(",")
205: .appendData(param7).appendScript(");");
206: addScript(script);
207: }
208:
209: /**
210: * Utility to add the given script to all known browsers.
211: * @param script The Javascript to send to the browsers
212: */
213: public void addScript(ScriptBuffer script) {
214: for (ScriptSession scriptSession : scriptSessions) {
215: scriptSession.addScript(script);
216: }
217: }
218:
219: /**
220: * The browsers that we affect.
221: */
222: private final List<ScriptSession> scriptSessions = new ArrayList<ScriptSession>();
223: }
|