samer@0: /* samer@0: * JApplet.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.applet; samer@0: import samer.core.*; samer@0: import samer.core.util.heavy.*; samer@0: import java.applet.*; samer@0: import java.awt.*; samer@0: import java.net.*; samer@0: import java.io.*; samer@0: samer@0: samer@0: /** Base class for applets that work with the JAppletShell framework. samer@0: Manages a border and scripts to run on create, destroy, init and term samer@0: */ samer@0: samer@0: public class JApplet extends Applet samer@0: { samer@0: private Border.Interface border; samer@0: protected String name, ondestroy; samer@0: samer@0: samer@0: public void setBorder( Border.Interface b) { border=b; } samer@0: public Border.Interface getBorder() { return border; } samer@0: samer@0: public void init() samer@0: { samer@0: JAppletShell.initialise(this); samer@0: name = getParameter("name"); samer@0: if (name!=null && name.length()==0) name=null; samer@0: samer@0: String args = getParameter("args"); samer@0: if (args!=null && args.length()>0) { samer@0: try { samer@0: JAppletShell.instance.loadargs(new URL(getDocumentBase(), args)); samer@0: } samer@0: catch (Exception ex) { samer@0: Shell.trace("JApplet: error loading "+args); samer@0: } samer@0: } samer@0: samer@0: Shell.trace("JApplet init "+name); samer@0: ondestroy=getParameter("ondestroy"); samer@0: setup(); samer@0: } samer@0: samer@0: public void setup() samer@0: { samer@0: // create a new environment based on Shell top level samer@0: // with name equal to Applet name, using this applet samer@0: // as a Dictionary (String -> String mapping) samer@0: samer@0: if (name!=null) { /* what? */ } samer@0: Node node= (name==null ? Shell.env().node() : new Node(name)); samer@0: Environment env=new Environment(Shell.env(),node) { samer@0: public Datum datum(final String nm) { samer@0: final String vl=getParameter(nm); samer@0: if (vl==null) return parent.datum(nm); samer@0: return new Datum() { samer@0: public String name() { return nm; } samer@0: public int score() { return 0; } samer@0: public Object get(Codec c, Object def) { return c.decode(vl); } samer@0: public void get(Autocoder obj) { obj.decode(vl); } samer@0: }; samer@0: } samer@0: }; samer@0: // can't push global Environment because samer@0: // applets may load mutli-threadedly. samer@0: samer@0: samer@0: // set up colours and fonts from properties samer@0: Color pageColour = X.color(env.datum("pagebg"), getBackground()); samer@0: getParent().setBackground( pageColour); samer@0: setBackground( X.color(env.datum("background"), pageColour)); samer@0: setForeground( X.color(env.datum("foreground"), getForeground())); samer@0: setFont((Font)(env.datum("font").get(X.FontCodec,"Helvetica-PLAIN-12"))); samer@0: samer@0: // create border using parameters from this applet samer@0: samer@0: setBorder( Borders.createBorder(new Environment(env,"border"))); samer@0: samer@0: // "export" means we should make this applets samer@0: // parameters globally visible as Shell properties samer@0: synchronized (JAppletShell.instance) { samer@0: String export=getParameter("export"); // local search only! samer@0: if ("true".equals(export)) { samer@0: Shell.trace("Applet: "+name+" exporting properties"); samer@0: Shell.push(env); samer@0: // ?? what happens when this applet goes away? samer@0: // maybe implement Peers environement, consisting of samer@0: // a list (rather than a stack) of other environments samer@0: } samer@0: } samer@0: } samer@0: samer@0: public void onStart() samer@0: { samer@0: String oncreate=getParameter("oncreate"); samer@0: if (oncreate!=null) { samer@0: Shell.trace("oncreate: "+oncreate); samer@0: Shell.interpret(new StringReader(oncreate)); samer@0: } samer@0: } samer@0: samer@0: public void onDestroy() samer@0: { samer@0: if (ondestroy!=null) { samer@0: Shell.trace("ondestroy: "+ondestroy); samer@0: Shell.interpret(new StringReader(ondestroy)); samer@0: } samer@0: } samer@0: samer@0: public Insets getInsets() { samer@0: if (border!=null) return border.getBorderInsets(this); samer@0: else return new Insets(0,0,0,0); samer@0: } samer@0: samer@0: public void paint(Graphics g) samer@0: { samer@0: // should paint border first, incase border erases bg samer@0: Dimension d=getSize(); samer@0: border.paintBorder(this,g,0,0,d.width,d.height); samer@0: g.setColor( getForeground()); samer@0: } samer@0: samer@0: public void setProperty(String nm,String vl) {} samer@0: public String getProperty(String nm) { return getParameter(nm); } samer@0: samer@0: public void start() { Shell.trace("JApplet start"); onStart(); } samer@0: public void stop() { Shell.trace("JApplet stop"); } samer@0: public void destroy() { Shell.trace("JApplet destroy"); onDestroy(); } samer@0: samer@0: protected void finalize() { Shell.trace("JApplet finalizing "+name); } samer@0: }