Example usage for com.google.gwt.safehtml.shared SafeHtmlUtils fromString

List of usage examples for com.google.gwt.safehtml.shared SafeHtmlUtils fromString

Introduction

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

Prototype

public static SafeHtml fromString(String s) 

Source Link

Document

Returns a SafeHtml containing the escaped string.

Usage

From source file:org.geomajas.gwt2.plugin.corewidget.example.client.sample.message.MessageBox.java

License:Open Source License

/**
 * This will show a MessageBox type Info.
 *//* w  w w  . j a v  a2 s.  com*/
public static MessageBox showMessageBox(String title, String message) {
    return showMessageBox(title, SafeHtmlUtils.fromString(message), MessageStyleType.INFO);
}

From source file:org.geomajas.gwt2.plugin.corewidget.example.client.sample.message.MessageBox.java

License:Open Source License

/**
 * This will show a MessageBox.//from   w  w w  .j a  v a2  s.  com
 */
public static MessageBox showMessageBox(String title, String message, MessageStyleType styleType) {
    return showMessageBox(title, SafeHtmlUtils.fromString(message), styleType);
}

From source file:org.glom.web.client.ui.cell.NumericCell.java

License:Open Source License

public NumericCell(final String foregroundColor, final String backgroundColor, final NumberFormat numberFormat,
        final boolean useAltColorForNegatives, final String currencyCode) {
    if (!StringUtils.isEmpty(foregroundColor)) {
        colorCSSProp = SafeHtmlUtils.fromString("color:" + foregroundColor + ";");
    } else {//  ww  w.j  ava 2  s. c o  m
        colorCSSProp = SafeHtmlUtils.fromSafeConstant("");
    }
    if (!StringUtils.isEmpty(backgroundColor)) {
        backgroundColorCSSProp = SafeHtmlUtils.fromString("background-color:" + backgroundColor + ";");
    } else {
        backgroundColorCSSProp = SafeHtmlUtils.fromSafeConstant("");
    }
    this.numberFormat = numberFormat;
    this.useAltColorForNegatives = useAltColorForNegatives;
    this.currencyCode = StringUtils.isEmpty(currencyCode) ? "" : currencyCode + " ";
}

From source file:org.glom.web.client.ui.cell.NumericCell.java

License:Open Source License

@Override
public void render(final Context context, final Double value, final SafeHtmlBuilder sb) {
    if (value == null) {
        // The value is from an empty row.
        sb.appendHtmlConstant(" ");
        return;//from  w ww . ja v a2 s. c  o  m
    }

    // set the foreground color to red if the number is negative and this is requested
    if (useAltColorForNegatives && value.doubleValue() < 0) {
        // The default alternative color in libglom is red.
        colorCSSProp = SafeHtmlUtils.fromString("color: #FF0000;");
    }

    // Convert the number to a string and set some CSS properties on the text.
    // The overflow and text-overflow properties tell the browser to add an ellipsis when the text overflows the
    // table cell.
    // FIXME this isn't using safe html correctly!
    sb.appendHtmlConstant("<div style=\"overflow: hidden; text-overflow: ellipsis; " + colorCSSProp.asString()
            + backgroundColorCSSProp.asString() + "\">");
    sb.append(SafeHtmlUtils.fromString(currencyCode + numberFormat.format(value)));
    sb.appendHtmlConstant("</div>");

}

From source file:org.glom.web.client.ui.cell.TextCell.java

License:Open Source License

public TextCell(final String foregroundColor, final String backgroundColor) {
    if (!StringUtils.isEmpty(foregroundColor)) {
        colorCSSProp = SafeHtmlUtils.fromString("color:" + foregroundColor + ";");
    } else {/*from  w w w . j  a  v a  2s .c om*/
        colorCSSProp = SafeHtmlUtils.fromSafeConstant("");
    }
    if (!StringUtils.isEmpty(backgroundColor)) {
        backgroundColorCSSProp = SafeHtmlUtils.fromString("background-color:" + backgroundColor + ";");
    } else {
        backgroundColorCSSProp = SafeHtmlUtils.fromSafeConstant("");
    }
}

From source file:org.glom.web.client.ui.cell.TextCell.java

License:Open Source License

@Override
public void render(final Context context, final String value, final SafeHtmlBuilder sb) {
    if (value == null) {
        // The value is from an empty row.
        sb.appendHtmlConstant("&nbsp;");
        return;//  w  w w  . ja v  a 2s. c o  m
    }

    // Set the text and some CSS properties for the text.
    // The overflow and text-overflow properties tell the browser to add an ellipsis when the text overflows the
    // table cell.
    // FIXME this isn't using safe html correctly!
    sb.appendHtmlConstant("<div style=\"overflow: hidden; text-overflow: ellipsis; " + colorCSSProp.asString()
            + backgroundColorCSSProp.asString() + "\">");
    sb.append(SafeHtmlUtils.fromString(value));
    sb.appendHtmlConstant("</div>");

}

From source file:org.glom.web.client.ui.details.DetailsCell.java

License:Open Source License

public void setData(final DataItem dataItem) {
    detailsData.clear();/*from   w  ww.  ja  va  2s .  co m*/

    if (dataItem == null) {
        return;
    }

    Formatting formatting = layoutItem.getFormatting();

    // FIXME use the cell renderers from the list view to render the information here
    switch (this.dataType) {
    case TYPE_BOOLEAN:
        final CheckBox checkBox = new CheckBox();
        checkBox.setValue(dataItem.getBoolean());
        checkBox.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(final ClickEvent event) {
                // don't let users change the checkbox
                checkBox.setValue(dataItem.getBoolean());
            }
        });
        detailsData.add(checkBox);
        break;
    case TYPE_NUMERIC:
        if (formatting == null) {
            GWT.log("setData(): formatting is null");
            formatting = new Formatting(); // To avoid checks later.
        }
        final NumericFormat numericFormat = formatting.getNumericFormat();
        final NumberFormat gwtNumberFormat = Utils.getNumberFormat(numericFormat);

        // set the foreground color to red if the number is negative and this is requested
        if (numericFormat.getUseAltForegroundColorForNegatives() && dataItem.getNumber() < 0) {
            // The default alternative color in libglom is red.
            detailsData.getElement().getStyle()
                    .setColor(NumericFormat.getAlternativeColorForNegativesAsHTMLColor());
        }

        final String currencyCode = StringUtils.isEmpty(numericFormat.getCurrencySymbol()) ? ""
                : numericFormat.getCurrencySymbol().trim() + " ";
        detailsLabel.setText(currencyCode + gwtNumberFormat.format(dataItem.getNumber()));
        detailsData.add(detailsLabel);
        break;
    case TYPE_DATE:
    case TYPE_TIME:
    case TYPE_TEXT:
        final String text = StringUtils.defaultString(dataItem.getText());

        // Deal with multiline text differently than single line text.
        if ((formatting != null) && (formatting.getTextFormatMultilineHeightLines() > 1)) {
            detailsData.getElement().getStyle().setOverflow(Overflow.AUTO);
            // Convert '\n' to <br/> escaping the data so that it won't be rendered as HTML.
            try {
                // JavaScript requires the charsetName to be "UTF-8". CharsetName values that work in Java (such as
                // "UTF8") will not work when compiled to JavaScript.
                final String utf8NewLine = new String(new byte[] { 0x0A }, "UTF-8");
                final String[] lines = text.split(utf8NewLine);
                final SafeHtmlBuilder sb = new SafeHtmlBuilder();
                for (final String line : lines) {
                    sb.append(SafeHtmlUtils.fromString(line));
                    sb.append(SafeHtmlUtils.fromSafeConstant("<br/>"));
                }

                // Manually add the HTML to the detailsData container.
                final DivElement div = Document.get().createDivElement();
                div.setInnerHTML(sb.toSafeHtml().asString());
                detailsData.getElement().appendChild(div);

                // Expand the width of detailsData if a vertical scrollbar has been placed on the inside of the
                // detailsData container.
                final int scrollBarWidth = detailsData.getOffsetWidth() - div.getOffsetWidth();
                if (scrollBarWidth > 0) {
                    // A vertical scrollbar is on the inside.
                    detailsData.setWidth((detailsData.getOffsetWidth() + scrollBarWidth + 4) + "px");
                }

                // TODO Add horizontal scroll bars when detailsData expands beyond its container.

            } catch (final UnsupportedEncodingException e) {
                // If the new String() line throws an exception, don't try to add the <br/> tags. This is unlikely
                // to happen but we should do something if it does.
                detailsLabel.setText(text);
                detailsData.add(detailsLabel);
            }

        } else {
            final SingleLineText textPanel = new SingleLineText(text);
            detailsData.add(textPanel);
        }
        break;
    case TYPE_IMAGE:
        final Image image = new Image();
        final String imageDataUrl = dataItem.getImageDataUrl();
        if (imageDataUrl != null) {
            image.setUrl(imageDataUrl);

            // Set an arbitrary default size:
            // image.setPixelSize(200, 200);
        }

        detailsData.add(image);
        break;
    default:
        break;
    }

    this.dataItem = dataItem;

    // enable the navigation button if it's safe
    if (openButton != null && openButtonHandlerReg != null && this.dataItem != null) {
        openButton.setEnabled(true);
    }

}

From source file:org.glom.web.client.ui.list.ListTable.java

License:Open Source License

private void addColumn(final LayoutItemField layoutItemField) {
    // Setup the default alignment of the column.
    HorizontalAlignmentConstant columnAlignment;
    Formatting formatting = layoutItemField.getFormatting();
    if (formatting == null) {
        GWT.log("addColumn(): Formatting is null for field=" + layoutItemField.getLayoutDisplayName());
        formatting = new Formatting(); // Just to avoid null dereferencing later.
    }/*  ww  w.jav  a 2 s. c o  m*/

    switch (formatting.getHorizontalAlignment()) {
    case HORIZONTAL_ALIGNMENT_LEFT:
        columnAlignment = HasHorizontalAlignment.ALIGN_LEFT;
        break;
    case HORIZONTAL_ALIGNMENT_RIGHT:
        columnAlignment = HasHorizontalAlignment.ALIGN_RIGHT;
        break;
    case HORIZONTAL_ALIGNMENT_AUTO:
    default:
        columnAlignment = HasHorizontalAlignment.ALIGN_DEFAULT;
        break;
    }

    // create a new column
    Column<DataItem[], ?> column = null;
    final int j = cellTable.getColumnCount();
    switch (layoutItemField.getGlomType()) {

    case TYPE_BOOLEAN:
        column = new Column<DataItem[], Boolean>(new BooleanCell()) {
            @Override
            public Boolean getValue(final DataItem[] row) {
                if (row.length == 1 && row[0] == null) {
                    // an empty row
                    return null;
                }

                if (j >= row.length) {
                    GWT.log("addColumn(): j=" + j + " is out of range. length=" + row.length);
                    return null;
                } else {
                    return row[j].getBoolean();
                }
            }
        };
        // override the configured horizontal alignment
        columnAlignment = HasHorizontalAlignment.ALIGN_CENTER;
        break;

    case TYPE_NUMERIC:
        // create a GWT NumberFormat for the column
        final NumericFormat numericFormat = formatting.getNumericFormat();
        final NumberFormat gwtNumberFormat = Utils.getNumberFormat(numericFormat);

        // create the actual column
        column = new Column<DataItem[], Double>(new NumericCell(
                formatting.getTextFormatColorForegroundAsHTMLColor(),
                formatting.getTextFormatColorBackgroundAsHTMLColor(), gwtNumberFormat,
                numericFormat.getUseAltForegroundColorForNegatives(), numericFormat.getCurrencySymbol())) {
            @Override
            public Double getValue(final DataItem[] row) {
                if (row.length == 1 && row[0] == null) {
                    // an empty row
                    return null;
                }

                if (j >= row.length) {
                    GWT.log("addColumn(): j=" + j + " is out of range. length=" + row.length);
                    return null;
                } else {
                    return row[j].getNumber();
                }
            }
        };
        break;
    case TYPE_IMAGE:
        column = new Column<DataItem[], String>(new ImageCell()) {
            @Override
            public String getValue(final DataItem[] row) {
                if (row.length == 1 && row[0] == null) {
                    // an empty row
                    return null;
                }

                if (j >= row.length) {
                    GWT.log("addColumn(): j=" + j + " is out of range. length=" + row.length);
                    return null;
                } else {
                    return row[j].getImageDataUrl();
                }
            }
        };
        // override the configured horizontal alignment
        columnAlignment = HasHorizontalAlignment.ALIGN_CENTER;
        break;

    default:
        // use a text rendering cell for types we don't know about but log an error
        // TODO log error here
    case TYPE_DATE:
    case TYPE_INVALID:
    case TYPE_TIME:
    case TYPE_TEXT:
        column = new Column<DataItem[], String>(
                new TextCell(formatting.getTextFormatColorForegroundAsHTMLColor(),
                        formatting.getTextFormatColorBackgroundAsHTMLColor())) {
            @Override
            public String getValue(final DataItem[] row) {
                if (row.length == 1 && row[0] == null) {
                    // an empty row
                    return null;
                }

                if (j >= row.length) {
                    GWT.log("addColumn(): j=" + j + " is out of range. length=" + row.length);
                    return null;
                } else {
                    return row[j].getText();
                }
            }
        };
        break;
    }

    // set column properties and add to cell cellTable
    column.setHorizontalAlignment(columnAlignment);
    column.setSortable(true);
    cellTable.addColumn(column, new SafeHtmlHeader(SafeHtmlUtils.fromString(layoutItemField.getTitle())));
}

From source file:org.guvnor.messageconsole.client.console.HyperLinkCell.java

License:Apache License

@Override
public void render(final Context context, final HyperLink value, final SafeHtmlBuilder sb) {
    if (value != null) {
        if (value.isLink()) {
            sb.append(/*from w  w w  .j a v  a  2 s  .co  m*/
                    hyperLinkTemplate.hyperLink(SafeHtmlUtils.fromString(value.getLabel()), value.getLabel()));
        } else {
            sb.append(textTemplate.text(value.getLabel(), value.getLabel()));
        }
    }
}

From source file:org.jboss.as.console.client.administration.audit.AuditLogView.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public Widget createWidget() {

    // table//from  w  w  w .j a v a2  s.c o m
    DefaultCellTable<AuditLogItem> table = new DefaultCellTable<AuditLogItem>(6, new AuditLogItemKeyProvider());
    AuditLogItemDataProvider dataProvider = new AuditLogItemDataProvider(beanFactory);
    dataProvider.addDataDisplay(table);
    final SingleSelectionModel<AuditLogItem> selectionModel = new SingleSelectionModel<AuditLogItem>();
    table.setSelectionModel(selectionModel);
    table.setRowCount(dataProvider.store.size(), true);
    DefaultPager pager = new DefaultPager();
    pager.setDisplay(table);

    // columns
    TextColumn<AuditLogItem> dateColumn = new TextColumn<AuditLogItem>() {
        @Override
        public String getValue(final AuditLogItem item) {
            return item.getDate();
        }
    };
    TextColumn<AuditLogItem> userColumn = new TextColumn<AuditLogItem>() {
        @Override
        public String getValue(final AuditLogItem item) {
            return item.getUser() == null ? "" : item.getUser();
        }
    };
    TextColumn<AuditLogItem> accessColumn = new TextColumn<AuditLogItem>() {
        @Override
        public String getValue(final AuditLogItem item) {
            return item.getAccess() == null ? "" : item.getAccess();
        }
    };
    TextColumn<AuditLogItem> remoteAddressColumn = new TextColumn<AuditLogItem>() {
        @Override
        public String getValue(final AuditLogItem item) {
            return item.getRemoteAddress() == null ? "" : item.getRemoteAddress();
        }
    };
    table.addColumn(dateColumn, Console.CONSTANTS.common_label_date());
    table.addColumn(userColumn, Console.CONSTANTS.common_label_user());
    table.addColumn(accessColumn, "Access");
    table.addColumn(remoteAddressColumn, "Remote Address");

    // basic attributes
    Form<AuditLogItem> basicsForm = new Form<AuditLogItem>(AuditLogItem.class);
    TextItem dateField = new TextItem("date", Console.CONSTANTS.common_label_date());
    TextItem userField = new TextItem("user", Console.CONSTANTS.common_label_user());
    TextItem accessField = new TextItem("access", "Access");
    TextItem domainUUIDField = new TextItem("domainUUID", "Domain UUID");
    TextItem remoteAddressField = new TextItem("remote-address", "Remote Address");
    CheckBoxItem booting = new CheckBoxItem("booting", "Booting");
    CheckBoxItem readOnly = new CheckBoxItem("r/o", "Read-only");
    CheckBoxItem success = new CheckBoxItem("success", "Success");
    basicsForm.setFields(dateField, userField, accessField, domainUUIDField, remoteAddressField, booting,
            readOnly, success);
    basicsForm.setEnabled(false);
    basicsForm.bind(table);
    VerticalPanel basicsPanel = new VerticalPanel();
    basicsPanel.setStyleName("fill-layout-width");
    basicsPanel.add(new AuditHelpPanel().asWidget());
    basicsPanel.add(basicsForm);

    // operations
    VerticalPanel operationsPanel = new VerticalPanel();
    operationsPanel.setStyleName("fill-layout-width");
    final Code code = new Code(Code.Language.JAVASCRIPT, false);
    operationsPanel.add(code);

    // form tabs
    TabPanel forms = new TabPanel();
    forms.setStyleName("default-tabpanel");
    forms.addStyleName("master_detail-detail");
    forms.add(basicsPanel, Console.CONSTANTS.common_label_attributes());
    forms.add(operationsPanel, Console.CONSTANTS.common_label_operations());
    forms.selectTab(0);

    // update operations upon selection
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
        @Override
        public void onSelectionChange(final SelectionChangeEvent event) {
            code.clear();
            AuditLogItem item = selectionModel.getSelectedObject();
            if (item != null) {
                JSONArray jsonArray = JSONParser.parseStrict(item.getOperations().getPayload()).isArray();
                if (jsonArray != null) {
                    String stringify = stringify(jsonArray.getJavaScriptObject());
                    code.setValue(SafeHtmlUtils.fromString(stringify));
                }
            }
        }
    });

    // setup layout
    VerticalPanel main = new VerticalPanel();
    main.setStyleName("rhs-content-panel");
    main.add(new ContentHeaderLabel("Audit Log"));
    main.add(new ContentDescription(Console.CONSTANTS.administration_audit_log_desc()));
    main.add(table);
    main.add(pager);
    main.add(forms);

    ScrollPanel scroll = new ScrollPanel(main);
    LayoutPanel layout = new LayoutPanel();
    layout.add(scroll);
    layout.setWidgetTopHeight(scroll, 0, Style.Unit.PX, 100, Style.Unit.PCT);

    DefaultTabLayoutPanel root = new DefaultTabLayoutPanel(40, Style.Unit.PX);
    root.addStyleName("default-tabpanel");
    root.add(layout, "Audit Log");
    root.selectTab(0);
    return root;
}