Example usage for com.vaadin.client WidgetUtil escapeHTML

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

Introduction

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

Prototype

public static String escapeHTML(String html) 

Source Link

Document

Converts html entities to text.

Usage

From source file:com.haulmont.cuba.web.toolkit.ui.client.jqueryfileupload.CubaFileUploadProgressWindow.java

License:Apache License

public void setCaption(String c, boolean asHtml) {
    String html;//from  w  w w .j a  v  a  2 s . c o  m
    if (asHtml) {
        html = c == null ? "" : c;
    } else {
        html = WidgetUtil.escapeHTML(c);
    }

    headerText.setInnerHTML(html);
}

From source file:com.haulmont.cuba.web.toolkit.ui.client.jqueryfileupload.CubaFileUploadWidget.java

License:Apache License

public CubaFileUploadWidget() {
    submitButton = new VButton();
    submitButton.addClickHandler(new ClickHandler() {
        @Override/*from  w w  w . j  a va2  s.  c  om*/
        public void onClick(ClickEvent event) {
            fireNativeClick(getFileInputElement());
        }
    });
    add(submitButton);
    submitButton.setTabIndex(-1);

    setStyleName(DEFAULT_CLASSNAME);

    Element inputElement = Document.get().createFileInputElement();
    inputElement.setAttribute("name", "files[]");
    inputElement.setAttribute("title", " ");
    listenToFocusEvents(inputElement);

    getElement().appendChild(inputElement);

    fileUpload = new JQueryFileUploadOverlay(this) {
        protected boolean canceled = false;

        @Override
        protected boolean isValidFile(String name, double size) {
            if (fileSizeLimit > 0 && size > fileSizeLimit) {
                if (filePermissionsHandler != null) {
                    filePermissionsHandler.fileSizeLimitExceeded(name);
                }
                return false;
            }

            if (hasInvalidExtension(name)) {
                if (filePermissionsHandler != null) {
                    filePermissionsHandler.fileExtensionNotAllowed(name);
                }
                return false;
            }

            return true;
        }

        protected boolean hasInvalidExtension(String name) {
            if (permittedExtensions != null && !permittedExtensions.isEmpty()) {
                if (name.lastIndexOf(".") > 0) {
                    String fileExtension = name.substring(name.lastIndexOf("."), name.length());
                    return !permittedExtensions.contains(fileExtension.toLowerCase());
                }
                return true;
            }
            return false;
        }

        @Override
        protected void queueUploadStart() {
            // listen to events of new input element
            listenToFocusEvents(getFileInputElement());

            progressWindow = new CubaFileUploadProgressWindow();
            progressWindow.setOwner(CubaFileUploadWidget.this);
            progressWindow.addStyleName(getStylePrimaryName() + "-progresswindow");

            progressWindow.setVaadinModality(true);
            progressWindow.setDraggable(true);
            progressWindow.setResizable(false);
            progressWindow.setClosable(true);

            progressWindow.setCaption(progressWindowCaption);
            progressWindow.setCancelButtonCaption(cancelButtonCaption);

            progressWindow.closeListener = new CubaFileUploadProgressWindow.CloseListener() {
                @Override
                public void onClose() {
                    canceled = true;
                    // null progress to prevent repeated hide() call inside cancelUploading
                    progressWindow = null;

                    cancelUploading();

                    if (queueUploadListener != null) {
                        queueUploadListener.uploadFinished();
                    }
                }
            };

            progressWindow.setVisible(false);
            progressWindow.show();
            progressWindow.center();
            progressWindow.setVisible(true);

            canceled = false;
        }

        @Override
        protected void fileUploadStart(String fileName) {
            if (progressWindow != null) {
                progressWindow.setCurrentFileName(fileName);
            }
        }

        @Override
        protected void fileUploadSucceed(String fileName) {
            if (fileUploadedListener != null) {
                fileUploadedListener.fileUploaded(fileName);
            }
        }

        @Override
        protected void uploadProgress(double loaded, double total) {
            if (progressWindow != null) {
                float ratio = (float) (loaded / total);
                progressWindow.setProgress(ratio);
            }
        }

        @Override
        protected void queueUploadStop() {
            if (progressWindow != null) {
                progressWindow.hide();
                progressWindow = null;
            }

            getFileInputElement().focus();

            if (queueUploadListener != null) {
                queueUploadListener.uploadFinished();
            }
        }

        @Override
        protected void uploadFailed(String textStatus, String errorThrown) {
            if (ignoreExceptions) {
                if (progressWindow != null)
                    progressWindow.hide();
                return;
            }

            if (!canceled) {
                if (unableToUploadFileMessage != null) {
                    // show notification without server round trip, server may be unreachable
                    VNotification notification = VNotification.createNotification(-1,
                            CubaFileUploadWidget.this);
                    String fileName = "";
                    if (progressWindow != null) {
                        fileName = progressWindow.getCurrentFileName();
                    }

                    String message = "<h1>"
                            + WidgetUtil.escapeHTML(unableToUploadFileMessage.replace("%s", fileName))
                            + "</h1>";
                    notification.show(message, Position.MIDDLE_CENTER, "error");
                }

                canceled = true;
                cancelUploading();
            }
        }
    };
}

From source file:com.haulmont.cuba.web.toolkit.ui.client.label.CubaLabelConnector.java

License:Apache License

@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
    // CAUTION copied from superclass
    super.onStateChanged(stateChangeEvent);
    boolean sinkOnloads = false;
    Profiler.enter("LabelConnector.onStateChanged update content");
    switch (getState().contentMode) {
    case PREFORMATTED:
        PreElement preElement = Document.get().createPreElement();
        preElement.setInnerText(getState().text);
        // clear existing content
        getWidget().setHTML("");
        // add preformatted text to dom
        getWidget().getElement().appendChild(preElement);
        break;/*from  w w w  .j  ava  2s. c o  m*/

    case TEXT:
        // clear existing content
        getWidget().setHTML("");
        // set multiline text if needed
        // Haulmont API
        String text = getState().text;
        if (text != null && text.contains("\n")) {
            text = WidgetUtil.escapeHTML(text).replace("\n", "<br/>");
            getWidget().setHTML(text);
        } else {
            getWidget().setText(text);
        }
        break;

    case HTML:
    case RAW:
        sinkOnloads = true;
    case XML:
        getWidget().setHTML(getState().text);
        break;
    default:
        getWidget().setText("");
        break;
    }

    // Haulmont API
    if ("".equals(getWidget().getText()) || getWidget().getText() == null) {
        getWidget().addStyleDependentName("empty");
    } else {
        getWidget().removeStyleDependentName("empty");
    }

    updateIcon();

    Profiler.leave("LabelConnector.onStateChanged update content");

    if (sinkOnloads) {
        Profiler.enter("LabelConnector.onStateChanged sinkOnloads");
        WidgetUtil.sinkOnloadForImages(getWidget().getElement());
        Profiler.leave("LabelConnector.onStateChanged sinkOnloads");
    }
}

From source file:com.haulmont.cuba.web.toolkit.ui.client.menubar.CubaMenuBarWidget.java

License:Apache License

@Override
public String buildItemHTML(UIDL item) {
    // Construct html from the text and the optional icon
    // Haulmont API : Added support for shortcuts
    StringBuilder itemHTML = new StringBuilder();
    if (item.hasAttribute("separator")) {
        itemHTML.append("<span>---</span><span>---</span>");
    } else {/*from   w  w  w . j  a  va  2  s.c o m*/
        itemHTML.append("<span class=\"").append(getStylePrimaryName()).append("-menuitem-caption\">");

        Icon icon = client.getIcon(item.getStringAttribute("icon"));
        if (icon != null) {
            itemHTML.append(icon.getElement().getString());
        }

        String itemText = item.getStringAttribute("text");
        if (!htmlContentAllowed) {
            itemText = WidgetUtil.escapeHTML(itemText);
        }
        itemHTML.append(itemText);
        itemHTML.append("</span>");

        // Add submenu indicator
        if (item.getChildCount() > 0) {
            String bgStyle = "";
            itemHTML.append("<span class=\"").append(getStylePrimaryName()).append("-submenu-indicator\"")
                    .append(bgStyle).append("><span class=\"").append(getStylePrimaryName())
                    .append("-submenu-indicator-icon\"")
                    .append("><span class=\"text\">&#x25BA;</span></span></span>");
        } else {
            itemHTML.append("<span class=\"");
            String shortcut = "";
            if (item.hasAttribute("shortcut")) {
                shortcut = item.getStringAttribute("shortcut");
            } else {
                itemHTML.append(getStylePrimaryName()).append("-menuitem-empty-shortcut ");
            }

            itemHTML.append(getStylePrimaryName()).append("-menuitem-shortcut\">").append(shortcut)
                    .append("</span");
        }
    }
    return itemHTML.toString();
}

From source file:com.haulmont.cuba.web.toolkit.ui.client.tooltip.CubaTooltip.java

License:Apache License

@Override
protected void setTooltipText(TooltipInfo info) {
    super.setTooltipText(info);

    if (info.getTitle() != null && !info.getTitle().isEmpty()) {
        description.removeAttribute("aria-hidden");
    } else {//  w  w  w  .j  a va2 s  .co m
        description.setAttribute("aria-hidden", "true");
    }

    String contextHelp = info.getContextHelp();
    if (contextHelp != null && !contextHelp.isEmpty()) {
        if (info.isContextHelpHtmlEnabled()) {
            contextHelpElement.setInnerHTML(contextHelp);
        } else {
            if (contextHelp.contains("\n")) {
                contextHelp = WidgetUtil.escapeHTML(contextHelp).replace("\n", "<br/>");
                contextHelpElement.setInnerHTML(contextHelp);
            } else {
                contextHelpElement.setInnerText(contextHelp);
            }
        }
        contextHelpElement.getStyle().clearDisplay();
    } else {
        contextHelpElement.setInnerHTML("");
        contextHelpElement.getStyle().setDisplay(Style.Display.NONE);
    }
}

From source file:com.haulmont.cuba.web.widgets.client.addons.contextmenu.ContextMenuConnector.java

License:Apache License

private static String buildItemHTML(MenuItemState state, boolean htmlContentAllowed,
        ApplicationConnection connection) {
    // Construct html from the text and the optional icon
    StringBuffer itemHTML = new StringBuffer();
    if (state.separator) {
        itemHTML.append("<span>---</span>");
    } else {/* w w  w  .  java  2 s  .c  o m*/
        // Add submenu indicator
        if (state.childItems != null && state.childItems.size() > 0) {
            itemHTML.append("<span class=\"v-menubar-submenu-indicator\">&#x25BA;</span>");
        }

        itemHTML.append("<span class=\"v-menubar-menuitem-caption\">");

        if (state.icon != null) {
            Icon icon = connection.getIcon(state.icon.getURL());
            if (icon != null) {
                itemHTML.append(icon.getElement().getString());
            }
        }

        String itemText = state.text;
        if (!htmlContentAllowed) {
            itemText = WidgetUtil.escapeHTML(itemText);
        }
        itemHTML.append(itemText);
        itemHTML.append("</span>");
    }
    return itemHTML.toString();
}

From source file:com.haulmont.cuba.web.widgets.client.capslockindicator.CubaCapsLockIndicatorWidget.java

License:Apache License

protected void setMessageOn(String message) {
    if (message == null || message.length() == 0) {
        removeStyleName("message-off");

        setHTML("<span></span>");
    } else {/*from w  ww .  j  a  v a  2  s  . co  m*/
        removeStyleName("message-off");
        addStyleName("message-on");

        setHTML("<span>" + WidgetUtil.escapeHTML(message) + "</span>");
    }
}

From source file:com.haulmont.cuba.web.widgets.client.capslockindicator.CubaCapsLockIndicatorWidget.java

License:Apache License

protected void setMessageOff(String message) {
    if (message == null || message.length() == 0) {
        removeStyleName("message-on");

        setHTML("<span></span>");
    } else {//w  w  w.j a v  a  2  s.  c  o m
        removeStyleName("message-on");
        addStyleName("message-off");

        setHTML("<span>" + WidgetUtil.escapeHTML(message) + "</span>");
    }
}

From source file:com.haulmont.cuba.web.widgets.client.jqueryfileupload.CubaFileUploadWidget.java

License:Apache License

public CubaFileUploadWidget() {
    submitButton = new VButton();
    submitButton.addClickHandler(new ClickHandler() {
        @Override/*www .  j  a v  a  2 s  .  com*/
        public void onClick(ClickEvent event) {
            fireNativeClick(getFileInputElement());
        }
    });
    add(submitButton);
    submitButton.setTabIndex(-1);

    setStyleName(DEFAULT_CLASSNAME);

    Element inputElement = Document.get().createFileInputElement();
    inputElement.setAttribute("name", "files[]");
    if (!BrowserInfo.get().isIE() && !BrowserInfo.get().isEdge()) {
        inputElement.setAttribute("title", " ");
    }
    listenToFocusEvents(inputElement);

    getElement().appendChild(inputElement);

    fileUpload = new JQueryFileUploadOverlay(this) {
        protected boolean canceled = false;

        @Override
        protected boolean isValidFile(String name, double size) {
            if (fileSizeLimit > 0 && size > fileSizeLimit) {
                if (filePermissionsHandler != null) {
                    filePermissionsHandler.fileSizeLimitExceeded(name);
                }
                return false;
            }

            if (hasInvalidExtension(name)) {
                if (filePermissionsHandler != null) {
                    filePermissionsHandler.fileExtensionNotAllowed(name);
                }
                return false;
            }

            return true;
        }

        protected boolean hasInvalidExtension(String name) {
            if (permittedExtensions != null && !permittedExtensions.isEmpty()) {
                if (name.lastIndexOf(".") > 0) {
                    String fileExtension = name.substring(name.lastIndexOf("."), name.length());
                    return !permittedExtensions.contains(fileExtension.toLowerCase());
                }
                return true;
            }
            return false;
        }

        @Override
        protected void queueUploadStart() {
            // listen to events of new input element
            listenToFocusEvents(getFileInputElement());

            progressWindow = new CubaFileUploadProgressWindow();
            progressWindow.setOwner(CubaFileUploadWidget.this);
            progressWindow.addStyleName(getStylePrimaryName() + "-progresswindow");

            progressWindow.setVaadinModality(true);
            progressWindow.setDraggable(true);
            progressWindow.setResizable(false);
            progressWindow.setClosable(true);

            progressWindow.setCaption(progressWindowCaption);
            progressWindow.setCancelButtonCaption(cancelButtonCaption);

            progressWindow.closeListener = new CubaFileUploadProgressWindow.CloseListener() {
                @Override
                public void onClose() {
                    canceled = true;
                    // null progress to prevent repeated hide() call inside cancelUploading
                    progressWindow = null;

                    cancelUploading();

                    if (queueUploadListener != null) {
                        queueUploadListener.uploadFinished();
                    }
                }
            };

            progressWindow.setVisible(false);
            progressWindow.show();
            progressWindow.center();
            progressWindow.setVisible(true);

            canceled = false;
        }

        @Override
        protected void fileUploadStart(String fileName) {
            if (progressWindow != null) {
                progressWindow.setCurrentFileName(fileName);
            }
        }

        @Override
        protected void fileUploadSucceed(String fileName) {
            if (fileUploadedListener != null) {
                fileUploadedListener.fileUploaded(fileName);
            }
        }

        @Override
        protected void uploadProgress(double loaded, double total) {
            if (progressWindow != null) {
                float ratio = (float) (loaded / total);
                progressWindow.setProgress(ratio);
            }
        }

        @Override
        protected void queueUploadStop() {
            if (progressWindow != null) {
                progressWindow.hide();
                progressWindow = null;
            }

            getFileInputElement().focus();

            if (queueUploadListener != null) {
                queueUploadListener.uploadFinished();
            }
        }

        @Override
        protected void uploadFailed(String textStatus, String errorThrown) {
            if (ignoreExceptions) {
                if (progressWindow != null)
                    progressWindow.hide();
                return;
            }

            if (!canceled) {
                if (unableToUploadFileMessage != null) {
                    // show notification without server round trip, server may be unreachable
                    VNotification notification = VNotification.createNotification(-1,
                            CubaFileUploadWidget.this);
                    String fileName = "";
                    if (progressWindow != null) {
                        fileName = progressWindow.getCurrentFileName();
                    }

                    String message = "<h1>"
                            + WidgetUtil.escapeHTML(unableToUploadFileMessage.replace("%s", fileName))
                            + "</h1>";
                    notification.show(message, Position.MIDDLE_CENTER, "error");
                }

                canceled = true;
                cancelUploading();
            }
        }
    };
}

From source file:com.haulmont.cuba.web.widgets.client.label.CubaLabelConnector.java

License:Apache License

@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
    // CAUTION copied from superclass
    // todo rework! extract extenstion points
    super.onStateChanged(stateChangeEvent);

    boolean sinkOnloads = false;
    Profiler.enter("LabelConnector.onStateChanged update content");
    VLabel widget = getWidget();/*from   www . ja  va  2 s .  c  o m*/
    switch (getState().contentMode) {
    case PREFORMATTED:
        PreElement preElement = Document.get().createPreElement();
        preElement.setInnerText(getState().text);
        // clear existing content
        widget.setHTML("");
        // add preformatted text to dom
        widget.getElement().appendChild(preElement);
        break;

    case TEXT:
        // clear existing content
        widget.setHTML("");
        // set multiline text if needed
        // Haulmont API
        String text = getState().text;
        if (text != null && text.contains("\n")) {
            text = WidgetUtil.escapeHTML(text).replace("\n", "<br/>");
            widget.setHTML(text);
        } else {
            widget.setText(text);
        }
        break;

    case HTML:
        sinkOnloads = true;
        widget.setHTML(getState().text);
        break;
    }

    // Haulmont API
    if ("".equals(getWidget().getText()) || getWidget().getText() == null) {
        getWidget().addStyleDependentName("empty");
    } else {
        getWidget().removeStyleDependentName("empty");
    }

    updateIcon();

    Profiler.leave("LabelConnector.onStateChanged update content");

    if (sinkOnloads) {
        Profiler.enter("LabelConnector.onStateChanged sinkOnloads");
        WidgetUtil.sinkOnloadForImages(widget.getElement());
        Profiler.leave("LabelConnector.onStateChanged sinkOnloads");
    }
}