Skip to content

Commit

Permalink
zk6 only refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
simbo1905 committed May 6, 2012
1 parent 684e747 commit df0e327
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 289 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.zkforge.zktodo2</groupId>
<artifactId>zktodo2</artifactId>
<packaging>war</packaging>
<version>1.7.0</version>
<version>1.8.0</version>
<name>zktodo2</name>
<url>http://www.slideshare.net/simbo1905/design-patterns-in-zk-java-mvvm-as-modelviewbinder</url>
<repositories>
Expand All @@ -17,6 +17,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<zk.version>6.0.0</zk.version>
<zkel.version>1.0.2</zkel.version>
<zkspring.version>3.1</zkspring.version>
<spring.version>3.0.5.RELEASE</spring.version>
<hsqldb.version>1.8.0.7</hsqldb.version>
<hibernate.version>3.3.1.ga</hibernate.version>
Expand All @@ -29,7 +30,6 @@
<servlet-api.version>2.4</servlet-api.version>
<junit.version>4.7</junit.version>
<concurrent.version>1.3.4</concurrent.version>
<zkspring.version>3.0</zkspring.version>
<commons-lang.version>2.5</commons-lang.version>
<jetty.version>7.4.5.v20110725</jetty.version>
<postgresql.version>8.4-702.jdbc3</postgresql.version>
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/org/zkforge/zktodo2/DateFormatConverter.java

This file was deleted.

46 changes: 46 additions & 0 deletions src/main/java/org/zkforge/zktodo2/Model.java
Original file line number Diff line number Diff line change
@@ -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<Reminder> 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<Reminder> getReminders() {
return this.findAll();
}
}
33 changes: 0 additions & 33 deletions src/main/java/org/zkforge/zktodo2/ZkToDoModel.java

This file was deleted.

74 changes: 0 additions & 74 deletions src/main/java/org/zkforge/zktodo2/ZkToDoModelImpl.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Window> {
@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();
Expand All @@ -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<Reminder> reminders = this.toDoModel.findAll();
this.model.persistEvent(reminder);
List<Reminder> reminders = this.model.findAll();
ListModel<Reminder> listModel = this.list.getModel();
ListModelList<Reminder> listModelList = (ListModelList<Reminder>)listModel;
listModelList.clear();
Expand All @@ -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<Object> listModelList = (ListModelList<Object>) 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);
Expand All @@ -83,28 +104,30 @@ public void setToDoModel(ZkToDoModel toDoModel) {
selectedReminder = null;
}
}
List<Reminder> reminders = toDoModel.findAll();
List<Reminder> 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<Reminder> listModel = this.list.getModel();
ListModelList<Reminder> listModelList = (ListModelList<Reminder>)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.");
}
listModelList.remove(selectedReminder);
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());
Expand All @@ -115,4 +138,10 @@ public void setToDoModel(ZkToDoModel toDoModel) {
}
}
}

Converter dateConverter = new TimestampConverter();

public Converter getDateConverter() {
return dateConverter;
}
}
Loading

0 comments on commit df0e327

Please sign in to comment.