You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by dr...@apache.org on 2010/08/01 14:49:23 UTC

svn commit: r981216 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/services/ main/java/org/apache/tapestry5/services/ test/app1/ test/java/org/apache/tapestry5/integration/app1/ test/java/org/apache/tapestry5/...

Author: drobiazko
Date: Sun Aug  1 12:49:22 2010
New Revision: 981216

URL: http://svn.apache.org/viewvc?rev=981216&view=rev
Log:
TAP5-1098: Provide new SelectModelFactory service that can automatically build a standard SelectModel from objects and property names

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/SelectModelFactoryImpl.java   (with props)
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/SelectModelFactory.java   (with props)
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SelectModelFromObjectsAndPropertyNameDemo.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SelectModelFromObjectsAndPropertyNameDemo.java   (with props)
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/SelectModelFactoryImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/SelectModelFactoryImpl.java?rev=981216&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/SelectModelFactoryImpl.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/SelectModelFactoryImpl.java Sun Aug  1 12:49:22 2010
@@ -0,0 +1,65 @@
+// Copyright 2010 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.services;
+
+import java.util.List;
+
+import org.apache.tapestry5.OptionModel;
+import org.apache.tapestry5.SelectModel;
+import org.apache.tapestry5.ValueEncoder;
+import org.apache.tapestry5.internal.OptionModelImpl;
+import org.apache.tapestry5.internal.SelectModelImpl;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry5.ioc.services.ClassPropertyAdapter;
+import org.apache.tapestry5.ioc.services.PropertyAccess;
+import org.apache.tapestry5.ioc.services.PropertyAdapter;
+import org.apache.tapestry5.services.SelectModelFactory;
+import org.apache.tapestry5.services.ValueEncoderSource;
+
+public class SelectModelFactoryImpl implements SelectModelFactory
+{
+    private final PropertyAccess propertyAccess;
+    private final ValueEncoderSource valueEncoderSource;
+
+    public SelectModelFactoryImpl(final PropertyAccess propertyAccess,
+            final ValueEncoderSource valueEncoderSource)
+    {
+        super();
+        this.propertyAccess = propertyAccess;
+        this.valueEncoderSource = valueEncoderSource;
+    }
+
+    @SuppressWarnings("unchecked")
+    public SelectModel create(final List<?> objects, final String labelProperty)
+    {
+        final List<OptionModel> options = CollectionFactory.newList();
+
+        for (final Object object : objects)
+        {
+            final ClassPropertyAdapter classPropertyAdapter = this.propertyAccess
+                    .getAdapter(object);
+
+            final PropertyAdapter propertyAdapter = classPropertyAdapter.getPropertyAdapter(labelProperty);
+
+            final ValueEncoder encoder = this.valueEncoderSource.getValueEncoder(propertyAdapter.getType());
+
+            final Object label = propertyAdapter.get(object);
+
+            options.add(new OptionModelImpl(encoder.toClient(label), object));
+
+        }
+
+        return new SelectModelImpl(null, options);
+    }
+}

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/SelectModelFactoryImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/SelectModelFactoryImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/SelectModelFactory.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/SelectModelFactory.java?rev=981216&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/SelectModelFactory.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/SelectModelFactory.java Sun Aug  1 12:49:22 2010
@@ -0,0 +1,37 @@
+// Copyright 2010 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.services;
+
+import java.util.List;
+
+import org.apache.tapestry5.SelectModel;
+
+/**
+ * Used to create an {@link org.apache.tapestry5.SelectModel}.
+ * 
+ * @since 5.2.0
+ */
+public interface SelectModelFactory
+{   
+    /**
+     * Creates a {@link org.apache.tapestry5.SelectModel} from a list of objects of the same type and a label property name.
+     * The returned model creates for every object in the list a selectable option and relies on existing 
+     * {@link org.apache.tapestry5.ValueEncoder} for the object type. The value of the label property is used as user-presentable label for the option.
+     * 
+     * @param objects objects to create model from
+     * @param labelProperty property for the client-side value
+     * @return the model
+     */
+    public SelectModel create(List<?> objects, String labelProperty);
+}

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/SelectModelFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/SelectModelFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=981216&r1=981215&r2=981216&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 Sun Aug  1 12:49:22 2010
@@ -387,6 +387,7 @@ public final class TapestryModule
         binder.bind(TranslatorAlternatesSource.class, TranslatorAlternatesSourceImpl.class);
         binder.bind(MetaWorker.class, MetaWorkerImpl.class);
         binder.bind(LinkTransformer.class, LinkTransformerImpl.class);
+        binder.bind(SelectModelFactory.class, SelectModelFactoryImpl.class);
     }
 
     // ========================================================================

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SelectModelFromObjectsAndPropertyNameDemo.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SelectModelFromObjectsAndPropertyNameDemo.tml?rev=981216&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SelectModelFromObjectsAndPropertyNameDemo.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SelectModelFromObjectsAndPropertyNameDemo.tml Sun Aug  1 12:49:22 2010
@@ -0,0 +1,20 @@
+<t:border xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
+	<t:form clientValidation="false">
+		<p>
+			<t:errors />
+		</p>
+		<p>
+			<t:select t:id="track" model="model" />
+
+		</p>
+
+		<p>
+			<t:submit value="literal:Submit" />
+		</p>
+	</t:form>
+	
+	<t:if test="track">
+		<p> Selected track: ${track.title}, ${track.album}</p>
+	</t:if>
+
+</t:border>
\ No newline at end of file

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java?rev=981216&r1=981215&r2=981216&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java Sun Aug  1 12:49:22 2010
@@ -887,4 +887,19 @@ public class FormTests extends TapestryC
 
         assertTextPresent("Selected color: Green");
     }
+    
+    /**
+     * TAP5-1098.
+     */
+    @Test
+    public void create_select_model_from_objects_and_property_name() throws Exception
+    {
+        clickThru("SelectModel from objects and property name");
+
+        select("track", "label=The Calling");
+
+        clickAndWait(SUBMIT);
+
+        assertTextPresent("Selected track: The Calling, Synaesthetic");
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java?rev=981216&r1=981215&r2=981216&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java Sun Aug  1 12:49:22 2010
@@ -445,7 +445,10 @@ public class Index
 
                     new Item("discardafterdemo", "@DiscardAfter Demo", "Demo using @DiscardAfter annotation"),
                     
-                    new Item("SelectDemo", "Select Demo", "Validation decoration for Select")
+                    new Item("SelectDemo", "Select Demo", "Validation decoration for Select"),
+                    
+                    new Item("SelectModelFromObjectsAndPropertyNameDemo", "SelectModel from objects and property name", 
+                            "Creating a SelectModel from a list of objects and a label property name")
 
             );
 

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SelectModelFromObjectsAndPropertyNameDemo.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SelectModelFromObjectsAndPropertyNameDemo.java?rev=981216&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SelectModelFromObjectsAndPropertyNameDemo.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SelectModelFromObjectsAndPropertyNameDemo.java Sun Aug  1 12:49:22 2010
@@ -0,0 +1,45 @@
+// Copyright 2010 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 org.apache.tapestry5.SelectModel;
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.annotations.Property;
+import org.apache.tapestry5.integration.app1.data.Track;
+import org.apache.tapestry5.integration.app1.services.MusicLibrary;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.services.SelectModelFactory;
+
+public class SelectModelFromObjectsAndPropertyNameDemo
+{
+    @Inject
+    private MusicLibrary library;
+    
+    @Inject
+    private SelectModelFactory modelFactory;
+    
+    @Property
+    private SelectModel model;
+    
+    @Property
+    @Persist
+    private Track track;
+    
+    void onPrepare()
+    {
+        model = modelFactory.create(library.getTracks(), "title");
+    }
+    
+    
+}

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SelectModelFromObjectsAndPropertyNameDemo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SelectModelFromObjectsAndPropertyNameDemo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain