Search in sources :

Example 1 with Renderer

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

the class UIManager method createRenderer.

/**
 * Attempts to create a Renderer with the given name.
 *
 * @param rendererName the name of the Renderer
 * @return a Renderer of the given type, or null if the class was not found.
 */
private static Renderer createRenderer(final String rendererName) {
    if (rendererName.endsWith(".vm")) {
        // This is a velocity template, so use a VelocityLayout
        return new VelocityRenderer(rendererName);
    }
    try {
        Class<?> managerClass = Class.forName(rendererName);
        Object manager = managerClass.newInstance();
        if (!(manager instanceof Renderer)) {
            throw new SystemException(rendererName + " is not a Renderer");
        }
        return (Renderer) manager;
    } catch (ClassNotFoundException e) {
        // Legal - there might not a manager implementation in a given theme
        return null;
    } catch (InstantiationException e) {
        throw new SystemException("Failed to instantiate " + rendererName, e);
    } catch (IllegalAccessException e) {
        throw new SystemException("Failed to access " + rendererName, e);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) Renderer(com.github.bordertech.wcomponents.Renderer) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer)

Example 2 with Renderer

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

the class UIManager method findRenderer.

/**
 * Finds the layout for the given theme and component.
 *
 * @param component the WComponent class to find a manager for.
 * @param key the component key to use for caching the renderer.
 * @return the LayoutManager for the component.
 */
private synchronized Renderer findRenderer(final WComponent component, final Duplet<String, Class<?>> key) {
    LOG.info("Looking for layout for " + key.getSecond().getName() + " in " + key.getFirst());
    Renderer renderer = findConfiguredRenderer(component, key.getFirst());
    if (renderer == null) {
        renderers.put(key, NULL_RENDERER);
    } else {
        renderers.put(key, renderer);
    }
    return renderer;
}
Also used : Renderer(com.github.bordertech.wcomponents.Renderer) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer)

Example 3 with Renderer

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

the class UIManager method getRenderer.

/**
 * Retrieves a renderer which can renderer the given component to the context.
 *
 * @param component the component to retrieve the renderer for.
 * @param context the render context.
 * @return an appropriate renderer for the component and context, or null if a suitable renderer could not be found.
 */
public static Renderer getRenderer(final WComponent component, final RenderContext context) {
    Class<? extends WComponent> clazz = component.getClass();
    Duplet<String, Class<?>> key = new Duplet<String, Class<?>>(context.getRenderPackage(), clazz);
    Renderer renderer = INSTANCE.renderers.get(key);
    if (renderer == null) {
        renderer = INSTANCE.findRenderer(component, key);
    } else if (renderer == NULL_RENDERER) {
        return null;
    }
    return renderer;
}
Also used : Duplet(com.github.bordertech.wcomponents.util.Duplet) Renderer(com.github.bordertech.wcomponents.Renderer) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer)

Example 4 with Renderer

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

the class UIManager method findConfiguredRenderer.

/**
 * Attempts to find the configured renderer for the given output format and component.
 *
 * @param component the component to find a manager for.
 * @param rendererPackage the package containing the renderers.
 * @return the Renderer for the component, or null if there is no renderer defined.
 */
private Renderer findConfiguredRenderer(final WComponent component, final String rendererPackage) {
    Renderer renderer = null;
    // Renderer may have been specified at a higher level.
    for (Class<?> c = component.getClass(); renderer == null && c != null && !AbstractWComponent.class.equals(c); c = c.getSuperclass()) {
        String qualifiedClassName = c.getName();
        // Is there an override for this class?
        String rendererName = ConfigurationProperties.getRendererOverride(qualifiedClassName);
        if (rendererName != null) {
            renderer = createRenderer(rendererName);
            if (renderer == null) {
                LOG.warn("Layout Manager \"" + rendererName + "\" specified for " + qualifiedClassName + " was not found");
            } else {
                return renderer;
            }
        }
        renderer = findRendererFactory(rendererPackage).getRenderer(c);
    }
    return renderer;
}
Also used : Renderer(com.github.bordertech.wcomponents.Renderer) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer)

Example 5 with Renderer

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

the class RendererFactoryImpl method createRenderer.

/**
 * Attempts to create a Renderer with the given name.
 *
 * @param rendererName the name of the Renderer
 * @return a LayoutManager of the given type, or null if the class was not found.
 */
private Renderer createRenderer(final String rendererName) {
    try {
        Class<?> managerClass = Class.forName(rendererName);
        Object manager = managerClass.newInstance();
        if (!(manager instanceof Renderer)) {
            throw new SystemException(rendererName + " is not a Renderer");
        }
        return (Renderer) manager;
    } catch (ClassNotFoundException e) {
        // Legal - there might not a renderer implementation for the component in this format
        return null;
    } catch (InstantiationException e) {
        throw new SystemException("Failed to instantiate " + rendererName, e);
    } catch (IllegalAccessException e) {
        throw new SystemException("Failed to access " + rendererName, e);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) Renderer(com.github.bordertech.wcomponents.Renderer)

Aggregations

Renderer (com.github.bordertech.wcomponents.Renderer)8 VelocityRenderer (com.github.bordertech.wcomponents.render.webxml.VelocityRenderer)6 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 RendererFactory (com.github.bordertech.wcomponents.RendererFactory)1 LayoutManager (com.github.bordertech.wcomponents.layout.LayoutManager)1 Duplet (com.github.bordertech.wcomponents.util.Duplet)1