package com.avanon.zk.bug;

import java.util.Arrays;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Window;

public class ListboxComposer extends SelectorComposer<Window> {

    private static final long serialVersionUID = 7623677116800973633L;
    
    private final static String ITEM1 = "ITEM1";
    private final static String ITEM2 = "ITEM2";
    private final static String ITEM3 = "ITEM3";
    private final static String ITEM4 = "ITEM4";
    
    @Wire
    private Listbox buggyremove;
    
    @Override
    public void doAfterCompose( Window comp ) throws Exception {
        super.doAfterCompose( comp );
        
        ListModelList<String> model = new ListModelList<String>();
        model.setMultiple( true );
        
        model.add( ITEM1 );
        model.add( ITEM2 );
        model.add( ITEM3 );
        model.add( ITEM4 );
        
        buggyremove.setModel( model );
        
        model.setSelection( Arrays.asList( ITEM3, ITEM4 ) ); // Error only happens when a selection is active!!
    }
    
    
    @Listen( Events.ON_CLICK + " = #buggyremove" )
    public void doRemove( Event event ) {
        ListModelList<String> sourceModel = (ListModelList<String>) buggyremove.<String>getModel();
        
        sourceModel.remove( ITEM4 );
    }
    
}
