0

How to get 'real' target component from popup menuitem

asked 2010-08-11 03:31:49 +0800

dis gravatar image dis flag of Switzerland
140 4

Hi

I have a div component with a context menu:

<div context="popupMenu">....

The context menu is:
<menupopup id="popupMenu">
<menuitem label="Edit" id="editMenu" />
.......
</menupopup>

I wrote a EventListener in Java code:
editMenu.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
System.out.println("editMenu.onClick: " + event);
}}
);

The context menu appears and the event listener is called correctly. Now the question is, how can I retieve the component where the popup menu was opened and the menuitem was selected? In my case I want to have the div component where the popup menu was opened.


The event object in the onEvent callback does not seem to have the div component. The event object has a parent object (getParent()), but this is the menuitem object from the popup.

Thanks
Dieter

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2010-10-25 01:08:42 +0800

dis gravatar image dis flag of Switzerland
140 4

Hi

Have a look at:
http://docs.zkoss.org/wiki/ZK5:_Client_Computing_with_ZUML

this explains how events can be catched on client side (browser).

- Dieter

link publish delete flag offensive edit

answered 2010-10-23 07:48:03 +0800

joshiv gravatar image joshiv
69

Hi,
this works nicely , but I am trying to get all the events processed on client side only. Can you please help me understand how to get the above explained reference in client side only event handler ?

link publish delete flag offensive edit

answered 2010-08-13 03:13:44 +0800

dis gravatar image dis flag of Switzerland
140 4

Thank you very much for your help. The onOpen() event of the popup is the solution.

There is an easy way to remember the component which opened the popup:

		// add an onOpen event listener to the popup which remembers the referencedComponent in a 'hidden' attribute 'ref' of the popup menu
		popupMenu.addEventListener("onOpen", new EventListener(){
			public void onEvent(Event event) throws Exception {
				OpenEvent openEvt = (OpenEvent)event;
				Popup popup = (Popup)openEvt.getTarget();
				Component referencedComponent = openEvt.getReference();
				// set the referenced object in a hidden reference of the popup
				popup.setAttribute("ref", referencedComponent);
			}}
		);

// now we can read that reference again in the 'onClick' event of the menuitem via the attribute 'ref' of the popup menu
		editMenu.addEventListener("onClick", new EventListener(){
			public void onEvent(Event event) throws Exception {
				Component menuitem = event.getTarget();
				Component popup = menuitem.getParent();
				
				Component referencedComponent = (Component)popup.getAttribute("ref");
				// continue with business logic on referencedComponent
			}}
		);

This approach has the advantage that its independent from the underlying Composer class.

- Dieter

link publish delete flag offensive edit

answered 2010-08-12 12:08:32 +0800

twiegand gravatar image twiegand
1807 3

For a simple example of what Peter is talking about, please have a look at this thread.

Regards,

Todd

link publish delete flag offensive edit

answered 2010-08-11 20:44:53 +0800

PeterKuo gravatar image PeterKuo
481 2

@dis
You may listen to onOpen() event of the popup.
And remember who opened the popup.
You may refer to the getReference() api of OpenEvent.java

link publish delete flag offensive edit

answered 2010-08-11 05:59:09 +0800

dis gravatar image dis flag of Switzerland
140 4

I cannot cast the event to a ForwardEvent. I get a MouseEvent in the onEvent callback.

I got folllowing exception:
>>java.lang.ClassCastException: org.zkoss.zk.ui.event.MouseEvent cannot be cast to org.zkoss.zk.ui.event.ForwardEvent
>> at zk.cc.CompositeDesigner$4.onEvent(CompositeDesigner.java:126)
>> at org.zkoss.zk.ui.impl.EventProcessor.process0(EventProcessor.java:196)
...

Thanks
Dieter

link publish delete flag offensive edit

answered 2010-08-11 04:07:49 +0800

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

updated 2010-08-11 04:08:33 +0800

If you will get the target component who is called the event. You must cast the Event to the right Event class.

Not tested :

       public void onEvent(Event event) throws InterruptedException {
 
                // first, casting to the right event
		String id = ((ForwardEvent) event).getOrigin().getTarget().getId();
		System.out.println(id);
		
		if (id.equals("menuItem_1_id")) {
			// do something here
		} else if (id.equals("menuItem_2_id")) {
			// do something there
		}

}

best
Stephan

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
1 follower

RSS

Stats

Asked: 2010-08-11 03:31:49 +0800

Seen: 1,018 times

Last updated: Oct 25 '10

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