Package org.onemind.swingweb.templaterender

Source Code of org.onemind.swingweb.templaterender.TemplateRenderContext

/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not,  write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Email: thlee@onemindsoft.org
*/

package org.onemind.swingweb.templaterender;

import java.awt.Window;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.onemind.awtbridge.peer.BridgePeer;
import org.onemind.awtbridge.render.RenderDelegate;
import org.onemind.awtbridge.render.RenderingException;
import org.onemind.commons.java.lang.ConfigurationException;
import org.onemind.commons.java.xml.digest.*;
import org.onemind.commons.java.xml.digest.SaxDigesterHandler;
import org.onemind.swingweb.SwingWebContext;
import org.onemind.swingweb.render.AbstractSwingWebRenderContext;
import org.onemind.swingweb.render.SwingWebRenderContext;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
/**
* The template render context
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class TemplateRenderContext extends AbstractSwingWebRenderContext implements SwingWebRenderContext
{

    /** the logger * */
    private static final Logger _logger = Logger.getLogger(TemplateRenderContext.class.getName());

    /** the template engine * */
    private TemplateEngine _engine;

    /**
     * Constructor
     * @param context the bridge context
     * @throws ConfigurationException if there's config problem
     */
    public TemplateRenderContext(SwingWebContext context) throws ConfigurationException
    {
        super(context);
    }

    /**
     * Set the template engine
     * @param engine the engine
     */
    public void setTemplateEngine(TemplateEngine engine)
    {
        _engine = engine;
    }

    /**
     * Install the peer delegate
     * @param peer the peer
     */
    public void installRenderPeerDelegate(BridgePeer peer)
    {
        Object obj = peer.getComponentObject();
        RenderDelegate delegate = resolveDelegate(obj.getClass());
        if (delegate == null)
        {
            throw new RuntimeException("Cannot resolve render delegate for " + peer);
        } else
        {
            if (_logger.isLoggable(Level.FINEST))
            {
                _logger.finest("Assigning delegate " + delegate + " to " + obj);
            }
            peer.setRenderDelegate(delegate);
        }
    }

    /**
     * render the template
     * @param peer the peer
     * @param writer the writer
     * @throws RenderingException if there's render problem
     */
    public void renderTemplate(BridgePeer peer, Writer writer) throws RenderingException
    {
        _engine.renderTemplate(this, peer, writer);
    }

    /**
     * {@inheritDoc}
     */
    public String getElementName()
    {
        return "templaterender";
    }

    /**
     * {@inheritDoc}
     */
    public void renderOutput(Object comObject, Object output) throws RenderingException
    {
        BridgePeer peer = getContext().getPeer(comObject);
        if (peer != null)
        {
            peer.render(this, output);
        } else
        {
            throw new IllegalStateException("component " + comObject + " has no peer");
        }
    }

    /**
     * {@inheritDoc}
     */
    public void renderOutput(Window window, Object output) throws RenderingException
    {
        renderOutput((Object) window, output);
    }

    /**
     * {@inheritDoc}
     */
    public void renderOutput(Object output) throws RenderingException
    {
        List consoleWindows = getContext().getComponentManager().getConsoleWindows();
        for (int i = 0; i < consoleWindows.size(); i++)
        {
            Window w = (Window) consoleWindows.get(i);
            renderOutput(w, output);
        }
        List windows = getContext().getComponentManager().getWindows();
        for (int i = 0; i < windows.size(); i++)
        {
            Window w = (Window) windows.get(i);
            renderOutput(w, output);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void startDigest(SaxDigesterHandler handler, Attributes attrs)
    {
        super.startDigest(handler, attrs);
        ElementCreatorDigester dig = new ChainedDigester("templateengine", "className", null);
        dig.addListener(new ElementListener()
        {

            /**
             * {@inheritDoc}
             */
            public void objectCreated(Object obj)
            {
                setTemplateEngine((TemplateEngine) obj);
            }
        });
        handler.addSubDigester(dig);
    }

    /**
     * {@inheritDoc}
     */
    public void endDigest(SaxDigesterHandler handler) throws SAXException
    {
        if (_engine == null)
        {
            throw new SAXException("Template engine is not configured at " + handler.getCurrentPath());
        }
    }

    /**
     * {@inheritDoc}
     */
    public void getRuntimeInfo(BridgePeer peer, Map env)
    {
        env.put("templaterenderdelegate", resolveDelegate(peer.getComponentObject().getClass()));
        _engine.getRuntimeInfo(peer, env);
    }

    public void destroy()
    {
    }
}
TOP

Related Classes of org.onemind.swingweb.templaterender.TemplateRenderContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.