Browse files

Change SetEditor to implement the Field interface

  • Loading branch information...
1 parent 1a155d1 commit 5dca968fce2930dc74ff3c0e2a1c539b1a634a59 @hlship committed Dec 8, 2011
View
83 tapx-core/src/main/java/com/howardlewisship/tapx/core/components/SetEditor.java
@@ -1,4 +1,4 @@
-// Copyright 2010 Howard M. Lewis Ship
+// Copyright 2010, 2011 Howard M. Lewis Ship
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -14,46 +14,60 @@
package com.howardlewisship.tapx.core.components;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.tapestry5.Asset;
-import org.apache.tapestry5.BindingConstants;
-import org.apache.tapestry5.Block;
-import org.apache.tapestry5.ComponentAction;
-import org.apache.tapestry5.ComponentResources;
+import org.apache.tapestry5.*;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;
+import org.apache.tapestry5.services.ComponentDefaultProvider;
import org.apache.tapestry5.services.FormSupport;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
+import java.util.HashSet;
+import java.util.Set;
+
/**
* A SetEditor is an dynamic client-side element used to edit a set of values (currently limited to Strings). The
* presentation is two columns: a text field on the left and a list on the right. Entering a value into the text field
* will add it to the list; the list shows the added items, with a control for each of them to delete the value.
- * <p>
+ * <p/>
* Behind the scenes is a hidden form field; as values are added or removed from the list, the hidden form field is
* updated.
- * <p>
+ * <p/>
* When the form is submitted, the component will obtain the current Set and clear it (if non-null). If the current Set
* is null, a new {@link HashSet} instance is created. The empty Set is populated with the values in the submission.
*/
@Import(stack = "tapx-core")
-public class SetEditor
+public class SetEditor implements Field
{
@Parameter(required = true, autoconnect = true)
private Set<String> set;
+ /**
+ * The user presentable label for the field. If not provided, a reasonable label is generated from the component's
+ * id, first by looking for a message key named "id-label" (substituting the component's actual id), then by
+ * converting the actual id to a presentable string (for example, "userId" to "User Id"). The default default valuesLabel Block
+ * uses this label (this is the label that appears above the list of items currently in the set).
+ */
+ @Parameter(defaultPrefix = BindingConstants.LITERAL)
+ private String label;
+
+ /**
+ * Block used as the label for the text field where new values can be entered. The default block
+ * is simply {@code <label>New Value</label>} and will usually be customized.
+ */
@Property
@Parameter(defaultPrefix = BindingConstants.LITERAL, value = "block:defaultFieldLabel")
private Block fieldLabel;
+ /**
+ * Label presented above the list of selected values (each of which will have a button to remove it from the list).
+ * The default is simply the value of the label parameter inside a {@code <label>} element.
+ */
@Property
@Parameter(defaultPrefix = BindingConstants.LITERAL, value = "block:defaultValuesLabel")
private Block valuesLabel;
@@ -64,15 +78,24 @@
@Environmental
private FormSupport formSupport;
- @Property
- private String fieldId;
+ private String clientId;
+
+ private String name;
@Inject
private ComponentResources resources;
@Inject
private Request request;
+ @Inject
+ private ComponentDefaultProvider defaultProvider;
+
+ final String defaultLabel()
+ {
+ return defaultProvider.defaultLabel(resources);
+ }
+
static class HandleSubmission implements ComponentAction<SetEditor>
{
private static final long serialVersionUID = 490527745794643539L;
@@ -92,14 +115,15 @@ public void execute(SetEditor component)
void setupRender()
{
- fieldId = javaScriptSupport.allocateClientId(resources);
+ clientId = javaScriptSupport.allocateClientId(resources);
+
+ name = formSupport.allocateControlName(clientId);
}
void afterRender()
{
- String name = formSupport.allocateControlName(fieldId);
- JSONObject spec = new JSONObject("id", fieldId, "name", name);
+ JSONObject spec = new JSONObject("id", clientId, "name", name);
JSONArray values = new JSONArray();
@@ -134,4 +158,29 @@ private void handleSubmission(String name)
for (int i = 0; i < values.length(); i++)
set.add(values.getString(i));
}
+
+ public String getControlName()
+ {
+ return name;
+ }
+
+ public String getLabel()
+ {
+ return label;
+ }
+
+ public boolean isDisabled()
+ {
+ return false;
+ }
+
+ public boolean isRequired()
+ {
+ return false;
+ }
+
+ public String getClientId()
+ {
+ return clientId;
+ }
}
View
9 tapx-core/src/main/resources/com/howardlewisship/tapx/core/components/SetEditor.tml
@@ -1,10 +1,10 @@
<div class="tx-seteditor" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
- xmlns:p="tapestry:parameter">
+ >
<div class="tx-fieldcolumn">
<div class="tx-columnlabel">
<t:delegate to="fieldLabel"/>
</div>
- <input type="text" id="${fieldId}"/>
+ <input type="text" id="${clientId}"/>
<t:remove>A hidden field is added, on the client side.</t:remove>
</div>
<div class="tx-valuescolumn">
@@ -14,12 +14,11 @@
<t:remove>Rendering of the values occurs on the client.</t:remove>
</div>
-
<t:block id="defaultFieldLabel">
- <label>New value</label>
+ <label>New Value</label>
</t:block>
<t:block id="defaultValuesLabel">
- <label>Values</label>
+ <label>${label}</label>
</t:block>
</div>
View
2 tapx-core/src/test/resources/core/demo/pages/SetEditorDemo.tml
@@ -5,7 +5,7 @@
<t:form>
- <x:seteditor set="values"/>
+ <x:seteditor set="values" label="Names" />
<br clear="both"/>

0 comments on commit 5dca968

Please sign in to comment.