Author: ivaynberg
Date: Thu Jan 25 09:44:29 2007
New Revision: 499866

URL: http://svn.apache.org/viewvc?view=rev&rev=499866
Log:
WICKET-234: Adding Listener to AjaxRequestTarget

Modified:
    
incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java

Modified: 
incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java?view=diff&rev=499866&r1=499865&r2=499866
==============================================================================
--- 
incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java 
(original)
+++ 
incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java 
Thu Jan 25 09:44:29 2007
@@ -64,9 +64,9 @@
  * feature can be useful when it is desirable to link component update with 
some
  * javascript effects.
  * <p>
- * The target provides a listener interface [EMAIL PROTECTED] Listener} that 
can be used to
- * add code that responds to various target events by adding listeners via
- * [EMAIL PROTECTED] #addListener(wicket.ajax.AjaxRequestTarget.Listener)}
+ * The target provides a listener interface [EMAIL PROTECTED] IListener} that 
can be used
+ * to add code that responds to various target events by adding listeners via
+ * [EMAIL PROTECTED] #addListener(wicket.ajax.AjaxRequestTarget.IListener)}
  * 
  * @since 1.2
  * 
@@ -79,7 +79,7 @@
         * various target-related events
         * 
         */
-       public static interface Listener
+       public static interface IListener
        {
                /**
                 * Triggered before ajax request target begins its response 
cycle
@@ -92,7 +92,42 @@
                 *            append/prepend javascript
                 * 
                 */
-               public void onBeforeRespond(Map map, AjaxRequestTarget target);
+               public void onBeforeRespond(Map<String, Component> map, 
AjaxRequestTarget target);
+
+               /**
+                * Triggered after ajax request target is done with its 
response cycle.
+                * At this point only additional javascript can be output to the
+                * response using the provided [EMAIL PROTECTED] 
IJavascriptResponse} object
+                * 
+                * NOTE: During this stage of processing any calls to target 
that
+                * manipulate the response (adding components, javascript) will 
have no
+                * effect
+                * 
+                * @param map
+                *            read-only map:markupId->component of components 
already
+                *            added to the target
+                * @param response
+                *            response object that can be used to output 
javascript
+                */
+               public void onAfterRespond(Map<String, Component> map, 
IJavascriptResponse response);
+       }
+
+       /**
+        * An ajax javascript response that allows users to add javascript to be
+        * executed on the client side
+        * 
+        * @author ivaynberg
+        */
+       public static interface IJavascriptResponse
+       {
+               /**
+                * Adds more javascript to the ajax response that will be 
executed on
+                * the client side
+                * 
+                * @param script
+                *            javascript
+                */
+               public void addJavascript(String script);
        }
 
        private static final Logger Log = 
LoggerFactory.getLogger(AjaxRequestTarget.class);
@@ -107,7 +142,7 @@
        private final Map<String, Component> markupIdToComponent = new 
HashMap<String, Component>();
 
        /** a list of listeners */
-       private List<Listener> listeners = null;
+       private List<IListener> listeners = null;
 
        /**
         * Constructor
@@ -121,7 +156,7 @@
         * 
         * @param listener
         */
-       public void addListener(Listener listener)
+       public void addListener(IListener listener)
        {
                if (listener == null)
                {
@@ -130,7 +165,7 @@
 
                if (listeners == null)
                {
-                       listeners = new LinkedList<Listener>();
+                       listeners = new LinkedList<IListener>();
                }
 
                if (!listeners.contains(listener))
@@ -297,7 +332,7 @@
                                final Map<String, Component> components = 
Collections
                                                
.unmodifiableMap(markupIdToComponent);
 
-                               for (Listener listener : listeners)
+                               for (IListener listener : listeners)
                                {
                                        listener.onBeforeRespond(components, 
this);
                                }
@@ -315,6 +350,31 @@
                        {
                                respondInvocation(response, js);
                        }
+
+                       // invoke onafterresponse event on listeners
+                       if (listeners != null)
+                       {
+                               final Map<String, Component> components = 
Collections
+                                               
.unmodifiableMap(markupIdToComponent);
+
+                               // create response that will be used by 
listeners to append
+                               // javascript
+                               final IJavascriptResponse jsresponse = new 
IJavascriptResponse()
+                               {
+
+                                       public void addJavascript(String script)
+                                       {
+                                               respondInvocation(response, 
script);
+                                       }
+
+                               };
+
+                               for (IListener listener : listeners)
+                               {
+                                       listener.onAfterRespond(components, 
jsresponse);
+                               }
+                       }
+
 
                        response.write("</ajax-response>");
                }


Reply via email to