/*
 * @(#)ImageTools.java    2.3.3  1 January 2005
 *
 * Copyright 2005
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import edu.neu.ccs.util.*;

import java.awt.*;
import javax.swing.*;
import java.io.*;

/**
 * <p>Class <code>ImageTools</code> is a special purpose class for reading
 * images in various formats from a directory on the local system.</p>
 *
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   2.3.3
 */
public class ImageTools {

    /** Prevent instantiation. */
    private ImageTools() { }
    
    
    /**
     * The filter to pick out jpg, gif, and png files.  Expects the file name
     * to end with ".jpg", ".gif", or, ".png", possibly with mixed or upper case.
     */
    public static final FilenameFilter imageFilter = new FilenameFilter() {
        public boolean accept(File directory, String name) {
            if (name == null)
                return false;
            
            int length = name.length();
            
            if (length < 5)
                return false;
            
            name = name.substring(length - 4).toLowerCase();
            
            return name.equals(".jpg")
                || name.equals(".gif")
                || name.equals(".png");
        }
    };
    
    
   /**
     * <p>Reads the image files in the given directory with the given
     * image file names.</p>
     *
     * <p>Returns the image list as <code>Image[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     *
     * @param directory the directory with the images
     * @param imageFileNames the array of image file names
     * @return the image list as <code>Image[]</code>
     */
    public static Image[] readImages
        (String directory, String[] imageFileNames)
    {
        if ((directory == null) || (imageFileNames == null))
            return new Image[0];
       
        int length = imageFileNames.length;
        
        Image[] images = new Image[length];
        
        for (int i = 0; i < length; i++) {
            String path = TextTools.makePath(directory, imageFileNames[i]);
            
            images[i] = new ImageIcon(path).getImage();
        }
        
        return images;
    }
    
    
    /**
     * <p>Reads the image files in the given directory with the given
     * filelist that contains the image file names.</p>
     *
     * <p>The filelist is assumed to be a file in the same directory
     * as the images.</p>
     * 
     * <p>Returns the image list as <code>Image[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     * 
     * <p>If filelist is <code>null</code> or is an empty string,
     * then this method reads all images in the directory.</p>
     *
     * @param directory the directory with the images and image file list
     * @param filelist the name of the text file with the image file list
     * @return the image list as <code>Image[]</code>
     */
    public static Image[] readImages
        (String directory, String filelist)
    {
        String[] imageFileNames = readImageFileNames(directory, filelist);
        
        return readImages(directory, imageFileNames);
    }
    
    
    /**
     * <p>Reads all image files in the given directory.</p>
     *
     * <p>Returns the image list as <code>Image[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     *
     * @param directory the directory with the images
     * @return the image list as <code>Image[]</code>
     */
    public static Image[] readAllImages(String directory)
    {
        String[] imageFileNames = readAllImageFileNames(directory);
        
        return readImages(directory, imageFileNames);
    }
    
    
    /**
     * <p>Reads the image files in the given directory with the given
     * image file names.</p>
     *
     * <p>Returns the image list as <code>ImagePaintable[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     *
     * @param directory the directory with the images
     * @param imageFileNames the array of image file names
     * @return the image list as <code>ImagePaintable[]</code>
     */
    public static ImagePaintable[] readImagesAsPaintable
        (String directory, String[] imageFileNames)
    {
        if ((directory == null) || (imageFileNames == null))
            return new ImagePaintable[0];
       
        int length = imageFileNames.length;
        
        ImagePaintable[] paintables = new ImagePaintable[length];
        
        for (int i = 0; i < length; i++) {
            String path = TextTools.makePath(directory, imageFileNames[i]);
            
            paintables[i] = new ImagePaintable(path);
        }
        
        return paintables;
    }
    
    
    /**
     * <p>Reads the image files in the given directory with the given
     * filelist that contains the image file names.</p>
     *
     * <p>The filelist is assumed to be a file in the same directory
     * as the images.</p>
     * 
     * <p>Returns the image list as <code>ImagePaintable[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     * 
     * <p>If filelist is <code>null</code> or is an empty string,
     * then this method reads all images in the directory.</p>
     *
     * @param directory the directory with the images and image file list
     * @param filelist the name of the text file with the image file list
     * @return the image list as <code>ImagePaintable[]</code>
     */
    public static ImagePaintable[] readImagesAsPaintable
        (String directory, String filelist)
    {
        String[] imageFileNames = readImageFileNames(directory, filelist);
        
        return readImagesAsPaintable(directory, imageFileNames);
    }
    
    
    /**
     * <p>Reads all image files in the given directory.</p>
     *
     * <p>Returns the image list as <code>ImagePaintable[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     *
     * @param directory the directory with the images
     * @return the image list as <code>ImagePaintable[]</code>
     */
    public static ImagePaintable[] readAllImagesAsPaintable
        (String directory)
    {
        String[] imageFileNames = readAllImageFileNames(directory);
        
        return readImagesAsPaintable(directory, imageFileNames);
    }
    
    
    /**
     * <p>Reads the image files in the given directory with the given
     * image file names.</p>
     *
     * <p>Returns the image list as <code>ImagePaintableLite[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     * 
     * @param directory the directory with the images
     * @param imageFileNames the array of image file names
     * @return the image list as <code>ImagePaintableLite[]</code>
     */
    public static ImagePaintableLite[] readImagesAsPaintableLite
        (String directory, String[] imageFileNames)
    {
        if ((directory == null) || (imageFileNames == null))
            return new ImagePaintableLite[0];
       
        int length = imageFileNames.length;
        
        ImagePaintableLite[] paintables =
            new ImagePaintableLite[length];
        
        for (int i = 0; i < length; i++) {
            String path =
                TextTools.makePath(directory, imageFileNames[i]);
            
            paintables[i] = new ImagePaintableLite(path);
        }
        
        return paintables;
    }
    
    
    /**
     * <p>Reads the image files in the given directory with the given
     * filelist that contains the image file names.</p>
     *
     * <p>The filelist is assumed to be a file in the same directory
     * as the images.</p>
     * 
     * <p>Returns the image list as <code>ImagePaintableLite[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     *
     * <p>If filelist is <code>null</code> or is an empty string,
     * then this method reads all images in the directory.</p>
     *
     * @param directory the directory with the images and image file list
     * @param filelist the name of the text file with the image file list
     * @return the image list as <code>ImagePaintableLite[]</code>
     */
    public static ImagePaintableLite[] readImagesAsPaintableLite
        (String directory, String filelist)
    {
        String[] imageFileNames = readImageFileNames(directory, filelist);
        
        return readImagesAsPaintableLite(directory, imageFileNames);
    }
    
    
    /**
     * <p>Reads all image files in the given directory.</p>
     *
     * <p>Returns the image list as <code>ImagePaintableLite[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     *
     * @param directory the directory with the images
     * @return the image list as <code>ImagePaintableLite[]</code>
     */
    public static ImagePaintableLite[] readAllImagesAsPaintableLite
        (String directory)
    {
        String[] imageFileNames = readAllImageFileNames(directory);
        
        return readImagesAsPaintableLite(directory, imageFileNames);
    }
    
    
    /**
     * <p>Returns all image file names in the given directory.</p>
     *
     * <p>If the directory is <code>null</code> or no image files
     * are available, returns an empty array.</p>
     *
     * @param directory the directory with the images
     * @return the image file names in the directory
     */
    public static String[] readAllImageFileNames(String directory) 
    {
        if (directory == null)
            return new String[0];
        
        File imagesDirectory = new File(directory);
        
        String[] files = imagesDirectory.list(imageFilter);
        
        if (files == null)
            return new String[0];
        
        return files;
    }
    
    
    /**
     * <p>Returns all image file names in the given directory that
     * are present in the given file list.</p>
     *
     * <p>If the directory is <code>null</code> or no image files
     * are available, returns an empty array.</p>
     *
     * <p>If the file list is <code>null</code> or blank, then
     * returns all image file names in the directory.</p>
     *
     * @param directory the directory with the images and image file list
     * @param filelist the name of the text file with the image file list
     * @return the image file names in the list
     */
    public static String[] readImageFileNames
        (String directory, String filelist)
    {
        if (directory == null)
            return new String[0];
        
        if (filelist == null)
            return readAllImageFileNames(directory);
        
        filelist = filelist.trim();
        
        if (filelist.length() == 0)
            return readAllImageFileNames(directory);
        
        return TextTools.readFileNames(directory, filelist);
    }

}

