/*
 * $Id: OptionsTag.java,v 1.2 2006/04/13 20:59:43 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: OptionsTag.java,v $
 * Revision 1.2  2006/04/13 20:59:43  marbobo
 * + Label-Wert kann nun auch als Message-Key behandelt werden
 *
 * Revision 1.1  2005/02/16 21:46:20  marbobo
 * + Initiale Version
 *
 */
package vkc.view.tag;

import javax.servlet.http.HttpServletRequest;

import vkc.db.entities.VKCUser;
import vkc.util.ServletUtils;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;

public class OptionsTag extends org.apache.struts.taglib.html.OptionsTag {
	/**
	 * The style associated with this tag.
	 */
	private String style = null;

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	/**
	 * The named style class associated with this tag.
	 */
	private String styleClass = null;

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * Handles label value as MessageResource-Key for this tag.
	 */
	private boolean handleLabelAsKey = false;

	public boolean getHandleLabelAsKey() {
		return handleLabelAsKey;
	}

	public void setHandleLabelAsKey(boolean handleLabelAsKey) {
		this.handleLabelAsKey = handleLabelAsKey;
	}

	// ------------------------------------------------------ Protected Methods

	/**
	 * Add an option element to the specified StringBuffer based on the
	 * specified parameters.
	 *<p>
	 * Note that this tag specifically does not support the
	 * <code>styleId</code> tag attribute, which causes the HTML
	 * <code>id</code> attribute to be emitted.  This is because the HTML
	 * specification states that all "id" attributes in a document have to be
	 * unique.  This tag will likely generate more than one <code>option</code>
	 * element element, but it cannot use the same <code>id</code> value.  It's
	 * conceivable some sort of mechanism to supply an array of <code>id</code>
	 * values could be devised, but that doesn't seem to be worth the trouble.
	 *
	 * @param sb StringBuffer accumulating our results
	 * @param value Value to be returned to the server for this option
	 * @param label Value to be shown to the user for this option
	 * @param matched Should this value be marked as selected?
	 */
	protected void addOption(StringBuffer sb, String value, String label, boolean matched) {
		sb.append("<option value=\"");

		if (filter) {
			sb.append(VKCStringUtils.filterUnicodeChars(value));
		} else {
			sb.append(value);
		}

		sb.append("\"");

		if (matched) {
			sb.append(" selected=\"selected\"");
		}

		if (style != null) {
			sb.append(" style=\"");
			sb.append(style);
			sb.append("\"");
		}

		if (styleClass != null) {
			sb.append(" class=\"");
			sb.append(styleClass);
			sb.append("\"");
		}

		sb.append(">");

		if (handleLabelAsKey) {
            VKCUser user = ServletUtils.getUser((HttpServletRequest)pageContext.getRequest());
			
            if (user != null) {
                label = WebShare.resources.getMessage(user.getLanguage().getLocale(), label);
            }
		}

		if (filter) {
			sb.append(VKCStringUtils.filterUnicodeChars(label));
		} else {
			sb.append(label);
		}

		sb.append("</option>\r\n");
	}

	/**
	 * Release any acquired resources.
	 */
	public void release() {
		super.release();
		collection = null;
		filter = true;
		handleLabelAsKey = false;
		labelName = null;
		labelProperty = null;
		name = null;
		property = null;
		style = null;
		styleClass = null;
	}
}