Example usage for com.vaadin.client WidgetUtil getFocusedElement

List of usage examples for com.vaadin.client WidgetUtil getFocusedElement

Introduction

In this page you can find the example usage for com.vaadin.client WidgetUtil getFocusedElement.

Prototype

public static native Element getFocusedElement()
;

Source Link

Document

Gets the currently focused element.

Usage

From source file:com.haulmont.cuba.web.toolkit.ui.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  av a  2  s.c  o  m
    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);

            // 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:com.haulmont.cuba.web.toolkit.ui.client.renderers.componentrenderer.componentcellkey.EscKeyDownHandler.java

License:Apache License

@Override
public void onKeyDown(KeyDownEvent keyDownEvent) {

    if (keyDownEvent.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {

        SimplePanel panel = WidgetUtil.findWidget(WidgetUtil.getFocusedElement(), SimplePanel.class);

        if (panel != null) {
            WidgetUtil.focus(panel.getParent().getElement());

            // prevent further bubbling of the event as it has only
            // navigational purpose if thrown at this depth. A subsequent
            // press of ESC is not stopped, so ESC abort actions of the
            // application should work as soon as the focus is in
            // "navigational mode" on a grid-cell
            keyDownEvent.preventDefault();
            keyDownEvent.stopPropagation();
        }//w w w  .j a  v  a2s. c  o  m
    }
}

From source file:com.haulmont.cuba.web.toolkit.ui.client.renderers.componentrenderer.focuspreserve.FocusPreservingRefreshClientRpcImpl.java

License:Apache License

@Override
public void saveFocus() {
    SimplePanel panel = WidgetUtil.findWidget(WidgetUtil.getFocusedElement(), SimplePanel.class);

    if (panel != null) {
        focus = panel.getParent().getElement();
    }//from   w w w  . ja v a2s . c  o  m
}

From source file:com.haulmont.cuba.web.toolkit.ui.client.tableshared.TableWidgetDelegate.java

License:Apache License

public void showPresentationEditorPopup(Event event, Widget presentationsEditIcon) {
    if (event.getEventTarget().cast() == presentationsEditIcon.getElement() && tableWidget.isEnabled()) {
        this.presentationsEditorPopup = new VOverlay();
        this.presentationsEditorPopup.setStyleName("c-table-prefs-editor");
        this.presentationsEditorPopup.setOwner(table);
        this.presentationsEditorPopup.setWidget(this.presentationsMenu);

        // Store the currently focused element, which will be re-focused when
        // context menu is closed
        Element focusedElement = WidgetUtil.getFocusedElement();

        this.presentationsEditorPopup.addCloseHandler(e -> {
            Element currentFocus = WidgetUtil.getFocusedElement();
            if (focusedElement != null
                    && (currentFocus == null || presentationsEditorPopup.getElement().isOrHasChild(currentFocus)
                            || RootPanel.getBodyElement().equals(currentFocus))) {
                focusedElement.focus();/*w  w  w  .  j  a v a 2  s  . co  m*/
            }

            presentationsEditorPopup = null;
        });

        this.presentationsEditorPopup.setAutoHideEnabled(true);
        this.presentationsEditorPopup.showRelativeTo(presentationsEditIcon);
    }
}

From source file:com.haulmont.cuba.web.toolkit.ui.client.tableshared.TableWidgetDelegate.java

License:Apache License

public void showContextMenuPopup(int left, int top) {
    if (this.customContextMenu instanceof HasWidgets) {
        if (!((HasWidgets) this.customContextMenu).iterator().hasNext()) {
            // there are no actions to show
            return;
        }//from   w  ww . j  a  va  2  s .com
    }

    // Store the currently focused element, which will be re-focused when
    // context menu is closed
    Element focusedElement = WidgetUtil.getFocusedElement();

    this.customContextMenuPopup = Tools.createCubaTableContextMenu();
    this.customContextMenuPopup.setOwner(table);
    this.customContextMenuPopup.setWidget(this.customContextMenu);

    this.customContextMenuPopup.addCloseHandler(e -> {
        Element currentFocus = WidgetUtil.getFocusedElement();
        if (focusedElement != null
                && (currentFocus == null || customContextMenuPopup.getElement().isOrHasChild(currentFocus)
                        || RootPanel.getBodyElement().equals(currentFocus))) {
            focusedElement.focus();
        }

        customContextMenuPopup = null;
    });

    Tools.showPopup(this.customContextMenuPopup, left, top);
}

From source file:com.haulmont.cuba.web.toolkit.ui.client.tableshared.TableWidgetDelegate.java

License:Apache License

public void showCustomPopup() {
    if (this.customPopupWidget != null) {
        if (this.customPopupWidget instanceof HasWidgets) {
            if (!((HasWidgets) this.customPopupWidget).iterator().hasNext()) {
                // there are no component to show
                return;
            }/*from   w  w w.j  a v  a2s  .c  om*/
        }

        // Store the currently focused element, which will be re-focused when
        // context menu is closed
        Element focusedElement = WidgetUtil.getFocusedElement();

        this.customPopupOverlay = Tools.createCubaTablePopup(this.customPopupAutoClose);
        this.customPopupOverlay.setOwner(table);
        this.customPopupOverlay.setWidget(this.customPopupWidget);

        this.customPopupOverlay.addCloseHandler(e -> {

            Element currentFocus = WidgetUtil.getFocusedElement();
            if (focusedElement != null
                    && (currentFocus == null || customPopupOverlay.getElement().isOrHasChild(currentFocus)
                            || RootPanel.getBodyElement().equals(currentFocus))) {
                focusedElement.focus();
            }

            customPopupOverlay = null;
        });

        Tools.showPopup(this.customPopupOverlay, this.lastClickClientX, this.lastClickClientY);
    }
}

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();//w ww  . ja  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();
            }
        }
    }
}