From df0e3278609d5f86c38892c57a16356b2c24a80f Mon Sep 17 00:00:00 2001 From: Simon Massey Date: Sun, 6 May 2012 09:29:17 +0100 Subject: [PATCH] zk6 only refresh --- pom.xml | 4 +- .../zkforge/zktodo2/DateFormatConverter.java | 32 ------- src/main/java/org/zkforge/zktodo2/Model.java | 46 +++++++++ .../java/org/zkforge/zktodo2/ZkToDoModel.java | 33 ------- .../org/zkforge/zktodo2/ZkToDoModelImpl.java | 74 --------------- .../Controller.java} | 93 ++++++++++++------- .../Presenter.java} | 15 +-- .../zktodo2/ui/TimestampConverter.java | 20 ++++ .../ViewModel.java} | 16 +++- .../WEB-INF/spring/application-context.xml | 12 +-- src/main/webapp/WEB-INF/zk.xml | 4 - .../{zktodo_e.zul => applicationmodel.zul} | 15 ++- src/main/webapp/index.jsp | 11 ++- ...zktodo_passiveview.zul => passiveview.zul} | 10 +- src/main/webapp/supervisingcontroller.zul | 55 +++++++++++ src/main/webapp/zktodo_b.zul | 33 ------- src/main/webapp/zktodo_c.zul | 4 - src/main/webapp/zktodo_d.zul | 41 -------- 18 files changed, 229 insertions(+), 289 deletions(-) delete mode 100644 src/main/java/org/zkforge/zktodo2/DateFormatConverter.java create mode 100644 src/main/java/org/zkforge/zktodo2/Model.java delete mode 100644 src/main/java/org/zkforge/zktodo2/ZkToDoModel.java delete mode 100644 src/main/java/org/zkforge/zktodo2/ZkToDoModelImpl.java rename src/main/java/org/zkforge/zktodo2/{ZkToDoControllerV2.java => ui/Controller.java} (50%) rename src/main/java/org/zkforge/zktodo2/{ZkToDoControllerV1.java => ui/Presenter.java} (90%) create mode 100644 src/main/java/org/zkforge/zktodo2/ui/TimestampConverter.java rename src/main/java/org/zkforge/zktodo2/{ZkToDoViewModel.java => ui/ViewModel.java} (77%) rename src/main/webapp/{zktodo_e.zul => applicationmodel.zul} (74%) rename src/main/webapp/{zktodo_passiveview.zul => passiveview.zul} (60%) create mode 100644 src/main/webapp/supervisingcontroller.zul delete mode 100644 src/main/webapp/zktodo_b.zul delete mode 100644 src/main/webapp/zktodo_c.zul delete mode 100644 src/main/webapp/zktodo_d.zul diff --git a/pom.xml b/pom.xml index a76b1e6..6cf3f88 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.zkforge.zktodo2 zktodo2 war - 1.7.0 + 1.8.0 zktodo2 http://www.slideshare.net/simbo1905/design-patterns-in-zk-java-mvvm-as-modelviewbinder @@ -17,6 +17,7 @@ UTF-8 6.0.0 1.0.2 + 3.1 3.0.5.RELEASE 1.8.0.7 3.3.1.ga @@ -29,7 +30,6 @@ 2.4 4.7 1.3.4 - 3.0 2.5 7.4.5.v20110725 8.4-702.jdbc3 diff --git a/src/main/java/org/zkforge/zktodo2/DateFormatConverter.java b/src/main/java/org/zkforge/zktodo2/DateFormatConverter.java deleted file mode 100644 index ec18c43..0000000 --- a/src/main/java/org/zkforge/zktodo2/DateFormatConverter.java +++ /dev/null @@ -1,32 +0,0 @@ - -package org.zkforge.zktodo2; - -import java.util.Date; -import java.sql.Timestamp; -import java.text.SimpleDateFormat; - -public class DateFormatConverter implements org.zkoss.zkplus.databind.TypeConverter { - - public Object coerceToBean(Object val, org.zkoss.zk.ui.Component comp) { - //do nothing - return val; - } - /** - * Depending whether the data is coming from the database or coming from the datebox - * we might be passed either a java.util.Date or a java.sql.Timestamp - * - * @see org.zkoss.zkplus.databind.TypeConverter#coerceToUi(java.lang.Object, org.zkoss.zk.ui.Component) - */ - public Object coerceToUi(Object val, org.zkoss.zk.ui.Component comp) { - Date date = null; - if( val instanceof Timestamp ){ - final Timestamp timestamp = (Timestamp)val; - date = new Date(timestamp.getTime()); - } else if( val instanceof Date ){ - date = (Date)val; - } - - //prepare dateFormat and convert Date to String - return (new SimpleDateFormat("MM/dd/yyyy")).format(date); - } -} \ No newline at end of file diff --git a/src/main/java/org/zkforge/zktodo2/Model.java b/src/main/java/org/zkforge/zktodo2/Model.java new file mode 100644 index 0000000..93ff0e2 --- /dev/null +++ b/src/main/java/org/zkforge/zktodo2/Model.java @@ -0,0 +1,46 @@ +package org.zkforge.zktodo2; + +import java.util.List; + +public class Model { + + protected ReminderService reminderService; + + public void deleteEvent(Reminder reminder) throws EntityNotFoundException { + reminderService.delete(reminder); + } + + public List findAll() { + return reminderService.findAll(); + } + + public void mergeEvent(Reminder reminder) throws EntityNotFoundException { + reminderService.merge(reminder); + } + + public void persistEvent(Reminder reminder) { + reminderService.persist(reminder); + } + + public ReminderService getReminderService() { + return reminderService; + } + + public void setReminderService(ReminderService reminderService) { + this.reminderService = reminderService; + } + + protected Reminder selectedReminder = new Reminder(); + + public Reminder getSelectedReminder() { + return this.selectedReminder; + } + + public void setSelectedReminder(Reminder reminder) { + this.selectedReminder = reminder; + } + + public List getReminders() { + return this.findAll(); + } +} diff --git a/src/main/java/org/zkforge/zktodo2/ZkToDoModel.java b/src/main/java/org/zkforge/zktodo2/ZkToDoModel.java deleted file mode 100644 index e91964b..0000000 --- a/src/main/java/org/zkforge/zktodo2/ZkToDoModel.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.zkforge.zktodo2; - -import java.util.List; - - -/** - * This is an Application Model class as it exposes resource via methods such - * as "findAll" yet it also store the uses selection with get/setSelectedItem. - * - * @author simon - */ -public interface ZkToDoModel { - - public abstract void deleteEvent(Reminder reminder) - throws EntityNotFoundException; - - public abstract List findAll(); - - public abstract void mergeEvent(Reminder reminder) - throws EntityNotFoundException; - - public abstract void persistEvent(Reminder reminder); - - //used by selectedItem="@{controller.selectedReminder}" and others - public abstract Reminder getSelectedReminder(); - - //used by selectedItem="@{controller.selectedReminder}" and others - public abstract void setSelectedReminder(Reminder reminder); - - //used by model="@{controller.reminders}" - public abstract List getReminders(); - -} \ No newline at end of file diff --git a/src/main/java/org/zkforge/zktodo2/ZkToDoModelImpl.java b/src/main/java/org/zkforge/zktodo2/ZkToDoModelImpl.java deleted file mode 100644 index 99d66ae..0000000 --- a/src/main/java/org/zkforge/zktodo2/ZkToDoModelImpl.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.zkforge.zktodo2; - -import java.util.List; - -/** - * The service is a singleton so stateless. This class wraps holds state for - * the user and delegates actions through to the statless service. - * @author simbo - */ -public class ZkToDoModelImpl implements ZkToDoModel { - protected ReminderService reminderService; - - /* (non-Javadoc) - * @see org.zkforge.zktodo2.ZkToDoModel#deleteEvent(org.zkforge.zktodo2.Reminder) - */ - public void deleteEvent(Reminder reminder) throws EntityNotFoundException { - reminderService.delete(reminder); - } - - /* (non-Javadoc) - * @see org.zkforge.zktodo2.ZkToDoModel#findAll() - */ - public List findAll() { - return reminderService.findAll(); - } - - /* (non-Javadoc) - * @see org.zkforge.zktodo2.ZkToDoModel#mergeEvent(org.zkforge.zktodo2.Reminder) - */ - public void mergeEvent(Reminder reminder) throws EntityNotFoundException { - reminderService.merge(reminder); - } - - /* (non-Javadoc) - * @see org.zkforge.zktodo2.ZkToDoModel#persistEvent(org.zkforge.zktodo2.Reminder) - */ - public void persistEvent(Reminder reminder) { - reminderService.persist(reminder); - } - - public ReminderService getReminderService() { - return reminderService; - } - - public void setReminderService(ReminderService reminderService) { - this.reminderService = reminderService; - } - - protected Reminder selectedReminder; - - //used by selectedItem="@{controller.selectedReminder}" and others - /* (non-Javadoc) - * @see org.zkforge.zktodo2.ZkToDoModel#getSelectedReminder() - */ - public Reminder getSelectedReminder() { - return this.selectedReminder; - } - - //used by selectedItem="@{controller.selectedReminder}" and others - /* (non-Javadoc) - * @see org.zkforge.zktodo2.ZkToDoModel#setSelectedReminder(org.zkforge.zktodo2.Reminder) - */ - public void setSelectedReminder(Reminder reminder) { - this.selectedReminder = reminder; - } - - //used by model="@{controller.reminders}" - /* (non-Javadoc) - * @see org.zkforge.zktodo2.ZkToDoModel#getReminders() - */ - public List getReminders() { - return this.findAll(); - } -} diff --git a/src/main/java/org/zkforge/zktodo2/ZkToDoControllerV2.java b/src/main/java/org/zkforge/zktodo2/ui/Controller.java similarity index 50% rename from src/main/java/org/zkforge/zktodo2/ZkToDoControllerV2.java rename to src/main/java/org/zkforge/zktodo2/ui/Controller.java index 794a342..56a4d5e 100644 --- a/src/main/java/org/zkforge/zktodo2/ZkToDoControllerV2.java +++ b/src/main/java/org/zkforge/zktodo2/ui/Controller.java @@ -1,50 +1,70 @@ -package org.zkforge.zktodo2; +package org.zkforge.zktodo2.ui; import static java.lang.System.out; import java.util.Date; import java.util.List; +import org.zkforge.zktodo2.EntityNotFoundException; +import org.zkforge.zktodo2.Model; +import org.zkforge.zktodo2.Reminder; +import org.zkoss.bind.Converter; +import org.zkoss.bind.annotation.BindingParam; +import org.zkoss.bind.annotation.Command; +import org.zkoss.bind.annotation.ContextParam; +import org.zkoss.bind.annotation.ContextType; +import org.zkoss.bind.annotation.Init; +import org.zkoss.bind.annotation.NotifyChange; +import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.event.Event; -import org.zkoss.zk.ui.util.GenericForwardComposer; +import org.zkoss.zk.ui.select.Selectors; +import org.zkoss.zk.ui.select.annotation.VariableResolver; +import org.zkoss.zk.ui.select.annotation.Wire; +import org.zkoss.zk.ui.select.annotation.WireVariable; import org.zkoss.zul.Datebox; import org.zkoss.zul.Intbox; import org.zkoss.zul.ListModel; import org.zkoss.zul.ListModelList; import org.zkoss.zul.Listbox; import org.zkoss.zul.Textbox; -import org.zkoss.zul.Window; - /** - * This class is more inline with true Model-View-Controller as the Composer - * class is not actively updating the view; it manipulates models that the - * view is observing. + * This class demonstrates "Supervising Presenter" pattern as the Composer is + * not doing all the explicit work of updating the UI. * - * @author simon + * {@link http://martinfowler.com/eaaDev/SupervisingPresenter.html} */ -public class ZkToDoControllerV2 extends GenericForwardComposer { +@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) +public class Controller { - private static final long serialVersionUID = 2560535692993939331L; - protected Textbox name; - protected Intbox priority; - protected Datebox date; - protected Listbox list; + @Wire Textbox name; + @Wire Intbox priority; + @Wire Datebox date; + @Wire Listbox list; - public ZkToDoControllerV2(){} + public Controller(){ + // noop + } - protected ZkToDoModel toDoModel = null; + @Init + public void init(@ContextParam(ContextType.VIEW) Component view){ + Selectors.wireComponents(view, this, false); + } - public ZkToDoModel getToDoModel() { - return toDoModel; + @WireVariable Model model = null; + + public void setModel(Model model) { + this.model = model; } - public void setToDoModel(ZkToDoModel toDoModel) { - this.toDoModel = toDoModel; + public Model getModel() { + return model; } - public void onClick$add(Event e) { + @Command + @NotifyChange({"reminders","selectedReminder"}) + public void create() { Date dateValue = date.getValue(); Integer priorityValue = priority.getValue(); String nameValue = name.getValue(); @@ -53,8 +73,8 @@ public void setToDoModel(ZkToDoModel toDoModel) { reminder.setDate(date.getValue()); reminder.setName(name.getValue()); reminder.setPriority(priority.getValue()); - this.toDoModel.persistEvent(reminder); - List reminders = this.toDoModel.findAll(); + this.model.persistEvent(reminder); + List reminders = this.model.findAll(); ListModel listModel = this.list.getModel(); ListModelList listModelList = (ListModelList)listModel; listModelList.clear(); @@ -63,16 +83,17 @@ public void setToDoModel(ZkToDoModel toDoModel) { return; } - public void onClick$update(Event e) { - Reminder selectedReminder = this.toDoModel.getSelectedReminder(); + @Command + @NotifyChange({"reminders","selectedReminder"}) + public void save() { + Reminder selectedReminder = this.model.getSelectedReminder(); if( selectedReminder != null ){ ListModelList listModelList = (ListModelList) this.list.getModel(); try { - this.toDoModel.mergeEvent(selectedReminder); + this.model.mergeEvent(selectedReminder); } catch (EntityNotFoundException exception){ int index = list.getSelectedIndex(); listModelList.remove(index); - alert("Reminder "+selectedReminder.getName()+" has been deleted by another user."); if( listModelList.size() > 0 ){ selectedReminder = (Reminder)listModelList.get(0); list.setSelectedIndex(0); @@ -83,20 +104,22 @@ public void setToDoModel(ZkToDoModel toDoModel) { selectedReminder = null; } } - List reminders = toDoModel.findAll(); + List reminders = model.findAll(); listModelList.clear(); listModelList.addAll(reminders); } } - public void onClick$delete(Event e) { - Reminder selectedReminder = this.toDoModel.getSelectedReminder(); + @Command + @NotifyChange({"reminders","selectedReminder"}) + public void delete(@BindingParam("e") Event e) { + Reminder selectedReminder = this.model.getSelectedReminder(); if( null != selectedReminder ){ ListModel listModel = this.list.getModel(); ListModelList listModelList = (ListModelList)listModel; int index = listModelList.indexOf(selectedReminder); try { - this.toDoModel.deleteEvent(selectedReminder); + this.model.deleteEvent(selectedReminder); } catch (EntityNotFoundException exception ){ out.println("This is harmless as someone else has already deleted this item."); } @@ -104,7 +127,7 @@ public void setToDoModel(ZkToDoModel toDoModel) { if( index >= listModelList.size() ){ index = listModelList.size() - 1; } - if( listModelList.size() > 0 ){ + if( listModelList.size() > 0 && index >= 0 ){ selectedReminder = (Reminder)listModelList.get(index); list.setSelectedIndex(index); name.setValue(selectedReminder.getName()); @@ -115,4 +138,10 @@ public void setToDoModel(ZkToDoModel toDoModel) { } } } + + Converter dateConverter = new TimestampConverter(); + + public Converter getDateConverter() { + return dateConverter; + } } diff --git a/src/main/java/org/zkforge/zktodo2/ZkToDoControllerV1.java b/src/main/java/org/zkforge/zktodo2/ui/Presenter.java similarity index 90% rename from src/main/java/org/zkforge/zktodo2/ZkToDoControllerV1.java rename to src/main/java/org/zkforge/zktodo2/ui/Presenter.java index 0d7f662..1e5d8cf 100644 --- a/src/main/java/org/zkforge/zktodo2/ZkToDoControllerV1.java +++ b/src/main/java/org/zkforge/zktodo2/ui/Presenter.java @@ -1,4 +1,4 @@ -package org.zkforge.zktodo2; +package org.zkforge.zktodo2.ui; import static java.lang.System.out; @@ -6,6 +6,9 @@ import java.util.Date; import java.util.List; +import org.zkforge.zktodo2.EntityNotFoundException; +import org.zkforge.zktodo2.Reminder; +import org.zkforge.zktodo2.ReminderService; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.select.SelectorComposer; @@ -30,7 +33,7 @@ * {@link http://martinfowler.com/eaaDev/PassiveScreen.html} */ @VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) -public class ZkToDoControllerV1 extends SelectorComposer implements +public class Presenter extends SelectorComposer implements ListitemRenderer { private static final long serialVersionUID = -3486059156312322420L; @@ -43,10 +46,10 @@ public class ZkToDoControllerV1 extends SelectorComposer implements @Wire Listbox list; ListModelList listModelList; - Reminder selectedReminder = null; + Reminder selectedReminder = new Reminder(); - public ZkToDoControllerV1() { - // no nothing + public Presenter() { + // noop } @SuppressWarnings({ "rawtypes", "unchecked" }) @@ -130,7 +133,7 @@ public void delete(Event e) { if( index >= listModelList.size() ){ index = listModelList.size() - 1; } - if( listModelList.size() > 0 ){ + if( listModelList.size() > 0 && index >= 0 ){ selectedReminder = (Reminder)listModelList.get(index); list.setSelectedIndex(index); name.setValue(selectedReminder.getName()); diff --git a/src/main/java/org/zkforge/zktodo2/ui/TimestampConverter.java b/src/main/java/org/zkforge/zktodo2/ui/TimestampConverter.java new file mode 100644 index 0000000..b576411 --- /dev/null +++ b/src/main/java/org/zkforge/zktodo2/ui/TimestampConverter.java @@ -0,0 +1,20 @@ +package org.zkforge.zktodo2.ui; + +import java.util.Date; + +import org.zkoss.bind.BindContext; +import org.zkoss.bind.Converter; +import org.zkoss.zk.ui.Component; + +final class TimestampConverter implements Converter { + @Override + public Object coerceToUi(Object val, Component c, BindContext b) { + return val; + } + + @Override + public Object coerceToBean(Object val, Component c, BindContext b) { + Date date = (Date) val; + return new java.sql.Timestamp(date.getTime()); + } +} \ No newline at end of file diff --git a/src/main/java/org/zkforge/zktodo2/ZkToDoViewModel.java b/src/main/java/org/zkforge/zktodo2/ui/ViewModel.java similarity index 77% rename from src/main/java/org/zkforge/zktodo2/ZkToDoViewModel.java rename to src/main/java/org/zkforge/zktodo2/ui/ViewModel.java index 4aa3663..2e3e49d 100644 --- a/src/main/java/org/zkforge/zktodo2/ZkToDoViewModel.java +++ b/src/main/java/org/zkforge/zktodo2/ui/ViewModel.java @@ -1,12 +1,16 @@ -package org.zkforge.zktodo2; +package org.zkforge.zktodo2.ui; import java.util.ArrayList; import java.util.List; +import org.zkforge.zktodo2.EntityNotFoundException; +import org.zkforge.zktodo2.Reminder; +import org.zkforge.zktodo2.ReminderService; +import org.zkoss.bind.Converter; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.NotifyChange; -public class ZkToDoViewModel { +public class ViewModel { protected ReminderService reminderService; @@ -42,7 +46,7 @@ public void delete() { this.reminderService.delete(selectedReminder); this.selectedReminder = new Reminder(); } catch (EntityNotFoundException e) { - // no doubt someone else deleted it at the same time. nothing to do. + e.printStackTrace(); // hum. some else deleted this. should really send this up to the user and ask them to reload the page. } } } @@ -66,5 +70,11 @@ public void save() { public void create() { this.selectedReminder = new Reminder(); } + + Converter dateConverter = new TimestampConverter(); + + public Converter getDateConverter() { + return dateConverter; + } } diff --git a/src/main/webapp/WEB-INF/spring/application-context.xml b/src/main/webapp/WEB-INF/spring/application-context.xml index 1b3f20b..dc85463 100644 --- a/src/main/webapp/WEB-INF/spring/application-context.xml +++ b/src/main/webapp/WEB-INF/spring/application-context.xml @@ -10,7 +10,7 @@ "> + ZK Desktop --> @@ -45,15 +45,11 @@ p:basicDao-ref="basicDao" /> - - - - - - + diff --git a/src/main/webapp/WEB-INF/zk.xml b/src/main/webapp/WEB-INF/zk.xml index 101ada9..9006316 100644 --- a/src/main/webapp/WEB-INF/zk.xml +++ b/src/main/webapp/WEB-INF/zk.xml @@ -9,9 +9,5 @@ org.springframework.web.context.request.RequestContextListener --> - - true diff --git a/src/main/webapp/zktodo_e.zul b/src/main/webapp/applicationmodel.zul similarity index 74% rename from src/main/webapp/zktodo_e.zul rename to src/main/webapp/applicationmodel.zul index 4691ed5..bbfa380 100644 --- a/src/main/webapp/zktodo_e.zul +++ b/src/main/webapp/applicationmodel.zul @@ -1,11 +1,10 @@ - - + - + form="@id('r') @load(vm.selectedReminder) @save(vm.selectedReminder, before='save') @load(vm.newReminder, after='new') @validator('org.zkforge.zktodo2.ReminderValidator')"> Item: + value="@bind(r.name)" /> Priority: + value="@bind(r.priority)" /> Date: + value="@bind(r.date) @converter(vm.dateConverter)" />