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 2007/02/15 17:30:19 UTC

svn commit: r507989 - in /tapestry/tapestry5/tapestry-core/trunk/src: main/java/org/apache/tapestry/ main/java/org/apache/tapestry/corelib/components/ main/java/org/apache/tapestry/corelib/data/ main/java/org/apache/tapestry/services/ main/resources/or...

Author: hlship
Date: Thu Feb 15 08:30:18 2007
New Revision: 507989

URL: http://svn.apache.org/viewvc?view=rev&rev=507989
Log:
Provide control over the Grid's pager position: top, bottom, both or none.

Added:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/StringToEnumCoercion.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/data/
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/data/GridPagerPosition.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/StringToEnumCoercionTest.java
Modified:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/TapestryMessages.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Grid.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridPager.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/TapestryStrings.properties
    tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/corelib/components/Grid.html
    tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/default.css
    tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/validation.apt

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/StringToEnumCoercion.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/StringToEnumCoercion.java?view=auto&rev=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/StringToEnumCoercion.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/StringToEnumCoercion.java Thu Feb 15 08:30:18 2007
@@ -0,0 +1,67 @@
+// Copyright 2007 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.tapestry;
+
+import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
+
+import java.util.Map;
+
+import org.apache.tapestry.ioc.internal.util.InternalUtils;
+import org.apache.tapestry.ioc.services.Coercion;
+
+/**
+ * A {@link Coercion} for converting strings into an instance of a particular enumerated type. The
+ * {@link Enum#name() name} is used as the key to identify the enum instance, in a case-insensitive
+ * fashion.
+ * 
+ * @param <T>
+ *            the type of enumeration
+ */
+public final class StringToEnumCoercion<T extends Enum> implements Coercion<String, T>
+{
+    private final Class<T> _enumClass;
+
+    private final Map<String, T> _stringToEnum = newCaseInsensitiveMap();
+
+    public StringToEnumCoercion(Class<T> enumClass)
+    {
+        this(enumClass, enumClass.getEnumConstants());
+    }
+
+    public StringToEnumCoercion(Class<T> enumClass, T... values)
+    {
+        _enumClass = enumClass;
+
+        for (T value : values)
+            _stringToEnum.put(value.name(), value);
+    }
+
+    public T coerce(String input)
+    {
+        if (InternalUtils.isBlank(input))
+            return null;
+
+        T result = _stringToEnum.get(input);
+
+        if (result == null)
+            throw new RuntimeException(TapestryMessages.missingEnumValue(
+                    input,
+                    _enumClass,
+                    _stringToEnum.keySet()));
+
+        return result;
+    }
+
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/TapestryMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/TapestryMessages.java?view=diff&rev=507989&r1=507988&r2=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/TapestryMessages.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/TapestryMessages.java Thu Feb 15 08:30:18 2007
@@ -33,4 +33,9 @@
     {
         return MESSAGES.format("missing-value", value, InternalUtils.joinSorted(values));
     }
+
+    static String missingEnumValue(String value, Class enumClass, Collection<String> values)
+    {
+        return MESSAGES.format("missing-enum-value", value, enumClass.getName(), InternalUtils.joinSorted(values));
+    }
 }

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Grid.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Grid.java?view=diff&rev=507989&r1=507988&r2=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Grid.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Grid.java Thu Feb 15 08:30:18 2007
@@ -24,6 +24,7 @@
 import org.apache.tapestry.annotations.SupportsInformalParameters;
 import org.apache.tapestry.beaneditor.BeanModel;
 import org.apache.tapestry.beaneditor.PropertyModel;
+import org.apache.tapestry.corelib.data.GridPagerPosition;
 import org.apache.tapestry.grid.GridDataSource;
 import org.apache.tapestry.grid.GridModelProvider;
 import org.apache.tapestry.internal.bindings.AbstractBinding;
@@ -44,6 +45,10 @@
     @Parameter
     private int _rowsPerPage = 25;
 
+    /** Literal strings will be coerced into a position value. */
+    @Parameter(defaultPrefix = "literal")
+    private GridPagerPosition _pagerPosition = GridPagerPosition.BOTTOM;
+
     @Persist
     private int _currentPage = 1;
 
@@ -89,11 +94,18 @@
             "row=row" })
     private GridRows _rows;
 
-    @SuppressWarnings("unused")
     @Component(parameters =
     { "source=dataSource", "rowsPerPage=rowsPerPage", "currentPage=currentPage" })
     private GridPager _pager;
 
+    @SuppressWarnings("unused")
+    @Component(parameters = "to=pagerTop")
+    private Delegate _pagerTop;
+
+    @SuppressWarnings("unused")
+    @Component(parameters = "to=pagerBottom")
+    private Delegate _pagerBottom;
+
     Binding defaultModel()
     {
         final ComponentResources containerResources = _resources.getContainerResources();
@@ -240,4 +252,13 @@
         _sortColumnId = sortColumnId;
     }
 
+    public Object getPagerTop()
+    {
+        return _pagerPosition.isMatchTop() ? _pager : null;
+    }
+
+    public Object getPagerBottom()
+    {
+        return _pagerPosition.isMatchBottom() ? _pager : null;
+    }
 }

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridPager.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridPager.java?view=diff&rev=507989&r1=507988&r2=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridPager.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridPager.java Thu Feb 15 08:30:18 2007
@@ -35,7 +35,7 @@
     private int _currentPage;
 
     /** Number of pages before and after the current page in the range. */
-    private int _range = 3;
+    private int _range = 5;
 
     private int _lastIndex;
 

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/data/GridPagerPosition.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/data/GridPagerPosition.java?view=auto&rev=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/data/GridPagerPosition.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/data/GridPagerPosition.java Thu Feb 15 08:30:18 2007
@@ -0,0 +1,56 @@
+// Copyright 2007 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.tapestry.corelib.data;
+
+import org.apache.tapestry.corelib.components.Grid;
+
+/**
+ * Used by the {@link Grid} component to control where the pager portion of the Grid should be
+ * displayed.
+ */
+public enum GridPagerPosition {
+    /** Position the pager above the Grid's table. */
+    TOP(true, false),
+
+    /** Position the pager below the Grid's table (this is the default). */
+    BOTTOM(false, true),
+
+    /** Show the pager above and below the Grid's table. */
+    BOTH(true, true),
+
+    /** Don't show a pager (the application will need to supply its own navigation mechanism). */
+    NONE(false, false);
+
+    private final boolean _matchTop;
+
+    private final boolean _matchBottom;
+
+    private GridPagerPosition(boolean matchTop, boolean matchBottom)
+    {
+        _matchTop = matchTop;
+        _matchBottom = matchBottom;
+    }
+
+    public boolean isMatchBottom()
+    {
+        return _matchBottom;
+    }
+
+    public boolean isMatchTop()
+    {
+        return _matchTop;
+    }
+
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java?view=diff&rev=507989&r1=507988&r2=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java Thu Feb 15 08:30:18 2007
@@ -30,6 +30,7 @@
 import org.apache.tapestry.MarkupWriter;
 import org.apache.tapestry.SelectModel;
 import org.apache.tapestry.StreamResponse;
+import org.apache.tapestry.StringToEnumCoercion;
 import org.apache.tapestry.Translator;
 import org.apache.tapestry.Validator;
 import org.apache.tapestry.annotations.AfterRender;
@@ -45,6 +46,7 @@
 import org.apache.tapestry.annotations.PageLoaded;
 import org.apache.tapestry.annotations.SetupRender;
 import org.apache.tapestry.beaneditor.Validate;
+import org.apache.tapestry.corelib.data.GridPagerPosition;
 import org.apache.tapestry.dom.DefaultMarkupModel;
 import org.apache.tapestry.dom.Document;
 import org.apache.tapestry.grid.GridDataSource;
@@ -1194,6 +1196,7 @@
      * <li>String to {@link SelectModel}
      * <li>List to {@link GridDataSource}
      * <li>null to {@link GridDataSource}
+     * <li>String to {@link GridPagerPosition}
      * </ul>
      */
     @Contribute("tapestry.ioc.TypeCoercer")
@@ -1225,6 +1228,11 @@
             }
         });
 
+        add(
+                configuration,
+                String.class,
+                GridPagerPosition.class,
+                new StringToEnumCoercion<GridPagerPosition>(GridPagerPosition.class));
     }
 
     private static <S, T> void add(Configuration<CoercionTuple> configuration, Class<S> sourceType,

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/TapestryStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/TapestryStrings.properties?view=diff&rev=507989&r1=507988&r2=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/TapestryStrings.properties (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/TapestryStrings.properties Thu Feb 15 08:30:18 2007
@@ -13,4 +13,5 @@
 # limitations under the License.
 
 duplicate-key=Key %s may not be added with value %s, as an existing value, %s, is already present.
-missing-value=Key for value %s not found. Available values: %s
\ No newline at end of file
+missing-value=Key for value %s not found. Available values: %s
+missing-enum-value=Input '%s' does not identify a value from enumerated type %s. Available values: %s.
\ No newline at end of file

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/corelib/components/Grid.html
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/corelib/components/Grid.html?view=diff&rev=507989&r1=507988&r2=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/corelib/components/Grid.html (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/corelib/components/Grid.html Thu Feb 15 08:30:18 2007
@@ -1,4 +1,7 @@
 <div class="t-data-grid" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+	
+	<t:comp id="pagerTop"/>
+	
 	<table class="t-data-grid" cellspacing="0">
 		<thead t:id="columns"/>
 		<tbody>
@@ -6,7 +9,11 @@
 		</tbody>
 	</table>
 
-	<t:comp id="pager"/>
+	<t:comp id="pagerBottom"/>
+	
+	<t:block>
+		<t:comp id="pager"/>
+	</t:block>	
 
 	<t:block id="empty"> There is no data to display. </t:block>
 

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/default.css
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/default.css?view=diff&rev=507989&r1=507988&r2=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/default.css (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/default.css Thu Feb 15 08:30:18 2007
@@ -188,7 +188,7 @@
 
 DIV.t-data-grid-pager
 {
-  margin-top: 8px;
+  margin: 8px 0px;
 }
 
 DIV.t-data-grid-pager A, DIV.t-data-grid-pager SPAN.current

Modified: tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/validation.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/validation.apt?view=diff&rev=507989&r1=507988&r2=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/validation.apt (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/validation.apt Thu Feb 15 08:30:18 2007
@@ -166,7 +166,7 @@
             <input t:type="TextField" t:id="userName" t:validate="required,minlength=3" size="30"/>
             <br/>
             <label t:type="Label" for="password"/>:
-            <input t:id="password" t:validate="required,minlength=3" size="30"/>
+            <input t:type="PasswordField" t:id="password" t:validate="required,minlength=3" size="30"/>
             <br/>
             <input type="submit" value="Login"/>
         </form>

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/StringToEnumCoercionTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/StringToEnumCoercionTest.java?view=auto&rev=507989
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/StringToEnumCoercionTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/StringToEnumCoercionTest.java Thu Feb 15 08:30:18 2007
@@ -0,0 +1,58 @@
+// Copyright 2007 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.tapestry;
+
+import junit.framework.AssertionFailedError;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class StringToEnumCoercionTest extends Assert
+{
+    @Test
+    public void value_found()
+    {
+        StringToEnumCoercion<Stooge> coercion = new StringToEnumCoercion<Stooge>(Stooge.class);
+
+        assertSame(coercion.coerce("moe"), Stooge.MOE);
+        assertSame(coercion.coerce("curly_joe"), Stooge.CURLY_JOE);
+    }
+
+    @Test
+    public void blank_is_null()
+    {
+        StringToEnumCoercion<Stooge> coercion = new StringToEnumCoercion<Stooge>(Stooge.class);
+
+        assertNull(coercion.coerce(""));
+    }
+
+    @Test
+    public void value_not_found()
+    {
+        StringToEnumCoercion<Stooge> coercion = new StringToEnumCoercion<Stooge>(Stooge.class);
+
+        try
+        {
+            coercion.coerce("shemp");
+            throw new AssertionFailedError("unreachable");
+        }
+        catch (RuntimeException ex)
+        {
+            assertEquals(
+                    ex.getMessage(),
+                    "Input \'shemp\' does not identify a value from enumerated type org.apache.tapestry.Stooge. Available values: CURLY_JOE, LARRY, MOE.");
+        }
+    }
+}