import java.applet.*;
import java.awt.*;
import java.net.*;
import isi.jserver.jserver;
import java.io.*;
import netscape.security.PrivilegeManager;	// NS4

public class nyse extends Applet
{
	jserver js;	boolean showflag;
	TextArea ta = new TextArea(10, 45);
	Button showj = new Button("Show J");
	String labelinit = "                                                                                        ";
	Label label = new Label(labelinit);
	Label titlelabel = new Label(labelinit);
	Choice choice = new Choice();
	
	String doc = "This applet gets data (URLs) from the web site\n"+
		"of the New York Stock Exchange.\n\n"+
		"The data is text with rows of dates and volumes, and\n"+
		"is ~140k bytes in size. J plots every 60th volume.\n\n"+
		"Reading URLs is not allowed in the Java sandbox, and\n"+
		"this applet must be on your disk to run.";

	String def = "fixdata=:3 : 0\n"+
		"q=.1e7<.1{\"1>0\".each<;._2(CR~:z)#z=.y.,LF\n"+
		"((#q)$59{.1)#q\n"+
		")";

	public void init()
	{
		resize(600,200);
		add(ta);
		ta.setEditable(false);
		ta.setText(doc);
		add(titlelabel);
		titlelabel.setText("NYSE historical trading volumes for period: ");
		add(choice);
		choice.addItem("");
		choice.addItem("vol00-19");
		choice.addItem("vol20-29");
		choice.addItem("vol30-39");
		choice.addItem("vol40-49");
		add(label);
		add(showj);

		js = new jserver();		// create jserver object
		js.start(0);			// start jserver run thread
		js.Log(true);

		// load basic scripts - J3.05 and J4.01 different path and suffix
		js.Do("a_z_=:'4'=0{11!:0'qabout'");
		js.Do("z_z_=:>a_z_{'.js';'.ijs'");
		js.Do("a_z_=:>a_z_{'main\\';'system\\main\\'");
		js.Do("boot_z_=: 3 : '0!:0 <a,y.,z'");
		js.Do("boot_z_ 'stdlib'");
		js.Do("boot_z_ 'winlib'");
		js.Do("boot_z_ :: ] 'colib'   NB. only in J4.01");
		js.Do("boot_j_ :: ] 'loadlib' NB. only in J4.02");
		js.Do("boot_j_ 'jadelib'");   
		js.Do("boot_z_ :: ]'jadeutil' NB. only in older versions");

		js.Do("load'plot'");

		js.Set("def", def);
		js.Do("def");
		js.Do("0!:100 def");
	}

	public void destroy(){js.close();}

	public void plotdata(String date)
	{
		Object obj;
		URL url;

		try{PrivilegeManager.enablePrivilege("30Capabilities");}
		catch(Error e){;}	// NS4 requires enablePrivilege

		showStatus("");
		String s = "http://www.nyse.com/public/market/2c/2c2/histfile/"+date+".dat";
		label.setText(s);
		try
		{
			url = new URL(s);
			obj = url.getContent();
		}
		catch(Exception e)
		{
			showStatus(e.toString() + "Can't get the URL data.");
			return;
		}

		// IE url content is returned as a String
		// NS url content is returned as a netscape.net.URLInputStream
		if(obj instanceof String)
			js.Set("stockdata", (String)obj);	// IE
		else
		{
			// assume netscape URLInputStream
			byte[] buf = new byte[160000];
			int i,k=0;
			for(i=0;i<buf.length;++i)
			{
				try
				{
					k = ((InputStream)obj).read();
				}
				catch(Exception e)
				{
					;
				}
				if(k==-1) break;
				buf[i] = (byte)k;
			}
			String stockdata = new String(buf, 0, 0, i);
			js.Set("stockdata", stockdata);
		}
		js.Do("'TITLE "+s+ "'plot fixdata stockdata");
	}

	public boolean action(Event event, Object obj)
	{
		Object target = event.target;
		if(target == choice)
		{
			plotdata(choice.getSelectedItem());
			return true;
		}
		if(target == showj)
		{
			js.Show(showflag=!showflag);
			return true;
		}
		return false;
	}
}
