Example usage for com.vaadin.client Util isTouchEvent

List of usage examples for com.vaadin.client Util isTouchEvent

Introduction

In this page you can find the example usage for com.vaadin.client Util isTouchEvent.

Prototype

@Deprecated
public static boolean isTouchEvent(NativeEvent event) 

Source Link

Usage

From source file:com.haulmont.cuba.web.widgets.client.addons.dragdroplayouts.ui.VLayoutDragDropMouseHandler.java

License:Apache License

/**
 * Called when the dragging a component should be initiated by both a mouse
 * down event as well as a touch start event
 *
 * FIXME This method is a BIG hack to circumvent Vaadin's very poor client
 * side API's. This will break often. Refactor once Vaadin gets a grip.
 *
 * @param event//from   ww w  .j  a  v  a  2 s.  c  o m
 */
protected void initiateDrag(NativeEvent event) {
    // Check that dragging is enabled
    if (dragMode == LayoutDragMode.NONE) {
        return;
    }

    // Dragging can only be done with left mouse button and no modifier keys
    if (!isMouseDragEvent(event) && !Util.isTouchEvent(event)) {
        return;
    }

    // Get target widget
    EventTarget eventTarget = event.getEventTarget();
    Element targetElement = Element.as(eventTarget);
    Widget target = WidgetUtil.findWidget(targetElement, null);

    if (isEventOnScrollBar(event)) {
        return;
    }

    // do not drag close button of TabSheet tab
    if (isElementNotDraggable(targetElement)) {
        VDragAndDropManager.get().interruptDrag();
        return;
    }

    // Abort if drag mode is caption mode and widget is not a caption
    boolean isPanelCaption = target instanceof VPanel
            && targetElement.getParentElement().getClassName().contains("v-panel-caption");
    boolean isCaption = isPanelCaption || VDragDropUtil.isCaptionOrCaptionless(target);

    if (dragMode == LayoutDragMode.CAPTION && !isCaption) {
        /*
         * Ensure target is a caption in caption mode
         */
        return;
    }

    if (dragMode == LayoutDragMode.CAPTION && isCaption) {

        /*
         * Ensure that captions in nested layouts don't get accepted if in
         * caption mode
         */

        Widget w = VDragDropUtil.getTransferableWidget(target);
        ComponentConnector c = Util.findConnectorFor(w);
        ComponentConnector parent = (ComponentConnector) c.getParent();
        if (parent.getWidget() != root) {
            return;
        }
    }

    // Create the transfarable
    VTransferable transferable = VDragDropUtil.createLayoutTransferableFromMouseDown(event, root, target);

    // Are we trying to drag the root layout
    if (transferable == null) {
        VConsole.log("Creating transferable on mouse down returned null");
        return;
    }

    // Resolve the component
    final Widget w;
    ComponentConnector c = null, parent = null;

    if (target instanceof TabCaption) {
        TabCaption tabCaption = (TabCaption) target;
        Tab tab = tabCaption.getTab();
        int tabIndex = ((ComplexPanel) tab.getParent()).getWidgetIndex(tab);
        VTabsheet tabsheet = tab.getTabsheet();

        w = tab;
        c = tabsheet.getTab(tabIndex);
        parent = Util.findConnectorFor(tabsheet);

    } else if (root instanceof VDDAccordion) {
        w = target;
        parent = Util.findConnectorFor(root);

        StackItem tab = WidgetUtil.findWidget(targetElement, StackItem.class);
        if (tab != null && root.getElement().isOrHasChild(tab.getElement())) {
            c = ((VDDAccordion) root).getTab(((VDDAccordion) root).getTabPosition(tab));
        }

    } else if (transferable.getData(Constants.TRANSFERABLE_DETAIL_COMPONENT) != null) {

        ComponentConnector connector = (ComponentConnector) transferable
                .getData(Constants.TRANSFERABLE_DETAIL_COMPONENT);
        w = connector.getWidget();
        c = Util.findConnectorFor(w);
        parent = (ComponentConnector) c.getParent();

    } else {
        // Failsafe if no widget was found
        w = root;
        c = Util.findConnectorFor(w);
        parent = (ComponentConnector) c.getParent();
        VConsole.log("Could not resolve component, using root as component");
    }

    VConsole.log("Dragging widget: " + w);
    VConsole.log(" in parent: " + parent);

    // Ensure component is draggable
    if (!VDragDropUtil.isDraggingEnabled(parent, w)) {
        VConsole.log("Dragging disabled for " + w.getClass().getName() + " in "
                + parent.getWidget().getClass().getName());
        VDragAndDropManager.get().interruptDrag();
        return;
    }

    // Announce drag start to listeners
    for (DragStartListener dl : dragStartListeners) {
        if (!dl.dragStart(w, dragMode)) {
            VDragAndDropManager.get().interruptDrag();
            return;
        }
    }

    currentDraggedWidget = w;

    // Announce to handler that we are starting a drag operation
    VDragEvent currentDragEvent = VDragAndDropManager.get().startDrag(transferable, event, true);

    /*
     * Create the drag image
     */
    boolean hasDragCaption = false;

    com.google.gwt.dom.client.Element dragImageElement = null;
    if (root instanceof VHasDragCaptionProvider) {
        VDragCaptionProvider dragCaptionProvider = ((VHasDragCaptionProvider) root).getDragCaptionProvider();
        if (dragCaptionProvider != null) {
            hasDragCaption = true;
            dragImageElement = dragCaptionProvider.getDragCaptionElement(currentDraggedWidget);
        }
    }

    if (!hasDragCaption && dragImageProvider != null) {
        dragImageElement = dragImageProvider.getDragImageElement(w);
    }

    if (dragImageElement != null) {

        // Set stylename to proxy component as well
        if (hasDragCaption) {
            dragImageElement.addClassName(ACTIVE_DRAG_CUSTOM_IMAGE_STYLENAME);
        } else {
            dragImageElement.addClassName(ACTIVE_DRAG_SOURCE_STYLENAME);
        }

    } else if (root instanceof VCssLayout) {
        /*
         * CSS Layout does not have an enclosing div so we just use the
         * component div
         */
        dragImageElement = w.getElement();

    } else if (root instanceof VTabsheet) {
        /*
         * Tabsheet should use the dragged tab as a drag image
         */
        dragImageElement = targetElement;

    } else if (root instanceof VAccordion) {
        /*
         * Accordion should use the dragged tab as a drag image
         */
        dragImageElement = targetElement;

    } else if (root instanceof VFormLayout) {
        /*
         * Dragging a component in a form layout should include the caption
         * and error indicator as well
         */
        Element rowElement = (Element) VDDFormLayout
                .getRowFromChildElement((com.google.gwt.dom.client.Element) w.getElement().cast(),
                        (com.google.gwt.dom.client.Element) root.getElement().cast())
                .cast();

        dragImageElement = rowElement;

    } else {
        /*
         * For other layouts we just use the target element;
         */
        dragImageElement = w.getElement();
    }

    Element clone;
    if (hasDragCaption) {
        currentDragEvent.setDragImage(dragImageElement);
        clone = dragImageElement;
    } else {
        currentDragEvent.createDragImage(dragImageElement, true);
        clone = currentDragEvent.getDragImage();
    }

    assert (clone != null);

    // Lock drag image dimensions
    if (!hasDragCaption) {
        clone.getStyle().setWidth(dragImageElement.getOffsetWidth(), Style.Unit.PX);
        clone.getStyle().setHeight(dragImageElement.getOffsetHeight(), Style.Unit.PX);
    }

    if (c != null && c.delegateCaptionHandling() && !(root instanceof VTabsheet)
            && !(root instanceof VAccordion)) {
        /*
         * Captions are not being dragged with the widget since they are
         * separate. Manually add a clone of the caption to the drag image.
         */
        if (target instanceof VCaption) {
            clone.insertFirst(targetElement.cloneNode(true));
        }
    }

    if (BrowserInfo.get().isIE()) {
        // Fix IE not aligning the drag image correctly when dragging
        // layouts
        clone.getStyle().setPosition(Position.ABSOLUTE);
    }

    currentDraggedWidget.addStyleName(ACTIVE_DRAG_SOURCE_STYLENAME);

    // Listen to mouse up for cleanup
    mouseUpHandlerReg = Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
        @Override
        public void onPreviewNativeEvent(NativePreviewEvent event) {
            if (event.getTypeInt() == Event.ONMOUSEUP || event.getTypeInt() == Event.ONTOUCHEND
                    || event.getTypeInt() == Event.ONTOUCHCANCEL) {
                if (mouseUpHandlerReg != null) {
                    mouseUpHandlerReg.removeHandler();
                    if (currentDraggedWidget != null) {

                        currentDraggedWidget.removeStyleName(ACTIVE_DRAG_SOURCE_STYLENAME);

                        if (dragImageProvider != null) {
                            com.google.gwt.dom.client.Element dragImageElement = dragImageProvider
                                    .getDragImageElement(currentDraggedWidget);
                            if (dragImageElement != null) {
                                dragImageElement.removeClassName(ACTIVE_DRAG_SOURCE_STYLENAME);
                            }
                        }

                        currentDraggedWidget = null;
                    }
                }

                // Ensure capturing is turned off at mouse up
                Event.releaseCapture(RootPanel.getBodyElement());
            }
        }
    });

}

From source file:fi.jasoft.dragdroplayouts.client.ui.VLayoutDragDropMouseHandler.java

License:Apache License

/**
 * Called when the dragging a component should be initiated by both a mouse
 * down event as well as a touch start event
 * /*from   w  w  w  .  j av a  2s.c om*/
 * FIXME This method is a BIG hack to circumvent Vaadin's very poor client
 * side API's. This will break often. Refactor once Vaadin gets a grip.
 * 
 * @param event
 */
protected void initiateDrag(NativeEvent event) {
    // Check that dragging is enabled
    if (dragMode == LayoutDragMode.NONE) {
        return;
    }

    // Dragging can only be done with left mouse button and no modifier keys
    if (!isMouseDragEvent(event) && !Util.isTouchEvent(event)) {
        return;
    }

    // Get target widget
    EventTarget eventTarget = event.getEventTarget();
    Element targetElement = Element.as(eventTarget);
    Widget target = WidgetUtil.findWidget(targetElement, null);

    if (isEventOnScrollBar(event)) {
        return;
    }

    // do not drag close button of TabSheet tab
    if (isElementNotDraggable(targetElement)) {
        VDragAndDropManager.get().interruptDrag();
        return;
    }

    // Abort if drag mode is caption mode and widget is not a caption
    boolean isPanelCaption = target instanceof VPanel
            && targetElement.getParentElement().getClassName().contains("v-panel-caption");
    boolean isCaption = isPanelCaption || VDragDropUtil.isCaptionOrCaptionless(target);

    if (dragMode == LayoutDragMode.CAPTION && !isCaption) {
        /*
         * Ensure target is a caption in caption mode
         */
        return;
    }

    if (dragMode == LayoutDragMode.CAPTION && isCaption) {

        /*
         * Ensure that captions in nested layouts don't get accepted if in
         * caption mode
         */

        Widget w = VDragDropUtil.getTransferableWidget(target);
        ComponentConnector c = Util.findConnectorFor(w);
        ComponentConnector parent = (ComponentConnector) c.getParent();
        if (parent.getWidget() != root) {
            return;
        }
    }

    // Create the transfarable
    VTransferable transferable = VDragDropUtil.createLayoutTransferableFromMouseDown(event, root, target);

    // Are we trying to drag the root layout
    if (transferable == null) {
        VConsole.log("Creating transferable on mouse down returned null");
        return;
    }

    // Resolve the component
    final Widget w;
    ComponentConnector c = null, parent = null;

    if (target instanceof TabCaption) {
        TabCaption tabCaption = (TabCaption) target;
        Tab tab = tabCaption.getTab();
        int tabIndex = ((ComplexPanel) tab.getParent()).getWidgetIndex(tab);
        VTabsheet tabsheet = tab.getTabsheet();

        w = tab;
        c = tabsheet.getTab(tabIndex);
        parent = Util.findConnectorFor(tabsheet);

    } else if (root instanceof VDDAccordion) {
        w = target;
        parent = Util.findConnectorFor(root);

        StackItem tab = WidgetUtil.findWidget(targetElement, StackItem.class);
        if (tab != null && root.getElement().isOrHasChild(tab.getElement())) {
            c = ((VDDAccordion) root).getTab(((VDDAccordion) root).getTabPosition(tab));
        }

    } else if (transferable.getData(Constants.TRANSFERABLE_DETAIL_COMPONENT) != null) {

        ComponentConnector connector = (ComponentConnector) transferable
                .getData(Constants.TRANSFERABLE_DETAIL_COMPONENT);
        w = connector.getWidget();
        c = Util.findConnectorFor(w);
        parent = (ComponentConnector) c.getParent();

    } else {
        // Failsafe if no widget was found
        w = root;
        c = Util.findConnectorFor(w);
        parent = (ComponentConnector) c.getParent();
        VConsole.log("Could not resolve component, using root as component");
    }

    VConsole.log("Dragging widget: " + w);
    VConsole.log(" in parent: " + parent);

    // Ensure component is draggable
    if (!VDragDropUtil.isDraggingEnabled(parent, w)) {
        VConsole.log("Dragging disabled for " + w.getClass().getName() + " in "
                + parent.getWidget().getClass().getName());
        VDragAndDropManager.get().interruptDrag();
        return;
    }

    // Announce drag start to listeners
    for (DragStartListener dl : dragStartListeners) {
        if (!dl.dragStart(w, dragMode)) {
            VDragAndDropManager.get().interruptDrag();
            return;
        }
    }

    currentDraggedWidget = w;

    // Announce to handler that we are starting a drag operation
    VDragEvent currentDragEvent = VDragAndDropManager.get().startDrag(transferable, event, true);

    /*
     * Create the drag image
     */
    com.google.gwt.dom.client.Element dragImageElement = dragImageProvider == null ? null
            : dragImageProvider.getDragImageElement(w);

    if (dragImageElement != null) {

        // Set stylename to proxy component as well
        dragImageElement.addClassName(ACTIVE_DRAG_SOURCE_STYLENAME);

    } else if (root instanceof VCssLayout) {
        /*
         * CSS Layout does not have an enclosing div so we just use the
         * component div
         */
        dragImageElement = w.getElement();

    } else if (root instanceof VTabsheet) {
        /*
         * Tabsheet should use the dragged tab as a drag image
         */
        dragImageElement = targetElement;

    } else if (root instanceof VAccordion) {
        /*
         * Accordion should use the dragged tab as a drag image
         */
        dragImageElement = targetElement;

    } else if (root instanceof VFormLayout) {
        /*
         * Dragging a component in a form layout should include the caption
         * and error indicator as well
         */
        Element rowElement = (Element) VDDFormLayout
                .getRowFromChildElement((com.google.gwt.dom.client.Element) w.getElement().cast(),
                        (com.google.gwt.dom.client.Element) root.getElement().cast())
                .cast();

        dragImageElement = rowElement;

    } else {
        /*
         * For other layouts we just use the target element;
         */
        dragImageElement = w.getElement();
    }

    currentDragEvent.createDragImage(dragImageElement, true);
    Element clone = currentDragEvent.getDragImage();
    assert (clone != null);

    // Lock drag image dimensions
    clone.getStyle().setWidth(dragImageElement.getOffsetWidth(), Unit.PX);
    clone.getStyle().setHeight(dragImageElement.getOffsetHeight(), Unit.PX);

    if (c != null && c.delegateCaptionHandling() && !(root instanceof VTabsheet)
            && !(root instanceof VAccordion)) {
        /*
         * Captions are not being dragged with the widget since they are
         * separate. Manually add a clone of the caption to the drag image.
         */
        if (target instanceof VCaption) {
            clone.insertFirst(targetElement.cloneNode(true));
        }
    }

    if (BrowserInfo.get().isIE()) {
        // Fix IE not aligning the drag image correctly when dragging
        // layouts
        clone.getStyle().setPosition(Position.ABSOLUTE);
    }

    currentDraggedWidget.addStyleName(ACTIVE_DRAG_SOURCE_STYLENAME);

    // Listen to mouse up for cleanup
    mouseUpHandlerReg = Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
        @Override
        public void onPreviewNativeEvent(NativePreviewEvent event) {
            if (event.getTypeInt() == Event.ONMOUSEUP || event.getTypeInt() == Event.ONTOUCHEND
                    || event.getTypeInt() == Event.ONTOUCHCANCEL) {
                if (mouseUpHandlerReg != null) {
                    mouseUpHandlerReg.removeHandler();
                    if (currentDraggedWidget != null) {

                        currentDraggedWidget.removeStyleName(ACTIVE_DRAG_SOURCE_STYLENAME);

                        if (dragImageProvider != null) {
                            com.google.gwt.dom.client.Element dragImageElement = dragImageProvider
                                    .getDragImageElement(currentDraggedWidget);
                            if (dragImageElement != null) {
                                dragImageElement.removeClassName(ACTIVE_DRAG_SOURCE_STYLENAME);
                            }
                        }

                        currentDraggedWidget = null;
                    }
                }

                // Ensure capturing is turned off at mouse up
                Event.releaseCapture(RootPanel.getBodyElement());
            }
        }
    });

}