0

Can't capture events triggered from composite inside controller

asked 2012-06-08 03:10:03 +0800

robertobado gravatar image robertobado
6

Hi guys. I am working with ZK Framework for a couple weeks now and it seems great. I am currently trying to convert the examples in ZK Essentials to fit my needs as I am in early stages and need a few CRUD screens. The problem here relates to the ZK Essentials/Displaying Information Using Grid, MVC and composite components/Composite Components in the MVC chapter.

I simply can't capture events posted programatically from a composite inside my controller. Here are a few excerpts from my code:

editComposite.zul

<zk>
	<button id="compositeButtonEdit"
		image="resources/icon/pencilpaper.png" />
</zk>

EditListcell.java

public class EditListcell extends Listcell implements IdSpace, AfterCompose {

	...

	@Override
	public void afterCompose() {
		Executions.createComponents("/WEB-INF/composite/editComposite.zul",
				this, null);

		Selectors.wireVariables(this, this, null);
		Selectors.wireComponents(this, this, false);
		Selectors.wireEventListeners(this, this);
	}

	@Listen("onClick=#compositeButtonEdit")
	public void compositeButtonEdit() {
		System.out.println(">>>compositeButtonEdit click event fired");
		Events.postEvent(this, new ItemEditEvent());
	}
	
	// Customize Event
	public static final String ON_ITEM_EDIT = "onItemEdit";
	
	public class ItemEditEvent extends Event {

		private static final long serialVersionUID = -462313777727415236L;

		public ItemEditEvent() {
			super(ON_ITEM_EDIT, EditListcell.this);
		}
	}

}

EditListcell.java

public class EditListcell extends Listcell implements IdSpace, AfterCompose {

	...

	@Override
	public void afterCompose() {
		Executions.createComponents("/WEB-INF/composite/editComposite.zul",
				this, null);

		Selectors.wireVariables(this, this, null);
		Selectors.wireComponents(this, this, false);
		Selectors.wireEventListeners(this, this);
	}

	@Listen("onClick=#compositeButtonEdit")
	public void compositeButtonEdit() {
		System.out.println(">>>compositeButtonEdit click event fired");
		Events.postEvent(this, new ItemEditEvent());
	}
	
	// Customize Event
	public static final String ON_ITEM_EDIT = "onItemEdit";
	
	public class ItemEditEvent extends Event {

		private static final long serialVersionUID = -462313777727415236L;

		public ItemEditEvent() {
			super(ON_ITEM_EDIT, EditListcell.this);
		}
	}

}

product.zul

<div apply="org.zkoss.bind.BindComposer"
	viewModel="@id('vm') @init('net.openrally.restaurant.webui.viewcontroller.ProductViewController')">
	<tabbox id="contentTabBoxProduct"
		apply="net.openrally.restaurant.webui.controller.ProductController">
		<tabs id="contentTabsProducts">
			<tab id="contentTabList" label="List" />
			<tab id="contentTabNew" label="New" />
			<tab id="contentTabEdit" label="Edit" visible="false" />
		</tabs>
		<tabpanels id="contentTabPanelsProduct">
			<tabpanel id="contentTabPanelList">
				<listbox id="contentListBoxProduct"
					model="@load(vm.productList)"
					selectedItem="@bind(vm.selectedItem)">
					<listhead sizable="true">
						<listheader label="Id" />
						<listheader label="Name" />
						<listheader label="Description" />
						<listheader label="Price" />
						<listheader align="center" />
					</listhead>
					<template name="model" var="product">
						<listitem>
							<listcell label="@load(product.productId)" />
							<listcell label="@load(product.name)" />
							<listcell
								label="@load(product.description)" />
							<listcell label="@load(product.price)" />
							<editCell editableEntity="${product}" />
							<listcell>
								<button image="resources/icon/trash.png"
									onClick="@command('deleteProduct', product=product)" />
							</listcell>
						</listitem>
					</template>
				</listbox>
			</tabpanel>
			...
		</tabpanels>
	</tabbox>
</div>

ProductController.java

public class ProductController extends SelectorComposer<Tabbox> {

	private static final long serialVersionUID = -7186783182440309573L;

	...
	
	@Listen("onClick")
	public void onClickV1(Event event) {
		System.out.println(">>>onClickV1 fired");
	}
	
	@Listen("onItemEdit=button")
	public void onItemEditV1(Event event) {
		System.out.println(">>>onItemEditV1 fired");
	}
	
	@Listen("onItemEdit=tabbox#contentTabBoxProduct")
	public void onItemEditV2(Event fe) {
		System.out.println(">>>onItemEditV2 fired");
	}
	
	@Listen("onItemEdit=tabbox#contentTabBoxProduct #contentTabPanelsProduct #contentTabPanelList #contentListBoxProduct listitem editCell")
	public void onItemEditV3(Event fe) {
		System.out.println(">>>onItemEditV3 fired");
	}
	
	@Listen("onItemEdit=tabbox#contentTabBoxProduct tabpanels tabpanel listbox listitem editCell")
	public void onItemEditV4(Event fe) {
		System.out.println(">>>onItemEditV4 fired");
	}
	
	...

}

ProductViewController.java

public class ProductViewController {

	private Product selectedItem;

	public Product getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(Product selectedItem) {
		this.selectedItem = selectedItem;
	}

	public List<Product> getProductList() {
		return generateProducts();
	}

	...
	
	@GlobalCommand
	@NotifyChange("productList")
	public void updateProductList() {
	}

}

As you can see, I tried binding listeners in my controller using several flavors of Listen annotations. None of them worked. On the other hand, the click event from EditListcell is fired as I can see it's message in my console. I think I am missing something simple here, any or suggestion is appreciated.

If there is any missing code that you would like to see, let me know and I will post it here, or feel free to take a look at my github repository

Thank you all in advance.

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2012-06-09 22:50:05 +0800

robertobado gravatar image robertobado
6

Hi all. After researching some more time and still not finding a solution, I decided to cease my development with ZK Framework.

It is certainly a good framework, but not being able to overcome a problem like this in early development stages was a blocker issue.

I wish success and all the best for ZK developers and maintainers.

link publish delete flag offensive edit

answered 2013-01-25 16:20:24 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

You can use @Command in your java code as well in your ZUL Page to fire a Event. As you are using MVVM Architecture. See Java Code method

@Command
    public void valueChangedListner(
            @BindingParam("taskListObject") TaskListData changedObject) {
        changedModel.add(changedObject);
    }

and in my ZUL i called this method like this.

<intbox value="@bind(taskListData.processPriority)" inplace="true" onChange="@command('valueChangedListner',taskListObject = taskListData)">
</intbox>

Here i am passing object in onChange and this object will be get into java Method by

@BindingParam("taskListObject") TaskListData changedObject
link publish delete flag offensive edit

answered 2013-01-28 09:48:14 +0800

takach gravatar image takach
30 2

I found a way to forward events to the composite component itself, and handle this events in the controller. However, I submitted a request at http://tracker.zkoss.org/browse/ZK-1598.

My component's ZUL mySelect.zul:

 <?xml version="1.0" encoding="UTF-8"?>
    <zk>
        <label id="lb" value="asdf" />
        <combobox id="cb">
                <comboitem label="a" />
                <comboitem label="b" />
           </combobox>
    </zk>

My component's Java file: public class MySelect extends Hbox implements IdSpace {

public MySelect() {
    // 1. Render the template
    Executions.createComponents("/mySelect.zul", this, null);
    // 2. Wire variables, components and event listeners (optional)
    Selectors.wireVariables(this, this, null);
    Selectors.wireComponents(this, this, false);
    Selectors.wireEventListeners(this, this);
}

@Listen("onSelect = #cb")
public void onCh(Event evt){
    System.out.println("MySelect.onCH() - " + this.getId());

    // Always null, why?
    System.out.println(
        this.getPage().getDesktop().getComponentByUuidIfAny("this.getId()"));

    // Workaround
    Component target = null;
    for (Component c : this.getPage().getDesktop().getComponents()){
        if (c.getId().equals(this.getId())){
            System.out.println("forward");
            target = c;
        }
    }
    if (target != null)
        Events.sendEvent(target, evt);
}

}

My ZUL page that where I use the component:

<?component name="mySelect" extends="hbox" class="composite.MySelect" ?>
<?page id="wfPage" ?>
<window title="TestData window" border="normal" width="300px" apply="controller.TestController"
    id="wfWindow">
    <mySelect id="wfS" />
</window>

The controller for the page:

public class TestController extends GenericForwardComposer<Component> {

    public void onSelect$wfS(Event evt){
        alert("TestController.onSelect$wfS() - " + evt.getName() + " - " + evt.getData());
    }

}
link publish delete flag offensive edit

answered 2013-01-28 13:11:34 +0800

gganassin gravatar image gganassin flag of Luxembourg
540 6
http://www.hybris.com/

updated 2013-01-28 13:12:30 +0800

@Override
public void afterCompose() {
    super.doAfterCompose();
    ....
}

isn't it?

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-06-08 03:10:03 +0800

Seen: 232 times

Last updated: Jan 28 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More