package schools; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.zkoss.zk.ui.Component; 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.util.GenericForwardComposer; import org.zkoss.zkplus.databind.AnnotateDataBinder; import org.zkoss.zul.Messagebox; import org.zkoss.zul.api.Listbox; import org.zkoss.zul.api.Window; public class SchoolsListController extends GenericForwardComposer { private static final long serialVersionUID = 1L; private AnnotateDataBinder binder; private Window winSchools; private Listbox lbSchools; private List schools; private School selectedSchool; @Override public void doAfterCompose(Component comp) throws Exception { try { super.doAfterCompose(comp); } catch (Exception e) { e.printStackTrace(); } /* * create the data binder and bind this composer as the base model bean */ binder = new AnnotateDataBinder(comp); binder.bindBean("controller", this); /* * populate data models */ createModels(); } public void onCreate(Event event) { /* * populate ui with data */ binder.loadAll(); } /* * Create some mock data models */ private void createModels() { schools = new ArrayList(); schools.add(new School("1", "1st school", "address-1")); schools.add(new School("2", "2nd school", "address-2")); schools.add(new School("3", "3rd school", "address-3")); schools.add(new School("4", "4th school", "address-4")); schools.add(new School("5", "5th school", "address-5")); schools.add(new School("6", "6th school", "address-6")); schools.add(new School("7", "7th school", "address-7")); schools.add(new School("8", "8th school", "address-8")); schools.add(new School("9", "9th school", "address-9")); schools.add(new School("10", "1st school", "address-1")); schools.add(new School("12", "2nd school", "address-2")); schools.add(new School("13", "3rd school", "address-3")); schools.add(new School("14", "4th school", "address-4")); schools.add(new School("15", "5th school", "address-5")); schools.add(new School("16", "6th school", "address-6")); schools.add(new School("17", "7th school", "address-7")); schools.add(new School("18", "8th school", "address-8")); schools.add(new School("19", "9th school", "address-9")); schools.add(new School("21", "1st school", "address-1")); schools.add(new School("22", "2nd school", "address-2")); schools.add(new School("23", "3rd school", "address-3")); schools.add(new School("24", "4th school", "address-4")); schools.add(new School("25", "5th school", "address-5")); schools.add(new School("26", "6th school", "address-6")); schools.add(new School("27", "7th school", "address-7")); schools.add(new School("28", "8th school", "address-8")); schools.add(new School("29", "9th school", "address-9")); schools.add(new School("31", "1st school", "address-1")); schools.add(new School("32", "2nd school", "address-2")); schools.add(new School("33", "3rd school", "address-3")); schools.add(new School("34", "4th school", "address-4")); schools.add(new School("35", "5th school", "address-5")); schools.add(new School("36", "6th school", "address-6")); schools.add(new School("37", "7th school", "address-7")); schools.add(new School("38", "8th school", "address-8")); schools.add(new School("39", "9th school", "address-9")); schools.add(new School("41", "1st school", "address-1")); schools.add(new School("42", "2nd school", "address-2")); schools.add(new School("43", "3rd school", "address-3")); schools.add(new School("44", "4th school", "address-4")); schools.add(new School("45", "5th school", "address-5")); schools.add(new School("46", "6th school", "address-6")); schools.add(new School("47", "7th school", "address-7")); schools.add(new School("48", "8th school", "address-8")); schools.add(new School("49", "9th school", "address-9")); } /** * Event handler method triggered when the "edit" toolbar button is pressed */ public void onClick$tbtnEdit() { editSchool(); } /** * Event handler method triggered wheneber the user selects a listitem using CR or Double Click */ public void onOK$lbSchools() { editSchool(); } private void editSchool() { if (selectedSchool == null) { try { Messagebox.show("You have to select a school from the list first.", "Error", Messagebox.OK, Messagebox.ERROR); } catch (InterruptedException e) { e.printStackTrace(); } return; } Map parameters = new HashMap(); parameters.put("SELECTED_SCHOOL", selectedSchool); parameters.put("PARENT_WINDOW", winSchools); Window editDialog = (Window) Executions.createComponents("/schools/SchoolEditor.zul", winSchools, parameters); try { editDialog.doOverlapped(); editDialog.setPosition("center"); } catch (SuspendNotAllowedException e) { e.printStackTrace(); } } /** * This method is actualy an event handler triggered only from the edit dialog * and it is responsible to reflect the data changes made to the list. */ public void onSchoolSaved() { int index = lbSchools.getSelectedIndex(); if (index == -1) return; binder.loadComponent( lbSchools.getItemAtIndexApi(index) ); } /** * Event handler method triggered when the "remove" toolbar button is pressed */ public void onClick$tbtnRemove() { if (selectedSchool == null) { try { Messagebox.show("You have to select a school from the list first.", "Error", Messagebox.OK, Messagebox.ERROR); } catch (InterruptedException e) { e.printStackTrace(); } return; } try { /* * This event listener is needed to catch the response given by the user in the confirmation dialog */ EventListener eventListener = new EventListener() { @Override public void onEvent(Event event) throws Exception { switch (((Integer) event.getData()).intValue()) { case Messagebox.OK: deleteSchool(); break; } } }; Messagebox.show("Are you sure you want to delete " + selectedSchool.getName() + "?", "Warning", Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION, eventListener); } catch (InterruptedException e) { e.printStackTrace(); } } /** * Delete the selected school */ private void deleteSchool() { schools.remove(selectedSchool); selectedSchool = null; binder.loadComponent(lbSchools); } public AnnotateDataBinder getBinder() { return binder; } public void setBinder(AnnotateDataBinder binder) { this.binder = binder; } public List getSchools() { return schools; } public void setSchools(List schools) { this.schools = schools; } public School getSelectedSchool() { return selectedSchool; } public void setSelectedSchool(School selectedSchool) { this.selectedSchool = selectedSchool; } }