/* ZKSpreadsheetTag.java

	Purpose:
		
	Description:
		
	History:
		Tue Dec 26 08:48:59 TST 2010, Created by Ashish Dasnurkar

Copyright (C) 2010 Potix Corporation. All Rights Reserved.

*/
package org.zkoss.zss.jsp.ui;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.zkoss.util.logging.Log;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.GenericRichlet;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.sys.DesktopCtrl;
import org.zkoss.zkplus.embed.Renders;

/**
 * ZK Head for js and css
 * @author dennis
 * @since 3.0.0
 */
public class HeadTag extends SimpleTagSupport {
	
	private static final Log log = Log.lookup(HeadTag.class);
	
	/**
	 * Reders ZK Spreadsheet JSP tag
	 * @throws JspException
	 * @throws IOException
	 */
	public void doTag() throws JspException, IOException {
		PageContext pgCtx = (PageContext) getJspContext();
		ServletContext svlctx = pgCtx.getServletContext();
		HttpServletRequest request = (HttpServletRequest) pgCtx.getRequest();
		HttpServletResponse response = (HttpServletResponse) pgCtx.getResponse();
		JspWriter jspWriter = getJspContext().getOut();
		try {
			Renders.render(svlctx, request, response, 
					new GenericRichlet() {
						public void service(Page page) throws Exception {
							//just for output head, remove unnecessary page
							Desktop dt = page.getDesktop();
							((DesktopCtrl)dt).removePage(page);
							
						}
					}, null, jspWriter);
		} catch (ServletException e) {
			throw new JspException(e.getMessage(),e);
		}
	}
}
