Image Managers

Image Managers provide a convenient method for handling nextapp.echo.ImageReference objects used in a rendered component. Images used in an Echo application can come from a variety of sources:

  • HttpImageReference - downloaded from a URI
  • AwtImageReference -generated from a java.awt.Image (automatically rendered to a PNG image)
  • StreamImageReference - retrieved from a database or other resource

An ImageManager allows a rendered component to work with the variety of available image sources without having to do any extra work. All the developer is required to do is add images to the image manager and then request their URIs at rendering time. In the case of AwtImageReference and StreamImageReference, the required Service will automatically be registered as an ancillary service with the rendering peer class. If the rendering peer class implements ImageUpdateListener, it will automatically be registered to receive events when the Image Manager is made aware of changes to its images.

Using Image Managers

To use an image manager with a rendering peer class, create a single instance of a nextapp.echoservlet.image.ImageManager. One image manager can handle multiple images for a rendering peer. The constructor of ImageManager requires that you pass in the ComponentPeer object that it is to support. This is necessary as it will be automatically registering and unregistering ancillary services to the rendering peer when required.

ImageManager imageManager = new ImageManager(this);

The registered() and unregistered() methods of the rendering peer should be modified to add images to and remove images from the ImageManager:

registered() {
    :
    imageManager.setImage("image1", getImageOne());
    :
}

unregistered() {
    :
    imageManager.setImage("image1", null);
    :
}

The rendering peer's PropertyChangeListener will need to be modified. The setImage() method should be called on the ImageManager when the image changes.

public void propertyChange(PropertyChangeEvent e) {
    :
    if (ImageExample.IMAGE_ONE_CHANGED_PROPERTY.equals(
            e.getPropertyName())) {
        ImageExample imageExample 
                = (ImageExample) getComponent();
        imageManager.setImage("image1", 
                imageExample.getImageOne());
    }
}

To display an image managed by an ImageManager within a document, use the getImageUri() method:

public void render(RenderingContext rc, Element parent) {
    :
    imageElement = new Element("img", false);
    imageElement.addAttribute("src", 
           imageManager.getImageUri(rc.getConnection(), 
           "image1"));
    :
}