Class SelectiveCachableXMLImageLoader

  • All Implemented Interfaces:
    IDataImageLoader, IImageLoader

    public class SelectiveCachableXMLImageLoader
    extends java.lang.Object
    implements IDataImageLoader
    Image loader based on an XML file. The XML file contains meta information for the images. The meta information for each image in the file is stored. Images of types 'n' (sprites) and 'c' (single cachable images). The caching happens on initialisation. For non cached images they are loaded each time when requested.
    Author:
    Andi Hotz, (c) Sahits GmbH, 2017 Created on Jan 22, 2017
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String fileName  
      protected static java.lang.String IMAGE_DIR  
      private java.util.Map<ImageMapKey,​javafx.scene.image.Image> imageCache
      Cache of the images.
      private java.util.Map<java.lang.String,​ImageData> imageData
      Maps storing additional image data.
      protected java.util.HashMap<java.lang.String,​java.lang.String> imageNameMap
      Mapping the name of the image to the file name.
      private org.springframework.oxm.Unmarshaller unmarshaller  
    • Constructor Summary

      Constructors 
      Constructor Description
      SelectiveCachableXMLImageLoader​(java.lang.String fileName)
      Constructor loading language specific images from the xml file fileName
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addImage​(javafx.scene.image.Image image, java.lang.String imgFileName, ch.sahits.game.graphic.data.image.ImageType imageType)
      Add an image that was loaded externaly.
      javafx.scene.image.Image getImage​(java.lang.String name)
      If the image is cached retreve it's reference, otherwise load it.
      javafx.scene.image.Image getImage​(java.lang.String name, double destWidth, double destHeight)
      If the image with the specified dimensions is cached it is retrieved from there, otherwise it is loaded.
      ImageData getImageData​(java.lang.String imageName)
      Retrieve the image data for a image name, not it's file name
      private java.lang.String getPrefix​(java.lang.String fnm)
      Extract the name before the last '.'
      private javax.xml.transform.stream.StreamSource getSourceFromFile​(java.lang.String fileName)  
      private void initLoader()  
      private boolean isCachable​(ch.sahits.game.graphic.data.image.ImageType type)  
      (package private) boolean isLoaded​(java.lang.String name)
      Check if the image has been loaded into the cache.
      javafx.scene.image.Image loadImage​(java.lang.String fnm)
      Load the image from fnm, returning it as a Image.
      javafx.scene.image.Image loadImage​(java.lang.String fnm, double width, double height)
      Load the image from fnm with the defined dimensions, returning it as a Image.
      private boolean loadImageData​(java.lang.String imageName)
      Verify that the image can be loaded and register its mapping for actual image name to its shortend name.
      (package private) void loadImageFile()  
      private boolean loadImageIntoCache​(java.lang.String imageName)
      Verify that the image is not yet cached and can be loaded.
      private void parseImageDefinitionAndLoad​(java.lang.String type, ImageData imgData)
      Register the image name and it's meta data and if required by it's type also store it in the cache.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • imageCache

        private java.util.Map<ImageMapKey,​javafx.scene.image.Image> imageCache
        Cache of the images.
      • imageNameMap

        protected java.util.HashMap<java.lang.String,​java.lang.String> imageNameMap
        Mapping the name of the image to the file name.
      • imageData

        private java.util.Map<java.lang.String,​ImageData> imageData
        Maps storing additional image data. Key is the image file name
      • unmarshaller

        @Autowired
        @Qualifier("jaxb2MarshallerImage")
        private org.springframework.oxm.Unmarshaller unmarshaller
      • fileName

        private final java.lang.String fileName
    • Constructor Detail

      • SelectiveCachableXMLImageLoader

        public SelectiveCachableXMLImageLoader​(java.lang.String fileName)
        Constructor loading language specific images from the xml file fileName
        Parameters:
        fileName - XML file name
    • Method Detail

      • initLoader

        private void initLoader()
      • loadImageFile

        @PostConstruct
        void loadImageFile()
      • getSourceFromFile

        private javax.xml.transform.stream.StreamSource getSourceFromFile​(java.lang.String fileName)
      • parseImageDefinitionAndLoad

        private void parseImageDefinitionAndLoad​(java.lang.String type,
                                                 ImageData imgData)
        Register the image name and it's meta data and if required by it's type also store it in the cache.
        Parameters:
        type - of the image. type 'n' and 'c' are put into the cache.
        imgData - image meta data.
      • loadImageData

        private boolean loadImageData​(java.lang.String imageName)
        Verify that the image can be loaded and register its mapping for actual image name to its shortend name.
        Parameters:
        imageName - image name
        Returns:
        true if the image could be loaded and the image name is cached
      • loadImageIntoCache

        private boolean loadImageIntoCache​(java.lang.String imageName)
        Verify that the image is not yet cached and can be loaded. If that is the case then it is put into the cache and it's file name is mapped to the shortened name.
        Parameters:
        imageName - image name
        Returns:
        true if the image could be loaded and is placed in the cache
      • getPrefix

        private java.lang.String getPrefix​(java.lang.String fnm)
        Extract the name before the last '.'
        Parameters:
        fnm - filename
        Returns:
        image name
      • isCachable

        private boolean isCachable​(ch.sahits.game.graphic.data.image.ImageType type)
      • addImage

        public void addImage​(javafx.scene.image.Image image,
                             java.lang.String imgFileName,
                             ch.sahits.game.graphic.data.image.ImageType imageType)
        Add an image that was loaded externaly.
        Parameters:
        image - to be put into the cache
        imgFileName - file name of the image
        imageType - type of the image
      • getImageData

        public ImageData getImageData​(java.lang.String imageName)
        Description copied from interface: IDataImageLoader
        Retrieve the image data for a image name, not it's file name
        Specified by:
        getImageData in interface IDataImageLoader
        Parameters:
        imageName - name tag of the image
        Returns:
        image meta data
      • getImage

        public javafx.scene.image.Image getImage​(java.lang.String name)
        If the image is cached retreve it's reference, otherwise load it.
        Specified by:
        getImage in interface IImageLoader
        Parameters:
        name - image name tag
        Returns:
        Image matching the name tag.
      • isLoaded

        boolean isLoaded​(java.lang.String name)
        Check if the image has been loaded into the cache.
        Parameters:
        name - image name tag
        Returns:
        true if the image was loaded into the cache.
      • loadImage

        public javafx.scene.image.Image loadImage​(java.lang.String fnm)
        Description copied from interface: IImageLoader
        Load the image from fnm, returning it as a Image.
        Specified by:
        loadImage in interface IImageLoader
      • loadImage

        public javafx.scene.image.Image loadImage​(java.lang.String fnm,
                                                  double width,
                                                  double height)
        Description copied from interface: IImageLoader
        Load the image from fnm with the defined dimensions, returning it as a Image.
        Specified by:
        loadImage in interface IImageLoader
      • getImage

        public javafx.scene.image.Image getImage​(java.lang.String name,
                                                 double destWidth,
                                                 double destHeight)
        If the image with the specified dimensions is cached it is retrieved from there, otherwise it is loaded.
        Specified by:
        getImage in interface IImageLoader
        Parameters:
        name - of the image
        destWidth - destination width
        destHeight - destination height
        Returns:
        image of the specified dimension.