package com.pre.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Window;
public class OptionBinder {
public static void bindStandardOptions(final Window source, final String gridName, final String targetWindow){
Menuitem createItem =(Menuitem)source.getFellow("createItem");
createItem.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
openDetails(source, gridName, "1", targetWindow);
Events.postEvent("onRefresh", source, null);
}
});
Menuitem showItem =(Menuitem)source.getFellow("showItem");
showItem.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
openDetails(source, gridName, "2", targetWindow);
Events.postEvent("onRefresh", source, null);
}
}); Menuitem editItem=(Menuitem)source.getFellow("editItem");
editItem.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
openDetails(source, gridName, "3", targetWindow);
Events.postEvent("onRefresh", source, null);
}
});
Menuitem deleteItem=(Menuitem)source.getFellow("deleteItem");
deleteItem.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
openDetails(source, gridName, "4", targetWindow);
Events.postEvent("refresh", source, null);
}
});
}
public static void openDetails(Window source, String gridName, String option, String targetDetailsWindow){
Map<String, ArrayList<String>> parameters=new HashMap<String, ArrayList<String>>();
ArrayList<String> options=new ArrayList<String>();
options.add(option);
parameters.put("options", options);
Window child=(Window)Executions.createComponents(targetDetailsWindow, source, parameters);
ArrayList<String> ids=new ArrayList<String>();
Rows rows=((Grid)source.getFellow(gridName)).getRows();
List rowList=rows.getChildren();
Iterator it=rowList.iterator();
while(it.hasNext()){
Object next=it.next();
if(next instanceof Row){
Iterator fit=((Row)next).getChildren().iterator();
if(((Checkbox)fit.next()).isChecked()){
ids.add(((Label)fit.next()).getValue());
}
}
}
parameters.put("ids", ids);
child.setClosable(true);
try {
child.doModal();
} catch (SuspendNotAllowedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}