Author: lfryc(a)redhat.com
Date: 2010-12-07 18:01:11 -0500 (Tue, 07 Dec 2010)
New Revision: 20439
Added:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java
Modified:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/Attribute.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/RichDataTableBean.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/RichExtendedDataTableBean.java
Log:
Attribute supports conversion of String or Collection to the expected types (incl. the
possibility to define collection's member type
Modified:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/Attribute.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/Attribute.java 2010-12-07
17:15:38 UTC (rev 20438)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/Attribute.java 2010-12-07
23:01:11 UTC (rev 20439)
@@ -22,9 +22,7 @@
package org.richfaces.tests.metamer;
import java.io.Serializable;
-import java.util.Arrays;
import java.util.Collection;
-import java.util.LinkedList;
import java.util.List;
import javax.faces.model.SelectItem;
@@ -32,7 +30,6 @@
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
-import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -50,6 +47,7 @@
private String name;
private Object value;
private Class<?> type;
+ private Class<?> memberType = Object.class;
private String help;
private List<SelectItem> selectOptions;
private Extensions extensions;
@@ -73,14 +71,23 @@
}
public Object getValue() {
+ if (value == null && type != null) {
+ if (Collection.class.isAssignableFrom(type)) {
+ CollectionConverter collectionConverter = new CollectionConverter(type,
memberType);
+ value = collectionConverter.convert(null);
+ }
+ }
return value;
}
public void setValue(Object value) {
- if (value instanceof String) {
- if (type == Collection.class) {
- String[] splitted = StringUtils.split((String) value, ",[] ");
- value = new LinkedList<String>(Arrays.asList(splitted));
+ if (type != null) {
+ if (value instanceof String || value instanceof Collection) {
+ if (Collection.class.isAssignableFrom(type)) {
+ CollectionConverter collectionConverter = new
CollectionConverter(type, memberType);
+ this.value = collectionConverter.convert(value);
+ return;
+ }
}
}
this.value = value;
@@ -130,6 +137,14 @@
public void setType(Class<?> type) {
this.type = type;
}
+
+ public Class<?> getMemberType() {
+ return memberType;
+ }
+
+ public void setMemberType(Class<?> memberType) {
+ this.memberType = memberType;
+ }
public boolean isBool() {
return type == Boolean.class || type == boolean.class;
Added:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java
(rev 0)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java 2010-12-07
23:01:11 UTC (rev 20439)
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+package org.richfaces.tests.metamer;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Pavol Pitonak</a>
+ * @version $Revision$
+ */
+public final class CollectionConverter<E, C extends Collection<E>> {
+
+ Class<C> collectionClass;
+ Class<E> memberClass;
+
+ public CollectionConverter(Class<?> collectionClass, Class<?>
memberClass) {
+ this.collectionClass = (Class<C>) collectionClass;
+ this.memberClass = (Class<E>) memberClass;
+ }
+
+ public static <E, C extends Collection<E>> CollectionConverter<E,
Collection<E>> getInstance(
+ Class<E> collectionClass, Class<C> memberClass) {
+ return new CollectionConverter<E, Collection<E>>(collectionClass,
memberClass);
+ }
+
+ public C convert(Object value) {
+ Collection<?> collection = getTransformedValue(value);
+
+ if (collection.isEmpty()) {
+ return createCollection();
+ }
+
+ ValueConverter<E> memberConverter =
ValueConverter.getInstance(memberClass);
+
+ C result = createCollection();
+
+ for (Object object : collection) {
+ result.add(memberConverter.convert(object));
+ }
+
+ return result;
+ }
+
+ private Collection<? extends Object> getTransformedValue(Object value) {
+ if (value == null) {
+ return Collections.EMPTY_LIST;
+ } else if (value instanceof String) {
+ String[] splitted = StringUtils.split((String) value, ",[] ");
+ return Arrays.asList(splitted);
+ } else if (value instanceof Collection) {
+ return (Collection) value;
+ } else {
+ throw new UnsupportedOperationException("the value '" + value +
"' of class " + value.getClass()
+ + " is not supported in collection transformer");
+ }
+ }
+
+ private C createCollection() {
+ try {
+ return getConcreteClass().newInstance();
+ } catch (Exception e) {
+ throw new IllegalStateException("wasn't able to construct new
collection of given type ("
+ + collectionClass.getName() + ")");
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private Class<? extends C> getConcreteClass() {
+ if (!collectionClass.isInterface()) {
+ return collectionClass;
+ } else {
+ if (Set.class.isAssignableFrom(collectionClass)) {
+ return (Class<? extends C>) (Object) TreeSet.class;
+ }
+ if (Collection.class.isAssignableFrom(collectionClass)) {
+ return (Class<? extends C>) (Object) LinkedList.class;
+ }
+ throw new IllegalStateException("Unsupported collection of interface
'" + collectionClass.getName() + "'");
+ }
+ }
+}
Added:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java
(rev 0)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java 2010-12-07
23:01:11 UTC (rev 20439)
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+package org.richfaces.tests.metamer;
+
+import java.lang.reflect.Constructor;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public final class ValueConverter<T> {
+
+ Class<T> type;
+
+ private ValueConverter(Class<?> type) {
+ this.type = (Class<T>) type;
+ }
+
+ public static <T> ValueConverter<T> getInstance(Class<T> type) {
+ return new ValueConverter<T>(type);
+ }
+
+ public T convert(Object value) {
+ if (value == null) {
+ return null;
+ } else if (type.isAssignableFrom(String.class)) {
+ return (T) value;
+ } else if (value instanceof String) {
+ return construct(value);
+ } else {
+ throw new UnsupportedOperationException("Can't convert the object of
'" + value.getClass().getName()
+ + "' to '" + type.getName() + "'");
+ }
+ }
+
+ private T construct(Object value) {
+ try {
+ return getConstructor().newInstance(value);
+ } catch (Exception e) {
+ throw new UnsupportedOperationException("Can't construct the object
of class '" + type.getName()
+ + "' from String");
+ }
+
+ }
+
+ public Constructor<T> getConstructor() {
+ try {
+ return type.getConstructor(String.class);
+ } catch (Exception e) {
+ throw new UnsupportedOperationException("Can't construct the object
of class '" + type.getName()
+ + "' from String");
+ }
+ }
+
+}
Modified:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/RichDataTableBean.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/RichDataTableBean.java 2010-12-07
17:15:38 UTC (rev 20438)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/RichDataTableBean.java 2010-12-07
23:01:11 UTC (rev 20439)
@@ -22,7 +22,6 @@
package org.richfaces.tests.metamer.bean;
import java.io.Serializable;
-import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@@ -92,7 +91,6 @@
attributes.setAttribute("rendered", true);
attributes.setAttribute("rows", 10);
- attributes.get("sortPriority").setType(Collection.class);
// hidden attributes
attributes.remove("filteringListeners");
Modified:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/RichExtendedDataTableBean.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/RichExtendedDataTableBean.java 2010-12-07
17:15:38 UTC (rev 20438)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/RichExtendedDataTableBean.java 2010-12-07
23:01:11 UTC (rev 20439)
@@ -25,6 +25,7 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
+import java.util.TreeSet;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
@@ -92,6 +93,10 @@
attributes.setAttribute("rows", 30);
attributes.setAttribute("styleClass",
"extended-data-table");
attributes.setAttribute("style", null);
+
+ // setup types
+ attributes.get("selection").setType(TreeSet.class);
+ attributes.get("selection").setMemberType(Integer.class);
// hidden attributes
attributes.remove("filterVar");