KeyFilter can be used to filter keyboard input on specified input components.
<div class="card">
<h:form>
<h5>Number</h5>
<p:inputText>
<p:keyFilter mask="num" />
</p:inputText>
<h5>Regex</h5>
<p:inputText>
<p:keyFilter regEx="/[ABC]/i"/>
</p:inputText>
<h5>AutoComplete with TestFunction</h5>
<p:autoComplete id="autoComplete1" value="#{autoCompleteView.txt1}"
completeMethod="#{autoCompleteView.completeText}"/>
<p:keyFilter for="autoComplete1" testFunction="return c == 'z';"/>
</h:form>
</div>
@Named
@RequestScoped
public class AutoCompleteView {
private String txt1;
private String txt2;
private String txt3;
private String txt4;
private String txt5;
private String txt6;
private String txt7;
private String txt8;
private String txt9;
private Country country1;
private Country country2;
private Country country3;
private Country country4;
private Country country5;
private List<Country> selectedCountries;
@Inject
private CountryService countryService;
public List<String> completeText(String query) {
String queryLowerCase = query.toLowerCase();
List<String> countryList = new ArrayList<>();
List<Country> countries = countryService.getCountries();
for (Country country : countries) {
countryList.add(country.getName());
}
return countryList.stream().filter(t -> t.toLowerCase().startsWith(queryLowerCase)).collect(Collectors.toList());
}
public List<String> noResults(String query) {
return Collections.EMPTY_LIST;
}
public List<Country> completeCountry(String query) {
String queryLowerCase = query.toLowerCase();
List<Country> countries = countryService.getCountries();
return countries.stream().filter(t -> t.getName().toLowerCase().contains(queryLowerCase)).collect(Collectors.toList());
}
public void onItemSelect(SelectEvent<String> event) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Country Selected", event.getObject()));
}
public void onEmptyMessageSelect() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Empty message selected"));
}
public String getTxt1() {
return txt1;
}
public void setTxt1(String txt1) {
this.txt1 = txt1;
}
public String getTxt2() {
return txt2;
}
public void setTxt2(String txt2) {
this.txt2 = txt2;
}
public String getTxt3() {
return txt3;
}
public void setTxt3(String txt3) {
this.txt3 = txt3;
}
public String getTxt4() {
return txt4;
}
public void setTxt4(String txt4) {
this.txt4 = txt4;
}
public String getTxt5() {
return txt5;
}
public void setTxt5(String txt5) {
this.txt5 = txt5;
}
public String getTxt6() {
return txt6;
}
public void setTxt6(String txt6) {
this.txt6 = txt6;
}
public String getTxt7() {
return txt7;
}
public void setTxt7(String txt7) {
this.txt7 = txt7;
}
public String getTxt8() {
return txt8;
}
public void setTxt8(String txt8) {
this.txt8 = txt8;
}
public String getTxt9() {
return txt9;
}
public void setTxt9(String txt9) {
this.txt9 = txt9;
}
public Country getCountry1() {
return country1;
}
public void setCountry1(Country country1) {
this.country1 = country1;
}
public Country getCountry2() {
return country2;
}
public void setCountry2(Country country2) {
this.country2 = country2;
}
public Country getCountry3() {
return country3;
}
public void setCountry3(Country country3) {
this.country3 = country3;
}
public Country getCountry4() {
return country4;
}
public void setCountry4(Country country4) {
this.country4 = country4;
}
public Country getCountry5() {
return country5;
}
public void setCountry5(Country country5) {
this.country5 = country5;
}
public List<Country> getSelectedCountries() {
return selectedCountries;
}
public void setSelectedCountries(List<Country> selectedCountries) {
this.selectedCountries = selectedCountries;
}
public void setCountryService(CountryService countryService) {
this.countryService = countryService;
}
public char getCountryGroup(Country country) {
return country.getName().charAt(0);
}
}
public class Country {
private int id;
private String name;
private String code;
public Country() {}
public Country(int id, String name, String code) {
this.id = id;
this.name = name;
this.code = code;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Country country = (Country) o;
return id == country.id &&
Objects.equals(name, country.name) &&
Objects.equals(code, country.code);
}
@Override
public int hashCode() {
return Objects.hash(id, name, code);
}
@Override
public String toString() {
return name;
}
}
@Named
@ApplicationScoped
public class CountryService {
private List<Country> countries;
@PostConstruct
public void init() {
countries = new ArrayList<>();
String[] locales = Locale.getISOCountries();
for (int i = 0; i < locales.length; i++) {
Locale country = new Locale("", locales[i]);
countries.add(new Country(i, country.getDisplayCountry(), country.getCountry().toLowerCase()));
}
Collections.sort(countries, (Country c1, Country c2) -> c1.getName().compareTo(c2.getName()));
}
public List<Country> getCountries() {
return new ArrayList<>(countries);
}
}