/*
 * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar
 * 2006) eelco12 $ $Revision: 5004 $ $Date: 2006-03-17 20:47:08 -0800 (Fri, 17
 * Mar 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.behavior;

import wicket.Component;
import wicket.Response;
import wicket.markup.html.IHeaderContributor;
import wicket.model.IModel;
import wicket.model.Model;

/**
 * A simple header contributor that just spits out the string it is constructed
 * with as a header contribution.
 * 
 * @author Eelco Hillenius
 */
public class StringHeaderContributor extends AbstractHeaderContributor
{
	private static final long serialVersionUID = 1L;

	/** the contributor instance. */
	private final StringContributor contributor;

	/**
	 * Simply writes out the string it was constructed with whenever it is
	 * called for a header contribution.
	 */
	private static final class StringContributor implements IHeaderContributor
	{
		private static final long serialVersionUID = 1L;

		/** The contribution as a model that returns a plain string. */
		private final IModel contribution;

		/**
		 * Construct.
		 * 
		 * @param contribution
		 *            The contribution as a plain string
		 */
		public StringContributor(String contribution)
		{
			if (contribution == null)
			{
				throw new IllegalArgumentException("argument contribition must be not null");
			}

			this.contribution = new Model(contribution);
		}

		/**
		 * Construct.
		 * 
		 * @param contribution
		 *            The contribution as a model that returns a plain string
		 */
		public StringContributor(IModel contribution)
		{
			if (contribution == null)
			{
				throw new IllegalArgumentException("argument contribition must be not null");
			}

			this.contribution = contribution;
		}

		/**
		 * @see wicket.markup.html.IHeaderContributor#renderHead(wicket.Response)
		 */
		public void renderHead(Response response)
		{
			Object object = contribution.getObject(null);
			if (object != null)
			{
				response.println(object.toString());
			}
		}

		/**
		 * @see java.lang.Object#hashCode()
		 */
		public int hashCode()
		{
			return contribution.hashCode();
		}

		/**
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		public boolean equals(Object obj)
		{
			if (obj instanceof StringContributor)
			{
				return ((StringContributor)obj).equals(this);
			}
			return false;
		}

		/**
		 * @see java.lang.Object#toString()
		 */
		public String toString()
		{
			return "StringContributor[contribution=" + contribution + "]";
		}
	}

	/**
	 * Construct.
	 * 
	 * @param contribution
	 *            header contribution as a plain string
	 */
	public StringHeaderContributor(String contribution)
	{
		contributor = new StringContributor(contribution);
	}

	/**
	 * Construct.
	 * 
	 * @param contribution
	 *            header contribution as a model that returns a plain string
	 */
	public StringHeaderContributor(IModel contribution)
	{
		contributor = new StringContributor(contribution);
	}

	/**
	 * @see wicket.behavior.AbstractHeaderContributor#getHeaderContributors()
	 */
	public final IHeaderContributor[] getHeaderContributors()
	{
		return new IHeaderContributor[] { contributor };
	}

	/**
	 * @see wicket.behavior.AbstractBehavior#detachModel(wicket.Component)
	 */
	public void detachModel(Component component)
	{
		contributor.contribution.detach();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return "StringHeaderContributor[contribution=" + contributor.contribution.getObject(null)
				+ "]";
	}
}
@@ -985,16 +985,49 @@
 l.Model;
+%0Aimport wicket.util.lang.Objects;
 %0A%0A/**%0A *
@@ -1733,16 +1733,113 @@
 ution;%0A%0A
+%09%09/** Temp var for the actual contribution. */%0A%09%09private transient String contributionAsString;%0A%0A
 %09%09/**%0A%09%09
@@ -2863,27 +2863,89 @@
 %0A%09%09%09
-return contribution
+Object object = contribution.getObject(null);%0A%09%09%09return (object != null) ? object
 .has
@@ -2951,16 +2951,20 @@
 shCode()
+ : 0
 ;%0A%09%09%7D%0A%0A%09
@@ -3120,51 +3120,188 @@
 %09%09%09%09
-return ((StringContributor)obj)
+Object thisContrib = contribution.getObject(null);%0A%09%09%09%09Object thatContrib = ((StringContributor)obj).contribution.getObject(null);%0A%09%09%09%09return Objects
 .equal
-s
 (this
+Contrib, thatContrib
 );%0A%09

fix for wrong hashCode/ equals