0

Type Converter Problems ?

asked 2010-05-29 02:10:24 +0800

vinhvo gravatar image vinhvo
369 3
Hi all
<listbox id="lbActiveSite" checkmark="true"
								rows="4" model="@{productWin$composer.availableSites}"
								width="100%" multiple="true" selectedItem="@{productWin$composer.product.sites, converter= 'fi.uwasa.netmes.util.SelectedItemsConverter'}">
							


package fi.uwasa.netmes.util;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.UiException;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zkplus.databind.BindingListModel;
import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;

/**
 * Convert selected items to bean and vice versa.
 *
 */
public class SelectedItemsConverter implements TypeConverter, java.io.Serializable {
	
	private static final long serialVersionUID = 1L;


	public Object coerceToUi(Object val, Component comp) { //load
		Listbox lbx = (Listbox) comp;
		lbx.clearSelection();
		if (val != null && val instanceof Collection) {
			Collection<Object> values = (Collection) val;
			Iterator<Object> valuesIterator = values.iterator();
			Set<Object> items = new HashSet<Object>();

			final ListModel xmodel = lbx.getModel();
			if (xmodel instanceof BindingListModel) {
				final BindingListModel model = (BindingListModel) xmodel;
				Listitem item = null;
				while (valuesIterator.hasNext()) {
					Object value = valuesIterator.next();
					int index = model.indexOf(value);
					if (index >= 0) {
						item = (Listitem) lbx.getItemAtIndex(index);
						if (item != null) {
							items.add(item);
						}
					}
				}	
				//We need this to support load-when:onSelect when first load 
				//the page in (so it is called only once).
				if (items.size() > 0 & items.size() != lbx.getSelectedCount()) { // bug 1647817, avoid endless-loop
					//bug #2140491
					Executions.getCurrent().setAttribute("zkoss.zkplus.databind.ON_SELECT"+lbx.getUuid(), Boolean.TRUE);
					Events.postEvent(new SelectEvent("onSelect", lbx, items, item));
				}    
				lbx.setSelectedItems(items);
				return TypeConverter.IGNORE;

			} else if (xmodel == null) { //no model case, assume Listitem.value to be used with selectedItem
				//iterate to find the selected item assume the value (select mold)
				while (valuesIterator.hasNext()) {
					Object value = valuesIterator.next();
					for (Iterator it = lbx.getItems().iterator(); it.hasNext();) {
						Listitem item = (Listitem) it.next();
						if (value.equals(item.getValue())) {
							items.add(item);
						}
					}
				}
				lbx.setSelectedItems(items);
				return TypeConverter.IGNORE;
			} else {
				throw new UiException("model of the databind listbox "+lbx+" must be an instanceof of org.zkoss.zkplus.databind.BindingListModel." + xmodel);
			}
		}
	  	return null;
	}
  
	public Object coerceToBean(Object val, Component comp) { //save
	  	final Listbox lbx = (Listbox) comp;
		if (Executions.getCurrent().getAttribute("zkoss.zkplus.databind.ON_SELECT"+lbx.getUuid()) != null) {
			//bug #2140491
			//triggered by coerceToUi(), ignore this
			Executions.getCurrent().removeAttribute("zkoss.zkplus.databind.ON_SELECT"+lbx.getUuid());
			return TypeConverter.IGNORE;
		}
	  	if (val != null) {
	  		final ListModel model = lbx.getModel();
	  		Set<Listitem> listitems = (Set<Listitem>) lbx.getSelectedItems();
	  		Iterator<Listitem> itemsIterator = listitems.iterator();
	  		HashSet<Object> selectedValues = new HashSet<Object>();
	  		
	  		while (itemsIterator.hasNext()) {
				Listitem listitem = itemsIterator.next();
				if (model != null) { // model case
					selectedValues.add(model.getElementAt(listitem.getIndex()));
				} else { // no model case --> Value
					selectedValues.add(listitem.getValue());
				}
	  		}	
	  		return selectedValues;
	  	}
	 	return null;
	}
}
But I keep getting error
code: null
SEVERE: >>org.zkoss.zk.ui.UiException: java.lang.ClassNotFoundException: >>java.lang.ClassNotFoundException: >> at org.zkoss.zkplus.databind.Binding.setConverter(Binding.java:307) >> at org.zkoss.zkplus.databind.Binding.<init>(Binding.java:133) >> at org.zkoss.zkplus.databind.DataBinder.addBinding(DataBinder.java:375) >> at org.zkoss.zkplus.databind.AnnotateDataBinder.loadComponentPropertyAnnotationByAnnotName(AnnotateDataBinder.java:424) >> at org.zkoss.zkplus.databind.AnnotateDataBinder.loadComponentPropertyAnnotation(AnnotateDataBinder.java:413) >> at org.zkoss.zkplus.databind.AnnotateDataBinder.loadAnnotations(AnnotateDataBinder.java:404) >>... [/code What I have done wrong ? Thank you very much for your help.
delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2010-05-29 03:17:15 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

TypeConveters are for Bean.Properties.

Have a look on Robert's sample page here

best
Stephan

link publish delete flag offensive edit

answered 2010-05-29 07:25:19 +0800

vinhvo gravatar image vinhvo
369 3

Thank you for your quick reply. But I followed this well-known thread and it is supposed to work just like others. However, it seems that I did something very wrong.
BR,VinhVo

link publish delete flag offensive edit

answered 2010-05-29 17:33:24 +0800

vinhvo gravatar image vinhvo
369 3

Can somebody help me figure out. It seems that the zul file can not find my converter ?

link publish delete flag offensive edit

answered 2010-05-31 21:51:18 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi vinhvo
ClassNotFoundException happen on which line ?

link publish delete flag offensive edit

answered 2010-06-01 02:17:13 +0800

vinhvo gravatar image vinhvo
369 3

It happens on my onCreate Event :

binder = (AnnotateDataBinder) event.getTarget().getAttributeOrFellow("binder",false);
----
binder.loadAll(); (It happens here)
Thank you.

link publish delete flag offensive edit

answered 2010-06-01 04:17:12 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

What version are you using ?
ZK 5?
You can refer to here

link publish delete flag offensive edit

answered 2010-06-01 04:33:54 +0800

vinhvo gravatar image vinhvo
369 3

Hi, Thank you as1225 . YEs , I do use ZK 5 . <listbox id="lbActiveSite" checkmark="true"
rows="4" model="@{productWin$composer.availableSites}"
width="100%" multiple="true" selectedItems="@{productWin$composer.product.sites, converter= 'fi.uwasa.netmes.util.SelectedItemsConverter'}">

The bolded part is where I have problem. It is bean converter which convert my 'site' bean to ListItem.
As I followed this thread :Here. However, I faced SEVERE: >>org.zkoss.zk.ui.UiException: java.lang.ClassNotFoundException: when I use binder.loadAll().

link publish delete flag offensive edit

answered 2010-06-01 21:42:45 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

updated 2010-06-01 21:43:02 +0800

please remove space after =

converter= 'fi.uwasa.netmes.util.SelectedItemsConverter'}
converter='fi.uwasa.netmes.util.SelectedItemsConverter'}

link publish delete flag offensive edit

answered 2010-06-02 01:18:21 +0800

vinhvo gravatar image vinhvo
369 3

Many thanks ,as1225 :). you are awesome.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-05-29 02:10:24 +0800

Seen: 1,872 times

Last updated: Jun 02 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More