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 jsx3.util;
017:
018: import org.directwebremoting.ScriptBuffer;
019: import org.directwebremoting.proxy.ScriptProxy;
020: import org.directwebremoting.proxy.io.Context;
021:
022: /**
023: * An Event Dispatcher mixin interface, adds the ability to register event listeners and dispatch events
024: to those listeners.
025:
026: (Deprecated) Classes that implement this mixin must also implement a method getServer(), which returns the server in
027: which to look for JSX ids.
028: * @author Joe Walker [joe at getahead dot org]
029: * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator
030: */
031: public class EventDispatcher extends jsx3.lang.Object {
032: /**
033: * All reverse ajax proxies need context to work from
034: * @param scriptProxy The place we are writing scripts to
035: * @param context The script that got us to where we are now
036: */
037: public EventDispatcher(Context context, String extension,
038: ScriptProxy scriptProxy) {
039: super (context, extension, scriptProxy);
040: }
041:
042: /**
043: *
044: */
045: public static final java.lang.Object SUBJECT_ALL = "*";
046:
047: /**
048: * Subscribes an object or function to a type of event published by this object.
049:
050: As of version 3.4 a string value for objHandler is deprecated.
051: * @param strEventId the event type(s).
052: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
053: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
054: */
055: public void subscribe(Object[] strEventId, String objHandler,
056: String objFunction) {
057: ScriptBuffer script = new ScriptBuffer();
058: script.appendCall(getContextPath() + "subscribe", strEventId,
059: objHandler, objFunction);
060: getScriptProxy().addScript(script);
061: }
062:
063: /**
064: * Subscribes an object or function to a type of event published by this object.
065:
066: As of version 3.4 a string value for objHandler is deprecated.
067: * @param strEventId the event type(s).
068: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
069: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
070: */
071: public void subscribe(String strEventId, String objHandler,
072: String objFunction) {
073: ScriptBuffer script = new ScriptBuffer();
074: script.appendCall(getContextPath() + "subscribe", strEventId,
075: objHandler, objFunction);
076: getScriptProxy().addScript(script);
077: }
078:
079: /**
080: * Subscribes an object or function to a type of event published by this object.
081:
082: As of version 3.4 a string value for objHandler is deprecated.
083: * @param strEventId the event type(s).
084: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
085: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
086: */
087: public void subscribe(String strEventId, String objHandler,
088: org.directwebremoting.proxy.CodeBlock objFunction) {
089: ScriptBuffer script = new ScriptBuffer();
090: script.appendCall(getContextPath() + "subscribe", strEventId,
091: objHandler, objFunction);
092: getScriptProxy().addScript(script);
093: }
094:
095: /**
096: * Subscribes an object or function to a type of event published by this object.
097:
098: As of version 3.4 a string value for objHandler is deprecated.
099: * @param strEventId the event type(s).
100: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
101: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
102: */
103: public void subscribe(String strEventId,
104: org.directwebremoting.proxy.CodeBlock objHandler,
105: org.directwebremoting.proxy.CodeBlock objFunction) {
106: ScriptBuffer script = new ScriptBuffer();
107: script.appendCall(getContextPath() + "subscribe", strEventId,
108: objHandler, objFunction);
109: getScriptProxy().addScript(script);
110: }
111:
112: /**
113: * Subscribes an object or function to a type of event published by this object.
114:
115: As of version 3.4 a string value for objHandler is deprecated.
116: * @param strEventId the event type(s).
117: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
118: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
119: */
120: public void subscribe(String strEventId,
121: jsx3.lang.Object objHandler, String objFunction) {
122: ScriptBuffer script = new ScriptBuffer();
123: script.appendCall(getContextPath() + "subscribe", strEventId,
124: objHandler, objFunction);
125: getScriptProxy().addScript(script);
126: }
127:
128: /**
129: * Subscribes an object or function to a type of event published by this object.
130:
131: As of version 3.4 a string value for objHandler is deprecated.
132: * @param strEventId the event type(s).
133: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
134: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
135: */
136: public void subscribe(Object[] strEventId,
137: org.directwebremoting.proxy.CodeBlock objHandler,
138: String objFunction) {
139: ScriptBuffer script = new ScriptBuffer();
140: script.appendCall(getContextPath() + "subscribe", strEventId,
141: objHandler, objFunction);
142: getScriptProxy().addScript(script);
143: }
144:
145: /**
146: * Subscribes an object or function to a type of event published by this object.
147:
148: As of version 3.4 a string value for objHandler is deprecated.
149: * @param strEventId the event type(s).
150: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
151: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
152: */
153: public void subscribe(Object[] strEventId,
154: jsx3.lang.Object objHandler, String objFunction) {
155: ScriptBuffer script = new ScriptBuffer();
156: script.appendCall(getContextPath() + "subscribe", strEventId,
157: objHandler, objFunction);
158: getScriptProxy().addScript(script);
159: }
160:
161: /**
162: * Subscribes an object or function to a type of event published by this object.
163:
164: As of version 3.4 a string value for objHandler is deprecated.
165: * @param strEventId the event type(s).
166: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
167: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
168: */
169: public void subscribe(String strEventId,
170: jsx3.lang.Object objHandler,
171: org.directwebremoting.proxy.CodeBlock objFunction) {
172: ScriptBuffer script = new ScriptBuffer();
173: script.appendCall(getContextPath() + "subscribe", strEventId,
174: objHandler, objFunction);
175: getScriptProxy().addScript(script);
176: }
177:
178: /**
179: * Subscribes an object or function to a type of event published by this object.
180:
181: As of version 3.4 a string value for objHandler is deprecated.
182: * @param strEventId the event type(s).
183: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
184: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
185: */
186: public void subscribe(Object[] strEventId, String objHandler,
187: org.directwebremoting.proxy.CodeBlock objFunction) {
188: ScriptBuffer script = new ScriptBuffer();
189: script.appendCall(getContextPath() + "subscribe", strEventId,
190: objHandler, objFunction);
191: getScriptProxy().addScript(script);
192: }
193:
194: /**
195: * Subscribes an object or function to a type of event published by this object.
196:
197: As of version 3.4 a string value for objHandler is deprecated.
198: * @param strEventId the event type(s).
199: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
200: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
201: */
202: public void subscribe(Object[] strEventId,
203: jsx3.lang.Object objHandler,
204: org.directwebremoting.proxy.CodeBlock objFunction) {
205: ScriptBuffer script = new ScriptBuffer();
206: script.appendCall(getContextPath() + "subscribe", strEventId,
207: objHandler, objFunction);
208: getScriptProxy().addScript(script);
209: }
210:
211: /**
212: * Subscribes an object or function to a type of event published by this object.
213:
214: As of version 3.4 a string value for objHandler is deprecated.
215: * @param strEventId the event type(s).
216: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
217: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
218: */
219: public void subscribe(String strEventId,
220: org.directwebremoting.proxy.CodeBlock objHandler,
221: String objFunction) {
222: ScriptBuffer script = new ScriptBuffer();
223: script.appendCall(getContextPath() + "subscribe", strEventId,
224: objHandler, objFunction);
225: getScriptProxy().addScript(script);
226: }
227:
228: /**
229: * Subscribes an object or function to a type of event published by this object.
230:
231: As of version 3.4 a string value for objHandler is deprecated.
232: * @param strEventId the event type(s).
233: * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored)
234: * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance
235: */
236: public void subscribe(Object[] strEventId,
237: org.directwebremoting.proxy.CodeBlock objHandler,
238: org.directwebremoting.proxy.CodeBlock objFunction) {
239: ScriptBuffer script = new ScriptBuffer();
240: script.appendCall(getContextPath() + "subscribe", strEventId,
241: objHandler, objFunction);
242: getScriptProxy().addScript(script);
243: }
244:
245: /**
246: * Unsubscribe an object or function from an event published by this object.
247:
248: As of version 3.4 a string value for objHandler is deprecated.
249: * @param strEventId the event type(s).
250: * @param objHandler the value of objHandler passed to subscribe
251: */
252: public void unsubscribe(Object[] strEventId,
253: jsx3.lang.Object objHandler) {
254: ScriptBuffer script = new ScriptBuffer();
255: script.appendCall(getContextPath() + "unsubscribe", strEventId,
256: objHandler);
257: getScriptProxy().addScript(script);
258: }
259:
260: /**
261: * Unsubscribe an object or function from an event published by this object.
262:
263: As of version 3.4 a string value for objHandler is deprecated.
264: * @param strEventId the event type(s).
265: * @param objHandler the value of objHandler passed to subscribe
266: */
267: public void unsubscribe(Object[] strEventId,
268: org.directwebremoting.proxy.CodeBlock objHandler) {
269: ScriptBuffer script = new ScriptBuffer();
270: script.appendCall(getContextPath() + "unsubscribe", strEventId,
271: objHandler);
272: getScriptProxy().addScript(script);
273: }
274:
275: /**
276: * Unsubscribe an object or function from an event published by this object.
277:
278: As of version 3.4 a string value for objHandler is deprecated.
279: * @param strEventId the event type(s).
280: * @param objHandler the value of objHandler passed to subscribe
281: */
282: public void unsubscribe(String strEventId,
283: jsx3.lang.Object objHandler) {
284: ScriptBuffer script = new ScriptBuffer();
285: script.appendCall(getContextPath() + "unsubscribe", strEventId,
286: objHandler);
287: getScriptProxy().addScript(script);
288: }
289:
290: /**
291: * Unsubscribe an object or function from an event published by this object.
292:
293: As of version 3.4 a string value for objHandler is deprecated.
294: * @param strEventId the event type(s).
295: * @param objHandler the value of objHandler passed to subscribe
296: */
297: public void unsubscribe(Object[] strEventId, String objHandler) {
298: ScriptBuffer script = new ScriptBuffer();
299: script.appendCall(getContextPath() + "unsubscribe", strEventId,
300: objHandler);
301: getScriptProxy().addScript(script);
302: }
303:
304: /**
305: * Unsubscribe an object or function from an event published by this object.
306:
307: As of version 3.4 a string value for objHandler is deprecated.
308: * @param strEventId the event type(s).
309: * @param objHandler the value of objHandler passed to subscribe
310: */
311: public void unsubscribe(String strEventId,
312: org.directwebremoting.proxy.CodeBlock objHandler) {
313: ScriptBuffer script = new ScriptBuffer();
314: script.appendCall(getContextPath() + "unsubscribe", strEventId,
315: objHandler);
316: getScriptProxy().addScript(script);
317: }
318:
319: /**
320: * Unsubscribe an object or function from an event published by this object.
321:
322: As of version 3.4 a string value for objHandler is deprecated.
323: * @param strEventId the event type(s).
324: * @param objHandler the value of objHandler passed to subscribe
325: */
326: public void unsubscribe(String strEventId, String objHandler) {
327: ScriptBuffer script = new ScriptBuffer();
328: script.appendCall(getContextPath() + "unsubscribe", strEventId,
329: objHandler);
330: getScriptProxy().addScript(script);
331: }
332:
333: /**
334: * Unsubscribes all subscribed objects to a type of event published by this object.
335: * @param strEventId the event type
336: */
337: public void unsubscribeAll(String strEventId) {
338: ScriptBuffer script = new ScriptBuffer();
339: script.appendCall(getContextPath() + "unsubscribeAll",
340: strEventId);
341: getScriptProxy().addScript(script);
342: }
343:
344: /**
345: * Publishes an event to all subscribed objects.
346: * @param objEvent the event, should have at least a field 'subject' that is the event id, another common field is 'target' (target will default to this instance)
347: * @param callback the number of listeners to which the event was broadcast
348: */
349: @SuppressWarnings("unchecked")
350: public void publish(jsx3.lang.Object objEvent,
351: org.directwebremoting.proxy.Callback<Integer> callback) {
352: ScriptBuffer script = new ScriptBuffer();
353: String callbackPrefix = "";
354:
355: if (callback != null) {
356: callbackPrefix = "var reply = ";
357: }
358:
359: script
360: .appendCall(callbackPrefix + getContextPath()
361: + "publish", objEvent);
362:
363: if (callback != null) {
364: String key = org.directwebremoting.extend.CallbackHelper
365: .saveCallback(callback, Integer.class);
366: script
367: .appendCall("__System.activateCallback", key,
368: "reply");
369: }
370:
371: getScriptProxy().addScript(script);
372: }
373:
374: }
|