Search in sources :

Example 1 with EventListener

use of org.gwtproject.user.client.EventListener in project gwtproject by treblereel.

the class EditableCellTestBase method testOnBrowserEvent.

/**
 * Test
 * {@link Cell#onBrowserEvent(Element, Object, Object, NativeEvent, ValueUpdater)}
 * with the specified conditions.
 *
 * @param startHtml the innerHTML of the cell before the test starts
 * @param event the event to fire
 * @param value the cell value
 * @param viewData the initial view data
 * @param expectedValue the expected value passed to the value updater, or
 *          null if none expected
 * @param expectedViewData the expected value of the view data after the event
 * @return the parent element
 */
protected Element testOnBrowserEvent(String startHtml, NativeEvent event, final T value, V viewData, T expectedValue, V expectedViewData) {
    // Setup the parent element.
    final Element parent = Document.get().createDivElement();
    parent.setInnerHTML(startHtml);
    Document.get().getBody().appendChild(parent);
    // If the element has a child, use it as the event target.
    Element child = parent.getFirstChildElement();
    Element target = (child == null) ? parent : child;
    // Pass the event to the cell.
    final MockValueUpdater valueUpdater = new MockValueUpdater();
    final AbstractEditableCell<T, V> cell = createCell();
    cell.setViewData(DEFAULT_KEY, viewData);
    Event.setEventListener(parent, new EventListener() {

        @Override
        public void onBrowserEvent(Event event) {
            try {
                DOM.setEventListener(parent, null);
                Context context = new Context(0, 0, DEFAULT_KEY);
                cell.onBrowserEvent(context, parent, value, event, valueUpdater);
                parent.removeFromParent();
            } catch (Exception e) {
                // We are in an event loop, so events may not propagate out to JUnit.
                fail("An exception occured while handling the event: " + e.getMessage());
            }
        }
    });
    Event.sinkEvents(target, Event.getTypeInt(event.getType()));
    target.dispatchEvent(event);
    assertNull(DOM.getEventListener(parent));
    // Check the expected value and view data.
    assertEquals(expectedViewData, cell.getViewData(DEFAULT_KEY));
    valueUpdater.assertLastValue(expectedValue);
    return parent;
}
Also used : Element(org.gwtproject.dom.client.Element) NativeEvent(org.gwtproject.dom.client.NativeEvent) Event(org.gwtproject.user.client.Event) EventListener(org.gwtproject.user.client.EventListener)

Example 2 with EventListener

use of org.gwtproject.user.client.EventListener in project gwtproject by treblereel.

the class CellTestBase method testOnBrowserEvent.

/**
 * Test
 * {@link org.gwtproject.cell.client.Cell#onBrowserEvent(Element, Object, Object, NativeEvent, ValueUpdater)}
 * with the specified conditions.
 *
 * @param cell the cell to use
 * @param startHtml the innerHTML of the cell before the test starts
 * @param event the event to fire
 * @param value the cell value
 * @param expectedValue the expected value passed to the value updater, or
 *          null if none expected
 * @param dispatchToFirstChild true to dispatch to the first child of the
 *          rendered parent element, if one is available
 * @return the parent element
 */
protected Element testOnBrowserEvent(final Cell<T> cell, String startHtml, NativeEvent event, final T value, T expectedValue, boolean dispatchToFirstChild) {
    // Setup the parent element.
    final Element parent = Document.get().createDivElement();
    parent.setInnerHTML(startHtml);
    Document.get().getBody().appendChild(parent);
    // If the element has a child, use it as the event target.
    Element target = parent;
    if (dispatchToFirstChild) {
        Element child = parent.getFirstChildElement();
        target = (child == null) ? parent : child;
    }
    // Pass the event to the cell.
    final MockValueUpdater valueUpdater = new MockValueUpdater();
    Event.setEventListener(parent, new EventListener() {

        @Override
        public void onBrowserEvent(Event event) {
            try {
                DOM.setEventListener(parent, null);
                Context context = new Context(0, 0, DEFAULT_KEY);
                cell.onBrowserEvent(context, parent, value, event, valueUpdater);
                parent.removeFromParent();
            } catch (Exception e) {
                // We are in an event loop, so events may not propagate out to JUnit.
                fail("An exception occured while handling the event: " + e.getMessage());
            }
        }
    });
    Event.sinkEvents(target, Event.getTypeInt(event.getType()));
    target.dispatchEvent(event);
    assertNull(DOM.getEventListener(parent));
    // Check the expected value and view data.
    valueUpdater.assertLastValue(expectedValue);
    return parent;
}
Also used : Context(org.gwtproject.cell.client.Cell.Context) Element(org.gwtproject.dom.client.Element) NativeEvent(org.gwtproject.dom.client.NativeEvent) Event(org.gwtproject.user.client.Event) EventListener(org.gwtproject.user.client.EventListener)

Example 3 with EventListener

use of org.gwtproject.user.client.EventListener in project gwtproject by treblereel.

the class CellBasedWidgetImplStandard method handleNonBubblingEvent.

/**
 * Handle an event from a cell. Used by {@link #initEventSystem()}.
 *
 * @param event the event to handle.
 */
private static void handleNonBubblingEvent(Event event) {
    // Get the event target.
    EventTarget eventTarget = event.getEventTarget();
    if (!Element.is(eventTarget)) {
        return;
    }
    Element target = eventTarget.cast();
    // Get the event listener, which is the first widget that handles the
    // specified event type.
    String typeName = event.getType();
    EventListener listener = DOM.getEventListener(target);
    while (target != null && listener == null) {
        target = target.getParentElement().cast();
        if (target != null && isNonBubblingEventHandled(target, typeName)) {
            // The target handles the event, so this must be the event listener.
            listener = DOM.getEventListener(target);
        }
    }
    // Fire the event.
    if (listener != null) {
        DOM.dispatchEvent(event, target, listener);
    }
}
Also used : Element(org.gwtproject.dom.client.Element) EventListener(org.gwtproject.user.client.EventListener) EventTarget(org.gwtproject.dom.client.EventTarget)

Example 4 with EventListener

use of org.gwtproject.user.client.EventListener in project gwtproject by treblereel.

the class CompositeCellTest method testOnBrowserEventCell.

/**
 * Fire an event to a specific cell.
 */
@SuppressWarnings("unchecked")
public void testOnBrowserEventCell() {
    // Setup the parent element.
    final Element parent = Document.get().createDivElement();
    parent.setInnerHTML(getExpectedInnerHtml());
    Document.get().getBody().appendChild(parent);
    // Create the composite cell and updater.
    List<HasCell<String, ?>> cells = createHasCells(2);
    MockCell<String> innerCell = new MockCell<>(false, "fromCell2", "click");
    addCell(innerCell, cells);
    final CompositeCell<String> cell = new CompositeCell<String>(cells);
    // Add an event listener.
    EventListener listener = new EventListener() {

        @Override
        public void onBrowserEvent(Event event) {
            Cell.Context context = new Cell.Context(3, 4, "key");
            cell.onBrowserEvent(context, parent, "test-x", event, null);
        }
    };
    DOM.sinkEvents(parent, Event.ONCLICK);
    DOM.setEventListener(parent, listener);
    // Fire the event on one of the inner cells.
    NativeEvent event = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
    Element.as(parent.getChild(2)).dispatchEvent(event);
    innerCell.assertLastEventValue("test-x");
    innerCell.assertLastParentElement(Element.as(parent.getChild(2)));
    Cell.Context innerContext = innerCell.getLastContext();
    assertEquals("key", innerContext.getKey());
    assertEquals(3, innerContext.getIndex());
    assertEquals(4, innerContext.getColumn());
    // Fire the event to another cell that doesn't consume this event. Shouldn't respond
    // to the event
    MockCell<String> innerCell2 = (MockCell<String>) cells.get(1).getCell();
    Element.as(parent.getChild(1)).dispatchEvent(event);
    innerCell2.assertLastEventValue(null);
    // Remove the element and event listener.
    DOM.setEventListener(parent, null);
    Document.get().getBody().removeChild(parent);
}
Also used : Element(org.gwtproject.dom.client.Element) NativeEvent(org.gwtproject.dom.client.NativeEvent) Event(org.gwtproject.user.client.Event) EventListener(org.gwtproject.user.client.EventListener) NativeEvent(org.gwtproject.dom.client.NativeEvent)

Aggregations

Element (org.gwtproject.dom.client.Element)4 EventListener (org.gwtproject.user.client.EventListener)4 NativeEvent (org.gwtproject.dom.client.NativeEvent)3 Event (org.gwtproject.user.client.Event)3 Context (org.gwtproject.cell.client.Cell.Context)1 EventTarget (org.gwtproject.dom.client.EventTarget)1