1 |
/* AppletViewer.java - a viewer for Java applets |
/* AppletViewer.java - a viewer for Java applets |
2 |
Copyright (C) 2003 Thomas Fitzsimmons <fitzsim@redhat.com> |
Copyright (C) 2003 Thomas Fitzsimmons <fitzsim@redhat.com> |
3 |
|
|
4 |
This file is part of gcjappletviewer. |
This file is part of GCJ Applet Viewer. |
5 |
|
|
6 |
gcjappletviewer is free software; you can redistribute it and/or |
GCJ Applet Viewer is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU General Public License as |
modify it under the terms of the GNU General Public License as |
8 |
published by the Free Software Foundation; either version 2 of the |
published by the Free Software Foundation; either version 2 of the |
9 |
License, or (at your option) any later version. |
License, or (at your option) any later version. |
10 |
|
|
11 |
gcjappletviewer is distributed in the hope that it will be useful, |
GCJ Applet Viewer is distributed in the hope that it will be |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
General Public License for more details. |
GNU General Public License for more details. |
15 |
|
|
16 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
17 |
along with gcjappletviewer; if not, write to the Free Software |
along with GCJ Applet Viewer; if not, write to the Free Software |
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
|
|
|
Linking this library statically or dynamically with other modules |
|
|
is making a combined work based on this library. Thus, the terms |
|
|
and conditions of the GNU General Public License cover the whole |
|
|
combination. |
|
|
|
|
|
As a special exception, the copyright holders of this library give |
|
|
you permission to link this library with independent modules to |
|
|
produce an executable, regardless of the license terms of these |
|
|
independent modules, and to copy and distribute the resulting |
|
|
executable under terms of your choice, provided that you also meet, |
|
|
for each linked independent module, the terms and conditions of the |
|
|
license of that module. An independent module is a module which is |
|
|
not derived from or based on this library. If you modify this |
|
|
library, you may extend this exception to your version of the |
|
|
library, but you are not obligated to do so. If you do not wish to |
|
|
do so, delete this exception statement from your version. |
|
19 |
*/ |
*/ |
20 |
|
|
21 |
package gnu.gcjwebplugin; |
package gnu.gcjwebplugin; |
22 |
|
|
23 |
|
import java.io.*; |
24 |
import gnu.java.awt.EmbeddedWindow; |
import gnu.java.awt.EmbeddedWindow; |
25 |
import gnu.getopt.Getopt; |
import gnu.getopt.Getopt; |
26 |
import gnu.getopt.LongOpt; |
import gnu.getopt.LongOpt; |
33 |
import java.awt.MenuItem; |
import java.awt.MenuItem; |
34 |
import java.awt.event.WindowAdapter; |
import java.awt.event.WindowAdapter; |
35 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
|
import java.io.IOException; |
|
36 |
import java.util.Map; |
import java.util.Map; |
37 |
import java.util.HashMap; |
import java.util.HashMap; |
38 |
import java.util.List; |
import java.util.List; |
39 |
import java.util.Iterator; |
import java.util.Iterator; |
40 |
import java.util.ArrayList; |
import java.util.ArrayList; |
41 |
import java.net.URL; |
import java.net.*; |
|
import java.net.URLClassLoader; |
|
42 |
|
|
43 |
public class AppletViewer extends WindowAdapter |
public class AppletViewer |
44 |
{ |
{ |
45 |
|
Frame appletWindow; |
46 |
|
URLClassLoader loader; |
47 |
|
AppletTag tag; |
48 |
|
Applet applet; |
49 |
|
|
50 |
|
AppletViewer () |
51 |
|
{ |
52 |
|
} |
53 |
|
|
54 |
public class ViewerWindow extends Frame |
public class ViewerWindow extends Frame |
55 |
{ |
{ |
56 |
public ViewerWindow (String title, int width, int height, Applet applet) |
public ViewerWindow (String title, int width, int height, Applet applet) |
58 |
setLayout (new BorderLayout()); |
setLayout (new BorderLayout()); |
59 |
add (applet, BorderLayout.CENTER); |
add (applet, BorderLayout.CENTER); |
60 |
|
|
61 |
|
addWindowListener (new WindowAdapter () |
62 |
|
{ |
63 |
|
public void windowClosing (WindowEvent event) |
64 |
|
{ |
65 |
|
applet.stop(); |
66 |
|
appletWindow.hide(); |
67 |
|
System.exit (0); |
68 |
|
} |
69 |
|
}); |
70 |
|
|
71 |
addNotify(); |
addNotify(); |
72 |
|
|
73 |
MenuBar menuBar = new MenuBar(); |
MenuBar menuBar = new MenuBar(); |
75 |
menuBar.add (menuFile); |
menuBar.add (menuFile); |
76 |
menuFile.add (new MenuItem ("Quit")); |
menuFile.add (new MenuItem ("Quit")); |
77 |
setMenuBar (menuBar); |
setMenuBar (menuBar); |
|
|
|
|
setSize (width, height); |
|
|
setVisible (true); |
|
|
} |
|
|
} |
|
|
|
|
|
public class PluginWindow extends EmbeddedWindow |
|
|
{ |
|
|
public PluginWindow (int window_id, int width, int height, Applet applet) |
|
|
{ |
|
|
super (window_id); |
|
|
|
|
|
addNotify(); |
|
|
|
|
|
setLayout (new BorderLayout()); |
|
|
add (applet, BorderLayout.CENTER); |
|
78 |
|
|
79 |
setSize (width, height); |
setSize (width, height); |
80 |
setVisible (true); |
setVisible (true); |
81 |
} |
} |
82 |
} |
} |
83 |
|
|
84 |
static List appletTags = new ArrayList(); |
static List appletTags = new ArrayList(); |
85 |
|
|
|
Applet applet; |
|
|
AppletTag tag; |
|
|
Frame appletWindow; |
|
|
|
|
86 |
public AppletViewer (AppletTag tag) |
public AppletViewer (AppletTag tag) |
87 |
throws IOException |
throws IOException |
88 |
{ |
{ |
94 |
tag.dimensions.width, |
tag.dimensions.width, |
95 |
tag.dimensions.height, |
tag.dimensions.height, |
96 |
applet); |
applet); |
|
appletWindow.addWindowListener (this); |
|
97 |
|
|
98 |
applet.init(); |
applet.init(); |
99 |
applet.validate(); |
applet.validate(); |
100 |
applet.start(); |
applet.start(); |
101 |
} |
} |
102 |
|
|
|
public void windowClosing (WindowEvent event) |
|
|
{ |
|
|
applet.stop(); |
|
|
appletWindow.hide(); |
|
|
System.exit (0); |
|
|
} |
|
|
|
|
103 |
public static Applet createApplet(AppletTag tag) |
public static Applet createApplet(AppletTag tag) |
104 |
{ |
{ |
105 |
Applet applet = null; |
Applet applet = null; |
207 |
Math.max (0, Integer.parseInt(opts.getOptarg())); |
Math.max (0, Integer.parseInt(opts.getOptarg())); |
208 |
} |
} |
209 |
else if (optionIndex == 4) // --plugin |
else if (optionIndex == 4) // --plugin |
210 |
pluginMode = true; |
{ |
211 |
|
pluginMode = true; |
212 |
|
break; |
213 |
|
} |
214 |
else if (optionIndex == 5) // --help |
else if (optionIndex == 5) // --help |
215 |
printHelpMessage (0); |
printHelpMessage (0); |
216 |
} |
} |
217 |
} |
} |
218 |
|
|
219 |
|
if (pluginMode) |
220 |
|
PluginAppletViewer.start (System.in, System.out); |
221 |
|
|
222 |
if (dimensions.height < 0) |
if (dimensions.height < 0) |
223 |
dimensions.height = 200; |
dimensions.height = 200; |
224 |
|
|
237 |
|
|
238 |
for (int i = optind; i < args.length; i++) |
for (int i = optind; i < args.length; i++) |
239 |
{ |
{ |
240 |
|
// If this argument doesn't end in a file separator and there |
241 |
|
// is no dot between the last file separator and the end of |
242 |
|
// the string, then assume we're dealing with a class file and |
243 |
|
// append the .class extension. |
244 |
|
String basename = |
245 |
|
args[i].substring(args[i].lastIndexOf(File.separatorChar) + 1, |
246 |
|
args[i].length()); |
247 |
|
|
248 |
|
if (!basename.equals ("") |
249 |
|
&& basename.lastIndexOf ('.') < 0) |
250 |
|
args[i] = args[i] + ".class"; |
251 |
|
|
252 |
if (args[i].endsWith(".class")) |
if (args[i].endsWith(".class")) |
253 |
{ |
{ |
254 |
classFileGiven = true; |
classFileGiven = true; |
298 |
} |
} |
299 |
} |
} |
300 |
} |
} |
301 |
|
|
302 |
|
class PluginAppletViewer extends AppletViewer |
303 |
|
{ |
304 |
|
PluginAppletViewer () |
305 |
|
{ |
306 |
|
} |
307 |
|
|
308 |
|
public class PluginInstance |
309 |
|
{ |
310 |
|
public Frame window; |
311 |
|
public URLClassLoader loader; |
312 |
|
public AppletTag tag; |
313 |
|
public Applet applet; |
314 |
|
|
315 |
|
public PluginInstance () |
316 |
|
{ |
317 |
|
} |
318 |
|
|
319 |
|
public void setFrame (int xid) |
320 |
|
{ |
321 |
|
window = new EmbeddedWindow (xid); |
322 |
|
|
323 |
|
window.addNotify(); |
324 |
|
|
325 |
|
window.setLayout (new BorderLayout()); |
326 |
|
|
327 |
|
applet = createApplet (tag); |
328 |
|
window.add (applet, BorderLayout.CENTER); |
329 |
|
|
330 |
|
window.setSize (200, 300); |
331 |
|
window.setVisible (true); |
332 |
|
|
333 |
|
applet.init(); |
334 |
|
applet.setVisible (true); |
335 |
|
applet.validate(); |
336 |
|
applet.start(); |
337 |
|
} |
338 |
|
|
339 |
|
public void setTag (String tag, String documentbase) |
340 |
|
throws MalformedURLException, IOException |
341 |
|
{ |
342 |
|
this.tag = AppletTag.parseNextTag (new StreamTokenizer (new StringReader(tag)), |
343 |
|
AppletTag.locationToURL (documentbase)); |
344 |
|
} |
345 |
|
} |
346 |
|
|
347 |
|
// A mapping of instance IDs to PluginInstances. |
348 |
|
static HashMap appletWindows = new HashMap (); |
349 |
|
|
350 |
|
private static BufferedReader stdin; |
351 |
|
private static BufferedWriter stdout; |
352 |
|
|
353 |
|
public static void start (InputStream inputStream, OutputStream outputStream) |
354 |
|
throws MalformedURLException, IOException |
355 |
|
{ |
356 |
|
// Set up input and output pipes. |
357 |
|
stdin = new BufferedReader (new InputStreamReader (inputStream)); |
358 |
|
stdout = new BufferedWriter (new OutputStreamWriter (outputStream)); |
359 |
|
|
360 |
|
write ("running"); |
361 |
|
|
362 |
|
String input = read (); |
363 |
|
PluginInstance currentInstance = null; |
364 |
|
|
365 |
|
while (!input.equals ("shutdown")) |
366 |
|
{ |
367 |
|
// FIXME: this instance scheme won't work. |
368 |
|
if (input.equals ("instance")) |
369 |
|
{ |
370 |
|
// Read applet instance identifier. |
371 |
|
String key = read (); |
372 |
|
|
373 |
|
if (appletWindows.get (key) == null) |
374 |
|
appletWindows.put (key, new PluginInstance ()); |
375 |
|
|
376 |
|
currentInstance = (PluginInstance) appletWindows.get (key); |
377 |
|
} |
378 |
|
else if (input.equals ("tag")) |
379 |
|
{ |
380 |
|
String documentbase = read (); |
381 |
|
String tag = read (); |
382 |
|
|
383 |
|
currentInstance.setTag (tag, documentbase); |
384 |
|
} |
385 |
|
else if (input.equals ("xid")) |
386 |
|
{ |
387 |
|
int xid = Integer.parseInt (read ()); |
388 |
|
|
389 |
|
currentInstance.setFrame (xid); |
390 |
|
} |
391 |
|
input = read (); |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
|
static void write (String output) |
396 |
|
throws IOException |
397 |
|
{ |
398 |
|
// Write string to plugin. |
399 |
|
stdout.write (output, 0, output.length()); |
400 |
|
stdout.newLine (); |
401 |
|
stdout.flush (); |
402 |
|
|
403 |
|
System.err.println (" PIPE: appletviewer wrote: " + output); |
404 |
|
} |
405 |
|
|
406 |
|
static String read () |
407 |
|
throws IOException |
408 |
|
{ |
409 |
|
// Read string from plugin. |
410 |
|
String input = stdin.readLine(); |
411 |
|
|
412 |
|
System.err.println (" PIPE: appletviewer read: " + input); |
413 |
|
|
414 |
|
// Return confirmation string to plugin. |
415 |
|
write (input); |
416 |
|
|
417 |
|
return input; |
418 |
|
} |
419 |
|
} |