Class WImage

  • All Implemented Interfaces:
    AjaxTarget, BeanAware, BeanBound, BeanProviderBound, DataBound, Targetable, WComponent, WebComponent, Serializable
    Direct Known Subclasses:
    WEditableImage

    public class WImage
    extends WBeanComponent
    implements Targetable, AjaxTarget

    The WImage component provides a way for applications to display images within their application. It may either serve up a pre-defined image which is part of the application, or generate the image dynamically.

     // Example of using a pre-defined image included in the applications class-path
     new WImage("/com/mycompany/myapp/somePackage/logo.png", "Application logo");
     
     // Example of using a dynamic image
     WImage image = new WImage();
     image.setImage(new com.github.bordertech.wcomponents.Image()
     {
         public Dimension getSize()
         {
             // Both width and height are unknown
             return new Dimension(-1, -1);
         }
    
         public String getString()
         {
             return "Include a relevant description of the image here";
         }
    
         public String getMimeType()
         {
             // e.g. if serving up a PNG image
             return "image/png";
         }
    
         public byte[] getBytes()
         {
             // read in or create the image binary data here.
         }
     });
     
    Since:
    1.0.0
    Author:
    Kishan Bisht
    See Also:
    Serialized Form
    • Constructor Detail

      • WImage

        public WImage()
        Creates a WImage with no content.
      • WImage

        public WImage​(String imageResource,
                      String description)

        Creates a WImage with the given static content. This is provided as a convenience method for when the image is included as static content in the class path rather than in the web application's resources.

        The mime type for the image is looked up from the "mimeType.*" mapping configuration parameters using the resource's file extension.

        Parameters:
        imageResource - the resource path to the image file.
        description - the image description.
      • WImage

        public WImage​(ImageResource image)
        Creates a WImage with the given image resource.
        Parameters:
        image - the image resource
    • Method Detail

      • getTargetUrl

        public String getTargetUrl()
        Creates a dynamic URL that the image can be loaded from. In fact the URL points to the main application servlet, but includes a non-null for the parameter associated with this WComponent (ie, its label). The handleRequest method below detects this when the browser requests the image
        Returns:
        the url to load the image from
      • handleRequest

        public void handleRequest​(Request request)
        When an img element is included in the html output of a page, the browser will make a second request to get the image contents. The handleRequest method has been overridden to detect whether the request is the "image content fetch" request by looking for the parameter that we encode in the image url.
        Specified by:
        handleRequest in interface WComponent
        Overrides:
        handleRequest in class AbstractWComponent
        Parameters:
        request - the request being responded to.
      • setImage

        public void setImage​(Image image)
        Sets the image.
        Parameters:
        image - the image to set.
      • setImageUrl

        public void setImageUrl​(String imageUrl)
        Sets the image to an external URL.
        Parameters:
        imageUrl - the image URL.
      • getImageUrl

        public String getImageUrl()
        Returns:
        the image to an external URL.
      • getImage

        public Image getImage()
        Retrieves the current image.
        Returns:
        the current image.
      • getCacheKey

        public String getCacheKey()
        Retrieves the cache key for this image. This is used to enable caching of the image on the client agent.
        Returns:
        the cacheKey
      • setCacheKey

        public void setCacheKey​(String cacheKey)
        A cache key is used to enable the caching of images on the client agent.

        The cache key should be unique for each image.

        Parameters:
        cacheKey - the cacheKey to set.
      • getSize

        public Dimension getSize()
        Retrieve the image size.

        Returns the size set via setSize(Dimension). If this has not been set and an image resource is provideing the image then the size of the image resource is returned. Otherwise return null.

        Returns:
        the size of the image.
      • setSize

        public void setSize​(Dimension size)
        Parameters:
        size - the size of the image.
      • getAlternativeText

        public String getAlternativeText()
        Retrieve the alternative text for the image.

        Returns the alternative text set via setAlternativeText(String). If this has not been set and an image resource is providing the image then the description of the image resource is returned. Otherwise return null.

        Returns:
        the alternative text for the image.
      • setAlternativeText

        public void setAlternativeText​(String text)
        Parameters:
        text - the alternative text for the image
      • getTargetId

        public String getTargetId()
        Returns the id to use to target this component.
        Specified by:
        getTargetId in interface Targetable
        Returns:
        this component's target id.
      • toString

        public String toString()
        Description copied from class: AbstractWComponent
        Creates a String representation of this component; usually for debugging purposes.
        Overrides:
        toString in class AbstractWComponent
        Returns:
        a String representation of this component, for debugging purposes.
      • getComponentModel

        protected WImage.ImageModel getComponentModel()
        Returns the effective component model for this component. Subclass may override this method to narrow the return type to their specific model type.
        Overrides:
        getComponentModel in class WBeanComponent
        Returns:
        the effective component model
      • getOrCreateComponentModel

        protected WImage.ImageModel getOrCreateComponentModel()
        Retrieves the model for this component so that it can be modified. If this method is called during request processing, and a session specific model does not yet exist, then a new model is created. Subclasses may override this method to narrow the return type to their specific model type.
        Overrides:
        getOrCreateComponentModel in class WBeanComponent
        Returns:
        the model for this component