Wednesday, 27 June 2012

Combo Box– Show images for Items based on some condition

We will see how we can also show the images for the combo items bases on some conditions.

ZK Version 6
Project Name : ComboItemIcons
Project Structure:

image

Demo.zul

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window id="myWin" title="new page title" border="normal"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('myvm') @init('mydomainUI.DemoVM')">
Users:
<combobox model="@load(myvm.allUsers)">
<template name="model" var="p1">
<comboitem label="@load(p1.userName)"
image="@load(p1.active eq 'Y' ? './img/active.png' : './img/inactive.png')"
value="@load(p1.userCode)" />
</template>
</combobox>
</window>
</zk>


users.java


package mydomain;

public class users {

private String userCode;
private String userName;
private String userPassword;
private String active;
private String firstName;
private String lastName;
private String homePhone;
private String mobilePhone;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getHomePhone() {
return homePhone;
}

public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}

public String getMobilePhone() {
return mobilePhone;
}

public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}

public String getUserCode() {
return userCode;
}

public void setUserCode(String userCode) {
this.userCode = userCode;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPassword() {
return userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public String getActive() {
return active;
}

public void setActive(String active) {
this.active = active;
}

}


usersDAO.java


package domainDAO;

import java.util.ArrayList;
import java.util.List;
import mydomain.users;

public class usersDAO {

public List<users> getUsers() {

List<users> allUsers = new ArrayList<users>();

users u1 = new users();
u1.setUserName("John");
u1.setUserCode("User101");
u1.setUserPassword("xxxxx");
u1.setFirstName("JohnFirstName");
u1.setLastName("JohnLastName");
u1.setHomePhone("1111111111");
u1.setMobilePhone("222222");
u1.setActive("Y");
allUsers.add(u1);

u1 = new users();
u1.setUserName("Robert");
u1.setUserCode("User102");
u1.setUserPassword("xxxxx");
u1.setActive("Y");
u1.setFirstName("RobertFirstName");
u1.setLastName("RobertLastName");
u1.setHomePhone("53534343");
u1.setMobilePhone("4534343");
allUsers.add(u1);

u1 = new users();
u1.setUserName("Sean");
u1.setUserCode("User105");
u1.setUserPassword("xxxxx");
u1.setFirstName("SeanFirstName");
u1.setLastName("SeanLastName");
u1.setHomePhone("1111111111");
u1.setMobilePhone("222222");
u1.setActive("Y");
allUsers.add(u1);

u1 = new users();
u1.setUserName("Marry");
u1.setUserCode("User112");
u1.setUserPassword("xxxxx");
u1.setActive("N");
u1.setFirstName("MarryFirstName");
u1.setLastName("MarryLastName");
u1.setHomePhone("53534343");
u1.setMobilePhone("4534343");
allUsers.add(u1);

return allUsers;
}

}


DemoVM.java


package mydomainUI;

import java.util.ArrayList;
import java.util.List;
import org.zkoss.bind.annotation.Init;

import domainDAO.usersDAO;
import mydomain.users;

public class DemoVM {

private List<users> allUsers = new ArrayList<users>();

public List<users> getAllUsers() {
return allUsers;
}

public void setAllUsers(List<users> allUsers) {
this.allUsers = allUsers;
}


@Init
public void initSetup() {
// Load the department first
allUsers = new usersDAO().getUsers();

}

}


Now you can run demo.zul. The output as follows


image



You can download source here

Tuesday, 26 June 2012

ZK Border Layout–Another example

This examples show how you can use ZK Border Layout to show the product information on clicking image.

ZK Version 6
Project Name : BorderLayout2
Project Structure:

image

Output:

image

Demo.zul

<zk>

<window border="none" width="100%" height="100%" id="main">
<separator />
<div left="20px" height="25px">
<image src="/images/zklogo_s.png"
style="padding-left: 10px;" height="30px" width="auto" />
</div>
<separator />
<borderlayout id="mainlayout">
<north border="normal" height="70px">
<div id="Menu" sclass="Mainmenudiv">
<include src="mainmenu.zul" />
</div>
</north>
<west title=" " size="20%" flex="true" splittable="true"
collapsible="true">
<div id="nav" style="background:white; height:100%">
<include src="navmenu.zul" />
</div>
</west>
<center border="normal">
<div height="99%" id="Screen"></div>
</center>
<south height="70px">
<div align="center">
<label
value="Copyright © 2012 xxxxxxxx All Rights Reserved. Powered By xxxxx"
sclass="loginlabel" />
</div>
</south>
</borderlayout>
</window>
</zk>

mainmenu.zul
<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="Welcome to ZK" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>

navmenu.zul
<?page title="Method3"?>

<zk>
<style>
.z-groupbox-3d-hm { background-image: none; background-color:
#0A246A !important; } .z-groupbox-3d-header .z-caption { color:
yellow; font-weight: bold;; } .z-groupbox-3d-cnt{
background-image: none; background-color: #EAECF0 !important; }

.open-true {float:right; background:url('img/UP201.png')
no-repeat right 0; height: 16px; padding-right: 20px;
font-weight: bold; }

.open-false {float:right; background:url('img/Down201.png')
no-repeat right 0; height: 16px; padding-right: 20px;
font-weight: bold; }

</style>
<window title="Administration" border="normal" width="270px"
apply="mydomain.democomposer">
<groupbox width="250px" mold="3d">
<attribute name="onOpen"><![CDATA[
arrow1.setSclass("open-" + self.isOpen());
]]></attribute>
<caption label="Products">
<div id="arrow1" class="open-true"></div>
</caption>

<vbox>
<image id = "product1" src="/img/product1.png" forward="onClick=onClickMenu()"
style="padding-left: 10px; cursor: pointer" height="100px" width="auto" />
<separator bar="true" width="250px"/>
<image id = "product2" src="/img/product2.png" forward="onClick=onClickMenu()"
style="padding-left: 10px; cursor: pointer" height="100px" width="auto" />
<separator bar="true" width="250px"/>
<image id = "product3" src="/img/product3.png" forward="onClick=onClickMenu()"
style="padding-left: 10px; cursor: pointer" height="100px" width="auto" />
<separator bar="true" width="250px"/>

</vbox>
</groupbox>

</window>
</zk>


product1.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="Product 1 Information goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>

product2.zul
<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="Product 2 Information goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>

product3.zul
<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="Product 3 Information goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


democomposer.java


package mydomain;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.MouseEvent;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Center;
import org.zkoss.zul.Messagebox;

public class democomposer extends GenericForwardComposer {

public void onClickMenu(MouseEvent event) {

String zulFilePathName;
Borderlayout bl = (Borderlayout) Path.getComponent("/main/mainlayout");
/* get an instance of the searched CENTER layout area */
Center center = bl.getCenter();

/* clear the center child comps */
center.getChildren().clear();

//Messagebox.show("inside" + event.getTarget().getId());
zulFilePathName = event.getTarget().getId() + ".zul";
/* create the page and put it in the center layout area */
Executions.createComponents(zulFilePathName, center, null);
}
}


You can download source here

Monday, 25 June 2012

MVVM–List Item–Hibernate–MySQL–Part 3

Note: This is continuation of my previous article Part 2.  Please click here to go to Part 2

In this Part 3, we will enhance our Listing and model window as follows

1. We will include one more column as last named as Actions. This action will contain image such as edit, activate and delete.
2. We will also give hyperlink for the first column. If the user clicks the first column, then we will show the information in the read-only and if the user clicks the edit image in the action column, then we will show the information in the read only and will allow the user to edit and save.

image

Project Structure

image

Demo.zul

<?page title="Listitem MVVM Demo with Hibernate" contentType="text/html;charset=UTF-8"?>
<zk>
<style>
.z-listcell.red .z-listcell-cnt, .z-label.red{ color:red; }

/* Start: Action Images- Edit
---------------------------------------------- */

.fimageedit { width: 25px; background-image:
url('./images/icon-edit.png'); background-repeat: no-repeat;
border: 0 none; cursor: pointer; }

/* End: Action Images - Edit
---------------------------------------------- */


/* Start: Action Images- Delete
---------------------------------------------- */

.fimageDelete { width: 25px; background-image:
url('./images/icon-trash-red.png'); background-repeat:
no-repeat; border: 0 none; cursor: pointer; }

/* End: Action Images - Delete
---------------------------------------------- */


/* Start: Action Images- Active
---------------------------------------------- */

.fimageActive { width: 25px; background-image:
url('./images/icon-enable.png'); background-repeat: no-repeat;
border: 0 none; cursor: pointer; }

/* End: Action Images - Active
---------------------------------------------- */

/* Start: Action Images- Inactive' ]

---------------------------------------------- */

.fimageInactive { width: 25px; background-image:
url('./images/icon-disable.png'); background-repeat: no-repeat;
border: 0 none; cursor: pointer; }

/* End: Action Images - InActive
---------------------------------------------- */

.z-listcell.highlightcell .z-listcell-cnt,
.z-label.highlightcell { color:blue; cursor: pointer; }


</style>
<window title="Listitem MVVM Demo with Hibernate" border="normal"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('myvm') @init('domainVMS.PersonVM')">
<div>
<button label="Add Person"
onClick="@command('addNewPerson')" />
</div>
<separator />

<listbox id="test" model="@load(myvm.allPersons)"
selectedItem="@bind(myvm.curSelectedPerson)">
<listhead sizable="true">
<listheader label="First Name" width="400px"
sort="auto(firstName)" />
<listheader label="Last Name" width="285px"
sort="auto(lastName)" />
<listheader label="email" width="285px"
sort="auto(email)" />
<listheader label="Action" />
</listhead>
<template name="model" var="p1">
<listitem>
<listcell label="@load(p1.firstName)"
onClick="@command('openAsReadOnly')" sclass="highlightcell" />
<listcell label="@load(p1.lastName)"
sclass="@load(empty p1.email ?'red':'')" />
<listcell label="@load(p1.email)" />
<listcell>
<hbox spacing="20px">
<image sclass="fimageActive" />
<image sclass="fimageDelete" />
<image sclass="fimageedit"
onClick="@command('editThisPerson')" />
</hbox>
</listcell>
</listitem>
</template>
</listbox>
</window>
</zk>



PersonVM.java

package domainVMS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import mydomain.Person;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Messagebox;
import domainDAO.PersonDAO;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;

public class PersonVM {

private List<Person> persons = new ArrayList<Person>();
private Person curSelectedPerson;

public Person getCurSelectedPerson() {
return curSelectedPerson;
}

public void setCurSelectedPerson(Person curSelectedPerson) {
this.curSelectedPerson = curSelectedPerson;
}

public List<Person> getallPersons() {
return persons;
}

@Init
public void initSetup() {
persons = new PersonDAO().getAllPersons();

}

@Command
public void editThisPerson () {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("selectedPerson", curSelectedPerson);
map.put("recordMode", "EDIT");
Executions.createComponents("AddPerson.zul", null, map);
}

@Command
public void openAsReadOnly()
{
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("recordMode", "READ");
map.put("selectedPerson", curSelectedPerson);
Executions.createComponents("AddPerson.zul", null, map);

}

@Command
public void addNewPerson() {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("selectedPerson", null);
map.put("recordMode", "NEW");
Executions.createComponents("AddPerson.zul", null, map);
}


//note this will be executed after when we new person from the model window
@GlobalCommand
@NotifyChange("allPersons")
public void refreshList(@BindingParam("newadded") Person p1)
{
persons.add(p1);
}

}


PersonCRUDVM.java


package domainVMS;

import java.util.HashMap;
import org.zkoss.zul.Button;
import java.util.List;
import java.util.Map;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Messagebox;
import org.zkoss.zk.ui.Component;
import domainDAO.PersonDAO;
import mydomain.Person;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;
import org.zkoss.zul.impl.InputElement;

public class PersonCRUDVM {

@Wire("#win")
private Window win;
@Wire
private Button submit;
@Wire
private Button cancel;
@Wire("textbox")
List<InputElement> inputs;

private Person selectedPerson;

public Person getSelectedPerson() {
return selectedPerson;
}

public void setSelectedPerson(Person selectedPerson) {
this.selectedPerson = selectedPerson;
}

@Init
public void initSetup(@ContextParam(ContextType.VIEW) Component view,
@ExecutionArgParam("selectedPerson") Person selectedPerson,
@ExecutionArgParam("recordMode") String recordMode) {
Selectors.wireComponents(view, this, false);
if (selectedPerson == null)
this.selectedPerson = new Person();
else
this.selectedPerson = selectedPerson;

if (recordMode == "READ") {
submit.setVisible(false);
cancel.setLabel("Ok");
for (InputElement i : inputs)
i.setReadonly(true);
win.setTitle(win.getTitle() + " (Readonly)");
}

}

@Command
public void save() {
new PersonDAO().saveOrUpdate(this.selectedPerson);
Map args = new HashMap();
args.put("newadded", this.selectedPerson);
BindUtils.postGlobalCommand(null, null, "refreshList", args);
win.detach();
}

@Command
public void closeThis() {
win.detach();
}
}


AddPerson.zul


<zk>
<window id="win" title="Person" width="520px" height="220px"
border="normal" minimizable="false" mode="modal" maximizable="false"
closable="true" action="show: slideDown;hide: slideUp"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('domainVMS.PersonCRUDVM')" >
<separator />
<label value="Person information" />
<separator />
<panel width="100%">
<panelchildren>
<separator />
<grid width="99.5%">
<columns>
<column label="" width="150px" />
<column label="" />
</columns>
<rows>
<row>
<hbox>
<label value="First Name" />
<label value="*" />
</hbox>
<textbox name="firstName"
value="@bind(vm.selectedPerson.firstName)" cols="50" />
</row>
<row>
<hbox>
<label value="Last Name" />
<label value="*" />
</hbox>
<textbox name="firstName"
value="@bind(vm.selectedPerson.lastName)" cols="50" />
</row>
<row>
<hbox>
<label value="email" />
<label value="*" />
</hbox>
<textbox name="email"
value="@bind(vm.selectedPerson.email)" cols="50" />
</row>
</rows>
</grid>
</panelchildren>
</panel>
<separator />
<div align="center">
<button id="submit" label="Submit" onClick="@command('save')" />
<button id="cancel" label="Cancel" onClick="@command('closeThis')" />
</div>
</window>
</zk>



Download the source as war file




Download as individual files as follows

hibernate.cfg.xml
Person.java
HibernateUtil.java
PersonVM.java
PersonCRUDVM.java
PersonDAO.java
icon-trash-red.png
icon-edit.png
icon-enable.png
Demo.zul
AddPerson.zul

Saturday, 23 June 2012

C Workbook

In the year 1999-2002, I was working as faculty in a computer center. During that period, I developed a small VB Application to explain the concept of C. Here are the some screenshots from that VB Project

image

 

image

 

image

 

image

 

image

 

image

Friday, 22 June 2012

MVVM Modal window–Pass Parameter and Return values

In this post, we will see how we can pass some values to the modal window when calling from the parent window and also vice versa (i.e) return some values from the modal window to parent window

 

Project Name : MVVMModalWindow
Project Structure:

image

Demo.zul

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="MVVM Modal window Passing arguments and retur values"
border="normal" apply="org.zkoss.bind.BindComposer"
viewModel="@id('e') @init('com.demo.UI.demoVM')">
Type any value and Press the Model Window Button
<separator />
Value 1 :
<textbox value="@bind(e.value1)" />
Value 2 :
<textbox value="@bind(e.value2)" />
<button label="Model Window" onClick="@command('showModelWin')" />
</window>
</zk>

 

demoVM.java

 

package com.demo.UI;

import java.util.HashMap;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.zk.ui.Executions;
import org.zkoss.bind.annotation.NotifyChange;

public class demoVM {

private String value1;
private String value2;


public String getValue1() {
return value1;
}

public void setValue1(String value1) {
this.value1 = value1;
}

public String getValue2() {
return value2;
}

public void setValue2(String value2) {
this.value2 = value2;
}

@Command
public void showModelWin()
{
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("value1", this.value1 );
map.put("value2", this.value2);
Executions.createComponents("ModelWindow.zul", null, map);
}

@GlobalCommand
@NotifyChange({"value1","value2"})
public void refreshvalues(@BindingParam("returnvalue1") String str1, @BindingParam("returnvalue2") String str2)
{
this.value1 = str1;
this.value2 = str2;
}
}


ModelWindow.zul

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>

<window id="modalDialog"
title="MVVM Modal window Passing arguments and retur values"
width="420px" height="auto" border="normal" minimizable="false"
mode="modal" maximizable="false" closable="true"
action="hide: slideUp" apply="org.zkoss.bind.BindComposer"
onCancel="@command('closeThis')"
viewModel="@id('e') @init('com.demo.UI.ModelWindowVM')">

Change the values and Press the Ok Button to return changed
values.
<separator />
Value 1 :
<textbox value="@bind(e.value1)" />
Value 2 :
<textbox value="@bind(e.value2)" />
<button label="Ok" onClick="@command('save')" />
</window>
</zk>


ModelWindowVM.java

 


package com.demo.UI;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;
import java.util.HashMap;
import org.zkoss.bind.BindUtils;
import java.util.Map;

public class ModelWindowVM {

@Wire("#modalDialog")
private Window win;
private String value1;
private String value2;


public String getValue1() {
return value1;
}

public void setValue1(String value1) {
this.value1 = value1;
}

public String getValue2() {
return value2;
}

public void setValue2(String value2) {
this.value2 = value2;
}

@Init
public void init(@ContextParam(ContextType.VIEW) Component view,
@ExecutionArgParam("value1") String v1,
@ExecutionArgParam("value2") String v2) {
Selectors.wireComponents(view, this, false);
this.value1 = v1;
this.value2 = v2;

}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Command
public void save() {
Map args = new HashMap();
args.put("returnvalue1", this.value1);
args.put("returnvalue2", this.value2);
BindUtils.postGlobalCommand(null, null, "refreshvalues", args);
win.detach();
}

@Command
public void closeThis() {
win.detach();
}
}


Now you can run the demo.zul file

Wednesday, 20 June 2012

C++ Workbook

In the year 1999-2002, I was working as faculty in a computer center. During that period, I developed a small VB Application to explain the concept of C++. Here are the some screenshots from that VB Project
Home Page
image
Chapters List
image
Sample Program
image

image

image

Monday, 18 June 2012

ZK Border Layout–Menu on the left and content on the right.

In this post, let us see how we can load all our project menu in the left and on click of each of menu, then we will load individual zul file on the right side. Here, we are going to use border layout, where north border going to content menu and west border will be used to load the individual zul file.

Left hugMany thanks Stephan who helped me to complete this task. Here is the forum thread
http://www.zkoss.org/forum/listComment/18840-Menu-Links-on-Left-Side-and-Page-Content-on-Right-Side?lang=en

ZK Version : ZK 6

Project Name :BorderLayout

Project Structure;

image

Demo.zul

<zk>

<window border="none" width="100%" height="100%" id="main">
<separator />
<div left="20px" height="25px">
<image src="/images/zklogo_s.png"
style="padding-left: 10px;" height="30px" width="auto" />
</div>
<separator />
<borderlayout id="mainlayout">
<north border="normal" height="70px">
<div id="Menu" sclass="Mainmenudiv">
<include src="mainmenu.zul" />
</div>
</north>
<west title=" " size="20%" flex="true" splittable="true"
collapsible="true">
<div id="nav" style="background:white; height:100%">
<include src="navmenu.zul" />
</div>
</west>
<center border="normal">
<div height="99%" id="Screen"></div>
</center>
<south height="70px">
<div align="center">
<label
value="Copyright © 2012 xxxxxxxx All Rights Reserved. Powered By xxxxx"
sclass="loginlabel" />
</div>
</south>
</borderlayout>
</window>
</zk>


democomposer.java


package mydomain;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.MouseEvent;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Center;
import org.zkoss.zul.Messagebox;

public class democomposer extends GenericForwardComposer {

public void onClickMenu(MouseEvent event) {

String zulFilePathName;
Borderlayout bl = (Borderlayout) Path.getComponent("/main/mainlayout");
/* get an instance of the searched CENTER layout area */
Center center = bl.getCenter();

/* clear the center child comps */
center.getChildren().clear();

//Messagebox.show("inside" + event.getTarget().getId());
zulFilePathName = event.getTarget().getId() + ".zul";
/* create the page and put it in the center layout area */
Executions.createComponents(zulFilePathName, center, null);
}
}


zk.zul



<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK Contents goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>

 


blog.zul



<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK blog Contents goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


Contributors.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK Contributors Contents goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


forum.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK forum goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


mainmenu.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="Welcome to ZK" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


navmenu.zul


<?page title="Method3"?>

<zk>
<style>
.z-groupbox-3d-hm { background-image: none; background-color:
#0A246A !important; } .z-groupbox-3d-header .z-caption { color:
yellow; font-weight: bold;; } .z-groupbox-3d-cnt{
background-image: none; background-color: #EAECF0 !important; }

.open-true {float:right; background:url('img/UP201.png')
no-repeat right 0; height: 16px; padding-right: 20px;
font-weight: bold; }

.open-false {float:right; background:url('img/Down201.png')
no-repeat right 0; height: 16px; padding-right: 20px;
font-weight: bold; }

</style>
<window title="Administration" border="normal" width="270px" apply="mydomain.democomposer">
<groupbox width="250px" mold="3d">
<attribute name="onOpen"><![CDATA[
arrow1.setSclass("open-" + self.isOpen());
]]></attribute>
<caption label="Products">
<div id="arrow1" class="open-true"></div>
</caption>

<vbox>
<toolbarbutton id="ZK" label="ZK" forward="onClick=onClickMenu()"/>
<toolbarbutton id="ZkStudio" label="ZK Studio" forward="onClick=onClickMenu()"/>
<toolbarbutton id="ZkSpring" label="ZK Spring" forward="onClick=onClickMenu()"/>
<toolbarbutton id="ZKSpread" label="ZK SpreadSheet" forward="onClick=onClickMenu()"/>
</vbox>
</groupbox>
<groupbox width="250px" mold="3d">
<attribute name="onOpen"><![CDATA[
arrow2.setSclass("open-" + self.isOpen());
]]></attribute>
<caption label="ZK Demo">
<div id="arrow2" class="open-true"></div>
</caption>

<vbox>
<toolbarbutton id="ZKCalender"
label="ZK Calender" forward="onClick=onClickMenu()"/>
<toolbarbutton id="ZKWebmail" label="ZK Web Mail" forward="onClick=onClickMenu()"/>
<toolbarbutton id="ZKSandbox"
label="ZK Sand Box" forward="onClick=onClickMenu()"/>
<toolbarbutton id="ZKPiv" label="ZK Pivottable" forward="onClick=onClickMenu()"/>
</vbox>
</groupbox>
<groupbox width="250px" mold="3d">
<attribute name="onOpen"><![CDATA[
arrow.setSclass("open-" + self.isOpen());
]]></attribute>
<caption label="Community">
<div id="arrow" class="open-true"></div>
</caption>

<vbox>
<toolbarbutton id="forum"
label="forum" forward="onClick=onClickMenu()"/>
<toolbarbutton id="blog"
label="Blog" forward="onClick=onClickMenu()"/>
<toolbarbutton id="Contributors"
label="ZK Contributors" forward="onClick=onClickMenu()"/>
</vbox>
</groupbox>

</window>
</zk>


ZKCalender.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK calender contents goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


ZKPiv.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK pivottable contents goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


ZKSandbox.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK sandbox goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


ZKSpread.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK spreadsheet content goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


ZkSpring.zul


<?page title="Main Menu" contentType="text/html;charset=UTF-8"?>
<zk>
<label value="ZK spring content goes here" style="color: #696569; font-size : 34px; font-weight: bold;" />
</zk>


Now run the demo.zul and Click on the links on the left menu


image


Click here to see the demo


Download the source as war file

Sunday, 17 June 2012

MVVM–List Item–Hibernate–MySQL–Part 2

In the part 1, we have seen, how to list the records from DB using ZK List box. Now let us go further and see how we can add new record and edit exiting record and update into DB.
In this post, we will see how we can do the following stuffs
1. Add new person by calling a model window in MVVM and update in the DB
2. On Double click of the records in the list item, edit the existing record by calling a modal window in MVVM and update in the DB
3. Then after edit and add, we will refresh the list. Note, after edit, we no need to do anything, because data binding will take care. But after adding new person, we will refresh the list using Global command

Left hug     Many thanks to potix Jimmy who helped me to complete this part 2. http://www.zkoss.org/forum/listComment/19829-ZK-MVVM-Modal-Window

ZK Version : ZK 6

Project Name :ListItemMVVMHibernate

Project Structure;

image

Demo.zul



<?page title="Listitem MVVM Demo with Hibernate" contentType="text/html;charset=UTF-8"?>
<zk>
<style>
.z-listcell.red .z-listcell-cnt, .z-label.red{ color:red; }
</style>
<window title="Listitem MVVM Demo with Hibernate" border="normal"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('myvm') @init('domainVMS.PersonVM')">
<div>
<button label="Add Person"
onClick="@command('addNewPerson')" />
</div>
<separator />

<listbox id="test" model="@load(myvm.allPersons)"
selectedItem="@bind(myvm.curSelectedPerson)">
<listhead sizable="true">
<listheader label="First Name" width="400px"
sort="auto(firstName)" />
<listheader label="Last Name" width="285px"
sort="auto(lastName)" />
<listheader label="email" width="285px"
sort="auto(email)" />
</listhead>
<template name="model" var="p1">
<listitem onDoubleClick="@command('onDoubleClicked')">
<listcell label="@load(p1.firstName)"
sclass="@load(empty p1.email ?'red':'')" />
<listcell label="@load(p1.lastName)"
sclass="@load(empty p1.email ?'red':'')" />
<listcell label="@load(p1.email)" />
</listitem>
</template>
</listbox>
</window>
</zk>



AddPerson.zul

<zk>
<window id="win" title=" " width="520px" height="220px"
border="normal" minimizable="false" mode="modal" maximizable="false"
closable="true" action="show: slideDown;hide: slideUp"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('domainVMS.PersonCRUDVM')" >
<separator />
<label value="Person information" />
<separator />
<panel width="100%">
<panelchildren>
<separator />
<grid width="99.5%">
<columns>
<column label="" width="150px" />
<column label="" />
</columns>
<rows>
<row>
<hbox>
<label value="First Name" />
<label value="*" />
</hbox>
<textbox name="firstName"
value="@bind(vm.selectedPerson.firstName)" cols="50" />
</row>
<row>
<hbox>
<label value="Last Name" />
<label value="*" />
</hbox>
<textbox name="firstName"
value="@bind(vm.selectedPerson.lastName)" cols="50" />
</row>
<row>
<hbox>
<label value="email" />
<label value="*" />
</hbox>
<textbox name="email"
value="@bind(vm.selectedPerson.email)" cols="50" />
</row>
</rows>
</grid>
</panelchildren>
</panel>
<separator />
<div align="center">
<button label="Submit" onClick="@command('save')" />
<button label="Cancel" onClick="@command('closeThis')" />
</div>
</window>
</zk>



PersonDAO.java

package domainDAO;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Query;
import org.zkoss.zul.Messagebox;

import HibernateUtilities.HibernateUtil;
import mydomain.Person;

public class PersonDAO {

@SuppressWarnings("unchecked")
public List<Person> getAllPersons() {
List<Person> allrecords = null;
try {
Session session = HibernateUtil.beginTransaction();
Query q1 = session.createQuery("from Person");
allrecords = q1.list();
HibernateUtil.CommitTransaction();
} catch (RuntimeException e) {
e.printStackTrace();
}
return allrecords;
}

public void saveOrUpdate(Person p1) {

try {
Session session = HibernateUtil.beginTransaction();
session.saveOrUpdate(p1);
HibernateUtil.CommitTransaction();
} catch (RuntimeException e) {
e.printStackTrace();
}

}
}


PersonCRUDVM.java


package domainVMS;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Messagebox;
import org.zkoss.zk.ui.Component;
import domainDAO.PersonDAO;
import mydomain.Person;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;

public class PersonCRUDVM {

@Wire("#win")
private Window win;

private Person selectedPerson;

public Person getSelectedPerson() {
return selectedPerson;
}

public void setSelectedPerson(Person selectedPerson) {
this.selectedPerson = selectedPerson;
}

@Init
public void initSetup(@ContextParam(ContextType.VIEW) Component view, @ExecutionArgParam("selectedPerson") Person selectedPerson) {
Selectors.wireComponents(view, this, false);
if (selectedPerson == null)
this.selectedPerson = new Person();
else
this.selectedPerson = selectedPerson;

}

@Command
public void save() {
new PersonDAO().saveOrUpdate(this.selectedPerson);
Map args = new HashMap();
args.put("newadded", this.selectedPerson);
BindUtils.postGlobalCommand(null, null, "refreshList", args);
win.detach();
}

@Command
public void closeThis() {
win.detach();
}
}


PersonVM.java


package domainVMS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import mydomain.Person;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Messagebox;
import domainDAO.PersonDAO;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;

public class PersonVM {

private List<Person> persons = new ArrayList<Person>();
private Person curSelectedPerson;

public Person getCurSelectedPerson() {
return curSelectedPerson;
}

public void setCurSelectedPerson(Person curSelectedPerson) {
this.curSelectedPerson = curSelectedPerson;
}

public List<Person> getallPersons() {
return persons;
}

@Init
public void initSetup() {
persons = new PersonDAO().getAllPersons();

}

@Command
public void onDoubleClicked() {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("selectedPerson", curSelectedPerson);
Executions.createComponents("AddPerson.zul", null, map);
}

@Command
public void addNewPerson() {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("selectedPerson", null);
Executions.createComponents("AddPerson.zul", null, map);
}

//note this will be executed after when we new person from the model window
@GlobalCommand
@NotifyChange("allPersons")
public void refreshList(@BindingParam("newadded") Person p1)
{
persons.add(p1);
}

}




HibernateUtil.java

package HibernateUtilities;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

private static SessionFactory factory;
private static ServiceRegistry serviceRegistry;

public static Configuration getInitConfiguration() {
Configuration config = new Configuration();
config.configure();
return config;
}

public static Session getSession() {
if (factory == null) {
Configuration config = HibernateUtil.getInitConfiguration();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
factory = config.buildSessionFactory(serviceRegistry);
}
Session hibernateSession = factory.getCurrentSession();
return hibernateSession;
}

public static Session beginTransaction() {
Session hibernateSession;
hibernateSession = HibernateUtil.getSession();
hibernateSession.beginTransaction();
return hibernateSession;
}

public static void CommitTransaction() {
HibernateUtil.getSession().getTransaction().commit();
}

public static void closeSession() {
HibernateUtil.getSession().close();
}

public static void rollbackTransaction() {
HibernateUtil.getSession().getTransaction().rollback();
}

}



Now you can run the demo.zul. And click Add Person to add new person or double click any one of the list items to edit.

image

Download the source as war file

continued on next post - Part 3