Skip to content

Commit

Permalink
reworked the Supervising Controller to have a dumb bindmodel.
Browse files Browse the repository at this point in the history
  • Loading branch information
simbo1905 committed May 8, 2012
1 parent ef4e8b2 commit 7f4340e
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 124 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<zk.version>6.0.1</zk.version>
<zkutil.version>6.0.0</zkutil.version>
<zk.version>6.0.1.FL.20120501</zk.version>
<zkutil.version>6.0.1.FL.20120501</zkutil.version>
<zkbind.version>6.0.1.FL.20120501</zkbind.version>
<zkel.version>1.0.2</zkel.version>
<zkspring.version>3.1</zkspring.version>
<spring.version>3.0.5.RELEASE</spring.version>
Expand Down Expand Up @@ -110,7 +111,7 @@
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zkbind</artifactId>
<version>${zkutil.version}</version>
<version>${zkbind.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.common</groupId>
Expand Down
30 changes: 6 additions & 24 deletions src/main/java/org/zkforge/zktodo2/Model.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.zkforge.zktodo2;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.context.annotation.Scope;
Expand All @@ -11,28 +11,8 @@
@Named("model")
public class Model {

@Inject
@Named("reminderService")
protected ReminderService reminderService;

public void deleteEvent(Reminder reminder) {
reminderService.delete(reminder);
}

public List<Reminder> findAll() {
return reminderService.findAll();
}

public void persistEvent(Reminder reminder) {
reminderService.persist(reminder);
}

public ReminderService getReminderService() {
return reminderService;
}

public void setReminderService(ReminderService reminderService) {
this.reminderService = reminderService;
public Model(){
//noop
}

protected Reminder selectedReminder = new Reminder();
Expand All @@ -44,8 +24,10 @@ public Reminder getSelectedReminder() {
public void setSelectedReminder(Reminder reminder) {
this.selectedReminder = reminder;
}

private List<Reminder> reminders = new ArrayList<Reminder>();

public List<Reminder> getReminders() {
return this.findAll();
return this.reminders;
}
}
4 changes: 3 additions & 1 deletion src/main/java/org/zkforge/zktodo2/Reminder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public int hashCode() {
@Column(name = "DATE")
private Date date = new Date(System.currentTimeMillis());

public Reminder(){}
public Reminder(){
//noop
}
public Reminder(String name,int priority,Date date){
this.name = name;
this.priority = priority;
Expand Down
129 changes: 58 additions & 71 deletions src/main/java/org/zkforge/zktodo2/ui/Controller.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@

package org.zkforge.zktodo2.ui;

import static java.lang.System.out;

import java.util.Date;
import java.util.List;

import org.zkforge.zktodo2.Model;
import org.zkforge.zktodo2.Reminder;
import org.zkforge.zktodo2.ReminderService;
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.select.Selectors;
Expand All @@ -23,124 +20,114 @@
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;

/**
* This class demonstrates "Supervising Presenter" pattern as the Composer is
* not doing all the explicit work of updating the UI.
* This class demonstrates "Supervising Presenter" pattern.
*
* {@link http://martinfowler.com/eaaDev/SupervisingPresenter.html}
*/
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) // wire with Spring
public class Controller {

public Controller(){
// noop
}

// wired components
@Wire Textbox name;
@Wire Intbox priority;
@Wire Datebox date;
@Wire Listbox list;

public Controller(){
// noop
}

@Init
public void init(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireComponents(view, this, false);
}

// wired property
@WireVariable Model model = null;

public void setModel(Model model) {
this.model = model;
}

public Model getModel() {
return model;
}

// wired property
@WireVariable ReminderService reminderService = null;

public ReminderService getReminderService() {
return reminderService;
}

public void setReminderService(ReminderService reminderService) {
this.reminderService = reminderService;
}

@Init
public void init(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireComponents(view, this, false);
reload();
}

/**
* @param e The event is unused but is here to indicated that the have access to the screen context
*/
@Command
@NotifyChange({"reminders","selectedReminder"})
public void create() {
public void create(@ContextParam(ContextType.TRIGGER_EVENT) Event e) {
Date dateValue = date.getValue();
Integer priorityValue = priority.getValue();
String nameValue = name.getValue();
if( dateValue != null && priorityValue != null && nameValue != null ){
Reminder reminder = new Reminder();
reminder.setDate(date.getValue());
reminder.setName(name.getValue());
reminder.setPriority(priority.getValue());
this.model.persistEvent(reminder);
List<Reminder> reminders = this.model.findAll();
ListModel<Reminder> listModel = this.list.getModel();
ListModelList<Reminder> listModelList = (ListModelList<Reminder>)listModel;
listModelList.clear();
listModelList.addAll(reminders);
Reminder reminder = new Reminder(nameValue, priorityValue, dateValue);
this.reminderService.persist(reminder);
this.model.setSelectedReminder(reminder);
reload();
}
return;
}

protected void reload() {
List<Reminder> reminders = this.reminderService.findAll();
this.model.getReminders().clear();
this.model.getReminders().addAll(reminders);
}

/**
* @param e The event is unused but is here to indicated that the have access to the screen context
*/
@Command
@NotifyChange({"reminders","selectedReminder"})
public void save() {
public void save(@ContextParam(ContextType.TRIGGER_EVENT) Event e) {
Reminder selectedReminder = this.model.getSelectedReminder();
if( selectedReminder != null ){
ListModelList<Object> listModelList = (ListModelList<Object>) this.list.getModel();
try {
this.model.persistEvent(selectedReminder);
this.reminderService.persist(selectedReminder);
} catch (Exception exception){
int index = list.getSelectedIndex();
listModelList.remove(index);
if( listModelList.size() > 0 ){
selectedReminder = (Reminder)listModelList.get(0);
list.setSelectedIndex(0);
name.setValue(selectedReminder.getName());
date.setValue(selectedReminder.getDate());
priority.setValue(selectedReminder.getPriority());
} else {
selectedReminder = null;
}
// not implemented
}
List<Reminder> reminders = model.findAll();
listModelList.clear();
listModelList.addAll(reminders);
reload();
}
}

/**
* @param e The event is unused but is here to indicated that the have access to the screen context
*/
@Command
@NotifyChange({"reminders","selectedReminder"})
public void delete(@BindingParam("e") Event e) {
public void delete(@ContextParam(ContextType.TRIGGER_EVENT) Event e) {
Reminder selectedReminder = this.model.getSelectedReminder();
if( null != selectedReminder ){
ListModel<Reminder> listModel = this.list.getModel();
ListModelList<Reminder> listModelList = (ListModelList<Reminder>)listModel;
int index = listModelList.indexOf(selectedReminder);
try {
this.model.deleteEvent(selectedReminder);
this.reminderService.delete(selectedReminder);
} catch (Exception exception ){
out.println("This is harmless as someone else has already deleted this item.");
}
listModelList.remove(selectedReminder);
if( index >= listModelList.size() ){
index = listModelList.size() - 1;
}
if( listModelList.size() > 0 && index >= 0 ){
selectedReminder = (Reminder)listModelList.get(index);
list.setSelectedIndex(index);
name.setValue(selectedReminder.getName());
date.setValue(selectedReminder.getDate());
priority.setValue(selectedReminder.getPriority());
} else {
selectedReminder = null;
// not implemented
}
this.model.setSelectedReminder(null);
reload();
}
}

Converter dateConverter = new TimestampConverter();
Converter<?,?,?> dateConverter = new TimestampConverter();

public Converter getDateConverter() {
public Converter<?,?,?> getDateConverter() {
return dateConverter;
}
}
7 changes: 5 additions & 2 deletions src/main/java/org/zkforge/zktodo2/ui/Presenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ public class Presenter extends SelectorComposer<Window> implements

private static final long serialVersionUID = -3486059156312322420L;

// service
@WireVariable ReminderService reminderService;

// components
@Wire Textbox name;
@Wire Intbox priority;
@Wire Datebox date;
@Wire Listbox list;

// conversation state
ListModelList<Reminder> listModelList;
Reminder selectedReminder = new Reminder();

Expand All @@ -53,7 +56,8 @@ public Presenter() {

@Override
public void doAfterCompose(Window comp) throws Exception {
super.doAfterCompose(comp);
super.doAfterCompose(comp); // super method wires the components
// load the data and bind to the list then set self as list renderer
listModelList = new ListModelList<Reminder>();
List<Reminder> reminders = reminderService.findAll();
listModelList.addAll(reminders);
Expand Down Expand Up @@ -113,7 +117,6 @@ public void update(Event e) {
List<Reminder> reminders = reminderService.findAll();
listModelList.clear();
listModelList.addAll(reminders);

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.zkforge.zktodo2;
package org.zkforge.zktodo2.ui;

import org.zkoss.bind.ValidationContext;
import org.zkoss.bind.validator.AbstractValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.zkoss.bind.Converter;
import org.zkoss.zk.ui.Component;

@SuppressWarnings("rawtypes")
final class TimestampConverter implements Converter {
@Override
public Object coerceToUi(Object val, Component c, BindContext b) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/zkforge/zktodo2/ui/ViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void create() {
this.selectedReminder = new Reminder();
}

Converter dateConverter = new TimestampConverter();
Converter<?,?,?> dateConverter = new TimestampConverter();

public Converter getDateConverter() {
public Converter<?,?,?> getDateConverter() {
return dateConverter;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/applicationmodel.zul
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</template>
</listbox>
<vlayout
form="@id('r') @load(vm.selectedReminder) @save(vm.selectedReminder, before='save') @load(vm.newReminder, after='new') @validator('org.zkforge.zktodo2.ReminderValidator')">
form="@id('r') @load(vm.selectedReminder) @save(vm.selectedReminder, before='save') @load(vm.newReminder, after='new') @validator('org.zkforge.zktodo2.ui.ReminderValidator')">
<hlayout>
Item:
<textbox cols="40" instant="true"
Expand Down
Loading

0 comments on commit 7f4340e

Please sign in to comment.