org.omnifaces.converter.ListConverter Maven / Gradle / Ivy
/*
* Copyright OmniFaces
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.omnifaces.converter;
import java.util.List;
import java.util.Objects;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;
/**
*
* The omnifaces.ListConverter
is intented for use in specialized selection components which doesn't
* use {@link SelectItem}s as the source for their selectable items, but work directly via a {@link List} of entities,
* and therefore the {@link SelectItemsConverter} isn't usable on them.
*
* This converter allows you to populate a selection component with complex Java objects and have JSF convert those
* automatically back without the need to provide a custom converter which may need to do the job based on possibly
* expensive service/DAO operations. This converter automatically converts based on the {@link #toString()} of the
* selected item.
*
*
Usage
*
* This converter is available by converter ID omnifaces.ListConverter
and should be used in combination
* with <o:converter>
in order to be able to pass the {@link List} source to it, which it can use
* for conversion. Here's a basic usage example with PrimeFaces <p:pickList>
, which is one of the
* few select components which doesn't use {@link SelectItem}s as the source, but work directly via a {@link List}.
*
* <p:pickList value="#{bean.dualListModel}" var="entity" itemValue="#{entity}" itemLabel="#{entity.someProperty}">
* <o:converter converterId="omnifaces.ListConverter" list="#{bean.dualListModel.source}" />
* </p:pickList>
*
*
* Make sure that your entity has a good toString()
implementation
*
* For detail, refer the javadoc of {@link SelectItemsConverter} and substitute "SelectItemsConverter
" by
* "ListConverter
" and "SelectItemsIndexConverter
" by "ListIndexConverter
".
*
* @since 1.5
* @author Arjan Tijms
*/
@FacesConverter("omnifaces.ListConverter")
public class ListConverter implements Converter {
private List> list;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
for (Object listValue : list) {
String convertedListValue = getAsString(context, component, listValue);
if (Objects.equals(value, convertedListValue)) {
return listValue;
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return null;
}
return value.toString();
}
public void setList(List> list) {
this.list = list;
}
}