This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
AutocompleteBean.java
77 lines (63 loc) · 2.07 KB
/
AutocompleteBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.richfaces.demo.autocomplete;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import org.richfaces.demo.tables.model.capitals.Capital;
@ManagedBean
@RequestScoped
public class AutocompleteBean {
private String value;
private List<String> autocompleteList;
@ManagedProperty(value = "#{capitalsParser.capitalsList}")
private List<Capital> capitals;
public AutocompleteBean() {
// TODO Auto-generated constructor stub
}
@PostConstruct
public void init() {
autocompleteList = new ArrayList<String>();
for (Capital cap : capitals) {
autocompleteList.add(cap.getState());
}
}
public List<String> autocomplete(String prefix) {
ArrayList<String> result = new ArrayList<String>();
if ((prefix == null) || (prefix.length() == 0)) {
for (int i = 0; i < 10; i++) {
result.add(capitals.get(i).getState());
}
} else {
Iterator<Capital> iterator = capitals.iterator();
while (iterator.hasNext()) {
Capital elem = ((Capital) iterator.next());
if ((elem.getState() != null && elem.getState().toLowerCase().indexOf(prefix.toLowerCase()) == 0)
|| "".equals(prefix)) {
result.add(elem.getState());
}
}
}
return result;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<String> getAutocompleteList() {
return autocompleteList;
}
public void setAutocompleteList(List<String> autocompleteList) {
this.autocompleteList = autocompleteList;
}
public List<Capital> getCapitals() {
return capitals;
}
public void setCapitals(List<Capital> capitals) {
this.capitals = capitals;
}
}