/*
 * $Id: AjaxFormComponentUpdatingBehavior.java,v 1.4 2006/02/02 18:49:46
 * ivaynberg Exp $ $Revision$ $Date$
 * 
 * ==============================================================================
 * 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 wicket.ajax.form;

import wicket.WicketRuntimeException;
import wicket.ajax.AjaxEventBehavior;
import wicket.ajax.AjaxRequestTarget;
import wicket.markup.html.form.FormComponent;
import wicket.util.string.AppendingStringBuffer;

/**
 * A behavior that updates the hosting FormComponent via ajax when an event it
 * is attached to is triggered.
 */
public abstract class AjaxFormComponentUpdatingBehavior extends AjaxEventBehavior
{
	/**
	 * Construct.
	 * 
	 * @param event
	 *            event to trigger this behavior
	 */
	public AjaxFormComponentUpdatingBehavior(final String event)
	{
		super(event);
	}

	/**
	 * 
	 * @see wicket.behavior.AbstractAjaxBehavior#onBind()
	 */
	protected void onBind()
	{
		if (!(getComponent() instanceof FormComponent))
		{
			throw new WicketRuntimeException("Behavior " + getClass().getName()
					+ " can only be added to an isntance of a FormComponent");
		}
	}


	/**
	 * 
	 * @return FormComponent
	 */
	protected final FormComponent getFormComponent()
	{
		return (FormComponent)getComponent();
	}

	/**
	 * 
	 * @see wicket.ajax.AjaxEventBehavior#getEventHandler()
	 */
	protected final String getEventHandler()
	{
		FormComponent fc = getFormComponent();

		String url = getCallbackUrl();
		AppendingStringBuffer buff = new AppendingStringBuffer(url.length() + 128);
		buff.append("wicketAjaxGet('");
		buff.append(url);
		buff.append("&");
		buff.append(fc.getInputName());
		buff.append("='+");
		buff.append("wicketGetValue(this));");

		return buff.toString();
	}

	/**
	 * 
	 * @see wicket.ajax.AjaxEventBehavior#onEvent(wicket.ajax.AjaxRequestTarget)
	 */
	protected final void onEvent(final AjaxRequestTarget target)
	{
		FormComponent fc = getFormComponent();
		fc.registerNewUserInput();
		fc.validate();
		if (fc.hasErrorMessage())
		{
			fc.invalid();
		}
		else
		{
			fc.valid();
			fc.updateModel();
			// TODO Ajax: Do we need to persist values for persistent components
		}

		onUpdate(target);
	}

	/**
	 * Listener invoked on the ajax request. This listener is invoked after the
	 * component's model has been updated.
	 * 
	 * @param target
	 */
	protected abstract void onUpdate(final AjaxRequestTarget target);
}

@@ -2429,24 +2429,30 @@
 arget)%0A%09%7B%0A%09%09
+final 
 FormComponen
@@ -2446,33 +2446,44 @@
  FormComponent f
-c
+ormComponent
  = getFormCompon
@@ -2492,17 +2492,28 @@
 t();%0A%09%09f
-c
+ormComponent
 .registe
@@ -2532,17 +2532,28 @@
 t();%0A%09%09f
-c
+ormComponent
 .validat
@@ -2564,17 +2564,28 @@
 %0A%09%09if (f
-c
+ormComponent
 .hasErro
@@ -2604,17 +2604,28 @@
 %09%09%7B%0A%09%09%09f
-c
+ormComponent
 .invalid
@@ -2647,17 +2647,28 @@
 %09%09%7B%0A%09%09%09f
-c
+ormComponent
 .valid()
@@ -2673,17 +2673,28 @@
 ();%0A%09%09%09f
-c
+ormComponent
 .updateM

Long name.