Search in sources :

Example 71 with WComponent

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

the class VelocityRenderer method paintXml.

/**
 * Paints the component in XML using the Velocity Template.
 *
 * @param component the component to paint.
 * @param writer the writer to send the HTML output to.
 */
public void paintXml(final WComponent component, final Writer writer) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("paintXml called for component class " + component.getClass());
    }
    String templateText = null;
    if (component instanceof AbstractWComponent) {
        AbstractWComponent abstractComp = ((AbstractWComponent) component);
        templateText = abstractComp.getTemplateMarkUp();
    }
    try {
        Map<String, WComponent> componentsByKey = new HashMap<>();
        VelocityContext context = new VelocityContext();
        fillContext(component, context, componentsByKey);
        VelocityWriter velocityWriter = new VelocityWriter(writer, componentsByKey, UIContextHolder.getCurrent());
        if (templateText != null) {
            VelocityEngine engine = VelocityEngineFactory.getVelocityEngine();
            engine.evaluate(context, velocityWriter, component.getClass().getSimpleName(), templateText);
        } else {
            Template template = getTemplate(component);
            if (template == null) {
                LOG.warn("VelocityRenderer invoked for a component with no template: " + component.getClass().getName());
            } else {
                template.merge(context, velocityWriter);
            }
        }
        velocityWriter.close();
        if (component instanceof VelocityProperties) {
            ((VelocityProperties) component).mapUsed();
        }
    } catch (ResourceNotFoundException rnfe) {
        LOG.error("Could not find template '" + url + "' for component " + component.getClass().getName(), rnfe);
    } catch (ParseErrorException pee) {
        // syntax error : problem parsing the template
        LOG.error("Parse problems", pee);
    } catch (MethodInvocationException mie) {
        // something invoked in the template
        // threw an exception
        Throwable wrapped = mie.getWrappedThrowable();
        LOG.error("Problems with velocity", mie);
        if (wrapped != null) {
            LOG.error("Wrapped exception...", wrapped);
        }
    } catch (Exception e) {
        LOG.error("Problems with velocity", e);
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) WTemplate(com.github.bordertech.wcomponents.WTemplate) AbstractWComponent(com.github.bordertech.wcomponents.AbstractWComponent) AbstractWComponent(com.github.bordertech.wcomponents.AbstractWComponent) WComponent(com.github.bordertech.wcomponents.WComponent) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) VelocityProperties(com.github.bordertech.wcomponents.velocity.VelocityProperties) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) VelocityWriter(com.github.bordertech.wcomponents.velocity.VelocityWriter)

Example 72 with WComponent

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

the class WAjaxControlRenderer method doRender.

/**
 * Paints the given AjaxControl.
 *
 * @param component the AjaxControl to paint
 * @param renderContext the RenderContext to paint to
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WAjaxControl ajaxControl = (WAjaxControl) component;
    XmlStringBuilder xml = renderContext.getWriter();
    WComponent trigger = ajaxControl.getTrigger() == null ? ajaxControl : ajaxControl.getTrigger();
    int delay = ajaxControl.getDelay();
    if (ajaxControl.getTargets() == null || ajaxControl.getTargets().isEmpty()) {
        return;
    }
    // Start tag
    xml.appendTagOpen("ui:ajaxtrigger");
    xml.appendAttribute("triggerId", trigger.getId());
    xml.appendOptionalAttribute("loadOnce", ajaxControl.isLoadOnce(), "true");
    xml.appendOptionalAttribute("delay", delay > 0, delay);
    xml.appendClose();
    // Targets
    for (AjaxTarget target : ajaxControl.getTargets()) {
        xml.appendTagOpen("ui:ajaxtargetid");
        xml.appendAttribute("targetId", target.getId());
        xml.appendEnd();
    }
    // End tag
    xml.appendEndTag("ui:ajaxtrigger");
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WAjaxControl(com.github.bordertech.wcomponents.WAjaxControl) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) AjaxTarget(com.github.bordertech.wcomponents.AjaxTarget)

Example 73 with WComponent

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

the class WButtonRenderer method doRender.

/**
 * Paints the given WButton.
 *
 * @param component the WButton to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    XmlStringBuilder xml = renderContext.getWriter();
    WButton button = (WButton) component;
    String text = button.getText();
    String imageUrl = button.getImageUrl();
    String accessibleText = button.getAccessibleText();
    String toolTip = button.getToolTip();
    if (Util.empty(text) && imageUrl == null && Util.empty(accessibleText) && Util.empty(toolTip)) {
        throw new SystemException("WButton text or imageUrl must be specified");
    }
    xml.appendTagOpen(getTagName(button));
    String buttonId = button.getId();
    ImagePosition pos = button.getImagePosition();
    if (Util.empty(text) && Util.empty(toolTip) && Util.empty(accessibleText)) {
        // If the button has an imageUrl but no text equivalent get the text equivalent off of the image
        WImage imgHolder = button.getImageHolder();
        if (null != imgHolder) {
            toolTip = imgHolder.getAlternativeText();
        }
    }
    xml.appendAttribute("id", buttonId);
    xml.appendAttribute("name", buttonId);
    xml.appendAttribute("value", "x");
    xml.appendAttribute("type", getButtonType(button));
    xml.appendAttribute("class", geHtmlClassName(button));
    xml.appendOptionalAttribute("disabled", button.isDisabled(), "disabled");
    xml.appendOptionalAttribute("hidden", button.isHidden(), "hidden");
    xml.appendOptionalAttribute("title", toolTip);
    xml.appendOptionalAttribute("aria-label", accessibleText);
    xml.appendOptionalAttribute("aria-haspopup", button.isPopupTrigger(), "true");
    xml.appendOptionalAttribute("accesskey", Util.upperCase(button.getAccessKeyAsString()));
    xml.appendOptionalAttribute("data-wc-btnmsg", button.getMessage());
    if (button.isCancel()) {
        xml.appendAttribute("formnovalidate", "formnovalidate");
    } else {
        Action action = button.getAction();
        if (action instanceof ValidatingAction) {
            WComponent validationTarget = ((ValidatingAction) action).getComponentToValidate();
            xml.appendAttribute("data-wc-validate", validationTarget.getId());
        }
    }
    xml.appendClose();
    if (imageUrl != null) {
        xml.appendTagOpen("span");
        String imageHolderClass = "wc_nti";
        if (pos != null) {
            StringBuffer imageHolderClassBuffer = new StringBuffer("wc_btn_img wc_btn_img");
            switch(pos) {
                case NORTH:
                    imageHolderClassBuffer.append("n");
                    break;
                case EAST:
                    imageHolderClassBuffer.append("e");
                    break;
                case SOUTH:
                    imageHolderClassBuffer.append("s");
                    break;
                case WEST:
                    imageHolderClassBuffer.append("w");
                    break;
                default:
                    throw new SystemException("Unknown image position: " + pos);
            }
            imageHolderClass = imageHolderClassBuffer.toString();
        }
        xml.appendAttribute("class", imageHolderClass);
        xml.appendClose();
        if (pos != null && text != null) {
            xml.appendTag("span");
            xml.appendEscaped(text);
            xml.appendEndTag("span");
        }
        xml.appendTagOpen("img");
        xml.appendUrlAttribute("src", imageUrl);
        String alternateText = pos == null ? text : "";
        xml.appendAttribute("alt", alternateText);
        xml.appendEnd();
        xml.appendEndTag("span");
    } else if (text != null) {
        xml.appendEscaped(text);
    }
    xml.appendEndTag(getTagName(button));
    if (button.isAjax()) {
        paintAjax(button, xml);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) ValidatingAction(com.github.bordertech.wcomponents.validation.ValidatingAction) Action(com.github.bordertech.wcomponents.Action) SystemException(com.github.bordertech.wcomponents.util.SystemException) ImagePosition(com.github.bordertech.wcomponents.WButton.ImagePosition) ValidatingAction(com.github.bordertech.wcomponents.validation.ValidatingAction) WImage(com.github.bordertech.wcomponents.WImage) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) WButton(com.github.bordertech.wcomponents.WButton)

Example 74 with WComponent

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

the class WDataTableRenderer method doPaintRows.

/**
 * Override paintRow so that we only paint the first-level nodes for tree-tables.
 *
 * @param table the table to paint the rows for.
 * @param renderContext the RenderContext to paint to.
 */
private void doPaintRows(final WDataTable table, final WebXmlRenderContext renderContext) {
    TableDataModel model = table.getDataModel();
    WRepeater repeater = table.getRepeater();
    List<?> beanList = repeater.getBeanList();
    final int rowCount = beanList.size();
    WComponent row = repeater.getRepeatedComponent();
    for (int i = 0; i < rowCount; i++) {
        if (model instanceof TreeTableDataModel) {
            Integer nodeIdx = (Integer) beanList.get(i);
            TableTreeNode node = ((TreeTableDataModel) model).getNodeAtLine(nodeIdx);
            if (node.getLevel() != 1) {
                // Handled by the layout, so don't paint the row.
                continue;
            }
        }
        // Each row has its own context. This is why we can reuse the same
        // WComponent instance for each row.
        UIContext rowContext = repeater.getRowContext(beanList.get(i), i);
        UIContextHolder.pushContext(rowContext);
        try {
            row.paint(renderContext);
        } finally {
            UIContextHolder.popContext();
        }
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) TableTreeNode(com.github.bordertech.wcomponents.TableTreeNode) TreeTableDataModel(com.github.bordertech.wcomponents.TreeTableDataModel) UIContext(com.github.bordertech.wcomponents.UIContext) TreeTableDataModel(com.github.bordertech.wcomponents.TreeTableDataModel) TableDataModel(com.github.bordertech.wcomponents.TableDataModel) WRepeater(com.github.bordertech.wcomponents.WRepeater)

Example 75 with WComponent

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

the class WDecoratedLabelRenderer method doRender.

/**
 * Paints the given WDecoratedLabel.
 *
 * @param component the WDecoratedLabel to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WDecoratedLabel label = (WDecoratedLabel) component;
    XmlStringBuilder xml = renderContext.getWriter();
    WComponent head = label.getHead();
    WComponent body = label.getBody();
    WComponent tail = label.getTail();
    xml.appendTagOpen("ui:decoratedlabel");
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("hidden", label.isHidden(), "true");
    xml.appendClose();
    if (head != null && head.isVisible()) {
        xml.appendTagOpen("ui:labelhead");
        xml.appendAttribute("id", label.getId() + "-head");
        xml.appendClose();
        head.paint(renderContext);
        xml.appendEndTag("ui:labelhead");
    }
    xml.appendTagOpen("ui:labelbody");
    xml.appendAttribute("id", label.getId() + "-body");
    xml.appendClose();
    body.paint(renderContext);
    xml.appendEndTag("ui:labelbody");
    if (tail != null && tail.isVisible()) {
        xml.appendTagOpen("ui:labeltail");
        xml.appendAttribute("id", label.getId() + "-tail");
        xml.appendClose();
        tail.paint(renderContext);
        xml.appendEndTag("ui:labeltail");
    }
    xml.appendEndTag("ui:decoratedlabel");
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) WDecoratedLabel(com.github.bordertech.wcomponents.WDecoratedLabel)

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