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

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * JApplet.java
3 *
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
5 * All rights reserved.
6 *
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or
9 * FITNESS FOR A PARTICULAR PURPOSE.
10 */
11
12 package samer.applet;
13 import samer.core.*;
14 import samer.core.util.heavy.*;
15 import java.applet.*;
16 import java.awt.*;
17 import java.net.*;
18 import java.io.*;
19
20
21 /** Base class for applets that work with the JAppletShell framework.
22 Manages a border and scripts to run on create, destroy, init and term
23 */
24
25 public class JApplet extends Applet
26 {
27 private Border.Interface border;
28 protected String name, ondestroy;
29
30
31 public void setBorder( Border.Interface b) { border=b; }
32 public Border.Interface getBorder() { return border; }
33
34 public void init()
35 {
36 JAppletShell.initialise(this);
37 name = getParameter("name");
38 if (name!=null && name.length()==0) name=null;
39
40 String args = getParameter("args");
41 if (args!=null && args.length()>0) {
42 try {
43 JAppletShell.instance.loadargs(new URL(getDocumentBase(), args));
44 }
45 catch (Exception ex) {
46 Shell.trace("JApplet: error loading "+args);
47 }
48 }
49
50 Shell.trace("JApplet init "+name);
51 ondestroy=getParameter("ondestroy");
52 setup();
53 }
54
55 public void setup()
56 {
57 // create a new environment based on Shell top level
58 // with name equal to Applet name, using this applet
59 // as a Dictionary (String -> String mapping)
60
61 if (name!=null) { /* what? */ }
62 Node node= (name==null ? Shell.env().node() : new Node(name));
63 Environment env=new Environment(Shell.env(),node) {
64 public Datum datum(final String nm) {
65 final String vl=getParameter(nm);
66 if (vl==null) return parent.datum(nm);
67 return new Datum() {
68 public String name() { return nm; }
69 public int score() { return 0; }
70 public Object get(Codec c, Object def) { return c.decode(vl); }
71 public void get(Autocoder obj) { obj.decode(vl); }
72 };
73 }
74 };
75 // can't push global Environment because
76 // applets may load mutli-threadedly.
77
78
79 // set up colours and fonts from properties
80 Color pageColour = X.color(env.datum("pagebg"), getBackground());
81 getParent().setBackground( pageColour);
82 setBackground( X.color(env.datum("background"), pageColour));
83 setForeground( X.color(env.datum("foreground"), getForeground()));
84 setFont((Font)(env.datum("font").get(X.FontCodec,"Helvetica-PLAIN-12")));
85
86 // create border using parameters from this applet
87
88 setBorder( Borders.createBorder(new Environment(env,"border")));
89
90 // "export" means we should make this applets
91 // parameters globally visible as Shell properties
92 synchronized (JAppletShell.instance) {
93 String export=getParameter("export"); // local search only!
94 if ("true".equals(export)) {
95 Shell.trace("Applet: "+name+" exporting properties");
96 Shell.push(env);
97 // ?? what happens when this applet goes away?
98 // maybe implement Peers environement, consisting of
99 // a list (rather than a stack) of other environments
100 }
101 }
102 }
103
104 public void onStart()
105 {
106 String oncreate=getParameter("oncreate");
107 if (oncreate!=null) {
108 Shell.trace("oncreate: "+oncreate);
109 Shell.interpret(new StringReader(oncreate));
110 }
111 }
112
113 public void onDestroy()
114 {
115 if (ondestroy!=null) {
116 Shell.trace("ondestroy: "+ondestroy);
117 Shell.interpret(new StringReader(ondestroy));
118 }
119 }
120
121 public Insets getInsets() {
122 if (border!=null) return border.getBorderInsets(this);
123 else return new Insets(0,0,0,0);
124 }
125
126 public void paint(Graphics g)
127 {
128 // should paint border first, incase border erases bg
129 Dimension d=getSize();
130 border.paintBorder(this,g,0,0,d.width,d.height);
131 g.setColor( getForeground());
132 }
133
134 public void setProperty(String nm,String vl) {}
135 public String getProperty(String nm) { return getParameter(nm); }
136
137 public void start() { Shell.trace("JApplet start"); onStart(); }
138 public void stop() { Shell.trace("JApplet stop"); }
139 public void destroy() { Shell.trace("JApplet destroy"); onDestroy(); }
140
141 protected void finalize() { Shell.trace("JApplet finalizing "+name); }
142 }