KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > selectinputtext > SelectInputTextRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.component.selectinputtext;
35
36 import com.icesoft.faces.context.DOMContext;
37 import com.icesoft.faces.context.effects.JavascriptContext;
38 import com.icesoft.faces.renderkit.dom_html_basic.DomBasicInputRenderer;
39 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
40 import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer;
41 import com.icesoft.faces.util.DOMUtils;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.w3c.dom.Element JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.Text JavaDoc;
47
48 import javax.faces.component.UIComponent;
49 import javax.faces.component.UIInput;
50 import javax.faces.context.FacesContext;
51 import javax.faces.model.SelectItem;
52 import java.io.IOException JavaDoc;
53 import java.util.HashSet JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.Map JavaDoc;
56 import java.util.Set JavaDoc;
57
58
59 public class SelectInputTextRenderer extends DomBasicInputRenderer {
60     private final String JavaDoc AUTOCOMPLETE_DIV = "autoCompleteDiv";
61     private static final Log log =
62             LogFactory.getLog(SelectInputTextRenderer.class);
63
64     public boolean getRendersChildren() {
65         return true;
66     }
67
68     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
69             throws IOException JavaDoc {
70         validateParameters(facesContext, uiComponent, null);
71         if (log.isTraceEnabled()) {
72             log.trace("encodeBegin");
73         }
74         SelectInputText component = (SelectInputText) uiComponent;
75         DOMContext domContext =
76                 DOMContext.attachDOMContext(facesContext, uiComponent);
77         String JavaDoc clientId = uiComponent.getClientId(facesContext);
78         String JavaDoc divId = clientId + AUTOCOMPLETE_DIV;
79         String JavaDoc call = " new Ice.Autocompleter('" + clientId + "','" + divId +
80                       "', " + component.getOptions() + " ,'" + component.getRowClass() + "','" +
81                       component.getSelectedRowClass() + "');";
82
83         if (!domContext.isInitialized()) {
84             Element JavaDoc root = domContext.createRootElement(HTML.DIV_ELEM);
85             Element JavaDoc input = domContext.createElement(HTML.INPUT_ELEM);
86             input.setAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_TEXT);
87             setRootElementId(facesContext, input, uiComponent);
88             root.appendChild(input);
89             input.setAttribute(HTML.NAME_ATTR, clientId);
90             input.setAttribute(HTML.CLASS_ATTR, component.getInputTextClass());
91             String JavaDoc inputStyle = component.getWidthAsStyle();
92             if(inputStyle != null && inputStyle.length() > 0)
93                 input.setAttribute(HTML.STYLE_ATTR, inputStyle);
94             else
95                 input.removeAttribute(HTML.STYLE_ATTR);
96             input.setAttribute("autocomplete", "off");
97             Element JavaDoc div = domContext.createElement(HTML.DIV_ELEM);
98             String JavaDoc listClass = component.getListClass();
99
100             div.setAttribute(HTML.ID_ATTR, divId);
101             if(listClass == null){
102                 div.setAttribute(HTML.STYLE_ATTR,
103                              "display:none;border:1px solid black;background-color:white;z-index:500;");
104             }else{
105                 div.setAttribute(HTML.CLASS_ATTR, listClass);
106             }
107             root.appendChild(div);
108             String JavaDoc rootStyle = component.getStyle();
109             if(rootStyle != null && rootStyle.length() > 0)
110                 root.setAttribute(HTML.STYLE_ATTR, rootStyle);
111             else
112                 root.removeAttribute(HTML.STYLE_ATTR);
113             root.setAttribute(HTML.CLASS_ATTR, component.getStyleClass());
114             
115           // Element script = domContext.createElement(HTML.SCRIPT_ELEM);
116
// script.setAttribute(HTML.SCRIPT_LANGUAGE_ATTR,
117
// HTML.SCRIPT_LANGUAGE_JAVASCRIPT);
118
// String scriptCode = "window.onLoad(function(){" + call + "});";
119
// Node node = domContext.createTextNode(scriptCode);
120
// script.appendChild(node);
121
// root.appendChild(script);
122
if (log.isDebugEnabled()) {
123                 log.debug(
124                         "SelectInputText:encodeBegin():component created with the following id : " +
125                         clientId);
126             }
127         }
128         Set JavaDoc excludes = new HashSet JavaDoc();
129         excludes.add(HTML.ONKEYDOWN_ATTR);
130         excludes.add(HTML.ONKEYUP_ATTR);
131         excludes.add(HTML.ONFOCUS_ATTR);
132         excludes.add(HTML.ONBLUR_ATTR);
133         PassThruAttributeRenderer.renderAttributes(facesContext, uiComponent, getExcludesArray(excludes));
134         JavascriptContext.addJavascriptCall(facesContext, call);
135     }
136
137     public void encodeChildren(FacesContext facesContext,
138                                UIComponent uiComponent)
139             throws IOException JavaDoc {
140         DOMContext domContext =
141                 DOMContext.getDOMContext(facesContext, uiComponent);
142         SelectInputText component = (SelectInputText) uiComponent;
143         Element JavaDoc input = (Element JavaDoc) domContext.getRootNode().getFirstChild();
144
145         input.setAttribute("onfocus", "setFocus(this.id);");
146         input.setAttribute("onblur", "setFocus('');");
147         // this would prevent, when first valueChangeListener fires with null value
148
Object JavaDoc value = component.getValue();
149         if (value != null) {
150             input.setAttribute(HTML.VALUE_ATTR, value.toString());
151             // populate list of values only if the component's value have been changed.
152
if (component.hasChanged() && value.toString().length() > 0) {
153                 if (log.isDebugEnabled()) {
154                     log.debug(
155                             "SelectInputText:encodeChildren(): component's value have been changed, start populating list : ");
156                 }
157                 populateList(facesContext, uiComponent);
158                 component.setChangedComponentId(null);
159             }
160         }
161         renderAttribute(uiComponent, input, HTML.DISABLED_ATTR,
162                         HTML.DISABLED_ATTR);
163         renderAttribute(uiComponent, input, HTML.READONLY_ATTR,
164                         HTML.READONLY_ATTR);
165         domContext.stepOver();
166         domContext.streamWrite(facesContext, uiComponent);
167     }
168
169
170     public void populateList(FacesContext facesContext, UIComponent uiComponent)
171             throws IOException JavaDoc {
172         if (uiComponent instanceof UIInput) {
173             if (log.isTraceEnabled()) {
174                 log.trace("populateList");
175             }
176             SelectInputText component = ((SelectInputText) uiComponent);
177             Iterator JavaDoc matchs = component.getItemList();
178
179             if (component.getSelectFacet() != null) {
180                 if (log.isDebugEnabled()) {
181                     log.debug(
182                             "SelectInputText:populateList(): \"selectInputText\" facet found, generate generic html for list");
183                 }
184                 UIComponent facet = component.getSelectFacet();
185                 DOMContext domContext =
186                         DOMContext.getDOMContext(facesContext, uiComponent);
187
188                 Element JavaDoc listDiv = domContext.createElement(HTML.DIV_ELEM);
189                 Map JavaDoc requestMap =
190                         facesContext.getExternalContext().getRequestMap();
191                 //set index to 0, so child components can get client id from autoComplete component
192
component.setIndex(0);
193                 while (matchs.hasNext()) {
194                     Element JavaDoc div = domContext.createElement(HTML.DIV_ELEM);
195                     SelectItem item = (SelectItem) matchs.next();
196                     requestMap.put(component.getListVar(), item.getValue());
197                     listDiv.appendChild(div);
198                     // When HTML is display we still need a selected value. Hidding the value in a hidden span
199
// accomplishes this.
200
Element JavaDoc spanToDisplay =
201                             domContext.createElement(HTML.SPAN_ELEM);
202                     spanToDisplay.setAttribute(HTML.CLASS_ATTR, "informal");
203                     div.appendChild(spanToDisplay);
204                     domContext.setCursorParent(spanToDisplay);
205                     encodeParentAndChildren(facesContext, facet);
206                     Element JavaDoc spanToSelect =
207                             domContext.createElement(HTML.SPAN_ELEM);
208                     spanToSelect.setAttribute(HTML.STYLE_ATTR,
209                                               "visibility:hidden;display:none;");
210                     Text JavaDoc label = domContext.createTextNode(DOMUtils.escapeAnsi(item.getLabel()));
211                     spanToSelect.appendChild(label);
212                     div.appendChild(spanToSelect);
213                     component.resetId(facet);
214                 }
215                 component.setIndex(-1);
216
217                 String JavaDoc nodeValue =
218                         DOMUtils.nodeToString(listDiv).replaceAll("\n", "");
219                 String JavaDoc call = "Autocompleter.Finder.find('" +
220                               component.getClientId(facesContext) +
221                               "').updateNOW('" + nodeValue + "');";
222                 JavascriptContext.addJavascriptCall(facesContext, call);
223
224             } else {
225                 if (log.isDebugEnabled()) {
226                     log.debug(
227                             "SelectInputText:populateList(): \"selectItem(s)\" found, generate plain-text for list");
228                 }
229                 if (matchs.hasNext()) {
230                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<div>");
231                     SelectItem item = null;
232                     while (matchs.hasNext()) {
233                         item = (SelectItem) matchs.next();
234                         sb.append("<div>").append(DOMUtils.escapeAnsi(item.getLabel()))
235                                 .append("</div>");
236                     }
237                     sb.append("</div>");
238                     String JavaDoc call = "Autocompleter.Finder.find('" +
239                                   component.getClientId(facesContext) +
240                                   "').updateNOW('" + sb.toString() + "');";
241                     JavascriptContext.addJavascriptCall(facesContext, call);
242                 }
243             }//outer if
244
}
245     }
246 }
247
Popular Tags