1 package net.sf.tacos.javascript.jquery.components;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6 import org.apache.tapestry.IMarkupWriter;
7 import org.apache.tapestry.IRequestCycle;
8 import org.apache.tapestry.PageRenderSupport;
9 import org.apache.tapestry.Tapestry;
10 import org.apache.tapestry.TapestryUtils;
11 import org.apache.tapestry.dojo.form.Autocompleter;
12 import org.apache.tapestry.dojo.form.IAutocompleteModel;
13 import org.apache.tapestry.engine.DirectServiceParameter;
14 import org.apache.tapestry.engine.ILink;
15 import org.apache.tapestry.json.JSONObject;
16 import org.apache.tapestry.valid.ValidatorException;
17
18 public abstract class JqueryAutocompleter extends Autocompleter {
19
20 protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle)
21 {
22 IAutocompleteModel model = getModel();
23 if (model == null)
24 throw Tapestry.createRequiredParameterException(this, "model");
25
26 Object value = getValue();
27 Object key = value != null && !"".equals(value.toString()) ? model.getPrimaryKey(value) : null;
28
29 renderDelegatePrefix(writer, cycle);
30
31 writer.beginEmpty("input");
32 writer.attribute("name", getName());
33 writer.attribute("autocomplete", "off");
34
35 if (isDisabled())
36 writer.attribute("disabled", "disabled");
37
38 renderIdAttribute(writer, cycle);
39
40 renderDelegateAttributes(writer, cycle);
41
42 getValidatableFieldSupport().renderContributions(this, writer, cycle);
43
44
45 renderInformalParameters(writer, cycle);
46
47
48
49 if (isLocal())
50 {
51 List list = model.getValues("");
52 for (int i=0; i<list.size(); i++)
53 {
54
55
56
57
58
59
60
61
62
63
64 }
65 }
66
67
68 renderDelegateSuffix(writer, cycle);
69
70 Map parms = new HashMap();
71 parms.put("id", getClientId());
72
73 JSONObject json = new JSONObject();
74 if (!isLocal())
75 {
76 ILink link = getDirectService().getLink(true, new DirectServiceParameter(this));
77 parms.put("url", link.getURL());
78 }
79
80 json.put("mode", isLocal() ? "local" : "remote");
81 json.put("widgetId", getName());
82 json.put("name", getName());
83 json.put("searchDelay", getSearchDelay());
84 json.put("fadeTime", getFadeTime());
85 json.put("maxListLength", getMaxListLength());
86 json.put("forceValidOption", isForceValidOption());
87 json.put("disabled", isDisabled());
88 json.put("autoComplete", getAutoCompleteField());
89
90 json.put("value", key != null ? getDataSqueezer().squeeze(key) : "");
91 json.put("label", value != null ? model.getLabelFor(value) : "");
92
93 parms.put("props", json.toString());
94 parms.put("form", getForm().getName());
95 parms.put("widget", this);
96
97 PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, this);
98 getScript().execute(this, cycle, prs, parms);
99 }
100
101 protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle)
102 {
103 String value = cycle.getParameter(getName());
104
105 Object object = null;
106
107 try
108 {
109 if (value != null && value.length() > 0) {
110
111 List list = getModel().getValues(value);
112 if (list!=null && list.size()>0) {
113 object = list.get(0);
114 }
115 }
116
117 getValidatableFieldSupport().validate(this, writer, cycle, object);
118
119 setValue(object);
120 }
121 catch (ValidatorException e)
122 {
123 getForm().getDelegate().record(e);
124 }
125 }
126
127 public void trigger(IRequestCycle cycle)
128 {
129 setFilter(cycle.getParameter("q"));
130 }
131
132 public boolean isJson()
133 {
134 return true;
135 }
136
137 }