You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/08/08 03:08:46 UTC

svn commit: r683782 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/ main/java/org/apache/tapestry5/corelib/components/ main/java/org/apache/tapestry5/internal/util/ main/java/org/apache/tapestry5/services/ main/resourc...

Author: hlship
Date: Thu Aug  7 18:08:45 2008
New Revision: 683782

URL: http://svn.apache.org/viewvc?rev=683782&view=rev
Log:
TAPESTRY-2509: Add type coercions from String to Renderable, Block

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/RenderableAsBlock.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/StringRenderable.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/Block.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Palette.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/PaletteDemo.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/Block.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/Block.java?rev=683782&r1=683781&r2=683782&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/Block.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/Block.java Thu Aug  7 18:08:45 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 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.
@@ -18,6 +18,11 @@
  * A block is a collection of static text and elements, and components, derived from a component template. In the
  * template, a block is demarcated using the <t:block> or <t:parameter> elements. The interface defines no
  * methods, but the provided implementations of Block are capable of rendering their contents on demand.
+ * <p/>
+ * Tapestry includes coecions from String to {@link org.apache.tapestry5.Renderable} and {@link
+ * org.apache.tapestry5.Renderable} to Block. This means that components that take Block parameters may be bound to
+ * literal strings, to arbitrary numbers (or other objects, with the expectation that they will be converted to
+ * strings), or to renderable objects such as components.
  */
 public interface Block
 {

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java?rev=683782&r1=683781&r2=683782&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java Thu Aug  7 18:08:45 2008
@@ -176,6 +176,24 @@
     private SelectModel model;
 
     /**
+     * Allows the title text for the available column (on the left) to be modified. As this is a Block, it can contain
+     * conditionals and components. The default is the text "Available".
+     */
+    @Property(write = false)
+    @Parameter(required = true, allowNull = false, value = "message:available-label",
+               defaultPrefix = BindingConstants.LITERAL)
+    private Block availableLabel;
+
+    /**
+     * Allows the title text for the selected column (on the right) to be modified. As this is a Block, it can contain
+     * conditionals and components. The default is the text "Available".
+     */
+    @Property(write = false)
+    @Parameter(required = true, allowNull = false, value = "message:selected-label",
+               defaultPrefix = BindingConstants.LITERAL)
+    private Block selectedLabel;
+
+    /**
      * The image to use for the move down button (the default is a downward pointing arrow).
      */
     @Parameter(value = "asset:move_down.png")

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/RenderableAsBlock.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/RenderableAsBlock.java?rev=683782&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/RenderableAsBlock.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/RenderableAsBlock.java Thu Aug  7 18:08:45 2008
@@ -0,0 +1,44 @@
+//  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.internal.util;
+
+import org.apache.tapestry5.Block;
+import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.Renderable;
+import org.apache.tapestry5.runtime.RenderCommand;
+import org.apache.tapestry5.runtime.RenderQueue;
+
+/**
+ * Allows any {@link org.apache.tapestry5.Renderable} object to act as a {@link org.apache.tapestry5.Block}. Basically,
+ * dressed up the Renderable with the Block interface, and delegates the {@link org.apache.tapestry5.Renderable}
+ * interface to the underlying renderable object.
+ */
+public class RenderableAsBlock implements Block, RenderCommand
+{
+    private final Renderable renderable;
+
+    public RenderableAsBlock(Renderable renderable)
+    {
+        this.renderable = renderable;
+    }
+
+    /**
+     * Invokes {@link Renderable#render(org.apache.tapestry5.MarkupWriter)}.
+     */
+    public void render(MarkupWriter writer, RenderQueue queue)
+    {
+        renderable.render(writer);
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/StringRenderable.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/StringRenderable.java?rev=683782&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/StringRenderable.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/StringRenderable.java Thu Aug  7 18:08:45 2008
@@ -0,0 +1,36 @@
+//  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.internal.util;
+
+import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.Renderable;
+
+/**
+ * Renders a string using {@link MarkupWriter#write(String)}.
+ */
+public class StringRenderable implements Renderable
+{
+    private final String text;
+
+    public StringRenderable(String text)
+    {
+        this.text = text;
+    }
+
+    public void render(MarkupWriter writer)
+    {
+        writer.write(text);
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=683782&r1=683781&r2=683782&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java Thu Aug  7 18:08:45 2008
@@ -33,6 +33,8 @@
 import org.apache.tapestry5.internal.transform.*;
 import org.apache.tapestry5.internal.translator.*;
 import org.apache.tapestry5.internal.util.IntegerRange;
+import org.apache.tapestry5.internal.util.RenderableAsBlock;
+import org.apache.tapestry5.internal.util.StringRenderable;
 import org.apache.tapestry5.ioc.*;
 import org.apache.tapestry5.ioc.annotations.*;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
@@ -623,7 +625,8 @@
      * org.apache.tapestry5.SelectModel} <li>{@link org.apache.tapestry5.runtime.ComponentResourcesAware} (typically, a
      * component) to {@link org.apache.tapestry5.ComponentResources} <li>String to {@link
      * org.apache.tapestry5.corelib.data.BlankOption} <li> {@link org.apache.tapestry5.ComponentResources} to {@link
-     * org.apache.tapestry5.PropertyOverrides} </ul>
+     * org.apache.tapestry5.PropertyOverrides} <li>String to {@link org.apache.tapestry5.Renderable} <li>{@link
+     * org.apache.tapestry5.Renderable} to {@link org.apache.tapestry5.Block}</ul>
      */
     public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration)
     {
@@ -704,6 +707,22 @@
                     return input.getComponentResources();
                 }
             });
+
+        add(configuration, String.class, Renderable.class, new Coercion<String, Renderable>()
+        {
+            public Renderable coerce(String input)
+            {
+                return new StringRenderable(input);
+            }
+        });
+
+        add(configuration, Renderable.class, Block.class, new Coercion<Renderable, Block>()
+        {
+            public Block coerce(Renderable input)
+            {
+                return new RenderableAsBlock(input);
+            }
+        });
     }
 
     /**

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Palette.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Palette.tml?rev=683782&r1=683781&r2=683782&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Palette.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Palette.tml Thu Aug  7 18:08:45 2008
@@ -1,6 +1,8 @@
 <div class="t-palette" xml:space="default" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
     <div class="t-palette-available">
-        <div class="t-palette-title">${message:available-label}</div>
+        <div class="t-palette-title">
+            <t:delegate to="availableLabel"/>
+        </div>
         <t:delegate to="availableRenderer"/>
     </div>
     <div class="t-palette-controls">
@@ -20,7 +22,9 @@
         </t:if>
     </div>
     <div class="t-palette-selected">
-        <div class="t-palette-title">${message:selected-label}</div>
+        <div class="t-palette-title">
+            <t:delegate to="selectedLabel"/>
+        </div>
         <t:delegate to="selectedRenderer"/>
     </div>
     <div class="t-palette-spacer"/>

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/PaletteDemo.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/PaletteDemo.tml?rev=683782&r1=683781&r2=683782&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/PaletteDemo.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/PaletteDemo.tml Thu Aug  7 18:08:45 2008
@@ -1,26 +1,35 @@
 <html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
 
-  <h1>Palette Demo</h1>
+    <h1>Palette Demo</h1>
 
 
-  <t:form>
-    <div>
-      <t:checkbox t:id="reorder" label="Enable Reorder"/>
-      <t:label for="reorder"/>
-    </div>
-
-    <div class="t-beaneditor">
-
-
-      <t:palette t:id="languages" model="languageModel" reorder="reorder" encoder="languageEncoder"/>
-      <br/>
-      <input type="submit"/>
-    </div>
-  </t:form>
-
-  <p> Selected Languages: ${languages} </p>
-  
-  <p>
-    [ <t:actionlink t:id="reset">reset</t:actionlink> ]
-  </p>
+    <t:form>
+        <div>
+            <t:checkbox t:id="reorder" label="Enable Reorder"/>
+            <t:label for="reorder"/>
+        </div>
+
+        <div class="t-beaneditor">
+
+
+            <t:palette t:id="languages" model="languageModel" reorder="reorder" encoder="languageEncoder"
+                       availableLabel="Languages Offered">
+                <t:parameter name="selectedLabel">
+                    Selected
+                    <t:if test="reorder">/Ranked</t:if>
+                    Languages
+                </t:parameter>
+            </t:palette>
+            <br/>
+            <input type="submit"/>
+        </div>
+    </t:form>
+
+    <p>Selected Languages: ${languages}</p>
+
+    <p>
+        [
+        <t:actionlink t:id="reset">reset</t:actionlink>
+        ]
+    </p>
 </html>

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=683782&r1=683781&r2=683782&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 Thu Aug  7 18:08:45 2008
@@ -948,6 +948,9 @@
     {
         start("Palette Demo", "reset");
 
+        assertText("//div[@class='t-palette-available']/div[@class='t-palette-title']", "Languages Offered");
+        assertText("//div[@class='t-palette-selected']/div[@class='t-palette-title']", "Selected Languages");
+
         addSelection("languages:avail", "label=Haskell");
         addSelection("languages:avail", "label=Javascript");
         click("languages:select");
@@ -976,6 +979,8 @@
         check("reorder");
         clickAndWait(SUBMIT);
 
+        assertText("//div[@class='t-palette-selected']/div[@class='t-palette-title']", "Selected/Ranked Languages");
+
         addSelection("languages", "label=Ruby");
 
         for (int i = 0; i < 6; i++)