The Final Grid"

From Documentation
m
m
Line 1: Line 1:
 +
{{ZKEssentialsPageHeader}}
 +
 
==The Final Grid==
 
==The Final Grid==
===ZK Scoped Singleton===
+
[[Image:product_view.png]]<br>
In the '''initOperation''' method, <javadoc>org.zkoss.zul.Spinner</javadoc> and <javadoc>org.zkoss.zul.Button</javadoc> are created programmatically to allow end users to select and submit the quantity of the items to be purchased; the <javadoc>org.zkoss.zul.Cell</javadoc> is set as the parent component for these components. At line 14, we manually add an event listener to the button so we could verify whether the quantity from all orders combined does not exceed the quantity in stock.  
+
 
 +
In the previous section, we saw how the renderer assigns UI components (for example, <javadoc>org.zkoss.zul.Image</javadoc> and <javadoc>org.zkoss.zul.Label</javadoc>) into rows of a Grid for UI presentation. <br>
 +
'''ProductViewCtrl.java - Rendering Rows'''
 +
<source lang="java">
 +
public void doAfterCompose(Component comp) throws Exception {
 +
...
 +
prodGrid.setRowRenderer(new RowRenderer() {
 +
public void render(Row row, Object data) throws Exception {
 +
final Product prod = (Product)data;
 +
 +
Image img = new Image(prod.getImgPath());
 +
img.setWidth("70px");
 +
img.setHeight("70px");
 +
img.setParent(row);
 +
new Label(prod.getName()).setParent(row);
 +
new Label(""+prod.getPrice()).setParent(row);
 +
new Label(""+prod.getQuantity()).setParent(row);
 +
new Label(YYYY_MM_DD_hh_ss.format(prod.getCreateDate())).setParent(row);
 +
initOperation(prod).setParent(row);
 +
}
 +
...
 +
 +
});
 +
}
 +
</source>
 +
The last column contains a <javadoc>org.zkoss.zul.Cell</javadoc> component (returned by the method '''initOperation''') which allows end users to ADD items to be purchased. In this section, we'll look into the implementation of this functionality which fulfills the requirements for the Product View Grid.<br>
 +
'''ProductViewCtrl.java - Implementation for "Adding" Items to Purchase'''
 
<source lang="java">
 
<source lang="java">
 
         private Cell initOperation(final Product prod){
 
         private Cell initOperation(final Product prod){
Line 30: Line 58:
 
}
 
}
 
</source>
 
</source>
 +
====The Cell Component====
 +
In the '''initOperation''' method, <javadoc>org.zkoss.zul.Spinner</javadoc> and <javadoc>org.zkoss.zul.Button</javadoc> are created programmatically to allow end users to select and submit the quantity of the items to be purchased; the <javadoc>org.zkoss.zul.Cell</javadoc> is set as the parent component for these components, serving as a container.
 +
====Setting Constraints for an Input UI Component====
 +
All ZK components implementing the <javadoc>org.zkoss.zul.impl.api.InputElement</javadoc> interface can call the <javadoc method="setConstraint(java.lang.String constr)">org.zkoss.zul.impl.api.InputElement</javadoc> method to set an upper and lower bound on the allowable input value. For instance, value selected for the <javadoc>org.zkoss.zul.Spinner</javadoc> component is only meaningful if it's between 1 and the quantity remained in stock for each product; hence, we could cal the '''setConstraint''' to impose the allowable range:
 +
<source lang="java">
 +
spinner.setConstraint("min 1 max "+prod.getQuantity());
 +
</source>
 +
This convenient method offers us an easy way to impose a simple validation mechanism for the input components such as <javadoc>org.zkoss.zul.Textbox</javadoc>, <javadoc>org.zkoss.zul.Intbox</javadoc>, <javadoc>org.zkoss.zul.Datebox</javadoc>, and <javadoc>org.zkoss.zul.Combobox</javadoc>.
 +
 +
====Registering an Event Listener====
 +
When users click the "add" button, we need to verify whether the quantity from all orders combined does not exceed the quantity in stock for each product. Since we are creating the button here programmatically, we'll need to add an event listener manually, as oppose to making button declaration in ZUL and rely on the <javadoc>org.zkoss.zk.ui.util.GenericForwardComposer</javadoc> facilitate this process.
 +
<source lang="java">
 +
        private Cell initOperation(final Product prod){
 +
...
 +
        Button button = new Button("add");
 +
 +
        button.addEventListener(ON_CLICK, new EventListener() {
 +
    public void onEvent(Event event) throws Exception {
 +
    //event handling code here...
 +
});
 +
return cell;
 +
}
 +
</source>
 +
====Utilizing Session Scope Attributes====
 +
 +
 +
<source lang="java">
 +
        private Cell initOperation(final Product prod){
 +
        ...
 +
        Button button = new Button("add");
 +
        ...
 +
        final Label errorLb = new Label();
 +
        ...
 +
 +
        button.addEventListener(ON_CLICK, new EventListener() {
 +
    public void onEvent(Event event) throws Exception {
 +
    ShoppingCartCtrl ctrl = ShoppingCartViewCtrl.getInstance(desktop);
 +
    try{
 +
ctrl.addItem(prod, spinner.getValue());
 +
errorLb.setValue("");
 +
    }catch(WrongValueException e){
 +
errorLb.setValue(e.getMessage());
 +
    }
 +
    }
 +
});
 +
return cell;
 +
}
 +
</source>
 +
 +
  
===The Final Grid===
 
  
[[Image:product_view.png]]
+
{{ZKEssentialsPageFooter}}

Revision as of 08:21, 18 October 2010

Stop.png This article is out of date, please refer to http://books.zkoss.org/zkessentials-book/master/ for more up to date information.


The Final Grid

File:Product view.png

In the previous section, we saw how the renderer assigns UI components (for example, Image and Label) into rows of a Grid for UI presentation.
ProductViewCtrl.java - Rendering Rows

public void doAfterCompose(Component comp) throws Exception {
		...
		prodGrid.setRowRenderer(new RowRenderer() {
			public void render(Row row, Object data) throws Exception {
				final Product prod = (Product)data;
				
				Image img = new Image(prod.getImgPath());
				img.setWidth("70px");
				img.setHeight("70px");
				img.setParent(row);
				new Label(prod.getName()).setParent(row);
				new Label(""+prod.getPrice()).setParent(row);
				new Label(""+prod.getQuantity()).setParent(row);
				new Label(YYYY_MM_DD_hh_ss.format(prod.getCreateDate())).setParent(row);
				initOperation(prod).setParent(row);
			}
			...
			
		});
	}

The last column contains a Cell component (returned by the method initOperation) which allows end users to ADD items to be purchased. In this section, we'll look into the implementation of this functionality which fulfills the requirements for the Product View Grid.
ProductViewCtrl.java - Implementation for "Adding" Items to Purchase

        private Cell initOperation(final Product prod){
		         Cell cell = new Cell();
		         final Spinner spinner = new Spinner(1);
		         spinner.setConstraint("min 1 max "+prod.getQuantity());
		         spinner.setParent(cell);
				
		         Button button = new Button("add");
		         button.setImage("/image/ShoppingCart-16x16.png");
		         button.setParent(cell);
				
		         final Label errorLb = new Label();
		         errorLb.setParent(cell);
				
		         button.addEventListener(ON_CLICK, new EventListener() {
			     public void onEvent(Event event) throws Exception {
			     ShoppingCartCtrl ctrl = ShoppingCartViewCtrl.getInstance(desktop);
			     try{
				 ctrl.addItem(prod, spinner.getValue());	
				 errorLb.setValue("");
			     }catch(WrongValueException e){
				 errorLb.setValue(e.getMessage());
			     }
			     }
			});
			return cell;
			}

The Cell Component

In the initOperation method, Spinner and Button are created programmatically to allow end users to select and submit the quantity of the items to be purchased; the Cell is set as the parent component for these components, serving as a container.

Setting Constraints for an Input UI Component

All ZK components implementing the InputElement interface can call the InputElement.setConstraint(String constr) method to set an upper and lower bound on the allowable input value. For instance, value selected for the Spinner component is only meaningful if it's between 1 and the quantity remained in stock for each product; hence, we could cal the setConstraint to impose the allowable range:

spinner.setConstraint("min 1 max "+prod.getQuantity());

This convenient method offers us an easy way to impose a simple validation mechanism for the input components such as Textbox, Intbox, Datebox, and Combobox.

Registering an Event Listener

When users click the "add" button, we need to verify whether the quantity from all orders combined does not exceed the quantity in stock for each product. Since we are creating the button here programmatically, we'll need to add an event listener manually, as oppose to making button declaration in ZUL and rely on the GenericForwardComposer facilitate this process.

        private Cell initOperation(final Product prod){
			...
		         Button button = new Button("add");
				
		         button.addEventListener(ON_CLICK, new EventListener() {
			     public void onEvent(Event event) throws Exception {
			     //event handling code here...
			});
			return cell;
			}

Utilizing Session Scope Attributes

        private Cell initOperation(final Product prod){
		         ...
		         Button button = new Button("add");
		         ...	
		         final Label errorLb = new Label();
		         ...
				
		         button.addEventListener(ON_CLICK, new EventListener() {
			     public void onEvent(Event event) throws Exception {
			     ShoppingCartCtrl ctrl = ShoppingCartViewCtrl.getInstance(desktop);
			     try{
				 ctrl.addItem(prod, spinner.getValue());	
				 errorLb.setValue("");
			     }catch(WrongValueException e){
				 errorLb.setValue(e.getMessage());
			     }
			     }
			});
			return cell;
			}




Last Update : 2010/10/18

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.