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