/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
import javax.swing.*;
import java.lang.reflect.*;
import javax.swing.event.*;
import java.awt.*;
import java.net.*;
 
public class BooleanFieldAdapter extends JToggleButton.ToggleButtonModel
{
  /**
   * double field
   */
  private Field field = null;

  /**
   * Object this slider is connected to.
   */
  private Object instance = null;

  public BooleanFieldAdapter(Object instance, String fieldName)
  {
    try {
      field = instance.getClass().getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
      throw new Error("No field " + fieldName);
    }

    if (!field.getType().toString().equals("boolean")) {
      throw new Error("Field must be of type boolean");
    }

    try {      
      super.setSelected(field.getBoolean(instance));
    } catch (IllegalAccessException e) {
      throw new Error("");
    }

    this.instance = instance;
  }
  
  public void setSelected(boolean newValue)
  {
    try {
      field.setBoolean(instance, newValue);
    } catch (IllegalAccessException e) {
      throw new Error("");
    }
    super.setSelected(newValue);
  }
}

