01: /*
02: * $Id$ $Revision$ $Date$
03: *
04: * ==============================================================================
05: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
06: * use this file except in compliance with the License. You may obtain a copy of
07: * the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14: * License for the specific language governing permissions and limitations under
15: * the License.
16: */
17: package wicket.extensions.ajax.markup.html.autocomplete;
18:
19: import wicket.RequestCycle;
20: import wicket.ResourceReference;
21: import wicket.Response;
22: import wicket.ajax.AbstractDefaultAjaxBehavior;
23: import wicket.ajax.AjaxRequestTarget;
24: import wicket.markup.html.resources.CompressedResourceReference;
25: import wicket.util.string.JavascriptUtils;
26:
27: /**
28: * @since 1.2
29: *
30: * @author Janne Hietamäki (jannehietamaki)
31: */
32: public abstract class AbstractAutoCompleteBehavior extends
33: AbstractDefaultAjaxBehavior {
34: private static final ResourceReference AUTOCOMPLETE_JS = new CompressedResourceReference(
35: AutoCompleteBehavior.class, "wicket-autocomplete.js");
36:
37: /**
38: *
39: */
40: private static final long serialVersionUID = 1L;
41:
42: protected void onRenderHeadContribution(Response response) {
43: }
44:
45: protected void onBind() {
46: // add empty AbstractDefaultAjaxBehavior to the component, to force
47: // rendering wicket-ajax.js reference if no other ajax behavior is on page
48: getComponent().add(new AbstractDefaultAjaxBehavior() {
49: private static final long serialVersionUID = 1L;
50:
51: protected void respond(AjaxRequestTarget target) {
52: }
53: });
54: }
55:
56: protected void onRenderHeadInitContribution(Response response) {
57: writeJsReference(response, AUTOCOMPLETE_JS);
58: }
59:
60: protected void onComponentRendered() {
61: Response response = getComponent().getResponse();
62: final String id = getComponent().getMarkupId();
63: response.write(JavascriptUtils.SCRIPT_OPEN_TAG);
64: response.write("new Wicket.AutoComplete('" + id + "','"
65: + getCallbackUrl() + "');");
66: response.write(JavascriptUtils.SCRIPT_CLOSE_TAG);
67: }
68:
69: protected String getImplementationId() {
70: return "wicket-autocomplete";
71: }
72:
73: protected void respond(AjaxRequestTarget target) {
74: final RequestCycle requestCycle = RequestCycle.get();
75: final String val = requestCycle.getRequest().getParameter("q");
76: onRequest(val, requestCycle);
77: }
78:
79: /**
80: * Callback for the ajax event generated by the javascript. This is where we
81: * need to generate our response.
82: *
83: * @param input
84: * the input entered so far
85: * @param requestCycle
86: * current request cycle
87: */
88: protected abstract void onRequest(String input,
89: RequestCycle requestCycle);
90: }
|