Processing...
Description & Source Code

MVVM is an abbreviation of a design pattern named Model-View-ViewModel which is originated from Microsoft.
It is a specialization of Presentation Model introduced by Martin Fowler, a variant of the famous MVC pattern.

For more details, please refer to ZK Developer's Reference - MVVM

mvvm_pattern.zul
<window sclass="cardSys" border="none" apply="org.zkoss.bind.BindComposer" 
	viewModel="@id('vm') @init('demo.composite.mvvm_pattern.ContributorVM')">

	<style src="/widgets/composite/mvvm_pattern/Style.css" />
	<borderlayout height="500px" sclass="mainLayout">
		<center title="Contributor List">
			<listbox sclass="contributor-list" model="@load(vm.contributorList)" 
				selectedItem="@bind(vm.selectedContributor)" vflex="true" hflex="true"
				onSelect="@command('select')">
				<listhead>
					<listheader label="Title" width="80px" align="center" />
					<listheader label="Full Name" align="center" />
					<listheader label="Extension" width="80px" align="center" />
				</listhead>
				<template name="model" var="contributor">
					<listitem>
						<listcell label="@load(contributor.title)" />
						<listcell label="@load(contributor.fullName)" />
						<listcell label="@load(contributor.extension)" />
					</listitem>
				</template>

			</listbox>
		</center>
		<east collapsible="true" title="Name Card Preview" width="350px">
			<div sclass="card-pre" vflex="true">
				<div sclass="card">
					<image sclass="logo" src="/images/ZK-Logo.gif" />
					<vlayout sclass="title">
						<label sclass="name" value="@load(vm.previewContributor.fullName)" />
						<hlayout sclass="dept">
							<label value="@load(vm.previewContributor.title)" />
							Contributor
						</hlayout>
					</vlayout>
					<div sclass="number">
						Tel:+1-23-456-789 ext
						<label value="@load(vm.previewContributor.extension)" />
					</div>
				</div>
			</div>
		</east>
		<south>
			<grid sclass="editGrid"
				form="@id('editor') @load(vm.previewContributor)
					  @load(vm.previewContributor, after={'create','preview'})
					  @save(vm.previewContributor, before={'save','preview','update','delete'})">
				<auxhead>
					<auxheader colspan="5" class="topic">Edit Data</auxheader>
				</auxhead>
				<columns>
					<column label="Title" align="center" />
					<column label="First Name" align="center" />
					<column label="Last Name" align="center" />
					<column label="Extension" align="center" />
					<column label="Full Name" align="center" />
				</columns>
				<rows>
					<row>
						<combobox id="field_title" model="@load(vm.contributorTitles)" width="99%" 
							selectedItem="@bind(editor.title)" onChange='@command("preview")' />
						<textbox id="field_firstName" value="@bind(editor.firstName)" width="95%" 
							onChange='@command("preview")' />
						<textbox id="field_lastName" value="@bind(editor.lastName)" width="95%" 
							onChange='@command("preview")' />
						<intbox id="field_ext" value="@bind(editor.extension)" width="95px" 
							onChange='@command("preview")' />
						<label value="@load(editor.fullName)" />
					</row>
					<row>
						<cell colspan="5">
							<!-- Use vm.addingNewItemMode as status -->
							<button label="CreateNew" onClick="@command('create')" 
								width="150px" height="30px" />
							<button label="Save" onClick="@command('save')" 
								width="150px" height="30px" disabled='@bind(!vm.addingNewItemMode)' />
							<button label="Update" onClick="@command('update')" 
								width="150px" height="30px" disabled='@bind(vm.addingNewItemMode)' />
							<button label="Delete" onClick="@command('delete')" 
								width="150px" height="30px" disabled='@bind(vm.addingNewItemMode)' />
						</cell>
					</row>
				</rows>
			</grid>

		</south>
	</borderlayout>
</window>
ContributorVM.java
package demo.composite.mvvm_pattern;

import demo.data.ContributorData;
import demo.data.pojo.Contributor;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;

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

public class ContributorVM {

	private List<String> availTitles;
	private List<Contributor> allContributors;

	// Used for binding with selected data,
	private Contributor selectedContributor;
	// A temporary variable which is used for previewing and editing contributor
	private Contributor previewContributor;
	// Default is doing selection
	private boolean addingNewItemMode = false;

	@Init
	public void init() {
		// Preparing data
		availTitles = new ArrayList<String>(new ContributorData().getTitles());
		allContributors = new ArrayList<Contributor>(new ContributorData().getContributors());
		// Select the first item as default
		setSelectedContributor(allContributors.get(0));
		copyToTemp(selectedContributor);
	}

	@Command("select")
	@NotifyChange({ "previewContributor", "addingNewItemMode" })
	public void selectContributor() {
		copyToTemp(selectedContributor);
		setAddingNewItemMode(false);
	}

	@Command("create")
	@NotifyChange({ "previewContributor", "addingNewItemMode" })
	public void createNewContributor() {
		setPreviewContributor(getInitializedContributor());
		setAddingNewItemMode(true);
	}

	@Command("preview")
	@NotifyChange("previewContributor")
	public void previewEditingContributor() {
	}

	@Command("save")
	@NotifyChange({ "contributorList", "addingNewItemMode" })
	public void addNewContributor() {
		getContributorList().add(previewContributor);
		setSelectedContributor(previewContributor);
		setAddingNewItemMode(false);
	}

	@Command("update")
	@NotifyChange({ "selectedContributor", "previewContributor" })
	public void updateSelectedContributor() {
		saveToSelected(previewContributor);
	}

	@Command("delete")
	@NotifyChange({ "selectedContributor", "contributorList", "previewContributor", "addingNewItemMode" })
	public void removeSelectedContributor() {
		getContributorList().remove(selectedContributor);
		setSelectedContributor(null);
		setPreviewContributor(getInitializedContributor());
		setAddingNewItemMode(true);
	}

	/* View Model API */
	public List<String> getContributorTitles() {
		return availTitles;
	}

	public List<Contributor> getContributorList() {
		return allContributors;
	}

	public void setSelectedContributor(Contributor selected) {
		this.selectedContributor = selected;
	}

	public Contributor getSelectedContributor() {
		return selectedContributor;
	}

	public Contributor getPreviewContributor() {
		return previewContributor;
	}

	public void setPreviewContributor(Contributor previewContributor) {
		this.previewContributor = previewContributor;
	}

	public boolean isAddingNewItemMode() {
		return addingNewItemMode;
	}

	public void setAddingNewItemMode(boolean addingNewItemMode) {
		this.addingNewItemMode = addingNewItemMode;
	}

	private Contributor getInitializedContributor() {
		return new Contributor("", "", getContributorTitles().get(0) + "", 0);
	}

	private void saveToSelected(Contributor contributor) {
		selectedContributor.setFirstName(contributor.getFirstName());
		selectedContributor.setLastName(contributor.getLastName());
		selectedContributor.setTitle(contributor.getTitle());
		selectedContributor.setExtension(contributor.getExtension());
	}

	private void copyToTemp(Contributor contributor) {
		previewContributor = new Contributor(contributor.getFirstName(), contributor.getLastName(),
				contributor.getTitle(), contributor.getExtension());
	}
}
ContributorData.java
package demo.data;

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

import demo.data.pojo.Contributor;
import demo.data.pojo.LanguageContribution;

public class ContributorData {

	private List<String> titles = new ArrayList<String>();
	private List<Contributor> contributors = new ArrayList<Contributor>();
	private List<LanguageContribution> languageContributions = new ArrayList<LanguageContribution>();

	public ContributorData() {
		titles.add("Code");
		titles.add("Bug");
		titles.add("Docs");
		titles.add("Arts");

		contributors.add(new Contributor("Kaleb", "Leonel", titles.get(0).toString(), 321));
		contributors.add(new Contributor("Balu", "Haben", titles.get(0).toString(), 321));
		contributors.add(new Contributor("Trey", "Wyatt", titles.get(0).toString(), 323));
		contributors.add(new Contributor("Balu", "Chen", titles.get(0).toString(), 324));
		contributors.add(new Contributor("Terry", "Tornado", titles.get(0).toString(), 711));
		contributors.add(new Contributor("Jesse", "Miles", titles.get(1).toString(), 712));
		contributors.add(new Contributor("Sadira", "Jobs", titles.get(1).toString(), 713));
		contributors.add(new Contributor("Jaquan", "Frederick", titles.get(2).toString(), 451));
		contributors.add(new Contributor("Avery", "Katrina", titles.get(2).toString(), 453));
		contributors.add(new Contributor("Heidi", "Nikolas", titles.get(2).toString(), 455));
		contributors.add(new Contributor("Katelyn", "Clara", titles.get(2).toString(), 457));
		contributors.add(new Contributor("Branden", "Shane", titles.get(2).toString(), 459));
		contributors.add(new Contributor("Dacey", "Obert", titles.get(2).toString(), 450));
		contributors.add(new Contributor("Julianna", "Allison", titles.get(3).toString(), 643));
		contributors.add(new Contributor("Rachel", "Elisabeth", titles.get(3).toString(), 644));
		contributors.add(new Contributor("Clarissa", "Francesca", titles.get(3).toString(), 645));
		contributors.add(new Contributor("Gabby", "Taffy", titles.get(3).toString(), 646));
		
		languageContributions.add(new LanguageContribution("Arabic (ar)", "Ayman Elgharabawy", "iso-8859-6"));
		languageContributions.add(new LanguageContribution("Hungarian (hu)", "Andreas Klein", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Italian (it)", "Matteo Barbieri", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Indonesian (id)", "James Liam Supangkat", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Japanese (ja)", "Poli Lee", " iso-2022-jp"));
		languageContributions.add(new LanguageContribution("Korean (ko)", "Deok-su Lee", "euc-kr"));
		languageContributions.add(new LanguageContribution("Dutch (nl)", "Wido Jansen", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Portuguese (pt)", "Airton Carrara", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Brazilian Portugese (pt_BR)", "Airton Carrara", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Romanian (ro)", "Calin Vaida", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Russian (ru)", "Denis Yarkovoy", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("French (fr)", "Jêrôme Vergereau", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Spanish (es)", "Daniel Octavio Ruiz Rodriguez", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("German (de)", "H.-Dirk Schmitt", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Czech (cs)", "Pavel Mica", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Catalan (ca)", "Xavier Covas O'Ryan", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Bulgarian (bg) ", "Boril Yonchev", "iso-8859-5"));
		languageContributions.add(new LanguageContribution("Ukrainian (uk)", "Denis Yarkovoy", "iso-8859-5"));
		languageContributions.add(new LanguageContribution("Turkish (tr)", "Kursat Kurt", "iso-8859-9"));
		languageContributions.add(new LanguageContribution("Swedish (sv)", "Easit AB", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Slovenian (sl) ", "Ziga", "iso-8859-2"));
	}

	public List<String> getTitles() {
		return titles;
	}

	public List<Contributor> getContributors() {
		return contributors;
	}

	public List<LanguageContribution> getLanguageContributors() {
		return languageContributions;
	}

}
Contributor.java
package demo.data.pojo;

import org.zkoss.bind.annotation.DependsOn;

public class Contributor {
	private String title;
	private String firstName;
	private String lastName;
	private Integer extension;

	public Contributor(String firstName, String lastName, String title, Integer ext) {
		setFirstName(firstName);
		setLastName(lastName);
		setTitle(title);
		setExtension(ext);
	}

	public Contributor() {
		setFirstName("");
		setLastName("");
		setTitle("Code");
		setExtension(12345);
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String fn) {
		firstName = fn;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String ln) {
		lastName = ln;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String tl) {
		title = tl;
	}

	public Integer getExtension() {
		return extension;
	}

	public void setExtension(Integer ext) {
		extension = ext;
	}

	@DependsOn({ "firstName", "lastName" })
	public String getFullName() {
		return getFirstName() + " " + getLastName();
	}
}
LanguageContribution.java
package demo.data.pojo;

public class LanguageContribution {
	private String language, name, charset;

	public LanguageContribution(String language, String name, String charset) {
		this.language = language;
		this.name = name;
		this.charset = charset;
	}
	
	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}
}