Skip to content
Browse files

RF-11117: Implemented the getConvertedValue method for SelectMnanyRen…

…dererBase
  • Loading branch information...
1 parent 3e4f984 commit 0ee4646ceaf679fc74660daf1226645c4fb962c9 @bleathem bleathem committed Jul 23, 2011
View
3 input/ui/src/main/java/org/richfaces/component/AbstractSelectManyComponent.java
@@ -62,6 +62,9 @@
public abstract String getDisabledClass();
@Attribute
+ public abstract boolean isDisabled();
+
+ @Attribute
public abstract String getEscape();
@Attribute
View
164 input/ui/src/main/java/org/richfaces/renderkit/SelectManyRendererBase.java
@@ -25,21 +25,33 @@
import org.richfaces.component.util.HtmlUtil;
import org.richfaces.renderkit.util.HtmlDimensions;
+import javax.el.ValueExpression;
+import javax.faces.FacesException;
import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
+import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
+import javax.faces.component.ValueHolder;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
import javax.faces.model.SelectItem;
import java.io.IOException;
+import java.lang.reflect.Array;
+import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
+import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Queue;
import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
/**
* @author <a href="http://community.jboss.org/people/bleathem">Brian Leathem</a>
@@ -56,28 +68,28 @@
@ResourceDependency(library = "org.richfaces", name = "popupList.js"),
@ResourceDependency(library = "org.richfaces", name = "pickList.js"),
@ResourceDependency(library = "org.richfaces", name = "pickList.ecss") })
-public class SelectManyRendererBase extends RendererBase {
+public class SelectManyRendererBase extends InputRendererBase {
public static final String ITEM_CSS = "rf-pick-opt";
+ private static final String HIDDEN_SUFFIX = "Hidden";
public List<ClientSelectItem> getClientSelectItems(FacesContext facesContext, UIComponent component) {
AbstractSelectManyComponent select = (AbstractSelectManyComponent) component;
List<SelectItem> selectItemsAll = SelectHelper.getSelectItems(facesContext, component);
List<ClientSelectItem> clientSelectItems = new ArrayList<ClientSelectItem>();
Object object = select.getValue();
- Object[] values;
+ List values;
if (object == null) {
- values = new Object[0];
+ values = new ArrayList();
}
else if (object instanceof List) {
- List list = (List) object;
- values = list.toArray();
+ values = (List) object;
} else if (object instanceof Object[]) {
- values = (Object[]) object;
+ values = Arrays.asList((Object[]) object);
} else {
throw new IllegalArgumentException("Value expression must evaluate to either a List or Object[]");
}
- Set<Object> valuesSet = new HashSet<Object>(Arrays.asList(values));
+ Set<Object> valuesSet = new HashSet<Object>(values);
int sortOrder = 0;
// TODO: Deal with SelectItemGroups
for (SelectItem selectItem : selectItemsAll) {
@@ -108,29 +120,127 @@ public String csvEncodeSelectedItems(List<ClientSelectItem> clientSelectItems) {
}
@Override
- public void doDecode(FacesContext facesContext, UIComponent component){
- Map requestParams = facesContext.getExternalContext().getRequestParameterMap();
- AbstractSelectManyComponent select = (AbstractSelectManyComponent)component;
- String clientId = select.getClientId(facesContext);
- String submittedValue = (String)requestParams.get(clientId);
- if (submittedValue != null) {
- String[] values = submittedValue.split(",");
- if (select.getValue() instanceof List) {
- select.setSubmittedValue(Arrays.asList(values));
- } else if (select.getValue() instanceof Object[]) {
- select.setSubmittedValue(values);
+ protected void doDecode(FacesContext context, UIComponent component) {
+ if (!(component instanceof AbstractSelectManyComponent)) {
+ throw new IllegalArgumentException(String.format("Component %s is not an AbstractSelectManyComponent", component.getClientId(context)));
+ }
+ AbstractSelectManyComponent picklist = (AbstractSelectManyComponent) component;
+
+ String hiddenClientId = picklist.getClientId(context);
+ Map<String, String> paramMap = context.getExternalContext().getRequestParameterMap();
+
+ if (picklist.isDisabled()) {
+ return;
+ }
+ String value = paramMap.get(hiddenClientId);
+ if (value != null) {
+ if (value.trim().equals("")) {
+ ((EditableValueHolder) picklist).setSubmittedValue(new String[] {});
} else {
- throw new IllegalArgumentException("Value expression must evaluate to either a List or Object[]");
+ String[] reqValues = value.split(",");
+ ((EditableValueHolder) picklist).setSubmittedValue(reqValues);
}
} else {
- if (select.getValue() instanceof List) {
- select.setSubmittedValue(Collections.emptyList());
- } else if (select.getValue() instanceof Object[]) {
- select.setSubmittedValue(new String[0]);
+ ((EditableValueHolder) picklist).setSubmittedValue(new String[] {});
+ }
+ }
+
+ public Object getConvertedValue(FacesContext facesContext, UIComponent component, Object val) throws ConverterException {
+ String[] values = (val == null) ? new String[0] : (String[]) val;
+ Converter converter = getItemConverter(facesContext, component);
+ ValueExpression ve = component.getValueExpression("value");
+ if (ve != null) {
+ Class<?> modelType = ve.getType(facesContext.getELContext());
+ if (modelType.isArray()) {
+ Object targetForConvertedValues = Array.newInstance(modelType, values.length);
+ for (int i = 0; i < values.length; i++) {
+ if (converter != null) {
+ ((Object[]) targetForConvertedValues)[i] = converter.getAsObject(facesContext, component, values[i]);
+ } else {
+ ((Object[]) targetForConvertedValues)[i] = values[i];
+ }
+ }
+ return targetForConvertedValues;
+ } else if (Collection.class.isAssignableFrom(modelType)) {
+ Collection targetForConvertedValues;
+ String collectionType = (String) component.getAttributes().get("collectionType");
+ if (collectionType != null) {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ if (classLoader == null) {
+ classLoader = SelectManyRendererBase.class.getClassLoader();
+ }
+ try {
+ targetForConvertedValues = classLoader.loadClass(collectionType).asSubclass(Collection.class).newInstance();
+ } catch (InstantiationException e) {
+ throw new FacesException(e);
+ } catch (IllegalAccessException e) {
+ throw new FacesException(e);
+ } catch (ClassNotFoundException e) {
+ throw new FacesException(e);
+ }
+ } else {
+ Collection value = (Collection) ((EditableValueHolder) component).getValue();
+ if (value instanceof Cloneable) {
+ try {
+ targetForConvertedValues = (Collection) value.getClass().getMethod("clone").invoke(value);
+ targetForConvertedValues.clear();
+ } catch (IllegalAccessException e) {
+ throw new FacesException(e);
+ } catch (InvocationTargetException e) {
+ throw new FacesException(e);
+ } catch (NoSuchMethodException e) {
+ throw new FacesException(e);
+ }
+ } else {
+ if (SortedSet.class.isAssignableFrom(modelType)) {
+ targetForConvertedValues = new TreeSet();
+ } else if (Queue.class.isAssignableFrom(modelType)) {
+ targetForConvertedValues = new LinkedList();
+ } else if (Set.class.isAssignableFrom(modelType)) {
+ targetForConvertedValues = new HashSet(values.length);
+ } else {
+ targetForConvertedValues = new ArrayList(values.length);
+ }
+ }
+ for (int i = 0; i < values.length; i++) {
+ if (converter != null) {
+ targetForConvertedValues.add(converter.getAsObject(facesContext, component, values[i]));
+ } else {
+ targetForConvertedValues.add(values[i]);
+ }
+ }
+ }
+ return targetForConvertedValues;
} else {
- throw new IllegalArgumentException("Value expression must evaluate to either a List or Object[]");
+ throw new FacesException(String.format("ModelType (%s) must be either an Array, or a Collection", modelType));
+ }
+ } else {
+ Object targetForConvertedValues = new Object[values.length];
+ for (int i = 0; i < values.length; i++) {
+ if (converter != null) {
+ ((Object[]) targetForConvertedValues)[i] = converter.getAsObject(facesContext, component, values[i]);
+ } else {
+ ((Object[]) targetForConvertedValues)[i] = values[i];
+ }
+ }
+ return targetForConvertedValues;
+ }
+ }
+
+ public Converter getItemConverter(FacesContext facesContext, UIComponent component) {
+ Converter converter = null;
+ if (component instanceof ValueHolder) {
+ converter = ((ValueHolder) component).getConverter();
+ } else {
+ ValueExpression ve = component.getValueExpression("value");
+ if (ve != null) {
+ Class<?> valueType = ve.getType(facesContext.getELContext());
+ if (valueType.isArray()) {
+ converter = facesContext.getApplication().createConverter(valueType);
+ }
}
}
+ return converter;
}
public void encodeTargetItems(FacesContext context, UIComponent component, List<ClientSelectItem> clientSelectItems) throws IOException {
@@ -154,7 +264,7 @@ public void encodeSourceItems(FacesContext context, UIComponent component, List<
}
private void encodeItems(FacesContext facesContext, UIComponent component, boolean source, List<ClientSelectItem> clientSelectItems) throws IOException {
- AbstractSelectManyComponent select = (AbstractSelectManyComponent) component;
+ AbstractSelectManyComponent select = (AbstractSelectManyComponent) component;
if (clientSelectItems != null && !clientSelectItems.isEmpty()) {
ResponseWriter writer = facesContext.getResponseWriter();
String clientId = component.getClientId(facesContext);
@@ -166,7 +276,7 @@ private void encodeItems(FacesContext facesContext, UIComponent component, boole
writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, itemClientId, null);
writer.writeAttribute(HtmlConstants.VALUE_ATTRIBUTE, clientSelectItem.getConvertedValue(), null);
writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE,
- HtmlUtil.concatClasses(select.getItemClass(), ITEM_CSS), null);
+ HtmlUtil.concatClasses(select.getItemClass(), ITEM_CSS), null);
String label = clientSelectItem.getLabel();
if (label != null && label.trim().length() > 0) {
writer.writeText(label, null);
View
3 input/ui/src/main/resources/META-INF/resources/org.richfaces/pickList.js
@@ -44,6 +44,9 @@
multipleSelect: true
};
+ // TODO: Find a way to toggle buttons on page load
+ // $.ready($.proxy(this.toggleButtons, this));
+
$.extend(rf.ui.PickList.prototype, (function () {
return {

0 comments on commit 0ee4646

Please sign in to comment.
Something went wrong with that request. Please try again.