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");
}
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");
}
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;
}
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();
}
}
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);
}
}
Aggregations