Example usage for com.google.gwt.safehtml.shared SafeHtmlBuilder SafeHtmlBuilder

List of usage examples for com.google.gwt.safehtml.shared SafeHtmlBuilder SafeHtmlBuilder

Introduction

In this page you can find the example usage for com.google.gwt.safehtml.shared SafeHtmlBuilder SafeHtmlBuilder.

Prototype

public SafeHtmlBuilder() 

Source Link

Document

Constructs an empty SafeHtmlBuilder.

Usage

From source file:org.zanata.webtrans.client.ui.SearchResultsDocumentTable.java

License:Open Source License

private static SafeHtml buildButtonHtml(String message, String buttonLabel, String title, boolean enabled) {
    SafeHtmlBuilder sb = new SafeHtmlBuilder().appendHtmlConstant(message)
            .appendHtmlConstant("<button type=\"button\" tabindex=\"-1\"");
    if (!enabled) {
        sb.appendHtmlConstant(" title=\"").appendEscaped(title).appendHtmlConstant("\" disabled=\"disabled\"");
    }/*from  ww w  .  j a v a 2  s .com*/
    return sb.appendHtmlConstant(">").appendEscaped(buttonLabel).appendHtmlConstant("</button>").toSafeHtml();
}

From source file:org.zanata.webtrans.client.ui.SearchResultsDocumentTable.java

License:Open Source License

private static SafeHtml buildProcessingIndicator(String message) {
    return new SafeHtmlBuilder().append(spinner).appendHtmlConstant("<br/>").appendHtmlConstant(message)
            .toSafeHtml();//from   w  w  w  . ja va 2  s. c o m
}

From source file:org.zanata.webtrans.client.ui.TransHistoryItemLine.java

License:Open Source License

public TransHistoryItemLine(TransHistoryItem item, TranslationHistoryDisplay.Listener listener,
        ContentStateRenderer stateRenderer) {
    this.item = item;
    this.listener = listener;

    if (item.getModifiedBy().isEmpty()) {
        // before rhbz1149968 modified by person can be empty if translation is
        // pushed from client
        SafeHtml anonymous = template.anonymousUser(messages.anonymousUser());
        heading = new InlineHTML(
                template.anonymousHeading(anonymous, ContentStateToStyleUtil.stateToStyle(item.getStatus()),
                        stateRenderer.render(item.getStatus())));
    } else {/*  w  w w  .  j av  a  2  s . c o  m*/
        SafeHtml username = new SafeHtmlBuilder().appendHtmlConstant(item.getModifiedBy()).toSafeHtml();
        String url = Application.getUserProfileURL(item.getModifiedBy());
        heading = new InlineHTML(
                template.heading(url, username, ContentStateToStyleUtil.stateToStyle(item.getStatus()),
                        stateRenderer.render(item.getStatus())));
    }

    targetContents = new InlineHTML(
            template.targetContent(TextContentsDisplay.asSyntaxHighlight(item.getContents()).toSafeHtml()));
    if (!Strings.isNullOrEmpty(item.getOptionalTag())) {
        revision = new InlineHTML(template.targetRevision(item.getVersionNum(), item.getOptionalTag()));
    } else {
        revision = new InlineHTML("");
    }

    if (!Strings.isNullOrEmpty(item.getRevisionComment())) {
        revisionComment = new InlineHTML(template.revisionComment(item.getRevisionComment()));
    } else {
        revisionComment = new InlineHTML("");
    }

    initWidget(ourUiBinder.createAndBindUi(this));

    if (item.getStatus() == ContentState.Approved || item.getStatus() == ContentState.Rejected) {
        icon.addClassName("i--review");
    } else {
        icon.addClassName("i--translate");
    }
    creationDate.setText(DateUtil.formatShortDate(item.getModifiedDate()));
}

From source file:org.zanata.webtrans.client.ui.ValidationMessagePanelView.java

License:Apache License

@Override
public void updateValidationMessages(Map<ValidationAction, List<String>> messages) {
    if (messages == null || messages.isEmpty()) {
        clear();/*from  w w w .j av  a  2  s  .c  o  m*/
        return;
    }

    this.displayMessages = messages;

    contents.clear();
    int warningCount = 0;
    int errorCount = 0;

    for (Entry<ValidationAction, List<String>> entry : messages.entrySet()) {
        for (String message : entry.getValue()) {
            SafeHtmlBuilder builder = new SafeHtmlBuilder();
            builder.appendEscaped(message);

            HTMLPanel liElement = new HTMLPanel("li", builder.toSafeHtml().asString());
            liElement.setTitle(entry.getKey().getId().getDisplayName());

            if (isErrorLocked(entry.getKey().getRules())) {
                liElement.addStyleName("txt--danger");
                errorCount++;
            } else {
                liElement.addStyleName("txt--warning");
                warningCount++;
            }

            contents.add(liElement);
        }
    }

    headerLabel.setText(this.messages.validationNotificationHeading(warningCount, errorCount));
    removeStyleName("is-hidden");
}

From source file:org.zanata.webtrans.client.ui.ValidationWarningPanel.java

License:Open Source License

private void refreshView(Map<ValidationAction, List<String>> errorMessages) {
    translations.clear();/*from  ww w .j a v a  2s .c  o  m*/
    errorList.clear();
    saveAsFuzzyOpt = false;

    for (String target : targets) {
        SafeHtmlBuilder builder = new SafeHtmlBuilder();
        builder.append(TextContentsDisplay.asSyntaxHighlight(Lists.newArrayList(target)).toSafeHtml());
        translations.add(new HTMLPanel("li", builder.toSafeHtml().asString()));
    }

    for (List<String> messages : errorMessages.values()) {
        for (String message : messages) {
            SafeHtmlBuilder builder = new SafeHtmlBuilder();
            builder.appendEscaped(message);
            errorList.add(new HTMLPanel("li", builder.toSafeHtml().asString()));
        }
    }
}

From source file:org.zanata.webtrans.client.view.AppView.java

License:Open Source License

private static SafeHtml createListItem(String messageStyle, String message) {
    return notificationTemplate.listItem(messageStyle,
            new SafeHtmlBuilder().appendEscaped(message).toSafeHtml());
}

From source file:py.edu.uca.intercajas.client.menumail.Mailboxes.java

License:Apache License

/**
 * Generates HTML for a tree item with an attached icon.
 * // w  w  w. j a  v  a2 s.c o  m
 * @param imageProto the image prototype to use
 * @param title the title of the item
 * @return the resultant HTML
 */
private SafeHtml imageItemHTML(ImageResource imageProto, String title) {
    SafeHtmlBuilder builder = new SafeHtmlBuilder();
    builder.append(AbstractImagePrototype.create(imageProto).getSafeHtml());
    builder.appendHtmlConstant(" ");
    builder.appendEscaped(title);
    return builder.toSafeHtml();
}

From source file:py.edu.uca.intercajas.client.menumail.MailDetail.java

License:Apache License

public void setItem(Destino item) {
    this.item = item;
    subject.setText(item.getMensaje().getReferencia());
    if (item.getMensaje().getRemitente() == null) {
        sender.setText("SistemaIntercajas");
    } else {//ww w .  j a  v a2  s  . c o  m
        sender.setText(item.getMensaje().getRemitente().getSiglas());
    }
    //TODO falta
    recipient.setText(item.getDestinatario().getSiglas());

    // WARNING: For the purposes of this demo, we're using HTML directly, on
    // the assumption that the "server" would have appropriately scrubbed the
    // HTML. Failure to do so would open your application to XSS attacks.
    //body.setHTML(item.body);

    acciones(item);

    //Obtenemos los adjuntos del mensaje
    BeneficiarioService.Util.get().adjuntoFindByMensajeId(item.getMensaje().getId(),
            new MethodCallback<List<Adjunto>>() {
                @Override
                public void onSuccess(Method method, List<Adjunto> response) {
                    panelAdjuntos.clear();
                    for (Adjunto a : response) {
                        panelAdjuntos.add(createDownloadLink(a.getTipo().toString(), a.getRutaArchivo(),
                                a.getNombreArchivo()));
                    }
                }

                @Override
                public void onFailure(Method method, Throwable exception) {
                    new UIErrorRestDialog(method, exception);
                }

            });

    //Actualizamos el cuerpo del mensaje
    if (item.getMensaje().getCuerpo() == null || item.getMensaje().getCuerpo().isEmpty()) {
        body.setHTML("");
    } else {
        body.setHTML(new SafeHtmlBuilder().appendEscapedLines(item.getMensaje().getCuerpo()).toSafeHtml());
    }

    //Aparecen los botones de impresion
    imprimirMensaje.setVisible(true);
    imprimirSolicitudDetalle.setVisible(true);

}

From source file:sk.turn.gwtmvp.gen.ViewGenerator.java

License:Apache License

@Override
public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    try {/* w w  w .  ja va 2 s.c  o  m*/
        TypeOracle typeOracle = context.getTypeOracle();
        JClassType viewType = typeOracle.getType(typeName);
        boolean asyncView = (viewType.getAnnotation(AsyncView.class) != null);
        ViewHtml viewHtml = viewType.getAnnotation(ViewHtml.class);
        String packageName = viewType.getPackage().getName();
        String generatedClassName = viewType.getName().replace('.', '_') + "Impl";
        PrintWriter w = context.tryCreate(logger, packageName, generatedClassName);
        if (w == null) {
            return new RebindResult(RebindMode.USE_ALL_CACHED, packageName + "." + generatedClassName);
        }
        JParameterizedType superType = null;
        String handlerType = null;
        for (JClassType intfc : viewType.getImplementedInterfaces()) {
            if (intfc.getQualifiedSourceName().equals(View.class.getName())
                    || intfc.getQualifiedSourceName().equals(HandlerView.class.getName())) {
                superType = intfc.isParameterized();
                if (intfc.getQualifiedSourceName().equals(HandlerView.class.getName())) {
                    handlerType = superType.getTypeArgs()[1].getQualifiedSourceName();
                }
                break;
            }
        }
        if (superType == null) {
            throw new Exception(typeName + " does not inherit from " + View.class.getName());
        }
        // If the view HTML is not inline, make sure the view HTML file exists
        Resource htmlResource = null;
        if (viewHtml == null) {
            htmlResource = context.getResourcesOracle().getResource(
                    packageName.replace('.', '/') + "/" + viewType.getSimpleSourceName() + ".html");
            if (htmlResource == null) {
                throw new Exception("Cannot find " + viewType.getSimpleSourceName() + ".html");
            }
        }
        String rootElementType = superType.getTypeArgs()[0].getQualifiedSourceName();
        // Map all annotated methods
        Map<String, JMethod> fieldsMap = new LinkedHashMap<>();
        // elemId -> { com.google.gwt.event.dom.client.???Handler -> JMethod, ... }, ...
        Map<String, Map<String, JMethod>> handlersMap = new LinkedHashMap<>();
        for (JMethod method : viewType.getMethods()) {
            // Check element mapping
            HtmlElement elemAnn = method.getAnnotation(HtmlElement.class);
            if (elemAnn != null) {
                if (method.getParameters().length > 0) {
                    throw new Exception("Method " + typeName + "." + method.getName()
                            + "() must have zero parameters (has " + method.getParameters().length + ").");
                }
                String id = elemAnn.value();
                if (id.equals("")) {
                    id = method.getName();
                    if (id.startsWith("get") && id.length() > 3) {
                        id = id.substring(3, 4).toLowerCase() + id.substring(4);
                    }
                }
                fieldsMap.put(id, method);
            }
            // Check handler mapping
            HtmlHandler handlerAnn = method.getAnnotation(HtmlHandler.class);
            if (handlerAnn != null) {
                JParameter param = (method.getParameters().length == 1 ? method.getParameters()[0] : null);
                JClassType paramType = (param != null ? param.getType().isInterface() : null);
                if (paramType == null
                        || !paramType.isAssignableTo(typeOracle.getType(EventHandler.class.getName()))) {
                    throw new Exception("Method " + typeName + "." + method.getName()
                            + "() must have exactly one parameter of an EventHandler subinterface (has "
                            + (paramType == null ? "none" : paramType.getQualifiedSourceName()) + ").");
                }
                for (String val : handlerAnn.value()) {
                    Map<String, JMethod> methods = handlersMap.get(val);
                    if (methods == null) {
                        methods = new LinkedHashMap<>();
                        handlersMap.put(val, methods);
                    }
                    if (methods.containsKey(paramType.getQualifiedSourceName())) {
                        throw new Exception(
                                "Element \"" + val + "\" already has a " + paramType.getName() + " defined.");
                    }
                    methods.put(paramType.getQualifiedSourceName(), method);
                }
            }
        }
        // Check whether dictionary class is defined
        String html = (viewHtml != null ? viewHtml.value()
                : Util.readStreamAsString(htmlResource.openContents()));
        Matcher dictMatcher = Pattern.compile("^<[^<]+data-mvp-dict=\"(.+)\"").matcher(html);
        String dictClassName = null;
        JClassType dictClass = null;
        if (dictMatcher.find()) {
            dictClassName = dictMatcher.group(1);
            if ((dictClass = typeOracle.getType(dictClassName)) == null) {
                throw new Exception("Localization classs " + dictClassName + " does not exist");
            }
        }
        // Check whether data-gwtid is still being used
        if (html.indexOf("data-gwtid=\"") != -1) {
            logger.log(TreeLogger.Type.WARN,
                    "The use of \"data-gwtid\" attribute is deprecated and its support will be removed in future versions, please use \"data-mvp-id\" instead.");
        }
        w.println("package " + packageName + ";");
        w.println();
        w.println("import com.google.gwt.core.client.GWT;");
        w.println("import com.google.gwt.dom.client.Element;");
        w.println("import com.google.gwt.dom.client.Document;");
        w.println("import com.google.gwt.dom.client.NodeList;");
        w.println("import com.google.gwt.event.dom.client.DomEvent;");
        w.println("import com.google.gwt.event.shared.HandlerManager;");
        w.println("import com.google.gwt.resources.client.ClientBundle;");
        w.println("import com.google.gwt.resources.client.ExternalTextResource;");
        w.println("import com.google.gwt.resources.client.ResourceCallback;");
        w.println("import com.google.gwt.resources.client.ResourceException;");
        w.println("import com.google.gwt.resources.client.TextResource;");
        w.println("import com.google.gwt.user.client.Event;");
        w.println("import com.google.gwt.user.client.EventListener;");
        w.println("import java.util.HashMap;");
        w.println("import java.util.Map;");
        w.println("import java.util.logging.Logger;");
        w.println();
        w.println("public class " + generatedClassName + " implements " + viewType.getQualifiedSourceName()
                + " {");
        if (htmlResource != null) {
            w.println("  interface Resources extends ClientBundle {");
            w.println("    Resources INSTANCE = GWT.create(Resources.class);");
            w.println("    @Source(\"" + viewType.getSimpleSourceName() + ".html\") "
                    + (asyncView ? "External" : "") + "TextResource htmlContent();");
            w.println("  }");
            w.println();
        }
        w.println("  private static final Logger LOG = Logger.getLogger(\"" + packageName + "."
                + generatedClassName + "\");");
        if (asyncView) {
            w.println("  private static String sHtml = null;");
        }
        w.println();
        w.println("  private " + rootElementType + " rootElement = null;");
        w.println("  private final Map<String, Element> elementsMap = new HashMap<>();");
        if (handlerType != null) {
            w.println("  private " + handlerType + " handler;");
        }
        w.println();
        w.println("  @Override");
        w.println(
                "  public void loadView(final ViewLoadedHandler<" + rootElementType + "> viewLoadedHandler) {");
        w.println("    if (rootElement != null) {");
        w.println("      viewLoadedHandler.onViewLoaded(rootElement);");
        w.println("    }");
        if (asyncView) {
            w.println("    if (sHtml != null) {");
            w.println("      loadView(new String(sHtml), viewLoadedHandler);");
            w.println("    } else {");
            w.println("      try {");
            w.println(
                    "        Resources.INSTANCE.htmlContent().getText(new ResourceCallback<TextResource>() {");
            w.println("          public void onSuccess(TextResource r) {");
            w.println("            sHtml = r.getText();");
            w.println("            loadView(new String(sHtml), viewLoadedHandler);");
            w.println("          }");
            w.println("          public void onError(ResourceException e) {");
            w.println("            LOG.severe(\"Failed to load " + viewType.getSimpleSourceName()
                    + ".html: \" + e);");
            w.println("            viewLoadedHandler.onViewLoaded(null);");
            w.println("          }");
            w.println("        });");
            w.println("      } catch (ResourceException e) {");
            w.println("        LOG.severe(\"Failed to load " + viewType.getSimpleSourceName()
                    + ".html: \" + e);");
            w.println("        viewLoadedHandler.onViewLoaded(null);");
            w.println("      }");
            w.println("    }");
        } else if (viewHtml != null) {
            w.println("    loadView(\"" + escapeJavaString(viewHtml.value()) + "\", viewLoadedHandler);");
        } else {
            w.println("    loadView(Resources.INSTANCE.htmlContent().getText(), viewLoadedHandler);");
        }
        w.println("  }");
        w.println();
        w.println("  private void loadView(String html, ViewLoadedHandler<" + rootElementType
                + "> viewLoadedHandler) {");
        // Replace any dictionary entries
        new SafeHtmlBuilder();
        if (dictClassName != null) {
            w.println("    " + dictClassName + " dict = GWT.create(" + dictClassName + ".class);");
            w.println("    Object dictEntry;");
            dictMatcher = Pattern.compile("\\{mvpDict\\.([^}]+)\\}").matcher(html);
            Set<String> replacedEntries = new HashSet<>();
            while (dictMatcher.find()) {
                String dictEntry = dictMatcher.group(1);
                if (!replacedEntries.contains(dictEntry)) {
                    try {
                        dictClass.getMethod(dictEntry, new JType[] {});
                    } catch (NotFoundException e) {
                        throw new Exception("Localization method " + dictClassName + "." + dictEntry
                                + "() does not exist.");
                    }
                    w.println("    dictEntry = dict." + dictEntry + "();");
                    w.println("    html = html.replace(\"" + dictMatcher.group(0)
                            + "\", dictEntry instanceof com.google.gwt.safehtml.shared.SafeHtml ? ((com.google.gwt.safehtml.shared.SafeHtml)dictEntry).asString() : dictEntry.toString());");
                    replacedEntries.add(dictEntry);
                }
            }
        }
        w.println("    Element tempElem = Document.get().create"
                + (rootElementType.equals("com.google.gwt.dom.client.TableRowElement") ? "TBody"
                        : rootElementType.equals("com.google.gwt.dom.client.TableCellElement") ? "TR" : "Div")
                + "Element();");
        w.println("    tempElem.setInnerHTML(html);");
        w.println("    rootElement = (" + rootElementType + ") tempElem.getFirstChild();");
        if (dictClassName != null) {
            w.println("    rootElement.removeAttribute(\"data-mvp-dict\");");
        }
        w.println("    addElementToMap(rootElement, elementsMap);");
        w.println("    NodeList<Element> elements = rootElement.getElementsByTagName(\"*\");");
        w.println("    for (int i = 0; i < elements.getLength(); i++) {");
        w.println("      addElementToMap(elements.getItem(i), elementsMap);");
        w.println("    }");
        for (Map.Entry<String, JMethod> entry : fieldsMap.entrySet()) {
            w.println("    if (elementsMap.get(\"" + entry.getKey() + "\") == null) {");
            w.println("      LOG.severe(\"Could not find element with data-mvp-id=\\\"" + entry.getKey()
                    + "\\\" in " + viewType.getSimpleSourceName() + ".html.\");");
            w.println("    }");
        }
        for (Map.Entry<String, Map<String, JMethod>> entry : handlersMap.entrySet()) {
            w.println("    if (elementsMap.get(\"" + entry.getKey() + "\") == null) {");
            w.println("      LOG.severe(\"Could not find element with data-mvp-id=\\\"" + entry.getKey()
                    + "\\\" in " + viewType.getSimpleSourceName() + ".html.\");");
            w.println("    }");
        }
        if (handlerType != null) {
            // Map @HtmlHandlers of enclosing class
            JClassType enclosingType = viewType.getEnclosingType();
            JMethod[] methods = (enclosingType != null ? enclosingType.getMethods() : new JMethod[] {});
            for (JMethod method : methods) {
                HtmlHandler handlerAnnotation = method.getAnnotation(HtmlHandler.class);
                if (handlerAnnotation == null) {
                    continue;
                }
                String eventType = method.getParameters()[0].getType().getQualifiedSourceName();
                String eventHandlerType = eventType.substring(0, eventType.length() - 5) + "Handler";
                String handlerMethod = "on" + eventType.substring(0, eventType.length() - 5)
                        .substring(eventType.lastIndexOf('.') + 1);
                for (String elemId : handlerAnnotation.value()) {
                    w.println("    sk.turn.gwtmvp.client.EventManager.setEventHandler(getElement(\"" + elemId
                            + "\"), " + eventType + ".getType(), new " + eventHandlerType + "() {");
                    w.println("      @Override");
                    w.println("      public void " + handlerMethod + "(" + eventType + " event) {");
                    w.println("        if (handler != null) {");
                    w.println("          try { handler." + method.getName() + "(event); }");
                    w.println("          catch (Exception e) { LOG.severe(\"Invoke of "
                            + enclosingType.getName() + "." + method.getName() + " failed: \" + e); }");
                    w.println("        } else {");
                    w.println("          LOG.severe(\"Ignoring " + enclosingType.getName() + "."
                            + method.getName() + " - no HandlerView.handler set\");");
                    w.println("        }");
                    w.println("      }");
                    w.println("    });");
                }
            }
        }
        w.println("    viewLoadedHandler.onViewLoaded(rootElement);");
        w.println("  }");
        w.println();
        w.println("  @Override");
        w.println("  public " + rootElementType + " getRootElement() {");
        w.println("    return rootElement;");
        w.println("  }");
        w.println();
        w.println("  @Override");
        w.println("  public <E2 extends Element> E2 getElement(String mvpId) {");
        w.println("    return (E2) elementsMap.get(mvpId);");
        w.println("  }");
        if (handlerType != null) {
            w.println();
            w.println("  @Override");
            w.println("  public void setHandler(" + handlerType + " handler) {");
            w.println("    this.handler = handler;");
            w.println("  }");
        }
        for (Map.Entry<String, JMethod> entry : fieldsMap.entrySet()) {
            w.println();
            w.println("  @Override");
            w.println("  public " + entry.getValue().getReturnType().getQualifiedSourceName() + " "
                    + entry.getValue().getName() + "() {");
            w.println("    return elementsMap.get(\"" + entry.getKey() + "\").<"
                    + entry.getValue().getReturnType().getQualifiedSourceName() + ">cast();");
            w.println("  }");
        }
        for (JMethod method : viewType.getMethods()) {
            HtmlHandler handlerAnn = method.getAnnotation(HtmlHandler.class);
            if (handlerAnn == null) {
                continue;
            }
            String paramType = method.getParameters()[0].getType().getQualifiedSourceName();
            String eventType = paramType.substring(0, paramType.length() - 7) + "Event.getType()";
            w.println();
            w.println("  @Override");
            w.println("  public void " + method.getName() + "(" + paramType + " handler) {");
            for (String id : handlerAnn.value()) {
                w.println("    sk.turn.gwtmvp.client.EventManager.setEventHandler(getElement(\"" + id + "\"), "
                        + eventType + ", handler);");
            }
            w.println("  }");
        }
        w.println();
        w.println("  private void addElementToMap(Element element, Map<String, Element> elementsMap) {");
        w.println(
                "    String attrName = (element.hasAttribute(\"data-mvp-id\") ? \"data-mvp-id\" : element.hasAttribute(\"data-gwtid\") ? \"data-gwtid\" : null);");
        w.println("    if (attrName == null) {");
        w.println("      return;");
        w.println("    }");
        w.println("    String mvpId = element.getAttribute(attrName);");
        w.println("    if (!mvpId.equals(\"\")) {");
        w.println("      element.removeAttribute(attrName);");
        w.println("      elementsMap.put(mvpId, element);");
        w.println("    }");
        w.println("  }");
        w.println("}");
        context.commit(logger, w);
        return new RebindResult(RebindMode.USE_ALL_NEW, packageName + "." + generatedClassName);
    } catch (Exception e) {
        logger.log(TreeLogger.Type.ERROR,
                "Failed generating wrapper for class " + typeName + ": " + e.getMessage());
        throw new UnableToCompleteException();
    }
}