I'm converting all the MouseListeners in an old hobby project into
Mouse*Handlers instead. My old MouseListeners were somewhat complex
and followed this form:

public class TooltipListener implements MouseListener {
        some private variables declarations here

        public void onMouseDown(Widget source, int x, int y) {}
        public void onMouseMove(Widget source, int x, int y) {}
        public void onMouseUp(Widget source, int x, int y) {}
        public void onMouseEnter(Widget source) {}
        public void onMouseLeave(Widget source) {}

        private methods here
}

with some code in each onMouse* method. I then added these listeners
like this:

myCustomWidget.addMouseListener(new TooltipListener());


With the new Handler system, I have changed my class to look like the
following:

public class ToolTipHandler implements MouseMoveHandler,
MouseOverHandler, MouseDownHandler, MouseUpHandler, MouseOutHandler {
        some private variables declarations here

        public void onMouseMove(MouseMoveEvent event) {}
        public void onMouseOver(MouseOverEvent event) {}
        public void onMouseDown(MouseDownEvent event) {}
        public void onMouseUp(MouseUpEvent event) {}
        public void onMouseOut(MouseOutEvent event) {}

        private methods here
}

Note that the ToolTipHandler class relies on shared private variables
between all the mouse methods. Each method has almost identical code
from before - the only difference are they use accessor methods from
the appropriate event object. I am adding this 5 part mouse handler
like this:

TooltipHandler handler = new TooltipHandler();
myCustomWidget.addMouseMoveHandler(handler);
myCustomWidget.addMouseOverHandler(handler);
myCustomWidget.addMouseDownHandler(handler);
myCustomWidget.addMouseUpHandler(handler);
myCustomWidget.addMouseOutHandler(handler);


Everything works great, but the code is a bit clunky and I suspect
there is a better way to do this. Is there? I'm using 1.7.1 right now,
but I'm happy to take suggestions pertaining to 2.0.

Thanks! =)

--

You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.


Reply via email to