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

import java.beans.PropertyEditorSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BooleanEditorSiNo extends PropertyEditorSupport {
	Log log = LogFactory.getLog(BooleanEditorSiNo.class);

	private Boolean content;

	/**
	 * Create a new CustomDateEditor instance, using the given DateFormat
	 * for parsing and rendering.
	 * <p>The "allowEmpty" parameter states if an empty String should
	 * be allowed for parsing, i.e. get interpreted as null value.
	 * Otherwise, an IllegalArgumentException gets thrown in that case.
	 */
	public BooleanEditorSiNo() {
	}

	/* (non-Javadoc)
	 * @see java.beans.PropertyEditorSupport#getAsText()
	 */
	@Override
	public String getAsText() {
		try {
			if (content == null) {
				return "null";
			}
			return content ? "Si" : "No";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/* (non-Javadoc)
	 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
	 */
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		try {
			if (text == null || text.equalsIgnoreCase("null") || text.equals("")) {
				content = null;
			} else {
				content = text.equalsIgnoreCase("si") || text.equalsIgnoreCase("S") || text.equalsIgnoreCase("Y") || text.equalsIgnoreCase("on") ? true : false;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void setValue(Object value) {
		content = (Boolean)value;
	}

	@Override
	public Object getValue() {
		return content;
	}

}
