Inspiration

http://code.google.com/p/google-web-toolkit/wiki/ImageBundleDesign

Goals

	// bundle class
@ImageBundle
interface WordProcessorImage {
  /**
   * Would match either file 'newFileIcon.gif' or 'newFileIcon.png'
   * in the same package as this type.
   * Note that other file extensions may also be recognized.
   */
  public Image newFileIcon(String id);
  /**
   * Would bundle the file 'open-file-icon.gif' residing in the same package as this type.
   */
  @Resource("open-file-icon.gif")
  public Image openFileIcon(String id);
  /**
   * Would bundle the file 'savefile.gif' residing in the folder icons relative to this package.
   */
  @Resource("icons/savefile.gif")
  public ImageItem saveFileIcon();
}

Using an Image Bundle

  /**
   *  If you configured eclipse properly WordProcessorImageBundle Class will be automatically created after you save
   *  the  WordProcessorImage interface
   */
  WordProcessorImage bundle = new WordProcessorImageBundle();
  add(bundle.openFileIcon("openfile"));
  ImageItem imageItem = bundle.saveFileIcon();
  Image img = new Image(id);
  img.add(new SimpleAttributeModifier("src", imageItem.getSrc()));
  img.add(new SimpleAttributeModifier("style", imageItem.getStyle()));
  add(img);

Localization

ImageBundler provides support for using different images for different locales.

  // localization eg
  @ImageBundle(locale = { "ta_IN" })
  public interface SampleImage
  {
	public ImageItem a();
	public ImageItem b();
	@Resource("c.png")
	public Image sample(String id);
  }

For each method there should be a image file without any locale(default). so for the above eg. there should be three images namely a.*,b.*,c.png.

The image file for the Tamil locale should be named as a_ta_IN.*,b_ta_IN.*,c_ta_IN.png. If you leave any of the image file for Tamil locale then the default image will be used instead.

ImageItem

The style and src of the image is available through the ImageItem interface. To get the ImageItem change the method signature as follows

public ImageItem methodName();

How it Works

JDK 6 Annotation processor is used to generate source files.A sample generated file.

public class SampleImageBundle implements SampleImage
{
	@Override
	public ImageItem a()
	{
		return new AbstractImageItem("images/clear.gif")
		{
			@Override
			public String getStyle()
			{
				String locale = RequestCycle.get().getSession().getLocale().toString();
				String style = " background-image :url(resources/org.imagebundler.wicket.examples.SampleImageBundle/SampleImageBundle.png) ; background-position:-48px -0px; width:24px; height:24px;  ";
				if(locale.equals("ta_IN"))
				{
					style = " background-image :url(resources/org.imagebundler.wicket.examples.SampleImageBundle/SampleImageBundle_ta_IN.png) ; background-position:-50px -0px; width:25px; height:25px;  ";
				}
				return style;
			}
		}
	}
	
	@Override
	public ImageItem b()
	{
		return new AbstractImageItem("images/clear.gif")
		{
			@Override
			public String getStyle()
			{
				String locale = RequestCycle.get().getSession().getLocale().toString();
				String style = " background-image :url(resources/org.imagebundler.wicket.examples.SampleImageBundle/SampleImageBundle.png) ; background-position:-24px -0px; width:24px; height:24px;  ";
				if(locale.equals("ta_IN"))
				{
					style = " background-image :url(resources/org.imagebundler.wicket.examples.SampleImageBundle/SampleImageBundle_ta_IN.png) ; background-position:-25px -0px; width:25px; height:25px;  ";
				}
				return style;
			}
		}
	}
	
	@Override
	public Image sample(String id)
	{
		Image image = new Image(id);
		image.add(new SimpleAttributeModifier("src", "images/clear.gif"));
		String locale = RequestCycle.get().getSession().getLocale().toString();
		String style = " background-image :url(resources/org.imagebundler.wicket.examples.SampleImageBundle/SampleImageBundle.png) ; background-position:-0px -0px; width:24px; height:24px;  ";
				if(locale.equals("ta_IN"))
				{
					style = " background-image :url(resources/org.imagebundler.wicket.examples.SampleImageBundle/SampleImageBundle_ta_IN.png) ; background-position:-0px -0px; width:25px; height:25px;  ";
				}
				image.add(new SimpleAttributeModifier("style", style));
		return image;
	}
}

Configuration

Interface

public Image methodName(String id)
public ImageItem methodName()

imagebundler.properties

# This needs a 1px transparent gif to work.
# [default images/clear.gif]
image.clear=path/relativeto/webapp

Example

see here for a sample project.

blog comments powered by Disqus