lhmc.gui.thumbnail
Class ThumbnailsManager

java.lang.Object
  extended by lhmc.gui.thumbnail.ThumbnailsManager

public class ThumbnailsManager
extends java.lang.Object

creates and manages image thumbnails.


Nested Class Summary
 class ThumbnailsManager.BackgroundLoader
           
 
Constructor Summary
ThumbnailsManager()
          creates a new thumbnails manager.
 
Method Summary
 void checkCacheSizeLimit()
           
 java.awt.image.BufferedImage checkCreateThumbnail(java.lang.String imagePathName, java.awt.image.BufferedImage originalImage)
          return a thumbnail image for the specified original image.
 void checkCreateThumbnailsDirectory(java.io.File dir)
          check whether the given directory exists; create it if necessary.
 void checkCreateThumbnailsForDirectory(java.io.File dir)
          create thumbnails for all (image) file in the given directory.
 void clearCache()
          clear the thumbnails cache, no questions asked.
 java.awt.image.BufferedImage createDirnameThumbnail(java.lang.String dir)
          create a special "directory thumbnail" image for the given directory.
 java.awt.image.BufferedImage createErrorThumbnail(java.lang.String path)
          create a special "error thumbnail" for the given file.
 java.awt.image.BufferedImage createThumbnailImage(java.net.URL imageURL, java.awt.image.BufferedImage image, boolean allowExifThumbnail)
          create a thumbnail for the given source image.
 void deleteThumbnail(java.lang.String imagePathName)
          delete the thumbnail image file for the given source image file.
 void deleteThumbnailDirectory(java.lang.String imageDirectoryName)
          delete the thumbnail directory for the given source image directory.
 java.awt.image.BufferedImage getCachedThumbnailOrNull(java.lang.String imagePathName, javax.swing.JComponent target)
          request a thumbnail image for the thumnails cache.
 java.lang.String getDirectoryName(java.lang.String path)
           
 int getHashForString(java.lang.String s)
          calculate a hash code for a given string.
 java.awt.image.BufferedImage getThumbnail(java.lang.String imagePathName)
          return the cached thumbnail image for the given source image, or null if no thumbnail exists.
 java.io.File getThumbnailFilename(java.lang.String imagePathName)
          calculates the file (name) used for the thumbnail for the given original image name.
 java.lang.String getThumbnailsBaseDirectoryName()
           
 java.lang.String getThumbnailsDirectoryName(java.lang.String dirname)
          return a (short) directory name used to store the thumbnail images for the given source directory name.
 boolean isInsideThumbnailsDirectory(java.io.File dir)
           
static void main(java.lang.String[] args)
           
 java.awt.image.BufferedImage paintScaledThumbnail(java.awt.image.BufferedImage image)
          returns a thumbnail image of (size)x(size) pixel by rescaling the given (non-null!)
 java.awt.image.BufferedImage readThumbnail(java.io.File file)
          return a BufferedImage for the given thumbnail file.
 void updateThumbnail(java.lang.String imagePathName, java.awt.Image image)
          Intended to (re-) create a thumbnail from the given source image, even if the thumbnail already exists.
static java.lang.String usage()
           
 void writeThumbnail(java.awt.image.BufferedImage thumbnail, java.lang.String filename)
          write the given image to the given file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThumbnailsManager

public ThumbnailsManager()
creates a new thumbnails manager.

Method Detail

clearCache

public void clearCache()
clear the thumbnails cache, no questions asked. Note that any "ERROR" entries will also be cleared.


checkCacheSizeLimit

public void checkCacheSizeLimit()

getCachedThumbnailOrNull

public java.awt.image.BufferedImage getCachedThumbnailOrNull(java.lang.String imagePathName,
                                                             javax.swing.JComponent target)
request a thumbnail image for the thumnails cache. If the thumbnail is cached, it is returned right away. Otherwise, this method returns null (quickly), and ask the background thumbnails loading thread to load (and possibly first create) the thumbnail image. Once the background thread has loaded a thumbnail, the 'target' JComponent (if non-null) is repainted. Unfortunately, using SoftReference objects for the thumbnails cache doesn't work too well on current JDKs, because the cache items are deleted very frequently. Therefore, hard references are used right now; but the whole cache might be cleared when an OutOfMemoryError occurs.


getThumbnailFilename

public java.io.File getThumbnailFilename(java.lang.String imagePathName)
                                  throws java.io.IOException
calculates the file (name) used for the thumbnail for the given original image name. This method handles canonical or non-canonical file names, and also the special (degenerate) case of the original image already inside the thumbnails directory tree.

Throws:
java.io.IOException

getThumbnail

public java.awt.image.BufferedImage getThumbnail(java.lang.String imagePathName)
return the cached thumbnail image for the given source image, or null if no thumbnail exists.

This method does NOT trigger the background creation of thumbnails that don't exist. It also does NOT check whether an existing thumbnail is up to date. Use checkCreateThumbnail instead.

This method handles the special case of a thumbnail being requested for a thumbnail image by returning the thumbnail... the extra check seems cheap when compared to the complications arising otherwise.


checkCreateThumbnail

public java.awt.image.BufferedImage checkCreateThumbnail(java.lang.String imagePathName,
                                                         java.awt.image.BufferedImage originalImage)
return a thumbnail image for the specified original image.

This first calculates the thumbnail directory name corresponding to the source image directory, and creates the thumbnail directory if necessary.

Next, we check whether the thumbnail exists. If yes, we check whether the thumbnail is up-to-date (newer than the original image). If so, we read the thumbnail image and return it. Otherwise, we create the thumbnail image and store it into the thumbnails directory, before returning the thumbnail image. FIXME: this should probably also check the file modification times before deciding whether to use or (re-) create a thumbnail.


deleteThumbnail

public void deleteThumbnail(java.lang.String imagePathName)
delete the thumbnail image file for the given source image file.


deleteThumbnailDirectory

public void deleteThumbnailDirectory(java.lang.String imageDirectoryName)
delete the thumbnail directory for the given source image directory. If an image file (instead of directory) is specified, we delete the thumbnails for the corresponding directory. This also unconditionally deletes everything in that directory.


updateThumbnail

public void updateThumbnail(java.lang.String imagePathName,
                            java.awt.Image image)
Intended to (re-) create a thumbnail from the given source image, even if the thumbnail already exists.


readThumbnail

public java.awt.image.BufferedImage readThumbnail(java.io.File file)
return a BufferedImage for the given thumbnail file. This method currently uses ImageIO.read.


checkCreateThumbnailsDirectory

public void checkCreateThumbnailsDirectory(java.io.File dir)
check whether the given directory exists; create it if necessary.


createDirnameThumbnail

public java.awt.image.BufferedImage createDirnameThumbnail(java.lang.String dir)
create a special "directory thumbnail" image for the given directory. The resulting BufferedImage includes the fixed text "directory" and the "name" of the given directory.


createErrorThumbnail

public java.awt.image.BufferedImage createErrorThumbnail(java.lang.String path)
create a special "error thumbnail" for the given file.


createThumbnailImage

public java.awt.image.BufferedImage createThumbnailImage(java.net.URL imageURL,
                                                         java.awt.image.BufferedImage image,
                                                         boolean allowExifThumbnail)
create a thumbnail for the given source image. If the image input parameter is non-null, a thumbnail-sized image is created from that image right away. Otherwise, the source image is read from the given source File first, and then rescaled to thumbnail size.


paintScaledThumbnail

public java.awt.image.BufferedImage paintScaledThumbnail(java.awt.image.BufferedImage image)
returns a thumbnail image of (size)x(size) pixel by rescaling the given (non-null!) master image.


writeThumbnail

public void writeThumbnail(java.awt.image.BufferedImage thumbnail,
                           java.lang.String filename)
write the given image to the given file. Uses ImageIO.write().


checkCreateThumbnailsForDirectory

public void checkCreateThumbnailsForDirectory(java.io.File dir)
create thumbnails for all (image) file in the given directory. This can take a long time, and should be called in a separate worker thread.


getDirectoryName

public java.lang.String getDirectoryName(java.lang.String path)

getHashForString

public int getHashForString(java.lang.String s)
calculate a hash code for a given string. This method is used by getThumbnailsDirectoryName() to create the (short) directory names used to store the thumbnail images.


getThumbnailsDirectoryName

public java.lang.String getThumbnailsDirectoryName(java.lang.String dirname)
return a (short) directory name used to store the thumbnail images for the given source directory name. The returned value currently takes the following form: {niffler.home}/thumbnails/hashcode/


getThumbnailsBaseDirectoryName

public java.lang.String getThumbnailsBaseDirectoryName()

isInsideThumbnailsDirectory

public boolean isInsideThumbnailsDirectory(java.io.File dir)

usage

public static java.lang.String usage()

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception