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