package com.pre.web.base;
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.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.Menu;
import org.zkoss.zul.Menubar;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.Menupopup;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;
import com.pre.entity.base.Location;
import com.pre.session.base.LocationManager;
import com.pre.web.WebApplication;
public class LocationWindow extends Window {
private Textbox warehouseText;
/**
*
*/
private static final long serialVersionUID = -262958976626255590L;
public void onCreate(){
init();
createMenubar();
feedGrid();
}
private void createMenubar() {
Menubar menubar=(Menubar)getFellow("menubar");
Menu actionsMenu=new Menu("Actions");
menubar.appendChild(actionsMenu);
Menupopup popup=new Menupopup();
actionsMenu.appendChild(popup);
Menuitem createItem =new Menuitem("Create");
createItem.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
Map<String, ArrayList<String>> map=new HashMap<String, ArrayList<String>>();
ArrayList<String> options=new ArrayList<String>();
options.add("create");
map.put("options", options);
openDetails(map);
}
});
Menuitem showItem =new Menuitem("Show");
showItem.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
Map<String, ArrayList<String>> map=new HashMap<String, ArrayList<String>>();
ArrayList<String> options=new ArrayList<String>();
options.add("show");
map.put("options", options);
openDetails(map);
}
});
Menuitem editItem=new Menuitem("Edit");
editItem.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
Map<String, ArrayList<String>> map=new HashMap<String, ArrayList<String>>();
ArrayList<String> options=new ArrayList<String>();
options.add("edit");
map.put("options", options);
openDetails(map);
}
});
Menuitem deleteItem=new Menuitem("Delete");
deleteItem.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
Map<String, ArrayList<String>> map=new HashMap<String, ArrayList<String>>();
ArrayList<String> options=new ArrayList<String>();
options.add("delete");
map.put("options", options);
openDetails(map);
}
});
popup.appendChild(createItem);
popup.appendChild(showItem);
popup.appendChild(editItem);
popup.appendChild(deleteItem);
appendChild(menubar);
}
@SuppressWarnings("unchecked")
private void openDetails(Map map) throws Exception{
Window child=(Window)Executions.createComponents("./LocationDetails.zul", null, map);
ArrayList<String> ids=new ArrayList<String>();
Rows rows=((Grid)getFellow("locationsGrid")).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());
}
}
}
map.put("ids", ids);
child.setClosable(true);
child.doModal();
refresh();
}
private void refresh(){
Grid groupsGrid=(Grid)getFellow("locationsGrid");
groupsGrid.removeChild(groupsGrid.getRows());
Rows newRows=new Rows();
groupsGrid.appendChild(newRows);
feedGrid();
}
private void feedGrid() {
Grid groupsGrid=(Grid)getFellow("locationsGrid");
Rows gridRows=groupsGrid.getRows();
LocationManager manager=(LocationManager)WebApplication.lookup(LocationManager.LocalJNDIName);
List<Location> warehouses=manager.findByWarehouse(warehouseText.getText());
Iterator<Location> it=warehouses.iterator();
while(it.hasNext()){
Row row=new Row();
Location c=it.next();
row.appendChild(new Checkbox());
row.appendChild(new Label(c.getId().toString()));
row.appendChild(new Label(c.getName()));
row.appendChild(new Label(c.getWarehouse().getName()));
row.appendChild(new Label(c.getDescription().getShortDescription()));
gridRows.appendChild(row);
}
}
private void init(){
warehouseText=(Textbox)getFellow("warehouseText");
warehouseText.addEventListener("onOK", new EventListener(){
public void onEvent(Event event) throws Exception {
refresh();
}
});
}
}