Search in sources :

Example 36 with WComponent

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

the class HandlebarsRendererImpl 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 handlebars inline template.");
    try {
        // Map the tagged components to be used in the replace writer
        Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
        // Get Engine
        Handlebars handlebars = getHandlebarsEngine(options);
        // Compile inline
        Template template = handlebars.compileInline(templateInline);
        // Setup handlebars context
        Context handlebarsContext = createContext(context);
        // Write template
        writeTemplate(template, handlebarsContext, componentsByKey, writer);
    } catch (Exception e) {
        throw new SystemException("Problems with handlebars inline template. " + e.getMessage(), e);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Context(com.github.jknack.handlebars.Context) UIContext(com.github.bordertech.wcomponents.UIContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) Handlebars(com.github.jknack.handlebars.Handlebars) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SystemException(com.github.bordertech.wcomponents.util.SystemException) Template(com.github.jknack.handlebars.Template)

Example 37 with WComponent

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

the class VelocityRendererImpl method renderTemplate.

/**
 * {@inheritDoc}
 */
@Override
public void renderTemplate(final String templateName, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
    LOG.debug("Rendering velocity template [" + templateName + "].");
    // Velocity uses a ClassLoader so dont use an absolute path.
    String name = templateName.startsWith("/") ? templateName.substring(1) : templateName;
    try {
        // Load template
        Template template = getVelocityEngine().getTemplate(name);
        // 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 template
        UIContext uic = UIContextHolder.getCurrent();
        try (TemplateWriter velocityWriter = new TemplateWriter(writer, componentsByKey, uic)) {
            template.merge(velocityContext, velocityWriter);
        }
    } catch (ResourceNotFoundException e) {
        throw new SystemException("Could not find velocity template [" + templateName + "]. " + e.getMessage(), e);
    } catch (Exception e) {
        throw new SystemException("Problems with velocity template [" + templateName + "]. " + e.getMessage(), e);
    }
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) VelocityContext(org.apache.velocity.VelocityContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Map(java.util.Map)

Example 38 with WComponent

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

the class TreeUtil method collateVisibles.

/**
 * Obtains a list of components which are visible in the given tree. Repeated components will be returned multiple
 * times, one for each row which they are visible in.
 *
 * @param comp the root component to search from.
 * @return a list of components which are visible in the given context.
 */
public static List<ComponentWithContext> collateVisibles(final WComponent comp) {
    final List<ComponentWithContext> list = new ArrayList<>();
    WComponentTreeVisitor visitor = new WComponentTreeVisitor() {

        @Override
        public VisitorResult visit(final WComponent comp) {
            // (so ignore them)
            if (comp.isVisible()) {
                list.add(new ComponentWithContext(comp, UIContextHolder.getCurrent()));
            }
            return VisitorResult.CONTINUE;
        }
    };
    traverseVisible(comp, visitor);
    return list;
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) ArrayList(java.util.ArrayList) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 39 with WComponent

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

the class TreeUtil method isIdFocusable.

/**
 * Check if this ID is focusable.
 * <p>
 * Considered focusable if the component and all its ancestors are visible and not hidden.
 * </p>
 *
 * @param root the root component to search from.
 * @param id the id to search for.
 * @return the component with context if it is focusable, otherwise null
 */
public static boolean isIdFocusable(final WComponent root, final String id) {
    FindComponentByIdVisitor visitor = new FindComponentByIdVisitor(id) {

        @Override
        public VisitorResult visit(final WComponent comp) {
            VisitorResult result = super.visit(comp);
            // If hidden then abort branch
            if (result == VisitorResult.CONTINUE && comp.isHidden()) {
                return VisitorResult.ABORT_BRANCH;
            }
            return result;
        }
    };
    // Only traverse visible
    doTraverse(root, true, visitor);
    // Check if matching component is hidden
    ComponentWithContext result = visitor.getResult();
    return result == null ? false : !result.getComponent().isHidden();
}
Also used : FindComponentByIdVisitor(com.github.bordertech.wcomponents.util.visitor.FindComponentByIdVisitor) WComponent(com.github.bordertech.wcomponents.WComponent) VisitorResult(com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 40 with WComponent

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

the class TreeUtil method getClosestContextForId.

/**
 * Retrieves the closest context for the component with the given Id.
 *
 * @param root the root component to search from.
 * @param id the id to search for.
 * @param visibleOnly true if process visible only
 * @return the closest context for the component with the given id, or null if not found.
 */
public static UIContext getClosestContextForId(final WComponent root, final String id, final boolean visibleOnly) {
    FindComponentByIdVisitor visitor = new FindComponentByIdVisitor(id) {

        @Override
        public VisitorResult visit(final WComponent comp) {
            VisitorResult result = super.visit(comp);
            if (result == VisitorResult.CONTINUE) {
                // Save closest UIC as processing tree
                setResult(new ComponentWithContext(comp, UIContextHolder.getCurrent()));
            }
            return result;
        }
    };
    doTraverse(root, visibleOnly, visitor);
    return visitor.getResult() == null ? null : visitor.getResult().getContext();
}
Also used : FindComponentByIdVisitor(com.github.bordertech.wcomponents.util.visitor.FindComponentByIdVisitor) WComponent(com.github.bordertech.wcomponents.WComponent) VisitorResult(com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

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