/*
 * @(#)WebImageTools.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 java.net.*;
import javax.swing.*;

/**
 * <p>Class <code>WebImageTools</code> is a special purpose class for reading
 * images in various formats from a web site.</p>
 *
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   2.3.3
 */
public class WebImageTools {
    
    /** Prevent instantiation. */
    private WebImageTools() { }
    
    
   /**
     * <p>Reads the image files at the given url 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 imagesURL the web location with the images
     * @param imageFileNames the array of image file names
     * @return the image list as <code>Image[]</code>
     */
    public static Image[] readImages
        (String imagesURL, String[] imageFileNames)
    {
        if ((imagesURL == null) || (imageFileNames == null))
            return new Image[0];
        
       try {
            imagesURL = imagesURL.trim();
            imagesURL = WebTools.appendSlashIfNeeded(imagesURL);
            
            int length = imageFileNames.length;
            
            Image[] images = new Image[length];
            
            for (int i = 0; i < length; i++) {
                URL url = new URL(imagesURL + imageFileNames[i]);
                ImageIcon imageicon = new ImageIcon(url);
                images[i] = imageicon.getImage();
            }
            
            return images;
        }
        catch (Exception ex) {
            return new Image[0];
        }
    }
    
    
    /**
     * <p>Reads the image files at the given url with the given
     * filelist that contains the image file names.</p>
     *
     * <p>The filelist is assumed to be a file at the same url
     * 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>
     * 
     * @param imagesURL the web location with the images
     * @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 imagesURL, String filelist)
    {
        return readImages(imagesURL, null, filelist);
    }
    
    
    /**
     * <p>Reads the image files at the given images url with the given
     * filelist that contains the image file names;
     * the filelist is assumed to be located at the given filelist url.</p>
     *
     * <p>Returns the image list as <code>Image[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     * 
     * @param imagesURL the web location with the images
     * @param filelistURL the web location with the 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 imagesURL, String filelistURL, String filelist)
    {
        if ((imagesURL == null) || (filelist == null))
            return new Image[0];
        
        imagesURL = imagesURL.trim();
        
        String[] imageFileNames = readImageFileNamesFromWeb(imagesURL, filelistURL, filelist);
        
        return readImages(imagesURL, imageFileNames);
    }


   /**
     * <p>Reads the image files at the given url 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 imagesURL the web location with the images
     * @param imageFileNames the array of image file names
     * @return the image list as <code>ImagePaintable[]</code>
     */
    public static ImagePaintable[] readImagesAsPaintable
        (String imagesURL, String[] imageFileNames)
    {
        if ((imagesURL == null) || (imageFileNames == null))
            return new ImagePaintable[0];
        
        try {
            imagesURL = imagesURL.trim();
            imagesURL = WebTools.appendSlashIfNeeded(imagesURL);
            
            int length = imageFileNames.length;
            
            ImagePaintable[] paintables = new ImagePaintable[length];
            
            for (int i = 0; i < length; i++) {
                URL url = new URL(imagesURL + imageFileNames[i]);
                paintables[i] = new ImagePaintable(url);
            }
            
            return paintables;
        }
        catch (Exception ex) {
            return new ImagePaintable[0];
        }
    }
    
    
    /**
     * <p>Reads the image files at the given url with the given
     * filelist that contains the image file names.</p>
     *
     * <p>The filelist is assumed to be a file at the same url
     * 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>
     * 
     * @param imagesURL the web location with the images
     * @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 imagesURL, String filelist)
    {
        return readImagesAsPaintable(imagesURL, null, filelist);
    }


    /**
     * <p>Reads the image files at the given images url with the given
     * filelist that contains the image file names;
     * the filelist is assumed to be located at the given filelist url.</p>
     *
     * <p>Returns the image list as <code>ImagePaintable[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     * 
     * @param imagesURL the web location with the images
     * @param filelistURL the web location with the 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 imagesURL, String filelistURL, String filelist)
    {
        if ((imagesURL == null) || (filelist == null))
            return new ImagePaintable[0];
        
        imagesURL = imagesURL.trim();
        
        String[] imageFileNames = readImageFileNamesFromWeb(imagesURL, filelistURL, filelist);
        
        return readImagesAsPaintable(imagesURL, imageFileNames);
    }


   /**
     * <p>Reads the image files at the given url 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 imagesURL the web location with the images
     * @param imageFileNames the array of image file names
     * @return the image list as <code>ImagePaintableLite[]</code>
     */
    public static ImagePaintableLite[] readImagesAsPaintableLite
        (String imagesURL, String[] imageFileNames)
    {
        if ((imagesURL == null) || (imageFileNames == null))
            return new ImagePaintableLite[0];
        
        try {
            imagesURL = imagesURL.trim();
            imagesURL = WebTools.appendSlashIfNeeded(imagesURL);
            
            int length = imageFileNames.length;
            
            ImagePaintableLite[] paintables = new ImagePaintableLite[length];
            
            for (int i = 0; i < length; i++) {
                URL url = new URL(imagesURL + imageFileNames[i]);
                paintables[i] = new ImagePaintableLite(url);
            }
            
            return paintables;
        }
        catch (Exception ex) {
            return new ImagePaintableLite[0];
        }
    }
    
    
    /**
     * <p>Reads the image files at the given url with the given
     * filelist that contains the image file names.</p>
     *
     * <p>The filelist is assumed to be a file at the same url
     * 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>
     * 
     * @param imagesURL the web location with the images
     * @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 imagesURL, String filelist)
    {
        return readImagesAsPaintableLite(imagesURL, null, filelist);
    }
    
    
    /**
     * <p>Reads the image files at the given images url with the given
     * filelist that contains the image file names;
     * the filelist is assumed to be located at the given filelist url.</p>
     *
     * <p>Returns the image list as <code>ImagePaintableLite[]</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</p>
     * 
     * @param imagesURL the web location with the images
     * @param filelistURL the web location with the 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 imagesURL, String filelistURL, String filelist)
    {
        if ((imagesURL == null) || (filelist == null))
            return new ImagePaintableLite[0];
        
        imagesURL = imagesURL.trim();
        
        String[] imageFileNames = readImageFileNamesFromWeb(imagesURL, filelistURL, filelist);
        
        return readImagesAsPaintableLite(imagesURL, imageFileNames);
    }
    
    
    /**
     * <p>Returns all image file names at the given filelist url that
     * are present in the given filelist.</p>
     *
     * <p>If the filelist url is <code>null</code> or blank then it
     * is replaced by the images url.  Otherwise, the images url is
     * unused.</p>
     *
     * <p>If the file list is <code>null</code> or blank, then it is
     * replaced by the default name: <code>imagelist.txt</code>.</p>
     *
     * <p>If no image files are available, returns an empty array.</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[] readImageFileNamesFromWeb
        (String imagesURL, String filelistURL, String filelist)
    {
        if (imagesURL == null)
            imagesURL = "";
        else
            imagesURL = imagesURL.trim();
        
        
        if (filelistURL == null)
            filelistURL = imagesURL;
        else
            filelistURL = filelistURL.trim();
        
        
        if (filelistURL.length() == 0)
            filelistURL = imagesURL;
        
        
        if (filelist == null)
            filelist = "";
        else
            filelist  = filelist.trim();
        
        
        if (filelist.length() == 0)
            filelist = "imagelist.txt";
        
        
        return WebTools.readFileNamesFromWeb(filelistURL, filelist);
    }
    
}