Author: hlship
Date: Thu Mar 26 19:41:40 2009
New Revision: 758816
URL: http://svn.apache.org/viewvc?rev=758816&view=rev
Log:
TAP5-265: Add a Hidden component, used to synchronize a value between the
server and the client
Added:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java
(with props)
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemo.tml
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemoOutput.tml
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/HiddenTest.java
(with props)
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemo.java
(with props)
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemoOutput.java
(with props)
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDefaultProviderImpl.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/Index.java
Added:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java?rev=758816&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java
Thu Mar 26 19:41:40 2009
@@ -0,0 +1,118 @@
+// Copyright 2009 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.corelib.components;
+
+import org.apache.tapestry5.*;
+import org.apache.tapestry5.annotations.Environmental;
+import org.apache.tapestry5.annotations.Parameter;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.services.ComponentDefaultProvider;
+import org.apache.tapestry5.services.FormSupport;
+import org.apache.tapestry5.services.Request;
+
+/**
+ * Used to record a page property as a value into the form. The value is
{...@linkplain
+ * org.apache.tapestry5.ValueEncoder#toClient(Object) encoded} when rendered,
then decoded when the form is submitted,
+ * and the value parameter updated.
+ *
+ * @since 5.1.0.2
+ */
+public class Hidden
+{
+ /**
+ * The value to read (when rendering) or update (when the form is
submitted).
+ */
+ @Parameter(required = true, autoconnect = true, principal = true)
+ private Object value;
+
+ /**
+ * Value encoder for the value, usually determined automatically from the
type of the property bound to the value
+ * parameter.
+ */
+ @Parameter(required = true)
+ private ValueEncoder encoder;
+
+ private String controlName;
+
+ @Environmental(false)
+ private FormSupport formSupport;
+
+ @Environmental
+ private RenderSupport renderSupport;
+
+ @Inject
+ private ComponentResources resources;
+
+ @Inject
+ private ComponentDefaultProvider defaultProvider;
+
+ @Inject
+ private Request request;
+
+ ValueEncoder defaultEncoder()
+ {
+ return defaultProvider.defaultValueEncoder("value", resources);
+ }
+
+ static class ProcessSubmission implements ComponentAction<Hidden>
+ {
+ private final String controlName;
+
+ public ProcessSubmission(String controlName)
+ {
+ this.controlName = controlName;
+ }
+
+ public void execute(Hidden component)
+ {
+ component.processSubmission(controlName);
+ }
+ }
+
+ boolean beginRender(MarkupWriter writer)
+ {
+ if (formSupport == null)
+ throw new RuntimeException("The Hidden component must be enclosed
by a Form component.");
+
+ controlName = formSupport.allocateControlName(resources.getId());
+
+ formSupport.store(this, new ProcessSubmission(controlName));
+
+ String encoded = encoder.toClient(value);
+
+ writer.element("input",
+ "type", "hidden",
+ "name", controlName,
+ "value", encoded);
+ writer.end();
+
+ return false;
+ }
+
+
+ private void processSubmission(String controlName)
+ {
+ String encoded = request.getParameter(controlName);
+
+ Object decoded = encoder.toClient(encoded);
+
+ value = decoded;
+ }
+
+ public String getControlName()
+ {
+ return controlName;
+ }
+}
Propchange:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDefaultProviderImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDefaultProviderImpl.java?rev=758816&r1=758815&r2=758816&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDefaultProviderImpl.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDefaultProviderImpl.java
Thu Mar 26 19:41:40 2009
@@ -18,8 +18,7 @@
import org.apache.tapestry5.internal.TapestryInternalUtils;
import org.apache.tapestry5.internal.bindings.InvariantBinding;
import org.apache.tapestry5.ioc.Messages;
-import static org.apache.tapestry5.ioc.internal.util.Defense.notBlank;
-import static org.apache.tapestry5.ioc.internal.util.Defense.notNull;
+import org.apache.tapestry5.ioc.internal.util.Defense;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.runtime.Component;
import org.apache.tapestry5.services.*;
@@ -53,7 +52,6 @@
}
};
-
public ComponentDefaultProviderImpl(PropertyAccess propertyAccess,
BindingSource bindingSource,
ValueEncoderSource valueEncoderSource,
FieldTranslatorSource
fieldTranslatorSource,
@@ -68,7 +66,7 @@
public String defaultLabel(ComponentResources resources)
{
- notNull(resources, "resources");
+ Defense.notNull(resources, "resources");
String componentId = resources.getId();
String key = componentId + "-label";
@@ -82,8 +80,8 @@
public Binding defaultBinding(String parameterName, ComponentResources
resources)
{
- notBlank(parameterName, "parameterName");
- notNull(resources, "resources");
+ Defense.notBlank(parameterName, "parameterName");
+ Defense.notNull(resources, "resources");
String componentId = resources.getId();
@@ -105,11 +103,11 @@
componentId);
}
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings({ "unchecked" })
public ValueEncoder defaultValueEncoder(String parameterName,
ComponentResources resources)
{
- notBlank(parameterName, "parameterName");
- notNull(resources, "resources");
+ Defense.notBlank(parameterName, "parameterName");
+ Defense.notNull(resources, "resources");
Class parameterType = resources.getBoundType(parameterName);
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemo.tml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemo.tml?rev=758816&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemo.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemo.tml Thu Mar
26 19:41:40 2009
@@ -0,0 +1,19 @@
+<t:border xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
+ xmlns:p="tapestry:parameter">
+ <h1>Hidden Demo</h1>
+
+ <p>
+ <t:pagelink page="hiddendemo" context="12345">setup</t:pagelink>
+ </p>
+
+ <t:if test="stored">
+
+ <t:form t:id="form">
+ <t:hidden t:id="stored"/>
+ <input type="submit" value="Submit"/>
+ </t:form>
+
+ </t:if>
+
+
+</t:border>
\ No newline at end of file
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemoOutput.tml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemoOutput.tml?rev=758816&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemoOutput.tml
(added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/HiddenDemoOutput.tml
Thu Mar 26 19:41:40 2009
@@ -0,0 +1,8 @@
+<t:border xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
+ xmlns:p="tapestry:parameter">
+ <h1>Hidden Demo (Output)</h1>
+
+ <p>Stored value:
+ <span id="stored">${stored}</span>
+ </p>
+</t:border>
\ No newline at end of file
Added:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/HiddenTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/HiddenTest.java?rev=758816&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/HiddenTest.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/HiddenTest.java
Thu Mar 26 19:41:40 2009
@@ -0,0 +1,37 @@
+// Copyright 2009 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.corelib.components;
+
+import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import org.testng.annotations.Test;
+
+public class HiddenTest extends InternalBaseTestCase
+{
+ @Test
+ public void no_form()
+ {
+ Hidden hidden = new Hidden();
+
+ try
+ {
+ hidden.beginRender(null);
+ unreachable();
+ }
+ catch (RuntimeException ex)
+ {
+ assertEquals(ex.getMessage(), "The Hidden component must be
enclosed by a Form component.");
+ }
+ }
+}
Propchange:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/HiddenTest.java
------------------------------------------------------------------------------
svn:eol-style = native
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=758816&r1=758815&r2=758816&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 Mar 26 19:41:40 2009
@@ -2895,4 +2895,14 @@
assertText("outputvalue", "37");
}
+
+ @Test
+ public void hidden_field()
+ {
+ start("Hidden Demo", "setup");
+
+ clickAndWait(SUBMIT);
+
+ assertText("stored", "12345");
+ }
}
\ No newline at end of file
Added:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemo.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemo.java?rev=758816&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemo.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemo.java
Thu Mar 26 19:41:40 2009
@@ -0,0 +1,40 @@
+// Copyright 2009 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.annotations.InjectPage;
+import org.apache.tapestry5.annotations.Property;
+
+public class HiddenDemo
+{
+ @Property
+ private int stored;
+
+ @InjectPage
+ private HiddenDemoOutput hiddenDemoOutput;
+
+ void onActivate(int stored)
+ {
+ this.stored = stored;
+ }
+
+ Object onSuccessFromForm()
+ {
+ hiddenDemoOutput.setStored(stored);
+
+ return hiddenDemoOutput;
+ }
+
+}
Propchange:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemo.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemoOutput.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemoOutput.java?rev=758816&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemoOutput.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemoOutput.java
Thu Mar 26 19:41:40 2009
@@ -0,0 +1,35 @@
+// Copyright 2009 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.PersistenceConstants;
+import org.apache.tapestry5.annotations.Persist;
+
+public class HiddenDemoOutput
+{
+ @Persist(PersistenceConstants.FLASH)
+ private Object stored;
+
+ public Object getStored()
+ {
+ return stored;
+ }
+
+ public void setStored(Object stored)
+ {
+ this.stored = stored;
+ }
+
+}
Propchange:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/HiddenDemoOutput.java
------------------------------------------------------------------------------
svn:eol-style = native
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=758816&r1=758815&r2=758816&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
Thu Mar 26 19:41:40 2009
@@ -65,6 +65,8 @@
private static final List<Item> ITEMS = CollectionFactory.newList(
+ new Item("HiddenDemo", "Hidden Demo", "Demo the use of the Hidden
component."),
+
new Item("FormZoneDemo", "Form Zone Demo", "Use a form to update a
zone."),
new Item("ZoneUpdateNamespace", "Zone/Namespace Interaction",
"Prove that TAP5-573 is fixed"),