/* @(#)ImageTools.java 1.0  12 October 2004 */

/**
 * Code authored by Richard Rasala
 * Associate Dean
 * College of Computer and Information Science
 * Northeastern University
 *
 * Modified by Brandon Schory
 * 
 */

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 from a directory.</p>
 */

public class ImageTools implements JPTConstants{

    /** Prevent instantiation. */
    private ImageTools() { }
        
    /** The filter to pick out jpg, gif, and png files. */
    public static final FilenameFilter imageFilter = new FilenameFilter() {
        public boolean accept(File directory, String name) {
            if (name == null)
                return false;
            
            return name.startsWith(".jpg", name.length() - 4)
                || name.startsWith(".JPG", name.length() - 4)
                || name.startsWith(".gif", name.length() - 4)
                || name.startsWith(".GIF", name.length() - 4)
                || name.startsWith(".png", name.length() - 4)
                || name.startsWith(".PNG", name.length() - 4);
        }
    };
    
    
    /**
     * <p>The method that reads the image files in the given directory.</p>
     *
     * <p>If no image files are available, returns an empty image array.</p>
     */
    public static Image[] readImageFiles(String directory) {
        
    		String[] files;
    		
    		files = readImageFileNames(directory);
    		
        
        int length = files.length;
        
        Image[] images = new Image[length];
        
        for (int i = 0; i < length; i++)
            images[i] = new ImageIcon(directory + "/" + files[i]).getImage();
        
        return images;
    }
    
    /**
     * <p>The method that reads the image files in the given directory in the 
     * order presented in the text file specified.</p>
     *
     * <p>If no image files are available, returns an empty image array.</p>
     */
    public static Image[] readImageFiles(String directory, String filename) {
        
    		String[] files;
    		
    		files = FileNameTools.getFileNames(directory + filename);
    		
        int length = files.length;
        
        Image[] images = new Image[length];
        
        for (int i = 0; i < length; i++)
            images[i] = new ImageIcon(directory + "/" + files[i]).getImage();
        
        return images;
    }
    
    
    /**
     * <p>The method that reads the image file names in the given directory.</p>
     *
     * <p>If no image files are available, returns an empty string array.</p>
     */
    public static String[] readImageFileNames(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;
    }
}

