KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > field > SuggestFieldTag


1 package fr.improve.struts.taglib.layout.field;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4
5 import org.apache.struts.taglib.html.BaseHandlerTag;
6 import org.apache.struts.taglib.html.TextTag;
7
8 import fr.improve.struts.taglib.layout.event.StaticCodeIncludeLayoutEvent;
9 import fr.improve.struts.taglib.layout.util.LayoutUtils;
10 import fr.improve.struts.taglib.layout.util.TagUtils;
11
12 /**
13  * A text field tag with a suggestion list
14  * @author: Francois Goldgewicht
15  */

16 public class SuggestFieldTag extends AbstractFieldTag {
17     
18     static class StrutsSuggestFieldTag extends TextTag {
19         
20         public void prepareTextEvents(StringBuffer JavaDoc in_buffer) {
21             super.prepareTextEvents(in_buffer);
22             in_buffer.append(" autocomplete=\"off\"");
23         }
24         
25     }
26     
27     private TextTag textFieldTag = new StrutsSuggestFieldTag();
28     
29     public static final String JavaDoc LOADED = "fr.improve.struts.taglib.layout.field.SuggestFieldTag.LOADED";
30
31     public static final int DEFAULT_SUGGESTCOUNT = 10;
32     public static final String JavaDoc DEFAULT_SUGGESTENCODING = "ISO-8859-1";
33     public static final int DEFAULT_MINWORDLENGTH = 1;
34     public static final int DEFAULT_TIMEOUT = 0;
35     protected String JavaDoc suggestAction;
36     protected int suggestCount;
37     protected String JavaDoc suggestEncoding;
38     protected int minWordLength;
39     protected int timeout;
40     protected boolean all = false;
41     
42     protected String JavaDoc jspOnkeyup;
43     protected String JavaDoc jspOnkeydown;
44     protected String JavaDoc jspOnkeypress;
45     protected String JavaDoc jspOnFocus;
46     
47
48     protected void initDynamicValues() {
49         super.initDynamicValues();
50         
51         jspOnkeyup = onkeyup;
52         jspOnkeydown = onkeydown;
53         jspOnkeypress = onkeypress;
54         jspOnFocus = onfocus;
55         
56         String JavaDoc url = null;
57
58         try {
59             url = LayoutUtils.computeURL(pageContext, null, null, null, this.getSuggestAction(), null, null, null, false, "suggest");
60         }
61         catch (JspException JavaDoc e) {
62             TagUtils.saveException(pageContext, e);
63             throw new RuntimeException JavaDoc(e.getMessage());
64         }
65         
66         if (suggestCount == 0)
67             suggestCount = DEFAULT_SUGGESTCOUNT;
68         
69         if (suggestEncoding == null)
70             suggestEncoding = DEFAULT_SUGGESTENCODING;
71         
72         if (minWordLength == 0)
73             minWordLength = DEFAULT_MINWORDLENGTH;
74
75         if (timeout == 0)
76             timeout = DEFAULT_TIMEOUT;
77
78         if (all) {
79             minWordLength = 0;
80             onfocus=(onfocus != null ? onfocus : "") +
81             ";return computeFocus('" + this.getStyleId() +
82             "', '" + url + "', " + suggestCount + ", '" + suggestEncoding +
83             "', " + minWordLength + ", " + timeout + ", '" + all + "' )";
84             /*onblur=(onblur != null ? onblur : "") +
85             ";return computeBlur('" + this.getStyleId() + "', '" + all + "' )";*/

86         }
87         
88         onkeyup=(onkeyup != null ? onkeyup : "") +
89         ";return computeKeyUp('" + this.getStyleId() +
90         "', getKey(event.keyCode, event.which), '" +
91         url + "', " + suggestCount + ", '" + suggestEncoding +
92         "', " + minWordLength + ", " + timeout + ", '" + all + "' )";
93     
94         onkeydown=(onkeydown != null ? onkeydown : "") +
95         ";return computeKeyDown('" + this.getStyleId() +
96         "', getKey(event.keyCode, event.which) , '" +
97         url + "' )";
98         
99         onkeypress=(onkeypress != null ? onkeypress : "") +
100         ";return computeKeyPress('" + this.getStyleId() +
101         "', getKey(event.keyCode, event.which) )";
102     }
103     
104     protected void copyProperties(BaseHandlerTag in_dest) throws JspException JavaDoc {
105         super.copyProperties(in_dest);
106         textFieldTag.setCols(getCols());
107         textFieldTag.setMaxlength(getMaxlength());
108         textFieldTag.setProperty(getProperty());
109         textFieldTag.setRows(getRows());
110         textFieldTag.setValue(getValue());
111         textFieldTag.setAccept(getAccept());
112         textFieldTag.setName(getName());
113     }
114     
115     protected boolean doBeforeValue() throws javax.servlet.jsp.JspException JavaDoc {
116         fieldTag = textFieldTag;
117         
118         loadScript();
119         
120         return true;
121     }
122     
123     protected void doAfterValue() throws JspException JavaDoc {
124         
125         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
126         
127         // Suggestion list Div
128
buffer.append("<div id=\"" + this.getStyleId() + "SuggestionList\" class=\"suggestionList\"></div>");
129         
130         // Hidden fields
131
buffer.append("<input type=\"hidden\" id=\"" + this.getStyleId() + "SuggestionList_selectedFieldText" + "\" value=\"0\">");
132         buffer.append("<input type=\"hidden\" id=\"" + this.getStyleId() + "SuggestionList_selectedSuggestionIndex" + "\" value=\"-1\">");
133         buffer.append("<input type=\"hidden\" id=\"" + this.getStyleId() + "SuggestionList_typedWord" + "\" value=\"\">");
134         
135         // In case we are already in a DIV.
136
TagUtils.write(pageContext, new StaticCodeIncludeLayoutEvent(this, buffer.toString()).send().toString());
137     }
138     
139     /**
140      * Load the required Javascript code.
141      */

142     protected void loadScript() throws JspException JavaDoc {
143         
144         if (pageContext.getRequest().getAttribute(LOADED)==null) {
145             TagUtils.write(pageContext, "<script SRC=\"");
146             TagUtils.write(pageContext, LayoutUtils.getSkin(pageContext.getSession()).getConfigDirectory(pageContext.getRequest()));
147             TagUtils.write(pageContext, "/suggest.js\"></script>");
148             pageContext.getRequest().setAttribute(LOADED, "");
149         }
150         
151     }
152         
153     /* (non-Javadoc)
154      * @see fr.improve.struts.taglib.layout.LayoutTagSupport#reset()
155      */

156     protected void reset() {
157         super.reset();
158         onkeyup = jspOnkeyup;
159         onkeydown = jspOnkeydown;
160         onkeypress = jspOnkeypress;
161         onfocus = jspOnFocus;
162     }
163     
164     public void release(){
165         super.release();
166         all = false;
167     }
168     
169     /**
170      * @return Returns the suggestAction.
171      */

172     public String JavaDoc getSuggestAction() {
173         return suggestAction;
174     }
175     
176     /**
177      * @param suggestAction The suggestAction to set.
178      */

179     public void setSuggestAction(String JavaDoc suggestAction) {
180         this.suggestAction = suggestAction;
181     }
182
183     /**
184      * @return Returns the suggestCount.
185      */

186     public int getSuggestCount() {
187         return suggestCount;
188     }
189     /**
190      * @param suggestCount The suggestCount to set.
191      */

192     public void setSuggestCount(int suggestCount) {
193         this.suggestCount = suggestCount;
194     }
195     /**
196      * @return Returns the suggestEncoding.
197      */

198     public String JavaDoc getSuggestEncoding() {
199         return suggestEncoding;
200     }
201     /**
202      * @param suggestEncoding The suggestEncoding to set.
203      */

204     public void setSuggestEncoding(String JavaDoc suggestEncoding) {
205         this.suggestEncoding = suggestEncoding;
206     }
207     /**
208      * @return Returns the minWordLength.
209      */

210     public int getMinWordLength() {
211         return minWordLength;
212     }
213     /**
214      * @param minWordLength The minWordLength to set.
215      */

216     public void setMinWordLength(int minWordLength) {
217         this.minWordLength = minWordLength;
218     }
219     /**
220      * @return Returns the timeout.
221      */

222     public int getTimeout() {
223         return timeout;
224     }
225     /**
226      * @param timeout The timeout to set.
227      */

228     public void setTimeout(int timeout) {
229         this.timeout = timeout;
230     }
231     /**
232      * @param all Returns the all.
233      */

234     public boolean isAll() {
235         return all;
236 }
237     /**
238      * @param all The all to set.
239      */

240     public void setAll(boolean all) {
241         this.all = all;
242     }
243 }
244
Popular Tags