// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)AppletWidget.java, 1.16, 11/15/96





package marimba.gui;





import java.io.*;


import java.net.*;


import java.awt.*;


import java.util.*;


import java.applet.*;





import marimba.util.*;


import marimba.persist.*;





/**


 * A wrapper widget for applets.


 *


 * @author	Arthur van Hoff


 * @version 	1.16, 11/15/96


 */


public class AppletWidget extends AWTWidget implements AppletStub, AppletContext {


    /**


     * The parameters for the applet stored in a properties object.


     * @see #paramaters


     */


    public Properties param = new Props(null, null);





    /**


     * The applet code.


     * @see #getCode


     * @see #setCode


     */


    public String code = "java.applet.Applet";





    /**


     * The parameters for the applet.


     * @see #getParameters


     * @see #setParameters


     */


    public String parameters = "";








    /**


     * Get the properties of this widget.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


	list.setString("code", code, "java.applet.Applet");


	list.setString("parameters", parameters, "");


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


	parameters = list.getString("parameters", "");


	code = list.getString("code", "java.applet.Applet");


	param = new Properties();


	try {


	    param.load(new StringBufferInputStream(parameters));


	} catch (IOException e) {


	    e.printStackTrace();


	}


    }





    /**


     * Get the code of this applet.


     * @see #code


     */


    public String getCode() {


	return code;


    }





    /**


     * Set the code of the applet.


     * This will actually change the applet displayed.


     * @see #code


     */


    public void setCode(String code) {


	if (code != null && !code.equals(this.code)) {


	    this.code = code;


	    stop();


	    destroy();


	    init();


	    start();


	    repaint();


	}


    }





    /**


     * Get the parameters of this widget (as a string seperated by


     * newlines).


     * @see #parameters


     */


    public String getParameters() {


	return parameters;


    }





    /**


     * Set the parameters for this widget (as a string seperated by


     * newlines).


     * @see #parameters


     */


    public void setParameters(String parameters) {


	if (parameters == null) {


	    parameters = "";


	}


	if (!this.parameters.equals(parameters)) {


	    this.parameters = parameters;


	    param = new Props(null, null);


	    try {


		param.load(new StringBufferInputStream(parameters));


	    } catch (IOException e) {


		e.printStackTrace();


	    }


	    if (comp.isVisible()) {


		stop();


		destroy();


		init();


		start();


	    } else {


		destroy();


		init();


	    }


	}


    }





    /**


     * Get the editor for this widget.


     */


    public String getEditor() {


	return "marimba.builder.AppletWidgetEditor";


    }





    /**


     * Create the AWT component.


     */


    public Component newComponent() {


	try {


	    return (Component)getPresentation().newInstance(code);


	} catch (Throwable e) {


	    e.printStackTrace();


	    return null;


	}


    }


    


    /**


     * Initialize the applet.


     */


    public void init() {


	super.init();





	if (comp instanceof Applet) {


	    comp.resize(width, height);


	    try {


		Applet applet = (Applet)comp;


		applet.setStub(this);


		applet.init();


		applet.validate();


	    } catch (ThreadDeath e) {


		throw e;


	    } catch (Throwable e) {


		e.printStackTrace();


	    }


	}


    }





    /**


     * Start the applet.


     */


    public void start() {


	super.start();





	if (comp instanceof Applet) {


	    comp.resize(width, height);


	    try {


		Applet applet = (Applet)comp;


		applet.start();


		applet.validate();


	    } catch (ThreadDeath e) {


		throw e;


	    } catch (Throwable e) {


		e.printStackTrace();


	    }


	}


    }





    /**


     * Stop the applet.


     */


    public void stop() {


	if (comp instanceof Applet) {


	    try {


		((Applet)comp).stop();


	    } catch (ThreadDeath e) {


		throw e;


	    } catch (Throwable e) {


		e.printStackTrace();


	    }


	}





	super.stop();


    }





    /**


     * Destroy the applet.


     */


    public void destroy() {


	if (comp instanceof Applet) {


	    try {


		((Applet)comp).destroy();


	    } catch (ThreadDeath e) {


		throw e;


	    } catch (Throwable e) {


		e.printStackTrace();


	    }


	}





	super.destroy();


    }





    /**


     * Paint the widget (if the awt component is not visible).


     */


    public void paint(Graphics g) {


	if (getPlayerPanel().editing()) {


	    FontMetrics fm = getFontMetrics(ComponentWidget.msgFont);


	    g.drawRect(0, 0, width-1, height-1);





	    if (editor == null) {


		String str = null;


		if (comp == null) {


		    str = "Instantiation Error";


		} else {


		    str = "Applet: " + code;


		}


		


		g.setFont(ComponentWidget.msgFont);


		g.drawString(str, 5, 5 + fm.getAscent());


	    }


	}


    }





    // AppletStub routines


    


    public boolean isActive() {


	return (getPlayerPanel() != null) && isShowing();


    }


    


    public URL getDocumentBase() {


	return getPresentation().getBase();


    }





    public URL getCodeBase() {


	return getPresentation().getBase();


    }





    public String getParameter(String name) {


	name = name.toLowerCase();


	if ("width".equals(name)) {


	    return String.valueOf(width);


	}


	if ("height".equals(name)) {


	    return String.valueOf(height);


	}


	if ("code".equals(name)) {


	    return code;


	}


	return param.getProperty(name);


    }





    public AppletContext getAppletContext() {


	return this;


    }





    public void appletResize(int width, int height) {


    }





    // AppletContext routines





    public AudioClip getAudioClip(URL url) {


	return getAudioClip(url.toExternalForm());


    }





    public Image getImage(URL url) {


	return getImage(url.toExternalForm());


    }





    public Applet getApplet(String name) {


	name = name.toLowerCase();


	for (Enumeration e = getApplets() ; e.hasMoreElements() ; ) {


	    Applet applet = (Applet)e.nextElement();


	    String nm = applet.getParameter("name");


	    if ((nm != null) && nm.toLowerCase().equals(name)) {


		return applet;


	    }


	}


	return null;


    }





    public Enumeration getApplets() {


	Vector v = new Vector();


	getApplets(getPresentation(), v);


	return v.elements();


    }





    void getApplets(Widget w, Vector v) {


	if (w instanceof AppletWidget) {


	    v.addElement(((AppletWidget)w).comp);


	}


	for (int i = 0 ; i < w.nwidgets ; i++) {


	    getApplets(w.widgets[i], v);


	}


    }





    public void showDocument(URL url) {


	Environment.showURL(url);


    }





    public void showDocument(URL url, String target) {


	Environment.showURL(url);


    }





    public void showStatus(String status) {


	System.err.println(status);


    }


}


