import java.applet.*;
import java.awt.*;
import netscape.javascript.JSObject;
import java.net.URL;
import java.io.*;

public class JSfileopener extends Applet
{
	public String bgcolor;
	public DataInputStream stream;
	
	public String getAppletInfo()
	{
		return "Name: JS Fileloader\n" +
		"Author: Daniel Thoma\n" +
		"E-Mail: dthoma@gmx.net\n" +
		"Tested with: MSIE 4.x/5.x NN 4.x";
	}
	
	public String[][] getParameterInfo()
	{
		String[][] info =
		{
			{"color","color","something like #ff00ab"}
		};
		return info;
	}
	
	public void init()
	{
		bgcolor = getParameter("color");
		if(bgcolor == null)
		{
			bgcolor = ((JSObject)JSObject.getWindow(this).getMember("document")).getMember("bgColor").toString();
		}
	}
	
	public void paint(Graphics g)
	{
		g.setColor(Color.decode(bgcolor));
		Rectangle r = g.getClipBounds();
		g.fillRect(r.x, r.y, r.width, r.height);
	}
	
	public void setBgcolor(String colorvalue)
	{
		bgcolor = colorvalue;
		repaint();
	}
	
	public void setFile(String master, String file) throws IOException
	{
		URL url = new URL(new URL(master), file);
		stream = new DataInputStream(url.openStream());
	}
	
	public void setFile(String file) throws IOException
	{
		URL url = new URL(file);
		stream = new DataInputStream(url.openStream());
	}
	
	public String readFile() throws IOException
	{
		String line    = "";
		if((line = stream.readLine()) != null)
		{
			return line;
		}
		else
		{
			stream.close();
			return null;
		}
	}
	
	public String getFile() throws IOException
	{
		String source    = "";
		String line;
		while((line = readFile()) != null)
		{
			source += line +"\n";
		}
		return source;
	}
}
