/**
 * MyValveCustomizer.java - MyValveCustomizer class provides Customizer for a Valve.
 * Copyright (c) 1997 by Dmitri Kondratiev
 *
 * @author <a href="mailto:dima@paragraph.com">Dmitri Kondratiev</a>
 * @version 1.0 Mar 1997
 * History :
 *
 * 10-Mar-97 - Created
 */

package bdktest.water;

import java.awt.*;
import java.beans.*;

public class MyValveCustomizer extends Panel implements Customizer {

  public MyValveCustomizer() {
    
    //dbgOut("MyValveCustomizer()");
	  setLayout(null);
    setBackground(Color.black);
    setForeground(Color.white);
    setFont(new Font("Dialog", Font.PLAIN, 10));
  }

  public void setObject(Object obj) {

    target_ = (MyValve)obj;

    //Build a panel, which contains a checkbox group
    cbg = new CheckboxGroup();

    if(target_.isOpen()) {
      cb1 = new Checkbox("on", cbg, true);
      cb2 = new Checkbox("off", cbg, false);
    } else {
      cb1 = new Checkbox("on", cbg, false);
      cb2 = new Checkbox("off", cbg, true);
    }
    setLayout(new FlowLayout());
    add(new Label("flow"));
    add(cb1);
    add(cb2);
    validate();

  }

  public Dimension preferredSize() {
    return new Dimension(200, 40);
  }

  public boolean handleEvent(Event evt) {

    if (evt.target instanceof Checkbox) {
      Checkbox checked = cbg.getCurrent();
      //dbgOut(checked.getLabel());
        
      if( (checked.getLabel()).equals("on") ) {
        target_.setOpen(true);
      } else {
        target_.setOpen(false);
      }

      support.firePropertyChange("", null, null);
    }
    return (super.handleEvent(evt));
  }

    //----------------------------------------------------------------------

  public void addPropertyChangeListener(PropertyChangeListener l) {
    support.addPropertyChangeListener(l);
  }

  public void removePropertyChangeListener(PropertyChangeListener l) {
    support.removePropertyChangeListener(l);
  }

    //----------------------------------------------------------------------

	/**
	 * Print this module debug info
	 */
	void dbgOut(String msg) {

		System.out.println("MyValveCustomizer : "+msg);
	}

    private PropertyChangeSupport support = new PropertyChangeSupport(this);
    private MyValve target_;
    private Checkbox cb1, cb2; //only one of these two can be selected
    private CheckboxGroup cbg;


}
