You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by gs...@apache.org on 2007/11/03 20:57:45 UTC

svn commit: r591670 - /wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/ImageButton.java

Author: gseitz
Date: Sat Nov  3 12:57:45 2007
New Revision: 591670

URL: http://svn.apache.org/viewvc?rev=591670&view=rev
Log:
WICKET-975: Update ImageButton to handle ResourceReference

Modified:
    wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/ImageButton.java

Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/ImageButton.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/ImageButton.java?rev=591670&r1=591669&r2=591670&view=diff
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/ImageButton.java (original)
+++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/ImageButton.java Sat Nov  3 12:57:45 2007
@@ -16,12 +16,16 @@
  */
 package org.apache.wicket.markup.html.form;
 
+import org.apache.wicket.Component;
 import org.apache.wicket.IResourceListener;
+import org.apache.wicket.Resource;
 import org.apache.wicket.ResourceReference;
 import org.apache.wicket.markup.ComponentTag;
-import org.apache.wicket.markup.html.WebResource;
-import org.apache.wicket.markup.html.image.resource.DefaultButtonImageResource;
 import org.apache.wicket.markup.html.image.resource.LocalizedImageResource;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.util.value.ValueMap;
+
 
 /**
  * <input type="image"> component - like {@link Button} only with an image.
@@ -39,55 +43,102 @@
 	private final LocalizedImageResource localizedImageResource = new LocalizedImageResource(this);
 
 	/**
+	 * This constructor can be used if you have an <code>img</code> tag that has a
+	 * <code>src</code> that points to a <code>PackageResource</code> (which will be created and
+	 * bind to the shared resources) or if you have a <code>value</code> attribute in your tag for
+	 * which the image factory can make an image.
+	 * 
 	 * @see org.apache.wicket.Component#Component(String)
 	 */
-	public ImageButton(String id)
+	public ImageButton(final String id)
 	{
 		super(id);
 	}
 
 	/**
-	 * Constructs an image button directly from an image resource.
+	 * Constructs an image button from an image <code>ResourceReference</code>. That resource
+	 * reference will bind its resource to the current SharedResources.
+	 * 
+	 * If you are using non sticky session clustering and the resource reference is pointing to a
+	 * <code>Resource</code> that isn't guaranteed to be on every server, for example a dynamic
+	 * image or resources that aren't added with a <code>IInitializer</code> at application
+	 * startup. Then if only that resource is requested from another server, without the rendering
+	 * of the page, the image won't be there and will result in a broken link.
 	 * 
 	 * @param id
 	 *            See Component
-	 * 
-	 * @param imageResource
-	 *            The image resource
+	 * @param resourceReference
+	 *            The shared image resource
 	 */
-	public ImageButton(final String id, final WebResource imageResource)
+	public ImageButton(final String id, final ResourceReference resourceReference)
 	{
-		super(id);
-		this.localizedImageResource.setResource(imageResource);
+		this(id, resourceReference, null);
 	}
 
 	/**
-	 * Constructs an image directly from an image resource.
+	 * Constructs an image button from an image <code>ResourceReference</code>. That resource
+	 * reference will bind its resource to the current SharedResources.
+	 * 
+	 * If you are using non sticky session clustering and the resource reference is pointing to a
+	 * <code>Resource</code> that isn't guaranteed to be on every server, for example a dynamic
+	 * image or resources that aren't added with a <code>IInitializer</code> at application
+	 * startup. Then if only that resource is requested from another server, without the rendering
+	 * of the page, the image won't be there and will result in a broken link.
 	 * 
 	 * @param id
 	 *            See Component
 	 * @param resourceReference
 	 *            The shared image resource
+	 * @param resourceParameters
+	 *            The resource parameters
 	 */
-	public ImageButton(final String id, final ResourceReference resourceReference)
+	public ImageButton(final String id, final ResourceReference resourceReference,
+			ValueMap resourceParameters)
 	{
 		super(id);
-		localizedImageResource.setResourceReference(resourceReference);
+		setImageResourceReference(resourceReference, resourceParameters);
 	}
 
 	/**
-	 * Constructor
+	 * Constructs an image directly from an image resource.
+	 * 
+	 * This one doesn't have the 'non sticky session clustering' problem that the
+	 * <code>ResourceReference</code> constructor has. But this will result in a non 'stable' url
+	 * and the url will have request parameters.
+	 * 
+	 * @param id
+	 *            See Component
 	 * 
+	 * @param imageResource
+	 *            The image resource
+	 */
+	public ImageButton(final String id, final Resource imageResource)
+	{
+		super(id);
+		setImageResource(imageResource);
+	}
+
+	/**
+	 * @see org.apache.wicket.Component#Component(String, IModel)
+	 */
+	public ImageButton(final String id, final IModel model)
+	{
+		super(id, model);
+	}
+
+	/**
 	 * @param id
 	 *            See Component
-	 * @param label
-	 *            The button label
+	 * @param string
+	 *            Name of image
+	 * @see org.apache.wicket.Component#Component(String, IModel)
 	 */
-	public ImageButton(final String id, final String label)
+	public ImageButton(final String id, final String string)
 	{
-		this(id, new DefaultButtonImageResource(label));
+		this(id, new Model(string));
 	}
 
+
 	/**
 	 * @see org.apache.wicket.IResourceListener#onResourceRequested()
 	 */
@@ -97,6 +148,64 @@
 	}
 
 	/**
+	 * @param imageResource
+	 *            The new ImageResource to set.
+	 */
+	public void setImageResource(final Resource imageResource)
+	{
+		localizedImageResource.setResource(imageResource);
+	}
+
+	/**
+	 * @param resourceReference
+	 *            The shared ImageResource to set.
+	 */
+	public void setImageResourceReference(final ResourceReference resourceReference)
+	{
+		localizedImageResource.setResourceReference(resourceReference);
+	}
+
+	/**
+	 * @param resourceReference
+	 *            The shared ImageResource to set.
+	 * @param parameters
+	 *            Set the resource parameters for the resource.
+	 */
+	public void setImageResourceReference(final ResourceReference resourceReference,
+			final ValueMap parameters)
+	{
+		localizedImageResource.setResourceReference(resourceReference, parameters);
+	}
+
+	/**
+	 * @see org.apache.wicket.Component#setModel(org.apache.wicket.model.IModel)
+	 */
+	public Component setModel(IModel model)
+	{
+		// Null out the image resource, so we reload it (otherwise we'll be
+		// stuck with the old model.
+		localizedImageResource.setResourceReference(null);
+		localizedImageResource.setResource(null);
+		return super.setModel(model);
+	}
+
+	/**
+	 * @return Resource returned from subclass
+	 */
+	protected Resource getImageResource()
+	{
+		return localizedImageResource.getResource();
+	}
+
+	/**
+	 * @return ResourceReference returned from subclass
+	 */
+	protected ResourceReference getImageResourceReference()
+	{
+		return localizedImageResource.getResourceReference();
+	}
+
+	/**
 	 * Processes the component tag.
 	 * 
 	 * @param tag
@@ -108,6 +217,17 @@
 		checkComponentTag(tag, "input");
 		checkComponentTagAttribute(tag, "type", "image");
 		super.onComponentTag(tag);
+
+		final Resource resource = getImageResource();
+		if (resource != null)
+		{
+			localizedImageResource.setResource(resource);
+		}
+		final ResourceReference resourceReference = getImageResourceReference();
+		if (resourceReference != null)
+		{
+			localizedImageResource.setResourceReference(resourceReference);
+		}
 		localizedImageResource.setSrcAttribute(tag);
 	}
 
@@ -116,6 +236,6 @@
 	 */
 	protected boolean getStatelessHint()
 	{
-		return localizedImageResource.isStateless();
+		return getImageResource() == null && localizedImageResource.isStateless();
 	}
 }