/*
 * $Id: CalendarButtonTag.java,v 1.4 2006/10/04 03:15:09 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: CalendarButtonTag.java,v $
 * Revision 1.4  2006/10/04 03:15:09  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.3  2006/03/09 01:31:48  marbobo
 * + Unicode-Unterstützung für Javascript und Html implementiert
 *
 * Revision 1.2  2005/02/03 23:27:07  marbobo
 * + title hinzugefügt
 *
 */

package vkc.view.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.Globals;
import org.apache.struts.util.MessageResources;

import vkc.db.entities.VKCLanguage;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCLanguageSerializer;
import vkc.util.ServletUtils;
import vkc.util.StringUtils;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;

public final class CalendarButtonTag extends TagSupport {
    private String imagePath = "/templates/${template.name}/gfx/clock.gif";
    private String buttonName = "";
    private boolean filter = true;

    public int doEndTag() throws JspException {
        try {
            JspWriter writer = pageContext.getOut();
            HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();

            MessageResources resources = (MessageResources)request.getAttribute(Globals.MESSAGES_KEY);
            VKCLanguage defaultLanguage = VKCLanguageSerializer.getDefaultLanguage(); 
            VKCUser user = ServletUtils.getUser(request);
            String lang = "";
            String title = "";
            String template = user != null ? user.getTemplate().getName() : WebShare.DEFAULT_TEMPLATE;
            
            if (user != null && user.getLanguage() != null) {
                lang = user.getLanguage().getShortName();
                title = resources.getMessage(user.getLanguage().getLocale(), "vkc.string.show.calendar");
            } else {
                lang = defaultLanguage.getShortName();
                title = resources.getMessage(defaultLanguage.getLocale(), "vkc.string.show.calendar");
            }
            
            if (filter) {
            	title = VKCStringUtils.filterUnicodeChars(title);
            }
            
            writer.print("<input type=\"image\" ");
            writer.print("name=\"" + buttonName + "\" ");
            writer.print("onclick=\"return showCalendar( this, '" + lang + "', '" + request.getContextPath() + "', '"  + template + "' )\" ");
            writer.print("src=\"" + request.getContextPath() + ServletUtils.replacePathVars(imagePath, user) + "\" border=\"0\" ");
            writer.print("title=\"" + title + "\">");
        } catch (IOException ioex) {
            throw new JspException(ioex);
        }

        return EVAL_PAGE;
    }

    public int doStartTag() throws JspException {
        return SKIP_BODY;
    }

    public void release() {
        super.release(  );
        imagePath = "/templates/${template.name}/gfx/clock.gif";
        buttonName = "";
        filter = true;
    }

    public boolean getFilter() {
        return filter;
    }

    public void setFilter(boolean filter) {
        this.filter = filter;
    }


    public String getButtonName() {
        return buttonName;
    }
    
    public void setButtonName(String buttonName) {
        this.buttonName = StringUtils.normalize(buttonName);
    }

    public String getImagePath() {
        return imagePath;
    }
    
    public void setImagePath(String imagePath) {
        this.imagePath = StringUtils.normalize(imagePath);
    }
}