KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > suggest > AutocompleteTextField


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element.suggest;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.riotfamily.common.markup.DocumentWriter;
35 import org.riotfamily.common.markup.Html;
36 import org.riotfamily.common.markup.TagWriter;
37 import org.riotfamily.forms.ContentElement;
38 import org.riotfamily.forms.DHTMLElement;
39 import org.riotfamily.forms.element.AbstractTextElement;
40 import org.riotfamily.forms.resource.FormResource;
41 import org.riotfamily.forms.resource.ResourceElement;
42 import org.riotfamily.forms.resource.Resources;
43
44 /**
45  * @author Felix Gnass [fgnass at neteye dot de]
46  * @since 6.4
47  */

48 public class AutocompleteTextField extends AbstractTextElement
49         implements ResourceElement, DHTMLElement, ContentElement {
50
51     private AutocompleterModel model;
52
53     public void setModel(AutocompleterModel model) {
54         this.model = model;
55     }
56
57     public FormResource getResource() {
58         return Resources.SCRIPTACULOUS_CONTROLS;
59     }
60     
61     public void renderInternal(PrintWriter JavaDoc writer) {
62         super.renderInternal(writer);
63         TagWriter tag = new TagWriter(writer);
64         tag.start(Html.DIV).attribute(Html.COMMON_ID, getChoicesDivId())
65                 .attribute(Html.COMMON_CLASS, "autocomplete")
66                 .attribute(Html.COMMON_STYLE, "display:none")
67                 .end();
68     }
69     
70     private String JavaDoc getChoicesDivId() {
71         return getId() + "-choices";
72     }
73     
74     public String JavaDoc getInitScript() {
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         sb.append("new Ajax.Autocompleter(\"").append(getId()).append("\", \"")
77             .append(getChoicesDivId()).append("\", \"")
78             .append(getFormContext().getContentUrl(this)).append("\", {});");
79         
80         return sb.toString();
81     }
82         
83     public void handleContentRequest(HttpServletRequest JavaDoc request,
84             HttpServletResponse JavaDoc response) throws IOException JavaDoc {
85
86         String JavaDoc search = request.getParameter(getParamName());
87         Collection JavaDoc suggestions = model.getSuggestions(search);
88         
89         DocumentWriter doc = new DocumentWriter(response.getWriter());
90         doc.start(Html.UL);
91         Iterator JavaDoc it = suggestions.iterator();
92         while (it.hasNext()) {
93             String JavaDoc value = (String JavaDoc) it.next();
94             doc.start(Html.LI).body(value).end();
95         }
96         doc.end();
97     }
98 }
99
Popular Tags