Search in sources :

Example 86 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class WTextAreaRenderer method doRender.

/**
 * Paints the given WTextArea.
 *
 * @param component the WTextArea to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WTextArea textArea = (WTextArea) component;
    XmlStringBuilder xml = renderContext.getWriter();
    boolean readOnly = textArea.isReadOnly();
    xml.appendTagOpen("ui:textarea");
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("hidden", textArea.isHidden(), "true");
    if (readOnly) {
        xml.appendAttribute("readOnly", "true");
    } else {
        int cols = textArea.getColumns();
        int rows = textArea.getRows();
        int minLength = textArea.getMinLength();
        int maxLength = textArea.getMaxLength();
        WComponent submitControl = textArea.getDefaultSubmitButton();
        String submitControlId = submitControl == null ? null : submitControl.getId();
        xml.appendOptionalAttribute("disabled", textArea.isDisabled(), "true");
        xml.appendOptionalAttribute("required", textArea.isMandatory(), "true");
        xml.appendOptionalAttribute("minLength", minLength > 0, minLength);
        xml.appendOptionalAttribute("maxLength", maxLength > 0, maxLength);
        xml.appendOptionalAttribute("toolTip", textArea.getToolTip());
        xml.appendOptionalAttribute("accessibleText", textArea.getAccessibleText());
        xml.appendOptionalAttribute("rows", rows > 0, rows);
        xml.appendOptionalAttribute("cols", cols > 0, cols);
        xml.appendOptionalAttribute("buttonId", submitControlId);
        xml.appendOptionalAttribute("placeholder", HtmlRenderUtil.getEffectivePlaceholder(textArea));
    }
    xml.appendClose();
    if (textArea.isRichTextArea()) {
        /*
			 * This is a nested element instead of an attribute to cater for future enhancements
			 * such as turning rich text features on or off, or specifying JSON config either as
			 * a URL attribute or a nested CDATA section.
			 */
        xml.append("<ui:rtf />");
    }
    String textString = textArea.getText();
    if (textString != null) {
        if (textArea.isReadOnly() && textArea.isRichTextArea()) {
            // read only we want to output unescaped, but it must still be XML valid.
            xml.write(HtmlToXMLUtil.unescapeToXML(textString));
        } else {
            xml.appendEscaped(textString);
        }
    }
    if (!readOnly) {
        DiagnosticRenderUtil.renderDiagnostics(textArea, renderContext);
    }
    xml.appendEndTag("ui:textarea");
}
Also used : WTextArea(com.github.bordertech.wcomponents.WTextArea) WComponent(com.github.bordertech.wcomponents.WComponent) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 87 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class WTextFieldRenderer method doRender.

/**
 * Paints the given WTextField.
 *
 * @param component the WTextField to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WTextField textField = (WTextField) component;
    XmlStringBuilder xml = renderContext.getWriter();
    boolean readOnly = textField.isReadOnly();
    xml.appendTagOpen("ui:textfield");
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("hidden", component.isHidden(), "true");
    if (readOnly) {
        xml.appendAttribute("readOnly", "true");
    } else {
        int cols = textField.getColumns();
        int minLength = textField.getMinLength();
        int maxLength = textField.getMaxLength();
        String pattern = textField.getPattern();
        WSuggestions suggestions = textField.getSuggestions();
        String suggestionsId = suggestions == null ? null : suggestions.getId();
        WComponent submitControl = textField.getDefaultSubmitButton();
        String submitControlId = submitControl == null ? null : submitControl.getId();
        xml.appendOptionalAttribute("disabled", textField.isDisabled(), "true");
        xml.appendOptionalAttribute("required", textField.isMandatory(), "true");
        xml.appendOptionalAttribute("minLength", minLength > 0, minLength);
        xml.appendOptionalAttribute("maxLength", maxLength > 0, maxLength);
        xml.appendOptionalAttribute("toolTip", textField.getToolTip());
        xml.appendOptionalAttribute("accessibleText", textField.getAccessibleText());
        xml.appendOptionalAttribute("size", cols > 0, cols);
        xml.appendOptionalAttribute("buttonId", submitControlId);
        xml.appendOptionalAttribute("pattern", !Util.empty(pattern), pattern);
        xml.appendOptionalAttribute("list", suggestionsId);
        xml.appendOptionalAttribute("placeholder", HtmlRenderUtil.getEffectivePlaceholder(textField));
    }
    xml.appendClose();
    xml.appendEscaped(textField.getText());
    if (!readOnly) {
        DiagnosticRenderUtil.renderDiagnostics(textField, renderContext);
    }
    xml.appendEndTag("ui:textfield");
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WSuggestions(com.github.bordertech.wcomponents.WSuggestions) WTextField(com.github.bordertech.wcomponents.WTextField) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 88 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class TemplateUtil method mapTaggedComponents.

/**
 * Replace each component tag with the key so it can be used in the replace writer.
 *
 * @param context the context to modify.
 * @param taggedComponents the tagged components
 *
 * @return the keyed components
 */
public static Map<String, WComponent> mapTaggedComponents(final Map<String, Object> context, final Map<String, WComponent> taggedComponents) {
    Map<String, WComponent> componentsByKey = new HashMap<>();
    // Replace each component tag with the key so it can be used in the replace writer
    for (Map.Entry<String, WComponent> tagged : taggedComponents.entrySet()) {
        String tag = tagged.getKey();
        WComponent comp = tagged.getValue();
        // The key needs to be something which would never be output by a Template.
        String key = "[WC-TemplateLayout-" + tag + "]";
        componentsByKey.put(key, comp);
        // Map the tag to the key in the context
        context.put(tag, key);
    }
    return componentsByKey;
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 89 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class TemplateWriter method doReplace.

/**
 * Replaces the search string by rendering the corresponding component.
 *
 * @param search the search String that was matched.
 * @param backing the underlying writer to write the replacement to.
 */
@Override
protected void doReplace(final String search, final Writer backing) {
    WComponent component = componentsByKey.get(search);
    UIContextHolder.pushContext(uic);
    try {
        component.paint(new WebXmlRenderContext((PrintWriter) backing));
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) PrintWriter(java.io.PrintWriter)

Example 90 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class VelocityRendererImpl method renderInline.

/**
 * {@inheritDoc}
 */
@Override
public void renderInline(final String templateInline, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
    LOG.debug("Rendering inline velocity template.");
    try {
        // Map the tagged components to be used in the replace writer
        Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
        // Setup context
        VelocityContext velocityContext = new VelocityContext();
        for (Map.Entry<String, Object> entry : context.entrySet()) {
            velocityContext.put(entry.getKey(), entry.getValue());
        }
        // Write inline template
        UIContext uic = UIContextHolder.getCurrent();
        try (TemplateWriter velocityWriter = new TemplateWriter(writer, componentsByKey, uic)) {
            getVelocityEngine().evaluate(velocityContext, velocityWriter, templateInline, templateInline);
        }
    } catch (Exception e) {
        throw new SystemException("Problems with inline velocity template." + e.getMessage(), e);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) VelocityContext(org.apache.velocity.VelocityContext) Map(java.util.Map) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Aggregations

WComponent (com.github.bordertech.wcomponents.WComponent)107 Test (org.junit.Test)35 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)30 UIContext (com.github.bordertech.wcomponents.UIContext)20 SystemException (com.github.bordertech.wcomponents.util.SystemException)16 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)14 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)8 WApplication (com.github.bordertech.wcomponents.WApplication)8 WLabel (com.github.bordertech.wcomponents.WLabel)8 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)7 IOException (java.io.IOException)6 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)5 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)5 PrintWriter (java.io.PrintWriter)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Size (com.github.bordertech.wcomponents.Size)4 WRepeater (com.github.bordertech.wcomponents.WRepeater)4 WText (com.github.bordertech.wcomponents.WText)4 WTextField (com.github.bordertech.wcomponents.WTextField)4