/*************************************************
 * Author: 	Evelyn Lai-Tee Cheok
 *			Department of Electrical Engineering
 *			Center for Telecommunications Research
 *			Schapiro Research Building
 *			Columbia University
 *
 * Email: 	laitee@ctr.columbia.edu
 *
 **************************************************/
import java.lang.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.*;
import MyList;
import MyButton;

public class ImageFrame extends Frame {
	SketchArea sketch_area;
	MyList mylist;
	MyButton mybutton;
	String filename = null;
	String DocumentBase="http://www.ctr.columbia.edu/~laitee/courses/ais/wboard/";
	String imageDir = "images/";
	String imageIndex = "index.image";
	public final static int X = 10;
	public final static int Y = 10;

	public ImageFrame(SketchArea parent, String title) {
		super(title);
		sketch_area = parent;
		GridBagLayout gridbag = new GridBagLayout();

		setLayout(gridbag);
		mylist = new MyList(gridbag, 5, false, "REMAINDER",
							10, 10, 10, 10);
		mybutton = new MyButton(gridbag, "OK", "REMAINDER",
							20, 30, 10, 30);
		add(mylist);
		add(mybutton);
		loadImageFiles(imageDir, imageIndex);
		resize(200,250);
	}

	public boolean handleEvent(Event e) {
		switch(e.id) {
			case Event.LIST_SELECT:
				filename = mylist.getSelectedItem();
				break;
			case Event.ACTION_EVENT:
				if (e.target instanceof Button) {
					if (e.arg.equals("OK")) {
						if (filename == null)
							System.out.println(" Please select a gif file.");
						else {
							getGIFImage(filename);
							filename = null;
							if ( this.isVisible() ) {
								this.removeAll();
								this.hide();
							}
						}
					}
				}
				break;
		}
		return true;
	}

	public void loadImageFiles(String imageDir, String imageIndex) {
		String buf;
		URL imageIndexURL = null;

		try {
			imageIndexURL = new URL(DocumentBase + imageDir + imageIndex);
		} catch (MalformedURLException e) {
			System.out.println("MalformedURLException : "+e);
		}

		try {
			DataInputStream dataStream = new DataInputStream(imageIndexURL.openStream());

			while ( (buf = dataStream.readLine()) != null ) 
				mylist.addItem(buf);
		} catch (IOException e) {
			System.err.println(e);
		}
				
	}

	public void getGIFImage(String filename) {
		URL gifURL = null;
		Toolkit myToolkit = Toolkit.getDefaultToolkit();
		Image img = null;
		int imageWidth, imageHeight;

		try {
			gifURL = new URL(DocumentBase + imageDir + filename);
		} catch (MalformedURLException e) {
			System.out.println("MalformedURLException : "+e);
		}
		img = myToolkit.getImage(gifURL);	
		imageWidth = img.getWidth(this);
		imageHeight = img.getHeight(this);
		System.out.println(" Image width and height in getGIFImage = "+
						imageWidth + "   " + imageHeight);

		if ( img == null )
			System.out.println(" IMAGE is NULL !!");
		else {
			System.out.println(" IMAGE is NOT NULL !!");
			Img wbImage = new Img(sketch_area, img, X, Y, 
								imageWidth, imageHeight, gifURL);
			wbImage.paint(sketch_area.offscreenG);									
			sketch_area.repaint();
			sketch_area.addStack((Object)wbImage);
			wbImage.send();
		}

	}
}

