<commit_before>/*
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.terminal.gwt.client.ui;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ContextMenuEvent;
import com.google.gwt.event.dom.client.ContextMenuHandler;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.vaadin.terminal.gwt.client.ComponentConnector;
import com.vaadin.terminal.gwt.client.Util;

public abstract class AbstractClickEventHandler implements MouseDownHandler,
        MouseUpHandler, DoubleClickHandler, ContextMenuHandler {

    private HandlerRegistration mouseDownHandlerRegistration;
    private HandlerRegistration mouseUpHandlerRegistration;
    private HandlerRegistration doubleClickHandlerRegistration;
    private HandlerRegistration contextMenuHandlerRegistration;

    protected ComponentConnector connector;
    private String clickEventIdentifier;

    /**
     * The element where the last mouse down event was registered.
     */
    private JavaScriptObject lastMouseDownTarget;

    /**
     * Set to true by {@link #mouseUpPreviewHandler} if it gets a mouseup at the
     * same element as {@link #lastMouseDownTarget}.
     */
    private boolean mouseUpPreviewMatched = false;

    private HandlerRegistration mouseUpEventPreviewRegistration;

    /**
     * Previews events after a mousedown to detect where the following mouseup
     * hits.
     */
    private final NativePreviewHandler mouseUpPreviewHandler = new NativePreviewHandler() {

        public void onPreviewNativeEvent(NativePreviewEvent event) {
            if (event.getTypeInt() == Event.ONMOUSEUP) {
                mouseUpEventPreviewRegistration.removeHandler();

                // Event's reported target not always correct if event
                // capture is in use
                Element elementUnderMouse = Util.getElementUnderMouse(event
                        .getNativeEvent());
                if (lastMouseDownTarget != null
                        && elementUnderMouse.cast() == lastMouseDownTarget) {
                    mouseUpPreviewMatched = true;
                } else {
                    System.out.println("Ignoring mouseup from "
                            + elementUnderMouse + " when mousedown was on "
                            + lastMouseDownTarget);
                }
            }
        }
    };

    public AbstractClickEventHandler(ComponentConnector connector,
            String clickEventIdentifier) {
        this.connector = connector;
        this.clickEventIdentifier = clickEventIdentifier;
    }

    public void handleEventHandlerRegistration() {
        // Handle registering/unregistering of click handler depending on if
        // server side listeners have been added or removed.
        if (hasEventListener()) {
            if (mouseDownHandlerRegistration == null) {
                mouseDownHandlerRegistration = registerHandler(this,
                        MouseDownEvent.getType());
                mouseUpHandlerRegistration = registerHandler(this,
                        MouseUpEvent.getType());
                doubleClickHandlerRegistration = registerHandler(this,
                        DoubleClickEvent.getType());
                contextMenuHandlerRegistration = registerHandler(this,
                        ContextMenuEvent.getType());
            }
        } else {
            if (mouseDownHandlerRegistration != null) {
                // Remove existing handlers
                mouseDownHandlerRegistration.removeHandler();
                mouseUpHandlerRegistration.removeHandler();
                doubleClickHandlerRegistration.removeHandler();
                contextMenuHandlerRegistration.removeHandler();

                mouseDownHandlerRegistration = null;
                mouseUpHandlerRegistration = null;
                doubleClickHandlerRegistration = null;
                contextMenuHandlerRegistration = null;
            }
        }

    }

    /**
     * Registers the given handler to the widget so that the necessary events
     * are passed to this {@link ClickEventHandler}.
     * <p>
     * By default registers the handler with the connector root widget.
     * </p>
     * 
     * @param <H>
     * @param handler
     *            The handler to register
     * @param type
     *            The type of the handler.
     * @return A reference for the registration of the handler.
     */
    protected <H extends EventHandler> HandlerRegistration registerHandler(
            final H handler, DomEvent.Type<H> type) {
        return connector.getWidget().addDomHandler(handler, type);
    }

    /**
     * Checks if there is a server side event listener registered for clicks
     * 
     * @return true if there is a server side event listener registered, false
     *         otherwise
     */
    public boolean hasEventListener() {
        return connector.hasEventListener(clickEventIdentifier);
    }

    /**
     * Event handler for context menu. Prevents the browser context menu from
     * popping up if there is a listener for right clicks.
     */

    public void onContextMenu(ContextMenuEvent event) {
        if (hasEventListener() && shouldFireEvent(event)) {
            // Prevent showing the browser's context menu when there is a right
            // click listener.
            event.preventDefault();
        }
    }

    public void onMouseDown(MouseDownEvent event) {
        /*
         * When getting a mousedown event, we must detect where the
         * corresponding mouseup event if it's on a different part of the page.
         */
        lastMouseDownTarget = event.getNativeEvent().getEventTarget();
        mouseUpPreviewMatched = false;
        mouseUpEventPreviewRegistration = Event
                .addNativePreviewHandler(mouseUpPreviewHandler);
    }

    public void onMouseUp(MouseUpEvent event) {
        /*
         * Only fire a click if the mouseup hits the same element as the
         * corresponding mousedown. This is first checked in the event preview
         * but we can't fire the even there as the event might get canceled
         * before it gets here.
         */
        if (hasEventListener()
                && mouseUpPreviewMatched
                && lastMouseDownTarget != null
                && Util.getElementUnderMouse(event.getNativeEvent()) == lastMouseDownTarget) {
            // "Click" with left, right or middle button
            fireClick(event.getNativeEvent());
        }
        mouseUpPreviewMatched = false;
        lastMouseDownTarget = null;
    }<commit_msg>Fixed regression: AbstractClickEventHandler.onMouseUp() should call shouldFireEvent()<commit_after>/*
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.terminal.gwt.client.ui;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ContextMenuEvent;
import com.google.gwt.event.dom.client.ContextMenuHandler;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.vaadin.terminal.gwt.client.ComponentConnector;
import com.vaadin.terminal.gwt.client.Util;

public abstract class AbstractClickEventHandler implements MouseDownHandler,
        MouseUpHandler, DoubleClickHandler, ContextMenuHandler {

    private HandlerRegistration mouseDownHandlerRegistration;
    private HandlerRegistration mouseUpHandlerRegistration;
    private HandlerRegistration doubleClickHandlerRegistration;
    private HandlerRegistration contextMenuHandlerRegistration;

    protected ComponentConnector connector;
    private String clickEventIdentifier;

    /**
     * The element where the last mouse down event was registered.
     */
    private JavaScriptObject lastMouseDownTarget;

    /**
     * Set to true by {@link #mouseUpPreviewHandler} if it gets a mouseup at the
     * same element as {@link #lastMouseDownTarget}.
     */
    private boolean mouseUpPreviewMatched = false;

    private HandlerRegistration mouseUpEventPreviewRegistration;

    /**
     * Previews events after a mousedown to detect where the following mouseup
     * hits.
     */
    private final NativePreviewHandler mouseUpPreviewHandler = new NativePreviewHandler() {

        public void onPreviewNativeEvent(NativePreviewEvent event) {
            if (event.getTypeInt() == Event.ONMOUSEUP) {
                mouseUpEventPreviewRegistration.removeHandler();

                // Event's reported target not always correct if event
                // capture is in use
                Element elementUnderMouse = Util.getElementUnderMouse(event
                        .getNativeEvent());
                if (lastMouseDownTarget != null
                        && elementUnderMouse.cast() == lastMouseDownTarget) {
                    mouseUpPreviewMatched = true;
                } else {
                    System.out.println("Ignoring mouseup from "
                            + elementUnderMouse + " when mousedown was on "
                            + lastMouseDownTarget);
                }
            }
        }
    };

    public AbstractClickEventHandler(ComponentConnector connector,
            String clickEventIdentifier) {
        this.connector = connector;
        this.clickEventIdentifier = clickEventIdentifier;
    }

    public void handleEventHandlerRegistration() {
        // Handle registering/unregistering of click handler depending on if
        // server side listeners have been added or removed.
        if (hasEventListener()) {
            if (mouseDownHandlerRegistration == null) {
                mouseDownHandlerRegistration = registerHandler(this,
                        MouseDownEvent.getType());
                mouseUpHandlerRegistration = registerHandler(this,
                        MouseUpEvent.getType());
                doubleClickHandlerRegistration = registerHandler(this,
                        DoubleClickEvent.getType());
                contextMenuHandlerRegistration = registerHandler(this,
                        ContextMenuEvent.getType());
            }
        } else {
            if (mouseDownHandlerRegistration != null) {
                // Remove existing handlers
                mouseDownHandlerRegistration.removeHandler();
                mouseUpHandlerRegistration.removeHandler();
                doubleClickHandlerRegistration.removeHandler();
                contextMenuHandlerRegistration.removeHandler();

                mouseDownHandlerRegistration = null;
                mouseUpHandlerRegistration = null;
                doubleClickHandlerRegistration = null;
                contextMenuHandlerRegistration = null;
            }
        }

    }

    /**
     * Registers the given handler to the widget so that the necessary events
     * are passed to this {@link ClickEventHandler}.
     * <p>
     * By default registers the handler with the connector root widget.
     * </p>
     * 
     * @param <H>
     * @param handler
     *            The handler to register
     * @param type
     *            The type of the handler.
     * @return A reference for the registration of the handler.
     */
    protected <H extends EventHandler> HandlerRegistration registerHandler(
            final H handler, DomEvent.Type<H> type) {
        return connector.getWidget().addDomHandler(handler, type);
    }

    /**
     * Checks if there is a server side event listener registered for clicks
     * 
     * @return true if there is a server side event listener registered, false
     *         otherwise
     */
    public boolean hasEventListener() {
        return connector.hasEventListener(clickEventIdentifier);
    }

    /**
     * Event handler for context menu. Prevents the browser context menu from
     * popping up if there is a listener for right clicks.
     */

    public void onContextMenu(ContextMenuEvent event) {
        if (hasEventListener() && shouldFireEvent(event)) {
            // Prevent showing the browser's context menu when there is a right
            // click listener.
            event.preventDefault();
        }
    }

    public void onMouseDown(MouseDownEvent event) {
        /*
         * When getting a mousedown event, we must detect where the
         * corresponding mouseup event if it's on a different part of the page.
         */
        lastMouseDownTarget = event.getNativeEvent().getEventTarget();
        mouseUpPreviewMatched = false;
        mouseUpEventPreviewRegistration = Event
                .addNativePreviewHandler(mouseUpPreviewHandler);
    }

    public void onMouseUp(MouseUpEvent event) {
        /*
         * Only fire a click if the mouseup hits the same element as the
         * corresponding mousedown. This is first checked in the event preview
         * but we can't fire the even there as the event might get canceled
         * before it gets here.
         */
        if (hasEventListener()
                && mouseUpPreviewMatched
                && lastMouseDownTarget != null
                && Util.getElementUnderMouse(event.getNativeEvent()) == lastMouseDownTarget
                && shouldFireEvent(event)) {
            // "Click" with left, right or middle button
            fireClick(event.getNativeEvent());
        }
        mouseUpPreviewMatched = false;
        lastMouseDownTarget = null;
    }
{'fraction_non_alphanumeric': 0.07360959651035986, 'fraction_numerical': 0.00013631406761177754, 'mean_word_length': 2.406779661016949, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7321760', 'n_tokens_mistral': 3553, 'n_tokens_neox': 3404, 'n_words': 938}