|
From: <sim...@us...> - 2007-05-21 22:45:14
|
Revision: 303
http://svn.sourceforge.net/zkforge/?rev=303&view=rev
Author: simon_massey
Date: 2007-05-21 15:45:14 -0700 (Mon, 21 May 2007)
Log Message:
-----------
refactored to use a databound macro component for the restaurant name and type.
Modified Paths:
--------------
trunk/foodToGo/src/main/webapp/zul/index.zul
Added Paths:
-----------
trunk/foodToGo/src/main/webapp/zul/load-dummy-data.zs
trunk/foodToGo/src/main/webapp/zul/restaurant-macro.zs
trunk/foodToGo/src/main/webapp/zul/restaurant.zul
Modified: trunk/foodToGo/src/main/webapp/zul/index.zul
===================================================================
--- trunk/foodToGo/src/main/webapp/zul/index.zul 2007-05-21 11:47:32 UTC (rev 302)
+++ trunk/foodToGo/src/main/webapp/zul/index.zul 2007-05-21 22:45:14 UTC (rev 303)
@@ -18,65 +18,34 @@
-->
<?page id="main-page"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
+<?init zscript="restaurant-macro.zs" ?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
-<?component name="menu-item" macro-uri="menu-item.zul"?>
+<?component name="restaurant" macro-uri="restaurant.zul" class="RestaurantMacro"?>
<zk xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:a="http://www.zkoss.org/2005/zk/annotation">
<window title="Food To Go :: Restaurant Manager" border="normal" id="main-window">
<zscript>
// load a list of sparcely populated restaurants
restaurants = restaurantFacadeSpringBean.listRestaurants().getRestaurants();
-
- /*
- If the database was empty create some sample data
- */
- if( 0 == restaurants.size() ) {
- menuList = new java.util.ArrayList();
- dummyMenuItem = new net.chrisrichardson.foodToGo.domain.MenuItem();
- dummyMenuItem.setName("Cannelloni");
- dummyMenuItem.setPrice(9.99d);
- menuList.add(dummyMenuItem);
- dummyMenuItem = new net.chrisrichardson.foodToGo.domain.MenuItem();
- dummyMenuItem.setName("Tagliata");
- dummyMenuItem.setPrice(17.00d);
- menuList.add(dummyMenuItem);
- dummyRestaurant = new net.chrisrichardson.foodToGo.domain.Restaurant();
- dummyRestaurant.setName("Italian To Go");
- dummyRestaurant.setType("Italian");
- dummyRestaurant.setMenuItems(menuList);
-
- restaurantFacadeSpringBean.saveRestaurant(dummyRestaurant);
-
- menuList = new java.util.ArrayList();
- dummyMenuItem = new net.chrisrichardson.foodToGo.domain.MenuItem();
- dummyMenuItem.setName("Tod Man Pla");
- dummyMenuItem.setPrice(8.50d);
- menuList.add(dummyMenuItem);
- dummyMenuItem = new net.chrisrichardson.foodToGo.domain.MenuItem();
- dummyMenuItem.setName("Pad ped mu");
- dummyMenuItem.setPrice(18.00d);
- menuList.add(dummyMenuItem);
- dummyRestaurant = new net.chrisrichardson.foodToGo.domain.Restaurant();
- dummyRestaurant.setName("Thai To Go");
- dummyRestaurant.setType("Thai");
- dummyRestaurant.setMenuItems(menuList);
-
- restaurantFacadeSpringBean.saveRestaurant(dummyRestaurant);
-
- restaurants = restaurantFacadeSpringBean.listRestaurants().getRestaurants();
- }
</zscript>
+ <zscript src="load-dummy-data.zs"/>
<hbox>
<vbox width="200px">
<groupbox width="300px">
<caption label="Restaurants In The Database" />
- <a:bind model="restaurants" selectedItem="selected" />
+ <!-- Bing a Listbox to the restaurants collection -->
+ <a:bind model="restaurants" selectedItem="restaurantListboxSelectedPojo" />
<listbox rows="10" id="restaurantsListBox">
+ <a:bind _var="restaurant" />
+ <listitem>
+ <a:bind label="restaurant.name" />
+ <listcell />
+ </listitem>
<attribute name="onSelect"><![CDATA[
- // selected.id calls getId() on the selected Restaurant POJO so now load it from the database
- restaurant = restaurantFacadeSpringBean.loadRestaurant(selected.id).getRestaurant();
-
- // cache the loaded POJO on the save button
+ // selected.id calls getId() on the sparcely loaded Restaurant POJO loaded with getRestaurants()
+ // now we want to load the full restaurant with all of it's menu items from the database
+ restaurant = restaurantFacadeSpringBean.loadRestaurant(""+restaurantListboxSelectedPojo.id).getRestaurant();
+ // cache the loaded POJO on an attribute the save button
saveRestaurantButton.setAttribute("restaurant", restaurant, Component.DESKTOP_SCOPE);
// force the included menu item frame to be rerendered. the timestamp means that
@@ -85,15 +54,11 @@
now = (new java.util.Date()).getTime();
menuItemInclude.setSrc("./menu-items.zul?now="+now);
]]></attribute>
- <a:bind _var="restaurant" />
- <listitem>
- <a:bind label="restaurant.name" />
- <listcell />
- </listitem>
</listbox>
</groupbox>
<button id="reload" label="Reload">
<attribute name="onClick"><![CDATA[
+ // refresh the data model of the Listbox with data re-loaded from the database
model = restaurantsListBox.model;
model.clear();
restaurants = restaurantFacadeSpringBean.listRestaurants().getRestaurants();
@@ -105,38 +70,11 @@
<vbox>
<groupbox width="500px">
<caption label="Restaurant Details" />
- <grid>
- <rows>
- <row>
- <label id="RestaurantNameLabel" value="Restaurant Name:" />
- <a:bind value="selected.name"/><textbox id="namebox"/>
- </row>
- <row>
- <label id="RestaurantTypeLabel" value="Restaurant Type:" />
- <a:bind selectedItem="selected.type"/>
- <listbox id="typeselect"
- mold="select" rows="1">
- <listitem
- label="--- Please Select ---" value="" />
- <listitem label="Italian"
- value="Italian">
- </listitem>
- <listitem label="Chinese"
- value="Chinese">
- </listitem>
- <listitem label="Indian"
- value="Indian">
- </listitem>
- <listitem label="Mexican"
- value="Mexican">
- </listitem>
- <listitem label="Thai"
- value="Thai">
- </listitem>
- </listbox>
- </row>
- </rows>
- </grid>
+
+ <a:bind
+ name="restaurant.name;save-when:self.onNameChange;access:both"
+ type="restaurant.type;save-when:self.onTypeChange;access:both"/>
+ <restaurant/>
</groupbox>
<include id="menuItemInclude" src=""/>
<hbox>
@@ -158,9 +96,6 @@
<attribute name="onClick"><![CDATA[
restaurant = self.getAttribute("restaurant", Component.DESKTOP_SCOPE);
if( null != restaurant ) {
- // The user may have changed the name and the type
- restaurant.name = selected.name;
- restaurant.type = selected.type;
restaurantFacadeSpringBean.saveRestaurant(restaurant);
Events.sendEvent(reload,event);
}
@@ -176,6 +111,5 @@
</hbox>
</vbox>
</hbox>
-
</window>
</zk>
\ No newline at end of file
Added: trunk/foodToGo/src/main/webapp/zul/load-dummy-data.zs
===================================================================
--- trunk/foodToGo/src/main/webapp/zul/load-dummy-data.zs (rev 0)
+++ trunk/foodToGo/src/main/webapp/zul/load-dummy-data.zs 2007-05-21 22:45:14 UTC (rev 303)
@@ -0,0 +1,38 @@
+/*
+If the database was empty create some sample data
+*/
+if( 0 == restaurants.size() ) {
+ menuList = new java.util.ArrayList();
+ dummyMenuItem = new net.chrisrichardson.foodToGo.domain.MenuItem();
+ dummyMenuItem.setName("Cannelloni");
+ dummyMenuItem.setPrice(9.99d);
+ menuList.add(dummyMenuItem);
+ dummyMenuItem = new net.chrisrichardson.foodToGo.domain.MenuItem();
+ dummyMenuItem.setName("Tagliata");
+ dummyMenuItem.setPrice(17.00d);
+ menuList.add(dummyMenuItem);
+ dummyRestaurant = new net.chrisrichardson.foodToGo.domain.Restaurant();
+ dummyRestaurant.setName("Italian To Go");
+ dummyRestaurant.setType("Italian");
+ dummyRestaurant.setMenuItems(menuList);
+
+ restaurantFacadeSpringBean.saveRestaurant(dummyRestaurant);
+
+ menuList = new java.util.ArrayList();
+ dummyMenuItem = new net.chrisrichardson.foodToGo.domain.MenuItem();
+ dummyMenuItem.setName("Tod Man Pla");
+ dummyMenuItem.setPrice(8.50d);
+ menuList.add(dummyMenuItem);
+ dummyMenuItem = new net.chrisrichardson.foodToGo.domain.MenuItem();
+ dummyMenuItem.setName("Pad Ped Mu");
+ dummyMenuItem.setPrice(18.00d);
+ menuList.add(dummyMenuItem);
+ dummyRestaurant = new net.chrisrichardson.foodToGo.domain.Restaurant();
+ dummyRestaurant.setName("Thai To Go");
+ dummyRestaurant.setType("Thai");
+ dummyRestaurant.setMenuItems(menuList);
+
+ restaurantFacadeSpringBean.saveRestaurant(dummyRestaurant);
+
+ restaurants = restaurantFacadeSpringBean.listRestaurants().getRestaurants();
+}
Added: trunk/foodToGo/src/main/webapp/zul/restaurant-macro.zs
===================================================================
--- trunk/foodToGo/src/main/webapp/zul/restaurant-macro.zs (rev 0)
+++ trunk/foodToGo/src/main/webapp/zul/restaurant-macro.zs 2007-05-21 22:45:14 UTC (rev 303)
@@ -0,0 +1,41 @@
+import org.zkoss.zk.ui.*;
+import org.zkoss.zul.*;
+public class RestaurantMacro extends HtmlMacroComponent {
+
+ public void setName(String str) {
+ getFellow("namebox").setValue(str);
+ }
+
+ public String getName() {
+ return getFellow("namebox").getValue();
+ }
+ /*
+ Given a new restaurant type from a pojo work out with
+ list box item to select.
+ */
+ public void setType(String str) {
+ if( str != null ) {
+ // find the listbox in the macro component
+ Listbox typeselect = getFellow("typeselect");
+ int j = 0; // counting along
+ for(Iterator i = typeselect.getItems().iterator(); i.hasNext();){
+ Listitem listItem = (Listitem)i.next();
+ if( listItem.getValue().equals(str) ) {
+ // found it. j is now the index of the item
+ break;
+ }
+ else {
+ // keep counting
+ j++;
+ }
+ }
+ typeselect.setSelectedIndex(j);
+ }
+ }
+
+ public String getType() {
+ alert("getType returning:"+getFellow("typeselect").getSelectedItem().getValue());
+ return getFellow("typeselect").getSelectedItem().getValue();
+ }
+
+}
\ No newline at end of file
Added: trunk/foodToGo/src/main/webapp/zul/restaurant.zul
===================================================================
--- trunk/foodToGo/src/main/webapp/zul/restaurant.zul (rev 0)
+++ trunk/foodToGo/src/main/webapp/zul/restaurant.zul 2007-05-21 22:45:14 UTC (rev 303)
@@ -0,0 +1,40 @@
+<grid>
+ <rows>
+ <row>
+ <label id="RestaurantNameLabel" value="Restaurant Name:" />
+ <textbox id="namebox">
+ <attribute name="onChange">
+ <!-- delegate event to the Macro Component -->
+ <![CDATA[
+ Events.sendEvent(spaceOwner, new Event("onNameChange", spaceOwner));
+ ]]>
+ </attribute>
+ </textbox>
+ </row>
+ <row>
+ <label id="RestaurantTypeLabel" value="Restaurant Type:" />
+ <listbox id="typeselect" mold="select" rows="1">
+ <attribute name="onSelect">
+ Events.sendEvent(spaceOwner, new Event("onTypeChange", spaceOwner));
+ </attribute>
+ <listitem
+ label="- - - Please Select - - -" value="" />
+ <listitem label="Italian"
+ value="Italian">
+ </listitem>
+ <listitem label="Chinese"
+ value="Chinese">
+ </listitem>
+ <listitem label="Indian"
+ value="Indian">
+ </listitem>
+ <listitem label="Mexican"
+ value="Mexican">
+ </listitem>
+ <listitem label="Thai"
+ value="Thai">
+ </listitem>
+ </listbox>
+ </row>
+ </rows>
+</grid>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|