PDA

View Full Version : BrowserEvents does not fire anymore on GWT Widgets



moldoe
1 Jun 2009, 1:39 PM
I'm using the GWTCanvas class from GWT Incubator project. When I was using GXT 1.2, I extended this class and added browser event handling to it by implementing SourcesMouseEvents and overriding Widget.onBrowserEvent(). Mouse events were firing fine on this new custom canvas. Now with GXT 2.0 the same implementation stoped working. I even tried to change the deprecated API that I was using with the new EventHandler API. It still didn't work. Than I noticed that I have this problem only if the canvas Widget is added to a GXT Container. If I add the canvas directly to the RootPanel events work fine.

Is this a bug in GXT implemetation or do I have to do it different way?

Here is my code. It relies on GWTCanvas from gwt-incubator for GWT 1.6.




public class Test implements EntryPoint {

public void onModuleLoad() {

TCCanvas2 canvas = new TCCanvas2();
canvas.setBackgroundColor(Color.YELLOW);

canvas.addMouseDownHandler(new MouseDownHandler() {
@Override
public void onMouseDown(MouseDownEvent event) {
MessageBox.info("MouseDownHandler.onMouseDown",
"Coords: " + event.getX() + ", " + event.getY(), null);
}
});

Viewport viewport = new Viewport();
viewport.add(canvas);
RootPanel.get().add(viewport);
}
}



public class TCCanvas2 extends GWTCanvas implements HasAllMouseHandlers, HasClickHandlers {

public TCCanvas2() {
}

public TCCanvas2(int coordX, int coordY) {
super(coordX, coordY);
}

public TCCanvas2(int coordX, int coordY, int pixelX, int pixelY) {
super(coordX, coordY, pixelX, pixelY);
}

@Override
public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
return addDomHandler(handler, MouseDownEvent.getType());
}

@Override
public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
return addDomHandler(handler, MouseUpEvent.getType());
}

@Override
public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
return addDomHandler(handler, MouseOutEvent.getType());
}

@Override
public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
return addDomHandler(handler, MouseOverEvent.getType());
}

@Override
public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
return addDomHandler(handler, MouseMoveEvent.getType());
}

@Override
public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
return addDomHandler(handler, MouseWheelEvent.getType());
}

@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}

Thanks,

Daniel


Windows XP
Hosted Mode
FF3
GXT 2.0-m2
JDK 1.6.4

micgala
2 Jun 2009, 12:26 AM
Hi.

GWT has changed....
For 1.6 you need to do as reported in GWT documentation:

Widget Developers

If you develop your own widgets and have overridden Widget.onBrowserEvent(), make sure that your override calls super.onBrowserEvent(). The Widget.onBrowserEvent() method contains code to automatically fire native events to their associated EventHandlers, so if you override the code and do not call the super method, your EventHandlers will not fire.
For example, this will not automatically call ClickHandlers added using the Widget.addDomHandler() method:

public class MyWidget extends Widget {
@Override
public void onBrowserEvent(Event event) {
if (DOM.eventGetType(event) == Event.ONCLICK) {
// Do something with click
}
}
} but this code will:

public class MyWidget extends Widget {
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
if (DOM.eventGetType(event) == Event.ONCLICK) {
// Do something with click
}
}
} An even better alternative is to use an anonymous inner class to handle the ClickEvent as follows (notice how we no longer need to override onBrowserEvent()):

public class MyWidget extends Widget {
public MyWidget() {
addDomHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Do something with click
}
}, ClickEvent.getType());
}
}Hope this helps you.
Regards,
Michel.

moldoe
2 Jun 2009, 5:45 AM
Michael thanks for your help. Your reply just confirmed that my example code is fine.
You can see the addXXXHandler methods in my TCCanvas class are doing exactly what you anonymous class is doing in your example. The difference is that I'm attaching the handler from outside the Widget class. Also you can see that I'm not overriding onBrowserEvent anymore since I switched to GWT 1.6. And I have to repeat myself, this only happens if I add my canvas to some GXT container.

Can someone from GXT team shed some light on this issue?

Thanks again,

Daniel

sven
2 Jun 2009, 5:48 AM
It will be fixed with one of the next pre releases. Please note that GXT2 is not yet stable.

moldoe
2 Jun 2009, 6:04 AM
Thanks! I just wanted to know if it's a problem with my code or GXT's code.
Anyway, you're doing a great job!

Daniel

sven
2 Jun 2009, 6:11 AM
Fixed in SVN

moldoe
2 Jun 2009, 6:13 AM
Fixed in SVN


This was FAAAST!!! :D

Daniel