You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2005/07/02 03:58:12 UTC

svn commit: r208814 - in /struts/shale/trunk/use-cases/src: java/org/apache/shale/usecases/ajax/ java/org/apache/shale/usecases/remote/ java/org/apache/shale/usecases/view/ test/org/apache/shale/usecases/remote/ web/ web/WEB-INF/ web/ajax/ web/lookup/

Author: craigmcc
Date: Fri Jul  1 18:58:10 2005
New Revision: 208814

URL: http://svn.apache.org/viewcvs?rev=208814&view=rev
Log:
Update the "list categories" and "list locales" implementations, and unit
tests, to use the new convenience base classes provided by the core library.
Implement a list of state names capability to illustrate use of the
"list completion" facility.

Also, add the beginnings of a very simple Ajax example, providing completion
lookup while using the standard JSF text input component.  It doesn't work
yet, but I need to commit (from the laptop I took to JavaOne) so that I can
work on this with my desktop machine).

Added:
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/ajax/
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/ajax/Completion.java
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListStateNames.java
    struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListStateNamesTestCase.java
    struts/shale/trunk/use-cases/src/web/ajax/
    struts/shale/trunk/use-cases/src/web/ajax/completion.jsp
Modified:
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListCategories.java
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListLocales.java
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Bundle.properties
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Domains.java
    struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListCategoriesTestCase.java
    struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListLocalesTestCase.java
    struts/shale/trunk/use-cases/src/web/WEB-INF/chain-config.xml
    struts/shale/trunk/use-cases/src/web/WEB-INF/faces-config.xml
    struts/shale/trunk/use-cases/src/web/lookup/listCategories.jsp
    struts/shale/trunk/use-cases/src/web/lookup/listLocales.jsp
    struts/shale/trunk/use-cases/src/web/usecases.jsp

Added: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/ajax/Completion.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/ajax/Completion.java?rev=208814&view=auto
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/ajax/Completion.java (added)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/ajax/Completion.java Fri Jul  1 18:58:10 2005
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.usecases.ajax;
+
+import javax.faces.application.Application;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.shale.usecases.view.BaseViewController;
+
+/**
+ * <p>Viwe controller for Ajax code completion test case.</p>
+ *
+ * $Id$
+ */
+public class Completion extends BaseViewController {
+    
+    
+    // -------------------------------------------------------- Static Variables
+
+
+    /**
+     * <p>The <code>Log</code> instance for this class.</p>
+     */
+    private static final Log log = LogFactory.getLog(Completion.class);
+
+
+    // -------------------------------------------------------------- Properties
+
+
+    /**
+     * <p>The state name to be entered.</p>
+     */
+    private String name = null;
+
+
+    /**
+     * <p>Return the state name to be entered.</p>
+     */
+    public String getName() {
+
+        return this.name;
+
+    }
+
+
+    /**
+     * <p>Set the state name to be entered.</p>
+     *
+     * @param name The new state name
+     */
+    public void setName(String name) {
+
+        this.name = name;
+
+    }
+
+
+    /**
+     * <p>The result stored by the submit action.</p>
+     */
+    private String result = null;
+
+
+    /**
+     * <p>Return the result stored by the submit action.</p>
+     */
+    public String getResult() {
+
+        return this.result;
+
+    }
+
+
+    /**
+     * <p>Set the result stored by the submit action.</p>
+     *
+     * @param result The new result
+     */
+    public void setResult(String result) {
+
+        this.result = result;
+
+    }
+
+
+    // ------------------------------------------------- Component Event Methods
+
+
+    /**
+     * <p>Store submitted value in the result property.</p.
+     */
+    public String submit() {
+
+        setResult("User submitted: " + getName());
+        setName(null);
+        return null; // Redisplay the current page
+
+    }
+
+
+    // -------------------------------------------------- ViewController Methods
+
+
+}

Modified: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListCategories.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListCategories.java?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListCategories.java (original)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListCategories.java Fri Jul  1 18:58:10 2005
@@ -17,20 +17,18 @@
 package org.apache.shale.usecases.remote;
 
 import javax.faces.model.SelectItem;
-import org.apache.commons.chain.Command;
-import org.apache.commons.chain.Context;
+import org.apache.shale.remote.AbstractSelectItems;
 import org.apache.shale.remote.RemoteContext;
-import org.apache.shale.remote.ResponseWrapper;
 import org.apache.shale.usecases.view.Domains;
 
 /**
- * <p>Remote command to list the supported message categories for this application,
- * localized based on the <code>Locale</code> specified by the request.</p>
+ * <p>Remote command to list a set of supported categories, localized for the request
+ * Locale, optionally filtered by matching a test String against the label.</p>
  *
  * $Id$
  */
-public class ListCategories implements Command {
-    
+public class ListCategories extends AbstractSelectItems {
+
     
     // -------------------------------------------------------- Static Variables
 
@@ -42,59 +40,24 @@
     private static final String DOMAINS = "domains";
 
 
-    // --------------------------------------------------------- Command Methods
+    // ------------------------------------------------------- Protected Methods
 
 
     /**
-     * <p>Look up the supported categories and create a response including them,
-     * with a root <code>cqategories</code> element, a <code>category</code>
-     * element for each supported category, with subordinate elements
-     * <code>label</code> and <code>value</code>.</p>
+     * <p>Return the set of legal supported categories.</p>
      *
      * @param context {@link RemoteContext} for this request
+     * @param test Test value optionally used to select legal values
      */
-    public boolean execute(Context context) throws Exception {
-
-        // Set up the response we will create
-        RemoteContext rcontext = (RemoteContext) context;
-        rcontext.setResponseContentType("text/xml");
-        rcontext.setResponseEncoding("UTF-8");
-        ResponseWrapper wrapper =
-          new ResponseWrapper(rcontext.getResponseWriter(), "UTF-8");
+    protected SelectItem[] legal(RemoteContext context, String test) {
 
-        // Look up the data we need in order to complete this response
-        Domains domains = (Domains) rcontext.getContextAttributes().get(DOMAINS);
+        Domains domains = (Domains) context.getContextAttributes().get(DOMAINS);
         if (domains == null) {
             // FIXME - fake managed beans setup because we are not a JSF request
             domains = new Domains();
-            rcontext.getContextAttributes().put(DOMAINS, domains);
+            context.getContextAttributes().put(DOMAINS, domains);
         }
-        SelectItem items[] =
-          domains.getSupportedCategories(rcontext.getRequestLocale());
-
-        // Generate the response content
-        wrapper.startDocument();
-        wrapper.startElement("categories");
-        wrapper.writeNewline();
-        for (int i = 0; i < items.length; i++) {
-            wrapper.startElement("category");
-            wrapper.writeNewline();
-            wrapper.startElement("label");
-            wrapper.writeText(items[i].getLabel());
-            wrapper.endElement("label");
-            wrapper.writeNewline();
-            wrapper.startElement("value");
-            wrapper.writeText(items[i].getValue());
-            wrapper.endElement("value");
-            wrapper.writeNewline();
-            wrapper.endElement("category");
-            wrapper.writeNewline();
-        }
-        wrapper.endElement("categories");
-        wrapper.endDocument();
-
-        // Return true because this response is now complete
-        return true;
+        return domains.getSupportedCategories(context.getRequestLocale());
 
     }
 

Modified: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListLocales.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListLocales.java?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListLocales.java (original)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListLocales.java Fri Jul  1 18:58:10 2005
@@ -17,20 +17,18 @@
 package org.apache.shale.usecases.remote;
 
 import javax.faces.model.SelectItem;
-import org.apache.commons.chain.Command;
-import org.apache.commons.chain.Context;
+import org.apache.shale.remote.AbstractSelectItems;
 import org.apache.shale.remote.RemoteContext;
-import org.apache.shale.remote.ResponseWrapper;
 import org.apache.shale.usecases.view.Domains;
 
 /**
- * <p>Remote command to list the supported locales for this application,
- * localized based on the <code>Locale</code> specified by the request.</p>
+ * <p>Remote command to list a set of supported locales, localized for the request
+ * Locale, optionally filtered by matching a test String against the label.</p>
  *
  * $Id$
  */
-public class ListLocales implements Command {
-    
+public class ListLocales extends AbstractSelectItems {
+
     
     // -------------------------------------------------------- Static Variables
 
@@ -42,59 +40,24 @@
     private static final String DOMAINS = "domains";
 
 
-    // --------------------------------------------------------- Command Methods
+    // ------------------------------------------------------- Protected Methods
 
 
     /**
-     * <p>Look up the supported locales and create a response including them,
-     * with a root <code>locales</code> element, a <code>locale</code> element
-     * for each supported <code>Locale</code>, with subordinate elements
-     * <code>label</code> and <code>value</code>.</p>
+     * <p>Return the set of legal supported locales.</p>
      *
      * @param context {@link RemoteContext} for this request
+     * @param test Test value optionally used to select legal values
      */
-    public boolean execute(Context context) throws Exception {
-
-        // Set up the response we will create
-        RemoteContext rcontext = (RemoteContext) context;
-        rcontext.setResponseContentType("text/xml");
-        rcontext.setResponseEncoding("UTF-8");
-        ResponseWrapper wrapper =
-          new ResponseWrapper(rcontext.getResponseWriter(), "UTF-8");
+    protected SelectItem[] legal(RemoteContext context, String test) {
 
-        // Look up the data we need in order to complete this response
-        Domains domains = (Domains) rcontext.getContextAttributes().get(DOMAINS);
+        Domains domains = (Domains) context.getContextAttributes().get(DOMAINS);
         if (domains == null) {
             // FIXME - fake managed beans setup because we are not a JSF request
             domains = new Domains();
-            rcontext.getContextAttributes().put(DOMAINS, domains);
+            context.getContextAttributes().put(DOMAINS, domains);
         }
-        SelectItem items[] =
-          domains.getSupportedLocales(rcontext.getRequestLocale());
-
-        // Generate the response content
-        wrapper.startDocument();
-        wrapper.startElement("locales");
-        wrapper.writeNewline();
-        for (int i = 0; i < items.length; i++) {
-            wrapper.startElement("locale");
-            wrapper.writeNewline();
-            wrapper.startElement("label");
-            wrapper.writeText(items[i].getLabel());
-            wrapper.endElement("label");
-            wrapper.writeNewline();
-            wrapper.startElement("value");
-            wrapper.writeText(items[i].getValue());
-            wrapper.endElement("value");
-            wrapper.writeNewline();
-            wrapper.endElement("locale");
-            wrapper.writeNewline();
-        }
-        wrapper.endElement("locales");
-        wrapper.endDocument();
-
-        // Return true because this response is now complete
-        return true;
+        return domains.getSupportedLocales(context.getRequestLocale());
 
     }
 

Added: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListStateNames.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListStateNames.java?rev=208814&view=auto
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListStateNames.java (added)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/remote/ListStateNames.java Fri Jul  1 18:58:10 2005
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.usecases.remote;
+
+import org.apache.shale.remote.AbstractListCompletions;
+import org.apache.shale.remote.RemoteContext;
+import org.apache.shale.usecases.view.Domains;
+
+/**
+ * <p>Remote command to list the state names that match the prefix specified
+ * by the <code>prefix</code> request parameter.  If there are no such matching
+ * names, zero elements are returned.  If no prefix is specified, all possible
+ * names are returned.</p>
+ *
+ * $Id$
+ */
+public class ListStateNames extends AbstractListCompletions {
+    
+    
+    // -------------------------------------------------------- Static Variables
+
+
+    /**
+     * <p>The context attribute key under which our {@link Domains} object
+     * may be found.</p>
+     */
+    private static final String DOMAINS = "domains";
+
+
+    // ------------------------------------------------------- Protected Methods
+
+
+    /**
+     * <p>Return the set of legal state names.</p>
+     *
+     * @param context {@link RemoteContext} for this request
+     * @param test Test value optionally used to select legal values
+     */
+    protected String[] legal(RemoteContext context, String test) {
+
+        Domains domains = (Domains) context.getContextAttributes().get(DOMAINS);
+        if (domains == null) {
+            // FIXME - fake managed beans setup because we are not a JSF request
+            domains = new Domains();
+            context.getContextAttributes().put(DOMAINS, domains);
+        }
+        return domains.getStateNames();
+
+    }
+
+
+}

Modified: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Bundle.properties
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Bundle.properties?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Bundle.properties (original)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Bundle.properties Fri Jul  1 18:58:10 2005
@@ -21,6 +21,12 @@
 category.1=Java
 category.2=Music
 
+# Ajax Code Completion Example
+ajax.completion.title=Ajax Code Completion Example
+ajax.completion.prompt=Enter the name of a US state:
+ajax.completion.finish=Finish
+ajax.completion.submit=Submit
+
 # JNDI Test Labels
 jndi.test.title=JNDI Test Title
 jndi.test.expected=Expected:
@@ -103,6 +109,8 @@
 
 # Use Cases Menu Messages
 usecases.categories=List Categories (Remoting)
+usecases.ajax=Ajax Interactions
+usecases.completion=Code Completion (Standard JSF Components)
 usecases.edit=Edit User Profile
 usecases.java=Remoting Support (Java Based)
 usecases.jndi=JNDI Access Via Expressions
@@ -112,6 +120,7 @@
 usecases.logoff=Log Off
 usecases.logon=Log On Dialog
 usecases.primary=Primary Use Cases
+usecases.states=List State Names (Remoting)
 usecases.subview=Subview Processing
 usecases.title=Shale Framework Use Cases
 usecases.validator=Commons Validator Integration

Modified: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Domains.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Domains.java?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Domains.java (original)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/view/Domains.java Fri Jul  1 18:58:10 2005
@@ -17,6 +17,7 @@
 package org.apache.shale.usecases.view;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -69,7 +70,100 @@
     private Map locales = new HashMap();
 
 
+    /**
+     * <p>Array of <code>SelectItem</code> representing the abbreviations
+     * and descriptions of all valid US states.</p>
+     */
+    private SelectItem[] states = {
+        new SelectItem("AL", "Alabama"),
+        new SelectItem("AK", "Alaska"),
+        new SelectItem("AZ", "Arizona"),
+        new SelectItem("AR", "Arkansas"),
+        new SelectItem("CA", "California"),
+        new SelectItem("CO", "Colorado"),
+        new SelectItem("CT", "Connecticut"),
+        new SelectItem("DE", "Delaware"),
+        new SelectItem("DC", "District of Columbia"),
+        new SelectItem("FL", "Florida"),
+        new SelectItem("GA", "Georgia"),
+        new SelectItem("HI", "Hawaii"),
+        new SelectItem("ID", "Idaho"),
+        new SelectItem("IL", "Illinois"),
+        new SelectItem("IN", "Indiana"),
+        new SelectItem("IA", "Iowa"),
+        new SelectItem("KS", "Kansas"),
+        new SelectItem("KY", "Kentucky"),
+        new SelectItem("LA", "Louisiana"),
+        new SelectItem("ME", "Maine"),
+        new SelectItem("MD", "Maryland"),
+        new SelectItem("MA", "Massachusetts"),
+        new SelectItem("MI", "Michigan"),
+        new SelectItem("MN", "Minnesota"),
+        new SelectItem("MS", "Mississippi"),
+        new SelectItem("MO", "Missouri"),
+        new SelectItem("MT", "Montana"),
+        new SelectItem("NE", "Nebraska"),
+        new SelectItem("NV", "Nevada"),
+        new SelectItem("NH", "New Hampshire"),
+        new SelectItem("NJ", "New Jersey"),
+        new SelectItem("NM", "New Mexico"),
+        new SelectItem("NY", "New York"),
+        new SelectItem("NC", "North Carolina"),
+        new SelectItem("ND", "North Dakota"),
+        new SelectItem("OH", "Ohio"),
+        new SelectItem("OK", "Oklahoma"),
+        new SelectItem("OR", "Oregon"),
+        new SelectItem("PA", "Pennyslvania"),
+        new SelectItem("RI", "Rhode Island"),
+        new SelectItem("SC", "South Carolina"),
+        new SelectItem("SD", "South Dakota"),
+        new SelectItem("TN", "Tennessee"),
+        new SelectItem("TX", "Texas"),
+        new SelectItem("UT", "Utah"),
+        new SelectItem("VT", "Vermont"),
+        new SelectItem("VA", "Virginia"),
+        new SelectItem("WA", "Washington"),
+        new SelectItem("WV", "West Virginia"),
+        new SelectItem("WI", "Wisconsin"),
+        new SelectItem("WY", "Wyoming")
+    };
+
+
+    /**
+     * <p>Array of state names in alphabetical order (lazily instantiated).</p>
+     */
+    private String[] stateNames = null;
+
+
     // -------------------------------------------------------------- Properties
+
+
+    /**
+     * <p>Return an array of selection items representing valid US state
+     * abbreviations and descriptions.</p>
+     */
+    public SelectItem[] getStates() {
+
+        return this.states;
+
+    }
+
+
+    /**
+     * <p>Return an array of state names in alphabetical order.</p>
+     */
+    public String[] getStateNames() {
+
+        if (stateNames == null) {
+            stateNames = new String[states.length];
+            for (int i = 0; i < stateNames.length; i++) {
+                stateNames[i] = states[i].getLabel();
+            }
+            Arrays.sort(stateNames);
+	}
+        return stateNames;
+
+    }
 
 
     /**

Modified: struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListCategoriesTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListCategoriesTestCase.java?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListCategoriesTestCase.java (original)
+++ struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListCategoriesTestCase.java Fri Jul  1 18:58:10 2005
@@ -117,6 +117,7 @@
 
     }
 
+
     // Tear Down the test
     public void tearDown() {
         super.tearDown();
@@ -126,6 +127,7 @@
         responseWriter = null;
     }
 
+
     // Test behavior of execute() method
     public void testExecute() throws Exception {
 
@@ -139,7 +141,7 @@
         ByteArrayInputStream in = new ByteArrayInputStream(dataPickle
                 .toString().getBytes());
         Document document = docBuilder.parse(in);
-        NodeList nodes = document.getElementsByTagName("category");
+        NodeList nodes = document.getElementsByTagName("item");
         assertEquals(nodes.getLength(), 3);
 
         locale = new Locale("de");
@@ -150,8 +152,30 @@
         docBuilder = factory.newDocumentBuilder();
         in = new ByteArrayInputStream(dataPickle.toString().getBytes());
         document = docBuilder.parse(in);
-        nodes = document.getElementsByTagName("category");
+        nodes = document.getElementsByTagName("item");
         assertEquals(nodes.getLength(), 3);
 
     }
+
+
+    // Test behavior of execute() method with a filtering prefix
+    public void testFilter() throws Exception {
+
+        Locale locale = new Locale("en");
+        request.setLocale(locale);
+        request.setPathElements(null, null, "/list/supportedCategories.remote", "prefix=M");
+        request.addParameter("prefix", "M");
+        remoteCommand.execute(swcontext);
+
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        DocumentBuilder docBuilder = factory.newDocumentBuilder();
+        ByteArrayInputStream in = new ByteArrayInputStream(dataPickle
+                .toString().getBytes());
+        Document document = docBuilder.parse(in);
+        NodeList nodes = document.getElementsByTagName("item");
+        assertEquals(nodes.getLength(), 1);
+
+    }
+
+
 }

Modified: struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListLocalesTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListLocalesTestCase.java?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListLocalesTestCase.java (original)
+++ struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListLocalesTestCase.java Fri Jul  1 18:58:10 2005
@@ -117,6 +117,7 @@
 
     }
 
+
     // Tear Down the test
     public void tearDown() {
         super.tearDown();
@@ -126,6 +127,7 @@
         responseWriter = null;
     }
 
+
     // Test behavior of execute() method
     public void testExecute() throws Exception {
 
@@ -139,9 +141,31 @@
         ByteArrayInputStream in = new ByteArrayInputStream(dataPickle
                 .toString().getBytes());
         Document document = docBuilder.parse(in);
-        NodeList nodes = document.getElementsByTagName("locale");
+        NodeList nodes = document.getElementsByTagName("item");
         assertEquals(nodes.getLength(), 4);
 
 
     }
+
+
+    // Test behavior of execute() method with a filtering prefix
+    public void testFilter() throws Exception {
+
+        Locale locale = new Locale("en");
+        request.setLocale(locale);
+        request.setPathElements(null, null, "/list/supportedLocales.remote", "prefix=e");
+        request.addParameter("prefix", "e");
+        remoteCommand.execute(swcontext);
+
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        DocumentBuilder docBuilder = factory.newDocumentBuilder();
+        ByteArrayInputStream in = new ByteArrayInputStream(dataPickle
+                .toString().getBytes());
+        Document document = docBuilder.parse(in);
+        NodeList nodes = document.getElementsByTagName("item");
+        assertEquals(nodes.getLength(), 1);
+
+    }
+
+
 }

Added: struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListStateNamesTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListStateNamesTestCase.java?rev=208814&view=auto
==============================================================================
--- struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListStateNamesTestCase.java (added)
+++ struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/remote/ListStateNamesTestCase.java Fri Jul  1 18:58:10 2005
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.usecases.remote;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.chain.Catalog;
+import org.apache.commons.chain.CatalogFactory;
+import org.apache.commons.chain.Command;
+import org.apache.commons.chain.impl.CatalogBase;
+import org.apache.shale.faces.ShaleWebContext;
+import org.apache.shale.remote.RemoteCommand;
+import org.apache.shale.test.base.AbstractViewControllerTestCase;
+import org.apache.shale.test.mock.MockHttpServletResponse;
+import org.apache.shale.usecases.view.Domains;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+
+public class ListStateNamesTestCase extends AbstractViewControllerTestCase {
+
+    // Construct a new instance of this test case.
+    public ListStateNamesTestCase(String name) {
+        super(name);
+    }
+
+    // Return the tests included in this test case.
+    public static Test suite() {
+
+        return (new TestSuite(ListStateNamesTestCase.class));
+
+    }
+
+    // remote filter command</p>
+    private Command remoteCommand = null;
+    
+    // shale web context created by the filter
+    private ShaleWebContext swcontext = null;
+
+    // buffer holding the response data
+    StringWriter dataPickle = null;
+
+    // response print writer decorator
+    PrintWriter responseWriter = null;
+    
+    
+
+    public void setUp() {
+        super.setUp();
+
+        // Configure the supported locales for this application
+        List list = new ArrayList();
+        list.add(new Locale("en"));
+        list.add(new Locale("fr"));
+        list.add(new Locale("de"));
+        list.add(new Locale("es"));
+        application.setSupportedLocales(list);
+
+        // push to application scope, this would be done
+        // declaratively in the app using the faces config
+        // managed bean mechanism
+        servletContext.setAttribute("domains", new Domains());
+
+        // create the command to test
+        Command command = new ListStateNames();
+        
+        // this command would be created by the shale servlet filter
+        remoteCommand = new RemoteCommand();
+
+        // override the base mock response providing a
+        // response buffer
+        dataPickle = new StringWriter();
+        responseWriter = new PrintWriter(dataPickle);
+        response = new MockHttpServletResponse() {
+            public PrintWriter getWriter() throws IOException {
+                return responseWriter;
+            };
+        };
+
+        // create a fake shale context
+        swcontext = new ShaleWebContext(servletContext,
+                request, response, null);
+
+        //create a catalog, this would be loaded from the chains XML config file        
+        Catalog catalog = CatalogFactory.getInstance().getCatalog("remote");
+        if (catalog == null) {
+            catalog = new CatalogBase();
+            catalog.addCommand("/list/stateNames.remote", command);
+            CatalogFactory.getInstance().addCatalog("remote", catalog);
+        }
+
+
+    }
+
+
+    // Tear Down the test
+    public void tearDown() {
+        super.tearDown();
+        remoteCommand = null;
+        swcontext = null;
+        dataPickle = null;
+        responseWriter = null;
+    }
+
+
+    // Test behavior of execute() method
+    public void testExecute() throws Exception {
+
+        Locale locale = new Locale("en");
+        request.setLocale(locale);
+        request.setPathElements(null, null, "/list/stateNames.remote", null);
+        remoteCommand.execute(swcontext);
+
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        DocumentBuilder docBuilder = factory.newDocumentBuilder();
+        ByteArrayInputStream in = new ByteArrayInputStream(dataPickle
+                .toString().getBytes());
+        Document document = docBuilder.parse(in);
+        NodeList nodes = document.getElementsByTagName("value");
+        assertEquals(nodes.getLength(), 51);
+
+    }
+
+
+    // Test behavior of execute() method with a filtering prefix
+    public void testFilter() throws Exception {
+
+        Locale locale = new Locale("en");
+        request.setLocale(locale);
+        request.setPathElements(null, null, "/list/stateNames.remote", "prefix=new");
+        request.addParameter("prefix", "new");
+        remoteCommand.execute(swcontext);
+
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        DocumentBuilder docBuilder = factory.newDocumentBuilder();
+        ByteArrayInputStream in = new ByteArrayInputStream(dataPickle
+                .toString().getBytes());
+        Document document = docBuilder.parse(in);
+        NodeList nodes = document.getElementsByTagName("value");
+        assertEquals(nodes.getLength(), 4);
+
+    }
+
+
+}

Modified: struts/shale/trunk/use-cases/src/web/WEB-INF/chain-config.xml
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/WEB-INF/chain-config.xml?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/web/WEB-INF/chain-config.xml (original)
+++ struts/shale/trunk/use-cases/src/web/WEB-INF/chain-config.xml Fri Jul  1 18:58:10 2005
@@ -35,9 +35,56 @@
 
   <catalog               name="remote">
 
+    <!-- NOTE - alternate implementations are provided to illustrate choices -->
+
+    <!-- This implementation uses the generic EvaluateExpression and
+         BasicListCompletions classes from the core library.  It cannot be
+         used until the core creates a FacesContext for remote requests. -->
+    <!--
+    <chain               name="/list/stateNames.remote">
+      <command      className="org.apache.shale.remote.EvaluateExpression"
+                   expression="#{domains.stateNames}"
+                 attributeKey="strings"/>
+      <command      className="org.apache.shale.remote.BasicListCompletions"/>
+    </chain>
+    -->
+
+    <!-- This implementation subclasses AbstractListCompletions and
+         implements the legal() method to return the appropriate data. -->
+    <command             name="/list/stateNames.remote"
+                    className="org.apache.shale.usecases.remote.ListStateNames"/>
+
+    <!-- This implementation uses the generic EvaluateExpression and
+         BasicSelectItems classes from the core library.  It cannot be
+         used until the core creates a FacesContext for remote requests. -->
+    <!--
+    <chain               name="/list/supportedCategories.remote">
+      <command      className="org.apache.shale.remote.EvaluateExpression"
+                   expression="#{domains.supportedCategories}"
+                 attributeKey="selectItems"/>
+      <command      className="org.apache.shale.remote.BasicSelectItems"/>
+    </chain>
+    -->
+
+    <!-- This implementation subclasses AbstractSelectItems and
+         implements the legal() method to return the appropriate data. -->
     <command             name="/list/supportedCategories.remote"
                     className="org.apache.shale.usecases.remote.ListCategories"/>
 
+    <!-- This implementation uses the generic EvaluateExpression and
+         BasicSelectItems classes from the core library.  It cannot be
+         used until the core creates a FacesContext for remote requests. -->
+    <!--
+    <chain               name="/list/supportedLocales.remote">
+      <command      className="org.apache.shale.remote.EvaluateExpression"
+                   expression="#{domains.supportedLocales}"
+                 attributeKey="selectItems"/>
+      <command      className="org.apache.shale.remote.BasicSelectItems"/>
+    </chain>
+    -->
+
+    <!-- This implementation subclasses AbstractSelectItems and
+         implements the legal() method to return the appropriate data. -->
     <command             name="/list/supportedLocales.remote"
                     className="org.apache.shale.usecases.remote.ListLocales"/>
 

Modified: struts/shale/trunk/use-cases/src/web/WEB-INF/faces-config.xml
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/WEB-INF/faces-config.xml?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/web/WEB-INF/faces-config.xml (original)
+++ struts/shale/trunk/use-cases/src/web/WEB-INF/faces-config.xml Fri Jul  1 18:58:10 2005
@@ -76,6 +76,36 @@
   </navigation-rule>
 
 
+  <!-- ============================= Ajax Examples ========================= -->
+
+  <!-- ViewController Beans -->
+
+  <managed-bean>
+    <managed-bean-name>ajax$completion</managed-bean-name>
+    <managed-bean-class>
+      org.apache.shale.usecases.ajax.Completion
+    </managed-bean-class>
+    <managed-bean-scope>request</managed-bean-scope>
+  </managed-bean>
+
+  <!-- Navigation Rules -->
+
+  <navigation-rule>
+    <from-view-id>*</from-view-id>
+    <navigation-case>
+      <from-outcome>ajax$completion</from-outcome>
+      <to-view-id>/ajax/completion.jsp</to-view-id>
+    </navigation-case>
+  </navigation-rule>
+  <navigation-rule>
+    <from-view-id>/ajax/completion.jsp</from-view-id>
+    <navigation-case>
+      <from-outcome>exit</from-outcome>
+      <to-view-id>/usecases.jsp</to-view-id>
+    </navigation-case>
+  </navigation-rule>
+
+
   <!-- ============================== JNDI Test ============================ -->
 
 

Added: struts/shale/trunk/use-cases/src/web/ajax/completion.jsp
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/ajax/completion.jsp?rev=208814&view=auto
==============================================================================
--- struts/shale/trunk/use-cases/src/web/ajax/completion.jsp (added)
+++ struts/shale/trunk/use-cases/src/web/ajax/completion.jsp Fri Jul  1 18:58:10 2005
@@ -0,0 +1,169 @@
+<%@page contentType="text/html;charset=UTF-8"%>
+<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
+<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
+<%@ taglib prefix="s" uri="http://struts.apache.org/shale/core" %>
+
+<%--
+
+ Copyright 2004-2005 The Apache Software Foundation.
+ 
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ 
+      http://www.apache.org/licenses/LICENSE-2.0
+ 
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--%>
+
+<f:view>
+<%@include              file="../messages.jspf"%>
+<html>
+<head>
+<title>
+  <h:outputText        value="#{messages['ajax.completion.title']}"/>
+</title>
+<script                 type="text/javascript">
+
+  // Global variables initialized at page load time
+  var name;                  // Element for the auto-completion field (name)
+  var table;                 // Table element containing the available names
+
+  // Global variables initialized per keystroke
+  var isIE;                  // Set to "true" for IE
+  var request;               // Platform dependent XMLHttpRequest instance
+
+  // append() - Append a state abbreviation and name to the table
+  function append(abbreviation, name) {
+    if (isIE) {
+      row = table.insertRow(table.rows.length);
+    } else {
+      row = document.createElement("tr");
+      table.appendChild(row);
+    }
+    col = document.createElement("td");
+    col.appendChild(document.createTextNode(abbreviation));
+    row.appendChild(col);
+    col = document.createElement("td");
+    col.appendChild(document.createTextNode(name));
+    row.appendChild(col);
+  }
+
+  // clear() - Clear the available names table
+  function clear() {
+    // Clear all the children (rows) of the table
+    for (i = table.childNodes.length - 1; i >= 0; i--) {
+      table.removeChild(table.childNodes[i]);
+    }
+    // Add back the column headers
+    if (isIE) {
+      row = table.insertRow(table.rows.length);
+    } else {
+      row = document.createElement("tr");
+      table.appendChild(row);
+    }
+    head = document.createElement("th");
+    head.appendChild(document.createTextNode("Abbr"));
+    row.appendChild(head);
+    head = document.createElement("th");
+    head.appendChild(document.createTextNode("State Name"));
+    row.appendChild(head);
+  }
+
+  // complete() - Perform completion processing on each keystroke
+  function complete() {
+    alert("complete()");
+    var url = "list/stateNames.remote?prefix=" + escape(name.value);
+    setup();
+    request.onreadystatechange = process;
+    request.open("GET", url, true);
+    request.send(null);
+  }
+
+  // init() - Initialize variables on page load
+  function init() {
+    name = document.getElementById("form:name");
+    table = document.getElementById("table");
+  }
+
+  // parse() - Parse the response and repopulate the available options table
+  function parse() {
+    clear();
+    states = request.responseXML.getElementsByTagName("items")[0];
+    for (i = 0; i < states.childNodes.length; i++) {
+      abbreviation = states.getElementsByTagName("value")[0];
+      name = states.getElementsByTagName("label")[0];
+      append(abbreviation.childNodes[0].nodeValue, name.childNodes[0].nodeValue);
+    }
+  }
+
+  // process() - Callback function to handle asynchronous response
+  function process() {
+    if (request.readyState == 4) {
+      if (request.status == 200) {
+        parse();
+      } else if (request.status == 204) {
+        alert("Clearing the table");
+        clear();
+      }
+    }
+  }
+
+  // setup() - Set up XMLHttpRequest object and related variables
+  function setup() {
+    if (window.XMLHttpRequest) {
+      isIE = false;
+      request = new XMLHttpRequest();
+    } else if (window.ActiveXObject) {
+      isIE = true;
+      request = new ActiveXObject("Microsoft.XMLHTTP");
+    }
+  }
+
+</script>
+</head>
+<body                 onload="init()">
+
+  <h:form                 id="form">
+
+    <h:panelGrid          id="grid"
+                     columns="3">
+
+      <h:outputText    value="#{messages['ajax.completion.prompt']}"/>
+      <h:inputText        id="name"
+                     onkeyup="complete();"
+                       value="#{ajax$completion.name}"/>
+      <f:verbatim>
+        <table            id="table"
+                      border="0">
+          <tr             id="header">
+            <th>Abbr</th>
+            <th>State Name</th>
+          </tr>
+        </table>
+      </f:verbatim>
+
+      <h:outputText       id="result"
+                       value="#{ajax$completion.result}"/>
+      <h:commandButton    id="submit"
+                       value="#{messages['ajax.completion.submit']}"
+                      action="#{ajax$completion.submit}"/>
+      <h:commandButton    id="finish"
+                       value="#{messages['ajax.completion.finish']}"
+                      action="exit"/>
+
+      <h:messages         id="messages"
+                  globalOnly="false"/>
+
+    </h:panelGrid>
+
+  </h:form>
+
+</body>
+</html>
+</f:view>

Modified: struts/shale/trunk/use-cases/src/web/lookup/listCategories.jsp
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/lookup/listCategories.jsp?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/web/lookup/listCategories.jsp (original)
+++ struts/shale/trunk/use-cases/src/web/lookup/listCategories.jsp Fri Jul  1 18:58:10 2005
@@ -26,14 +26,14 @@
   <jsp:directive.page
                   contentType="text/xml;charset=UTF-8"/>
 
-  <categories>
+  <items>
     <c:forEach            var="category"
                         items="${lookup$listCategories.supportedCategories}">
-      <category>
+      <item>
         <label><c:out value="${category.label}"/></label>
         <value><c:out value="${category.value}"/></value>
-      </category>
+      </item>
     </c:forEach>
-  </categories>
+  </items>
 
 </jsp:root>

Modified: struts/shale/trunk/use-cases/src/web/lookup/listLocales.jsp
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/lookup/listLocales.jsp?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/web/lookup/listLocales.jsp (original)
+++ struts/shale/trunk/use-cases/src/web/lookup/listLocales.jsp Fri Jul  1 18:58:10 2005
@@ -26,14 +26,14 @@
   <jsp:directive.page
                   contentType="text/xml;charset=UTF-8"/>
 
-  <locales>
+  <items>
     <c:forEach            var="locale"
                         items="${lookup$listLocales.supportedLocales}">
-      <locale>
+      <item>
         <label><c:out value="${locale.label}"/></label>
         <value><c:out value="${locale.value}"/></value>
-      </locale>
+      </item>
     </c:forEach>
-  </locales>
+  </items>
 
 </jsp:root>

Modified: struts/shale/trunk/use-cases/src/web/usecases.jsp
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/usecases.jsp?rev=208814&r1=208813&r2=208814&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/web/usecases.jsp (original)
+++ struts/shale/trunk/use-cases/src/web/usecases.jsp Fri Jul  1 18:58:10 2005
@@ -109,9 +109,26 @@
 
   </h:panelGrid>
 
+  <h1><h:outputText     value="#{messages['usecases.ajax']}"/></h1>
+
+  <h:panelGrid        columns="1">
+
+    <h:commandLink         id="ajaxCodeCompletion"
+                       action="ajax$completion">
+      <h:outputText     value="#{messages['usecases.completion']}"/>
+    </h:commandLink>
+
+  </h:panelGrid>
+
   <h1><h:outputText     value="#{messages['usecases.java']}"/></h1>
 
   <h:panelGrid        columns="1">
+
+    <h:outputLink          id="javaStateNames"
+                        value="list/stateNames.remote"
+                       target="_new">
+      <h:outputText     value="#{messages['usecases.states']}"/>
+    </h:outputLink>
 
     <h:outputLink          id="javaCategories"
                         value="list/supportedCategories.remote"



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org