view src/samer/applet/JApplet.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
/*
 *	JApplet.java	
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.applet;
import  samer.core.*;
import  samer.core.util.heavy.*;
import  java.applet.*;
import  java.awt.*;
import  java.net.*;
import  java.io.*;


/** Base class for applets that work with the JAppletShell framework.
	Manages a border and scripts to run on create, destroy, init and term
*/

public class JApplet extends Applet
{
	private Border.Interface	border;
	protected String			name, ondestroy;


	public void setBorder( Border.Interface b) { border=b; }
	public Border.Interface getBorder() { return border; }

	public void init() 
	{
		JAppletShell.initialise(this);
		name = getParameter("name");
		if (name!=null && name.length()==0) name=null;

		String args = getParameter("args");
		if (args!=null && args.length()>0) {
			try {
				JAppletShell.instance.loadargs(new URL(getDocumentBase(), args));
			}
			catch (Exception ex) {
				Shell.trace("JApplet: error loading "+args);
			}
		}

		Shell.trace("JApplet init "+name); 
		ondestroy=getParameter("ondestroy");
		setup();
	}

	public void setup()
	{
		// create a new environment based on Shell top level
		// with name equal to Applet name, using this applet
		// as a Dictionary (String -> String mapping)

		if (name!=null) { /* what? */ }
		Node node= (name==null ? Shell.env().node() : new Node(name));
		Environment env=new Environment(Shell.env(),node) {
			public Datum datum(final String nm) {
				final String vl=getParameter(nm);
				if (vl==null) return parent.datum(nm);
				return new Datum() {
					public String name() { return nm; }
					public int    score() { return 0; }
					public Object get(Codec c, Object def) { return c.decode(vl); }
					public void   get(Autocoder obj) { obj.decode(vl); }
				};
			}
		};
		// can't push global Environment because 
		// applets may load mutli-threadedly.


		// set up colours and fonts from properties
		Color pageColour = X.color(env.datum("pagebg"), getBackground());
		getParent().setBackground( pageColour);
		setBackground( X.color(env.datum("background"), pageColour));
		setForeground( X.color(env.datum("foreground"), getForeground()));
		setFont((Font)(env.datum("font").get(X.FontCodec,"Helvetica-PLAIN-12")));

		// create border using parameters from this applet

		setBorder( Borders.createBorder(new Environment(env,"border")));

		// "export" means  we should make this applets
		// parameters globally visible as Shell properties 
		synchronized (JAppletShell.instance) {
			String export=getParameter("export"); // local search only!
			if ("true".equals(export)) {
				Shell.trace("Applet: "+name+" exporting properties");
				Shell.push(env);
				// ?? what happens when this applet goes away? 
				// maybe implement Peers environement, consisting of
				// a list (rather than a stack) of other environments
			}
		}
	}

	public void onStart() 
	{
		String oncreate=getParameter("oncreate");
		if (oncreate!=null) {
			Shell.trace("oncreate: "+oncreate);
			Shell.interpret(new StringReader(oncreate));
		}
	}

	public void onDestroy() 
	{
		if (ondestroy!=null) {
			Shell.trace("ondestroy: "+ondestroy);
			Shell.interpret(new StringReader(ondestroy));
		}
	}

	public Insets getInsets() { 
		if (border!=null) return border.getBorderInsets(this); 
		else return new Insets(0,0,0,0);
	}

	public void paint(Graphics g)
	{
		// should paint border first, incase border erases bg
		Dimension d=getSize();
		border.paintBorder(this,g,0,0,d.width,d.height);
		g.setColor( getForeground());
	}

	public void   setProperty(String nm,String vl) {}
	public String getProperty(String nm) { return getParameter(nm); }

	public void start() { Shell.trace("JApplet start"); onStart(); }
	public void stop() { Shell.trace("JApplet stop"); }
	public void destroy() { Shell.trace("JApplet destroy"); onDestroy(); }

	protected void finalize() { Shell.trace("JApplet finalizing "+name); } 
}