0

How does button action in main frame propagates to children pages?

asked 2011-07-14 08:06:04 +0800

creata87 gravatar image creata87
197 2

updated 2011-07-14 08:24:41 +0800

ok, the easiest way to get a reply is to give an example.

consider a main.zul, two buttons and an include tag to center.zul. center.zul contains its own menu and an include tag. based on the action of the buttons, center.zul's include displays either a.zul or b.zul. main.zul has a controller, center.zul has its own controller and both a.zul and b.zul have their own controller. each zul file has a java associated controller.

the question is, how to send the onclick action from main.zul to center.zul in order to display the desired .zul file?

main.zul

<?page id="main"?>
<zk>
 <borderlayout apply="${mainController}">
  <north>
   <toolbarbutton id="tbb_a" label="a"/>	
   <toolbarbutton id="tbb_b" label="b"/>
  </north>
  <center>
   <include  id="center_contents"/>
  </center>
 </borderlayout>
</zk>

MainController.java

public class MainController {
 private Include center_contents;
 public void onClick$tbb_a(Event event) {
  // this clearly is not the solution - Include time_contents is defined in center.zul
  time_contents.setSrc("a.zul");
 }
 public void onClick$tbb_b(Event event) {
  // this clearly is not the solution - Include time_contents is defined in center.zul
  time_contents.setSrc("b.zul");
 }
 public void onUpdateCenterContent(Event event) {
  if (event == null || event.getData() == null) {
   return;
  }       
  String content = (String) event.getData();
  if (content == null) {
   return;
  }
  center_contents.setSrc(content);   
 }
 public void doAfterCompose(Component comp) {
  super.doAfterCompose(comp);
  Events.postEvent("onUpdateCenterContent", comp, "center.zul");
 }
}

center.zul

<?page id="center"?>
<zk>
 <groupbox apply="${centerController}">  
   <toolbarbutton id="tbb_1" label="home"/>	
   <toolbarbutton id="tbb_2" label="help"/>
   <panel>		
    <panelchildren>
     <include id="time_contents"/>
    </panelchildren>		
   </panel>	
 </groupbox>	
</zk>

CenterController.java

public class CenterController {
 private Include time_contents;
 public void onClick$tbb_1(Event event) {
 }
 public void onClick$tbb_2(Event event) {
 }
 public void onUpdateTimeContent(Event event) {
  if (event == null || event.getData() == null) {
   return;
  }       
  String content = (String) event.getData();
  if (content == null) {
   return;
  }
  time_contents.setSrc(content);   
 }
 public void doAfterCompose(Component comp) {
  super.doAfterCompose(comp);
  Events.postEvent("onUpdateTimeContent", comp, "a.zul");
 }
}

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2011-07-14 14:59:15 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2011-07-14 15:06:11 +0800

the question is, how to send the onclick action from main.zul to center.zul in order to display the desired .zul file?

Why not made it easy. Throw away the center.zul and load the a.zul or b.zul in the center AREA of your borderlayout.

search forum for Executions.createComponents() and GenericForwardComposer .

code snippets from running code:

public class YourMainController extends GenericForwardController {

   protected Button btn_a;  // autowired
   protected Button btn_b;  // autowired

     public void onClick$btn_a(Event event) {
       loadZulInCenterArea("/WEB-INF/pages/a.zul");
      }

     public void onClick$btn_b(Event event) {
       loadZulInCenterArea("/WEB-INF/pages/b.zul");
      }

	/**
	 * Shows the welcome page in the borderlayouts CENTER area.<br>
	 * 
	 * @throws InterruptedException
	 */
	public void loadZulInCenterArea(String zulPath) throws InterruptedException {

		// get an instance of the borderlayout defined in the zul-file
		Borderlayout bl = (Borderlayout) Path.getComponent("/outerIndexWindow/borderlayoutMain");
		// get an instance of the searched CENTER layout area
		Center center = bl.getCenter();
		// clear the center child comps
		center.getChildren().clear();

		// call the zul-file and put it in the center layout area
		Executions.createComponents(zulPath,  center,   null);
	}

best
Stephan

link publish delete flag offensive edit

answered 2011-07-15 09:50:29 +0800

creata87 gravatar image creata87
197 2

updated 2011-07-18 03:31:48 +0800

Throw away the center.zul and load the a.zul or b.zul in the center AREA of your borderlayout.

it's a little bit more complicated than that. i cannot throw away center.zul :) it actually contains a slider and 3 panels. the middle panel changes its content depending on the action of the slider or the buttons in main.zul. the left panel and the right panel modify only at the action of the slider. in this case, when it is necessary to propagate the button action through .zul files, should idSpace be used? can you please give a working example for this scenario?

by the way, i tried @Autowire on the button too, no luck

i added

<listener>
         <listener-class>org.zkoss.spring.web.context.CoreContextListener</listener-class>
</listener>

to web.xml before
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

and got this error
Could not instantiate listener org.zkoss.spring.web.context.CoreContextListener
java.lang.ClassNotFoundException: org.zkoss.spring.web.context.CoreContextListener

and after adding to the context.xml

    <context:component-scan base-package="org.zkoss.zkspringessentials.controller,org.zkoss.spring.beans.zkcomponents"></context:component-scan>
    <zksp:zk-config/>

i got the error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController' : Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.zkoss.zul.Toolbarbutton com.bayoda.client.webapp.controller.MainController.tbb_add_new; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.zkoss.zul.Toolbarbutton] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.zkoss.zul.Toolbarbutton com.bayoda.client.webapp.controller.MainController.tbb_add_new; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.zkoss.zul.Toolbarbutton] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

ps: thank you for replying :)

link publish delete flag offensive edit

answered 2011-07-18 19:43:59 +0800

samchuang gravatar image samchuang
4084 4

Hi

maybe you could try t use Event Queues

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: 2011-07-14 08:06:04 +0800

Seen: 319 times

Last updated: Jul 18 '11

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