You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by eh...@apache.org on 2007/03/30 06:36:07 UTC

svn commit: r523930 - /incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket/src/main/java/wicket/behavior/HeaderContributor.java

Author: ehillenius
Date: Thu Mar 29 21:36:01 2007
New Revision: 523930

URL: http://svn.apache.org/viewvc?view=rev&rev=523930
Log:
cleaned up the mess: got rid of instance methods, made contructor proctected, and rather than keeping a list of contributions, just keep one (slightly more efficient, but as we got rid of the instance methods, there's no need for them anymore)

Modified:
    incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket/src/main/java/wicket/behavior/HeaderContributor.java

Modified: incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket/src/main/java/wicket/behavior/HeaderContributor.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket/src/main/java/wicket/behavior/HeaderContributor.java?view=diff&rev=523930&r1=523929&r2=523930
==============================================================================
--- incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket/src/main/java/wicket/behavior/HeaderContributor.java (original)
+++ incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket/src/main/java/wicket/behavior/HeaderContributor.java Thu Mar 29 21:36:01 2007
@@ -16,16 +16,12 @@
  */
 package wicket.behavior;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import wicket.Application;
 import wicket.RequestCycle;
 import wicket.ResourceReference;
 import wicket.markup.html.IHeaderContributor;
 import wicket.markup.html.IHeaderResponse;
 import wicket.markup.html.resources.CompressedResourceReference;
-import wicket.markup.html.resources.JavascriptResourceReference;
 import wicket.protocol.http.WebRequestCycle;
 
 /**
@@ -45,53 +41,21 @@
  */
 public class HeaderContributor extends AbstractHeaderContributor
 {
-	// adds the context path on the front of the location, if it's not
-	// a fully-qualified URL.
-	private static final String returnLocationWithContextPath(String location)
-	{
-
-		// WICKET-59 allow external URLs.
-		if (location.startsWith("http://") || location.startsWith("https://"))
-		{
-			return location;
-		}
-		else
-		{
-			StringBuffer b = new StringBuffer();
-			String contextPath = Application.get().getApplicationSettings().getContextPath();
-			if (contextPath == null)
-			{
-				contextPath = ((WebRequestCycle)RequestCycle.get()).getWebRequest()
-						.getContextPath();
-				if (contextPath == null)
-				{
-					contextPath = "";
-				}
-			}
-			b.append(contextPath);
-			if (!contextPath.endsWith("/") && !location.startsWith("/"))
-			{
-				b.append("/");
-			}
-			b.append(location);
-			return b.toString();
-		}
-	}
-
 	private static final long serialVersionUID = 1L;
 
 	/**
 	 * Returns a new instance of {@link HeaderContributor} with a header
-	 * contributor that references a CSS file that lives in the web application
-	 * directory and that is addressed relative to the context path.
+	 * contributor that references a CSS file that lives in a package.
 	 * 
-	 * @param location
-	 *            The location of the css file relative to the context path
-	 * @param media
-	 *            The media type for this CSS ("print", "screen", etc.)
+	 * @param scope
+	 *            The scope of the package resource (typically the class of the
+	 *            caller, or a class that lives in the package where the
+	 *            resource lives).
+	 * @param path
+	 *            The path
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forCss(final String location, final String media)
+	public static final HeaderContributor forCss(final Class scope, final String path)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -99,21 +63,27 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderCSSReference(returnLocationWithContextPath(location), media);
+				response.renderCSSReference(new CompressedResourceReference(scope, path));
 			}
 		});
 	}
 
 	/**
 	 * Returns a new instance of {@link HeaderContributor} with a header
-	 * contributor that references a CSS file that lives in the web application
-	 * directory and that is addressed relative to the context path.
+	 * contributor that references a CSS file that lives in a package.
 	 * 
-	 * @param location
-	 *            The location of the css file relative to the context path
+	 * @param scope
+	 *            The scope of the package resource (typically the class of the
+	 *            caller, or a class that lives in the package where the
+	 *            resource lives).
+	 * @param path
+	 *            The path
+	 * @param media
+	 *            The media type for this CSS ("print", "screen", etc.)
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forCss(final String location)
+	public static final HeaderContributor forCss(final Class scope, final String path,
+			final String media)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -121,7 +91,7 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderCSSReference(returnLocationWithContextPath(location));
+				response.renderCSSReference(new CompressedResourceReference(scope, path), media);
 			}
 		});
 	}
@@ -131,12 +101,10 @@
 	 * contributor that references a CSS file that lives in a package.
 	 * 
 	 * @param reference
-	 * @param media
-	 *            The media type for this CSS ("print", "screen", etc.)
+	 * 
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forCss(final ResourceReference reference,
-			final String media)
+	public static final HeaderContributor forCss(final ResourceReference reference)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -144,7 +112,7 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderCSSReference(reference, media);
+				response.renderCSSReference(reference);
 			}
 		});
 	}
@@ -154,10 +122,12 @@
 	 * contributor that references a CSS file that lives in a package.
 	 * 
 	 * @param reference
-	 * 
+	 * @param media
+	 *            The media type for this CSS ("print", "screen", etc.)
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forCss(final ResourceReference reference)
+	public static final HeaderContributor forCss(final ResourceReference reference,
+			final String media)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -165,21 +135,21 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderCSSReference(reference);
+				response.renderCSSReference(reference, media);
 			}
 		});
 	}
 
 	/**
 	 * Returns a new instance of {@link HeaderContributor} with a header
-	 * contributor that references a JavaScript file that lives in the web
-	 * application directory and that is addressed relative to the context path.
+	 * contributor that references a CSS file that lives in the web application
+	 * directory and that is addressed relative to the context path.
 	 * 
 	 * @param location
 	 *            The location of the css file relative to the context path
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forJavaScript(final String location)
+	public static final HeaderContributor forCss(final String location)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -187,27 +157,23 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderJavascriptReference(returnLocationWithContextPath(location));
+				response.renderCSSReference(returnLocationWithContextPath(location));
 			}
 		});
 	}
 
 	/**
 	 * Returns a new instance of {@link HeaderContributor} with a header
-	 * contributor that references a CSS file that lives in a package.
+	 * contributor that references a CSS file that lives in the web application
+	 * directory and that is addressed relative to the context path.
 	 * 
-	 * @param scope
-	 *            The scope of the package resource (typically the class of the
-	 *            caller, or a class that lives in the package where the
-	 *            resource lives).
-	 * @param path
-	 *            The path
+	 * @param location
+	 *            The location of the css file relative to the context path
 	 * @param media
 	 *            The media type for this CSS ("print", "screen", etc.)
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forCss(final Class scope, final String path,
-			final String media)
+	public static final HeaderContributor forCss(final String location, final String media)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -215,14 +181,14 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderCSSReference(new CompressedResourceReference(scope, path), media);
+				response.renderCSSReference(returnLocationWithContextPath(location), media);
 			}
 		});
 	}
 
 	/**
 	 * Returns a new instance of {@link HeaderContributor} with a header
-	 * contributor that references a CSS file that lives in a package.
+	 * contributor that references a java script file that lives in a package.
 	 * 
 	 * @param scope
 	 *            The scope of the package resource (typically the class of the
@@ -232,7 +198,7 @@
 	 *            The path
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forCss(final Class scope, final String path)
+	public static final HeaderContributor forJavaScript(final Class scope, final String path)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -240,7 +206,7 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderCSSReference(new CompressedResourceReference(scope, path));
+				response.renderJavascriptReference(new CompressedResourceReference(scope, path));
 			}
 		});
 	}
@@ -249,15 +215,11 @@
 	 * Returns a new instance of {@link HeaderContributor} with a header
 	 * contributor that references a java script file that lives in a package.
 	 * 
-	 * @param scope
-	 *            The scope of the package resource (typically the class of the
-	 *            caller, or a class that lives in the package where the
-	 *            resource lives).
-	 * @param path
-	 *            The path
+	 * @param reference
+	 * 
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forJavaScript(final Class scope, final String path)
+	public static final HeaderContributor forJavaScript(final ResourceReference reference)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -265,20 +227,21 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderJavascriptReference(new CompressedResourceReference(scope, path));
+				response.renderJavascriptReference(reference);
 			}
 		});
 	}
 
 	/**
 	 * Returns a new instance of {@link HeaderContributor} with a header
-	 * contributor that references a java script file that lives in a package.
-	 * 
-	 * @param reference
+	 * contributor that references a JavaScript file that lives in the web
+	 * application directory and that is addressed relative to the context path.
 	 * 
+	 * @param location
+	 *            The location of the css file relative to the context path
 	 * @return the new header contributor instance
 	 */
-	public static final HeaderContributor forJavaScript(final ResourceReference reference)
+	public static final HeaderContributor forJavaScript(final String location)
 	{
 		return new HeaderContributor(new IHeaderContributor()
 		{
@@ -286,118 +249,61 @@
 
 			public void renderHead(IHeaderResponse response)
 			{
-				response.renderJavascriptReference(reference);
+				response.renderJavascriptReference(returnLocationWithContextPath(location));
 			}
 		});
 	}
 
-	/**
-	 * set of resource references to contribute.
-	 */
-	private List headerContributors = null;
-
-	/**
-	 * Construct.
-	 */
-	public HeaderContributor()
-	{
-	}
-
-	/**
-	 * Construct with a single header contributor.
-	 * 
-	 * @param headerContributor
-	 *            the header contributor
-	 */
-	public HeaderContributor(IHeaderContributor headerContributor)
-	{
-		headerContributors = new ArrayList(1);
-		headerContributors.add(headerContributor);
-	}
-
-	/**
-	 * Adds a custom header contributor.
-	 * 
-	 * @param headerContributor
-	 *            instance of {@link IHeaderContributor}
-	 */
-	public final void addContributor(final IHeaderContributor headerContributor)
+	// adds the context path on the front of the location, if it's not
+	// a fully-qualified URL.
+	private static final String returnLocationWithContextPath(String location)
 	{
-		checkHeaderContributors();
-		if (!headerContributors.contains(headerContributor))
+		// WICKET-59 allow external URLs.
+		if (location.startsWith("http://") || location.startsWith("https://"))
 		{
-			headerContributors.add(headerContributor);
+			return location;
 		}
-	}
-
-	/**
-	 * Adds a custom header contributor at the given position.
-	 * 
-	 * @param index
-	 *            the position where the contributor should be added (e.g. 0 to
-	 *            put it in front of the rest).
-	 * @param headerContributor
-	 *            instance of {@link IHeaderContributor}
-	 * 
-	 * @throws IndexOutOfBoundsException
-	 *             if the index is out of range (index < 0 || index >
-	 *             size()).
-	 */
-	public final void addContributor(final int index, final IHeaderContributor headerContributor)
-	{
-		checkHeaderContributors();
-		if (!headerContributors.contains(headerContributor))
+		else
 		{
-			headerContributors.add(index, headerContributor);
+			StringBuffer b = new StringBuffer();
+			String contextPath = Application.get().getApplicationSettings().getContextPath();
+			if (contextPath == null)
+			{
+				contextPath = ((WebRequestCycle)RequestCycle.get()).getWebRequest()
+						.getContextPath();
+				if (contextPath == null)
+				{
+					contextPath = "";
+				}
+			}
+			b.append(contextPath);
+			if (!contextPath.endsWith("/") && !location.startsWith("/"))
+			{
+				b.append("/");
+			}
+			b.append(location);
+			return b.toString();
 		}
 	}
 
 	/**
-	 * Adds a reference to a css file that should be contributed to the page
-	 * header.
-	 * 
-	 * @param scope
-	 *            The scope of the package resource (typically the class of the
-	 *            caller, or a class that lives in the package where the
-	 *            resource lives).
-	 * @param path
-	 *            The path
+	 * Resource reference to contribute.
 	 */
-	public final void addCssReference(final Class scope, final String path)
-	{
-		addContributor(new IHeaderContributor()
-		{
-			private static final long serialVersionUID = 1L;
-
-			public void renderHead(IHeaderResponse response)
-			{
-				response.renderCSSReference(new CompressedResourceReference(scope, path));
-			}
-		});
-	}
+	private IHeaderContributor headerContributor = null;
 
 	/**
-	 * Adds a reference to a javascript file that should be contributed to the
-	 * page header.
+	 * Construct.
 	 * 
-	 * @param scope
-	 *            The scope of the package resource (typically the class of the
-	 *            caller, or a class that lives in the package where the
-	 *            resource lives).
-	 * @param path
-	 *            The path
+	 * @param headerContributor
+	 *            the header contributor
 	 */
-	public final void addJavaScriptReference(final Class scope, final String path)
+	protected HeaderContributor(IHeaderContributor headerContributor)
 	{
-		addContributor(new IHeaderContributor()
+		if (headerContributor == null)
 		{
-			private static final long serialVersionUID = 1L;
-
-			public void renderHead(IHeaderResponse response)
-			{
-				response.renderJavascriptReference(new JavascriptResourceReference(scope, path));
-			}
-		});
+			throw new IllegalArgumentException("header contributor may not be null");
+		}
+		this.headerContributor = headerContributor;
 	}
 
 	/**
@@ -405,22 +311,6 @@
 	 */
 	public final IHeaderContributor[] getHeaderContributors()
 	{
-		if (headerContributors != null)
-		{
-			return (IHeaderContributor[])headerContributors
-					.toArray(new IHeaderContributor[headerContributors.size()]);
-		}
-		return null;
-	}
-
-	/**
-	 * Create lazily to save memory.
-	 */
-	private void checkHeaderContributors()
-	{
-		if (headerContributors == null)
-		{
-			headerContributors = new ArrayList(1);
-		}
+		return new IHeaderContributor[] { headerContributor };
 	}
 }