import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; public class URLAppletViewer extends Frame implements AppletContext { Applet theApplet = null; SimpleAppletStub theStub = null; Panel appletPanel = new Panel(); TextField appletName = new TextField(35); Button loadButton = new Button("Load"); Button removeButton = new Button("Remove"); Button reloadButton = new Button("Reload"); Button restartButton = new Button("Restart"); Vector theApplets = new Vector(); TextField status = new TextField(33); public static void main(String[] args) { URLAppletViewer uav = new URLAppletViewer(); uav.show(); System.setSecurityManager(new SimpleSecurityManager()); } public URLAppletViewer() { super("URL Applet Viewer"); init(); resize(400, 250); } protected void init() { Panel p1 = new Panel(); p1.setLayout(new GridLayout(3, 1)); Panel p0 = new Panel(); p0.setLayout(new FlowLayout(FlowLayout.LEFT)); p0.add(new Label("URL: ")); p0.add(appletName); p1.add(p0); Panel p2 = new Panel(); p2.add(loadButton); p2.add(removeButton); p2.add(reloadButton); p2.add(restartButton); p1.add(p2); Panel p4 = new Panel(); p4.setLayout(new FlowLayout(FlowLayout.LEFT)); p4.add(new Label("Status: ")); status.setEditable(false); p4.add(status); p1.add(p4); add("South", p1); appletPanel.setLayout(new BorderLayout()); add("Center", appletPanel); } public void loadApplet(String name) { removeApplet(); // is the name a URL? try { URL u = new URL(name); loadApplet(u); return; } catch(MalformedURLException e) { } removeApplet(); if (name != null && !name.equals("")) { try { Class c = Class.forName(name); theApplet = (Applet) c.newInstance(); appletPanel.add("Center", theApplet); theApplets.addElement(theApplet); theApplet.init(); theApplet.start(); validate(); } catch (Exception e) { showStatus(e.toString()); } } } public void loadApplet(URL u) { Class c = null; removeApplet(); try { URLClassLoader ucl = new URLClassLoader(u); String appletname = u.getFile(); appletname = appletname.substring(appletname.lastIndexOf('/')+1); showStatus("Loading " + appletname); c = ucl.loadClass(appletname); theApplet = (Applet) c.newInstance(); theStub = new SimpleAppletStub(this); theApplet.setStub(theStub); appletPanel.add("Center", theApplet); theApplets.addElement(theApplet); theApplet.init(); theStub.activate(); theApplet.start(); validate(); repaint(); } catch (Exception e) { showStatus(e.toString()); } } public void removeApplet() { if (theApplet != null) { theApplet.stop(); theApplet.destroy(); theApplets.removeElement(theApplet); appletPanel.remove(theApplet); theApplet = null; validate(); repaint(); } } public void reload() { String name = appletName.getText(); if (theApplet != null) { Class c = theApplet.getClass(); name = c.getName(); removeApplet(); loadApplet(name); } } public void restart() { if (theApplet != null) { theApplet.stop(); theApplet.start(); } else loadApplet(appletName.getText()); } public boolean action(Event e, Object what) { if (e.target == restartButton) { restart(); return true; } else if (e.target == reloadButton) { reload(); return true; } else if (e.target == loadButton) { loadApplet(appletName.getText()); return true; } else if (e.target == removeButton) { removeApplet(); return true; } else if (e.target == appletName) { loadApplet(appletName.getText()); return true; } return false; } public Image getImage(URL u) { return Toolkit.getDefaultToolkit().getImage(u); } public AudioClip getAudioClip(URL u) { return new NullAudioClip(); } public Applet getApplet(String s) { return null; } public Enumeration getApplets() { return (Enumeration) theApplets; } public void showDocument(URL url) {}; public void showDocument(URL url, String target) {}; public void showStatus(String s) { status.setText(s); } }