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


// @(#)ComponentWidget.java, 1.13, 10/25/96





package marimba.gui;





import java.awt.*;


import java.util.*;





import marimba.persist.*;





/**


 * A wrapper widget for AWT components.


 *


 * @author	Arthur van Hoff


 * @version 	1.13, 10/25/96


 */


public class ComponentWidget extends AWTWidget {


    public final static Font msgFont = new Font("Dialog", Font.PLAIN, 10);





    /**


     * The label for this component.


     * @see #getLabel


     * @see #setLabel


     */


    public String  label = "AWT";





    /**


     * The string describing what the component is.


     * @see #getComponentSource


     * @see #setComponentSource


     */


    public String  component = "java.awt.Canvas";





    /**


     * Get the properties of this widget.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


	list.setString("component", component, "java.awt.Canvas");


	list.setString("label", label, "AWT");


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


	component = list.getString("component", "java.awt.Canvas");


	label = list.getString("label", "AWT");


    }





    /**


     * Get the label for this component.


     * @see #label


     */


    public String getLabel() {


	return label;


    }





    /**


     * Set the label for this component.


     * @see #label


     */


    public void setLabel(String label) {


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


	    this.label = label;


	    stop();


	    init();


	    start();


	}


    }





    /**


     * Get the source of the component.


     * @see #component


     */


    public String getComponentSource() {


	return component;


    }





    /**


     * Set the source of the component.


     * This will actually change the component.


     * @see #component


     */


    public void setComponentSource(String component) {


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


	    this.component = component;


	    stop();


	    destroy();


	    init();


	    start();


	    repaint();


	}


    }





    /**


     * Create the AWT component.


     */


    public Component newComponent() {


	try {


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


	} catch (ClassNotFoundException e) {


	    return null;


	} catch (Exception e) {


	    return null;


	}


    }





    /**


     * Initialize the component.


     */


    public void init() {


	super.init();


	if (comp instanceof Button) {


	    ((Button)comp).setLabel(label);


	} else if (comp instanceof Checkbox) {


	    ((Checkbox)comp).setLabel(label);


	} else if (comp instanceof Label) {


	    ((Label)comp).setText(label);


	} else if (comp instanceof Choice) {


	    for (StringTokenizer t = new StringTokenizer(label, ",") ; t.hasMoreTokens() ;) {


		((Choice)comp).addItem(t.nextToken());


	    }


	} else if (comp instanceof TextComponent) {


	    ((TextComponent)comp).setText(label);


	}


    }





    /**


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


     */


    public void paint(Graphics g) {


	if (getPlayerPanel().editing()) {


	    FontMetrics fm = getFontMetrics(msgFont);


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





	    String str = null;


	    if (comp == null) {


		str = "Instantiation Error";


	    } else {


		str = comp.getClass().getName();


	    }





	    g.setFont(msgFont);


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


	}


    }


}


