package it.mice.voila.runtime.springmvc.taglibs;

import org.springframework.context.MessageSource;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
import org.springframework.web.util.ExpressionEvaluationUtils;

public class FrameCommandTag extends RequestContextAwareTag implements Cloneable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String label;
	private String action;
	private String title;

	
	public String getLabel() {
		return label;
	}


	public void setLabel(String label) {
		this.label = label;
	}

	public String getAction() {
		return action;
	}


	public void setAction(String action) {
		this.action = action;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	protected int doStartTagInternal() throws Exception {
        InternalFrameTag tag = (InternalFrameTag) findAncestorWithClass(this, InternalFrameTag.class);
        if (tag == null)
        {
            throw new IllegalArgumentException("Use of frameCommand tag can be used only inside an internalFrame tag");
        }
        try {
        	MessageSource messageSource = getRequestContext().getMessageSource();
        	setAction(ExpressionEvaluationUtils.evaluateString("action", this.getAction(), pageContext));
        	setLabel(ExpressionEvaluationUtils.evaluateString("label", this.getLabel(), pageContext));
        	setTitle(ExpressionEvaluationUtils.evaluateString("title", this.getTitle(), pageContext));

        	if (TagUtils.isMessageCode(this.label)) {
            	setLabel(messageSource.getMessage(TagUtils.getMessageCode(this.getLabel()), TagUtils.EMPTY_ARRAY, getLabel(), getRequestContext().getLocale()));
        	}
        	if (TagUtils.isMessageCode(this.title)) {
            	setTitle(messageSource.getMessage(TagUtils.getMessageCode(getTitle()), TagUtils.EMPTY_ARRAY, getTitle(), getRequestContext().getLocale()));
        	}

        	tag.getCommandList().add((FrameCommandTag)this.clone());
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
        
		return SKIP_BODY;
	}

}
