OrderList is used to sort a collection via Drag and Drop based reordering, transition effects, pojo support, country support and more.
<h:form>
<p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true"/>
<div class="card">
<h5>Basic</h5>
<p:orderList value="#{orderListView.cities}" var="city" controlsLocation="none" itemLabel="#{city}" itemValue="#{city}" />
</div>
<div class="card">
<h5>Advanced</h5>
<p:orderList value="#{orderListView.countries}" var="country" itemValue="#{country}"
converter="#{countryConverter}" controlsLocation="left" responsive="true">
<p:ajax event="select" listener="#{orderListView.onSelect}" update="msgs"/>
<p:ajax event="unselect" listener="#{orderListView.onUnselect}" update="msgs"/>
<p:ajax event="reorder" listener="#{orderListView.onReorder}" update="msgs"/>
<f:facet name="caption">Available</f:facet>
<p:column style="width:40px">
<span class="flag flag-#{country.code}" style="width: 30px; height: 20px"/>
</p:column>
<p:column>
<h:outputText value="#{country.name}"/>
</p:column>
</p:orderList>
</div>
</h:form>
@Named
@RequestScoped
public class OrderListView {
@Inject
private CountryService service;
private List<String> cities;
private List<Country> countries;
@PostConstruct
public void init() {
//Cities
cities = new ArrayList<>();
cities.add("San Francisco");
cities.add("London");
cities.add("Paris");
cities.add("Istanbul");
cities.add("Berlin");
cities.add("Barcelona");
cities.add("Rome");
//Countrys
countries = service.getCountries().subList(0, 10);
}
public CountryService getService() {
return service;
}
public void setService(CountryService service) {
this.service = service;
}
public List<String> getCities() {
return cities;
}
public void setCities(List<String> cities) {
this.cities = cities;
}
public List<Country> getCountries() {
return countries;
}
public void setCountries(List<Country> countries) {
this.countries = countries;
}
public void onSelect(SelectEvent<Country> event) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Item Selected", event.getObject().getName()));
}
public void onUnselect(UnselectEvent<Country> event) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Item Unselected", event.getObject().getName()));
}
public void onReorder() {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "List Reordered", null));
}
}
@Named
@ApplicationScoped
public class CountryService {
private List<Country> countries;
private Map<Integer, Country> countriesAsMap;
private List<Country> locales;
private Map<Integer, Country> localesAsMap;
public static Stream<Country> toCountryStream(String... isoCodes) {
return Stream.of(isoCodes)
.map(isoCode -> new Locale("", isoCode))
.map(CountryService::toCountry);
}
public static Country toCountry(Locale locale) {
return CountryService.toCountry(locale, false);
}
public static Country toCountry(Locale locale, boolean rtl) {
//use hash code from locale to have a reproducible ID (required for CountryConverter)
return new Country(locale.hashCode(), locale, rtl);
}
@PostConstruct
public void init() {
countries = CountryService.toCountryStream(Locale.getISOCountries())
.sorted(Comparator.comparing(Country::getName))
.collect(Collectors.toList());
locales = new ArrayList<>();
locales.add(CountryService.toCountry(Locale.US));
locales.add(CountryService.toCountry(Locale.UK));
locales.add(CountryService.toCountry(new Locale("en", "AU")));
locales.add(CountryService.toCountry(Locale.FRANCE));
locales.add(CountryService.toCountry(Locale.GERMANY));
locales.add(CountryService.toCountry(new Locale("de", "AT")));
locales.add(CountryService.toCountry(new Locale("de", "CH")));
locales.add(CountryService.toCountry(Locale.ITALY));
locales.add(CountryService.toCountry(Locale.KOREA));
locales.add(CountryService.toCountry(new Locale("es", "ES")));
locales.add(CountryService.toCountry(new Locale("ca", "ES")));
locales.add(CountryService.toCountry(new Locale("nl", "NL")));
locales.add(CountryService.toCountry(new Locale("pt", "BR")));
locales.add(CountryService.toCountry(new Locale("pt", "PT")));
locales.add(CountryService.toCountry(new Locale("ar", "SA"), true));
locales.add(CountryService.toCountry(new Locale("ar", "TN"), true));
locales.add(CountryService.toCountry(new Locale("bg", "BG")));
locales.add(CountryService.toCountry(new Locale("cs", "CZ")));
locales.add(CountryService.toCountry(new Locale("el", "GR")));
locales.add(CountryService.toCountry(new Locale("fa", "IR"), true));
locales.add(CountryService.toCountry(new Locale("fi", "FI")));
locales.add(CountryService.toCountry(new Locale("da", "DK")));
locales.add(CountryService.toCountry(new Locale("hi", "IN")));
locales.add(CountryService.toCountry(new Locale("in", "ID")));
locales.add(CountryService.toCountry(new Locale("hr", "HR")));
locales.add(CountryService.toCountry(new Locale("ja", "JP")));
locales.add(CountryService.toCountry(new Locale("hu", "HU")));
locales.add(CountryService.toCountry(new Locale("he", "IL"), true));
locales.add(CountryService.toCountry(new Locale("ka", "GE")));
locales.add(CountryService.toCountry(new Locale("ckb", "IQ"), true));
locales.add(CountryService.toCountry(new Locale("km", "KH")));
locales.add(CountryService.toCountry(new Locale("ky", "KG")));
locales.add(CountryService.toCountry(new Locale("kk", "KZ")));
locales.add(CountryService.toCountry(new Locale("lt", "LT")));
locales.add(CountryService.toCountry(new Locale("lv", "LV")));
locales.add(CountryService.toCountry(new Locale("ms", "MY")));
locales.add(CountryService.toCountry(new Locale("no", "NO")));
locales.add(CountryService.toCountry(new Locale("pl", "PL")));
locales.add(CountryService.toCountry(new Locale("ro", "RO")));
locales.add(CountryService.toCountry(new Locale("ru", "RU")));
locales.add(CountryService.toCountry(new Locale("sk", "SK")));
locales.add(CountryService.toCountry(new Locale("sl", "SI")));
locales.add(CountryService.toCountry(new Locale("sr", "BA")));
locales.add(CountryService.toCountry(new Locale("sr", "RS")));
locales.add(CountryService.toCountry(new Locale("sv", "SE")));
locales.add(CountryService.toCountry(new Locale("th", "TH")));
locales.add(CountryService.toCountry(new Locale("tr", "TR")));
locales.add(CountryService.toCountry(new Locale("uk", "UA")));
locales.add(CountryService.toCountry(new Locale("vi", "VN")));
locales.add(CountryService.toCountry(Locale.SIMPLIFIED_CHINESE));
locales.add(CountryService.toCountry(Locale.TRADITIONAL_CHINESE));
}
public List<Country> getCountries() {
return new ArrayList<>(countries);
}
public Map<Integer, Country> getCountriesAsMap() {
if (countriesAsMap == null) {
countriesAsMap = getCountries().stream().collect(Collectors.toMap(Country::getId, country -> country));
}
return countriesAsMap;
}
public List<Country> getLocales() {
return new ArrayList<>(locales);
}
public Map<Integer, Country> getLocalesAsMap() {
if (localesAsMap == null) {
localesAsMap = getLocales().stream().collect(Collectors.toMap(Country::getId, country -> country));
}
return localesAsMap;
}
}
public class Country implements Serializable, Comparable<Country> {
private int id;
private String name;
private String code;
private Locale locale;
private boolean rtl;
public Country() {
}
public Country(int id, Locale locale) {
this(id, locale.getDisplayCountry(), locale.getCountry().toLowerCase(), locale);
}
public Country(int id, Locale locale, boolean rtl) {
this(id, locale.getDisplayCountry(), locale.getCountry().toLowerCase(), locale);
this.rtl = rtl;
}
public Country(int id, String name, String code) {
this(id, name, code, null);
}
public Country(int id, String name, String code, Locale locale) {
this.id = id;
this.name = name;
this.code = code;
this.locale = locale;
}
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;
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public String getLanguage() {
return locale == null ? "en" : locale.getLanguage();
}
public String getDisplayLanguage() {
return locale == null ? "English" : locale.getDisplayLanguage();
}
public boolean isRtl() {
return rtl;
}
public void setRtl(boolean rtl) {
this.rtl = rtl;
}
@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;
}
@Override
public int compareTo(Country o) {
return name.compareTo(o.name);
}
}
@Named
@ApplicationScoped
@FacesConverter(value = "countryConverter", managed = true)
public class CountryConverter implements Converter<Country> {
@Inject
private CountryService countryService;
@Override
public Country getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && value.trim().length() > 0) {
try {
return countryService.getCountriesAsMap().get(Integer.parseInt(value));
}
catch (NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid country."));
}
}
else {
return null;
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Country value) {
if (value != null) {
return String.valueOf(value.getId());
}
else {
return null;
}
}
}