Author: craigmcc Date: Wed Jan 24 17:25:19 2007 New Revision: 499636 URL: http://svn.apache.org/viewvc?view=rev&rev=499636 Log: First crack at activate/passivate support on DialogContextListener. The basic mechanism appears to work, but we need to add some unit tests to validate correct event firing on the various use cases. To fulfill the vision of the enhancement ticket, we'll also want to provide a base listener class that can save and restore request scoped variables, which can be used as the "data" object for a particular dialog. Because the dialog impls notice when the "data" object implements DialogContextListener, they auto-register and auto-deregister the instances, so no explicit listener registration will be required.
SHALE-399 Modified: shale/framework/trunk/shale-apps/shale-test-dialog-basic/src/main/java/org/apache/shale/examples/test/dialog/basic/ContextListener.java shale/framework/trunk/shale-dialog-basic/src/main/java/org/apache/shale/dialog/basic/BasicDialogContext.java shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogContextListener.java shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogHelper.java shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContext.java shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContextListener.java shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/faces/DialogPhaseListener.java Modified: shale/framework/trunk/shale-apps/shale-test-dialog-basic/src/main/java/org/apache/shale/examples/test/dialog/basic/ContextListener.java URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-apps/shale-test-dialog-basic/src/main/java/org/apache/shale/examples/test/dialog/basic/ContextListener.java?view=diff&rev=499636&r1=499635&r2=499636 ============================================================================== --- shale/framework/trunk/shale-apps/shale-test-dialog-basic/src/main/java/org/apache/shale/examples/test/dialog/basic/ContextListener.java (original) +++ shale/framework/trunk/shale-apps/shale-test-dialog-basic/src/main/java/org/apache/shale/examples/test/dialog/basic/ContextListener.java Wed Jan 24 17:25:19 2007 @@ -47,6 +47,26 @@ } + public void onActivate() { + + // Log the fact that we are processing this event + System.out.println("ContextListener.onActivate(name=" + + getDialogContext().getName() + ", id=" + + getDialogContext().getId() + ")"); + + } + + + public void onPassivate() { + + // Log the fact that we are processing this event + System.out.println("ContextListener.onPassivate(name=" + + getDialogContext().getName() + ", id=" + + getDialogContext().getId() + ")"); + + } + + public void onException(Exception e) { // Log the fact that we are processing this event Modified: shale/framework/trunk/shale-dialog-basic/src/main/java/org/apache/shale/dialog/basic/BasicDialogContext.java URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog-basic/src/main/java/org/apache/shale/dialog/basic/BasicDialogContext.java?view=diff&rev=499636&r1=499635&r2=499636 ============================================================================== --- shale/framework/trunk/shale-dialog-basic/src/main/java/org/apache/shale/dialog/basic/BasicDialogContext.java (original) +++ shale/framework/trunk/shale-dialog-basic/src/main/java/org/apache/shale/dialog/basic/BasicDialogContext.java Wed Jan 24 17:25:19 2007 @@ -502,6 +502,9 @@ // set the dialog in motion start(dialog); + // inform listeners we've been activated as well + fireOnActivate(); + // Advance the computation of our dialog until a view state // is encountered, then navigate to it advance(context, START_OUTCOME); @@ -524,6 +527,7 @@ + getName() + ")"); } + fireOnPassivate(); deactivate(); manager.remove(this); Modified: shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java?view=diff&rev=499636&r1=499635&r2=499636 ============================================================================== --- shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java (original) +++ shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java Wed Jan 24 17:25:19 2007 @@ -413,6 +413,9 @@ stop(context); } + // Tell listeners we have been activated as well + fireOnActivate(); + navigateTo(stateId, context, dp); } @@ -433,6 +436,7 @@ + getName() + ")"); } + fireOnPassivate(); deactivate(); manager.remove(this); Modified: shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogContextListener.java URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogContextListener.java?view=diff&rev=499636&r1=499635&r2=499636 ============================================================================== --- shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogContextListener.java (original) +++ shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogContextListener.java Wed Jan 24 17:25:19 2007 @@ -53,6 +53,42 @@ public void onException(Exception exception); + // ---------------------------------------------- Medium grained callbacks + + + /** + * <p>Handle our parent [EMAIL PROTECTED] DialogContext} instance being placed + * into service for the current request. This will occur when a + * dialog is newly created (by any of the available means), or when + * it is recognized, during a subsequent postback, that there is a + * currently active dialog identiier.</p> + * + * <p><strong>NOTE</strong> - For a newly created [EMAIL PROTECTED] DialogContext} + * instance, this event will be fired <strong>after</strong> the + * instance has been started (and, therefore, after an <code>onStart()</code> + * callback to this listener).</p> + * + * @since 1.1.0 + */ + public void onActivate(); + + + /** + * <p>Handle our parent [EMAIL PROTECTED] DialogContext} instance being taken out + * of service at the end of a request. The response for this request + * has already been rendered, so any changes to the JSF component tree + * will not have any effect.</p> + * + * <p><strong>NOTE</strong> - For a [EMAIL PROTECTED] DialogContext} instance being + * stopped, this event will be fired <strong>before</strong> the + * instance has been started (and, therefore, before an <code>onStop()</code> + * callback to this listener).</p> + * + * @since 1.1.0 + */ + public void onPassivate(); + + //------------------------------------------------- Fine grained callbacks /** Modified: shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogHelper.java URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogHelper.java?view=diff&rev=499636&r1=499635&r2=499636 ============================================================================== --- shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogHelper.java (original) +++ shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/DialogHelper.java Wed Jan 24 17:25:19 2007 @@ -25,8 +25,16 @@ * * @since 1.1.0 */ -public class DialogHelper { +public final class DialogHelper { + + /** + * <p>Private constructor to prevent instantiation.</p> + */ + private DialogHelper() { + ; + } + /** * <p>Return the active [EMAIL PROTECTED] DialogContext} instance for the current Modified: shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContext.java URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContext.java?view=diff&rev=499636&r1=499635&r2=499636 ============================================================================== --- shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContext.java (original) +++ shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContext.java Wed Jan 24 17:25:19 2007 @@ -117,6 +117,36 @@ /** + * <p>Inform all registered [EMAIL PROTECTED] DialogContextListener}s that the dialog + * instance has been activated.</p> + * + */ + protected void fireOnActivate() { + + DialogContextListener[] listeners = getDialogContextListeners(); + for (int i = 0; i < listeners.length; i++) { + listeners[i].onActivate(); + } + + } + + + /** + * <p>Inform all registered [EMAIL PROTECTED] DialogContextListener}s that the dialog + * instance has been passivated.</p> + * + */ + protected void fireOnPassivate() { + + DialogContextListener[] listeners = getDialogContextListeners(); + for (int i = 0; i < listeners.length; i++) { + listeners[i].onPassivate(); + } + + } + + + /** * Inform all registered [EMAIL PROTECTED] DialogContextListener}s that the dialog * instance has begun execution. * Modified: shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContextListener.java URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContextListener.java?view=diff&rev=499636&r1=499635&r2=499636 ============================================================================== --- shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContextListener.java (original) +++ shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/AbstractDialogContextListener.java Wed Jan 24 17:25:19 2007 @@ -74,6 +74,32 @@ /** * [EMAIL PROTECTED] * + * @see org.apache.shale.dialog.DialogContextListener#onActivate() + * + * @since 1.1.0 + */ + public void onActivate() { + + // Do nothing + + } + + /** + * [EMAIL PROTECTED] + * + * @see org.apache.shale.dialog.DialogContextListener#onPassivate() + * + * @since 1.1.0 + */ + public void onPassivate() { + + // Do nothing + + } + + /** + * [EMAIL PROTECTED] + * * @see org.apache.shale.dialog.DialogContextListener#onEntry(java.lang.String) */ public void onEntry(String stateId) { Modified: shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/faces/DialogPhaseListener.java URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/faces/DialogPhaseListener.java?view=diff&rev=499636&r1=499635&r2=499636 ============================================================================== --- shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/faces/DialogPhaseListener.java (original) +++ shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/faces/DialogPhaseListener.java Wed Jan 24 17:25:19 2007 @@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.shale.dialog.Constants; import org.apache.shale.dialog.DialogContext; +import org.apache.shale.dialog.DialogContextListener; import org.apache.shale.dialog.DialogContextManager; import org.apache.shale.dialog.DialogHelper; @@ -125,6 +126,8 @@ if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId())) { afterRestoreView(event.getFacesContext()); + } else if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())) { + afterRenderResponse(event.getFacesContext()); } } @@ -154,6 +157,46 @@ /** + * <p>Fire an <code>onActivate</code> event to all registered listeners + * of the specified [EMAIL PROTECTED] DialogContext} instance.</p> + * + * @param dcontext [EMAIL PROTECTED] DialogContext} instance being activated + */ + private void activate(DialogContext dcontext) { + + DialogContextListener[] listeners = dcontext.getDialogContextListeners(); + for (int i = 0; i < listeners.length; i++) { + listeners[i].onActivate(); + } + + } + + + /** + * <p>Perform the required processing after the <em>Render Response</em> + * phase of request processing lifecycle has been completed for the + * current request:</p> + * <ul> + * <li>If there is a currently active [EMAIL PROTECTED] DialogContext} instance + * for the current request, fire an <code>onPassivate()</code> event + * to all of its registered [EMAIL PROTECTED] DialogContextListener}s.</li> + * </ul> + * + * @param context <code>FacesContext</code> for the current request + * + * @since 1.1.0 + */ + private void afterRenderResponse(FacesContext context) { + + DialogContext dcontext = DialogHelper.getDialogContext(context); + if (dcontext != null) { + passivate(dcontext); + } + + } + + + /** * <p>Perform the required processing after the <em>Restore View Phase</em> * of the request processing lifecycle has been completed for the current * request:</p> @@ -169,6 +212,9 @@ * the <code>id</code> of an active [EMAIL PROTECTED] DialogContext} instance for * the current user, that existing instance will be configured as the * parent [EMAIL PROTECTED] DialogContext} instance for the newly created instance.</li> + * <li>(Since 1.1.0) If a [EMAIL PROTECTED] DialogContext} instance was restored + * for the current request, fire an <code>onActivate()</code> + * event to all of its registered [EMAIL PROTECTED] DialogContextListener}s.</li> * </ul> * * @param context <code>FacesContext</code> for the current request @@ -187,7 +233,10 @@ get(Constants.DIALOG_ID); } if (id != null) { - restore(context, id); + DialogContext dcontext = restore(context, id); + if (dcontext != null) { + activate(dcontext); + } return; } @@ -291,6 +340,22 @@ /** + * <p>Fire an <code>onPassivate</code> event to all registered listeners + * of the specified [EMAIL PROTECTED] DialogContext} instance.</p> + * + * @param dcontext [EMAIL PROTECTED] DialogContext} instance being activated + */ + private void passivate(DialogContext dcontext) { + + DialogContextListener[] listeners = dcontext.getDialogContextListeners(); + for (int i = 0; i < listeners.length; i++) { + listeners[i].onPassivate(); + } + + } + + + /** * <p>Restore access to the [EMAIL PROTECTED] DialogContext} with the specified id, * if possible. If there was any opaque state information stored, update * the corresponding [EMAIL PROTECTED] DialogContext} instance as well.</p> @@ -298,17 +363,19 @@ * @param context FacesContext for the current request * @param dialogId Dialog identifier of the [EMAIL PROTECTED] DialogContext} * to be restored + * + * @return The restored [EMAIL PROTECTED] DialogContext} instance (if any) */ - private void restore(FacesContext context, String dialogId) { + private DialogContext restore(FacesContext context, String dialogId) { DialogContextManager manager = DialogHelper.getDialogContextManager(context); if (manager == null) { - return; + return null; } DialogContext dcontext = manager.get(dialogId); if (dcontext == null) { - return; + return null; } if (log.isDebugEnabled()) { log.debug("afterPhase() restoring dialog context with id '" @@ -320,6 +387,7 @@ if (opaqueState != null) { dcontext.setOpaqueState(opaqueState); } + return dcontext; }