Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Change SetEditor to implement the Field interface

  • Loading branch information...
commit 5dca968fce2930dc74ff3c0e2a1c539b1a634a59 1 parent 1a155d1
Howard M. Lewis Ship authored
83 tapx-core/src/main/java/com/howardlewisship/tapx/core/components/SetEditor.java
... ... @@ -1,4 +1,4 @@
1   -// Copyright 2010 Howard M. Lewis Ship
  1 +// Copyright 2010, 2011 Howard M. Lewis Ship
2 2 //
3 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 4 // you may not use this file except in compliance with the License.
@@ -14,14 +14,7 @@
14 14
15 15 package com.howardlewisship.tapx.core.components;
16 16
17   -import java.util.HashSet;
18   -import java.util.Set;
19   -
20   -import org.apache.tapestry5.Asset;
21   -import org.apache.tapestry5.BindingConstants;
22   -import org.apache.tapestry5.Block;
23   -import org.apache.tapestry5.ComponentAction;
24   -import org.apache.tapestry5.ComponentResources;
  17 +import org.apache.tapestry5.*;
25 18 import org.apache.tapestry5.annotations.Environmental;
26 19 import org.apache.tapestry5.annotations.Import;
27 20 import org.apache.tapestry5.annotations.Parameter;
@@ -29,31 +22,52 @@
29 22 import org.apache.tapestry5.ioc.annotations.Inject;
30 23 import org.apache.tapestry5.json.JSONArray;
31 24 import org.apache.tapestry5.json.JSONObject;
  25 +import org.apache.tapestry5.services.ComponentDefaultProvider;
32 26 import org.apache.tapestry5.services.FormSupport;
33 27 import org.apache.tapestry5.services.Request;
34 28 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
35 29
  30 +import java.util.HashSet;
  31 +import java.util.Set;
  32 +
36 33 /**
37 34 * A SetEditor is an dynamic client-side element used to edit a set of values (currently limited to Strings). The
38 35 * presentation is two columns: a text field on the left and a list on the right. Entering a value into the text field
39 36 * will add it to the list; the list shows the added items, with a control for each of them to delete the value.
40   - * <p>
  37 + * <p/>
41 38 * Behind the scenes is a hidden form field; as values are added or removed from the list, the hidden form field is
42 39 * updated.
43   - * <p>
  40 + * <p/>
44 41 * When the form is submitted, the component will obtain the current Set and clear it (if non-null). If the current Set
45 42 * is null, a new {@link HashSet} instance is created. The empty Set is populated with the values in the submission.
46 43 */
47 44 @Import(stack = "tapx-core")
48   -public class SetEditor
  45 +public class SetEditor implements Field
49 46 {
50 47 @Parameter(required = true, autoconnect = true)
51 48 private Set<String> set;
52 49
  50 + /**
  51 + * The user presentable label for the field. If not provided, a reasonable label is generated from the component's
  52 + * id, first by looking for a message key named "id-label" (substituting the component's actual id), then by
  53 + * converting the actual id to a presentable string (for example, "userId" to "User Id"). The default default valuesLabel Block
  54 + * uses this label (this is the label that appears above the list of items currently in the set).
  55 + */
  56 + @Parameter(defaultPrefix = BindingConstants.LITERAL)
  57 + private String label;
  58 +
  59 + /**
  60 + * Block used as the label for the text field where new values can be entered. The default block
  61 + * is simply {@code <label>New Value</label>} and will usually be customized.
  62 + */
53 63 @Property
54 64 @Parameter(defaultPrefix = BindingConstants.LITERAL, value = "block:defaultFieldLabel")
55 65 private Block fieldLabel;
56 66
  67 + /**
  68 + * Label presented above the list of selected values (each of which will have a button to remove it from the list).
  69 + * The default is simply the value of the label parameter inside a {@code <label>} element.
  70 + */
57 71 @Property
58 72 @Parameter(defaultPrefix = BindingConstants.LITERAL, value = "block:defaultValuesLabel")
59 73 private Block valuesLabel;
@@ -64,8 +78,9 @@
64 78 @Environmental
65 79 private FormSupport formSupport;
66 80
67   - @Property
68   - private String fieldId;
  81 + private String clientId;
  82 +
  83 + private String name;
69 84
70 85 @Inject
71 86 private ComponentResources resources;
@@ -73,6 +88,14 @@
73 88 @Inject
74 89 private Request request;
75 90
  91 + @Inject
  92 + private ComponentDefaultProvider defaultProvider;
  93 +
  94 + final String defaultLabel()
  95 + {
  96 + return defaultProvider.defaultLabel(resources);
  97 + }
  98 +
76 99 static class HandleSubmission implements ComponentAction<SetEditor>
77 100 {
78 101 private static final long serialVersionUID = 490527745794643539L;
@@ -92,14 +115,15 @@ public void execute(SetEditor component)
92 115
93 116 void setupRender()
94 117 {
95   - fieldId = javaScriptSupport.allocateClientId(resources);
  118 + clientId = javaScriptSupport.allocateClientId(resources);
  119 +
  120 + name = formSupport.allocateControlName(clientId);
96 121 }
97 122
98 123 void afterRender()
99 124 {
100   - String name = formSupport.allocateControlName(fieldId);
101 125
102   - JSONObject spec = new JSONObject("id", fieldId, "name", name);
  126 + JSONObject spec = new JSONObject("id", clientId, "name", name);
103 127
104 128 JSONArray values = new JSONArray();
105 129
@@ -134,4 +158,29 @@ private void handleSubmission(String name)
134 158 for (int i = 0; i < values.length(); i++)
135 159 set.add(values.getString(i));
136 160 }
  161 +
  162 + public String getControlName()
  163 + {
  164 + return name;
  165 + }
  166 +
  167 + public String getLabel()
  168 + {
  169 + return label;
  170 + }
  171 +
  172 + public boolean isDisabled()
  173 + {
  174 + return false;
  175 + }
  176 +
  177 + public boolean isRequired()
  178 + {
  179 + return false;
  180 + }
  181 +
  182 + public String getClientId()
  183 + {
  184 + return clientId;
  185 + }
137 186 }
9 tapx-core/src/main/resources/com/howardlewisship/tapx/core/components/SetEditor.tml
... ... @@ -1,10 +1,10 @@
1 1 <div class="tx-seteditor" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
2   - xmlns:p="tapestry:parameter">
  2 + >
3 3 <div class="tx-fieldcolumn">
4 4 <div class="tx-columnlabel">
5 5 <t:delegate to="fieldLabel"/>
6 6 </div>
7   - <input type="text" id="${fieldId}"/>
  7 + <input type="text" id="${clientId}"/>
8 8 <t:remove>A hidden field is added, on the client side.</t:remove>
9 9 </div>
10 10 <div class="tx-valuescolumn">
@@ -14,12 +14,11 @@
14 14 <t:remove>Rendering of the values occurs on the client.</t:remove>
15 15 </div>
16 16
17   -
18 17 <t:block id="defaultFieldLabel">
19   - <label>New value</label>
  18 + <label>New Value</label>
20 19 </t:block>
21 20
22 21 <t:block id="defaultValuesLabel">
23   - <label>Values</label>
  22 + <label>${label}</label>
24 23 </t:block>
25 24 </div>
2  tapx-core/src/test/resources/core/demo/pages/SetEditorDemo.tml
@@ -5,7 +5,7 @@
5 5
6 6 <t:form>
7 7
8   - <x:seteditor set="values"/>
  8 + <x:seteditor set="values" label="Names" />
9 9
10 10 <br clear="both"/>
11 11

0 comments on commit 5dca968

Please sign in to comment.
Something went wrong with that request. Please try again.