/*
 * $Id: OnClickLink.java,v 1.6 2005/02/14 14:18:16 hillenius Exp $
 * $Revision: 1.6 $
 * $Date: 2005/02/14 14:18:16 $
 *
 * ====================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.wehkamp.prodconf.ui.web.components;

import java.io.Serializable;

import wicket.markup.ComponentTag;
import wicket.markup.html.link.AbstractLink;
import wicket.markup.html.link.PopupSettings;
import wicket.model.IModel;

/**
 * Copy from wicket component with extra features. Verry nasty, i know but they made some
 * crusial methods final. Implementation of a special link component that can handle
 * linkClicked events (implements {@link wicket.markup.html.link.ILinkListener}) but that
 * can be used with any kind of tag. At render time, an onclick event handler is added to
 * the tag (or the existing one is replaced) like:
 * 
 * <pre>
 * onclick = &quot;location.href='/myapp/...'&quot;
 * </pre>.
 * <p>
 * The OnClickLink can be placed on any tag that has a onclick handler (e.g. buttons, tr/
 * td's etc).
 * </p>
 * <p>
 * You can use a onClickLink like:
 * 
 * <pre>
 * 
 *  
 *   
 *    
 *     
 *      
 *       add(new OnClickLink(&quot;myLink&quot;){
 *      
 *         public void linkClicked(RequestCycle cycle)
 *         {
 *            // do something here...  
 *         }
 *       );
 *       
 *      
 *     
 *    
 *   
 *  
 * </pre>
 * 
 * and in your HTML file:
 * 
 * <pre>
 * 
 *  
 *   
 *    
 *     
 *      
 *        &lt;input type=&quot;button&quot; id=&quot;wicket-remove&quot; value=&quot;push me&quot; /&gt;
 *       
 *      
 *     
 *    
 *   
 *  
 * </pre>
 * 
 * or (with a onclick handler that will be replaced but can be usefull when designing):
 * 
 * <pre>
 * 
 *  
 *   
 *    
 *     
 *      
 *        &lt;input type=&quot;button&quot; onclick=&quot;alert('test');&quot; id=&quot;wicket-remove&quot; value=&quot;push me&quot; /&gt;
 *       
 *      
 *     
 *    
 *   
 *  
 * </pre>
 * 
 * </p>
 * @author Eelco Hillenius
 * @author Marrink
 */
public abstract class OnClickLink extends AbstractLink
{
	private String jsCondition = null;

	private boolean enabled = true;

	/**
	 * Construct.
	 * @param componentName the name of the component
	 */
	public OnClickLink(String componentName)
	{
		super(componentName);
	}

	/**
	 * Constructor that uses the provided {@link IModel}as its model.
	 * @param name The non-null name of this component
	 * @param model the model
	 * @throws wicket.WicketRuntimeException Thrown if the component has been given a null
	 *             name.
	 */
	public OnClickLink(String name, IModel model)
	{
		super(name, model);
	}

	/**
	 * Constructor that uses the provided instance of {@link IModel}as a dynamic model.
	 * This model will be wrapped in an instance of {@link wicket.model.PropertyModel}
	 * using the provided expression.
	 * @param name The non-null name of this component
	 * @param model the instance of {@link IModel}from which the model object will be
	 *            used as the subject for the given expression
	 * @param expression the OGNL expression that works on the given object
	 * @throws wicket.WicketRuntimeException Thrown if the component has been given a null
	 *             name.
	 */
	public OnClickLink(String name, IModel model, String expression)
	{
		super(name, model, expression);
	}

	/**
	 * Constructor that uses the provided object as a simple model. This object will be
	 * wrapped in an instance of {@link wicket.model.Model}.
	 * @param name The non-null name of this component
	 * @param object the object that will be used as a simple model
	 * @throws wicket.WicketRuntimeException Thrown if the component has been given a null
	 *             name.
	 */
	public OnClickLink(String name, Serializable object)
	{
		super(name, object);
	}

	/**
	 * Constructor that uses the provided object as a dynamic model. This object will be
	 * wrapped in an instance of {@link wicket.model.Model}that will be wrapped in an
	 * instance of {@link wicket.model.PropertyModel}using the provided expression.
	 * @param name The non-null name of this component
	 * @param object the object that will be used as the subject for the given expression
	 * @param expression the OGNL expression that works on the given object
	 * @throws wicket.WicketRuntimeException Thrown if the component has been given a null
	 *             name.
	 */
	public OnClickLink(String name, Serializable object, String expression)
	{
		super(name, object, expression);
	}

	/**
	 * Processes the component tag.
	 * @param tag Tag to modify
	 * @see wicket.Component#onComponentTag(ComponentTag)
	 */
	protected void onComponentTag(final ComponentTag tag)
	{
		if (!enabled)
		{
			tag.put("style","cursor:not-allowed;");
			return;
		}
		// Add simple javascript on click handler that links to this
		// link's linkClicked method
		final String url = getURL();
		boolean condition = (getJsCondition() != null && !"".equals(getJsCondition().trim()));
		PopupSettings popupSpecification = getPopupSettings();
		if (popupSpecification != null)
		{
			popupSpecification.setTarget("'" + url + "'");
			String popupScript = popupSpecification.getPopupJavaScript();
			if (condition)
				tag.put("onclick", "if("
						+ getJsCondition() + "){" + popupScript + "}else return false;");
			else
				tag.put("onclick", popupScript);
		}
		else
		{
			if (condition)
				tag.put("onclick", "if("
						+ getJsCondition() + "){location.href='" + url + "';}else return false;");
			else
				tag.put("onclick", "location.href='" + url + "';");
		}
	}

	/**
	 * Gets jsCondition.
	 * @return jsCondition
	 */
	public String getJsCondition()
	{
		return jsCondition;
	}

	/**
	 * Sets a condition that will be checked before the normal action. if the check fails
	 * the action will not be executed. The check can either be a function like check() or
	 * a boolean expression like popups < 5.
	 * @param jsCondition jsCondition
	 * @return this
	 */
	public OnClickLink setJsCondition(String jsCondition)
	{
		this.jsCondition = jsCondition;
		return this;
	}

	/**
	 * @return true if the component is enabled
	 */
	public boolean isEnabled()
	{
		return enabled;
	}

	/**
	 * Enables or disables this component. If disabled the onclick tag is ommitted.
	 * @param enabled
	 * @return this
	 */
	public OnClickLink setEnabled(boolean enabled)
	{
		this.enabled = enabled;
		return this;
	}
}