You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by aj...@apache.org on 2010/02/03 06:22:36 UTC

svn commit: r905896 - /incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/EventResolution.java

Author: ajaquith
Date: Wed Feb  3 05:22:36 2010
New Revision: 905896

URL: http://svn.apache.org/viewvc?rev=905896&view=rev
Log:
Preview is now a tab, and its contents refresh automatically when it is clicked based on the editor's contents. This replaces "live" or "sneak" preview. It looks good and works well. Related: a new EventResolution class now exists to help events methods return AJAX that stripes-support.js understands.

Added:
    incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/EventResolution.java

Added: incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/EventResolution.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/EventResolution.java?rev=905896&view=auto
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/EventResolution.java (added)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/EventResolution.java Wed Feb  3 05:22:36 2010
@@ -0,0 +1,147 @@
+package org.apache.wiki.ui.stripes;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import net.sourceforge.stripes.action.ActionBeanContext;
+import net.sourceforge.stripes.action.Message;
+import net.sourceforge.stripes.action.Resolution;
+import net.sourceforge.stripes.ajax.JavaScriptBuilder;
+import net.sourceforge.stripes.validation.ValidationError;
+import net.sourceforge.stripes.validation.ValidationErrors;
+
+/**
+ * Resolution that returns, in JavaScript object notation format, an arbitrary
+ * Object result, along with the ActionBeanContext's messages and validation
+ * errors.
+ */
+public class EventResolution implements Resolution
+{
+    public static class Result
+    {
+        private final List<ValidationError> m_globalErrors = new ArrayList<ValidationError>();
+
+        private final List<ValidationError> m_fieldErrors = new ArrayList<ValidationError>();
+
+        private final List<Message> m_messages;
+
+        private final Object m_rootObject;
+
+        private final boolean m_isHtml;
+
+        /**
+         * Constructs a new EventResolution.
+         * 
+         * @param context the ActionBeanContext that supplies the messages and
+         *            validation errors.
+         * @param rootObject
+         * @param isHtml whether the results should be interpreted as HTML
+         */
+        public Result( ActionBeanContext context, Object rootObject, boolean isHtml )
+        {
+            super();
+
+            // Set the messages
+            m_messages = context.getMessages();
+
+            // Set the validation errors
+            ValidationErrors errors = context.getValidationErrors();
+            Set<String> fields = errors.keySet();
+            for( String field : fields )
+            {
+                List<ValidationError> fieldErrors = errors.get( field );
+                if( fieldErrors != null )
+                {
+                    if( ValidationErrors.GLOBAL_ERROR.equals( field ) )
+                    {
+                        m_globalErrors.addAll( fieldErrors );
+                    }
+                    else
+                    {
+                        m_fieldErrors.addAll( fieldErrors );
+                    }
+                }
+            }
+
+            // Set the root object & HTML properties
+            m_rootObject = rootObject;
+            m_isHtml = isHtml;
+        }
+
+        /**
+         * Return the messages set by the event.
+         * 
+         * @return the messages
+         */
+        public List<Message> getMessages()
+        {
+            return m_messages;
+        }
+
+        /**
+         * Return the global ValidationErrors set by the event.
+         * 
+         * @return the errors
+         */
+        public List<ValidationError> getGlobalErrors()
+        {
+            return m_globalErrors;
+        }
+
+        /**
+         * Return the field-level ValidationErrors set by the event.
+         * 
+         * @return the errors
+         */
+        public List<ValidationError> getFieldErrors()
+        {
+            return m_fieldErrors;
+        }
+
+        /**
+         * Returns the root object.
+         * 
+         * @return the root object
+         */
+        public Object getResults()
+        {
+            return m_rootObject;
+        }
+
+        /**
+         * Returns {@code true} if the result should be interpreted as HTML,
+         * rather than as a JavaScript object that should be {@code eval}-ed.
+         * 
+         * @return the result
+         */
+        public boolean isHtml()
+        {
+            return m_isHtml;
+        }
+    }
+
+    private final JavaScriptBuilder m_builder;
+
+    public EventResolution( ActionBeanContext context, Object rootObject, boolean isHtml )
+    {
+        m_builder = new JavaScriptBuilder( new Result( context, rootObject, isHtml ) );
+    }
+
+    /**
+     * Converts the ActionBean messges, validation errors and root object passed
+     * in to a series of JavaScript statements that reconstruct the {@link Result}
+     * object in JavaScript, and store the object under the variable name
+     * {@code eventResponse}.
+     */
+    public void execute( HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        response.setContentType("text/javascript");
+        m_builder.setRootVariableName( "eventResponse" );
+        m_builder.build(response.getWriter());
+        response.flushBuffer();
+    }
+}