Author: hlship
Date: Fri Oct 31 14:30:49 2008
New Revision: 709575

URL: http://svn.apache.org/viewvc?rev=709575&view=rev
Log:
TAP5-205: Add an empty parameter to the Loop component, to display a message 
when there are no items to loop over

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/EmptyLoopDemo.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EmptyLoopDemo.java
Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Loop.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Loop.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Loop.java?rev=709575&r1=709574&r2=709575&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Loop.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Loop.java
 Fri Oct 31 14:30:49 2008
@@ -17,7 +17,7 @@
 import org.apache.tapestry5.*;
 import org.apache.tapestry5.annotations.*;
 import org.apache.tapestry5.ioc.annotations.Inject;
-import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newList;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.services.FormSupport;
 import org.apache.tapestry5.services.Heartbeat;
 
@@ -239,6 +239,12 @@
     @Parameter
     private int index;
 
+    /**
+     * A Block to render instead of the loop when the source is empty.  The 
default is to render nothing.
+     */
+    @Parameter(defaultPrefix = BindingConstants.LITERAL)
+    private Block empty;
+
     private Iterator<?> iterator;
 
     @Environmental
@@ -249,6 +255,8 @@
     @Inject
     private ComponentResources resources;
 
+    private Block cleanupBlock;
+
 
     String defaultElement()
     {
@@ -260,24 +268,21 @@
     {
         index = 0;
 
-        if (source == null) return false;
-
-        iterator = source.iterator();
+        iterator = source == null ? null : source.iterator();
 
         storeRenderStateInForm = formSupport != null && !volatileState;
 
         // Only render the body if there is something to iterate over
 
-        boolean result = iterator.hasNext();
+        boolean hasContent = iterator != null && iterator.hasNext();
 
-        if (formSupport != null && result)
+        if (formSupport != null && hasContent)
         {
-
             formSupport.store(this, volatileState ? SETUP_FOR_VOLATILE : 
RESET_INDEX);
 
             if (encoder != null)
             {
-                List<Serializable> keyList = newList();
+                List<Serializable> keyList = CollectionFactory.newList();
 
                 // We'll keep updating the _keyList while the Loop renders, 
the values will "lock
                 // down" when the Form serializes all the data.
@@ -286,7 +291,20 @@
             }
         }
 
-        return result;
+        cleanupBlock = hasContent ? null : empty;
+
+        // Jump directly to cleanupRender if there is no content
+
+        return hasContent;
+    }
+
+    /**
+     * Returns the empty block, or null, after the render has finished. It 
will only be the empty block (which itself
+     * may be null) if the source was null or empty.
+     */
+    Block cleanupRender()
+    {
+        return cleanupBlock;
     }
 
     private void prepareForKeys(List<Serializable> keys)

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/EmptyLoopDemo.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/EmptyLoopDemo.tml?rev=709575&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/EmptyLoopDemo.tml 
(added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/EmptyLoopDemo.tml Fri 
Oct 31 14:30:49 2008
@@ -0,0 +1,20 @@
+<html t:type="border" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
+
+    <h1>Empty Loop Demo</h1>
+
+
+    <div id="first">
+        <t:loop source="null"/>
+    </div>
+
+    <div id="second">
+        <t:loop source="null" empty="Source is null."/>
+    </div>
+
+    <t:loop source="emptyList">
+        <t:parameter name="empty">
+            <div id="third">Source is the empty list.</div>
+        </t:parameter>
+    </t:loop>
+
+</html>
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java?rev=709575&r1=709574&r2=709575&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
 Fri Oct 31 14:30:49 2008
@@ -2434,4 +2434,16 @@
 
         assertText("activePageName", "nested/AssetDemo");
     }
+
+    /**
+     * TAP5-205
+     */
+    public void handling_of_empty_loop()
+    {
+        start("Empty Loop Demo");
+
+        assertText("first", "");
+        assertText("second", "Source is null.");
+        assertText("third", "Source is the empty list.");
+    }
 }

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EmptyLoopDemo.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EmptyLoopDemo.java?rev=709575&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EmptyLoopDemo.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EmptyLoopDemo.java
 Fri Oct 31 14:30:49 2008
@@ -0,0 +1,23 @@
+//  Copyright 2008 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.tapestry5.integration.app1.pages;
+
+import java.util.Collections;
+import java.util.List;
+
+public class EmptyLoopDemo
+{
+    public List getEmptyList() { return Collections.emptyList(); }
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java?rev=709575&r1=709574&r2=709575&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java
 Fri Oct 31 14:30:49 2008
@@ -65,6 +65,8 @@
 
     private static final List<Item> ITEMS = CollectionFactory.newList(
 
+            new Item("EmptyLoopDemo", "Empty Loop Demo", "Use of empty 
parameter with the Loop component."),
+
             new Item("BlankPasswordDemo", "Blank Password Demo",
                      "Show that a blank value in a PasswordField does not 
update the server side value."),
 


Reply via email to