http://code.google.com/p/google-web-toolkit/wiki/ImageBundleDesign
Allow developers to easily create bundles of static images that can be downloaded in a single HTTP request.
Solve the “bouncy UI startup” problem in an automated way that does not require developers to specify image sizes explicitly in code.
Avoid even simple HTTP freshness checks, leaving HTTP connections available for important work.
Find the optimal way to combine individual images into a single image in multiple dimensions. Simply lining up images left-to-right to create an image strip is sufficient.
Support use cases that need a dynamic set of images, such as those in which end user input controls the set of images. For example, this mechanism is not intended as a way to implement a photo gallery.
// 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();
}
/**
* 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);
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.
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();
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;
}
}
public Image methodName(String id)
public ImageItem methodName()
# This needs a 1px transparent gif to work.
# [default images/clear.gif]
image.clear=path/relativeto/webapp
see here for a sample project.
blog comments powered by Disqus