Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 		Fri Dec  1 16:55:36     2006, Created by Henri Chen
Copyright (C) 2006 Potix Corporation. All Rights Reserved.
package org.zkoss.zkplus.databind;
The TypeConverter implmentation for converting collection to ListModel and vice versa.

Henri Chen
public class ListModelConverter implements {
	private static final long serialVersionUID = 200808191433L;
Convert a Set, Map, List, Object[], Enum, or other kind of BindingListModel to associated BindingListModel.

val must be instanceof Set, Map, List, Object[], Enum Class, or other kind of BindingListModel implementation.
	public Object coerceToUi(Object valComponent comp) {
		if (val == null) {
			val = new ArrayList();
		final boolean distinct = isDistinct(comp);
		//since 3.1, 20080416, Henri Chen: allow custom arguments.
		//model can specify distinct=false in annotation to allow handle same-object-in-multiple-items
		if (val instanceof BindingListModel) {
			return val;
else if (val instanceof Set) {
			return new BindingListModelSet((Set)valtrue);
else if (val instanceof List) {
			return new BindingListModelList((List)valtruedistinct);
else if (val instanceof Map) {
			return new BindingListModelMap((Map)valtrue);
else if (val instanceof Object[]) {
			return new BindingListModelArray((Object[]) valtruedistinct);
else if ((val instanceof Class) && Enum.class.isAssignableFrom((Class)val)) {
			return new BindingListModelArray((Object[]) ((Class)val).getEnumConstants(), true);
else {
			throw new UiException("Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, or BindingListModel only. "+val.getClass());
	/*package*/ static boolean isDistinct(Component comp) {
		Map args = (Mapcomp.getAttribute(.);
		boolean distinct = true;
		if (args != null) {
			final String distinctstr = (Stringargs.get("distinct");
			if (distinctstr != null) {
				distinct = Boolean.valueOf(distinctstr).booleanValue();
		return distinct;

Convert a BindingListModel to Set, Map, List, or BindingListModel (itself).

val must be BindingListModelSet, BindingListModelList, BindingListModelMap, or other kind of BindingListModel
	public Object coerceToBean(Object valComponent comp) {
		if (val == null) {
			throw new NullPointerException("val");
		if (val instanceof BindingListModelSet) {
else if (val instanceof BindingListModelList) {
else if (val instanceof BindingListModelMap) {
else if (val instanceof BindingListModelArray) {
else if (val instanceof BindingListModel) {
			return val;
else {
			throw new UiException("Expects BindingListModelSet, BindingListModelList, BindingListModelMap, or BindingListModel only."+val.getClass());
New to GrepCode? Check out our FAQ X