Cancelling an event handler within another event handler

57 views
Skip to first unread message

Thomas Lefort

unread,
Nov 19, 2012, 4:19:53 PM11/19/12
to google-we...@googlegroups.com
I have potentially several click handlers on a widget. The first one is used to capture click events on an image (then the event is fired on the image) within the widget and created at creation time. The other ones are added "outside" of the widget's code.

I would like to cancel (not remove) the other handlers if it's the image that is being clicked but I just can't find a way to do so. Any help would be appreciated.

Here's the code I use... for some reason if I don't capture teh click and fire the event on the image (in the constructor) it just doesn't trigger the event. May be if I can solve that problem another way to start with? I have the feeling the issue comes form the DOM.appendChild.

public class IconAnchorActions extends IconAnchor {

    private List<Image> actions = new ArrayList<Image>();

    public IconAnchorActions(){
        super();
        addClickHandler(new ClickHandler() {
           
            @Override
            public void onClick(ClickEvent event) {
                for(Image image : actions) {
                    if(event.getNativeEvent().getEventTarget().equals(image.getElement())) {
                        image.fireEvent(new GwtEvent<ClickHandler>() {
                            @Override
                            public com.google.gwt.event.shared.GwtEvent.Type<ClickHandler> getAssociatedType() {
                                return ClickEvent.getType();
                            }
                            @Override
                            protected void dispatch(ClickHandler handler) {
                                handler.onClick(null);
                            }
                        });
                        event.stopPropagation();
                        return;
                    }
                }
            }
        });
    }
   
    public void addClickAction(ImageResource imageResource, ClickHandler clickHandler, boolean left) {
        Image image  = new Image(imageResource);
        actions.add(image);
        image.getElement().getStyle().setVerticalAlign(VerticalAlign.MIDDLE);
        if(left) {
            image.getElement().getStyle().setFloat(Float.LEFT);
            span.getStyle().setMarginLeft(5, Unit.PX);
        } else {
            image.getElement().getStyle().setFloat(Float.RIGHT);
            span.getStyle().setMarginRight(5, Unit.PX);
        }
        image.getElement().getStyle().setCursor(Cursor.POINTER);
        image.addClickHandler(clickHandler);
        DOM.appendChild(getElement(), image.getElement());
    }
   
}

Thomas Broyer

unread,
Nov 19, 2012, 4:29:10 PM11/19/12
to google-we...@googlegroups.com
Add only *your* click handler on the image, and add the others use addHandler (*not* addDomHandler), then from your click handler, either fire the event on the widget to trigger the "external" click handlers, or ignore it.

lefor...@gmail.com

unread,
Nov 19, 2012, 4:33:36 PM11/19/12
to google-we...@googlegroups.com
Thanks Thomas. Unfortunately i cannot prevent the use of addDomHandler on the widget.

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/wMBr7vOenXAJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Thomas Broyer

unread,
Nov 19, 2012, 5:43:31 PM11/19/12
to google-we...@googlegroups.com


On Monday, November 19, 2012 10:34:37 PM UTC+1, Thomas Lefort wrote:
Thanks Thomas. Unfortunately i cannot prevent the use of addDomHandler on the widget.

Well, because the image is a child widget, you can stopPropagation() from your handler to prevent the click to bubble up to your widget; and delegate the event to the handlers otherwise, or something like that.

The thing is: you cannot prevent "other handlers" on the same widget to be triggered from within a handler, so you have to split those handlers in two buckets.

Or possibly wrap the "external" handlers into ones that check a flag in the widget to conditionally delegate to the "real" handler, and have the first ("internal") handler set that flag; but IMO using two buckets is the cleanest solution.

Thomas Lefort

unread,
Nov 20, 2012, 3:32:29 AM11/20/12
to google-we...@googlegroups.com
Your solution works indeed but in the end I decided to override the onBrowser event with a first filter to check for click on images.
Thanks again for helping out.
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
Reply all
Reply to author
Forward
0 new messages