KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > autocomplete > AutoCompleteBean


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.icefaces.samples.showcase.components.autocomplete;
35
36 import com.icesoft.faces.component.selectinputtext.SelectInputText;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 import javax.faces.event.ValueChangeEvent;
41 import javax.faces.model.SelectItem;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46
47 /**
48  * Stores the values picked from the AutoCompleteDictionary (different scope to
49  * avoid memory hole). The showcase stores one static copy of the actual
50  * dictionary information.
51  *
52  * @see AutoCompleteDictionary
53  */

54 public class AutoCompleteBean {
55
56     private static Log log = LogFactory.getLog(AutoCompleteBean.class);
57
58     // list of cities, used for auto complete list.
59
private static List JavaDoc dictionary;
60
61
62     // default city, no value.
63
private City currentCity = new City("", "", "", "", "", "", 0);
64
65     // list of possible matches.
66
private List JavaDoc matchesList = new ArrayList JavaDoc();
67
68     /**
69      * Called when a user has modifed the SelectInputText value. This method
70      * call causes the match list to be updated.
71      *
72      * @param event
73      */

74     public void updateList(ValueChangeEvent event) {
75
76         // get a new list of matches.
77
setMatches(event);
78
79         // Get the auto complete component from the event and assing
80
if (event.getComponent() instanceof SelectInputText) {
81             SelectInputText autoComplete =
82                     (SelectInputText) event.getComponent();
83             // if no selected item then return the previously selected item.
84
if (autoComplete.getSelectedItem() != null) {
85                 currentCity = (City) autoComplete.getSelectedItem().getValue();
86             }
87             // otherwise if there is a selected item get the value from the match list
88
else {
89                 City tempCity = getMatch(autoComplete.getValue().toString());
90                 if (tempCity != null) {
91                     currentCity = tempCity;
92                 }
93             }
94         }
95     }
96
97     /**
98      * Gets the currently selected city.
99      *
100      * @return selected city.
101      */

102     public City getCurrentCity() {
103         return currentCity;
104     }
105
106     /**
107      * The list of possible matches for the given SelectInputText value
108      *
109      * @return list of possible matches.
110      */

111     public List JavaDoc getList() {
112         return matchesList;
113     }
114
115     private City getMatch(String JavaDoc value) {
116         City result = null;
117         if (matchesList != null) {
118             SelectItem si;
119             Iterator JavaDoc iter = matchesList.iterator();
120             while (iter.hasNext()) {
121                 si = (SelectItem) iter.next();
122                 if (value.equals(si.getLabel())) {
123                     result = (City) si.getValue();
124                 }
125             }
126         }
127         return result;
128     }
129
130
131     public List JavaDoc getDictionary() {
132         return dictionary;
133     }
134
135     public void setDictionary(List JavaDoc dictionary) {
136         AutoCompleteBean.dictionary = dictionary;
137     }
138
139     /**
140      * Utility method for building the match list given the current value of the
141      * SelectInputText component.
142      *
143      * @param event
144      */

145     private void setMatches(ValueChangeEvent event) {
146
147         Object JavaDoc searchWord = event.getNewValue();
148         int maxMatches = ((SelectInputText) event.getComponent()).getRows();
149         List JavaDoc matchList = new ArrayList JavaDoc(maxMatches);
150
151         try {
152
153             int insert = Collections.binarySearch(dictionary, searchWord,
154                                                   AutoCompleteDictionary.LABEL_COMPARATOR);
155
156             // less then zero if wer have a partial match
157
if (insert < 0) {
158                 insert = Math.abs(insert) - 1;
159             }
160
161             for (int i = 0; i < maxMatches; i++) {
162                 // quit the match list creation if the index is larger then
163
// max entries in the dictionary if we have added maxMatches.
164
if ((insert + i) >= dictionary.size() ||
165                     i >= maxMatches) {
166                     break;
167                 }
168                 matchList.add(dictionary.get(insert + i));
169             }
170         } catch (Throwable JavaDoc e) {
171             log.error("Erorr finding autocomplete matches", e);
172         }
173         // assign new matchList
174
if (this.matchesList != null) {
175             this.matchesList.clear();
176             this.matchesList = null;
177         }
178         this.matchesList = matchList;
179     }
180 }
Popular Tags