/**
 * MyValveOpenEditor.java - MyValveOpenEditor class provides special case property editor 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;

public class MyValveOpenEditor
	extends java.beans.PropertyEditorSupport {

	/**
	 * @return isOpen property value as a string suitable for presentation to a human.
	 * PropertyEditor should be prepared to parse that string back in setAsText(). 
	 */
	public String getAsText() {

		if(isOpen_)	{
			//dbgOut(" getAsText() : on");
			return "on";		
		} else {
			//dbgOut(" getAsText() : off");
			return "off";
		}
	}


	/**
	 * Set isOpen value by parsing a given String. If either the String is
   * badly formatted or if this kind of property can't be expressed as text
	 * raise java.lang.IllegalArgumentException 
   * @param text - the string to be parsed. 
	 */
  public void setAsText(String text) throws IllegalArgumentException {
		
		if(text.equals("on")) {
			//dbgOut(" setAsText() : on");
			isOpen_ = true;
			firePropertyChange();
		} else if (text.equals("off")) {
			//dbgOut(" setAsText() : off");
			isOpen_ = false;
			firePropertyChange();
		} else
			throw new IllegalArgumentException();
	}

	/**
	 * Return array of known tagged values for isOpen property value.
	 * As soon a isOpen PropertyEditor supports tags, then it should
	 * support the use of setAsText with a tag value as a way of setting 
	 * isOpen value. 
	 *
	 * @return  array of tag values for isOpen property
	 */
	
	public String[] getTags() {
		String result[] = {
			"on",
			"off"
		};
		return result;
	}

	/**
	 *   Set (or change) isOpen value that is to be edited. 
	 * @param value - The new target object to be edited. 
	 * Note that this object should not be modified by the PropertyEditor,
	 * rather the PropertyEditor should create a new object 
	 * to hold any modified value. 
	 */
	public void setValue(Object value) {

		isOpen_ = ((Boolean)value).booleanValue();
		//dbgOut(" setValue() : "+isOpen_);
	}

	/**
	 * @return the value of the property. 
	 */
	public Object getValue() {

		//dbgOut(" getValue()");
		return new Boolean(isOpen_);
	}

	/**
	 * Print this module debug info
	 */
	void dbgOut(String msg) {

		System.out.println("MyValveOpenEditor : "+msg);
	}

	private boolean isOpen_ = false;

}

