root / trunk / code / projects / colonet / client / AppletViewer.java @ 2006
History | View | Annotate | Download (8.55 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
|
3 |
* All rights reserved. Software written by Ian F. Darwin and others.
|
4 |
* $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
|
5 |
*
|
6 |
* Redistribution and use in source and binary forms, with or without
|
7 |
* modification, are permitted provided that the following conditions
|
8 |
* are met:
|
9 |
* 1. Redistributions of source code must retain the above copyright
|
10 |
* notice, this list of conditions and the following disclaimer.
|
11 |
* 2. Redistributions in binary form must reproduce the above copyright
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
13 |
* documentation and/or other materials provided with the distribution.
|
14 |
*
|
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
|
16 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
17 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
18 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
|
19 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
20 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
21 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
24 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
25 |
* POSSIBILITY OF SUCH DAMAGE.
|
26 |
*
|
27 |
* Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
|
28 |
* cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
|
29 |
* pioneering role in inventing and promulgating (and standardizing) the Java
|
30 |
* language and environment is gratefully acknowledged.
|
31 |
*
|
32 |
* The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
|
33 |
* inventing predecessor languages C and C++ is also gratefully acknowledged.
|
34 |
*/
|
35 |
|
36 |
import java.applet.Applet; |
37 |
import java.applet.AppletContext; |
38 |
import java.applet.AppletStub; |
39 |
import java.applet.AudioClip; |
40 |
import java.awt.BorderLayout; |
41 |
import java.awt.Container; |
42 |
import java.awt.Dimension; |
43 |
import java.awt.Image; |
44 |
import java.awt.Label; |
45 |
import java.awt.Panel; |
46 |
import java.awt.event.WindowAdapter; |
47 |
import java.awt.event.WindowEvent; |
48 |
import java.io.IOException; |
49 |
import java.io.InputStream; |
50 |
import java.net.URL; |
51 |
import java.util.Enumeration; |
52 |
import java.util.HashMap; |
53 |
import java.util.Iterator; |
54 |
import java.util.Map; |
55 |
|
56 |
import javax.swing.JFrame; |
57 |
|
58 |
/*
|
59 |
* AppletViewer - a simple Applet Viewer program.
|
60 |
* @author Ian Darwin, http://www.darwinsys.com/
|
61 |
*/
|
62 |
public class AppletViewer { |
63 |
/** The main Frame of this program */
|
64 |
JFrame f;
|
65 |
/** The AppletAdapter (gives AppletStub, AppletContext, showStatus) */
|
66 |
static AppletAdapter aa = null; |
67 |
/** The name of the Applet subclass */
|
68 |
String appName = null; |
69 |
/** The Class for the actual applet type */
|
70 |
Class ac = null; |
71 |
/** The Applet instance we are running, or null. Can not be a JApplet
|
72 |
* until all the entire world is converted to JApplet. */
|
73 |
Applet ai = null; |
74 |
/** The width of the Applet */
|
75 |
final int WIDTH = 250; |
76 |
/** The height of the Applet */
|
77 |
final int HEIGHT = 200; |
78 |
|
79 |
/** Main is where it all starts.
|
80 |
* Construct the GUI. Load the Applet. Start it running.
|
81 |
*/
|
82 |
public static void main(String[] av) { |
83 |
if (av.length==0) |
84 |
{ |
85 |
System.err.println("No class name specified\n"); |
86 |
System.exit(1); |
87 |
} |
88 |
|
89 |
new AppletViewer(av[0]); |
90 |
} |
91 |
|
92 |
/** Construct the GUI for an Applet Viewer */
|
93 |
AppletViewer(String appName) {
|
94 |
super();
|
95 |
|
96 |
this.appName = appName;
|
97 |
|
98 |
f = new JFrame(appName); |
99 |
f.addWindowListener(new WindowAdapter() { |
100 |
public void windowClosing(WindowEvent e) { |
101 |
f.setVisible(false);
|
102 |
f.dispose(); |
103 |
System.exit(0); |
104 |
} |
105 |
}); |
106 |
Container cp = f.getContentPane();
|
107 |
cp.setLayout(new BorderLayout()); |
108 |
|
109 |
// Instantiate the AppletAdapter which gives us
|
110 |
// AppletStub and AppletContext.
|
111 |
if (aa == null) |
112 |
aa = new AppletAdapter();
|
113 |
|
114 |
// The AppletAdapter also gives us showStatus.
|
115 |
// Therefore, must add() it very early on, since the Applet's
|
116 |
// Constructor or its init() may use showStatus()
|
117 |
cp.add(BorderLayout.SOUTH, aa);
|
118 |
|
119 |
showStatus("Loading Applet " + appName);
|
120 |
|
121 |
loadApplet(appName , WIDTH, HEIGHT); // sets ac and ai
|
122 |
if (ai == null) |
123 |
return;
|
124 |
|
125 |
// Now right away, tell the Applet how to find showStatus et al.
|
126 |
ai.setStub(aa); |
127 |
|
128 |
// Connect the Applet to the Frame.
|
129 |
cp.add(BorderLayout.CENTER, ai);
|
130 |
|
131 |
Dimension d = ai.getSize();
|
132 |
d.height += aa.getSize().height; |
133 |
f.setSize(d); |
134 |
f.setVisible(true); // make the Frame and all in it appear |
135 |
|
136 |
showStatus("Applet " + appName + " loaded"); |
137 |
|
138 |
// Here we pretend to be a browser!
|
139 |
ai.init(); |
140 |
ai.start(); |
141 |
} |
142 |
|
143 |
/*
|
144 |
* Load the Applet into memory. Should do caching.
|
145 |
*/
|
146 |
void loadApplet(String appletName, int w, int h) { |
147 |
// appletName = ... extract from the HTML CODE= somehow ...;
|
148 |
// width = ditto
|
149 |
// height = ditto
|
150 |
try {
|
151 |
// get a Class object for the Applet subclass
|
152 |
ac = Class.forName(appletName);
|
153 |
// Construct an instance (as if using no-argument constructor)
|
154 |
ai = (Applet) ac.newInstance();
|
155 |
} catch(ClassNotFoundException e) { |
156 |
showStatus("Applet subclass " + appletName + " did not load"); |
157 |
return;
|
158 |
} catch (Exception e ){ |
159 |
showStatus("Applet " + appletName + " did not instantiate"); |
160 |
return;
|
161 |
} |
162 |
ai.setSize(w, h); |
163 |
} |
164 |
|
165 |
public void showStatus(String s) { |
166 |
aa.getAppletContext().showStatus(s); |
167 |
} |
168 |
} |
169 |
|
170 |
/*
|
171 |
* AppletAdaptor: partial implementation of AppletStub and AppletContext.
|
172 |
*
|
173 |
* This code is far from finished, as you will see.
|
174 |
*
|
175 |
* @author Ian Darwin, http://www.darwinsys.com/, for Learning Tree Course 478
|
176 |
*/
|
177 |
class AppletAdapter extends Panel implements AppletStub, AppletContext { |
178 |
/** The status window at the bottom */
|
179 |
Label status = null; |
180 |
|
181 |
/** Construct the GUI for an Applet Status window */
|
182 |
AppletAdapter() { |
183 |
super();
|
184 |
|
185 |
// Must do this very early on, since the Applet's
|
186 |
// Constructor or its init() may use showStatus()
|
187 |
add(status = new Label()); |
188 |
|
189 |
// Give "status" the full width
|
190 |
status.setSize(getSize().width, status.getSize().height); |
191 |
|
192 |
showStatus("AppletAdapter constructed"); // now it can be said |
193 |
} |
194 |
|
195 |
/****************** AppletStub ***********************/
|
196 |
/** Called when the applet wants to be resized. */
|
197 |
public void appletResize(int w, int h) { |
198 |
// applet.setSize(w, h);
|
199 |
} |
200 |
|
201 |
/** Gets a reference to the applet's context. */
|
202 |
public AppletContext getAppletContext() { |
203 |
return this; |
204 |
} |
205 |
|
206 |
/** Gets the base URL. */
|
207 |
public URL getCodeBase() { |
208 |
return getClass().getResource("."); |
209 |
} |
210 |
|
211 |
/** Gets the document URL. */
|
212 |
public URL getDocumentBase() { |
213 |
return getClass().getResource("."); |
214 |
} |
215 |
|
216 |
/** Returns the value of the named parameter in the HTML tag. */
|
217 |
public String getParameter(String name) { |
218 |
String value = null; |
219 |
return value;
|
220 |
} |
221 |
/** Determines if the applet is active. */
|
222 |
public boolean isActive() { |
223 |
return true; |
224 |
} |
225 |
|
226 |
/************************ AppletContext ************************/
|
227 |
|
228 |
/** Finds and returns the applet with the given name. */
|
229 |
public Applet getApplet(String an) { |
230 |
return null; |
231 |
} |
232 |
|
233 |
/** Finds all the applets in the document */
|
234 |
public Enumeration<Applet> getApplets() { |
235 |
class AppletLister implements Enumeration<Applet> { |
236 |
public boolean hasMoreElements() { |
237 |
return false; |
238 |
} |
239 |
public Applet nextElement() { |
240 |
return null; |
241 |
} |
242 |
} |
243 |
return new AppletLister(); |
244 |
} |
245 |
|
246 |
/** Create an audio clip for the given URL of a .au file */
|
247 |
public AudioClip getAudioClip(URL u) { |
248 |
return null; |
249 |
} |
250 |
|
251 |
/** Look up and create an Image object that can be paint()ed */
|
252 |
public Image getImage(URL u) { |
253 |
return null; |
254 |
} |
255 |
|
256 |
/** Request to overlay the current page with a new one - ignored */
|
257 |
public void showDocument(URL u) { |
258 |
} |
259 |
|
260 |
/** as above but with a Frame target */
|
261 |
public void showDocument(URL u, String frame) { |
262 |
} |
263 |
|
264 |
/** Called by the Applet to display a message in the bottom line */
|
265 |
public void showStatus(String msg) { |
266 |
if (msg == null) |
267 |
msg = "";
|
268 |
status.setText(msg); |
269 |
} |
270 |
|
271 |
/* StreamKey stuff - new in JDK1.4 */
|
272 |
Map<String, InputStream> streamMap = new HashMap<String, InputStream>(); |
273 |
|
274 |
/** Associate the stream with the key. */
|
275 |
public void setStream(String key, InputStream stream) throws IOException { |
276 |
streamMap.put(key, stream); |
277 |
} |
278 |
|
279 |
public InputStream getStream(String key) { |
280 |
return streamMap.get(key);
|
281 |
} |
282 |
|
283 |
public Iterator<String> getStreamKeys() { |
284 |
return streamMap.keySet().iterator();
|
285 |
} |
286 |
} |