import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.beans.*;

public class BeanTest extends Applet implements MouseListener{
	CustomBean cBean;
	Button b;
	Button bLabel;     
	     
    public void init() {
	setLayout(new BorderLayout());
	cBean = new CustomBean();
        b = new Button("Red&Green");
        b.addMouseListener(this);
        bLabel = new Button("Yellow");              
        bLabel.addMouseListener(this);     	
        Color tempColor=cBean.getColor();
        String iniColor="red";
        if(tempColor==Color.green)
        	iniColor="green";      
        BeanListener bl = new BeanListener(iniColor);
        cBean.addVetoableChangeListener(bl);
        add("Center", cBean);
	add("South",b);
    	add("North",bLabel);
    	add("East",bl);     
   }

    public void destroy() {
        remove(cBean);
        remove(b);
    }

    public void mousePressed(MouseEvent e) 
    {
	try
	{
	if(e.getSource()==b)
	{
	if(cBean.getColor()==Color.red)
        cBean.setColor(Color.green);
        else
        cBean.setColor(Color.red);
 	}
	else if(e.getSource()==bLabel)
	{
        cBean.setColor(Color.yellow);
	}
	}
	catch(Exception except){}
   }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public static void main(String args[]) {
	Frame f = new Frame("BeanTest");
	BeanTest bTest = new BeanTest();
	bTest.init();
	bTest.start();

	f.add("Center", bTest);
	f.setSize(300, 300);
	f.show();
    }
    public String getAppletInfo() {
        return "A simple bean test program.";
    }
}

class BeanListener extends Panel                                    
                  implements VetoableChangeListener 
{      
    TextField s;      
                                                   
    public BeanListener(String Lab) 
    {                         
 	setLayout(new GridLayout(4,1));
 	add(new Label("I'm color Vetolistener"));                                                                              
 	add(new Label("you must be red or green"));                                                                              
 	add(new Label("I know now you are"));                                                                              
 	add(s = new TextField(Lab, 20));
   }
      
     
    public void vetoableChange(PropertyChangeEvent evt)
                    throws PropertyVetoException
    {
    	String propertyName=evt.getPropertyName();
    	if(propertyName.equalsIgnoreCase("color"))
    	{
    		String curColor="red";
    		if((Color)(evt.getNewValue())==Color.yellow)
    		{
    			throw new PropertyVetoException("price<0",evt);
    		}
    		else
    		{
    			if((Color)(evt.getNewValue())==Color.green)
    			     curColor="green";
    			s.setText(curColor);
    		}
    	}
    }  
}                                                                  