1 /***
2 * Completion-capable JSF text field.
3 */
4 /***
5 Copyright (C) 2005 The Java Community
6
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20 package org.bejug.javacareers.common.ajax;
21
22 import javax.faces.component.html.HtmlInputText;
23 import javax.faces.context.FacesContext;
24
25
26 /***
27 * <p/>
28 * Completion-capable JSF text field.
29 * The component has a completionMethod which is called repeatedly (asynchronously)
30 * and is in charge of registering completion results (which will be populated
31 * in the completion dialog for this field).
32 * </p>
33 * <p/>
34 * The component will render CSS and JavaScript into the page. These JavaScript and CSS
35 * class names could potentially interact with your own JSP contents if there is a name
36 * conflict so if you observe strange results, try changing your method names or style
37 * classes.
38 * </p>
39 *
40 * @author Tor Norbye
41 * Todo Pick more unique namespace names for the style classes and the javascript
42 * methods - perhaps prefixed by "ajax" or "ajtf" (ajax text field), or maybe even
43 * "blueprints" ?
44 * Todo Add property "maxResults", setting the max number of completion items
45 * shown in the completion popup
46 * Todo Add property "Delay (milliseconds)" - an optional timer delay before
47 * popup should appear or get updated after keystrokes
48 * Todo Add property "showOnFocus", which when set will cause the completion
49 * dialog to be shown showing the first possible matches even before the user
50 * has typed anything into the text field
51 */
52 public class TextField extends HtmlInputText {
53
54 /***
55 * a String containing the completionMethod.
56 */
57 private String completionMethod;
58
59 /***
60 * a String containing the action when choosing an element.
61 */
62 private String onchoose;
63
64 /***
65 * String containing the action when displaying an element.
66 */
67 private String ondisplay;
68
69
70 /***
71 * constructor.
72 */
73 public TextField() {
74 super();
75 setRendererType("TextField");
76 }
77
78
79 /***
80 * to save the state of the textfield.
81 *
82 * @param context the FacesContext.
83 * @return an array containing the possible states of the textfield.
84 */
85 public Object saveState(FacesContext context) {
86 Object[] values = new Object[4];
87 values[0] = super.saveState(context);
88 values[1] = completionMethod;
89 values[2] = onchoose;
90 values[3] = ondisplay;
91
92 return values;
93 }
94
95 /***
96 * to restore the state of the textfield.
97 *
98 * @param context the FacesContext.
99 * @param state the state to restore.
100 */
101 public void restoreState(FacesContext context, Object state) {
102 Object[] values = (Object[]) state;
103 super.restoreState(context, values[0]);
104 this.completionMethod = (String) values[1];
105 this.onchoose = (String) values[2];
106 this.ondisplay = (String) values[3];
107 }
108
109 /***
110 * @param onchoose a String containing the onchoose to set.
111 */
112 public void setOnchoose(String onchoose) {
113 this.onchoose = onchoose;
114 }
115
116 /***
117 * @return the onchoose.
118 */
119 public String getOnchoose() {
120 return onchoose;
121 }
122
123 /***
124 * @param ondisplay the ondisplay, as a string, to set.
125 */
126 public void setOndisplay(String ondisplay) {
127 this.ondisplay = ondisplay;
128 }
129
130 /***
131 * @return the ondisplay.
132 */
133 public String getOndisplay() {
134 return ondisplay;
135 }
136
137 /***
138 * @param completionMethod the completionmethod as a string.
139 */
140 public void setCompletionMethod(String completionMethod) {
141 this.completionMethod = completionMethod;
142 }
143
144 /***
145 * @return the completionmethod.
146 */
147 public String getCompletionMethod() {
148 return completionMethod;
149 }
150
151 /***
152 * <p>Render a textual textfield.</p>
153 *
154 * @return a String containing the rendering of a textfield.
155 */
156 public String getText() {
157
158 Object value = getValue();
159 if (value == null) {
160 return null;
161 }
162 return value.toString();
163 }
164
165 /***
166 * @param text is text to set
167 * @see #getText()
168 */
169 public void setText(String text) {
170 setValue(text);
171 }
172
173 /***
174 * Return the maximum number of results returned from this text field
175 *
176 * @return A numberf indicating the maximum number of completion matches
177 * that should be returned/displayed
178 */
179 public int getMaxCount() {
180 return PhaseListener.MAX_RESULTS_RETURNED;
181 }
182 }
183
184
185 /***
186 * $Log: TextField.java,v $
187 * Revision 1.5 2005/12/20 15:36:46 shally
188 * CheckStyle and PMD changes.
189 *
190 * Revision 1.4 2005/12/08 14:53:45 shally
191 * Opkuis voor checkstyle.
192 *
193 * Revision 1.3 2005/08/10 12:07:42 bavo_jcs
194 * Optimized imports according to checkstyle
195 *
196 * Revision 1.2 2005/08/08 12:07:55 bme_jcs
197 * resolved checkstyle errors
198 *
199 * Revision 1.1 2005/07/13 11:42:08 bavo_jcs
200 * Ajax rename and test-cleanup
201 *
202 * Revision 1.1 2005/07/13 11:16:53 bavo_jcs
203 * Moved Ajax files
204 *
205 * Revision 1.1 2005/07/06 14:15:30 bavo_jcs
206 * Ajax integration
207 *
208 */