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.gui;
017:
018: import org.directwebremoting.ScriptBuffer;
019: import org.directwebremoting.proxy.ScriptProxy;
020: import org.directwebremoting.proxy.io.Context;
021:
022: /**
023: * Encapsulates a keydown event listener that is invoked by a certain combination of keys pressed simultaneously.
024: * @author Joe Walker [joe at getahead dot org]
025: * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator
026: */
027: public class HotKey extends jsx3.lang.Object {
028: /**
029: * All reverse ajax proxies need context to work from
030: * @param scriptProxy The place we are writing scripts to
031: * @param context The script that got us to where we are now
032: */
033: public HotKey(Context context, String extension,
034: ScriptProxy scriptProxy) {
035: super (context, extension, scriptProxy);
036: }
037:
038: /**
039: * Event type published just after a hot key is invoked.
040: */
041: public static final String WAS_INVOKED = "invoked";
042:
043: /**
044: *
045: * @param strKey
046: * @param fctCallback
047: */
048: @SuppressWarnings("unchecked")
049: public jsx3.gui.HotKey valueOf(String strKey,
050: org.directwebremoting.proxy.CodeBlock fctCallback) {
051: String extension = "valueOf(\"" + strKey + "\", \""
052: + fctCallback + "\").";
053: try {
054: java.lang.reflect.Constructor<jsx3.gui.HotKey> ctor = jsx3.gui.HotKey.class
055: .getConstructor(Context.class, String.class,
056: ScriptProxy.class);
057: return ctor.newInstance(this , extension, getScriptProxy());
058: } catch (Exception ex) {
059: throw new IllegalArgumentException("Unsupported type: "
060: + jsx3.gui.HotKey.class.getName());
061: }
062: }
063:
064: /**
065: * Returns the keycode that this hot key responds to.
066: */
067: @SuppressWarnings("unchecked")
068: public void getKeyCode(
069: org.directwebremoting.proxy.Callback<Integer> callback) {
070: ScriptBuffer script = new ScriptBuffer();
071: String callbackPrefix = "";
072:
073: if (callback != null) {
074: callbackPrefix = "var reply = ";
075: }
076:
077: script.appendCall(callbackPrefix + getContextPath()
078: + "getKeyCode");
079:
080: if (callback != null) {
081: String key = org.directwebremoting.extend.CallbackHelper
082: .saveCallback(callback, Integer.class);
083: script
084: .appendCall("__System.activateCallback", key,
085: "reply");
086: }
087:
088: getScriptProxy().addScript(script);
089: }
090:
091: /**
092: * Returns whether this hot key should be invoked for the keydown event objEvent.
093: * @param objEvent
094: */
095: @SuppressWarnings("unchecked")
096: public void isMatch(jsx3.gui.Event objEvent,
097: org.directwebremoting.proxy.Callback<Boolean> callback) {
098: ScriptBuffer script = new ScriptBuffer();
099: String callbackPrefix = "";
100:
101: if (callback != null) {
102: callbackPrefix = "var reply = ";
103: }
104:
105: script
106: .appendCall(callbackPrefix + getContextPath()
107: + "isMatch", objEvent);
108:
109: if (callback != null) {
110: String key = org.directwebremoting.extend.CallbackHelper
111: .saveCallback(callback, Boolean.class);
112: script
113: .appendCall("__System.activateCallback", key,
114: "reply");
115: }
116:
117: getScriptProxy().addScript(script);
118: }
119:
120: /**
121: * Invokes this hot key by executing its callback function. This hot key also publishes a WAS_INVOKED
122: event through the event dispatcher interface.
123: * @param objThis
124: * @param arrArgs
125: * @return this method returns whatever value was returned by the hot key callback function.
126: */
127: @SuppressWarnings("unchecked")
128: public jsx3.lang.Object invoke(jsx3.lang.Object objThis,
129: Object[] arrArgs) {
130: String extension = "invoke(\"" + objThis + "\", \"" + arrArgs
131: + "\").";
132: try {
133: java.lang.reflect.Constructor<jsx3.lang.Object> ctor = jsx3.lang.Object.class
134: .getConstructor(Context.class, String.class,
135: ScriptProxy.class);
136: return ctor.newInstance(this , extension, getScriptProxy());
137: } catch (Exception ex) {
138: throw new IllegalArgumentException("Unsupported type: "
139: + jsx3.lang.Object.class.getName());
140: }
141: }
142:
143: /**
144: * Invokes this hot key by executing its callback function. This hot key also publishes a WAS_INVOKED
145: event through the event dispatcher interface.
146: * @param objThis
147: * @param arrArgs
148: * @param returnType The expected return type
149: * @return this method returns whatever value was returned by the hot key callback function.
150: */
151: @SuppressWarnings("unchecked")
152: public <T> T invoke(jsx3.lang.Object objThis, Object[] arrArgs,
153: Class<T> returnType) {
154: String extension = "invoke(\"" + objThis + "\", \"" + arrArgs
155: + "\").";
156: try {
157: java.lang.reflect.Constructor<T> ctor = returnType
158: .getConstructor(Context.class, String.class,
159: ScriptProxy.class);
160: return ctor.newInstance(this , extension, getScriptProxy());
161: } catch (Exception ex) {
162: throw new IllegalArgumentException(
163: "Unsupported return type: " + returnType.getName());
164: }
165: }
166:
167: /**
168: * Returns whether this hot key is enabled.
169: */
170: @SuppressWarnings("unchecked")
171: public void isEnabled(
172: org.directwebremoting.proxy.Callback<Boolean> callback) {
173: ScriptBuffer script = new ScriptBuffer();
174: String callbackPrefix = "";
175:
176: if (callback != null) {
177: callbackPrefix = "var reply = ";
178: }
179:
180: script.appendCall(callbackPrefix + getContextPath()
181: + "isEnabled");
182:
183: if (callback != null) {
184: String key = org.directwebremoting.extend.CallbackHelper
185: .saveCallback(callback, Boolean.class);
186: script
187: .appendCall("__System.activateCallback", key,
188: "reply");
189: }
190:
191: getScriptProxy().addScript(script);
192: }
193:
194: /**
195: * Sets whether this hot key is enabled. Hot keys may be turned off temporarily by sending false to
196: this method.
197: * @param bEnabled
198: */
199: public void setEnabled(boolean bEnabled) {
200: ScriptBuffer script = new ScriptBuffer();
201: script.appendCall(getContextPath() + "setEnabled", bEnabled);
202: getScriptProxy().addScript(script);
203: }
204:
205: /**
206: * Returns whether this hot key had been destoyed.
207: */
208: @SuppressWarnings("unchecked")
209: public void isDestroyed(
210: org.directwebremoting.proxy.Callback<Boolean> callback) {
211: ScriptBuffer script = new ScriptBuffer();
212: String callbackPrefix = "";
213:
214: if (callback != null) {
215: callbackPrefix = "var reply = ";
216: }
217:
218: script.appendCall(callbackPrefix + getContextPath()
219: + "isDestroyed");
220:
221: if (callback != null) {
222: String key = org.directwebremoting.extend.CallbackHelper
223: .saveCallback(callback, Boolean.class);
224: script
225: .appendCall("__System.activateCallback", key,
226: "reply");
227: }
228:
229: getScriptProxy().addScript(script);
230: }
231:
232: /**
233: * Destroys this hot key. Once a hot key is destroyed it cannot be invoked again.
234: */
235: public void destroy() {
236: ScriptBuffer script = new ScriptBuffer();
237: script.appendCall(getContextPath() + "destroy");
238: getScriptProxy().addScript(script);
239: }
240:
241: /**
242: * Converts the string representation of a keyboard key to an integer keycode. This keycode will match the keycode
243: value of a jsx3.gui.Event of type keydown.
244:
245: The following string representations are supported:
246:
247: alpha numeric characters: A-Z, a-z, 0-9
248:
249: the punctuation keys in the string: ";,./'[]\-=`"
250:
251: functions keys: F1-F15
252:
253: special keys: enter, esc, tab, del, space,
254: backspace, up, down, left, right,
255: insert, home, end, pgup, pgdn.
256: * @param strChar the string representation of a key.
257: * @param callback the keycode.
258: */
259: @SuppressWarnings("unchecked")
260: public void keyDownCharToCode(String strChar,
261: org.directwebremoting.proxy.Callback<Integer> callback) {
262: ScriptBuffer script = new ScriptBuffer();
263: String callbackPrefix = "";
264:
265: if (callback != null) {
266: callbackPrefix = "var reply = ";
267: }
268:
269: script.appendCall(callbackPrefix + getContextPath()
270: + "keyDownCharToCode", strChar);
271:
272: if (callback != null) {
273: String key = org.directwebremoting.extend.CallbackHelper
274: .saveCallback(callback, Integer.class);
275: script
276: .appendCall("__System.activateCallback", key,
277: "reply");
278: }
279:
280: getScriptProxy().addScript(script);
281: }
282:
283: /**
284: * Publishes an event to all subscribed objects.
285: * @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)
286: * @param callback the number of listeners to which the event was broadcast
287: */
288: @SuppressWarnings("unchecked")
289: public void publish(jsx3.lang.Object objEvent,
290: org.directwebremoting.proxy.Callback<Integer> callback) {
291: ScriptBuffer script = new ScriptBuffer();
292: String callbackPrefix = "";
293:
294: if (callback != null) {
295: callbackPrefix = "var reply = ";
296: }
297:
298: script
299: .appendCall(callbackPrefix + getContextPath()
300: + "publish", objEvent);
301:
302: if (callback != null) {
303: String key = org.directwebremoting.extend.CallbackHelper
304: .saveCallback(callback, Integer.class);
305: script
306: .appendCall("__System.activateCallback", key,
307: "reply");
308: }
309:
310: getScriptProxy().addScript(script);
311: }
312:
313: /**
314: * Subscribes an object or function to a type of event published by this object.
315:
316: As of version 3.4 a string value for objHandler is deprecated.
317: * @param strEventId the event type(s).
318: * @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)
319: * @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
320: */
321: public void subscribe(Object[] strEventId,
322: org.directwebremoting.proxy.CodeBlock objHandler,
323: org.directwebremoting.proxy.CodeBlock objFunction) {
324: ScriptBuffer script = new ScriptBuffer();
325: script.appendCall(getContextPath() + "subscribe", strEventId,
326: objHandler, objFunction);
327: getScriptProxy().addScript(script);
328: }
329:
330: /**
331: * Subscribes an object or function to a type of event published by this object.
332:
333: As of version 3.4 a string value for objHandler is deprecated.
334: * @param strEventId the event type(s).
335: * @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)
336: * @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
337: */
338: public void subscribe(Object[] strEventId,
339: jsx3.lang.Object objHandler, String objFunction) {
340: ScriptBuffer script = new ScriptBuffer();
341: script.appendCall(getContextPath() + "subscribe", strEventId,
342: objHandler, objFunction);
343: getScriptProxy().addScript(script);
344: }
345:
346: /**
347: * Subscribes an object or function to a type of event published by this object.
348:
349: As of version 3.4 a string value for objHandler is deprecated.
350: * @param strEventId the event type(s).
351: * @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)
352: * @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
353: */
354: public void subscribe(String strEventId, String objHandler,
355: org.directwebremoting.proxy.CodeBlock objFunction) {
356: ScriptBuffer script = new ScriptBuffer();
357: script.appendCall(getContextPath() + "subscribe", strEventId,
358: objHandler, objFunction);
359: getScriptProxy().addScript(script);
360: }
361:
362: /**
363: * Subscribes an object or function to a type of event published by this object.
364:
365: As of version 3.4 a string value for objHandler is deprecated.
366: * @param strEventId the event type(s).
367: * @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)
368: * @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
369: */
370: public void subscribe(Object[] strEventId, String objHandler,
371: String objFunction) {
372: ScriptBuffer script = new ScriptBuffer();
373: script.appendCall(getContextPath() + "subscribe", strEventId,
374: objHandler, objFunction);
375: getScriptProxy().addScript(script);
376: }
377:
378: /**
379: * Subscribes an object or function to a type of event published by this object.
380:
381: As of version 3.4 a string value for objHandler is deprecated.
382: * @param strEventId the event type(s).
383: * @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)
384: * @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
385: */
386: public void subscribe(String strEventId,
387: org.directwebremoting.proxy.CodeBlock objHandler,
388: org.directwebremoting.proxy.CodeBlock objFunction) {
389: ScriptBuffer script = new ScriptBuffer();
390: script.appendCall(getContextPath() + "subscribe", strEventId,
391: objHandler, objFunction);
392: getScriptProxy().addScript(script);
393: }
394:
395: /**
396: * Subscribes an object or function to a type of event published by this object.
397:
398: As of version 3.4 a string value for objHandler is deprecated.
399: * @param strEventId the event type(s).
400: * @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)
401: * @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
402: */
403: public void subscribe(Object[] strEventId, String objHandler,
404: org.directwebremoting.proxy.CodeBlock objFunction) {
405: ScriptBuffer script = new ScriptBuffer();
406: script.appendCall(getContextPath() + "subscribe", strEventId,
407: objHandler, objFunction);
408: getScriptProxy().addScript(script);
409: }
410:
411: /**
412: * Subscribes an object or function to a type of event published by this object.
413:
414: As of version 3.4 a string value for objHandler is deprecated.
415: * @param strEventId the event type(s).
416: * @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)
417: * @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
418: */
419: public void subscribe(String strEventId,
420: jsx3.lang.Object objHandler, String objFunction) {
421: ScriptBuffer script = new ScriptBuffer();
422: script.appendCall(getContextPath() + "subscribe", strEventId,
423: objHandler, objFunction);
424: getScriptProxy().addScript(script);
425: }
426:
427: /**
428: * Subscribes an object or function to a type of event published by this object.
429:
430: As of version 3.4 a string value for objHandler is deprecated.
431: * @param strEventId the event type(s).
432: * @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)
433: * @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
434: */
435: public void subscribe(String strEventId,
436: jsx3.lang.Object objHandler,
437: org.directwebremoting.proxy.CodeBlock objFunction) {
438: ScriptBuffer script = new ScriptBuffer();
439: script.appendCall(getContextPath() + "subscribe", strEventId,
440: objHandler, objFunction);
441: getScriptProxy().addScript(script);
442: }
443:
444: /**
445: * Subscribes an object or function to a type of event published by this object.
446:
447: As of version 3.4 a string value for objHandler is deprecated.
448: * @param strEventId the event type(s).
449: * @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)
450: * @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
451: */
452: public void subscribe(Object[] strEventId,
453: jsx3.lang.Object objHandler,
454: org.directwebremoting.proxy.CodeBlock objFunction) {
455: ScriptBuffer script = new ScriptBuffer();
456: script.appendCall(getContextPath() + "subscribe", strEventId,
457: objHandler, objFunction);
458: getScriptProxy().addScript(script);
459: }
460:
461: /**
462: * Subscribes an object or function to a type of event published by this object.
463:
464: As of version 3.4 a string value for objHandler is deprecated.
465: * @param strEventId the event type(s).
466: * @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)
467: * @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
468: */
469: public void subscribe(String strEventId,
470: org.directwebremoting.proxy.CodeBlock objHandler,
471: String objFunction) {
472: ScriptBuffer script = new ScriptBuffer();
473: script.appendCall(getContextPath() + "subscribe", strEventId,
474: objHandler, objFunction);
475: getScriptProxy().addScript(script);
476: }
477:
478: /**
479: * Subscribes an object or function to a type of event published by this object.
480:
481: As of version 3.4 a string value for objHandler is deprecated.
482: * @param strEventId the event type(s).
483: * @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)
484: * @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
485: */
486: public void subscribe(String strEventId, String objHandler,
487: String objFunction) {
488: ScriptBuffer script = new ScriptBuffer();
489: script.appendCall(getContextPath() + "subscribe", strEventId,
490: objHandler, objFunction);
491: getScriptProxy().addScript(script);
492: }
493:
494: /**
495: * Subscribes an object or function to a type of event published by this object.
496:
497: As of version 3.4 a string value for objHandler is deprecated.
498: * @param strEventId the event type(s).
499: * @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)
500: * @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
501: */
502: public void subscribe(Object[] strEventId,
503: org.directwebremoting.proxy.CodeBlock objHandler,
504: String objFunction) {
505: ScriptBuffer script = new ScriptBuffer();
506: script.appendCall(getContextPath() + "subscribe", strEventId,
507: objHandler, objFunction);
508: getScriptProxy().addScript(script);
509: }
510:
511: /**
512: * Unsubscribe an object or function from an event published by this object.
513:
514: As of version 3.4 a string value for objHandler is deprecated.
515: * @param strEventId the event type(s).
516: * @param objHandler the value of objHandler passed to subscribe
517: */
518: public void unsubscribe(String strEventId,
519: org.directwebremoting.proxy.CodeBlock objHandler) {
520: ScriptBuffer script = new ScriptBuffer();
521: script.appendCall(getContextPath() + "unsubscribe", strEventId,
522: objHandler);
523: getScriptProxy().addScript(script);
524: }
525:
526: /**
527: * Unsubscribe an object or function from an event published by this object.
528:
529: As of version 3.4 a string value for objHandler is deprecated.
530: * @param strEventId the event type(s).
531: * @param objHandler the value of objHandler passed to subscribe
532: */
533: public void unsubscribe(Object[] strEventId,
534: org.directwebremoting.proxy.CodeBlock objHandler) {
535: ScriptBuffer script = new ScriptBuffer();
536: script.appendCall(getContextPath() + "unsubscribe", strEventId,
537: objHandler);
538: getScriptProxy().addScript(script);
539: }
540:
541: /**
542: * Unsubscribe an object or function from an event published by this object.
543:
544: As of version 3.4 a string value for objHandler is deprecated.
545: * @param strEventId the event type(s).
546: * @param objHandler the value of objHandler passed to subscribe
547: */
548: public void unsubscribe(String strEventId,
549: jsx3.lang.Object objHandler) {
550: ScriptBuffer script = new ScriptBuffer();
551: script.appendCall(getContextPath() + "unsubscribe", strEventId,
552: objHandler);
553: getScriptProxy().addScript(script);
554: }
555:
556: /**
557: * Unsubscribe an object or function from an event published by this object.
558:
559: As of version 3.4 a string value for objHandler is deprecated.
560: * @param strEventId the event type(s).
561: * @param objHandler the value of objHandler passed to subscribe
562: */
563: public void unsubscribe(Object[] strEventId,
564: jsx3.lang.Object objHandler) {
565: ScriptBuffer script = new ScriptBuffer();
566: script.appendCall(getContextPath() + "unsubscribe", strEventId,
567: objHandler);
568: getScriptProxy().addScript(script);
569: }
570:
571: /**
572: * Unsubscribe an object or function from an event published by this object.
573:
574: As of version 3.4 a string value for objHandler is deprecated.
575: * @param strEventId the event type(s).
576: * @param objHandler the value of objHandler passed to subscribe
577: */
578: public void unsubscribe(Object[] strEventId, String objHandler) {
579: ScriptBuffer script = new ScriptBuffer();
580: script.appendCall(getContextPath() + "unsubscribe", strEventId,
581: objHandler);
582: getScriptProxy().addScript(script);
583: }
584:
585: /**
586: * Unsubscribe an object or function from an event published by this object.
587:
588: As of version 3.4 a string value for objHandler is deprecated.
589: * @param strEventId the event type(s).
590: * @param objHandler the value of objHandler passed to subscribe
591: */
592: public void unsubscribe(String strEventId, String objHandler) {
593: ScriptBuffer script = new ScriptBuffer();
594: script.appendCall(getContextPath() + "unsubscribe", strEventId,
595: objHandler);
596: getScriptProxy().addScript(script);
597: }
598:
599: /**
600: * Unsubscribes all subscribed objects to a type of event published by this object.
601: * @param strEventId the event type
602: */
603: public void unsubscribeAll(String strEventId) {
604: ScriptBuffer script = new ScriptBuffer();
605: script.appendCall(getContextPath() + "unsubscribeAll",
606: strEventId);
607: getScriptProxy().addScript(script);
608: }
609:
610: }
|