0

How can I make a method in a major project, can be executed in another project

asked 2015-09-11 03:08:54 +0800

onsir gravatar image onsir
132 2

updated 2015-09-11 03:10:36 +0800

How can I make a method in a major project, can be executed in another project For example in the Main Project I created such as this, MainProject

public class MainClass extends GenericForwardComposer {
private Listbox listboxUser;
private Toolbarbutton tbEditRow;

@Override
public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);

    listboxUser.setModel(getListModel);

     //file class UserRenderer in other project (etc: ProjectOther1)
    listboxUser.setItemRenderer(new UserRenderer());

}

@Listen("onClick = #Edit")
public void methodInMain(){
   System.out.println("execute form other project");
}

}

and in other project, ProjectOther1.

public class UserRenderer extends GenericForwardComposer implements ListitemRenderer {

  @Wire private Toolbarbutton tbEditRow;

  @Override
  public void render(Listitem listItem, Object arg1, int arg2)throws Exception {

            User user=(User)arg1;

            Listcell userCodeCell=new Listcell();
            userCodeCell.setLabel(user.getCode());


            Listcell editRowCell = new Listcell();
            tbEditRow=new Toolbarbutton();
            tbEditRow.setLabel("Edit");
            tbEditRow.setParent(editRowCell);
            tbEditRow.addEventListener("onClick", new EventListener() {
            public void onEvent(Event event) throws Exception {

                 //I want methodInMain() in the main project be execution here
                 Events.sendEvent(Events.ON_CLICK,tbEditRow,"Edit");
                 System.out.println("do process method main");

            }});

            listItem.appendChild(userCodeCell);
            listItem.appendChild(editRowCell);
            listItem.setSelected(false);
            listItem.setValue(user);
  }

i get erorr

SEVERE: >>java.lang.StackOverflowError

at java.util.HashMap.getEntry(HashMap.java:466) at java.util.LinkedHashMap.get(LinkedHashMap.java:301) at org.zkoss.util.CacheMap.getWithoutExpunge(CacheMap.java:399) at org.zkoss.util.CacheMap.get(CacheMap.java:390) at org.zkoss.util.FastReadCache.get(FastReadCache.java:58) at org.zkoss.zk.ui.sys.ComponentsCtrl.getEventMethod(ComponentsCtrl.java:327) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2651) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.sendEvent(EventProcessingThreadImpl.java:126) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:321) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:331) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:338)

at web.myapp.user.a.onEvent(SourceFile:83)

at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2741) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2712) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2653) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.sendEvent(EventProcessingThreadImpl.java:126) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:321) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:331) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:338)

at web.myapp.user.a.onEvent(SourceFile:83)

at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2741) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2712) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2653) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.sendEvent(EventProcessingThreadImpl.java:126) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:321) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:331) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:338)

this line

at web.myapp.user.a.onEvent(SourceFile:83)

This code indicates to the line Events.sendEvent(Events.ON_CLICK,tbEditRow,"Edit");

thanks,for your help.

delete flag offensive retag edit

5 Answers

Sort by ยป oldest newest most voted
0

answered 2015-09-11 15:13:17 +0800

cyiannoulis gravatar image cyiannoulis
1191 10

A composer is listening for events fired to the underlying component. So, if you want to execute an event handler method in another composer you have to send the event to its component. For example:

Events.sendEvent( new Event("onDoSomething", mainWindow, yourData) ) ;

where mainWindow is the ZK Component instance supported by the composer which contains the method you wish to call:

@Listen("onDoSomething = #MainWindowId")
public void methodInMain(Event event) {
   System.out.println("execute form other project");
}

/Costas

link publish delete flag offensive edit
0

answered 2015-09-12 02:29:15 +0800

onsir gravatar image onsir
132 2

I have try like this

in MainProject i add id window main

private Window winUser;

and in method MainProjecct

@Listen("onClick = #winUser")
public void methodInMain(Event event) {
     System.out.println("execute form other project");
}

in ProjectOther1, i add variable, id window main

@Wire private Window winUser;

i change like this

 tbEditRow.addEventListener("onClick", new EventListener() {
 public void onEvent(Event event) throws Exception {
      Events.sendEvent( new Event("onClick", winUser, null) ) ;
      System.out.println("do process method main");
 }});

but i get error

SEVERE: >>java.lang.IllegalArgumentException: Both comp and event must be specified

at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.sendEvent(EventProcessingThreadImpl.java:118) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:321) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:331) at web.myapp.user.a.onEvent(SourceFile:83) at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2741) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2712) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2653) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.run(EventProcessingThreadImpl.java:461)

link publish delete flag offensive edit
0

answered 2015-09-13 15:26:20 +0800

Darksu gravatar image Darksu
1991 1 4

Hello onsir,

It is not really clear how you have set up your project but you could also use GlobalCommand for sending events:

http://books.zkoss.org/zk-mvvm-book/8.0/databinding/globalcommand_binding.html

Best Regards,

Darksu

link publish delete flag offensive edit
0

answered 2015-09-19 03:09:08 +0800

onsir gravatar image onsir
132 2

this structur my project first i make project1 with Class UserRenderer

  public class UserRenderer extends GenericForwardComposer implements ListitemRenderer {

//component from UserController.java @Wire private Toolbarbutton tbEditRow; @Wire private Window winUser;

@Override public void render(Listitem listItem, Object arg1, int arg2)throws Exception {

        User user=(User)arg1;

        Listcell userCodeCell=new Listcell();
        userCodeCell.setLabel(user.getCode());


        Listcell editRowCell = new Listcell();
        tbEditRow=new Toolbarbutton();
        tbEditRow.setLabel("Edit");
        tbEditRow.setParent(editRowCell);
        tbEditRow.addEventListener("onClick", new EventListener() {
        public void onEvent(Event event) throws Exception {

             //I want methodInMain() in the main project be execution here
             Events.sendEvent( new Event("onClick", winUser, "methodInMain") ) ;
             System.out.println("do process method main");

        }});

        listItem.appendChild(userCodeCell);
        listItem.appendChild(editRowCell);
        listItem.setSelected(false);
        listItem.setValue(user);

}

in the project1 i not have zul file,then project1 i clean and build second, i make projectWeb - i import file project1.jar - i create file user.zul

<?xml version="1.0" encoding="UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" root="./winUser" ?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk xmlns="http://www.zkoss.org/2005/zul"
xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:w="http://www.zkoss.org/2005/zk/client"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org 
   /2005/zul zul.xsd">


<window id="winUser" width="1200px"  border="normal" apply="${UserController}" >
    <div> 
      <listbox id="listboxUser" rows="50" mold="paging" pageSize="10000"   height="420px">                                    
    <listhead>
  <listheader label="Code" />
      <listheader label="Name" />
  <listheader label="Edit" width="130px"/>
    </listhead>
    <listitem self="@{each=user}" />
   </listbox>
 </div>                        
 </window>
 </zk>

then i create file controller UserController.java

private Window winUser;
private Toolbarbutton tbEditRow;
@org.springframework.stereotype.Component("UserController")
@Scope("prototype")
public class UserController extends GenericForwardComposer {
    private Listbox listboxUser;
    private Toolbarbutton tbEditRow;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);

    ListModelList listModelUser = new ListModelList();
    listModelUser = userService.getListUser();
    listboxUser.setModel(listModelUser);
    //file class UserRenderer in other project (etc: Project1)
    listboxUser.setItemRenderer(new UserRenderer());

}

@GlobalCommand("methodInMain")
public void methodInMain(){
    System.out.println("execute form other project");
 }

}

i get erorr

SEVERE: >>java.lang.IllegalArgumentException: Both comp and event must be specified

at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.sendEvent(EventProcessingThreadImpl.java:118) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:321) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:331) at web.myapp.user.a.onEvent(SourceFile:98) at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2741) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2712) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2653) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.run(EventProcessingThreadImpl.java:461)

link publish delete flag offensive edit
0

answered 2015-09-21 02:21:23 +0800

vincentjian gravatar image vincentjian
2245 6

updated 2015-09-21 02:22:15 +0800

What is the relation between the two projects? ZK event listener can only used within one project. If you need to communicate between two projects (two war files deployed in one server), please try using JMS or iframe with javascript.

link publish delete flag offensive edit
Your answer
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
1 follower

RSS

Stats

Asked: 2015-09-11 03:08:54 +0800

Seen: 65 times

Last updated: Sep 21 '15

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