Roughian Examples Site Map - Examples - Tutorials

EventListener


Version 1.0 onwards

Lets You Pick Up Many Events When Building Widgets


Put your mouse cursor in the box and use the mouse wheel. The amount that the number will change each click will depend on your settings in Control Panel - Mouse - Wheel - Vertical Scrolling.


Sources



Notes


This is something you should only use if you are subclassing a widget - it's not much use otherwise, you can't addEventListener() to anything I'm aware of.

So, as a builder of widgets, you'd use this to pick up events and use them in ways that you can't do with the ordinarily supported event interfaces


Code


package com.roughian.examples.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseWheelVelocity;

class Demo extends Label
{
    // Scroll position
    private int count = 0;
    public Demo()
    {
        setText("Count = " + count);
        /*
         * You need to 'sink' (register for) the events you want
         */
        sinkEvents(Event.ONMOUSEWHEEL);
        DOM.setStyleAttribute(getElement(), "border", "1px dotted red");
        DOM.setStyleAttribute(getElement(), "padding", "20px");
    }
    public void onBrowserEvent(Event event)
    {
        switch (DOM.eventGetType(event))
        {
        case Event.ONMOUSEWHEEL:
            /*
             * Get the mouse wheel information
             */
            MouseWheelVelocity velocity = new MouseWheelVelocity(event);
            count += velocity.getDeltaY();
            setText("Count = " + count);
            /*
             * Stop the event happening where it usually would. If we don't
             * do this, then the panel this widget is in would scroll if it
             * could
             */
            DOM.eventPreventDefault(event);
            break;
        }
    }
}