KickJava   Java API By Example, From Geeks To Geeks.

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


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.component.CSS_DEFAULT;
37 import com.icesoft.faces.component.ext.HtmlInputText;
38 import com.icesoft.faces.component.ext.KeyEvent;
39 import com.icesoft.faces.component.ext.renderkit.FormRenderer;
40 import com.icesoft.faces.component.ext.taglib.Util;
41 import com.icesoft.faces.context.effects.JavascriptContext;
42
43 import javax.faces.component.NamingContainer;
44 import javax.faces.component.UIComponent;
45 import javax.faces.context.FacesContext;
46 import javax.faces.el.ValueBinding;
47 import javax.faces.event.AbortProcessingException;
48 import javax.faces.event.ActionEvent;
49 import javax.faces.event.FacesEvent;
50 import javax.faces.event.ValueChangeEvent;
51 import javax.faces.model.SelectItem;
52 import java.io.IOException JavaDoc;
53 import java.util.*;
54 import java.text.DateFormat JavaDoc;
55
56
57 /**
58  * SelectInputText is a JSF component class that represents an ICEfaces
59  * autocomplete input text. This component requires the application developer to
60  * implement the matching list rearch algorithm in their backing bean.
61  * <p/>
62  * SelectInputText extends the ICEfaces extended HtmlInputText component.
63  * <p/>
64  * By default this component is rendered by the "com.icesoft.faces.SelectInputText"
65  * renderer type.
66  */

67 public class SelectInputText extends HtmlInputText implements NamingContainer {
68     public static final String JavaDoc COMPONENT_TYPE =
69             "com.icesoft.faces.SelectInputText";
70
71     //default style classes
72

73     //private properties for style classes
74
private String JavaDoc styleClass;
75
76
77     /**
78      * A request-scope attribute under which the model data for the row selected
79      * by the current value of the "rowIndex" property will be exposed.
80      */

81     private String JavaDoc listVar = null;
82
83     /**
84      * A state variable for number of rows to be matched.
85      */

86     private Integer JavaDoc rows;
87     private final int DEFAULT_MAX_MATCHS = 10;
88
89     /**
90      * A state variable for width of both inputtext and dropdownlist
91      */

92     private String JavaDoc width;
93     private final String JavaDoc DEFAULT_WIDTH = "150";
94
95     /**
96      * A property to store the selectedItem, after successfull match
97      */

98     private SelectItem selectedItem = null;
99
100     /**
101      * list of selectItems
102      */

103     List itemList;
104
105     private String JavaDoc options;
106
107     /**
108      * Map for selectItems, where the key is the "SelectItem.getlabel()" and the
109      * value is the selectItem object
110      */

111     Map itemMap = new HashMap();
112
113     private int index = -1;
114
115     public SelectInputText() {
116         super();
117         JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
118                                      FacesContext.getCurrentInstance());
119     }
120
121     /*
122     * (non-Javadoc)
123     * @see javax.faces.component.UIComponent#encodeBegin(javax.faces.context.FacesContext)
124     */

125     public void encodeBegin(FacesContext context) throws IOException JavaDoc {
126         super.encodeBegin(context);
127     }
128
129     /*
130     * (non-Javadoc)
131     * @see javax.faces.component.UIComponent#decode(javax.faces.context.FacesContext)
132     */

133     public void decode(FacesContext facesContext) {
134         super.decode(facesContext);
135         setSelectedItem(facesContext);
136         if (Util.isEventSource(facesContext,this)) {
137           queueEventIfEnterKeyPressed(facesContext);
138          // queueEvent(new ActionEvent(this));
139
}
140
141     }
142
143     /**
144      * this method is responsible to setting the seletecdItem on the component
145      *
146      * @param facesContext
147      */

148     private void setSelectedItem(FacesContext facesContext) {
149         Map requestMap =
150                 facesContext.getExternalContext().getRequestParameterMap();
151         String JavaDoc clientId = getClientId(facesContext);
152         String JavaDoc value = (String JavaDoc) requestMap.get(clientId);
153         setSelectedItem(value);
154     }
155
156
157     /**
158      * return true if I had focus when submitted
159      *
160      * @param facesContext
161      * @return focus
162      */

163     private boolean hadFocus(FacesContext facesContext) {
164         Object JavaDoc focusId = facesContext.getExternalContext()
165                 .getRequestParameterMap().get(FormRenderer.getFocusElementId());
166         boolean focus = false;
167         if (focusId != null) {
168             if (focusId.toString().equals(getClientId(facesContext))) {
169                 focus = true;
170             }
171         }
172         setFocus(focus);
173         return focus;
174     }
175
176     //this list would be set in populateItemList()
177

178     /**
179      * <p>Return the value of the <code>itemList</code> property.</p>
180      */

181     public Iterator getItemList() {
182         if (itemList == null) {
183             return Collections.EMPTY_LIST.iterator();
184         }
185         return itemList.iterator();
186     }
187
188     /**
189      * <p>Set the value of the <code>index</code> property.</p>
190      */

191     public void setIndex(int index) {
192         this.index = index;
193
194     }
195
196     /**
197      * <p>Return the value of the <code>clientId</code> property.</p>
198      */

199     public String JavaDoc getClientId(FacesContext context) {
200         if (context == null) {
201             throw new NullPointerException JavaDoc();
202         }
203         String JavaDoc baseClientId = super.getClientId(context);
204         if (index >= 0) {
205             return (baseClientId + NamingContainer.SEPARATOR_CHAR + index++);
206         } else {
207             return (baseClientId);
208         }
209     }
210
211     /**
212      * reset parent's and its children's ids
213      */

214     void resetId(UIComponent component) {
215         String JavaDoc id = component.getId();
216         component.setId(id); // Forces client id to be reset
217
Iterator kids = component.getChildren().iterator();
218         while (kids.hasNext()) {
219             UIComponent kid = (UIComponent) kids.next();
220             resetId(kid);
221         }
222
223     }
224
225     //this method generating the list of selectItems "itemList", which can be bounded
226
//with the bean, or could be static on jsf page
227
//matches list can be change after value change event, so we are calling
228
//this method after value change event in broadcast(), where the method bounded
229
//with valueChangeListner is being called and updates the data model.
230
void populateItemList() {
231         if (getSelectFacet() != null) {
232             //facet being used on jsf page, so get the list from the value binding
233
itemList = getListValue();
234         } else {
235             //selectItem or selectItems has been used on jsf page, so get the selectItems
236
itemList = Util.getSelectItems(FacesContext.getCurrentInstance(),
237                                            this);
238         }
239         try {
240             Iterator items = itemList.iterator();
241             SelectItem item = null;
242             itemMap.clear();
243             while (items.hasNext()) {
244                 item = (SelectItem) items.next();
245                 itemMap.put(item.getLabel(), item);
246             }
247         } catch(NullPointerException JavaDoc e) {
248             e.printStackTrace();
249         }
250     }
251
252     /* (non-Javadoc)
253     * @see javax.faces.component.UIComponent#broadcast(javax.faces.event.FacesEvent)
254     */

255     public void broadcast(FacesEvent event) throws AbortProcessingException {
256         //keyevent should be process by this component
257
super.broadcast(event);
258         if ((event instanceof ValueChangeEvent)) {
259             populateItemList();
260             setChangedComponentId(
261                     this.getClientId(FacesContext.getCurrentInstance()));
262         }
263     }
264
265     /**
266      * <p>Return the value of the <code>selectInputText</code> property.</p>
267      */

268     public UIComponent getSelectFacet() {
269         return (UIComponent) getFacet("selectInputText");
270     }
271
272     /**
273      * <p>Set the value of the <code>selectedItem</code> property.</p>
274      */

275     public void setSelectedItem(String JavaDoc key) {
276
277         this.selectedItem = (SelectItem) itemMap.get(key);
278     }
279
280     /**
281      * <p>Return the value of the <code>selectedItem</code> property.</p>
282      */

283     public SelectItem getSelectedItem() {
284         return selectedItem;
285     }
286
287     //property to set the max matches to be displayed
288

289     /**
290      * <p>Set the value of the <code>rows</code> property.</p>
291      */

292     public void setRows(int rows) {
293         this.rows = new Integer JavaDoc(rows);
294     }
295
296     /**
297      * <p>Return the value of the <code>rows</code> property.</p>
298      */

299     public int getRows() {
300         if (rows != null) {
301             if (itemMap != null) {
302                 return itemMap.size() > 0 && itemMap.size() < rows.intValue() ?
303                        itemMap.size() : rows.intValue();
304             } else {
305                 return rows.intValue();
306             }
307         }
308
309         ValueBinding vb = getValueBinding("rows");
310         return vb != null ?
311                Integer.parseInt(vb.getValue(getFacesContext()).toString()) :
312                DEFAULT_MAX_MATCHS;
313     }
314
315     /**
316      * <p>Set the value of the <code>width</code> property.</p>
317      */

318     public void setWidth(String JavaDoc width) {
319         this.width = width;
320     }
321
322     /**
323      * <p>Set the value of the <code>listVar</code> property.</p>
324      */

325     public void setListVar(String JavaDoc listVar) {
326         this.listVar = listVar;
327     }
328
329     /**
330      * <p>Return the value of the <code>listVar</code> property.</p>
331      */

332     public String JavaDoc getListVar() {
333         if (listVar != null) {
334             return listVar;
335         }
336         ValueBinding vb = getValueBinding("listVar");
337         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
338     }
339
340     /**
341      * <p>Set the value of the <code>listValue</code> property.</p>
342      */

343     public void setListValue(List listValue) {
344         this.itemList = listValue;
345     }
346
347     /**
348      * <p>Return the value of the <code>listValue</code> property.</p>
349      */

350     public List getListValue() {
351         ValueBinding vb = getValueBinding("listValue");
352         return (List) vb.getValue(FacesContext.getCurrentInstance());
353     }
354
355     /**
356      * <p>Return the value of the <code>width</code> property.</p>
357      */

358     public String JavaDoc getWidth() {
359         if (width != null) {
360             return width;
361         }
362         ValueBinding vb = getValueBinding("width");
363         return vb != null ? vb.getValue(getFacesContext()).toString() :
364                DEFAULT_WIDTH;
365     }
366
367     String JavaDoc getWidthAsStyle() {
368         try {//no measurement unit defined, so add the px unit
369
int width = Integer.parseInt(getWidth());
370             return "width:" + width + "px;";
371         } catch (NumberFormatException JavaDoc e) {
372             return "width:" + getWidth().trim();
373         }
374     }
375
376     /**
377      * <p>Set the value of the <code>styleClass</code> property.</p>
378      */

379     public void setStyleClass(String JavaDoc styleClass) {
380         this.styleClass = styleClass;
381     }
382
383     /**
384      * <p>Return the value of the <code>styleClass</code> property.</p>
385      */

386     public String JavaDoc getStyleClass() {
387         return Util.getQualifiedStyleClass(this,
388                             styleClass,
389                             CSS_DEFAULT.DEFAULT_SELECT_INPUT,
390                             "styleClass",
391                             isDisabled());
392     }
393
394     /**
395      * <p>Return the value of the <code>inputTextClass</code> property.</p>
396      */

397     public String JavaDoc getInputTextClass() {
398         return Util.getQualifiedStyleClass(this,
399                 CSS_DEFAULT.DEFAULT_SELECT_INPUT_TEXT_CLASS, isDisabled());
400     }
401
402
403     /**
404      * <p>Return the value of the <code>listClass</code> property.</p>
405      */

406     public String JavaDoc getListClass() {
407         return Util.getQualifiedStyleClass(this,
408                 CSS_DEFAULT.DEFAULT_SELECT_INPUT_LIST_CLASS, isDisabled());
409     }
410
411
412     /**
413      * <p>Return the value of the <code>rowClass</code> property.</p>
414      */

415     public String JavaDoc getRowClass() {
416         return Util.getQualifiedStyleClass(this,
417                 CSS_DEFAULT.DEFAULT_SELECT_INPUT_ROW_CLASS, isDisabled());
418     }
419
420
421     /**
422      * <p>Return the value of the <code>selectedRowClass</code> property.</p>
423      */

424     public String JavaDoc getSelectedRowClass() {
425         return Util.getQualifiedStyleClass(this,
426                 CSS_DEFAULT.DEFAULT_SELECT_INPUT_SELECTED_ROW_CLASS, isDisabled());
427     }
428
429     public String JavaDoc getOptions() {
430         return options;
431     }
432
433     public void setOptions(String JavaDoc options) {
434         this.options = options;
435     }
436     
437
438     //the following code is a fix for iraptor bug 347
439
//on first page submit, all input elements gets valueChangeEvent (null to ""),
440
//so component's ids can be more then one
441
private List changedComponentIds = new ArrayList();
442
443     /**
444      * <p>Set the value of the <code>selectedPanel</code> property.</p>
445      */

446     void setChangedComponentId(Object JavaDoc id) {
447         if (id == null) {
448             changedComponentIds.clear();
449         } else {
450             changedComponentIds.add(id);
451         }
452     }
453
454     /**
455      * <p>Return the value of the <code>selectedPanel</code> property.</p>
456      */

457     boolean hasChanged() {
458         return changedComponentIds
459                 .contains(this.getClientId(FacesContext.getCurrentInstance()));
460     }
461
462     /**
463      * queue the event if the enter key was pressed
464      *
465      * @param facesContext
466      */

467     private void queueEventIfEnterKeyPressed(FacesContext facesContext) {
468         try {
469             Map requestParemeterMap = facesContext.getExternalContext()
470                 .getRequestParameterMap();
471             KeyEvent keyEvent =
472                     new KeyEvent(this, requestParemeterMap);
473
474             if (keyEvent.getKeyCode() == KeyEvent.CARRIAGE_RETURN) {
475                 queueEvent(new ActionEvent(this));
476             }
477             if("true".equals(requestParemeterMap.get("ice.event.left"))){
478                 queueEvent(new ActionEvent(this));
479             }
480             if("onclick".equals(requestParemeterMap.get("ice.event.type"))){
481                 queueEvent(new ActionEvent(this));
482             }
483
484
485         } catch (Exception JavaDoc e) {
486             e.printStackTrace();
487         }
488     }
489
490
491
492     /**
493      * <p>Gets the state of the instance as a <code>Serializable</code>
494      * Object.</p>
495      */

496     public Object JavaDoc saveState(FacesContext context) {
497         Object JavaDoc values[] = new Object JavaDoc[8];
498         values[0] = super.saveState(context);
499         values[1] = styleClass;
500         values[2] = listVar;
501         values[3] = rows;
502         values[4] = width;
503         values[5] = selectedItem;
504         values[6] = itemList;
505         values[7] = options;
506         return ((Object JavaDoc) (values));
507     }
508
509     /**
510      * <p>Perform any processing required to restore the state from the entries
511      * in the state Object.</p>
512      */

513     public void restoreState(FacesContext context, Object JavaDoc state) {
514         Object JavaDoc values[] = (Object JavaDoc[]) state;
515         super.restoreState(context, values[0]);
516         styleClass = (String JavaDoc) values[1];
517         listVar = (String JavaDoc) values[2];
518         rows = (Integer JavaDoc) values[3];
519         width = (String JavaDoc) values[4];
520         selectedItem = (SelectItem) values[5];
521         itemList = (List) values[6];
522         options = (String JavaDoc)values[7];
523     }
524 }
525
Popular Tags