/*
 * $Id: AbstractTime.java 5791 2006-05-20 00:32:57 +0000 (Sat, 20 May 2006)
 * joco01 $ $Revision: 5874 $ $Date: 2006-05-20 00:32:57 +0000 (Sat, 20 May
 * 2006) $
 * 
 * ==============================================================================
 * 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.extensions.ajax.markup.html.autocomplete;

import java.util.Iterator;

import wicket.IRequestTarget;
import wicket.RequestCycle;
import wicket.Response;

/**
 * This behavior builds on top of {@link AbstractAutoCompleteBehavior} by
 * introducing the concept of a {@link IAutoCompleteRenderer} to make response
 * writing easier.
 * 
 * @see IAutoCompleteRenderer
 * 
 * @since 1.2
 * 
 * @author Igor Vaynberg (ivaynberg)
 * @author Janne Hietam&auml;ki (jannehietamaki)
 */
public abstract class AutoCompleteBehavior extends AbstractAutoCompleteBehavior
{
	private static final long serialVersionUID = 1L;

	private final IAutoCompleteRenderer renderer;

	/**
	 * Constructor
	 * 
	 * @param renderer
	 *            renderer that will be used to generate output
	 */
	public AutoCompleteBehavior(IAutoCompleteRenderer renderer)
	{
		if (renderer == null)
		{
			throw new IllegalArgumentException("renderer cannot be null");
		}
		this.renderer = renderer;
	}


	@Override
	protected final void onRequest(final String val, RequestCycle requestCycle)
	{
		IRequestTarget target = new IRequestTarget()
		{
			public void respond(RequestCycle requestCycle)
			{
				Response r = requestCycle.getResponse();
				Iterator comps = getChoices(val);
				renderer.renderHeader(r);
				while (comps.hasNext())
				{
					final Object comp = comps.next();
					renderer.render(comp, r, val);
				}
				renderer.renderFooter(r);
			}

			public void detach(RequestCycle requestCycle)
			{
			}

			public Object getLock(RequestCycle requestCycle)
			{
				return requestCycle.getSession();
			}

		};
		requestCycle.setRequestTarget(target);
	}

	/**
	 * Callback method that should return an iterator over all possiblet
	 * choice objects. These objects will be passed to the renderer to generate
	 * output. Usually it is enough to return an iterator over strings.
	 * 
	 * @param input
	 *            current input
	 * @return iterator ver all possible choice objects
	 */
	protected abstract Iterator getChoices(String input);
}

@@ -906,24 +906,52 @@
 terator;%0D%0A%0D%0A
+import wicket.Application;%0D%0A
 import wicke
@@ -1012,16 +1012,33 @@
  wicket.
+protocol.http.Web
 Response
@@ -2094,16 +2094,19 @@
 %09%7B%0D%0A%09%09%09%09
+Web
 Response
@@ -2110,16 +2110,29 @@
 nse r = 
+(WebResponse)
 requestC
@@ -2148,24 +2148,464 @@
 esponse();%0D%0A
+%09%09%09%09%0D%0A%09%09%09%09// Determine encoding%0D%0A%09%09%09%09final String encoding = Application.get().getRequestCycleSettings().getResponseRequestEncoding();%0D%0A%09%09%09%09r.setCharacterEncoding(encoding);%0D%0A%09%09%09%09r.setContentType(%22text/xml; charset=%22 + encoding);%0D%0A%0D%0A%09%09%09%09// Make sure it is not cached%0D%0A%09%09%09%09r.setHeader(%22Expires%22, %22Mon, 26 Jul 1997 05:00:00 GMT%22);%0D%0A%09%09%09%09r.setHeader(%22Cache-Control%22, %22no-cache, must-revalidate%22);%0D%0A%09%09%09%09r.setHeader(%22Pragma%22, %22no-cache%22);%0D%0A%09%09%09%09%0D%0A
 %09%09%09%09Iterator

Set the AutoComplete response headers properly