/******************************************************************************
 * Product: Posterita Ajax UI 												  *
 * Copyright (C) 2007 Posterita Ltd.  All Rights Reserved.                    *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * Posterita Ltd., 3, Draper Avenue, Quatre Bornes, Mauritius                 *
 * or via info@posterita.org or http://www.posterita.org/                     *
 *****************************************************************************/
package org.adempiere.webui.component;
import org.compiere.model.Obscure;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
/**
 * Extend {@link org.zkoss.zul.Textbox}.
 * @author  Ashley G Ramdass
 * @date    Feb 25, 2007
 * @version $Revision: 0.10 $
 */
public class Textbox extends org.zkoss.zul.Textbox implements EventListener
{
    /**
	 * 
	 */
	private static final long serialVersionUID = -3919623360765045602L;
	/** Optional obscure support */
	private Obscure	m_obscure = null;
	/** true if text box is in focus */
	private boolean m_infocus;
	/**
	 * Default constructor
	 */
    public Textbox()
    {
        super();
        addFocusListener(this);
    }
    /**
     * @param value
     * @throws WrongValueException
     */
    public Textbox(String value) throws WrongValueException
    {
        super(value);
        addFocusListener(this);
    }
    /**
     * Enable/disable text box
     * @param enabled
     */
    public void setEnabled(boolean enabled)
    {
        this.setDisabled(!enabled);
    }
    @Override
    public void setReadonly(boolean readonly) {
    	super.setReadonly(readonly);
    	this.setDisabled(readonly);
    }
    /**
     * Set obscure type
     * @param obscureType One of OBSCURETYPE_* at {@link Obscure}.
     */
    public void setObscureType(String obscureType)
    {
    	if (obscureType != null && obscureType.length() > 0)
		{
			m_obscure = new Obscure ("", obscureType);
		}
    	else
    	{
    		m_obscure = null;
    	}
    	setValue(getValue());
    }
    /**
     * Add ON_FOCUS and ON_BLUR listener
     * @param listener
     */
	public void addFocusListener(EventListener listener) {
		addEventListener(Events.ON_FOCUS, listener);
		addEventListener(Events.ON_BLUR, listener);
	}
	
	@Override
	public String getValue() throws WrongValueException {
		String value = super.getValue();
		if (m_obscure != null && value != null && value.length() > 0)
		{
			if (!isReadonly() && value.equals(m_obscure.getObscuredValue(getMaxlength())))
				value = m_obscure.getClearValue();
		}
		return value;
	}
	@Override
	public void setValue(String value) throws WrongValueException {
		if (m_obscure != null && ("password".equals(getType()) || !m_infocus))
		{
			super.setValue(m_obscure.getObscuredValue(value, getMaxlength()));
		}
		else
		{
			super.setValue(value);
		}
	}
	@Override
	public void onEvent(Event event) throws Exception {
		if (Events.ON_FOCUS.equals(event.getName()))
		{
			m_infocus = true;
			if (m_obscure != null)
				setValue(getValue());
		}
		else if (Events.ON_BLUR.equals(event.getName()))
		{
			m_infocus = false;
			if (m_obscure != null)
				setValue(getValue());
		}		
	}
	@Override
	public void setType(String type) throws WrongValueException {
		if ("password".equals(type))
			setObscureType(Obscure.OBSCURETYPE_ObscureMaskMax10Asterisk);
		super.setType(type);
        setClientAttribute("autocomplete", "new-password");
	}
}