Example usage for com.vaadin.client Util findConnectorFor

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

Introduction

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

Prototype

public static ComponentConnector findConnectorFor(Widget widget) 

Source Link

Usage

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

License:Apache License

/**
 * Creates a transferable for the Accordion
 * /*from   w w  w  . j a  v  a2 s.  co  m*/
 * @param accordion
 *            The Accordion where the event occurred
 * @param tab
 *            The tab on which the event occurred
 * @param event
 *            The event
 * @param root
 *            The root widget
 * @return
 */
private static VTransferable createAccordionTransferableFromMouseDown(VDDAccordion accordion, StackItem tab,
        NativeEvent event) {
    VTransferable transferable = new VTransferable();
    transferable.setDragSource(Util.findConnectorFor(accordion));
    transferable.setData(Constants.TRANSFERABLE_DETAIL_COMPONENT,
            accordion.getTab(accordion.getTabPosition(tab)));
    transferable.setData(Constants.TRANSFERABLE_DETAIL_INDEX, accordion.getTabPosition(tab));
    transferable.setData(Constants.TRANSFERABLE_DETAIL_MOUSEDOWN,
            MouseEventDetailsBuilder.buildMouseEventDetails(event).serialize());
    return transferable;
}

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

License:Apache License

/**
 * Creates a transferable from a mouse down event. Returns null if creation
 * was not successful.// ww w  .  j  av a  2s .  c o  m
 * 
 * @param event
 *            The mouse down event
 * @param root
 *            The root layout from where the component is dragged
 * @return A transferable or NULL if something failed
 */
public static VTransferable createLayoutTransferableFromMouseDown(NativeEvent event, Widget root,
        Widget target) {

    // NPE check
    if (target == null) {
        VConsole.error("Could not find widget");
        return null;
    }

    VConsole.log("Creating transferable for root:" + root.getElement() + "\t target:" + target.getElement());

    // Special treatment for Tabsheet
    if (root instanceof VDDTabSheet) {
        VDDTabSheet tabsheet = (VDDTabSheet) root;
        TabCaption tab = WidgetUtil.findWidget(target.getElement(), TabCaption.class);
        if (tab != null && tabsheet.getElement().isOrHasChild(tab.getElement())) {
            return createTabsheetTransferableFromMouseDown(tabsheet, tab, event);
        } else {
            // Not a tab
            VConsole.error("Not on tab");
            return null;
        }
    }

    // Special treatment for Accordion
    if (root instanceof VDDAccordion) {
        VDDAccordion accordion = (VDDAccordion) root;
        StackItem tab = WidgetUtil.findWidget(target.getElement(), StackItem.class);
        if (tab != null && accordion.getElement().isOrHasChild(tab.getElement())) {
            return createAccordionTransferableFromMouseDown(accordion, tab, event);
        } else {
            // Not on tab
            VConsole.error("Not on tab");
            return null;
        }
    }

    // Ensure we have the right widget
    target = getTransferableWidget(target);

    // Find the containing layout of the component
    ComponentConnector widgetConnector = Util.findConnectorFor(target);
    if (widgetConnector == null) {
        VConsole.error("No connector found for " + target);
        return null;
    }

    // Iterate until parent either is the root or a layout with drag and
    // drop enabled
    ComponentConnector layoutConnector = (ComponentConnector) widgetConnector.getParent();
    Widget layout = layoutConnector.getWidget();
    while (layout != root && layout != null && layoutConnector != null) {
        if (isDraggingEnabled(layoutConnector, target)) {
            // Found parent layout with support for drag and drop
            break;
        }
        target = layout;
        widgetConnector = layoutConnector;

        layoutConnector = (ComponentConnector) layoutConnector.getParent();
        if (layoutConnector == null) {
            break;
        }

        layout = layoutConnector.getWidget();
    }

    // Consistency check
    if (target == null) {
        VConsole.error("Target was null");
        return null;
    }
    if (root == target) {
        /*
         * Dispatch event again so parent layout can handle the drag of the
         * root
         */
        target.getElement().dispatchEvent(createMouseDownEvent(event));
        return null;
    }
    if (layoutConnector == null) {
        VConsole.error("No layout connector was found");
        return null;
    }

    return createTransferable(layoutConnector, widgetConnector, event);
}

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

License:Apache License

public static Widget getTransferableWidget(Widget w) {

    if (isCaption(w)) {
        // Dragging caption means dragging component the caption belongs to
        Widget owner = null;/* www  .  ja  v  a  2 s . c om*/
        if (w instanceof TabCaption) {
            TabCaption caption = (TabCaption) w;
            owner = caption.getTab().getTabsheet();
        }
        if (w instanceof VCaption) {
            ComponentConnector ownerConnector = ((VCaption) w).getOwner();
            owner = ownerConnector == null ? null : ownerConnector.getWidget();
        } else if (w instanceof VFormLayout.Caption) {
            ComponentConnector ownerConnector = ((VFormLayout.Caption) w).getOwner();
            owner = ownerConnector == null ? null : ownerConnector.getWidget();
        }
        if (owner != null) {
            w = owner;
        }
    } else {
        // Ensure we are dealing with a Vaadin component
        ComponentConnector connector = Util.findConnectorFor(w);
        while (connector == null) {
            w = w.getParent();
            connector = Util.findConnectorFor(w);
        }
    }

    return w;
}

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/* w  ww. j ava  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:com.haulmont.cuba.web.widgets.client.addons.dragdroplayouts.VDragFilter.java

License:Apache License

private ComponentConnector findConnectorFor(Widget widget) {
    if (!isCaptionForAccordion(widget)) {
        return Util.findConnectorFor(widget);
    } else {//  w ww  .  ja  va  2 s  .  c  om
        return findConnectorForAccordionCaption(widget);
    }
}

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

License:Apache License

private ComponentConnector findConnectorForAccordionCaption(Widget widget) {
    StackItem parent = (StackItem) widget.getParent();
    return Util.findConnectorFor(parent.getChildWidget());
}

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

License:Apache License

protected boolean canBeGrabbedRecursive(Widget root, Widget widget) {
    if (widget == root) {
        return true;
    }//w  ww .j  av  a 2s. com

    ComponentConnector connector;
    if (!isCaptionForAccordion(widget)) {
        connector = Util.findConnectorFor(widget);
    } else {
        connector = findConnectorForAccordionCaption(widget);
    }

    if (connector != null && state.nonGrabbable.contains(connector)) {
        return false;
    }

    Widget parent = widget.getParent();
    if (parent == null || parent == root) {
        return true;
    }

    return canBeGrabbedRecursive(root, parent);
}

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

License:Apache License

protected ComponentConnector findConnectorForAccordionCaption(Widget widget) {
    VAccordion.StackItem parent = (VAccordion.StackItem) widget.getParent();
    return Util.findConnectorFor(parent.getChildWidget());
}

From source file:com.haulmont.cuba.web.widgets.client.orderedactionslayout.CubaOrderedLayoutSlot.java

License:Apache License

public void setCaption(String captionText, boolean contextHelpIconEnabled, Icon icon, List<String> styles,
        String error, boolean showError, boolean required, boolean enabled, boolean captionAsHtml) {
    // CAUTION copied from super
    // Caption wrappers
    Widget widget = getWidget();//from ww  w . j  a  v  a  2  s. com
    final Element focusedElement = WidgetUtil.getFocusedElement();
    // By default focus will not be lost
    boolean focusLost = false;
    if (captionText != null || icon != null || error != null || required || contextHelpIconEnabled) {
        if (caption == null) {
            caption = DOM.createDiv();
            captionWrap = DOM.createDiv();
            captionWrap.addClassName(StyleConstants.UI_WIDGET);
            captionWrap.addClassName("v-has-caption");
            getElement().appendChild(captionWrap);
            orphan(widget);
            captionWrap.appendChild(widget.getElement());
            adopt(widget);

            // Made changes to DOM. Focus can be lost if it was in the
            // widget.
            focusLost = (focusedElement == null ? false : widget.getElement().isOrHasChild(focusedElement));
        }
    } else if (caption != null) {
        orphan(widget);
        getElement().appendChild(widget.getElement());
        adopt(widget);
        captionWrap.removeFromParent();
        caption = null;
        captionWrap = null;

        // Made changes to DOM. Focus can be lost if it was in the widget.
        focusLost = (focusedElement == null ? false : widget.getElement().isOrHasChild(focusedElement));
    }

    // Caption text
    if (captionText != null) {
        if (this.captionText == null) {
            this.captionText = DOM.createSpan();
            this.captionText.addClassName("v-captiontext");

            if (caption != null) {
                caption.appendChild(this.captionText);
            }
        }
        if (captionText.trim().equals("")) {
            this.captionText.setInnerHTML("&nbsp;");
        } else {
            if (captionAsHtml) {
                this.captionText.setInnerHTML(captionText);
            } else {
                this.captionText.setInnerText(captionText);
            }
        }
    } else if (this.captionText != null) {
        this.captionText.removeFromParent();
        this.captionText = null;
    }

    // Icon
    if (this.icon != null) {
        this.icon.getElement().removeFromParent();
    }
    if (icon != null) {
        if (caption != null) {
            caption.insertFirst(icon.getElement());
        }
    }
    this.icon = icon;

    // Required
    if (required) {
        if (requiredIcon == null) {
            requiredIcon = DOM.createSpan();
            // TODO decide something better (e.g. use CSS to insert the
            // character)
            requiredIcon.setInnerHTML("*");
            requiredIcon.setClassName("v-required-field-indicator");

            // The star should not be read by the screen reader, as it is
            // purely visual. Required state is set at the element level for
            // the screen reader.
            Roles.getTextboxRole().setAriaHiddenState(requiredIcon, true);
        }
        if (caption != null) {
            caption.appendChild(requiredIcon);
        }
    } else if (requiredIcon != null) {
        requiredIcon.removeFromParent();
        requiredIcon = null;
    }

    // Context Help
    // Haulmont API
    if (contextHelpIconEnabled) {
        if (contextHelpIcon == null) {
            contextHelpIcon = DOM.createSpan();
            // TODO decide something better (e.g. use CSS to insert the character)
            contextHelpIcon.setInnerHTML("?");
            contextHelpIcon.setClassName(CONTEXT_HELP_CLASSNAME);

            ComponentConnector componentConnector = Util.findConnectorFor(widget);
            if (hasContextHelpIconListeners(componentConnector.getState())) {
                contextHelpIcon.addClassName(CONTEXT_HELP_CLICKABLE_CLASSNAME);
            }

            // The question mark should not be read by the screen reader, as it is
            // purely visual. Required state is set at the element level for
            // the screen reader.
            Roles.getTextboxRole().setAriaHiddenState(contextHelpIcon, true);
        }
        if (caption != null) {
            caption.appendChild(contextHelpIcon);

            if (clickHandlerRegistration == null) {
                clickHandlerRegistration = addDomHandler(this, ClickEvent.getType());
            }
        }
    } else {
        if (this.contextHelpIcon != null) {
            this.contextHelpIcon.removeFromParent();
            this.contextHelpIcon = null;
        }

        if (clickHandlerRegistration != null) {
            clickHandlerRegistration.removeHandler();
            clickHandlerRegistration = null;
        }
    }

    // Error
    if (error != null && showError) {
        if (errorIcon == null) {
            errorIcon = DOM.createSpan();
            errorIcon.setClassName("v-errorindicator");
        }
        if (caption != null) {
            caption.appendChild(errorIcon);
        }
    } else if (errorIcon != null) {
        errorIcon.removeFromParent();
        errorIcon = null;
    }

    if (caption != null) {
        // Styles
        caption.setClassName("v-caption");

        if (styles != null) {
            for (String style : styles) {
                caption.addClassName("v-caption-" + style);
            }
        }

        if (enabled) {
            caption.removeClassName("v-disabled");
        } else {
            caption.addClassName("v-disabled");
        }

        // Caption position
        if (captionText != null || icon != null) {
            setCaptionPosition(CaptionPosition.TOP);
        } else {
            setCaptionPosition(CaptionPosition.RIGHT);
        }
    }

    if (focusLost) {
        // Find out what element is currently focused.
        Element currentFocus = WidgetUtil.getFocusedElement();
        if (currentFocus != null && currentFocus.equals(Document.get().getBody())) {
            // Focus has moved to BodyElement and should be moved back to
            // original location. This happened because of adding or
            // removing the captionWrap
            focusedElement.focus();
        } else if (currentFocus != focusedElement) {
            // Focus is either moved somewhere else on purpose or IE has
            // lost it. Investigate further.
            Timer focusTimer = new Timer() {

                @Override
                public void run() {
                    if (WidgetUtil.getFocusedElement() == null) {
                        // This should never become an infinite loop and
                        // even if it does it will be stopped once something
                        // is done with the browser.
                        schedule(25);
                    } else if (WidgetUtil.getFocusedElement().equals(Document.get().getBody())) {
                        // Focus found it's way to BodyElement. Now it can
                        // be restored
                        focusedElement.focus();
                    }
                }
            };
            if (BrowserInfo.get().isIE8()) {
                // IE8 can't fix the focus immediately. It will fail.
                focusTimer.schedule(25);
            } else {
                // Newer IE versions can handle things immediately.
                focusTimer.run();
            }
        }
    }
}

From source file:fi.jasoft.draganddrop.client.DragAndDropConnector.java

License:Apache License

protected void onMouseDown(NativeEvent event) {
    if (isDraggingDisabled()) {
        return;//from www .  j  av a  2s. c  o  m
    }

    Element element = Element.as(event.getEventTarget());
    Widget widget = Util.findWidget(element, null);
    ComponentConnector connector = Util.findConnectorFor(widget);
    if (connector == null) {
        return;
    }

    dragStartEvent = event;
    createDragImage(connector.getWidget().getElement(), true);
    attachDragElement(connector);
    updateDragImagePosition(event);

    currentDraggedComponent = connector;
    getLogger().warning("Started drag");

    Event.setCapture(RootPanel.getBodyElement());

    event.preventDefault();
    event.stopPropagation();
}