Handling custom events in GWT

1,252 views
Skip to first unread message

Christopher

unread,
Mar 1, 2011, 3:48:30 AM3/1/11
to Google Web Toolkit
Hi there,

I've got a custom event fired by some JS code in an iframe. If I add
an event listener to the iframe via JS then the event is captured.
However I'm having difficulty capturing the same event within GWT;
indeed is it possible to capture custom events in GWT?

The event is constructed and fired in JS like so:

var event = document.createEvent("Event");
event.initEvent("hideWithMessage", true, true);
event.message = "This message to be reported.";

window.parent.document.getElementById("hatsPopup").dispatchEvent(event);

(executes within an iframe hence the window.parent.document business).

Here's how I attach my handler:

frame.addDomHandler(new HideWithMessageHandler() {
@Override
public void onHideWithMessage(String message) {
Window.alert("Yippee! " + message);
}

}, HideWithMessageEvent.getType());

The event is defined as:

public class HideWithMessageEvent extends
DomEvent<HideWithMessageHandler> {

private static final Type<HideWithMessageHandler> TYPE = new
Type<HideWithMessageHandler>(
"hideWithMessage", new HideWithMessageEvent());

public static Type<HideWithMessageHandler> getType() {
return TYPE;
}

@Override
public Type<HideWithMessageHandler> getAssociatedType() {
return TYPE;
}

@Override
protected void dispatch(HideWithMessageHandler handler) {
handler.onHideWithMessage(null);
}

}

...and the handler:

public interface HideWithMessageHandler extends EventHandler {
void onHideWithMessage(String message);
}

Incidentally if I try the same sort of thing using a clickEvent then
all is well i.e. I'm able to fire a click event at the iframe from JS
and capture it in GWT. The difficulty is with custom events.

Help and guidance very much appreciated.

Kind regards,
Christopher

Jim Douglas

unread,
Mar 1, 2011, 8:49:11 AM3/1/11
to Google Web Toolkit
Here's one way to support arbitrary events:

// iOS & Android touch events:

private final static String TOUCHSTART = "touchstart";
private final static String TOUCHMOVE = "touchmove";
private final static String TOUCHEND = "touchend";
private final static String TOUCHCANCEL = "touchcancel";
private final static int TOUCHTIMER = 550;
private Timer touchTimer = null;
private boolean cancelClickEvent = false;

private void addTouchListeners(Element element)
{
if (TouchHandler.supportsTouch())
{
sinkEvents(Event.ONCLICK);
addEventListener(element, TOUCHSTART, this, false);
addEventListener(element, TOUCHMOVE, this, false);
addEventListener(element, TOUCHEND, this, false);
addEventListener(element, TOUCHCANCEL, this, false);
}
}

private static native void addEventListener(Element element,
String event,
EventListener
listener,
boolean capture)
/*-{
element.addEventListener(
event,
function(e)
{

listener.@com.google.gwt.user.client.EventListener::onBrowserEvent(Lcom/
google/gwt/user/client/Event;)(e);
},
capture);
}-*/;

@Override
public void onBrowserEvent(Event event)
{
String type = event.getType();
if (TOUCHSTART.equals(type))
{
TouchEvent touchEvent = event.cast();
if (touchEvent.getTouches().length() == 1)
startTouchTimer(event);
else
cancelTouchTimer();
}
else if (TOUCHMOVE.equals(type))
{
cancelTouchTimer();
}
else if (TOUCHEND.equals(type))
{
cancelTouchTimer();
}
else if (TOUCHCANCEL.equals(type))
{
cancelTouchTimer();
}
else if ("click".equals(type))
{
if (cancelClickEvent)
{
event.preventDefault();
event.stopPropagation();
}
else
{
super.onBrowserEvent(event);

Christopher

unread,
Mar 1, 2011, 5:24:06 PM3/1/11
to Google Web Toolkit
Hi Jim,

You're doing what I was hoping to avoid - that is getting down to JSNI
and manipulating the DOM objects directly. I had hoped that I could
leverage the existing event architecture in GWT, but perhaps I can't.

Thanks anyhow.

Kind regards,
Christopher

Thomas Broyer

unread,
Mar 1, 2011, 5:30:23 PM3/1/11
to google-we...@googlegroups.com


On Tuesday, March 1, 2011 11:24:06 PM UTC+1, Christopher wrote:
Hi Jim,

You're doing what I was hoping to avoid - that is getting down to JSNI
and manipulating the DOM objects directly. I had hoped that I could
leverage the existing event architecture in GWT, but perhaps I can't.

No, not currently (there's hope it will be "fixed" one day, but there are things with higher priority)
Reply all
Reply to author
Forward
0 new messages