All Downloads are FREE. Search and download functionalities are using the official Maven repository.

echopointng.ui.syncpeer.TemplatePanelPeer Maven / Gradle / Ivy

Go to download

Echo2 bundled with Echo2_Extras, Echo2_FileTransfer and echopointing and various improvements/bugfixes

There is a newer version: 2.0.4
Show newest version
/* 
 * This file is part of the Echo Point Project.  This project is a collection
 * of Components that have extended the Echo Web Application Framework.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of anyone of the MPL, the GPL or the LGPL.
 */
package echopointng.ui.syncpeer;

import java.util.ArrayList;
import java.util.List;

import nextapp.echo2.app.ApplicationInstance;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Style;
import nextapp.echo2.app.StyleSheet;
import nextapp.echo2.app.update.ServerComponentUpdate;
import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
import nextapp.echo2.webcontainer.ContainerContext;
import nextapp.echo2.webcontainer.ContainerInstance;
import nextapp.echo2.webcontainer.DomUpdateSupport;
import nextapp.echo2.webcontainer.RenderContext;
import nextapp.echo2.webcontainer.SynchronizePeerFactory;
import nextapp.echo2.webrender.ClientProperties;
import nextapp.echo2.webrender.ServerMessage;
import nextapp.echo2.webrender.Service;
import nextapp.echo2.webrender.WebRenderServlet;
import nextapp.echo2.webrender.output.CssStyle;
import nextapp.echo2.webrender.servermessage.DomUpdate;
import nextapp.echo2.webrender.service.JavaScriptService;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import echopointng.TemplatePanel;
import echopointng.stylesheet.CssStyleSheetException;
import echopointng.stylesheet.CssStyleSheetLoader;
import echopointng.template.TemplateCachingHints;
import echopointng.template.TemplateDataSource;
import echopointng.template.TemplateTextSubstitution;
import echopointng.ui.resource.Resources;
import echopointng.ui.template.TemplateCompiler;
import echopointng.ui.template.TemplateCompilerLoader;
import echopointng.ui.util.CssStyleEx;
import echopointng.ui.util.Render;
import echopointng.ui.util.RenderingContext;
import echopointng.ui.util.io.StringInputStream;
import echopointng.util.HtmlKit;
import echopointng.util.collections.ExpiryCache;
import echopointng.util.reflect.ReflectionKit;

/**
 * TemplatePanelPeer is a peer for TemplatePanel
 */
public class TemplatePanelPeer extends AbstractEchoPointContainerPeer {

	/**
	 * Service to provide supporting JavaScript library.
	 */
	public static final Service TEMPLATE_JS_SERVICE = JavaScriptService.forResource("EPNG.TemplatePanel",
			"/echopointng/ui/resource/js/templatepanel.js");
	static {
		WebRenderServlet.getServiceRegistry().add(TEMPLATE_JS_SERVICE);
	}

	/**
	 * A cache of templates previously encountered. As the XHTML results are
	 * cached it will save time compiling them again.  SoftReference's are not
	 * used by default.
	 * 

* You can change the default "time to live" and "default access timeout" * values by calling * TemplatePanelPeer.COMPILED_TEMPLATE_CACHE.setTimeToLive(xxx) * and * TemplatePanelPeer.COMPILED_TEMPLATE_CACHE.setAccessTimeout(xxx) * and whether SoftReferences are used to cache objects by * using * TemplatePanelPeer.COMPILED_TEMPLATE_CACHE.setSoftReferences(xxx) *

* You should however consider using TemplateCachingHints to provide these * caching values on a case by case basis. * * @see TemplateCachingHints * @see java.lang.ref.SoftReference */ public static final ExpiryCache COMPILED_TEMPLATE_CACHE; static { COMPILED_TEMPLATE_CACHE = new ExpiryCache(ExpiryCache.DEFAULT_TIME_TO_LIVE, ExpiryCache.DEFAULT_ACCESS_TIMEOUT, false); } /** * @see echopointng.ui.syncpeer.AbstractEchoPointPeer#renderHtml(echopointng.ui.util.RenderingContext, * Node, nextapp.echo2.app.Component) */ public void renderHtml(RenderingContext rc, Node parent, Component component) { TemplatePanel templatePanel = (TemplatePanel) component; // we now need to ask an appropriate template compiler to return // a W3C DOM Element which has been "transmogrified" and we // can then send it back as the panels DOM TemplateResult compiledTemplate = renderTemplateData(rc, templatePanel); if (compiledTemplate != null) { createInitDirective(rc, templatePanel, compiledTemplate); rc.addLibrary(Resources.EP_SCRIPT_SERVICE); rc.addLibrary(TEMPLATE_JS_SERVICE); compiledTemplate.markupE.setAttribute("id", ContainerInstance.getElementId(templatePanel)); compiledTemplate.markupE.setAttribute("xmlns", "http://www.w3.org/1999/xhtml"); rc.addStandardWebSupport(compiledTemplate.markupE); if (!compiledTemplate.markupE.hasAttribute("style")) { CssStyleEx cssStyle = new CssStyleEx(component); compiledTemplate.markupE.setAttribute("style", cssStyle.renderInline()); } parent.appendChild(compiledTemplate.markupE); } } /** * @see echopointng.ui.syncpeer.AbstractEchoPointPeer#renderDispose(nextapp.echo2.webcontainer.RenderContext, * nextapp.echo2.app.update.ServerComponentUpdate, * nextapp.echo2.app.Component) */ public void renderDispose(RenderContext rc, ServerComponentUpdate update, Component component) { super.renderDispose(rc, update, component); rc.getServerMessage().addLibrary(Resources.EP_SCRIPT_SERVICE.getId()); rc.getServerMessage().addLibrary(TEMPLATE_JS_SERVICE.getId()); createDisposeDirective(rc.getServerMessage(), (TemplatePanel) component); } /** * */ protected void createDisposeDirective(ServerMessage serverMessage, TemplatePanel templatePanel) { serverMessage.addLibrary(Resources.EP_SCRIPT_SERVICE.getId()); serverMessage.addLibrary(TEMPLATE_JS_SERVICE.getId()); Element itemizedUpdateElement = serverMessage.getItemizedDirective(ServerMessage.GROUP_ID_PREREMOVE, "EPTemplatePanel.MessageProcessor", "dispose", new String[0], new String[0]); Element itemElement = serverMessage.getDocument().createElement("item"); itemizedUpdateElement.appendChild(itemElement); itemElement.setAttribute("eid", ContainerInstance.getElementId(templatePanel)); } /** * Has this XML format * * item {eid} inlineStyleContainer style style style externalStyleContainer * style style style * */ protected void createInitDirective(RenderingContext rc, TemplatePanel templatePanel, TemplateResult templateResult) { Element itemizedUpdateElement = rc.getServerMessage().getItemizedDirective(ServerMessage.GROUP_ID_POSTUPDATE, "EPTemplatePanel.MessageProcessor", "init", new String[0], new String[0]); Element itemElement = rc.getServerMessage().getDocument().createElement("item"); itemizedUpdateElement.appendChild(itemElement); itemElement.setAttribute("eid", ContainerInstance.getElementId(templatePanel)); ApplicationInstance app = ApplicationInstance.getActive(); ContainerContext containerContext = (ContainerContext) app.getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME); ClientProperties clientProperties = containerContext.getClientProperties(); String userAgent = clientProperties.getString(ClientProperties.NAVIGATOR_USER_AGENT); // fixup // // The link elements must be sent down to the client and // inserted directly via JS. This is because they // have come out of the head and need to be replaced there! // Element externalStyleContainerE = rc.getServerMessage().getDocument().createElement("externalStyleContainer"); for (int i = 0; i < templateResult.externalStyles.length; i++) { externalStyleContainerE.appendChild(templateResult.externalStyles[i]); } itemElement.appendChild(externalStyleContainerE); //