My event is not triggered by parent class

Hi !
I am using a floating indicator to show some text/progressBar in an helpPanel over a contentPanel.

This helpPanel does not need any events.
The only events I need are those of my contentPanel and its parent.

final StyledPanel helpPanel = new StyledPanel() {
		@Override
		public boolean handleEvent(int event) {
			final Widget focus = contentPanel.getFocus();
			if (focus != null) {
				return focus.handleEvent(event);
			}
			return super.handleEvent(event);
		}
	}; 

When my helpPanel isSetActive, I can trigger my contentPanel events but not its parents events. As soon as my helpPanel is unactive, I am able to trigger all of my events.

I can handle a parent event using by instance :

if (focus != null) {
    focus.getParent().getParent().handleEvent(event) 
    return focus.handleEvent(event);
}

But I have to know exactly which parent/subparent owns the right method and I prefer to avoid helpPanel duplication.

Is there a way to always catch my contentPanel and parents events instead of helpPanel ?

Thanks,
Elias

Hello Elias,

In your example, the event handling is different depending on whether the helpPanel is active or not.

In the first case, where the helpPanel is active, it catches the event and forwards it to the widget of the contentPanel that owns the focus:

 final Widget focus = contentPanel.getFocus();
 if (focus != null) {
 	return focus.handleEvent(event);
 }

The widget focus will handle the event and that’s it (it will not forward it to its parent hierarchy).

In the second case, where the helpPanel is not active, the event is handled following the default’s event handling policy. The policy states that the event is sent to the widget and recursively to its parents until it is consumed. For your interest, this policy is implemented in the class DesktopEventManagementPolicy of the library ej.library.ui.mwt. Its role is to dispatch the events received on a desktop.

In order to implement the same policy in your case, you can do the following instead:

final StyledPanel helpPanel = new StyledPanel() {
	@Override
	public boolean handleEvent(int event) {
		Widget focus = contentPanel.getFocus();
		return sendEventHierarchy(contentPanel, focus, event);
	}

	private boolean sendEventHierarchy(Panel panel, Widget widget, int event) {
		// send the event recursively to the parent hierarchy until it is consumed.
		while (widget != null) {
			if (widget.isEnabled() && widget.handleEvent(event)) {
				return true;
			}
			widget = widget.getParent();
		}

		// send the event to the panel if no child consumed the event.
		if (panel != null && panel.handleEvent(event)) {
			return true;
		}

		return false;
	}
};

The event is sent to the widget that owns the focus and then to the parent hierarchy, this way you don’t have explicit calls to getParent().

Best regards,

Thomas

1 Like

Thank you Thomas, it works like a charm !!