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

// Please feel free to use this code as an example.



import java.awt.*;

import marimba.gui.*;

import marimba.persist.*;



/**

 * This example show how you can create a new widget

 * class.

 *

 * @author Arthur van Hoff

 */

public class RoundButtonWidget extends Widget {

    public String   label;

    public boolean  filled;

    public boolean  down;



    /**

     * Get the properties of this widget.

     */

    public void getProperties(PropertyList list) {

	// store super class properties in the list

	super.getProperties(list);



	// store additional properties in the list

	list.setBoolean("filled", filled, false);

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

    }



    /**

     * Set the properties of this widget.

     */

    public void setProperties(PropertyList list) {

	// retrieve super class properties from the list

	super.setProperties(list);



	// retrieve additional properties from list

	filled = list.getBoolean("filled", false);

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

    }



    /**

     * Paint the round button.

     */

    public void paint(Graphics g) {

	FontMetrics fm = g.getFontMetrics();



	// Draw the button

	g.setColor(background);

	if (filled) {

	    Bevel.fillOval(g, 0, 0, width, height, !down, 2);

	} else {

	    Bevel.drawOval(g, 0, 0, width, height, !down, 2);

	}



	// Draw the label

	if (label != null) {

	    g.setColor(foreground);

	    int lx = (width - fm.stringWidth(label))/2;

	    int ly = (height - fm.getHeight())/2 + fm.getAscent();

	    if (down) {

		g.drawString(label, lx+1, ly+1);

	    } else {

		g.drawString(label, lx, ly);

	    }

	}

    }



    /**

     * Handle user events.

     */

    public boolean handleEvent(Event evt) {

	switch (evt.id) {

	  case Event.MOUSE_DOWN:

	    down = true;

	    repaint();

	    return true;

	    

	  case Event.MOUSE_UP:

	    down = false;

	    repaint();

	    return true;

	}

	return super.handleEvent(evt);

    }

}

